From d0a3b37f741d5a97cf3be96feaf149ec07facdac Mon Sep 17 00:00:00 2001 From: yairm210 Date: Thu, 5 Dec 2024 22:19:15 +0200 Subject: [PATCH] Resolved #12572 - "Stats from tiles" uniques work with terrain + improvement filter combos --- core/src/com/unciv/logic/map/tile/Tile.kt | 7 +++---- .../com/unciv/logic/map/tile/TileStatFunctions.kt | 14 +++++++------- tests/src/com/unciv/uniques/GlobalUniquesTests.kt | 13 +++++++++++++ 3 files changed, 23 insertions(+), 11 deletions(-) diff --git a/core/src/com/unciv/logic/map/tile/Tile.kt b/core/src/com/unciv/logic/map/tile/Tile.kt index 2f3fe61556..6b82a4bef8 100644 --- a/core/src/com/unciv/logic/map/tile/Tile.kt +++ b/core/src/com/unciv/logic/map/tile/Tile.kt @@ -469,15 +469,14 @@ class Tile : IsPartOfGameInfoSerialization, Json.Serializable { } /** Implements [UniqueParameterType.TileFilter][com.unciv.models.ruleset.unique.UniqueParameterType.TileFilter] */ - fun matchesFilter(filter: String, civInfo: Civilization? = null, ignoreImprovement: Boolean = false): Boolean { - return MultiFilter.multiFilter(filter, { matchesSingleFilter(it, civInfo, ignoreImprovement) }) + fun matchesFilter(filter: String, civInfo: Civilization? = null): Boolean { + return MultiFilter.multiFilter(filter, { matchesSingleFilter(it, civInfo) }) } - private fun matchesSingleFilter(filter: String, civInfo: Civilization? = null, ignoreImprovement: Boolean = false): Boolean { + private fun matchesSingleFilter(filter: String, civInfo: Civilization? = null): Boolean { if (matchesSingleTerrainFilter(filter, civInfo)) return true if ((improvement == null || improvementIsPillaged) && filter == "unimproved") return true if (improvement != null && !improvementIsPillaged && filter == "improved") return true - if (ignoreImprovement) return false if (getUnpillagedTileImprovement()?.matchesFilter(filter, stateThisTile, false) == true) return true return getUnpillagedRoadImprovement()?.matchesFilter(filter, stateThisTile, false) == true } diff --git a/core/src/com/unciv/logic/map/tile/TileStatFunctions.kt b/core/src/com/unciv/logic/map/tile/TileStatFunctions.kt index 16a08718c1..0a4196ecc7 100644 --- a/core/src/com/unciv/logic/map/tile/TileStatFunctions.kt +++ b/core/src/com/unciv/logic/map/tile/TileStatFunctions.kt @@ -76,10 +76,10 @@ class TileStatFunctions(val tile: Tile) { for (unique in statsFromTilesUniques + statsFromObjectsUniques + statsFromTilesWithoutUniques) { val tileType = unique.params[1] - if (tile.matchesFilter(tileType, observingCiv, true)) - listOfStats.add("{${unique.sourceObjectName}} ({${unique.getDisplayText()}})" to unique.stats) - else if (improvement != null && improvement.matchesFilter(tileType, stateForConditionals)) + if (improvement != null && improvement.matchesFilter(tileType, stateForConditionals)) improvementStats.add(unique.stats) + else if (tile.matchesFilter(tileType, observingCiv)) + listOfStats.add("{${unique.sourceObjectName}} ({${unique.getDisplayText()}})" to unique.stats) else if (road != null && road.matchesFilter(tileType, stateForConditionals)) roadStats.add(unique.stats) } @@ -129,7 +129,7 @@ class TileStatFunctions(val tile: Tile) { return listOfStats.filter { !it.second.isEmpty() }.map { it.first to it.second.clone() } } - /** Ensures each stat is >= [other].stat - modifies in place */ + /** Ensures each stat is >= [minimumStats].stat - modifies in place */ private fun missingFromMinimum(current: Stats, minimumStats: Stats): Stats { // Note: Not `for ((stat, value) in other)` - that would skip zero values val missingStats = Stats() @@ -184,10 +184,10 @@ class TileStatFunctions(val tile: Tile) { val roadStats = Stats() fun addStats(filter: String, stat: Stat, amount: Float) { - if (tile.matchesFilter(filter, observingCiv, true)) - terrainStats.add(stat, amount) - else if (improvement != null && improvement.matchesFilter(filter, stateForConditionals)) + if (improvement != null && improvement.matchesFilter(filter, stateForConditionals)) improvementStats.add(stat, amount) + else if (tile.matchesFilter(filter, observingCiv)) + terrainStats.add(stat, amount) else if (road != null && road.matchesFilter(filter, stateForConditionals)) roadStats.add(stat, amount) } diff --git a/tests/src/com/unciv/uniques/GlobalUniquesTests.kt b/tests/src/com/unciv/uniques/GlobalUniquesTests.kt index ff23f04d4a..9f46fafd60 100644 --- a/tests/src/com/unciv/uniques/GlobalUniquesTests.kt +++ b/tests/src/com/unciv/uniques/GlobalUniquesTests.kt @@ -133,6 +133,19 @@ class GlobalUniquesTests { Assert.assertTrue(tile2.stats.getTileStats(city, civInfo).gold == 4f) } + @Test + fun statsFromTilesMultifilter() { + game.makeHexagonalMap(2) + val civInfo = game.addCiv() + val city = game.addCity(civInfo, game.getTile(Vector2.Zero), true) + val building = game.createBuilding("[+4 Gold] from [{${Constants.grassland}} {Farm}] tiles [in all cities]") + city.cityConstructions.addBuilding(building) + + val tile2 = game.setTileTerrain(Vector2(0f,1f), Constants.grassland) + tile2.setImprovement("Farm") + Assert.assertTrue(tile2.stats.getTileStats(city, civInfo).gold == 4f) + } + @Test fun statsFromTilesWithout() { game.makeHexagonalMap(3)