From cbbec40e0020f17c69c9496c01fa672d2220303f Mon Sep 17 00:00:00 2001 From: yairm210 Date: Tue, 29 Oct 2024 15:12:33 +0200 Subject: [PATCH] perf(memory): Don't store city distances intermediately --- .../automation/civilization/NextTurnAutomation.kt | 12 ++++++++---- .../unciv/logic/automation/unit/WorkerAutomation.kt | 2 +- .../components/tilegroups/layers/TileLayerTerrain.kt | 2 +- 3 files changed, 10 insertions(+), 6 deletions(-) diff --git a/core/src/com/unciv/logic/automation/civilization/NextTurnAutomation.kt b/core/src/com/unciv/logic/automation/civilization/NextTurnAutomation.kt index be5e2bbcc39c8..39eee74b7715f 100644 --- a/core/src/com/unciv/logic/automation/civilization/NextTurnAutomation.kt +++ b/core/src/com/unciv/logic/automation/civilization/NextTurnAutomation.kt @@ -647,13 +647,17 @@ object NextTurnAutomation { fun getClosestCities(civ1: Civilization, civ2: Civilization): CityDistance? { if (civ1.cities.isEmpty() || civ2.cities.isEmpty()) return null + + var minDistance: CityDistance? = null - val cityDistances = arrayListOf() for (civ1city in civ1.cities) - for (civ2city in civ2.cities) - cityDistances += CityDistance(civ1city, civ2city, + for (civ2city in civ2.cities){ + val currentDistance = CityDistance(civ1city, civ2city, civ1city.getCenterTile().aerialDistanceTo(civ2city.getCenterTile())) + if (minDistance == null || currentDistance.aerialDistance < minDistance.aerialDistance) + minDistance = currentDistance + } - return cityDistances.minByOrNull { it.aerialDistance }!! + return minDistance } } diff --git a/core/src/com/unciv/logic/automation/unit/WorkerAutomation.kt b/core/src/com/unciv/logic/automation/unit/WorkerAutomation.kt index dfb125254efd7..ff847556eb5f5 100644 --- a/core/src/com/unciv/logic/automation/unit/WorkerAutomation.kt +++ b/core/src/com/unciv/logic/automation/unit/WorkerAutomation.kt @@ -531,7 +531,7 @@ class WorkerAutomation( if (tile.hasViewableResource(civInfo)) valueOfFort -= 1 // if this place is not perfect, let's see if there is a better one - val nearestTiles = tile.getTilesInDistance(1).filter { it.owningCity?.civ == civInfo }.toList() + val nearestTiles = tile.getTilesInDistance(1).filter { it.owningCity?.civ == civInfo } for (closeTile in nearestTiles) { // don't build forts too close to the cities if (closeTile.isCityCenter()) { 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 22b639bbea73e..b15027ef3ad14 100644 --- a/core/src/com/unciv/ui/components/tilegroups/layers/TileLayerTerrain.kt +++ b/core/src/com/unciv/ui/components/tilegroups/layers/TileLayerTerrain.kt @@ -54,7 +54,7 @@ class TileLayerTerrain(tileGroup: TileGroup, size: Float) : TileLayer(tileGroup, val baseHexagon = if (strings().tileSetConfig.useColorAsBaseTerrain) listOf(strings().hexagon) - else listOf() + else emptyList() val tile = tileGroup.tile