From 38231bf937015d0c7e1f9ad4202f0a37c9144951 Mon Sep 17 00:00:00 2001 From: yairm210 Date: Fri, 17 Sep 2021 10:53:03 +0300 Subject: [PATCH] More reorg, first unique with cityFilter --- core/src/com/unciv/logic/city/CityStats.kt | 3 +- core/src/com/unciv/models/ruleset/Unique.kt | 62 ------------------ .../com/unciv/models/ruleset/UniqueType.kt | 64 +++++++++++++++++++ 3 files changed, 65 insertions(+), 64 deletions(-) create mode 100644 core/src/com/unciv/models/ruleset/UniqueType.kt diff --git a/core/src/com/unciv/logic/city/CityStats.kt b/core/src/com/unciv/logic/city/CityStats.kt index c68eb8c3e4..6e668eadb8 100644 --- a/core/src/com/unciv/logic/city/CityStats.kt +++ b/core/src/com/unciv/logic/city/CityStats.kt @@ -218,8 +218,7 @@ class CityStats(val cityInfo: CityInfo) { val stats = Stats() for (unique in uniques.toList()) { // Should help mitigate getConstructionButtonDTOs concurrency problems. - // "[stats] [cityFilter]" - if (unique.placeholderText == "[] []" && cityInfo.matchesFilter(unique.params[1])) + if (unique.isOfType(UniqueType.StatsPerCity) && cityInfo.matchesFilter(unique.params[1])) stats.add(unique.stats) // "[stats] per [amount] population [cityFilter]" diff --git a/core/src/com/unciv/models/ruleset/Unique.kt b/core/src/com/unciv/models/ruleset/Unique.kt index f257e6381d..4ce5546630 100644 --- a/core/src/com/unciv/models/ruleset/Unique.kt +++ b/core/src/com/unciv/models/ruleset/Unique.kt @@ -5,68 +5,6 @@ import com.unciv.models.translations.getPlaceholderParameters import com.unciv.models.translations.getPlaceholderText - -enum class UniqueType(val text:String, val replacedBy: UniqueType? = null) { - - ConsumesResources("Consumes [amount] [resource]"), - FreeUnits("[amount] units cost no maintenance"), - UnitMaintenanceDiscount("[amount]% maintenance costs for [mapUnitFilter] units"), - @Deprecated("As of 3.16.16") - DecreasedUnitMaintenanceCostsByFilter("-[amount]% [mapUnitFilter] unit maintenance costs", UnitMaintenanceDiscount), - @Deprecated("As of 3.16.16") - DecreasedUnitMaintenanceCostsGlobally("-[amount]% unit upkeep costs", UnitMaintenanceDiscount), - StatBonusForNumberOfSpecialists("[stats] if this city has at least [amount] specialists") - ; - - /** For uniques that have "special" parameters that can accept multiple types, we can override them manually - * For 95% of cases, auto-matching is fine. */ - private val parameterTypeMap = ArrayList>() - - init { - for (placeholder in text.getPlaceholderParameters()) { - val matchingParameterType = - UniqueParameterType.values().firstOrNull { it.parameterName == placeholder } - ?: UniqueParameterType.Unknown - parameterTypeMap.add(listOf(matchingParameterType)) - } - } - - val placeholderText = text.getPlaceholderText() - - /** Ordinal determines severity - ordered from most severe at 0 */ - enum class UniqueComplianceErrorSeverity { - - /** This is a problem like "numbers don't parse", "stat isn't stat", "city filter not applicable" */ - RulesetInvariant, - - /** This is a problem like "unit/resource/tech name doesn't exist in ruleset" - definite bug */ - RulesetSpecific, - - /** This is for filters that can also potentially accept free text, like UnitFilter and TileFilter */ - WarningOnly - } - - /** Maps uncompliant parameters to their required types */ - fun getComplianceErrors( - unique: Unique, - ruleset: Ruleset - ): List { - val errorList = ArrayList() - for ((index, param) in unique.params.withIndex()) { - val acceptableParamTypes = parameterTypeMap[index] - val errorTypesForAcceptableParameters = - acceptableParamTypes.map { it.getErrorSeverity(param, ruleset) } - if (errorTypesForAcceptableParameters.any { it == null }) continue // This matches one of the types! - val leastSevereWarning = - errorTypesForAcceptableParameters.maxByOrNull { it!!.ordinal }!! - errorList += UniqueComplianceError(param, acceptableParamTypes, leastSevereWarning) - } - return errorList - } -} - - - class Unique(val text:String) { val placeholderText = text.getPlaceholderText() val params = text.getPlaceholderParameters() diff --git a/core/src/com/unciv/models/ruleset/UniqueType.kt b/core/src/com/unciv/models/ruleset/UniqueType.kt new file mode 100644 index 0000000000..9eddeecade --- /dev/null +++ b/core/src/com/unciv/models/ruleset/UniqueType.kt @@ -0,0 +1,64 @@ +package com.unciv.models.ruleset + +import com.unciv.models.translations.getPlaceholderParameters +import com.unciv.models.translations.getPlaceholderText + +enum class UniqueType(val text:String, val replacedBy: UniqueType? = null) { + + ConsumesResources("Consumes [amount] [resource]"), + FreeUnits("[amount] units cost no maintenance"), + UnitMaintenanceDiscount("[amount]% maintenance costs for [mapUnitFilter] units"), + @Deprecated("As of 3.16.16") + DecreasedUnitMaintenanceCostsByFilter("-[amount]% [mapUnitFilter] unit maintenance costs", UnitMaintenanceDiscount), + @Deprecated("As of 3.16.16") + DecreasedUnitMaintenanceCostsGlobally("-[amount]% unit upkeep costs", UnitMaintenanceDiscount), + StatBonusForNumberOfSpecialists("[stats] if this city has at least [amount] specialists"), + StatsPerCity("[stats] [cityFilter]") + ; + + /** For uniques that have "special" parameters that can accept multiple types, we can override them manually + * For 95% of cases, auto-matching is fine. */ + private val parameterTypeMap = ArrayList>() + + init { + for (placeholder in text.getPlaceholderParameters()) { + val matchingParameterType = + UniqueParameterType.values().firstOrNull { it.parameterName == placeholder } + ?: UniqueParameterType.Unknown + parameterTypeMap.add(listOf(matchingParameterType)) + } + } + + val placeholderText = text.getPlaceholderText() + + /** Ordinal determines severity - ordered from most severe at 0 */ + enum class UniqueComplianceErrorSeverity { + + /** This is a problem like "numbers don't parse", "stat isn't stat", "city filter not applicable" */ + RulesetInvariant, + + /** This is a problem like "unit/resource/tech name doesn't exist in ruleset" - definite bug */ + RulesetSpecific, + + /** This is for filters that can also potentially accept free text, like UnitFilter and TileFilter */ + WarningOnly + } + + /** Maps uncompliant parameters to their required types */ + fun getComplianceErrors( + unique: Unique, + ruleset: Ruleset + ): List { + val errorList = ArrayList() + for ((index, param) in unique.params.withIndex()) { + val acceptableParamTypes = parameterTypeMap[index] + val errorTypesForAcceptableParameters = + acceptableParamTypes.map { it.getErrorSeverity(param, ruleset) } + if (errorTypesForAcceptableParameters.any { it == null }) continue // This matches one of the types! + val leastSevereWarning = + errorTypesForAcceptableParameters.maxByOrNull { it!!.ordinal }!! + errorList += UniqueComplianceError(param, acceptableParamTypes, leastSevereWarning) + } + return errorList + } +} \ No newline at end of file