perf(minor): TileLayerCityButton faster "no-op" check

This commit is contained in:
yairm210 2024-12-15 21:45:20 +02:00
parent 1a0bb512bf
commit fdb8026c1b
3 changed files with 12 additions and 7 deletions

View File

@ -34,6 +34,8 @@ import com.unciv.utils.DebugUtils
import com.unciv.utils.Log import com.unciv.utils.Log
import com.unciv.utils.withItem import com.unciv.utils.withItem
import com.unciv.utils.withoutItem import com.unciv.utils.withoutItem
import kotlin.collections.ArrayList
import kotlin.collections.HashSet
import kotlin.math.abs import kotlin.math.abs
import kotlin.math.min import kotlin.math.min
import kotlin.random.Random 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 // 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) { cachedTerrainData = tileMap.tileUniqueMapCache.getOrPut(terrainNameList) {
TileMap.TerrainListData(UniqueMap(allTerrains.flatMap { it.uniqueObjects }), TileMap.TerrainListData(
terrainNameList.toSet()) UniqueMap(allTerrains.flatMap { it.uniqueObjects }),
terrainNameList.toSet()
)
} }
} }

View File

@ -21,18 +21,18 @@ class TileLayerCityButton(tileGroup: TileGroup, size: Float) : TileLayer(tileGro
} }
override fun act(delta: Float) { override fun act(delta: Float) {
if (tileGroup.tile.isCityCenter()) if (tile.isCityCenter())
super.act(delta) super.act(delta)
} }
override fun hit(x: Float, y: Float, touchable: Boolean): Actor? { override fun hit(x: Float, y: Float, touchable: Boolean): Actor? {
if (tileGroup.tile.isCityCenter()) if (tile.isCityCenter())
return super.hit(x, y, touchable) return super.hit(x, y, touchable)
return null return null
} }
override fun draw(batch: Batch?, parentAlpha: Float) { override fun draw(batch: Batch?, parentAlpha: Float) {
if (tileGroup.tile.isCityCenter()) if (tile.isCityCenter())
super.draw(batch, parentAlpha) super.draw(batch, parentAlpha)
} }

View File

@ -105,7 +105,8 @@ class TileLayerTerrain(tileGroup: TileGroup, size: Float) : TileLayer(tileGroup,
val vectorToNeighbor = neighborTile.position.cpy().sub(originTile.position) val vectorToNeighbor = neighborTile.position.cpy().sub(originTile.position)
val direction = NeighborDirection.fromVector(vectorToNeighbor) val direction = NeighborDirection.fromVector(vectorToNeighbor)
?: return emptySequence() ?: 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() // Required for performance - full matchesFilter is too expensive for something that needs to run every update()
fun matchesFilterMinimal(originTile: Tile, filter: String): Boolean { fun matchesFilterMinimal(originTile: Tile, filter: String): Boolean {
@ -114,7 +115,7 @@ class TileLayerTerrain(tileGroup: TileGroup, size: Float) : TileLayer(tileGroup,
return false return false
} }
return possibleEdgeFiles.asSequence().filter { return possibleEdgeImages.asSequence().filter {
if (!matchesFilterMinimal(originTile, it.originTileFilter)) return@filter false if (!matchesFilterMinimal(originTile, it.originTileFilter)) return@filter false
if (!matchesFilterMinimal(neighborTile, it.destinationTileFilter)) return@filter false if (!matchesFilterMinimal(neighborTile, it.destinationTileFilter)) return@filter false
return@filter true return@filter true