From 70bbfe14d634c896aece6096a00b3f5be1291358 Mon Sep 17 00:00:00 2001 From: Will Allen Date: Thu, 30 Nov 2023 11:13:39 -0600 Subject: [PATCH] Mitigate #10619 (#10629) - Clear all unit movement caches upon a road being pillaged - Log when this happens - Minor whitespace fixes --- .../logic/automation/unit/UnitAutomation.kt | 17 ++++++++--------- core/src/com/unciv/logic/map/tile/Tile.kt | 15 ++++++++++++++- 2 files changed, 22 insertions(+), 10 deletions(-) diff --git a/core/src/com/unciv/logic/automation/unit/UnitAutomation.kt b/core/src/com/unciv/logic/automation/unit/UnitAutomation.kt index f90dc10f41..377e5d78a8 100644 --- a/core/src/com/unciv/logic/automation/unit/UnitAutomation.kt +++ b/core/src/com/unciv/logic/automation/unit/UnitAutomation.kt @@ -125,7 +125,7 @@ object UnitAutomation { } internal fun tryUpgradeUnit(unit: MapUnit): Boolean { - if (unit.civ.isHuman() && (!UncivGame.Current.settings.automatedUnitsCanUpgrade + if (unit.civ.isHuman() && (!UncivGame.Current.settings.automatedUnitsCanUpgrade || UncivGame.Current.settings.autoPlay.isAutoPlayingAndFullAI())) return false if (unit.baseUnit.upgradesTo == null) return false val upgradedUnit = unit.upgrade.getUnitToUpgradeTo() @@ -173,18 +173,18 @@ object UnitAutomation { CivilianUnitAutomation.automateCivilianUnit(unit) return } - + if (unit.baseUnit.isAirUnit()) { if (unit.canIntercept()) return AirUnitAutomation.automateFighter(unit) - + if (!unit.baseUnit.isNuclearWeapon()) return AirUnitAutomation.automateBomber(unit) - + // Note that not all nukes have to be air units if (unit.baseUnit.isNuclearWeapon()) return AirUnitAutomation.automateNukes(unit) - + if (unit.hasUnique(UniqueType.SelfDestructs)) return AirUnitAutomation.automateMissile(unit) } @@ -263,7 +263,7 @@ object UnitAutomation { if (unit.isCivilian()) return false // Better to do a more healing oriented move then if (unit.civ.threatManager.getDistanceToClosestEnemyUnit(unit.getTile(),6, true) > 4) return false - + if (unit.baseUnit.isAirUnit()) { return false } @@ -273,9 +273,8 @@ object UnitAutomation { for (swapTile in swapableTiles) { val otherUnit = swapTile.militaryUnit!! val ourDistanceToClosestEnemy = unit.civ.threatManager.getDistanceToClosestEnemyUnit(unit.getTile(),6, false) - if (otherUnit.health > 80 + if (otherUnit.health > 80 && ourDistanceToClosestEnemy < otherUnit.civ.threatManager.getDistanceToClosestEnemyUnit(otherUnit.getTile(),6,false)) { - if (otherUnit.baseUnit.isRanged()) { // Don't swap ranged units closer than they have to be val range = otherUnit.baseUnit.range @@ -588,6 +587,6 @@ object UnitAutomation { unit.civ.addNotification("${unit.shortDisplayName()} finished exploring.", unit.currentTile.position, NotificationCategory.Units, unit.name, "OtherIcons/Sleep") unit.action = null } - + } diff --git a/core/src/com/unciv/logic/map/tile/Tile.kt b/core/src/com/unciv/logic/map/tile/Tile.kt index c64a617815..3e96ff8d87 100644 --- a/core/src/com/unciv/logic/map/tile/Tile.kt +++ b/core/src/com/unciv/logic/map/tile/Tile.kt @@ -27,6 +27,7 @@ import com.unciv.models.ruleset.unique.UniqueType import com.unciv.ui.components.extensions.withItem import com.unciv.ui.components.extensions.withoutItem import com.unciv.utils.DebugUtils +import com.unciv.utils.Log import kotlin.math.abs import kotlin.math.min import kotlin.random.Random @@ -895,8 +896,10 @@ open class Tile : IsPartOfGameInfoSerialization { // otherwise use pillage/repair systems if (canPillageTileImprovement()) improvementIsPillaged = true - else + else { roadIsPillaged = true + clearAllPathfindingCaches() + } } owningCity?.reassignPopulationDeferred() @@ -904,6 +907,16 @@ open class Tile : IsPartOfGameInfoSerialization { owningCity!!.civ.cache.updateCivResources() } + private fun clearAllPathfindingCaches() { + val units = tileMap.gameInfo.civilizations.asSequence() + .filter { it.isAlive() } + .flatMap { it.units.getCivUnits() } + Log.debug("%s: road pillaged, clearing cache for %d units", this, { units.count() }) + for (otherUnit in units) { + otherUnit.movement.clearPathfindingCache() + } + } + fun isPillaged(): Boolean = improvementIsPillaged || roadIsPillaged fun setRepaired() {