From 6cc5a87f428dcf9e102fc342be6f5df87f456c67 Mon Sep 17 00:00:00 2001 From: yairm210 Date: Tue, 5 Nov 2024 08:53:09 +0200 Subject: [PATCH] perf(memory): Save stateForConditionals on tile for single-create --- core/src/com/unciv/logic/map/tile/Tile.kt | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/core/src/com/unciv/logic/map/tile/Tile.kt b/core/src/com/unciv/logic/map/tile/Tile.kt index a8e54ba5ca724..1de0a29d31c4e 100644 --- a/core/src/com/unciv/logic/map/tile/Tile.kt +++ b/core/src/com/unciv/logic/map/tile/Tile.kt @@ -366,16 +366,17 @@ class Tile : IsPartOfGameInfoSerialization, Json.Serializable { fun isRoughTerrain() = allTerrains.any { it.isRough() } + private val stateThisTile: StateForConditionals = StateForConditionals(tile = this) /** Checks whether any of the TERRAINS of this tile has a certain unique */ - fun terrainHasUnique(uniqueType: UniqueType, state: StateForConditionals = StateForConditionals(tile = this)) = + fun terrainHasUnique(uniqueType: UniqueType, state: StateForConditionals = stateThisTile) = terrainUniqueMap.getMatchingUniques(uniqueType, state).any() /** Get all uniques of this type that any TERRAIN on this tile has */ - fun getTerrainMatchingUniques(uniqueType: UniqueType, stateForConditionals: StateForConditionals = StateForConditionals(tile = this) ): Sequence { + fun getTerrainMatchingUniques(uniqueType: UniqueType, stateForConditionals: StateForConditionals = stateThisTile ): Sequence { return terrainUniqueMap.getMatchingUniques(uniqueType, stateForConditionals) } /** Get all uniques of this type that any part of this tile has: terrains, improvement, resource */ - fun getMatchingUniques(uniqueType: UniqueType, stateForConditionals: StateForConditionals = StateForConditionals(tile=this)): Sequence { + fun getMatchingUniques(uniqueType: UniqueType, stateForConditionals: StateForConditionals = stateThisTile): Sequence { var uniques = getTerrainMatchingUniques(uniqueType, stateForConditionals) if (getUnpillagedImprovement() != null) { val tileImprovement = getTileImprovement() @@ -549,7 +550,7 @@ class Tile : IsPartOfGameInfoSerialization, Json.Serializable { if (naturalWonder != null) bonus += getNaturalWonder().defenceBonus val tileImprovement = getUnpillagedTileImprovement() if (tileImprovement != null && includeImprovementBonus) { - for (unique in tileImprovement.getMatchingUniques(UniqueType.DefensiveBonus, StateForConditionals(tile = this))) + for (unique in tileImprovement.getMatchingUniques(UniqueType.DefensiveBonus, stateThisTile)) bonus += unique.params[0].toFloat() / 100 } return bonus @@ -766,7 +767,7 @@ class Tile : IsPartOfGameInfoSerialization, Json.Serializable { return } - for (unique in newResource.getMatchingUniques(UniqueType.ResourceAmountOnTiles, StateForConditionals(tile = this))) { + for (unique in newResource.getMatchingUniques(UniqueType.ResourceAmountOnTiles, stateThisTile)) { if (matchesTerrainFilter(unique.params[0])) { resourceAmount = unique.params[1].toInt() return