diff --git a/core/src/com/unciv/logic/automation/NextTurnAutomation.kt b/core/src/com/unciv/logic/automation/NextTurnAutomation.kt index f4f7aeab53..d46e7bcfd3 100644 --- a/core/src/com/unciv/logic/automation/NextTurnAutomation.kt +++ b/core/src/com/unciv/logic/automation/NextTurnAutomation.kt @@ -116,7 +116,7 @@ object NextTurnAutomation{ val construction = city.cityConstructions.getCurrentConstruction() if (construction.canBePurchased() && city.civInfo.gold / 3 >= construction.getGoldCost(civInfo)) { - city.cityConstructions.purchaseConstruction(construction.name) + city.cityConstructions.purchaseConstruction(construction.name, 0, true) } } } diff --git a/core/src/com/unciv/logic/city/CityConstructions.kt b/core/src/com/unciv/logic/city/CityConstructions.kt index 81242391d8..05659a274c 100644 --- a/core/src/com/unciv/logic/city/CityConstructions.kt +++ b/core/src/com/unciv/logic/city/CityConstructions.kt @@ -309,13 +309,26 @@ class CityConstructions { builtBuildings.remove(buildingName) } - fun purchaseConstruction(constructionName: String): Boolean { + /** + * Purchase a construction for gold + * called from NextTurnAutomation and the City UI + * Build / place the new item, deduct cost, and maintain queue. + * + * @param constructionName What to buy (needed since buying something not queued is allowed) + * @param queuePosition Position in the queue or -1 if not from queue + * Note: -1 does not guarantee queue will remain unchanged (validation) + * @param automatic Flag whether automation should try to choose what next to build (not coming from UI) + * Note: settings.autoAssignCityProduction is handled later + * @return Success (false e.g. unit cannot be placed + */ + fun purchaseConstruction(constructionName: String, queuePosition: Int, automatic: Boolean): Boolean { if (!getConstruction(constructionName).postBuildEvent(this, true)) return false // nothing built - no pay cityInfo.civInfo.gold -= getConstruction(constructionName).getGoldCost(cityInfo.civInfo) - if (currentConstructionFromQueue == constructionName) - removeCurrentConstruction() + + if (queuePosition in 0 until constructionQueue.size) + removeFromQueue(queuePosition, automatic) validateConstructionQueue() return true diff --git a/core/src/com/unciv/ui/cityscreen/ConstructionsTable.kt b/core/src/com/unciv/ui/cityscreen/ConstructionsTable.kt index e39851bcfa..fad5cadbcb 100644 --- a/core/src/com/unciv/ui/cityscreen/ConstructionsTable.kt +++ b/core/src/com/unciv/ui/cityscreen/ConstructionsTable.kt @@ -17,8 +17,8 @@ import com.unciv.ui.cityscreen.ConstructionInfoTable.Companion.turnOrTurns import com.unciv.ui.utils.* class ConstructionsTable(val cityScreen: CityScreen) : Table(CameraStageBaseScreen.skin) { - /* -2 = Nothing, -1 = current construction, >= 0 queue entry */ - private var selectedQueueEntry = -2 // None + /* -1 = Nothing, >= 0 queue entry (0 = current construction) */ + private var selectedQueueEntry = -1 // None private val showCityInfoTableButton: TextButton private val constructionsQueueScrollPane: ScrollPane @@ -252,14 +252,14 @@ class ConstructionsTable(val cityScreen: CityScreen) : Table(CameraStageBaseScre pickProductionButton.onClick { cityScreen.selectedConstruction = construction cityScreen.selectedTile = null - selectedQueueEntry = -2 + selectedQueueEntry = -1 cityScreen.update() } return pickProductionButton } - private fun isSelectedQueueEntry(): Boolean = selectedQueueEntry > -2 + private fun isSelectedQueueEntry(): Boolean = selectedQueueEntry >= 0 private fun getQueueButton(construction: IConstruction?): TextButton { val city = cityScreen.city @@ -273,7 +273,7 @@ class ConstructionsTable(val cityScreen: CityScreen) : Table(CameraStageBaseScre button.onClick { cityConstructions.removeFromQueue(selectedQueueEntry,false) cityScreen.selectedConstruction = null - selectedQueueEntry = -2 + selectedQueueEntry = -1 cityScreen.update() } } @@ -303,12 +303,7 @@ class ConstructionsTable(val cityScreen: CityScreen) : Table(CameraStageBaseScre fun purchaseConstruction(construction: IConstruction) { val city = cityScreen.city - val cityConstructions = city.cityConstructions - // We can't trust the isSelectedQueueEntry because that fails when we have the same unit as both the current construction and in the queue, - // and then we purchase the unit from the queue - see #2157 - val constructionIsCurrentConstruction = construction.name==cityConstructions.currentConstructionFromQueue - - if (!cityConstructions.purchaseConstruction(construction.name)) { + if (!city.cityConstructions.purchaseConstruction(construction.name, selectedQueueEntry, false)) { Popup(cityScreen).apply { add("No space available to place [${construction.name}] near [${city.name}]".tr()).row() addCloseButton() @@ -317,10 +312,9 @@ class ConstructionsTable(val cityScreen: CityScreen) : Table(CameraStageBaseScre return } if (isSelectedQueueEntry()) { - selectedQueueEntry = -2 + selectedQueueEntry = -1 cityScreen.selectedConstruction = null } - if (!construction.shouldBeDisplayed(cityConstructions)) cityScreen.selectedConstruction = null cityScreen.update() }