diff --git a/core/src/com/unciv/logic/city/CityInfo.kt b/core/src/com/unciv/logic/city/CityInfo.kt index 37ae8238f8935..86a46b30f701d 100644 --- a/core/src/com/unciv/logic/city/CityInfo.kt +++ b/core/src/com/unciv/logic/city/CityInfo.kt @@ -322,10 +322,11 @@ class CityInfo { } } } - + + val freeBuildings = civInfo.civConstructions.getFreeBuildings(id) for (building in cityConstructions.getBuiltBuildings()) { // Free buildings cost no resources - if (building.name in civInfo.civConstructions.getFreeBuildings(id)) + if (building.name in freeBuildings) continue for ((resourceName, amount) in building.getResourceRequirements()) { val resource = getRuleset().tileResources[resourceName]!! diff --git a/core/src/com/unciv/logic/civilization/CivConstructions.kt b/core/src/com/unciv/logic/civilization/CivConstructions.kt index f856210f968fd..3a9ba4dbf4068 100644 --- a/core/src/com/unciv/logic/civilization/CivConstructions.kt +++ b/core/src/com/unciv/logic/civilization/CivConstructions.kt @@ -59,7 +59,10 @@ class CivConstructions { fun getFreeBuildings(cityId: String): HashSet { val toReturn = freeBuildings[cityId] ?: hashSetOf() for (city in civInfo.cities) { - toReturn.addAll(city.cityConstructions.freeBuildingsProvidedFromThisCity[cityId] ?: hashSetOf()) + val freeBuildingsProvided = + city.cityConstructions.freeBuildingsProvidedFromThisCity[cityId] + if (freeBuildingsProvided != null) + toReturn.addAll(freeBuildingsProvided) } return toReturn } diff --git a/core/src/com/unciv/logic/civilization/CivilizationInfo.kt b/core/src/com/unciv/logic/civilization/CivilizationInfo.kt index 5eb4e0f24ea4d..83da42c537f2e 100644 --- a/core/src/com/unciv/logic/civilization/CivilizationInfo.kt +++ b/core/src/com/unciv/logic/civilization/CivilizationInfo.kt @@ -1055,8 +1055,9 @@ class CivilizationInfo { fun addUnit(unitName: String, city: CityInfo? = null): MapUnit? { if (cities.isEmpty()) return null - val cityToAddTo = city ?: cities.random() if (!gameInfo.ruleSet.units.containsKey(unitName)) return null + + val cityToAddTo = city ?: cities.random() val unit = getEquivalentUnit(unitName) val placedUnit = placeUnitNearTile(cityToAddTo.location, unit.name) // silently bail if no tile to place the unit is found diff --git a/core/src/com/unciv/models/ruleset/Building.kt b/core/src/com/unciv/models/ruleset/Building.kt index 68e3231cd34e9..89b06770afbff 100644 --- a/core/src/com/unciv/models/ruleset/Building.kt +++ b/core/src/com/unciv/models/ruleset/Building.kt @@ -792,13 +792,15 @@ class Building : RulesetStatsObject(), INonPerpetualConstruction { fun isSellable() = !isAnyWonder() && !hasUnique(UniqueType.Unsellable) - override fun getResourceRequirements(): HashMap { + override fun getResourceRequirements(): HashMap = resourceRequirementsInternal + + private val resourceRequirementsInternal: HashMap by lazy { val resourceRequirements = HashMap() if (requiredResource != null) resourceRequirements[requiredResource!!] = 1 for (unique in uniqueObjects) if (unique.isOfType(UniqueType.ConsumesResources)) resourceRequirements[unique.params[1]] = unique.params[0].toInt() - return resourceRequirements + resourceRequirements } override fun requiresResource(resource: String): Boolean { diff --git a/core/src/com/unciv/models/ruleset/unit/BaseUnit.kt b/core/src/com/unciv/models/ruleset/unit/BaseUnit.kt index 8ba5b9c9c64a4..ffe569861f2e6 100644 --- a/core/src/com/unciv/models/ruleset/unit/BaseUnit.kt +++ b/core/src/com/unciv/models/ruleset/unit/BaseUnit.kt @@ -582,13 +582,15 @@ class BaseUnit : RulesetObject(), INonPerpetualConstruction { fun movesLikeAirUnits() = getType().getMovementType() == UnitMovementType.Air - override fun getResourceRequirements(): HashMap { + override fun getResourceRequirements(): HashMap = resourceRequirementsInternal + + private val resourceRequirementsInternal: HashMap by lazy { val resourceRequirements = HashMap() if (requiredResource != null) resourceRequirements[requiredResource!!] = 1 for (unique in uniqueObjects) if (unique.isOfType(UniqueType.ConsumesResources)) resourceRequirements[unique.params[1]] = unique.params[0].toInt() - return resourceRequirements + resourceRequirements } override fun requiresResource(resource: String): Boolean {