minor perf: Map getTile does one lookup instead of two

This commit is contained in:
yairm210 2024-08-07 22:24:23 +03:00
parent 5eddb505d2
commit 0c4814eae4
2 changed files with 12 additions and 11 deletions

View File

@ -277,8 +277,8 @@ class TileMap(initialCapacity: Int = 10) : IsPartOfGameInfoSerialization {
/** @return tile at hex coordinates ([x],[y]) or null if they are outside the map. Respects map edges and world wrap. */ /** @return tile at hex coordinates ([x],[y]) or null if they are outside the map. Respects map edges and world wrap. */
fun getIfTileExistsOrNull(x: Int, y: Int): Tile? { fun getIfTileExistsOrNull(x: Int, y: Int): Tile? {
if (contains(x, y)) val tile = getOrNull(x, y)
return get(x, y) if (tile != null) return tile
if (!mapParameters.worldWrap) if (!mapParameters.worldWrap)
return null return null
@ -287,14 +287,15 @@ class TileMap(initialCapacity: Int = 10) : IsPartOfGameInfoSerialization {
if (mapParameters.shape == MapShape.rectangular) if (mapParameters.shape == MapShape.rectangular)
radius = mapParameters.mapSize.width / 2 radius = mapParameters.mapSize.width / 2
//tile is outside of the map // Maybe tile is "outside of the map" in world wrap.
if (contains(x + radius, y - radius)) { //tile is on right side
//get tile wrapped around from right to left // A. Get tile wrapped around from right to left
return get(x + radius, y - radius) val rightSideTile = getOrNull(x + radius, y - radius)
} else if (contains(x - radius, y + radius)) { //tile is on left side if (rightSideTile != null) return rightSideTile
//get tile wrapped around from left to right
return get(x - radius, y + radius) // B. Get tile wrapped around from left to right
} val leftSideTile = getOrNull(x - radius, y + radius)
if (leftSideTile != null) return leftSideTile
return null return null
} }

View File

@ -136,6 +136,7 @@ class TestGame {
civInfo.gameInfo = gameInfo civInfo.gameInfo = gameInfo
civInfo.setNameForUnitTests(nation.name) civInfo.setNameForUnitTests(nation.name)
if (isPlayer) civInfo.playerType = PlayerType.Human if (isPlayer) civInfo.playerType = PlayerType.Human
gameInfo.civilizations.add(civInfo)
civInfo.setTransients() civInfo.setTransients()
// Add 1 tech to the player so the era is computed correctly // Add 1 tech to the player so the era is computed correctly
@ -143,7 +144,6 @@ class TestGame {
if (cityStateType != null) { if (cityStateType != null) {
civInfo.cityStateFunctions.initCityState(ruleset, "Ancient era", emptyList()) civInfo.cityStateFunctions.initCityState(ruleset, "Ancient era", emptyList())
} }
gameInfo.civilizations.add(civInfo)
return civInfo return civInfo
} }