From 487ad69d2853452d0101780423eb8abb22ac68b5 Mon Sep 17 00:00:00 2001 From: Jack Rainy Date: Thu, 28 Apr 2022 14:31:46 +0300 Subject: [PATCH] Correct swap of the full-loaded carriers (#6634) * Correct swap of the full-loaded carriers * Code comments --- .../unciv/logic/map/UnitMovementAlgorithms.kt | 25 ++++++++++++------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/core/src/com/unciv/logic/map/UnitMovementAlgorithms.kt b/core/src/com/unciv/logic/map/UnitMovementAlgorithms.kt index 7001de988d..8be969b6f6 100644 --- a/core/src/com/unciv/logic/map/UnitMovementAlgorithms.kt +++ b/core/src/com/unciv/logic/map/UnitMovementAlgorithms.kt @@ -501,14 +501,16 @@ class UnitMovementAlgorithms(val unit: MapUnit) { // The .toList() here is because we have a sequence that's running on the units in the tile, // then if we move one of the units we'll get a ConcurrentModificationException, se we save them all to a list - for (payload in origin.getUnits().filter { it.isTransported && unit.canTransport(it) }.toList()) { // bring along the payloads + val payloadUnits = origin.getUnits().filter { it.isTransported && unit.canTransport(it) }.toList() + // bring along the payloads + for (payload in payloadUnits) { payload.removeFromTile() for (tile in pathToLastReachableTile){ payload.moveThroughTile(tile) if (tile == finalTileReached) break // this is the final tile the transport reached } payload.putInTile(finalTileReached) - payload.isTransported = true // restore the flag to not leave the payload in the cit + payload.isTransported = true // restore the flag to not leave the payload in the city payload.mostRecentMoveType = UnitMovementMemoryType.UnitMoved } @@ -530,13 +532,13 @@ class UnitMovementAlgorithms(val unit: MapUnit) { destination.civilianUnit else destination.militaryUnit - )!! // The precondition guarantees that there is an eligible same-type unit at the destination + )?: return // The precondition guarantees that there is an eligible same-type unit at the destination val ourOldPosition = unit.getTile() val theirOldPosition = otherUnit.getTile() - val ourPayload = ourOldPosition.getUnits().filter { it.isTransported && unit.canTransport(it) }.toList() - val theirPayload = theirOldPosition.getUnits().filter { it.isTransported && otherUnit.canTransport(it) }.toList() + val ourPayload = ourOldPosition.getUnits().filter { it.isTransported }.toList() + val theirPayload = theirOldPosition.getUnits().filter { it.isTransported }.toList() // Swap the units // Step 1: Release the destination tile @@ -549,15 +551,20 @@ class UnitMovementAlgorithms(val unit: MapUnit) { unit.removeFromTile() for (payload in ourPayload) payload.removeFromTile() - // Step 4: Perform the another movement + // Step 4: Restore the initial position after step 1 otherUnit.putInTile(theirOldPosition) - for (payload in theirPayload) + for (payload in theirPayload) { payload.putInTile(theirOldPosition) + payload.isTransported = true // restore the flag to not leave the payload in the city + } + // Step 5: Perform the another movement otherUnit.movement.moveToTile(ourOldPosition) - // Step 5: Restore the position in the new tile + // Step 6: Restore the position in the new tile after step 3 unit.putInTile(theirOldPosition) - for (payload in ourPayload) + for (payload in ourPayload) { payload.putInTile(theirOldPosition) + payload.isTransported = true // restore the flag to not leave the payload in the city + } // Step 6: Update states otherUnit.mostRecentMoveType = UnitMovementMemoryType.UnitMoved unit.mostRecentMoveType = UnitMovementMemoryType.UnitMoved