From fdb8026c1bbaaf4694475682ec59f007dc42bd0e Mon Sep 17 00:00:00 2001 From: yairm210 Date: Sun, 15 Dec 2024 21:45:20 +0200 Subject: [PATCH] perf(minor): TileLayerCityButton faster "no-op" check --- core/src/com/unciv/logic/map/tile/Tile.kt | 8 ++++++-- .../components/tilegroups/layers/TileLayerCityButton.kt | 6 +++--- .../ui/components/tilegroups/layers/TileLayerTerrain.kt | 5 +++-- 3 files changed, 12 insertions(+), 7 deletions(-) diff --git a/core/src/com/unciv/logic/map/tile/Tile.kt b/core/src/com/unciv/logic/map/tile/Tile.kt index 0240a4affd..8511c71503 100644 --- a/core/src/com/unciv/logic/map/tile/Tile.kt +++ b/core/src/com/unciv/logic/map/tile/Tile.kt @@ -34,6 +34,8 @@ import com.unciv.utils.DebugUtils import com.unciv.utils.Log import com.unciv.utils.withItem import com.unciv.utils.withoutItem +import kotlin.collections.ArrayList +import kotlin.collections.HashSet import kotlin.math.abs import kotlin.math.min import kotlin.random.Random @@ -836,8 +838,10 @@ class Tile : IsPartOfGameInfoSerialization, Json.Serializable { // List hash is function of all its items, so the same items in the same order will always give the same hash cachedTerrainData = tileMap.tileUniqueMapCache.getOrPut(terrainNameList) { - TileMap.TerrainListData(UniqueMap(allTerrains.flatMap { it.uniqueObjects }), - terrainNameList.toSet()) + TileMap.TerrainListData( + UniqueMap(allTerrains.flatMap { it.uniqueObjects }), + terrainNameList.toSet() + ) } } diff --git a/core/src/com/unciv/ui/components/tilegroups/layers/TileLayerCityButton.kt b/core/src/com/unciv/ui/components/tilegroups/layers/TileLayerCityButton.kt index 1b92a43b38..0375b08d47 100644 --- a/core/src/com/unciv/ui/components/tilegroups/layers/TileLayerCityButton.kt +++ b/core/src/com/unciv/ui/components/tilegroups/layers/TileLayerCityButton.kt @@ -21,18 +21,18 @@ class TileLayerCityButton(tileGroup: TileGroup, size: Float) : TileLayer(tileGro } override fun act(delta: Float) { - if (tileGroup.tile.isCityCenter()) + if (tile.isCityCenter()) super.act(delta) } override fun hit(x: Float, y: Float, touchable: Boolean): Actor? { - if (tileGroup.tile.isCityCenter()) + if (tile.isCityCenter()) return super.hit(x, y, touchable) return null } override fun draw(batch: Batch?, parentAlpha: Float) { - if (tileGroup.tile.isCityCenter()) + if (tile.isCityCenter()) super.draw(batch, parentAlpha) } diff --git a/core/src/com/unciv/ui/components/tilegroups/layers/TileLayerTerrain.kt b/core/src/com/unciv/ui/components/tilegroups/layers/TileLayerTerrain.kt index 2c62c6ebbe..cc0cd7bf92 100644 --- a/core/src/com/unciv/ui/components/tilegroups/layers/TileLayerTerrain.kt +++ b/core/src/com/unciv/ui/components/tilegroups/layers/TileLayerTerrain.kt @@ -105,7 +105,8 @@ class TileLayerTerrain(tileGroup: TileGroup, size: Float) : TileLayer(tileGroup, val vectorToNeighbor = neighborTile.position.cpy().sub(originTile.position) val direction = NeighborDirection.fromVector(vectorToNeighbor) ?: return emptySequence() - val possibleEdgeFiles = strings.edgeImagesByPosition[direction] ?: return emptySequence() + + val possibleEdgeImages = strings.edgeImagesByPosition[direction] ?: return emptySequence() // Required for performance - full matchesFilter is too expensive for something that needs to run every update() fun matchesFilterMinimal(originTile: Tile, filter: String): Boolean { @@ -114,7 +115,7 @@ class TileLayerTerrain(tileGroup: TileGroup, size: Float) : TileLayer(tileGroup, return false } - return possibleEdgeFiles.asSequence().filter { + return possibleEdgeImages.asSequence().filter { if (!matchesFilterMinimal(originTile, it.originTileFilter)) return@filter false if (!matchesFilterMinimal(neighborTile, it.destinationTileFilter)) return@filter false return@filter true