From c475fb1d55156a48dda24f790fad4d73cb0a4b56 Mon Sep 17 00:00:00 2001 From: Yair Morgenstern Date: Wed, 9 Nov 2022 13:55:36 +0200 Subject: [PATCH] Fixed mod promotions conflicting with existing promotion locations --- core/src/com/unciv/models/ruleset/Ruleset.kt | 30 +++++++++++++++++-- .../unciv/models/ruleset/unit/Promotion.kt | 9 ++++++ 2 files changed, 36 insertions(+), 3 deletions(-) diff --git a/core/src/com/unciv/models/ruleset/Ruleset.kt b/core/src/com/unciv/models/ruleset/Ruleset.kt index c126834436..2765955348 100644 --- a/core/src/com/unciv/models/ruleset/Ruleset.kt +++ b/core/src/com/unciv/models/ruleset/Ruleset.kt @@ -122,11 +122,15 @@ class Ruleset { fun add(ruleset: Ruleset) { beliefs.putAll(ruleset.beliefs) buildings.putAll(ruleset.buildings) - for (buildingToRemove in ruleset.modOptions.buildingsToRemove) buildings.remove(buildingToRemove) + for (buildingToRemove in ruleset.modOptions.buildingsToRemove) buildings.remove( + buildingToRemove + ) difficulties.putAll(ruleset.difficulties) eras.putAll(ruleset.eras) speeds.putAll(ruleset.speeds) - globalUniques = GlobalUniques().apply { uniques.addAll(globalUniques.uniques); uniques.addAll(ruleset.globalUniques.uniques) } + globalUniques = GlobalUniques().apply { + uniques.addAll(globalUniques.uniques); uniques.addAll(ruleset.globalUniques.uniques) + } nations.putAll(ruleset.nations) for (nationToRemove in ruleset.modOptions.nationsToRemove) nations.remove(nationToRemove) policyBranches.putAll(ruleset.policyBranches) @@ -140,13 +144,33 @@ class Ruleset { terrains.putAll(ruleset.terrains) tileImprovements.putAll(ruleset.tileImprovements) tileResources.putAll(ruleset.tileResources) - unitPromotions.putAll(ruleset.unitPromotions) units.putAll(ruleset.units) unitTypes.putAll(ruleset.unitTypes) victories.putAll(ruleset.victories) for (unitToRemove in ruleset.modOptions.unitsToRemove) units.remove(unitToRemove) modOptions.uniques.addAll(ruleset.modOptions.uniques) modOptions.constants.merge(ruleset.modOptions.constants) + + // We should never be editing the original ruleset objects, only copies + val addRulesetUnitPromotionClones = ruleset.unitPromotions.values.map { it.clone() } + val existingPromotionLocations = + unitPromotions.values.map { "${it.row}/${it.column}" }.toHashSet() + val promotionsWithConflictingLocations = addRulesetUnitPromotionClones.filter { + existingPromotionLocations.contains("${it.row}/${it.column}") + } + val columnsWithConflictingLocations = + promotionsWithConflictingLocations.map { it.column }.distinct() + + if (columnsWithConflictingLocations.isNotEmpty()) { + var highestExistingColumn = unitPromotions.values.maxOf { it.column } + for (column in columnsWithConflictingLocations) { + highestExistingColumn += 1 + val newColumn = highestExistingColumn + for (promotion in addRulesetUnitPromotionClones) promotion.column = newColumn + } + } + unitPromotions.putAll(ruleset.unitPromotions) + mods += ruleset.mods } diff --git a/core/src/com/unciv/models/ruleset/unit/Promotion.kt b/core/src/com/unciv/models/ruleset/unit/Promotion.kt index 8c5857b5be..c3146185eb 100644 --- a/core/src/com/unciv/models/ruleset/unit/Promotion.kt +++ b/core/src/com/unciv/models/ruleset/unit/Promotion.kt @@ -18,6 +18,15 @@ class Promotion : RulesetObject() { var row = -1 var column = 0 + fun clone():Promotion { + val newPromotion = Promotion() + newPromotion.prerequisites = prerequisites + newPromotion.unitTypes = unitTypes + newPromotion.row = row + newPromotion.column = column + return newPromotion + } + override fun getUniqueTarget() = UniqueTarget.Promotion