From 457ee1aff7639b302e54d46e0c2cc441076e5ff0 Mon Sep 17 00:00:00 2001 From: yairm210 Date: Sun, 8 Sep 2024 16:54:08 +0300 Subject: [PATCH] perf: Map modifiers by type for fast lookup --- .../com/unciv/models/ruleset/GlobalUniques.kt | 2 +- core/src/com/unciv/models/ruleset/Policy.kt | 18 +++++++++++------ core/src/com/unciv/models/ruleset/Speed.kt | 20 +++++++++---------- .../com/unciv/models/ruleset/unique/Unique.kt | 5 +++-- 4 files changed, 25 insertions(+), 20 deletions(-) diff --git a/core/src/com/unciv/models/ruleset/GlobalUniques.kt b/core/src/com/unciv/models/ruleset/GlobalUniques.kt index 8d4bc3b0a9..1d06157556 100644 --- a/core/src/com/unciv/models/ruleset/GlobalUniques.kt +++ b/core/src/com/unciv/models/ruleset/GlobalUniques.kt @@ -12,7 +12,7 @@ class GlobalUniques: RulesetObject() { companion object { fun getUniqueSourceDescription(unique: Unique): String { - if (unique.modifiers.none()) + if (unique.modifiers.isEmpty()) return "Global Effect" return when (unique.modifiers.first().type) { diff --git a/core/src/com/unciv/models/ruleset/Policy.kt b/core/src/com/unciv/models/ruleset/Policy.kt index 127cfa9351..1c06a446b1 100644 --- a/core/src/com/unciv/models/ruleset/Policy.kt +++ b/core/src/com/unciv/models/ruleset/Policy.kt @@ -124,12 +124,18 @@ open class Policy : RulesetObject() { } - fun isDisabledByPolicy(rulesetObject: IRulesetObject) = - rulesetObject.getMatchingUniques(UniqueType.OnlyAvailable, StateForConditionals.IgnoreConditionals).any { it.modifiers.any { - it.type == UniqueType.ConditionalBeforePolicyOrBelief && it.params[0] == name - } } || rulesetObject.getMatchingUniques(UniqueType.Unavailable).any { it.modifiers.any { - it.type == UniqueType.ConditionalAfterPolicyOrBelief && it.params[0] == name - } } + fun isDisabledByPolicy(rulesetObject: IRulesetObject): Boolean { + if (rulesetObject.getMatchingUniques(UniqueType.OnlyAvailable, StateForConditionals.IgnoreConditionals).any { + it.getModifiers(UniqueType.ConditionalBeforePolicyOrBelief).any { it.params[0] == name } + }) + return true + + if (rulesetObject.getMatchingUniques(UniqueType.Unavailable, StateForConditionals.IgnoreConditionals).any { + it.getModifiers(UniqueType.ConditionalAfterPolicyOrBelief).any { it.params[0] == name } }) + return true + + return false + } val disabledBuildings = ruleset.buildings.values.filter { isDisabledByPolicy(it) } diff --git a/core/src/com/unciv/models/ruleset/Speed.kt b/core/src/com/unciv/models/ruleset/Speed.kt index 76f8ed4b19..0fe282831c 100644 --- a/core/src/com/unciv/models/ruleset/Speed.kt +++ b/core/src/com/unciv/models/ruleset/Speed.kt @@ -6,6 +6,9 @@ import com.unciv.models.stats.Stat import com.unciv.models.translations.tr import com.unciv.ui.components.fonts.Fonts import com.unciv.ui.screens.civilopediascreen.FormattedLine +import java.util.* +import kotlin.collections.ArrayList +import kotlin.collections.HashMap import kotlin.math.abs class Speed : RulesetObject(), IsPartOfGameInfoSerialization { @@ -32,9 +35,9 @@ class Speed : RulesetObject(), IsPartOfGameInfoSerialization { } } - val statCostModifiers: HashMap by lazy { - val map = HashMap() - for (stat in Stat.values()) { + val statCostModifiers: EnumMap by lazy { + val map = EnumMap(Stat::class.java) + for (stat in Stat.entries) { val modifier = when (stat) { Stat.Production -> productionCostModifier Stat.Gold -> goldCostModifier @@ -81,12 +84,7 @@ class Speed : RulesetObject(), IsPartOfGameInfoSerialization { fun numTotalTurns(): Int = yearsPerTurn.last().untilTurn } -class YearsPerTurn { - var yearInterval: Float = 0f - var untilTurn: Int = 0 - - constructor(yearsPerTurn: Float, turnsPerIncrement: Int) { - this.yearInterval = yearsPerTurn - this.untilTurn = turnsPerIncrement - } +class YearsPerTurn(yearsPerTurn: Float, turnsPerIncrement: Int) { + var yearInterval: Float = yearsPerTurn + var untilTurn: Int = turnsPerIncrement } diff --git a/core/src/com/unciv/models/ruleset/unique/Unique.kt b/core/src/com/unciv/models/ruleset/unique/Unique.kt index 0913271f1e..4f6020bffa 100644 --- a/core/src/com/unciv/models/ruleset/unique/Unique.kt +++ b/core/src/com/unciv/models/ruleset/unique/Unique.kt @@ -28,6 +28,7 @@ class Unique(val text: String, val sourceObjectType: UniqueTarget? = null, val s else Stats.parse(firstStatParam) } val modifiers: List = text.getModifiers() + val modifiersMap: Map> = modifiers.filterNot { it.type == null }.groupBy { it.type!! } val isTimedTriggerable = hasModifier(UniqueType.ConditionalTimedUnique) @@ -45,8 +46,8 @@ class Unique(val text: String, val sourceObjectType: UniqueTarget? = null, val s fun hasFlag(flag: UniqueFlag) = type != null && type.flags.contains(flag) fun isHiddenToUsers() = hasFlag(UniqueFlag.HiddenToUsers) || hasModifier(UniqueType.ModifierHiddenFromUsers) - fun getModifiers(type: UniqueType) = modifiers.asSequence().filter { it.type == type } - fun hasModifier(type: UniqueType) = getModifiers(type).any() + fun getModifiers(type: UniqueType) = modifiersMap[type] ?: emptyList() + fun hasModifier(type: UniqueType) = modifiersMap.containsKey(type) fun isModifiedByGameSpeed() = hasModifier(UniqueType.ModifiedByGameSpeed) fun hasTriggerConditional(): Boolean { if (modifiers.none()) return false