Fix loop when AI is trying to remove an improvement with the same name as a terrain feature (#11352)

* Fix loop when AI is trying to remove an improvement with the same name as a terrain feature

* whoops
This commit is contained in:
SeventhM 2024-03-25 14:20:16 -07:00 committed by GitHub
parent c5611e9588
commit e17229779a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -394,15 +394,22 @@ class WorkerAutomation(
val stats = tile.stats.getStatDiffForImprovement(improvement, civInfo, tile.getCity(), localUniqueCache)
if (improvementName.startsWith("Remove ")) {
if (improvementName.startsWith(Constants.remove)) {
// We need to look beyond what we are doing right now and at the final improvement that will be on this tile
val terrainName = improvementName.replace("Remove ", "")
if (ruleSet.terrains.containsKey(terrainName)) { // Otherwise we get an infinite loop with remove roads
tile.removeTerrainFeature(terrainName)
val wantedFinalImprovement = chooseImprovement(unit, tile)
val removedObject = improvementName.replace(Constants.remove, "")
val removedFeature = tile.terrainFeatures.firstOrNull { it == removedObject }
val removedImprovement = if (removedObject == tile.improvement) removedObject else null
if (removedFeature != null || removedImprovement != null) {
val newTile = tile.clone()
newTile.setTerrainTransients()
if (removedFeature != null)
newTile.removeTerrainFeature(removedFeature)
if (removedImprovement != null)
newTile.removeImprovement()
val wantedFinalImprovement = chooseImprovement(unit, newTile)
if (wantedFinalImprovement != null)
stats.add(tile.stats.getStatDiffForImprovement(wantedFinalImprovement, civInfo, tile.getCity(), localUniqueCache))
tile.addTerrainFeature(terrainName)
stats.add(newTile.stats.getStatDiffForImprovement(wantedFinalImprovement, civInfo, newTile.getCity(), localUniqueCache))
}
}