Skip to content

Commit

Permalink
Performance improvements - inspired by ANR errors for this callstack (y…
Browse files Browse the repository at this point in the history
  • Loading branch information
yairm210 authored Jan 3, 2022
1 parent e68bd14 commit 0e64ff5
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 8 deletions.
5 changes: 3 additions & 2 deletions core/src/com/unciv/logic/city/CityInfo.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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]!!
Expand Down
5 changes: 4 additions & 1 deletion core/src/com/unciv/logic/civilization/CivConstructions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,10 @@ class CivConstructions {
fun getFreeBuildings(cityId: String): HashSet<String> {
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
}
Expand Down
3 changes: 2 additions & 1 deletion core/src/com/unciv/logic/civilization/CivilizationInfo.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 4 additions & 2 deletions core/src/com/unciv/models/ruleset/Building.kt
Original file line number Diff line number Diff line change
Expand Up @@ -792,13 +792,15 @@ class Building : RulesetStatsObject(), INonPerpetualConstruction {

fun isSellable() = !isAnyWonder() && !hasUnique(UniqueType.Unsellable)

override fun getResourceRequirements(): HashMap<String, Int> {
override fun getResourceRequirements(): HashMap<String, Int> = resourceRequirementsInternal

private val resourceRequirementsInternal: HashMap<String, Int> by lazy {
val resourceRequirements = HashMap<String, Int>()
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 {
Expand Down
6 changes: 4 additions & 2 deletions core/src/com/unciv/models/ruleset/unit/BaseUnit.kt
Original file line number Diff line number Diff line change
Expand Up @@ -582,13 +582,15 @@ class BaseUnit : RulesetObject(), INonPerpetualConstruction {

fun movesLikeAirUnits() = getType().getMovementType() == UnitMovementType.Air

override fun getResourceRequirements(): HashMap<String, Int> {
override fun getResourceRequirements(): HashMap<String, Int> = resourceRequirementsInternal

private val resourceRequirementsInternal: HashMap<String, Int> by lazy {
val resourceRequirements = HashMap<String, Int>()
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 {
Expand Down

0 comments on commit 0e64ff5

Please sign in to comment.