From ee5d06d187850ca578566af69e1f940a4921b5f1 Mon Sep 17 00:00:00 2001 From: yairm210 Date: Fri, 28 Jun 2024 00:32:18 +0300 Subject: [PATCH] perf: small 1% cpu optimizations --- core/src/com/unciv/logic/automation/Automation.kt | 5 +++-- core/src/com/unciv/logic/city/CityFocus.kt | 7 +++++-- .../com/unciv/logic/map/tile/TileImprovementFunctions.kt | 5 ++--- core/src/com/unciv/models/ruleset/Ruleset.kt | 2 ++ 4 files changed, 12 insertions(+), 7 deletions(-) diff --git a/core/src/com/unciv/logic/automation/Automation.kt b/core/src/com/unciv/logic/automation/Automation.kt index 042f200ccf..f005315178 100644 --- a/core/src/com/unciv/logic/automation/Automation.kt +++ b/core/src/com/unciv/logic/automation/Automation.kt @@ -105,7 +105,8 @@ object Automation { if (city.civ.wantsToFocusOn(stat)) yieldStats[stat] *= 2f - yieldStats[stat] *= civPersonality.scaledFocus(PersonalityValue[stat]) + val scaledFocus = civPersonality.scaledFocus(PersonalityValue[stat]) + if (scaledFocus != 1f) yieldStats[stat] *= scaledFocus } // Apply City focus @@ -184,7 +185,7 @@ object Automation { } // Only now do we filter out the constructable units because that's a heavier check .filter { it.isBuildable(city.cityConstructions) } - .toList() + .toList().asSequence() val chosenUnit: BaseUnit if (!city.civ.isAtWar() diff --git a/core/src/com/unciv/logic/city/CityFocus.kt b/core/src/com/unciv/logic/city/CityFocus.kt index 3524035461..9108394e17 100644 --- a/core/src/com/unciv/logic/city/CityFocus.kt +++ b/core/src/com/unciv/logic/city/CityFocus.kt @@ -66,12 +66,15 @@ enum class CityFocus( else -> 1f } + private val statValuesForFocus: List by lazy { + Stat.values().filter { getStatMultiplier(it) != 1f } + } + fun applyWeightTo(stats: Stats) { - for (stat in Stat.values()) { + for (stat in statValuesForFocus) { val currentStat = stats[stat] if (currentStat == 0f) continue val statMultiplier = getStatMultiplier(stat) - if (statMultiplier == 1f) continue stats[stat] = currentStat * statMultiplier } } diff --git a/core/src/com/unciv/logic/map/tile/TileImprovementFunctions.kt b/core/src/com/unciv/logic/map/tile/TileImprovementFunctions.kt index fe1a3ebd30..a5d7add63f 100644 --- a/core/src/com/unciv/logic/map/tile/TileImprovementFunctions.kt +++ b/core/src/com/unciv/logic/map/tile/TileImprovementFunctions.kt @@ -82,10 +82,9 @@ class TileImprovementFunctions(val tile: Tile) { .any { civInfo.getResourceAmount(it.params[1]) < it.params[0].toInt() }) yield(ImprovementBuildingProblem.MissingResources) - val knownFeatureRemovals = tile.ruleset.tileRemovals + val knownFeatureRemovals = tile.ruleset.nonRoadTileRemovals .filter { rulesetImprovement -> - RoadStatus.values().none { it.removeAction == rulesetImprovement.name } - && (rulesetImprovement.techRequired == null || civInfo.tech.isResearched(rulesetImprovement.techRequired!!)) + rulesetImprovement.techRequired == null || civInfo.tech.isResearched(rulesetImprovement.techRequired!!) } if (!canImprovementBeBuiltHere(improvement, tile.hasViewableResource(civInfo), knownFeatureRemovals, stateForConditionals)) diff --git a/core/src/com/unciv/models/ruleset/Ruleset.kt b/core/src/com/unciv/models/ruleset/Ruleset.kt index 7b20cd8fb0..1a2514963b 100644 --- a/core/src/com/unciv/models/ruleset/Ruleset.kt +++ b/core/src/com/unciv/models/ruleset/Ruleset.kt @@ -88,6 +88,8 @@ class Ruleset { } val tileRemovals by lazy { tileImprovements.values.filter { it.name.startsWith(Constants.remove) } } + val nonRoadTileRemovals by lazy { tileRemovals.filter { rulesetImprovement -> + RoadStatus.values().none { it.removeAction == rulesetImprovement.name } } } /** Contains all happiness levels that moving *from* them, to one *below* them, can change uniques that apply */ val allHappinessLevelsThatAffectUniques by lazy {