From f50a757069475e3a3f77bd012715ee004790667c Mon Sep 17 00:00:00 2001 From: Yair Morgenstern Date: Wed, 30 Sep 2020 10:51:22 +0300 Subject: [PATCH] Unified "improvement on tile" checks --- changelog.md | 2 +- core/src/com/unciv/logic/map/TileInfo.kt | 22 +++++++++---- .../ui/mapeditor/TileEditorOptionsTable.kt | 32 ++----------------- 3 files changed, 20 insertions(+), 36 deletions(-) diff --git a/changelog.md b/changelog.md index 8c19fe0eea..25654b31b4 100644 --- a/changelog.md +++ b/changelog.md @@ -8,7 +8,7 @@ Cannot open multiple gold selection popups in trade table "No Maintenance costs for improvements in []" genericified - By givehub99 -Tranlation updates +Translation updates ## 3.10.12 diff --git a/core/src/com/unciv/logic/map/TileInfo.kt b/core/src/com/unciv/logic/map/TileInfo.kt index 51ef0e4398..39c6c990e4 100644 --- a/core/src/com/unciv/logic/map/TileInfo.kt +++ b/core/src/com/unciv/logic/map/TileInfo.kt @@ -285,17 +285,27 @@ open class TileInfo { /** Returns true if the [improvement] can be built on this [TileInfo] */ fun canBuildImprovement(improvement: TileImprovement, civInfo: CivilizationInfo): Boolean { - val topTerrain = getLastTerrain() return when { - isCityCenter() -> false - improvement.name == this.improvement -> false improvement.uniqueTo != null && improvement.uniqueTo != civInfo.civName -> false improvement.techRequired?.let { civInfo.tech.isResearched(it) } == false -> false - "Cannot be built on bonus resource" in improvement.uniques && resource != null - && getTileResource().resourceType == ResourceType.Bonus -> false getOwner() != null && getOwner() != civInfo && !improvement.hasUnique("Can be built outside your borders") -> false + !canImprovementBeBuiltHere(improvement) -> false + else -> getTileResource().improvement == improvement.name && hasViewableResource(civInfo) + } + } + /** Without regards to what civinfo it is, a lot of the checks are ust for the improvement on the tile. + * Doubles as a check for the map editor. + */ + fun canImprovementBeBuiltHere(improvement: TileImprovement): Boolean { + val topTerrain = getLastTerrain() + + return when { + improvement.name == this.improvement -> false + isCityCenter() -> false + "Cannot be built on bonus resource" in improvement.uniques && resource != null + && getTileResource().resourceType == ResourceType.Bonus -> false improvement.terrainsCanBeBuiltOn.contains(topTerrain.name) -> true improvement.name == "Road" && roadStatus == RoadStatus.None -> true improvement.name == "Railroad" && this.roadStatus != RoadStatus.Railroad -> true @@ -306,7 +316,7 @@ open class TileInfo { improvement.hasUnique("Can also be built on tiles adjacent to fresh water") && isAdjacentToFreshwater -> true "Can only be built on Coastal tiles" in improvement.uniques && isCoastalTile() -> true - else -> hasViewableResource(civInfo) && getTileResource().improvement == improvement.name + else -> true } } diff --git a/core/src/com/unciv/ui/mapeditor/TileEditorOptionsTable.kt b/core/src/com/unciv/ui/mapeditor/TileEditorOptionsTable.kt index d4bfc46e46..638f79c8f7 100644 --- a/core/src/com/unciv/ui/mapeditor/TileEditorOptionsTable.kt +++ b/core/src/com/unciv/ui/mapeditor/TileEditorOptionsTable.kt @@ -516,35 +516,9 @@ class TileEditorOptionsTable(val mapEditorScreen: MapEditorScreen): Table(Camera return } val improvement = tileInfo.getTileImprovement()!! - val resource = if (tileInfo.resource!=null) tileInfo.getTileResource() else null - when { - // Precedence, simplified: terrainsCanBeBuiltOn, then improves-resource, then 'everywhere', default to false - // 'everywhere' improvements (city ruins, encampments, ancient ruins, great improvements) - // are recognized as without terrainsCanBeBuiltOn and with turnsToBuild == 0 - "Cannot be built on bonus resource" in improvement.uniques - && tileInfo.resource != null - && resource?.resourceType == ResourceType.Bonus - -> tileInfo.improvement = null // forbid if this unique matches - tileInfo.isLand // Fishing boats have Coast allowed even though they're meant resource-only - && topTerrain.name in improvement.terrainsCanBeBuiltOn - -> Unit // allow where top terrain explicitly named - resource?.improvement == improvement.name - && (!topTerrain.unbuildable || topTerrain.name in improvement.resourceTerrainAllow) - -> Unit // allow where it improves a resource and feature OK - tileInfo.isWater || topTerrain.impassable - -> tileInfo.improvement = null // forbid if water or mountains - improvement.terrainsCanBeBuiltOn.isEmpty() && improvement.turnsToBuild == 0 - // Allow Great Improvement but clear unbuildable terrain feature - // Allow barbarian camps, ruins and similar without clear - -> if (topTerrain.unbuildable && improvement.isGreatImprovement()) - tileInfo.terrainFeature = null - topTerrain.unbuildable -> tileInfo.improvement = null // forbid on unbuildable feature - improvement.hasUnique("Can also be built on tiles adjacent to fresh water") - && tileInfo.isAdjacentToFreshwater -> Unit // allow farms on tiles adjacent to fresh water - "Can only be built on Coastal tiles" in improvement.uniques && tileInfo.isCoastalTile() - -> Unit // allow Moai where appropriate - else -> tileInfo.improvement = null - } + tileInfo.improvement = null // Unset, and check if it can be reset. If so, do it, if not, invalid. + if (tileInfo.canImprovementBeBuiltHere(improvement)) + tileInfo.improvement = improvement.name } private fun setCurrentHex(tileInfo: TileInfo, text:String){