From 402a9ba8257b69118af9f070ee2ae61c25bc9375 Mon Sep 17 00:00:00 2001 From: itanasi <44038014+itanasi@users.noreply.github.com> Date: Sat, 19 Feb 2022 09:34:27 -0800 Subject: [PATCH] Show Improvements that are buildable after Removing TerrainFeature (#6149) * Initial working version! * More comments * Add missing .tr() translation calls * add brackets * Fixed * Optimize checking for removable last feature More complete tileInfo.clone() Co-authored-by: itanasi --- .../jsons/translations/template.properties | 1 + core/src/com/unciv/logic/map/TileInfo.kt | 8 ++++++ .../pickerscreens/ImprovementPickerScreen.kt | 27 ++++++++++++++++--- 3 files changed, 33 insertions(+), 3 deletions(-) diff --git a/android/assets/jsons/translations/template.properties b/android/assets/jsons/translations/template.properties index 58639d6fc8..2e73863226 100644 --- a/android/assets/jsons/translations/template.properties +++ b/android/assets/jsons/translations/template.properties @@ -809,6 +809,7 @@ Provides [resource] = Provides [amount] [resource] = Replaces [improvement] = Pick now! = +Remove [feature] first = Build [building] = Train [unit] = Produce [thingToProduce] = diff --git a/core/src/com/unciv/logic/map/TileInfo.kt b/core/src/com/unciv/logic/map/TileInfo.kt index 34a6917dca..b2816dadf9 100644 --- a/core/src/com/unciv/logic/map/TileInfo.kt +++ b/core/src/com/unciv/logic/map/TileInfo.kt @@ -92,6 +92,14 @@ open class TileInfo { fun clone(): TileInfo { val toReturn = TileInfo() + toReturn.tileMap = tileMap + toReturn.ruleset = ruleset + toReturn.isCityCenterInternal = isCityCenterInternal + toReturn.owningCity = owningCity + toReturn.baseTerrainObject = baseTerrainObject + toReturn.isLand = isLand + toReturn.isWater = isWater + toReturn.isOcean = isOcean if (militaryUnit != null) toReturn.militaryUnit = militaryUnit!!.clone() if (civilianUnit != null) toReturn.civilianUnit = civilianUnit!!.clone() for (airUnit in airUnits) toReturn.airUnits.add(airUnit.clone()) diff --git a/core/src/com/unciv/ui/pickerscreens/ImprovementPickerScreen.kt b/core/src/com/unciv/ui/pickerscreens/ImprovementPickerScreen.kt index b83b707ae1..299d5799f7 100644 --- a/core/src/com/unciv/ui/pickerscreens/ImprovementPickerScreen.kt +++ b/core/src/com/unciv/ui/pickerscreens/ImprovementPickerScreen.kt @@ -51,12 +51,27 @@ class ImprovementPickerScreen(val tileInfo: TileInfo, unit: MapUnit, val onAccep val regularImprovements = Table() regularImprovements.defaults().pad(5f) + + // clone tileInfo without "top" feature if it could be removed + // Keep this copy around for speed + val tileInfoNoLast:TileInfo = tileInfo.clone() + if (ruleSet.tileImprovements.any{it.key == Constants.remove + tileInfoNoLast.getLastTerrain().name}) { + tileInfoNoLast.terrainFeatures.remove(tileInfoNoLast.getLastTerrain().name) + } for (improvement in ruleSet.tileImprovements.values) { + var suggestRemoval:Boolean = false // canBuildImprovement() would allow e.g. great improvements thus we need to exclude them - except cancel if (improvement.turnsToBuild == 0 && improvement.name != Constants.cancelImprovementOrder) continue if (improvement.name == tileInfo.improvement) continue // also checked by canImprovementBeBuiltHere, but after more expensive tests - if (!tileInfo.canBuildImprovement(improvement, currentPlayerCiv)) continue + if (!tileInfo.canBuildImprovement(improvement, currentPlayerCiv)){ + // if there is an improvement that could remove that terrain + if(tileInfoNoLast.canBuildImprovement(improvement, currentPlayerCiv)) { + suggestRemoval = true + } else { + continue + } + } if (!unit.canBuildImprovement(improvement)) continue val image = ImageGetter.getImprovementIcon(improvement.name, 30f) @@ -89,9 +104,12 @@ class ImprovementPickerScreen(val tileInfo: TileInfo, unit: MapUnit, val onAccep && improvement.name != Constants.cancelImprovementOrder) if (tileInfo.improvement != null && removeImprovement) labelText += "\n" + "Replaces [${tileInfo.improvement}]".tr() - val pickNow = if (tileInfo.improvementInProgress != improvement.name) + val pickNow = if (suggestRemoval) + (Constants.remove + "[" + tileInfo.getLastTerrain().name + "] first").toLabel() + else if (tileInfo.improvementInProgress != improvement.name) "Pick now!".toLabel().onClick { accept(improvement) } - else "Current construction".toLabel() + else + "Current construction".toLabel() val statIcons = getStatIconsTable(provideResource, removeImprovement) @@ -118,6 +136,9 @@ class ImprovementPickerScreen(val tileInfo: TileInfo, unit: MapUnit, val onAccep } if (improvement.name == tileInfo.improvementInProgress) improvementButton.color = Color.GREEN + if (suggestRemoval){ + improvementButton.disable() + } regularImprovements.add(improvementButton) if (shortcutKey != null) {