From e4025afb4cb73f7623583e2038d8a93c98a49a5b Mon Sep 17 00:00:00 2001 From: yairm210 Date: Tue, 26 Nov 2024 08:18:15 +0200 Subject: [PATCH] Resolved #12522 - fixed crash when swapping while retreating --- .../logic/automation/unit/UnitAutomation.kt | 27 ++++++++++--------- .../map/mapunit/movement/UnitMovement.kt | 1 + 2 files changed, 15 insertions(+), 13 deletions(-) diff --git a/core/src/com/unciv/logic/automation/unit/UnitAutomation.kt b/core/src/com/unciv/logic/automation/unit/UnitAutomation.kt index f1812c6002..f0c9e507df 100644 --- a/core/src/com/unciv/logic/automation/unit/UnitAutomation.kt +++ b/core/src/com/unciv/logic/automation/unit/UnitAutomation.kt @@ -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 } } } diff --git a/core/src/com/unciv/logic/map/mapunit/movement/UnitMovement.kt b/core/src/com/unciv/logic/map/mapunit/movement/UnitMovement.kt index a26950a8e1..3e3c7dafa1 100644 --- a/core/src/com/unciv/logic/map/mapunit/movement/UnitMovement.kt +++ b/core/src/com/unciv/logic/map/mapunit/movement/UnitMovement.kt @@ -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) {