From 97916e15a199408e8b1d4205e20ff9d772c1fb20 Mon Sep 17 00:00:00 2001 From: Yair Morgenstern Date: Thu, 6 Aug 2020 18:26:43 +0300 Subject: [PATCH] More usage of existing Uniques --- core/src/com/unciv/logic/city/CityStats.kt | 8 ++--- .../unciv/logic/civilization/PolicyManager.kt | 6 ++-- core/src/com/unciv/models/ruleset/Building.kt | 34 ++++--------------- core/src/com/unciv/models/ruleset/Unique.kt | 24 +++++++++++++ .../unciv/models/translations/Translations.kt | 4 +-- 5 files changed, 38 insertions(+), 38 deletions(-) create mode 100644 core/src/com/unciv/models/ruleset/Unique.kt diff --git a/core/src/com/unciv/logic/city/CityStats.kt b/core/src/com/unciv/logic/city/CityStats.kt index e561e4bf..db8f934b 100644 --- a/core/src/com/unciv/logic/city/CityStats.kt +++ b/core/src/com/unciv/logic/city/CityStats.kt @@ -175,7 +175,6 @@ class CityStats { // needs to be a separate function because we need to know the global happiness state // in order to determine how much food is produced in a city! - // -3 happiness per city fun updateCityHappiness() { val civInfo = cityInfo.civInfo val newHappinessList = LinkedHashMap() @@ -183,7 +182,7 @@ class CityStats { if (!civInfo.isPlayerCivilization()) unhappinessModifier *= civInfo.gameInfo.getDifficulty().aiUnhappinessModifier - var unhappinessFromCity = -3f + var unhappinessFromCity = -3f // -3 happiness per city if (civInfo.nation.unique == UniqueAbility.POPULATION_GROWTH) unhappinessFromCity *= 2f//doubled for the Indian @@ -217,9 +216,8 @@ class CityStats { happinessFromPolicies += 1f if (cityInfo.getCenterTile().militaryUnit != null) - for (unique in civInfo.policies.policyEffects) - if (unique.equalsPlaceholderText("[] in all cities with a garrison")) - happinessFromPolicies += Stats.parse(unique.getPlaceholderParameters()[0]).happiness + for (unique in civInfo.getMatchingUniques("[] in all cities with a garrison")) + happinessFromPolicies += Stats.parse(unique.params[0]).happiness newHappinessList["Policies"] = happinessFromPolicies diff --git a/core/src/com/unciv/logic/civilization/PolicyManager.kt b/core/src/com/unciv/logic/civilization/PolicyManager.kt index e96da505..7864052c 100644 --- a/core/src/com/unciv/logic/civilization/PolicyManager.kt +++ b/core/src/com/unciv/logic/civilization/PolicyManager.kt @@ -134,10 +134,10 @@ class PolicyManager { val hasCapital = civInfo.cities.any { it.isCapital() } - for(effect in policy.uniques) - when (effect.getPlaceholderText()) { + for (unique in policy.uniqueObjects) + when (unique.placeholderText) { "Free [] appears" -> { - val unitName = effect.getPlaceholderParameters()[0] + val unitName = unique.params[0] if (hasCapital && (unitName != Constants.settler || !civInfo.isOneCityChallenger())) civInfo.addUnit(unitName, civInfo.getCapital()) } diff --git a/core/src/com/unciv/models/ruleset/Building.kt b/core/src/com/unciv/models/ruleset/Building.kt index eb32cdc4..5d900740 100644 --- a/core/src/com/unciv/models/ruleset/Building.kt +++ b/core/src/com/unciv/models/ruleset/Building.kt @@ -7,31 +7,9 @@ import com.unciv.logic.civilization.CivilizationInfo import com.unciv.models.stats.NamedStats import com.unciv.models.stats.Stat import com.unciv.models.stats.Stats -import com.unciv.models.translations.equalsPlaceholderText -import com.unciv.models.translations.getPlaceholderParameters -import com.unciv.models.translations.getPlaceholderText import com.unciv.models.translations.tr import kotlin.math.pow -class Unique(val text:String){ - val placeholderText = text.getPlaceholderText() - val params = text.getPlaceholderParameters() -} - -class UniqueMap:HashMap>() { - fun addUnique(unique: Unique) { - if (!containsKey(unique.placeholderText)) this[unique.placeholderText] = ArrayList() - this[unique.placeholderText]!!.add(unique) - } - - fun getUniques(placeholderText: String): List { - val result = this.get(placeholderText) - if (result == null) return listOf() - else return result - } - - fun getAllUniques() = this.asSequence().flatMap { it.value.asSequence() } -} class Building : NamedStats(), IConstruction { @@ -254,10 +232,10 @@ class Building : NamedStats(), IConstruction { val cityCenter = construction.cityInfo.getCenterTile() - for(unique in uniques) - if(unique.equalsPlaceholderText("Must be next to []") - && !cityCenter.getTilesInDistance(1).any { it.baseTerrain == unique.getPlaceholderParameters()[0] }) - return unique + for(unique in uniqueObjects) + if(unique.placeholderText == "Must be next to []" + && !cityCenter.getTilesInDistance(1).any { it.baseTerrain == unique.params[0] }) + return unique.text if ("Must be next to river" in uniques && !cityCenter.isAdjacentToRiver()) return "Must be next to river" @@ -375,8 +353,8 @@ class Building : NamedStats(), IConstruction { } if ("Empire enters golden age" in uniques) civInfo.goldenAges.enterGoldenAge() - for(unique in uniques) if(unique.equalsPlaceholderText("Free [] appears")){ - val unitName = unique.getPlaceholderParameters()[0] + for(unique in uniqueObjects.filter { it.placeholderText == "Free [] appears" }) { + val unitName = unique.params[0] civInfo.addUnit(unitName, cityConstructions.cityInfo) } if ("2 free Great Artists appear" in uniques) { diff --git a/core/src/com/unciv/models/ruleset/Unique.kt b/core/src/com/unciv/models/ruleset/Unique.kt new file mode 100644 index 00000000..e4b5ad23 --- /dev/null +++ b/core/src/com/unciv/models/ruleset/Unique.kt @@ -0,0 +1,24 @@ +package com.unciv.models.ruleset + +import com.unciv.models.translations.getPlaceholderParameters +import com.unciv.models.translations.getPlaceholderText + +class Unique(val text:String){ + val placeholderText = text.getPlaceholderText() + val params = text.getPlaceholderParameters() +} + +class UniqueMap:HashMap>() { + fun addUnique(unique: Unique) { + if (!containsKey(unique.placeholderText)) this[unique.placeholderText] = ArrayList() + this[unique.placeholderText]!!.add(unique) + } + + fun getUniques(placeholderText: String): List { + val result = this.get(placeholderText) + if (result == null) return listOf() + else return result + } + + fun getAllUniques() = this.asSequence().flatMap { it.value.asSequence() } +} \ No newline at end of file diff --git a/core/src/com/unciv/models/translations/Translations.kt b/core/src/com/unciv/models/translations/Translations.kt index 6b734c9e..0dbbf092 100644 --- a/core/src/com/unciv/models/translations/Translations.kt +++ b/core/src/com/unciv/models/translations/Translations.kt @@ -107,7 +107,7 @@ class Translations : LinkedHashMap(){ targetTranslations: Translations = this) { for (translation in languageTranslations) { val hashKey = if (translation.key.contains('[')) - translation.key.replace(squareBraceRegex, "[]") + translation.key.getPlaceholderText() else translation.key if (!containsKey(hashKey)) targetTranslations[hashKey] = TranslationEntry(translation.key) @@ -241,7 +241,7 @@ fun String.tr(): String { */ // Convert "work on [building] has completed in [city]" to "work on [] has completed in []" - val translationStringWithSquareBracketsOnly = this.replace(squareBraceRegex, "[]") + val translationStringWithSquareBracketsOnly = this.getPlaceholderText() // That is now the key into the translation HashMap! val translationEntry = UncivGame.Current.translations .get(translationStringWithSquareBracketsOnly, UncivGame.Current.settings.language, activeMods)