Skip to content

Commit

Permalink
chore: Split CityStateFunctions.cityStateAttacked into subfunctions
Browse files Browse the repository at this point in the history
  • Loading branch information
yairm210 committed Jan 19, 2025
1 parent 629081f commit 350a0cb
Showing 1 changed file with 68 additions and 48 deletions.
116 changes: 68 additions & 48 deletions core/src/com/unciv/logic/civilization/diplomacy/CityStateFunctions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -626,90 +626,110 @@ class CityStateFunctions(val civInfo: Civilization) {
}
}
// Others might become wary!
if (attacker.isMinorCivAggressor()) {
for (cityState in civInfo.gameInfo.getAliveCityStates()) {
if (cityState == civInfo) // Must be a different minor
continue
if (cityState.getAllyCiv() == attacker.civName) // Must not be allied to the attacker
continue
if (!cityState.knows(attacker)) // Must have met
continue
if (cityState.questManager.wantsDead(civInfo.civName)) // Must not want us dead
continue

var probability: Int
if (attacker.isMinorCivWarmonger()) {
// High probability if very aggressive
probability = when (cityState.getProximity(attacker)) {
Proximity.Neighbors -> 100
Proximity.Close -> 75
Proximity.Far -> 50
Proximity.Distant -> 25
else -> 0
}
} else {
// Lower probability if only somewhat aggressive
probability = when (cityState.getProximity(attacker)) {
Proximity.Neighbors -> 50
Proximity.Close -> 20
else -> 0
}
}
if (attacker.isMinorCivAggressor()) makeOtherCityStatesWaryOfAttacker(attacker)

triggerProtectorCivs(attacker)

// Higher probability if already at war
if (cityState.isAtWarWith(attacker))
probability += 50
// Even if we aren't *technically* protectors, we *can* still be pissed you attacked our allies
triggerAllyCivs(attacker)

if (Random.Default.nextInt(100) <= probability) {
cityState.getDiplomacyManager(attacker)!!.becomeWary()
// Set up war with major pseudo-quest
civInfo.questManager.wasAttackedBy(attacker)
civInfo.getDiplomacyManager(attacker)!!.setFlag(DiplomacyFlags.RecentlyAttacked, 2) // Reminder to ask for unit gifts in 2 turns
}

private fun makeOtherCityStatesWaryOfAttacker(attacker: Civilization) {
for (cityState in civInfo.gameInfo.getAliveCityStates()) {
if (cityState == civInfo) // Must be a different minor
continue
if (cityState.getAllyCiv() == attacker.civName) // Must not be allied to the attacker
continue
if (!cityState.knows(attacker)) // Must have met
continue
if (cityState.questManager.wantsDead(civInfo.civName)) // Must not want us dead
continue

var probability: Int = if (attacker.isMinorCivWarmonger()) {
// High probability if very aggressive
when (cityState.getProximity(attacker)) {
Proximity.Neighbors -> 100
Proximity.Close -> 75
Proximity.Far -> 50
Proximity.Distant -> 25
else -> 0
}
} else {
// Lower probability if only somewhat aggressive
when (cityState.getProximity(attacker)) {
Proximity.Neighbors -> 50
Proximity.Close -> 20
else -> 0
}
}

// Higher probability if already at war
if (cityState.isAtWarWith(attacker))
probability += 50

if (Random.nextInt(100) <= probability) {
cityState.getDiplomacyManager(attacker)!!.becomeWary()
}
}
}

private fun triggerProtectorCivs(attacker: Civilization) {
for (protector in civInfo.cityStateFunctions.getProtectorCivs()) {
val protectorDiplomacy = protector.getDiplomacyManager(attacker) ?: continue // Who?
if (protectorDiplomacy.hasModifier(DiplomaticModifiers.AttackedProtectedMinor)
&& protectorDiplomacy.getFlag(DiplomacyFlags.RememberAttackedProtectedMinor) > 50)
protectorDiplomacy.addModifier(DiplomaticModifiers.AttackedProtectedMinor, -15f) // Penalty less severe for second offence
&& protectorDiplomacy.getFlag(DiplomacyFlags.RememberAttackedProtectedMinor) > 50
)
protectorDiplomacy.addModifier(
DiplomaticModifiers.AttackedProtectedMinor,
-15f
) // Penalty less severe for second offence
else
protectorDiplomacy.addModifier(DiplomaticModifiers.AttackedProtectedMinor, -20f)
protectorDiplomacy.setFlag(DiplomacyFlags.RememberAttackedProtectedMinor, 75) // Reset their memory

if (protector.playerType != PlayerType.Human) // Humans can have their own emotions
attacker.addNotification("[${protector.civName}] is upset that you attacked [${civInfo.civName}], whom they have pledged to protect!",
NotificationCategory.Diplomacy, NotificationIcon.Diplomacy, protector.civName)
attacker.addNotification(
"[${protector.civName}] is upset that you attacked [${civInfo.civName}], whom they have pledged to protect!",
NotificationCategory.Diplomacy, NotificationIcon.Diplomacy, protector.civName
)
else // Let humans choose who to side with
protector.popupAlerts.add(
PopupAlert(
AlertType.AttackedProtectedMinor,
attacker.civName + "@" + civInfo.civName)
attacker.civName + "@" + civInfo.civName
)
) // we need to pass both civs as argument, hence the horrible chimera
}
}

// Even if we aren't *technically* protectors, we *can* still be pissed you attacked our allies*
private fun triggerAllyCivs(attacker: Civilization) {
val allyCivName = civInfo.getAllyCiv()
val allyCiv = if (allyCivName != null) civInfo.gameInfo.getCivilization(allyCivName) else null
if (allyCiv != null && allyCiv !in civInfo.cityStateFunctions.getProtectorCivs() && allyCiv.knows(attacker)){
if (allyCiv != null && allyCiv !in civInfo.cityStateFunctions.getProtectorCivs() && allyCiv.knows(attacker)) {
val allyDiplomacy = allyCiv.getDiplomacyManager(attacker)!!
// Less than if we were protectors
allyDiplomacy.addModifier(DiplomaticModifiers.AttackedAlliedMinor, -10f)

if (allyCiv.playerType != PlayerType.Human) // Humans can have their own emotions
attacker.addNotification("[${allyCiv.civName}] is upset that you attacked [${civInfo.civName}], whom they are allied with!",
NotificationCategory.Diplomacy, NotificationIcon.Diplomacy, allyCiv.civName)
attacker.addNotification(
"[${allyCiv.civName}] is upset that you attacked [${civInfo.civName}], whom they are allied with!",
NotificationCategory.Diplomacy, NotificationIcon.Diplomacy, allyCiv.civName
)
else // Let humans choose who to side with
allyCiv.popupAlerts.add(
PopupAlert(
AlertType.AttackedAllyMinor,
attacker.civName + "@" + civInfo.civName)
attacker.civName + "@" + civInfo.civName
)
)
}

// Set up war with major pseudo-quest
civInfo.questManager.wasAttackedBy(attacker)
civInfo.getDiplomacyManager(attacker)!!.setFlag(DiplomacyFlags.RecentlyAttacked, 2) // Reminder to ask for unit gifts in 2 turns
}


/** A city state was destroyed. Its protectors are going to be upset! */
fun cityStateDestroyed(attacker: Civilization) {
if (!civInfo.isCityState) return // What are we doing here?
Expand Down

0 comments on commit 350a0cb

Please sign in to comment.