From 8519453d19fa80d26601950fd81bf68f4d5664a7 Mon Sep 17 00:00:00 2001 From: yairm210 Date: Wed, 11 Dec 2024 19:17:01 +0200 Subject: [PATCH] Fixed cities built on pillages roads colored red --- .../com/unciv/logic/BackwardCompatibility.kt | 4 +--- core/src/com/unciv/logic/city/City.kt | 15 +++++------- core/src/com/unciv/logic/map/tile/Tile.kt | 23 ++++++++----------- .../map/tile/TileImprovementFunctions.kt | 4 ++-- .../screens/devconsole/ConsoleTileCommands.kt | 3 +-- .../tabs/MapEditorEditSubTabs.kt | 2 +- 6 files changed, 21 insertions(+), 30 deletions(-) diff --git a/core/src/com/unciv/logic/BackwardCompatibility.kt b/core/src/com/unciv/logic/BackwardCompatibility.kt index fc4ac55f86..a92d1644b7 100644 --- a/core/src/com/unciv/logic/BackwardCompatibility.kt +++ b/core/src/com/unciv/logic/BackwardCompatibility.kt @@ -5,7 +5,6 @@ import com.unciv.logic.city.CityConstructions import com.unciv.logic.civilization.diplomacy.DiplomacyFlags import com.unciv.logic.civilization.diplomacy.DiplomacyManager import com.unciv.logic.civilization.managers.TechManager -import com.unciv.logic.map.tile.RoadStatus import com.unciv.models.ruleset.ModOptions import com.unciv.models.ruleset.PerpetualConstruction import com.unciv.models.ruleset.Ruleset @@ -61,8 +60,7 @@ object BackwardCompatibility { if (!ruleset.tileImprovements.containsKey(Constants.repair)) for (tile in tileMap.values) { if (tile.roadIsPillaged) { - tile.roadStatus = RoadStatus.None - tile.roadIsPillaged = false + tile.removeRoad() } if (tile.improvementIsPillaged) { tile.improvement = null diff --git a/core/src/com/unciv/logic/city/City.kt b/core/src/com/unciv/logic/city/City.kt index 2c4b4473dd..1a22019cc3 100644 --- a/core/src/com/unciv/logic/city/City.kt +++ b/core/src/com/unciv/logic/city/City.kt @@ -91,7 +91,7 @@ class City : IsPartOfGameInfoSerialization, INamed { civ.gameInfo.getCities().filter { it != this && it.getCenterTile().isExplored(civ) && it.getCenterTile().aerialDistanceTo(getCenterTile()) <= 12 }.toList() } - var cityAIFocus: String = CityFocus.NoFocus.name + private var cityAIFocus: String = CityFocus.NoFocus.name fun getCityFocus() = CityFocus.entries.firstOrNull { it.name == cityAIFocus } ?: CityFocus.NoFocus fun setCityFocus(cityFocus: CityFocus){ cityAIFocus = cityFocus.name } @@ -385,15 +385,12 @@ class City : IsPartOfGameInfoSerialization, INamed { CityConquestFunctions(this).moveToCiv(newCivInfo) internal fun tryUpdateRoadStatus() { - if (getCenterTile().roadStatus == RoadStatus.None) { - val roadImprovement = getRuleset().roadImprovement - if (roadImprovement != null && roadImprovement.techRequired in civ.tech.techsResearched) - getCenterTile().roadStatus = RoadStatus.Road - } else if (getCenterTile().roadStatus != RoadStatus.Railroad) { - val railroadImprovement = getRuleset().railroadImprovement - if (railroadImprovement != null && railroadImprovement.techRequired in civ.tech.techsResearched) - getCenterTile().roadStatus = RoadStatus.Railroad + val requiredRoad = when{ + getRuleset().railroadImprovement?.let { it.techRequired == null || it.techRequired in civ.tech.techsResearched } == true -> RoadStatus.Railroad + getRuleset().roadImprovement?.let { it.techRequired == null || it.techRequired in civ.tech.techsResearched } == true -> RoadStatus.Road + else -> RoadStatus.None } + getCenterTile().setRoadStatus(requiredRoad, civ) } fun getGoldForSellingBuilding(buildingName: String) = diff --git a/core/src/com/unciv/logic/map/tile/Tile.kt b/core/src/com/unciv/logic/map/tile/Tile.kt index 6b82a4bef8..83499737fd 100644 --- a/core/src/com/unciv/logic/map/tile/Tile.kt +++ b/core/src/com/unciv/logic/map/tile/Tile.kt @@ -80,6 +80,7 @@ class Tile : IsPartOfGameInfoSerialization, Json.Serializable { private val improvementQueue = ArrayList(1) var roadStatus = RoadStatus.None + var roadIsPillaged = false private var roadOwner: String = "" // either who last built the road or last owner of tile @@ -885,11 +886,16 @@ class Tile : IsPartOfGameInfoSerialization, Json.Serializable { fun setImprovement(improvementStr: String, civToHandleCompletion: Civilization? = null, unit: MapUnit? = null) = improvementFunctions.setImprovement(improvementStr, civToHandleCompletion, unit) - // function handling when adding a road to the tile - fun addRoad(roadType: RoadStatus, creatingCivInfo: Civilization?) { - roadStatus = roadType + // function handling when removing a road from the tile + fun removeRoad() = setRoadStatus(RoadStatus.None, null) + + fun setRoadStatus(newRoadStatus: RoadStatus, creatingCivInfo: Civilization?) { + roadStatus = newRoadStatus roadIsPillaged = false - if (getOwner() != null) { + + if (newRoadStatus == RoadStatus.None && owningCity == null) + getRoadOwner()?.neutralRoads?.remove(this.position) + else if (getOwner() != null) { roadOwner = getOwner()!!.civName } else if (creatingCivInfo != null) { roadOwner = creatingCivInfo.civName // neutral tile, use building unit @@ -897,15 +903,6 @@ class Tile : IsPartOfGameInfoSerialization, Json.Serializable { } } - // function handling when removing a road from the tile - fun removeRoad() { - roadIsPillaged = false - if (roadStatus == RoadStatus.None) return - roadStatus = RoadStatus.None - if (owningCity == null) - getRoadOwner()?.neutralRoads?.remove(this.position) - } - fun startWorkingOnImprovement(improvement: TileImprovement, civInfo: Civilization, unit: MapUnit) { improvementQueue.clear() queueImprovement(improvement, civInfo, unit) diff --git a/core/src/com/unciv/logic/map/tile/TileImprovementFunctions.kt b/core/src/com/unciv/logic/map/tile/TileImprovementFunctions.kt index be76e27ce7..a4d0f4ebe6 100644 --- a/core/src/com/unciv/logic/map/tile/TileImprovementFunctions.kt +++ b/core/src/com/unciv/logic/map/tile/TileImprovementFunctions.kt @@ -203,8 +203,8 @@ class TileImprovementFunctions(val tile: Tile) { improvementName?.startsWith(Constants.remove) == true -> { activateRemovalImprovement(improvementName, civToActivateBroaderEffects) } - improvementName == RoadStatus.Road.name -> tile.addRoad(RoadStatus.Road, civToActivateBroaderEffects) - improvementName == RoadStatus.Railroad.name -> tile.addRoad(RoadStatus.Railroad, civToActivateBroaderEffects) + improvementName == RoadStatus.Road.name -> tile.setRoadStatus(RoadStatus.Road, civToActivateBroaderEffects) + improvementName == RoadStatus.Railroad.name -> tile.setRoadStatus(RoadStatus.Railroad, civToActivateBroaderEffects) improvementName == Constants.repair -> tile.setRepaired() else -> { tile.improvementIsPillaged = false diff --git a/core/src/com/unciv/ui/screens/devconsole/ConsoleTileCommands.kt b/core/src/com/unciv/ui/screens/devconsole/ConsoleTileCommands.kt index 000fc2df4a..2c9a7c43bf 100644 --- a/core/src/com/unciv/ui/screens/devconsole/ConsoleTileCommands.kt +++ b/core/src/com/unciv/ui/screens/devconsole/ConsoleTileCommands.kt @@ -8,7 +8,6 @@ import com.unciv.logic.civilization.NotificationCategory import com.unciv.logic.civilization.NotificationIcon import com.unciv.logic.map.mapgenerator.RiverGenerator import com.unciv.logic.map.mapgenerator.RiverGenerator.RiverDirections -import com.unciv.logic.map.tile.RoadStatus import com.unciv.logic.map.tile.Tile import com.unciv.models.ruleset.tile.Terrain import com.unciv.models.ruleset.tile.TerrainType @@ -42,7 +41,7 @@ internal class ConsoleTileCommands: ConsoleCommandNode { "removeroad" to ConsoleAction("tile removeroad") { console, _ -> val selectedTile = console.getSelectedTile() - selectedTile.roadStatus = RoadStatus.None + selectedTile.removeRoad() //todo this covers many cases but not all - do we really need to loop over all civs? selectedTile.getOwner()?.cache?.updateCitiesConnectedToCapital() DevConsoleResponse.OK diff --git a/core/src/com/unciv/ui/screens/mapeditorscreen/tabs/MapEditorEditSubTabs.kt b/core/src/com/unciv/ui/screens/mapeditorscreen/tabs/MapEditorEditSubTabs.kt index 82fbc3d877..35ce165e6b 100644 --- a/core/src/com/unciv/ui/screens/mapeditorscreen/tabs/MapEditorEditSubTabs.kt +++ b/core/src/com/unciv/ui/screens/mapeditorscreen/tabs/MapEditorEditSubTabs.kt @@ -224,7 +224,7 @@ class MapEditorEditImprovementsTab( val road = RoadStatus.entries.firstOrNull { r -> r.name == it } if (road != null) editTab.setBrush(it, "Improvement/$it", handlerType = BrushHandlerType.Road) { tile -> - tile.roadStatus = if (tile.roadStatus == road) RoadStatus.None else road + tile.setRoadStatus(if (tile.roadStatus == road) RoadStatus.None else road, null) } else editTab.setBrush(it, "Improvement/$it") { tile ->