Solved rare teleport bug

This commit is contained in:
Yair Morgenstern 2021-07-27 22:50:55 +03:00
parent 7abaa11fd4
commit eb7cf85482
3 changed files with 10 additions and 5 deletions

View File

@ -479,7 +479,7 @@ class CityInfo {
// Edge case! What if a water unit is in a city, and you raze the city? // Edge case! What if a water unit is in a city, and you raze the city?
// Well, the water unit has to return to the water! // Well, the water unit has to return to the water!
for (unit in getCenterTile().getUnits()) { for (unit in getCenterTile().getUnits().toList()) {
if (!unit.movement.canPassThrough(getCenterTile())) if (!unit.movement.canPassThrough(getCenterTile()))
unit.movement.teleportToClosestMoveableTile() unit.movement.teleportToClosestMoveableTile()
} }

View File

@ -340,7 +340,8 @@ class DiplomacyManager() {
hasOpenBorders = newHasOpenBorders hasOpenBorders = newHasOpenBorders
if (bordersWereClosed) { // borders were closed, get out! if (bordersWereClosed) { // borders were closed, get out!
for (unit in civInfo.getCivUnits().filter { it.currentTile.getOwner()?.civName == otherCivName }) { for (unit in civInfo.getCivUnits()
.filter { it.currentTile.getOwner()?.civName == otherCivName }.toList()) {
unit.movement.teleportToClosestMoveableTile() unit.movement.teleportToClosestMoveableTile()
} }
} }
@ -597,7 +598,7 @@ class DiplomacyManager() {
diplomaticStatus = DiplomaticStatus.Peace diplomaticStatus = DiplomaticStatus.Peace
val otherCiv = otherCiv() val otherCiv = otherCiv()
// Get out of others' territory // Get out of others' territory
for (unit in civInfo.getCivUnits().filter { it.getTile().getOwner() == otherCiv }) for (unit in civInfo.getCivUnits().filter { it.getTile().getOwner() == otherCiv }.toList())
unit.movement.teleportToClosestMoveableTile() unit.movement.teleportToClosestMoveableTile()
for (thirdCiv in civInfo.getKnownCivs()) { for (thirdCiv in civInfo.getKnownCivs()) {

View File

@ -291,6 +291,7 @@ class UnitMovementAlgorithms(val unit:MapUnit) {
* This will not use movement points or check for a possible route. * This will not use movement points or check for a possible route.
* It is used e.g. if an enemy city expands its borders, or trades or diplomacy change a unit's * It is used e.g. if an enemy city expands its borders, or trades or diplomacy change a unit's
* allowed position. * allowed position.
* CAN DESTROY THE UNIT.
*/ */
fun teleportToClosestMoveableTile() { fun teleportToClosestMoveableTile() {
var allowedTile: TileInfo? = null var allowedTile: TileInfo? = null
@ -310,13 +311,16 @@ class UnitMovementAlgorithms(val unit:MapUnit) {
if (allowedTile != null) break if (allowedTile != null) break
} }
} }
unit.removeFromTile() // we "teleport" them away if (allowedTile != null) {
if (allowedTile != null) { // it's possible that there is no close tile, and all the guy's cities are full. Screw him then. unit.removeFromTile() // we "teleport" them away
unit.putInTile(allowedTile) unit.putInTile(allowedTile)
// Cancel sleep or fortification if forcibly displaced - for now, leave movement / auto / explore orders // Cancel sleep or fortification if forcibly displaced - for now, leave movement / auto / explore orders
if (unit.isSleeping() || unit.isFortified()) if (unit.isSleeping() || unit.isFortified())
unit.action = null unit.action = null
} }
// it's possible that there is no close tile, and all the guy's cities are full.
// Nothing we can do.
else unit.destroy()
} }
fun moveToTile(destination: TileInfo) { fun moveToTile(destination: TileInfo) {