Resolved #12437 - Worker units do not try and swap with non-adjacent tiles

This commit is contained in:
yairm210
2024-11-24 11:14:24 +02:00
parent b09b6c92f7
commit d816b50cd9
2 changed files with 12 additions and 6 deletions

View File

@ -85,13 +85,17 @@ class WorkerAutomation(
if (tileToWork != currentTile && tileToWork != null) {
debug("WorkerAutomation: %s -> head towards %s", unit.toString(), tileToWork)
if (unit.movement.canReachInCurrentTurn(tileToWork) && unit.movement.canMoveTo(tileToWork, allowSwap = true)) {
if (!unit.movement.canMoveTo(tileToWork, allowSwap = false) && unit.movement.canUnitSwapTo(tileToWork)) {
// There must be a unit on the target tile! Lets swap with it.
unit.movement.swapMoveToTile(tileToWork)
}
}
val reachedTile = unit.movement.headTowards(tileToWork)
if (tileToWork in reachedTile.neighbors
&& unit.movement.canMoveTo(tileToWork, allowSwap = true)
&& !unit.movement.canMoveTo(tileToWork, allowSwap = false)
&& unit.movement.canUnitSwapTo(tileToWork)) {
// There must be a unit on the target tile! Let's swap with it.
unit.movement.swapMoveToTile(tileToWork)
}
if (reachedTile != currentTile) unit.doAction() // otherwise, we get a situation where the worker is automated, so it tries to move but doesn't, then tries to automate, then move, etc, forever. Stack overflow exception!
// If we have reached a fort tile that is in progress and shouldn't be there, cancel it.

View File

@ -529,6 +529,8 @@ class UnitMovement(val unit: MapUnit) {
* Precondition: this unit can swap-move to the given tile, as determined by canUnitSwapTo
*/
fun swapMoveToTile(destination: Tile) {
if (!unit.currentTile.neighbors.contains(destination))
throw Exception("Unit $unit cannot swap with a tile that is not a neighbor")
unit.stopEscorting()
val otherUnit = (
if (unit.isCivilian())