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

@ -320,7 +320,7 @@ class WorkerAutomation(
val localUniqueCache = LocalUniqueCache() val localUniqueCache = LocalUniqueCache()
var bestBuildableImprovement = potentialTileImprovements.values.asSequence() var bestBuildableImprovement = potentialTileImprovements.values.asSequence()
.map { Pair(it, getImprovementRanking(tile, unit,it.name, localUniqueCache)) } .map { Pair(it, getImprovementRanking(tile, unit, it.name, localUniqueCache)) }
.filter { it.second > 0f } .filter { it.second > 0f }
.maxByOrNull { it.second }?.first .maxByOrNull { it.second }?.first
@ -341,8 +341,8 @@ class WorkerAutomation(
&& isRemovable(lastTerrain) && isRemovable(lastTerrain)
&& !tile.providesResources(civInfo) && !tile.providesResources(civInfo)
&& !isResourceImprovementAllowedOnFeature(tile, potentialTileImprovements) -> Constants.remove + lastTerrain.name && !isResourceImprovementAllowedOnFeature(tile, potentialTileImprovements) -> Constants.remove + lastTerrain.name
else -> tile.tileResource.getImprovements().filter { it in potentialTileImprovements || it==tile.improvement } else -> tile.tileResource.getImprovements().filter { it in potentialTileImprovements || it == tile.improvement }
.maxByOrNull { getImprovementRanking(tile, unit,it, localUniqueCache) } .maxByOrNull { getImprovementRanking(tile, unit, it, localUniqueCache) }
} }
// After gathering all the data, we conduct the hierarchy in one place // After gathering all the data, we conduct the hierarchy in one place
@ -354,7 +354,7 @@ class WorkerAutomation(
bestBuildableImprovement == null -> null bestBuildableImprovement == null -> null
tile.improvement != null && tile.improvement != null &&
getImprovementRanking(tile, unit, tile.improvement!!, localUniqueCache) > getImprovementRanking(tile, unit,bestBuildableImprovement.name, localUniqueCache) getImprovementRanking(tile, unit, tile.improvement!!, localUniqueCache) > getImprovementRanking(tile, unit, bestBuildableImprovement.name, localUniqueCache)
-> null // What we have is better, even if it's pillaged we should repair it -> null // What we have is better, even if it's pillaged we should repair it
lastTerrain.let { lastTerrain.let {
@ -394,15 +394,22 @@ class WorkerAutomation(
val stats = tile.stats.getStatDiffForImprovement(improvement, civInfo, tile.getCity(), localUniqueCache) 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 // 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 ", "") val removedObject = improvementName.replace(Constants.remove, "")
if (ruleSet.terrains.containsKey(terrainName)) { // Otherwise we get an infinite loop with remove roads val removedFeature = tile.terrainFeatures.firstOrNull { it == removedObject }
tile.removeTerrainFeature(terrainName) val removedImprovement = if (removedObject == tile.improvement) removedObject else null
val wantedFinalImprovement = chooseImprovement(unit, tile)
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) if (wantedFinalImprovement != null)
stats.add(tile.stats.getStatDiffForImprovement(wantedFinalImprovement, civInfo, tile.getCity(), localUniqueCache)) stats.add(newTile.stats.getStatDiffForImprovement(wantedFinalImprovement, civInfo, newTile.getCity(), localUniqueCache))
tile.addTerrainFeature(terrainName)
} }
} }