From fa939e15d981c28357867b9dc4ad0cd9204e4781 Mon Sep 17 00:00:00 2001 From: yairm210 Date: Fri, 17 Sep 2021 09:55:57 +0300 Subject: [PATCH] getMatchingUnique for UniqueType - will allow us to convert most of the uniques painlessly --- core/src/com/unciv/logic/city/CityInfo.kt | 7 +++++++ .../logic/civilization/CivilizationInfo.kt | 21 +++++++++++++++++++ .../com/unciv/models/ruleset/IHasUniques.kt | 2 ++ core/src/com/unciv/models/ruleset/Unique.kt | 8 +++++-- 4 files changed, 36 insertions(+), 2 deletions(-) diff --git a/core/src/com/unciv/logic/city/CityInfo.kt b/core/src/com/unciv/logic/city/CityInfo.kt index 86787b990c..c8017bbaa7 100644 --- a/core/src/com/unciv/logic/city/CityInfo.kt +++ b/core/src/com/unciv/logic/city/CityInfo.kt @@ -730,6 +730,13 @@ class CityInfo { // Note that we don't query religion here, as those only have local effects (for now at least) } + + fun getMatchingUniquesWithNonLocalEffectsByEnum(uniqueType: UniqueType): Sequence { + return cityConstructions.builtBuildingUniqueMap.getUniques(uniqueType) + .filter { it.params.none { param -> param == "in this city" } } + // Note that we don't query religion here, as those only have local effects (for now at least) + } + // Get all uniques that don't apply to only this city fun getAllUniquesWithNonLocalEffects(): Sequence { return cityConstructions.builtBuildingUniqueMap.getAllUniques() diff --git a/core/src/com/unciv/logic/civilization/CivilizationInfo.kt b/core/src/com/unciv/logic/civilization/CivilizationInfo.kt index 6b16728f8e..de93eebea5 100644 --- a/core/src/com/unciv/logic/civilization/CivilizationInfo.kt +++ b/core/src/com/unciv/logic/civilization/CivilizationInfo.kt @@ -306,6 +306,27 @@ class CivilizationInfo { fun hasUnique(unique: String) = getMatchingUniques(unique).any() + /** Destined to replace getMatchingUniques, gradually, as we fill the enum */ + fun getMatchingUniquesByEnum(uniqueType: UniqueType, cityToIgnore: CityInfo?=null): Sequence { + val ruleset = gameInfo.ruleSet + return nation.uniqueObjects.asSequence().filter { it.matches(uniqueType, ruleset) } + + cities.asSequence().filter { it != cityToIgnore }.flatMap { city -> + city.getMatchingUniquesWithNonLocalEffectsByEnum(uniqueType) + } + + policies.policyUniques.getUniques(uniqueType) + + tech.techUniques.getUniques(uniqueType) + + temporaryUniques + .asSequence().map { it.first } + .filter { it.matches(uniqueType, ruleset) } + + getEra().getMatchingUniques(uniqueType) + + ( + if (religionManager.religion != null) + religionManager.religion!!.getFounderUniques() + .filter { it.isOfType(uniqueType) } + else sequenceOf() + ) + } + // Does not return local uniques, only global ones. fun getMatchingUniques(uniqueTemplate: String, cityToIgnore: CityInfo? = null): Sequence { return nation.uniqueObjects.asSequence().filter { it.placeholderText == uniqueTemplate } + diff --git a/core/src/com/unciv/models/ruleset/IHasUniques.kt b/core/src/com/unciv/models/ruleset/IHasUniques.kt index 55e6b16ed7..44c49484ec 100644 --- a/core/src/com/unciv/models/ruleset/IHasUniques.kt +++ b/core/src/com/unciv/models/ruleset/IHasUniques.kt @@ -8,6 +8,8 @@ interface IHasUniques { val uniqueObjects: List fun getMatchingUniques(uniqueTemplate: String) = uniqueObjects.asSequence().filter { it.placeholderText == uniqueTemplate } + fun getMatchingUniques(uniqueType: UniqueType) = uniqueObjects.asSequence().filter { it.isOfType(uniqueType) } fun hasUnique(uniqueTemplate: String) = uniqueObjects.any { it.placeholderText == uniqueTemplate } + fun hasUnique(uniqueType: UniqueType) = uniqueObjects.any { it.isOfType(uniqueType) } } diff --git a/core/src/com/unciv/models/ruleset/Unique.kt b/core/src/com/unciv/models/ruleset/Unique.kt index 36fc206469..2dcc5d5db7 100644 --- a/core/src/com/unciv/models/ruleset/Unique.kt +++ b/core/src/com/unciv/models/ruleset/Unique.kt @@ -11,7 +11,7 @@ enum class UniqueParameterType(val parameterName:String) { Number("amount") { override fun getErrorType(parameterText: String, ruleset: Ruleset): UniqueType.UniqueComplianceErrorSeverity? { - return if (parameterText.toIntOrNull() != null) UniqueType.UniqueComplianceErrorSeverity.RulesetInvariant + return if (parameterText.toIntOrNull() == null) UniqueType.UniqueComplianceErrorSeverity.RulesetInvariant else null } }, @@ -60,7 +60,8 @@ class UniqueComplianceError( enum class UniqueType(val text:String) { - ConsumesResources("Consumes [amount] [resource]"); + ConsumesResources("Consumes [amount] [resource]"), + FreeUnits("[amount] units cost no maintenance"); /** For uniques that have "special" parameters that can accept multiple types, we can override them manually * For 95% of cases, auto-matching is fine. */ @@ -132,6 +133,7 @@ class Unique(val text:String) { && uniqueType.getComplianceErrors(this, ruleset).isEmpty() } + class UniqueMap:HashMap>() { fun addUnique(unique: Unique) { if (!containsKey(unique.placeholderText)) this[unique.placeholderText] = ArrayList() @@ -144,5 +146,7 @@ class UniqueMap:HashMap>() { else return result.asSequence() } + fun getUniques(uniqueType: UniqueType) = getUniques(uniqueType.placeholderText) + fun getAllUniques() = this.asSequence().flatMap { it.value.asSequence() } } \ No newline at end of file