Fixed mod promotions conflicting with existing promotion locations

This commit is contained in:
Yair Morgenstern 2022-11-09 13:55:36 +02:00
parent d748d489e2
commit c475fb1d55
2 changed files with 36 additions and 3 deletions

View File

@ -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
}

View File

@ -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