From f0ee25dcacd8ee2446e7be8b5261888cdceaec3b Mon Sep 17 00:00:00 2001 From: Yair Morgenstern Date: Mon, 3 Apr 2023 10:49:50 +0300 Subject: [PATCH] Performance: Removed map lookup every time we want to get a unit's type --- core/src/com/unciv/logic/map/mapunit/MapUnit.kt | 2 +- .../src/com/unciv/models/ruleset/unit/BaseUnit.kt | 15 ++++++++------- 2 files changed, 9 insertions(+), 8 deletions(-) diff --git a/core/src/com/unciv/logic/map/mapunit/MapUnit.kt b/core/src/com/unciv/logic/map/mapunit/MapUnit.kt index 065665b15d..fb4e4c1bfc 100644 --- a/core/src/com/unciv/logic/map/mapunit/MapUnit.kt +++ b/core/src/com/unciv/logic/map/mapunit/MapUnit.kt @@ -189,7 +189,7 @@ class MapUnit : IsPartOfGameInfoSerialization { } val type: UnitType - get() = baseUnit.getType() + get() = baseUnit.type fun baseUnit(): BaseUnit = baseUnit fun getMovementString(): String = diff --git a/core/src/com/unciv/models/ruleset/unit/BaseUnit.kt b/core/src/com/unciv/models/ruleset/unit/BaseUnit.kt index 437b728b34..03e6f3d374 100644 --- a/core/src/com/unciv/models/ruleset/unit/BaseUnit.kt +++ b/core/src/com/unciv/models/ruleset/unit/BaseUnit.kt @@ -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 = 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) }