From b0dff6a71cc71d3692af30ac34adfd10fdf9e19b Mon Sep 17 00:00:00 2001 From: Xander Lenstra <71121390+xlenstra@users.noreply.github.com> Date: Fri, 17 Sep 2021 07:44:48 +0200 Subject: [PATCH] Fix bugs and crashes (#5245) * Fixed crash when liberating city-states * Fixed crash & building duplication from 'legalism' policy --- .../logic/city/CityInfoConquestFunctions.kt | 4 ++-- core/src/com/unciv/logic/city/CityStats.kt | 7 +++++-- .../logic/civilization/CivConstructions.kt | 19 ++++++++++--------- 3 files changed, 17 insertions(+), 13 deletions(-) diff --git a/core/src/com/unciv/logic/city/CityInfoConquestFunctions.kt b/core/src/com/unciv/logic/city/CityInfoConquestFunctions.kt index 40f5a32632..3b0bc80106 100644 --- a/core/src/com/unciv/logic/city/CityInfoConquestFunctions.kt +++ b/core/src/com/unciv/logic/city/CityInfoConquestFunctions.kt @@ -87,9 +87,9 @@ class CityInfoConquestFunctions(val city: CityInfo){ val reconqueredCityWhileStillInResistance = previousOwner == conqueringCiv.civName && resistanceCounter != 0 - this@CityInfoConquestFunctions.moveToCiv(receivingCiv) - destroyBuildingsOnCapture() + + this@CityInfoConquestFunctions.moveToCiv(receivingCiv) Battle.destroyIfDefeated(conqueredCiv, conqueringCiv) diff --git a/core/src/com/unciv/logic/city/CityStats.kt b/core/src/com/unciv/logic/city/CityStats.kt index 903135d025..b33bbd2322 100644 --- a/core/src/com/unciv/logic/city/CityStats.kt +++ b/core/src/com/unciv/logic/city/CityStats.kt @@ -153,9 +153,12 @@ class CityStats(val cityInfo: CityInfo) { stats.add(getStatPercentBonusesFromUniques(currentConstruction, cityInfo.civInfo.nation.uniqueObjects.asSequence())) if (currentConstruction is Building - && cityInfo.civInfo.getCapital().cityConstructions.builtBuildings.contains(currentConstruction.name) - && cityInfo.civInfo.hasUnique("+25% Production towards any buildings that already exist in the Capital")) + && cityInfo.civInfo.cities.isNotEmpty() + && cityInfo.civInfo.getCapital().cityConstructions.builtBuildings.contains(currentConstruction.name) + && cityInfo.civInfo.hasUnique("+25% Production towards any buildings that already exist in the Capital") + ) { stats.production += 25f + } return stats } diff --git a/core/src/com/unciv/logic/civilization/CivConstructions.kt b/core/src/com/unciv/logic/civilization/CivConstructions.kt index 64c2a154a4..8130318471 100644 --- a/core/src/com/unciv/logic/civilization/CivConstructions.kt +++ b/core/src/com/unciv/logic/civilization/CivConstructions.kt @@ -15,19 +15,20 @@ class CivConstructions() { val boughtItemsWithIncreasingPrice: Counter = Counter() // Maps to cities to all free buildings they contain - private val freeBuildings: HashMap> = hashMapOf() + private val freeBuildings: HashMap> = hashMapOf() // Maps stats to the cities that have received a building of that stat - // Android Studio says an EnumMap would be better, but that thing isn't serializable. - // I don't know how to suppress that hint :( - private val freeStatBuildingsProvided: HashMap> = hashMapOf() + // We can't use an enum instead of a string, due to the inability of the JSON-parser + // to function properly and forcing this to be an `HashMap>` + // when loading, even if this wasn't the original type, leading to run-time errors. + private val freeStatBuildingsProvided: HashMap> = hashMapOf() // Maps buildings to the cities that have received that building private val freeSpecificBuildingsProvided: HashMap> = hashMapOf() init { for (stat in Stat.values()) { - freeStatBuildingsProvided[stat] = hashSetOf() + freeStatBuildingsProvided[stat.name] = hashSetOf() } } @@ -43,7 +44,7 @@ class CivConstructions() { fun setTransients(civInfo: CivilizationInfo) { this.civInfo = civInfo - + // civInfo.boughtConstructionsWithGloballyIncreasingPrice deprecated since 3.16.15, this is replacement code if (civInfo.boughtConstructionsWithGloballyIncreasingPrice.isNotEmpty()) { for (item in civInfo.boughtConstructionsWithGloballyIncreasingPrice) { @@ -71,7 +72,7 @@ class CivConstructions() { if (civInfo.policies.cultureBuildingsAdded.isNotEmpty()) { for ((cityId, building) in civInfo.policies.cultureBuildingsAdded) { - freeStatBuildingsProvided[Stat.Culture]!!.add(cityId) + freeStatBuildingsProvided[Stat.Culture.name]!!.add(cityId) if (cityId !in freeBuildings) freeBuildings[cityId] = hashSetOf() @@ -126,11 +127,11 @@ class CivConstructions() { private fun addFreeStatBuildings(stat: Stat, amount: Int) { for (city in civInfo.cities.take(amount)) { - if (freeStatBuildingsProvided[stat]!!.contains(city.id) || !city.cityConstructions.hasBuildableStatBuildings(stat)) continue + if (freeStatBuildingsProvided[stat.name]!!.contains(city.id) || !city.cityConstructions.hasBuildableStatBuildings(stat)) continue val builtBuilding = city.cityConstructions.addCheapestBuildableStatBuilding(stat) if (builtBuilding != null) { - freeStatBuildingsProvided[stat]!!.add(city.id) + freeStatBuildingsProvided[stat.name]!!.add(city.id) addFreeBuilding(city.id, builtBuilding) } }