Resolved #12522 - fixed crash when swapping while retreating

This commit is contained in:
yairm210
2024-11-26 08:18:15 +02:00
parent d91319b792
commit e4025afb4c
2 changed files with 15 additions and 13 deletions

View File

@ -319,23 +319,24 @@ object UnitAutomation {
val otherUnit = retreatTile.militaryUnit
if (otherUnit == null) {
// See if we can retreat to the tile
if (unit.movement.canMoveTo(retreatTile)) {
unit.movement.moveToTile(retreatTile)
return true
}
if (!unit.movement.canMoveTo(retreatTile)) continue
unit.movement.moveToTile(retreatTile)
return true
} else if (otherUnit.civ == unit.civ) {
// The tile is taken, lets see if we want to swap retreat to it
if (otherUnit.health > 80) {
if (otherUnit.baseUnit.isRanged()) {
// Don't swap ranged units closer than they have to be
val range = otherUnit.baseUnit.range
if (ourDistanceToClosestEnemy < range)
continue
}
if (unit.movement.canUnitSwapTo(retreatTile)) {
if (otherUnit.health <= 80) continue
if (otherUnit.baseUnit.isRanged()) {
// Don't swap ranged units closer than they have to be
val range = otherUnit.baseUnit.range
if (ourDistanceToClosestEnemy < range)
continue
}
if (unit.movement.canUnitSwapTo(retreatTile)) {
unit.movement.headTowards(retreatTile) // we need to move through the intermediate tiles
if (unit.currentTile.neighbors.contains(otherUnit.currentTile)) {
unit.movement.swapMoveToTile(retreatTile)
return true
}
return true
}
}
}

View File

@ -526,6 +526,7 @@ class UnitMovement(val unit: MapUnit) {
/**
* Swaps this unit with the unit on the given tile
* Limited to neighboring tiles only
* Precondition: this unit can swap-move to the given tile, as determined by canUnitSwapTo
*/
fun swapMoveToTile(destination: Tile) {