Performance: Removed map lookup every time we want to get a unit's type

This commit is contained in:
Yair Morgenstern 2023-04-03 10:49:50 +03:00
parent bfb869f64f
commit f0ee25dcac
2 changed files with 9 additions and 8 deletions

View File

@ -189,7 +189,7 @@ class MapUnit : IsPartOfGameInfoSerialization {
}
val type: UnitType
get() = baseUnit.getType()
get() = baseUnit.type
fun baseUnit(): BaseUnit = baseUnit
fun getMovementString(): String =

View File

@ -34,7 +34,8 @@ class BaseUnit : RulesetObject(), INonPerpetualConstruction {
var range: Int = 2
var interceptRange = 0
var unitType: String = ""
fun getType() = ruleset.unitTypes[unitType]!!
val type by lazy { ruleset.unitTypes[unitType]!! }
override var requiredTech: String? = null
private var requiredResource: String? = null
@ -296,7 +297,7 @@ class BaseUnit : RulesetObject(), INonPerpetualConstruction {
"Great Person" -> isGreatPerson()
"Religious" -> hasUnique(UniqueType.ReligiousUnit)
else -> {
if (getType().matchesFilter(filter)) return true
if (type.matchesFilter(filter)) return true
if (
// Uniques using these kinds of filters should be deprecated and replaced with adjective-only parameters
filter.endsWith(" units")
@ -313,7 +314,7 @@ class BaseUnit : RulesetObject(), INonPerpetualConstruction {
fun isNuclearWeapon() = hasUnique(UniqueType.NuclearWeapon)
fun movesLikeAirUnits() = getType().getMovementType() == UnitMovementType.Air
fun movesLikeAirUnits() = type.getMovementType() == UnitMovementType.Air
/** Returns resource requirements from both uniques and requiredResource field */
override fun getResourceRequirements(): HashMap<String, Int> = resourceRequirementsInternal
@ -333,15 +334,15 @@ class BaseUnit : RulesetObject(), INonPerpetualConstruction {
fun isMilitary() = isRanged() || isMelee()
fun isCivilian() = !isMilitary()
val isLandUnitInternal by lazy { getType().isLandUnit() }
val isLandUnitInternal by lazy { type.isLandUnit() }
fun isLandUnit() = isLandUnitInternal
fun isWaterUnit() = getType().isWaterUnit()
fun isAirUnit() = getType().isAirUnit()
fun isWaterUnit() = type.isWaterUnit()
fun isAirUnit() = type.isAirUnit()
fun isProbablySiegeUnit() =
(
isRanged()
&& (uniqueObjects + getType().uniqueObjects)
&& (uniqueObjects + type.uniqueObjects)
.any { it.isOfType(UniqueType.Strength)
&& it.params[0].toInt() > 0
&& it.conditionals.any { conditional -> conditional.isOfType(UniqueType.ConditionalVsCity) }