From 350a0cb90ab476c8001dea77ff5fa2524f388d94 Mon Sep 17 00:00:00 2001 From: yairm210 Date: Sun, 19 Jan 2025 13:03:04 +0200 Subject: [PATCH] chore: Split CityStateFunctions.cityStateAttacked into subfunctions --- .../diplomacy/CityStateFunctions.kt | 172 ++++++++++-------- 1 file changed, 96 insertions(+), 76 deletions(-) diff --git a/core/src/com/unciv/logic/civilization/diplomacy/CityStateFunctions.kt b/core/src/com/unciv/logic/civilization/diplomacy/CityStateFunctions.kt index 724831a091..e002998b5c 100644 --- a/core/src/com/unciv/logic/civilization/diplomacy/CityStateFunctions.kt +++ b/core/src/com/unciv/logic/civilization/diplomacy/CityStateFunctions.kt @@ -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 + if (attacker.isMinorCivAggressor()) makeOtherCityStatesWaryOfAttacker(attacker) - 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 - } - } + triggerProtectorCivs(attacker) - // Higher probability if already at war - if (cityState.isAtWarWith(attacker)) - probability += 50 - - if (Random.Default.nextInt(100) <= probability) { - cityState.getDiplomacyManager(attacker)!!.becomeWary() - } - } - } - - 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 - 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) - else // Let humans choose who to side with - protector.popupAlerts.add( - PopupAlert( - AlertType.AttackedProtectedMinor, - 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* - 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)){ - 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) - else // Let humans choose who to side with - allyCiv.popupAlerts.add( - PopupAlert( - AlertType.AttackedAllyMinor, - attacker.civName + "@" + civInfo.civName) - ) - } + // Even if we aren't *technically* protectors, we *can* still be pissed you attacked our allies + triggerAllyCivs(attacker) // 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 + 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 + ) + else // Let humans choose who to side with + protector.popupAlerts.add( + PopupAlert( + AlertType.AttackedProtectedMinor, + attacker.civName + "@" + civInfo.civName + ) + ) // we need to pass both civs as argument, hence the horrible chimera + } + } + + 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)) { + 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 + ) + else // Let humans choose who to side with + allyCiv.popupAlerts.add( + PopupAlert( + AlertType.AttackedAllyMinor, + attacker.civName + "@" + civInfo.civName + ) + ) + } + } + + /** 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?