From 1d2ec21210351f5fd41ebedca11edf075876e12f Mon Sep 17 00:00:00 2001 From: Yair Morgenstern Date: Thu, 16 Apr 2020 15:58:46 +0300 Subject: [PATCH] There is only one true source of removing constructions from the queue, to which all other functions lead --- .../com/unciv/logic/city/CityConstructions.kt | 28 +++++++++---------- .../src/com/unciv/logic/city/IConstruction.kt | 14 +++++----- core/src/com/unciv/models/ruleset/Building.kt | 4 +-- .../com/unciv/models/ruleset/unit/BaseUnit.kt | 5 ++-- .../unciv/ui/cityscreen/ConstructionsTable.kt | 4 +-- 5 files changed, 26 insertions(+), 29 deletions(-) diff --git a/core/src/com/unciv/logic/city/CityConstructions.kt b/core/src/com/unciv/logic/city/CityConstructions.kt index ace2cdbf..df037d77 100644 --- a/core/src/com/unciv/logic/city/CityConstructions.kt +++ b/core/src/com/unciv/logic/city/CityConstructions.kt @@ -217,7 +217,8 @@ class CityConstructions { } fun addProductionPoints(productionToAdd: Int) { - if (!inProgressConstructions.containsKey(currentConstructionFromQueue)) inProgressConstructions[currentConstructionFromQueue] = 0 + if (!inProgressConstructions.containsKey(currentConstructionFromQueue)) + inProgressConstructions[currentConstructionFromQueue] = 0 inProgressConstructions[currentConstructionFromQueue] = inProgressConstructions[currentConstructionFromQueue]!! + productionToAdd } @@ -351,11 +352,7 @@ class CityConstructions { return cultureBuildingToBuild } - private fun cancelCurrentConstruction() { - currentConstructionIsUserSet = false - constructionQueue.removeAt(0) - chooseNextConstruction() - } + private fun cancelCurrentConstruction() = removeFromQueue(0,true) fun chooseNextConstruction() { if(currentConstructionIsUserSet) return @@ -381,17 +378,18 @@ class CityConstructions { fun removeFromQueue(constructionName: String) { if (constructionName in constructionQueue) - constructionQueue.remove(constructionName) + removeFromQueue(constructionQueue.indexOf(constructionName), true) } - fun removeFromQueue(constructionQueueIndex: Int) { - // constructionQueueIndex -1 is the current construction - if (constructionQueueIndex < 0) { - // To prevent Construction Automation - if (constructionQueue.isEmpty()) constructionQueue.add("Nothing") - cancelCurrentConstruction() - } else - constructionQueue.removeAt(constructionQueueIndex) + /** If this was done automatically, we should automatically try to choose a new construction and treat it as such */ + fun removeFromQueue(constructionQueueIndex: Int, automatic:Boolean) { + constructionQueue.removeAt(constructionQueueIndex) + if (constructionQueue.isEmpty()){ + if(automatic) chooseNextConstruction() + else constructionQueue.add("Nothing") // To prevent Construction Automation + currentConstructionIsUserSet = false + } + else currentConstructionIsUserSet = true // we're just continuing the regular queue } fun raisePriority(constructionQueueIndex: Int) { diff --git a/core/src/com/unciv/logic/city/IConstruction.kt b/core/src/com/unciv/logic/city/IConstruction.kt index 56a8338f..0d2f5db7 100644 --- a/core/src/com/unciv/logic/city/IConstruction.kt +++ b/core/src/com/unciv/logic/city/IConstruction.kt @@ -8,7 +8,7 @@ import kotlin.math.roundToInt interface IConstruction : INamed { fun getProductionCost(civInfo: CivilizationInfo): Int fun getGoldCost(civInfo: CivilizationInfo): Int - fun isBuildable(construction: CityConstructions): Boolean + fun isBuildable(cityConstructions: CityConstructions): Boolean fun shouldBeDisplayed(cityConstructions: CityConstructions): Boolean fun postBuildEvent(construction: CityConstructions, wasBought: Boolean = false): Boolean // Yes I'm hilarious. fun getResource(): String? @@ -27,17 +27,17 @@ open class PerpetualConstruction(override var name: String, val description: Str companion object { const val CONVERSION_RATE: Int = 4 val science = object : PerpetualConstruction("Science", "Convert production to science at a rate of $CONVERSION_RATE to 1") { - override fun isBuildable(construction: CityConstructions): Boolean { - return construction.cityInfo.civInfo.tech.getTechUniques().contains("Enables conversion of city production to science") + override fun isBuildable(cityConstructions: CityConstructions): Boolean { + return cityConstructions.cityInfo.civInfo.tech.getTechUniques().contains("Enables conversion of city production to science") } } val gold = object : PerpetualConstruction("Gold", "Convert production to gold at a rate of $CONVERSION_RATE to 1") { - override fun isBuildable(construction: CityConstructions): Boolean { - return construction.cityInfo.civInfo.tech.getTechUniques().contains("Enables conversion of city production to gold") + override fun isBuildable(cityConstructions: CityConstructions): Boolean { + return cityConstructions.cityInfo.civInfo.tech.getTechUniques().contains("Enables conversion of city production to gold") } } val idle = object : PerpetualConstruction("Nothing", "The city will not produce anything.") { - override fun isBuildable(construction: CityConstructions): Boolean = true + override fun isBuildable(cityConstructions: CityConstructions): Boolean = true override fun getProductionTooltip(cityInfo: CityInfo): String = "" } @@ -58,7 +58,7 @@ open class PerpetualConstruction(override var name: String, val description: Str throw Exception("Impossible!") } - override fun isBuildable(construction: CityConstructions): Boolean { + override fun isBuildable(cityConstructions: CityConstructions): Boolean { throw Exception("Impossible!") } diff --git a/core/src/com/unciv/models/ruleset/Building.kt b/core/src/com/unciv/models/ruleset/Building.kt index 8d51ae12..b2ad901a 100644 --- a/core/src/com/unciv/models/ruleset/Building.kt +++ b/core/src/com/unciv/models/ruleset/Building.kt @@ -334,8 +334,8 @@ class Building : NamedStats(), IConstruction{ return "" } - override fun isBuildable(construction: CityConstructions): Boolean { - return getRejectionReason(construction)=="" + override fun isBuildable(cityConstructions: CityConstructions): Boolean { + return getRejectionReason(cityConstructions)=="" } override fun postBuildEvent(cityConstructions: CityConstructions, wasBought: Boolean): Boolean { diff --git a/core/src/com/unciv/models/ruleset/unit/BaseUnit.kt b/core/src/com/unciv/models/ruleset/unit/BaseUnit.kt index 887b1980..7997b13b 100644 --- a/core/src/com/unciv/models/ruleset/unit/BaseUnit.kt +++ b/core/src/com/unciv/models/ruleset/unit/BaseUnit.kt @@ -1,7 +1,6 @@ package com.unciv.models.ruleset.unit import com.unciv.Constants -import com.unciv.UncivGame import com.unciv.logic.city.CityConstructions import com.unciv.logic.city.IConstruction import com.unciv.logic.civilization.CivilizationInfo @@ -149,8 +148,8 @@ class BaseUnit : INamed, IConstruction { fun isBuildable(civInfo: CivilizationInfo) = getRejectionReason(civInfo)=="" - override fun isBuildable(construction: CityConstructions): Boolean { - return getRejectionReason(construction) == "" + override fun isBuildable(cityConstructions: CityConstructions): Boolean { + return getRejectionReason(cityConstructions) == "" } override fun postBuildEvent(construction: CityConstructions, wasBought: Boolean): Boolean { diff --git a/core/src/com/unciv/ui/cityscreen/ConstructionsTable.kt b/core/src/com/unciv/ui/cityscreen/ConstructionsTable.kt index 3e7e7f7a..d673f116 100644 --- a/core/src/com/unciv/ui/cityscreen/ConstructionsTable.kt +++ b/core/src/com/unciv/ui/cityscreen/ConstructionsTable.kt @@ -269,7 +269,7 @@ class ConstructionsTable(val cityScreen: CityScreen) : Table(CameraStageBaseScre if (!UncivGame.Current.worldScreen.isPlayersTurn || city.isPuppet) button.disable() else { button.onClick { - cityConstructions.removeFromQueue(selectedQueueEntry) + cityConstructions.removeFromQueue(selectedQueueEntry,false) cityScreen.selectedConstruction = null selectedQueueEntry = -2 cityScreen.update() @@ -318,7 +318,7 @@ class ConstructionsTable(val cityScreen: CityScreen) : Table(CameraStageBaseScre // currentConstruction is removed from the queue by purchaseConstruction // to avoid conflicts with NextTurnAutomation if (!constructionIsCurrentConstruction && cityConstructions.constructionQueue[selectedQueueEntry] == construction.name) - cityConstructions.removeFromQueue(selectedQueueEntry) + cityConstructions.removeFromQueue(selectedQueueEntry,false) selectedQueueEntry = -2 cityScreen.selectedConstruction = null }