From fea342a31b29c4ecfb5e044620e45dc23abbc594 Mon Sep 17 00:00:00 2001 From: Federico Luongo Date: Thu, 8 Oct 2020 15:29:56 +0200 Subject: [PATCH] Clear Barbarian Camp quest (#3246) --- .../assets/jsons/Civ V - Vanilla/Quests.json | 3 +- .../unciv/logic/civilization/QuestManager.kt | 42 +++++++++++++++++++ core/src/com/unciv/logic/map/MapUnit.kt | 3 ++ 3 files changed, 46 insertions(+), 2 deletions(-) diff --git a/android/assets/jsons/Civ V - Vanilla/Quests.json b/android/assets/jsons/Civ V - Vanilla/Quests.json index c4991d157f..8501acf407 100644 --- a/android/assets/jsons/Civ V - Vanilla/Quests.json +++ b/android/assets/jsons/Civ V - Vanilla/Quests.json @@ -4,14 +4,13 @@ "description": "Build a road to connect your capital to our city.", "influence": 50 }, - /* { "name": "Clear Barbarian Camp", "description": "We feel threatened by a Barbarian Camp near our city. Please take care of it.", "type": "Global", "influence": 50, "minimumCivs": 1 - },*/ + }, { "name": "Connect Resource", "description": "In order to make our civilizations stronger, connect [tileResource] to your trade network." diff --git a/core/src/com/unciv/logic/civilization/QuestManager.kt b/core/src/com/unciv/logic/civilization/QuestManager.kt index 7f0f8d638c..7f3abfeb83 100644 --- a/core/src/com/unciv/logic/civilization/QuestManager.kt +++ b/core/src/com/unciv/logic/civilization/QuestManager.kt @@ -2,6 +2,7 @@ package com.unciv.logic.civilization import com.badlogic.gdx.graphics.Color import com.badlogic.gdx.math.Vector2 +import com.unciv.Constants import com.unciv.UncivGame import com.unciv.logic.GameInfo import com.unciv.logic.map.TileInfo @@ -248,6 +249,11 @@ class QuestManager { var data2 = "" when (quest.name) { + QuestName.ClearBarbarianCamp.value -> { + val camp = getBarbarianEncampmentForQuest(assignee)!! + data1 = camp.position.x.toInt().toString() + data2 = camp.position.y.toInt().toString() + } QuestName.ConnectResource.value -> data1 = getResourceForQuest(assignee)!!.name QuestName.ConstructWonder.value -> data1 = getWonderToBuildForQuest(assignee)!!.name QuestName.GreatPerson.value -> data1 = getGreatPersonForQuest(assignee)!!.name @@ -287,6 +293,7 @@ class QuestManager { return false return when (quest.name) { + QuestName.ClearBarbarianCamp.value -> getBarbarianEncampmentForQuest(challenger) != null QuestName.Route.value -> civInfo.hasEverBeenFriendWith(challenger) && !civInfo.isCapitalConnectedToCity(challenger.getCapital()) QuestName.ConnectResource.value -> civInfo.hasEverBeenFriendWith(challenger) && getResourceForQuest(challenger) != null QuestName.ConstructWonder.value -> civInfo.hasEverBeenFriendWith(challenger) && getWonderToBuildForQuest(challenger) != null @@ -315,6 +322,7 @@ class QuestManager { private fun isObsolete(assignedQuest: AssignedQuest): Boolean { val assignee = civInfo.gameInfo.getCivilization(assignedQuest.assignee) return when (assignedQuest.questName) { + QuestName.ClearBarbarianCamp.value -> civInfo.gameInfo.tileMap[assignedQuest.data1.toInt(), assignedQuest.data2.toInt()].improvement != Constants.barbarianEncampment QuestName.ConstructWonder.value -> civInfo.gameInfo.getCities().any { it.civInfo != assignee && it.cityConstructions.isBuilt(assignedQuest.data1) } QuestName.FindPlayer.value -> civInfo.gameInfo.getCivilization(assignedQuest.data1).isDefeated() else -> false @@ -340,7 +348,37 @@ class QuestManager { } } + /** + * Gets notified a barbarian camp in [location] has been cleared by [civInfo]. + * Since [QuestName.ClearBarbarianCamp] is a global quest, it could have been assigned to + * multiple civilizations, so after this notification all matching quests are removed. + */ + fun barbarianCampCleared(civInfo: CivilizationInfo, location: Vector2) { + val matchingQuests = assignedQuests.asSequence() + .filter { it.questName == QuestName.ClearBarbarianCamp.value } + .filter { it.data1.toFloat() == location.x && it.data2.toFloat() == location.y } + + val winningQuest = matchingQuests.filter { it.assignee == civInfo.civName }.firstOrNull() + if (winningQuest != null) + giveReward(winningQuest) + + assignedQuests.removeAll(matchingQuests) + } + //region get-quest-target + /** + * Returns a random [TileInfo] containing a Barbarian encampment within 8 tiles of [civInfo] + * to be destroyed + */ + private fun getBarbarianEncampmentForQuest(challenger: CivilizationInfo): TileInfo? { + val encampments = civInfo.getCapital().getCenterTile().getTilesInDistance(8) + .filter { it.improvement == Constants.barbarianEncampment }.toList() + + if (encampments.isNotEmpty()) + return encampments.random() + + return null + } /** * Returns a random resource to be connected to the [challenger]'s trade route as a quest. @@ -456,6 +494,10 @@ class AssignedQuest(val questName: String = "", val game = UncivGame.Current when (questName) { + QuestName.ClearBarbarianCamp.value -> { + game.setWorldScreen() + game.worldScreen.mapHolder.setCenterPosition(Vector2(data1.toFloat(), data2.toFloat()), selectUnit = false) + } QuestName.Route.value -> { game.setWorldScreen() game.worldScreen.mapHolder.setCenterPosition(gameInfo.getCivilization(assigner).getCapital().location, selectUnit = false) diff --git a/core/src/com/unciv/logic/map/MapUnit.kt b/core/src/com/unciv/logic/map/MapUnit.kt index 74e3e3b859..e248a9aace 100644 --- a/core/src/com/unciv/logic/map/MapUnit.kt +++ b/core/src/com/unciv/logic/map/MapUnit.kt @@ -531,6 +531,9 @@ class MapUnit { private fun clearEncampment(tile: TileInfo) { tile.improvement = null + // Notify city states that this unit cleared a Barbarian Encampment, required for quests + civInfo.gameInfo.getAliveCityStates().forEach{ it.questManager.barbarianCampCleared(civInfo, tile.position) } + var goldGained = civInfo.getDifficulty().clearBarbarianCampReward * civInfo.gameInfo.gameParameters.gameSpeed.modifier if (civInfo.hasUnique("Receive triple Gold from Barbarian encampments and pillaging Cities")) goldGained *= 3f