From c037776674cfd978bdb478d5ce5bbfc0bc380555 Mon Sep 17 00:00:00 2001 From: TommasoPetrolito Date: Tue, 9 Apr 2024 22:11:32 +0200 Subject: [PATCH] Implement Same-majority-religion-based diplomatic modifier (#11415) * implement BelieveSameReligion/BelieveDifferentReligion DiplomaticModifiers * implemented feedbacks * remove unuseful val instanciation/assignment * add getMajorityReligion() in ReligionManager.kt * fixing code style --- .../diplomacy/DiplomacyManager.kt | 17 ++++++++++++++++ .../diplomacy/DiplomacyTurnManager.kt | 2 ++ .../civilization/managers/ReligionManager.kt | 20 +++++++++++++++++++ 3 files changed, 39 insertions(+) diff --git a/core/src/com/unciv/logic/civilization/diplomacy/DiplomacyManager.kt b/core/src/com/unciv/logic/civilization/diplomacy/DiplomacyManager.kt index ac68b1c608..3db2f00db9 100644 --- a/core/src/com/unciv/logic/civilization/diplomacy/DiplomacyManager.kt +++ b/core/src/com/unciv/logic/civilization/diplomacy/DiplomacyManager.kt @@ -102,6 +102,7 @@ enum class DiplomaticModifiers(val text: String) { GaveUsUnits("You gave us units!"), GaveUsGifts("We appreciate your gifts"), ReturnedCapturedUnits("You returned captured units to us"), + BelieveSameReligion("We believe in the same religion"), } @@ -276,6 +277,14 @@ class DiplomacyManager() : IsPartOfGameInfoSerialization { } } + private fun believesSameReligion(): Boolean { + // what is the majorityReligion of civInfo? If it is null, we immediately return false + val civMajorityReligion = civInfo.religionManager.getMajorityReligion() ?: return false + // if not yet returned false from previous line, return the Boolean isMajorityReligionForCiv + // true if majorityReligion of civInfo is also majorityReligion of otherCiv, false otherwise + return otherCiv().religionManager.isMajorityReligionForCiv(civMajorityReligion) + } + /** Returns the number of turns to degrade from Ally or from Friend */ fun getTurnsToRelationshipChange(): Int { if (otherCiv().isCityState()) @@ -587,6 +596,14 @@ class DiplomacyManager() : IsPartOfGameInfoSerialization { } } + internal fun setReligionBasedModifier() { + if (civInfo.getDiplomacyManager(otherCiv()).believesSameReligion()) + // they share same majority religion + setModifier(DiplomaticModifiers.BelieveSameReligion, 5f) + else + // their majority religions differ or one or both don't have a majority religion at all + removeModifier(DiplomaticModifiers.BelieveSameReligion) + } fun denounce() { setModifier(DiplomaticModifiers.Denunciation, -35f) diff --git a/core/src/com/unciv/logic/civilization/diplomacy/DiplomacyTurnManager.kt b/core/src/com/unciv/logic/civilization/diplomacy/DiplomacyTurnManager.kt index 0e4e662b57..fcb4caf3b9 100644 --- a/core/src/com/unciv/logic/civilization/diplomacy/DiplomacyTurnManager.kt +++ b/core/src/com/unciv/logic/civilization/diplomacy/DiplomacyTurnManager.kt @@ -291,6 +291,8 @@ object DiplomacyTurnManager { setDefensivePactBasedModifier() + setReligionBasedModifier() + if (!hasFlag(DiplomacyFlags.DeclarationOfFriendship)) revertToZero(DiplomaticModifiers.DeclarationOfFriendship, 1 / 2f) //decreases slowly and will revert to full if it is declared later diff --git a/core/src/com/unciv/logic/civilization/managers/ReligionManager.kt b/core/src/com/unciv/logic/civilization/managers/ReligionManager.kt index 10dacd3f3f..e212c19676 100644 --- a/core/src/com/unciv/logic/civilization/managers/ReligionManager.kt +++ b/core/src/com/unciv/logic/civilization/managers/ReligionManager.kt @@ -478,6 +478,26 @@ class ReligionManager : IsPartOfGameInfoSerialization { if (religion == null) return null return civInfo.gameInfo.getCities().firstOrNull { it.isHolyCityOf(religion!!.name) } } + + fun getMajorityReligion(): Religion? { + // let's count for each religion (among those actually presents in civ's cities) + val religionCounter = Counter() + for (city in civInfo.cities) { + // if city's majority Religion is null, let's just continue to next loop iteration + val cityMajorityReligion = city.religion.getMajorityReligion() ?: continue + // if not yet continued to next iteration from previous line, let's add the Religion to religionCounter + religionCounter.add(cityMajorityReligion, 1) + } + // let's get the max-counted Religion if there is one, null otherwise; if null, return null + val maxReligionCounterEntry = religionCounter.maxByOrNull { it.value } ?: return null + // if not returned null from prev. line, check if the maxReligion is in most of the cities + return if (maxReligionCounterEntry.value > civInfo.cities.size / 2) + // if maxReligionCounterEntry > half-cities-count we return the Religion of maxReligionCounterEntry + maxReligionCounterEntry.key + else + // if maxReligionCounterEntry <= half-cities-count we just return null + null + } } enum class ReligionState : IsPartOfGameInfoSerialization {