mirror of
https://github.com/yairm210/Unciv.git
synced 2025-07-26 15:49:14 +07:00
Resolved #12437 - Worker units do not try and swap with non-adjacent tiles
This commit is contained in:
@ -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.
|
||||
|
@ -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())
|
||||
|
Reference in New Issue
Block a user