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
This commit is contained in:
TommasoPetrolito 2024-04-09 22:11:32 +02:00 committed by GitHub
parent 892b54f651
commit c037776674
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 39 additions and 0 deletions

View File

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

View File

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

View File

@ -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<Religion>()
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 {