perf: Map modifiers by type for fast lookup

This commit is contained in:
yairm210 2024-09-08 16:54:08 +03:00
parent 563925bbf5
commit 457ee1aff7
4 changed files with 25 additions and 20 deletions

View File

@ -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) {

View File

@ -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) }

View File

@ -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<Stat, Float> by lazy {
val map = HashMap<Stat, Float>()
for (stat in Stat.values()) {
val statCostModifiers: EnumMap<Stat, Float> by lazy {
val map = EnumMap<Stat, Float>(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
}

View File

@ -28,6 +28,7 @@ class Unique(val text: String, val sourceObjectType: UniqueTarget? = null, val s
else Stats.parse(firstStatParam)
}
val modifiers: List<Unique> = text.getModifiers()
val modifiersMap: Map<UniqueType, List<Unique>> = 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