From eb26a3f2416f75f3dcb40481f90065d9b3913235 Mon Sep 17 00:00:00 2001 From: Yair Morgenstern Date: Sun, 23 Sep 2018 15:42:06 +0300 Subject: [PATCH] Civ city list is now concurrency-proof --- core/src/com/unciv/logic/city/CityInfo.kt | 8 ++++---- core/src/com/unciv/logic/civilization/CivilizationInfo.kt | 8 ++++++-- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/core/src/com/unciv/logic/city/CityInfo.kt b/core/src/com/unciv/logic/city/CityInfo.kt index 94880038..63ca0c7d 100644 --- a/core/src/com/unciv/logic/city/CityInfo.kt +++ b/core/src/com/unciv/logic/city/CityInfo.kt @@ -46,7 +46,7 @@ class CityInfo { } this.location = cityLocation - civInfo.cities.add(this) + civInfo.cities = civInfo.cities.toMutableList().apply { add(this@CityInfo) } if(civInfo == civInfo.gameInfo.getPlayerCivilization()) civInfo.addNotification("$name {has been founded}!", cityLocation, Color.PURPLE) if (civInfo.policies.isAdopted("Legalism") && civInfo.cities.size <= 4) cityConstructions.addCultureBuilding() @@ -184,14 +184,14 @@ class CityInfo { } fun destroyCity() { - civInfo.cities.remove(this) + civInfo.cities = civInfo.cities.toMutableList().apply { remove(this@CityInfo) } getTiles().forEach { expansion.relinquishOwnership(it) } getCenterTile().improvement="City ruins" } fun moveToCiv(newCivInfo: CivilizationInfo){ - civInfo.cities.remove(this) - newCivInfo.cities.add(this) + civInfo.cities = civInfo.cities.toMutableList().apply { remove(this@CityInfo) } + newCivInfo.cities = newCivInfo.cities.toMutableList().apply { add(this@CityInfo) } civInfo = newCivInfo // now that the tiles have changed, we need to reassign population diff --git a/core/src/com/unciv/logic/civilization/CivilizationInfo.kt b/core/src/com/unciv/logic/civilization/CivilizationInfo.kt index 0e395659..ce78664c 100644 --- a/core/src/com/unciv/logic/civilization/CivilizationInfo.kt +++ b/core/src/com/unciv/logic/civilization/CivilizationInfo.kt @@ -35,7 +35,11 @@ class CivilizationInfo { var greatPeople = GreatPersonManager() var scienceVictory = ScienceVictoryManager() var diplomacy = HashMap() - var cities = ArrayList() + + // if we only use lists, and change the list each time the cities are changed, + // we won't get concurrent modification exceptions. + // This is basically a way to ensure our lists are immutable. + var cities = listOf() var exploredTiles = HashSet() constructor() @@ -58,7 +62,7 @@ class CivilizationInfo { toReturn.greatPeople=greatPeople.clone() toReturn.scienceVictory = scienceVictory.clone() toReturn.diplomacy.putAll(diplomacy.values.map { it.clone() }.associateBy { it.otherCivName }) - toReturn.cities.addAll(cities.map { it.clone() }) + toReturn.cities = cities.map { it.clone() } toReturn.exploredTiles.addAll(exploredTiles) return toReturn }