From a495e5c8fe0ecdaca4a4f71afa7a264bae85b7da Mon Sep 17 00:00:00 2001 From: Yair Morgenstern Date: Mon, 7 Feb 2022 11:01:44 +0200 Subject: [PATCH] Memory and performance improvements for tileGroup imagelocation --- core/src/com/unciv/ui/tilegroups/TileGroup.kt | 13 +++++++------ core/src/com/unciv/ui/tilegroups/TileGroupIcons.kt | 2 +- core/src/com/unciv/ui/tilegroups/TileSetStrings.kt | 7 ++++--- 3 files changed, 12 insertions(+), 10 deletions(-) diff --git a/core/src/com/unciv/ui/tilegroups/TileGroup.kt b/core/src/com/unciv/ui/tilegroups/TileGroup.kt index 350d10ed9c..a02931cd80 100644 --- a/core/src/com/unciv/ui/tilegroups/TileGroup.kt +++ b/core/src/com/unciv/ui/tilegroups/TileGroup.kt @@ -203,8 +203,9 @@ open class TileGroup(var tileInfo: TileInfo, val tileSetStrings:TileSetStrings, crosshairImage.isVisible = true } + private fun getTileBaseImageLocations(viewingCiv: CivilizationInfo?): List { - if (viewingCiv == null && !showEntireMap) return listOf(tileSetStrings.orFallback { hexagon } ) + if (viewingCiv == null && !showEntireMap) return tileSetStrings.hexagonList if (tileInfo.naturalWonder != null) return listOf(tileSetStrings.orFallback { getTile(tileInfo.naturalWonder!!) }) val shownImprovement = tileInfo.getShownImprovement(viewingCiv) @@ -213,10 +214,10 @@ open class TileGroup(var tileInfo: TileInfo, val tileSetStrings:TileSetStrings, val shouldShowResource = UncivGame.Current.settings.showPixelImprovements && tileInfo.resource != null && (showEntireMap || viewingCiv == null || tileInfo.hasViewableResource(viewingCiv)) - var resourceAndImprovementSequence = sequenceOf() - if (shouldShowResource) resourceAndImprovementSequence += sequenceOf(tileInfo.resource) - if (shouldShowImprovement) resourceAndImprovementSequence += sequenceOf(shownImprovement) - resourceAndImprovementSequence = resourceAndImprovementSequence.filterNotNull() + val resourceAndImprovementSequence = sequence { + if (shouldShowResource) yield(tileInfo.resource) + if (shouldShowImprovement) yield(shownImprovement) + }.filterNotNull() val terrainImages = (sequenceOf(tileInfo.baseTerrain) + tileInfo.terrainFeatures.asSequence()).filterNotNull() val allTogether = (terrainImages + resourceAndImprovementSequence).joinToString("+") @@ -296,7 +297,7 @@ open class TileGroup(var tileInfo: TileInfo, val tileSetStrings:TileSetStrings, } if (tileBaseImages.isEmpty()) { // Absolutely nothing! This is for the 'default' tileset - val image = ImageGetter.getImage(tileSetStrings.orFallback { hexagon }) + val image = ImageGetter.getImage(tileSetStrings.hexagon) tileBaseImages.add(image) baseLayerGroup.addActor(image) setHexagonImageSize(image) diff --git a/core/src/com/unciv/ui/tilegroups/TileGroupIcons.kt b/core/src/com/unciv/ui/tilegroups/TileGroupIcons.kt index 5310cc0c9b..5b4fe0de17 100644 --- a/core/src/com/unciv/ui/tilegroups/TileGroupIcons.kt +++ b/core/src/com/unciv/ui/tilegroups/TileGroupIcons.kt @@ -162,7 +162,7 @@ class TileGroupIcons(val tileGroup: TileGroup) { if (tileGroup.resourceImage != null) { // This could happen on any turn, since resources need certain techs to reveal them val shouldDisplayResource = - if (tileGroup.showEntireMap) tileGroup.tileInfo.resource != null + if (tileGroup.showEntireMap) showResourcesAndImprovements else showResourcesAndImprovements && tileGroup.tileInfo.hasViewableResource(UncivGame.Current.worldScreen.viewingCiv) tileGroup.resourceImage!!.isVisible = shouldDisplayResource diff --git a/core/src/com/unciv/ui/tilegroups/TileSetStrings.kt b/core/src/com/unciv/ui/tilegroups/TileSetStrings.kt index 30417a375a..b21089bff1 100644 --- a/core/src/com/unciv/ui/tilegroups/TileSetStrings.kt +++ b/core/src/com/unciv/ui/tilegroups/TileSetStrings.kt @@ -17,13 +17,14 @@ class TileSetStrings(tileSet: String = UncivGame.Current.settings.tileSet, fallb val tileSetLocation = "TileSets/$tileSet/" val tileSetConfig = TileSetCache[tileSet] ?: TileSetConfig() - val hexagon = tileSetLocation + "Hexagon" + // These need to be by lazy since the orFallback expects a tileset, which it may not get. + val hexagon: String by lazy { orFallback {tileSetLocation + "Hexagon"} } + val hexagonList by lazy { listOf(hexagon) } val crosshatchHexagon = tileSetLocation + "CrosshatchHexagon" val cityOverlay = tileSetLocation + "CityOverlay" val roadsMap = RoadStatus.values() .filterNot { it == RoadStatus.None } - .map { it to tileSetLocation + it.name } - .toMap() + .associateWith { tileSetLocation + it.name } val naturalWonderOverlay = tileSetLocation + "NaturalWonderOverlay" val tilesLocation = tileSetLocation + "Tiles/"