From 59e815c30e1c193dea217f53c04c49b18604f7c3 Mon Sep 17 00:00:00 2001 From: Federico Luongo Date: Sat, 25 Jan 2020 18:50:43 +0100 Subject: [PATCH] Fix turns to construction (#1767) Fix #1759: the stored production is used only for the first construction of its kind in the construction queue --- .../com/unciv/logic/city/CityConstructions.kt | 25 +++++++++++++++---- .../unciv/ui/cityscreen/ConstructionsTable.kt | 10 +++++--- 2 files changed, 27 insertions(+), 8 deletions(-) diff --git a/core/src/com/unciv/logic/city/CityConstructions.kt b/core/src/com/unciv/logic/city/CityConstructions.kt index 1f4a945a5a..50ca14e531 100644 --- a/core/src/com/unciv/logic/city/CityConstructions.kt +++ b/core/src/com/unciv/logic/city/CityConstructions.kt @@ -116,6 +116,7 @@ class CityConstructions { fun isBuilt(buildingName: String): Boolean = builtBuildings.contains(buildingName) fun isBeingConstructed(constructionName: String): Boolean = currentConstruction == constructionName fun isEnqueued(constructionName: String): Boolean = constructionQueue.contains(constructionName) + fun isBeingConstructedOrEnqueued(constructionName: String): Boolean = isBeingConstructed(constructionName) || isEnqueued(constructionName) fun isQueueFull(): Boolean = constructionQueue.size == queueMaxSize fun isQueueEmpty(): Boolean = constructionQueue.isEmpty() @@ -125,6 +126,17 @@ class CityConstructions { return currentConstruction is Building && currentConstruction.isWonder } + fun isFirstOfItsKind(idx: Int, name: String): Boolean { + // idx = 1 is the currentConstruction, so it is the first + if (idx == -1) + return true + + // if the construction name is the same as the current construction, it isn't the first + return !isBeingConstructed(name) && + idx == constructionQueue.indexOfFirst{it == name} + } + + internal fun getConstruction(constructionName: String): IConstruction { val gameBasics = cityInfo.getRuleset() if (gameBasics.buildings.containsKey(constructionName)) @@ -151,14 +163,17 @@ class CityConstructions { else return 0 } - fun getRemainingWork(constructionName: String): Int { + fun getRemainingWork(constructionName: String, useStoredProduction: Boolean = true): Int { val constr = getConstruction(constructionName) - if (constr is SpecialConstruction) return 0 - return constr.getProductionCost(cityInfo.civInfo) - getWorkDone(constructionName) + return when { + constr is SpecialConstruction -> 0 + useStoredProduction -> constr.getProductionCost(cityInfo.civInfo) - getWorkDone(constructionName) + else -> constr.getProductionCost(cityInfo.civInfo) + } } - fun turnsToConstruction(constructionName: String): Int { - val workLeft = getRemainingWork(constructionName) + fun turnsToConstruction(constructionName: String, useStoredProduction: Boolean = true): Int { + val workLeft = getRemainingWork(constructionName, useStoredProduction) if(workLeft < 0) // we've done more work than actually necessary - possible if circumstances cause buildings to be cheaper later return 1 // we'll finish this next turn diff --git a/core/src/com/unciv/ui/cityscreen/ConstructionsTable.kt b/core/src/com/unciv/ui/cityscreen/ConstructionsTable.kt index fc2e121fa4..49fa99f9eb 100644 --- a/core/src/com/unciv/ui/cityscreen/ConstructionsTable.kt +++ b/core/src/com/unciv/ui/cityscreen/ConstructionsTable.kt @@ -131,7 +131,8 @@ class ConstructionsTable(val cityScreen: CityScreen) : Table(CameraStageBaseScre val specialConstructions = ArrayList() for (unit in city.getRuleset().units.values.filter { it.shouldBeDisplayed(cityConstructions) }) { - val turnsToUnit = cityConstructions.turnsToConstruction(unit.name) + val useStoredProduction = !cityConstructions.isBeingConstructedOrEnqueued(unit.name) + val turnsToUnit = cityConstructions.turnsToConstruction(unit.name, useStoredProduction) val productionButton = getProductionButton(unit.name, unit.name.tr() + "\r\n" + turnsToUnit + turnOrTurns(turnsToUnit), unit.getRejectionReason(cityConstructions)) @@ -139,6 +140,7 @@ class ConstructionsTable(val cityScreen: CityScreen) : Table(CameraStageBaseScre } for (building in city.getRuleset().buildings.values.filter { it.shouldBeDisplayed(cityConstructions)}) { + val useStoredProduction = !cityConstructions.isBeingConstructedOrEnqueued(building.name) val turnsToBuilding = cityConstructions.turnsToConstruction(building.name) val productionTextButton = getProductionButton(building.name, building.name.tr() + "\r\n" + turnsToBuilding + turnOrTurns(turnsToBuilding), @@ -163,6 +165,8 @@ class ConstructionsTable(val cityScreen: CityScreen) : Table(CameraStageBaseScre private fun getQueueEntry(idx: Int, name: String, isLast: Boolean, isSelected: Boolean): Table { val city = cityScreen.city + val cityConstructions = city.cityConstructions + val table = Table() table.align(Align.left).pad(5f) table.background = ImageGetter.getBackground(Color.BLACK) @@ -170,7 +174,7 @@ class ConstructionsTable(val cityScreen: CityScreen) : Table(CameraStageBaseScre if (isSelected) table.background = ImageGetter.getBackground(Color.GREEN.cpy().lerp(Color.BLACK, 0.5f)) - val turnsToComplete = cityScreen.city.cityConstructions.turnsToConstruction(name) + val turnsToComplete = cityConstructions.turnsToConstruction(name, cityConstructions.isFirstOfItsKind(idx, name)) val text = name.tr() + "\r\n" + turnsToComplete + turnOrTurns(turnsToComplete) table.defaults().pad(2f).minWidth(40f) @@ -192,7 +196,7 @@ class ConstructionsTable(val cityScreen: CityScreen) : Table(CameraStageBaseScre return table } - private fun getProductionButton(construction: String, buttonText: String, rejectionReason: String = "", isSelectable: Boolean = true): Table { + private fun getProductionButton(construction: String, buttonText: String, rejectionReason: String = ""): Table { val pickProductionButton = Table() pickProductionButton.align(Align.left).pad(5f)