Improved UnitAutomation tryPrepare() cityToDefend evaluation (#11879)

This commit is contained in:
Oskar Niesen 2024-06-29 15:57:18 -05:00 committed by GitHub
parent 4db4400105
commit 4bb9d3bbc9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -512,12 +512,17 @@ object UnitAutomation {
return false
}
val hostileCivs = civInfo.getKnownCivs().filter { it.isAtWarWith(civInfo) || hasPreparationFlag(it) }
val citiesToDefend = hostileCivs.mapNotNull { NextTurnAutomation.getClosestCities(civInfo, it) }
.sortedBy { unit.getTile().aerialDistanceTo(it.city1.getCenterTile()) }
val hostileCivs = civInfo.getKnownCivs().filter { it.isAtWarWith(civInfo) || hasPreparationFlag(it) }.toSet()
val closeCities = civInfo.threatManager.getNeighboringCitiesOfOtherCivs().filter { it.second.civ in hostileCivs }
val closestDistance = closeCities.minOfOrNull { it.first.getCenterTile().aerialDistanceTo(it.second.getCenterTile()) }
?: return false
val citiesToDefend = closeCities.filter { it.first.getCenterTile().aerialDistanceTo(it.second.getCenterTile()) <= closestDistance + 2 }
.map { it.first }
.distinct() // Remove duplicate cities
.sortedBy { unit.getTile().aerialDistanceTo(it.getCenterTile()) }
// Move to the closest city with a tile we can enter nearby
for ((city, _) in citiesToDefend) {
for (city in citiesToDefend) {
if (unit.getTile().aerialDistanceTo(city.getCenterTile()) <= 2) return true
val tileToMoveTo = city.getCenterTile().getTilesInDistance(2).firstOrNull { unit.movement.canMoveTo(it) && unit.movement.canReach(it) } ?: continue
unit.movement.headTowards(tileToMoveTo)