﻿# Cleanup effect: enforce only basic Warlord trait consistency.
riseandfall_warlord_cleanup_se = {
    every_ruler = {
        limit = { is_landed = no riseandfall_is_any_warlord_trigger = yes }
        if = { limit = { has_trait = warlord } remove_trait = warlord }
        if = { limit = { has_trait = loyalwarlord } remove_trait = loyalwarlord }
        remove_character_flag = rf_warlord_legacy
        remove_variable = rf_warlord_legacy_expiry
        remove_variable = rf_warlord_legacy_expiry_initialized
    }

    # Independent Warlords cease to be part of a liege's internal crisis.
    # Do not add retiredwarlord: compatibility mods own that trait's lifecycle.
    every_independent_ruler = {
        limit = { riseandfall_is_any_warlord_trigger = yes }
        if = { limit = { has_trait = warlord } remove_trait = warlord }
        if = { limit = { has_trait = loyalwarlord } remove_trait = loyalwarlord }
        remove_character_flag = rf_warlord_legacy
        remove_variable = rf_warlord_legacy_expiry
        remove_variable = rf_warlord_legacy_expiry_initialized
    }
}

# Expected scope: a ruler whose succession law should follow their Rebel
# Warlord state. The prior vanilla law is stored on the primary title.
riseandfall_warlord_reconcile_succession_se = {
    # The yearly ruler loop has no implicit root scope; preserve the ruler
    # before entering primary_title so law changes target the realm correctly.
    save_scope_as = rf_warlord_succession_ruler
    if = {
        limit = {
            exists = primary_title
            has_realm_law = riseandfall_warlord_succession_law
            OR = {
                is_independent_ruler = yes
                NOT = { has_trait = warlord }
            }
        }
        primary_title = {
            if = {
                limit = { has_variable = rf_warlord_restore_japanese_civic }
                scope:rf_warlord_succession_ruler = { add_realm_law_skip_effects = japanese_appointment_succession_law }
                remove_variable = rf_warlord_restore_japanese_civic
            }
            else_if = {
                limit = { has_variable = rf_warlord_restore_japanese_military }
                scope:rf_warlord_succession_ruler = { add_realm_law_skip_effects = japanese_military_appointment_succession_law }
                remove_variable = rf_warlord_restore_japanese_military
            }
            else_if = {
                limit = { has_variable = rf_warlord_restore_meritocratic_civic }
                scope:rf_warlord_succession_ruler = { add_realm_law_skip_effects = meritocratic_appointment_succession_law }
                remove_variable = rf_warlord_restore_meritocratic_civic
            }
            else_if = {
                limit = { has_variable = rf_warlord_restore_meritocratic_military }
                scope:rf_warlord_succession_ruler = { add_realm_law_skip_effects = meritocratic_military_appointment_succession_law }
                remove_variable = rf_warlord_restore_meritocratic_military
            }
            else_if = {
                limit = { has_variable = rf_warlord_restore_celestial_civic }
                scope:rf_warlord_succession_ruler = { add_realm_law_skip_effects = celestial_appointment_succession_law }
                remove_variable = rf_warlord_restore_celestial_civic
            }
            else_if = {
                limit = { has_variable = rf_warlord_restore_celestial_military }
                scope:rf_warlord_succession_ruler = { add_realm_law_skip_effects = celestial_military_appointment_succession_law }
                remove_variable = rf_warlord_restore_celestial_military
            }
            else = {
                # Recover old saves where the temporary law exists without a
                # saved marker by using the government's normal civic/military
                # contract split.
                scope:rf_warlord_succession_ruler = {
                    if = {
                        limit = {
                            government_has_flag = government_is_japan_administrative
                            vassal_contract_has_flag = japan_administrative_military_appointment
                        }
                        add_realm_law_skip_effects = japanese_military_appointment_succession_law
                    }
                    else_if = {
                        limit = { government_has_flag = government_is_japan_administrative }
                        add_realm_law_skip_effects = japanese_appointment_succession_law
                    }
                    else_if = {
                        limit = {
                            government_has_flag = government_is_meritocratic
                            vassal_contract_has_flag = meritocratic_military_appointment
                        }
                        add_realm_law_skip_effects = meritocratic_military_appointment_succession_law
                    }
                    else_if = {
                        limit = { government_has_flag = government_is_meritocratic }
                        add_realm_law_skip_effects = meritocratic_appointment_succession_law
                    }
                    else_if = {
                        limit = {
                            government_has_flag = government_is_celestial
                            vassal_contract_has_flag = celestial_military_appointment
                        }
                        add_realm_law_skip_effects = celestial_military_appointment_succession_law
                    }
                    else_if = {
                        limit = { government_has_flag = government_is_celestial }
                        add_realm_law_skip_effects = celestial_appointment_succession_law
                    }
                }
            }
        }
    }
    if = {
        limit = {
            exists = primary_title
            is_independent_ruler = no
            NOT = { has_realm_law = riseandfall_warlord_succession_law }
            has_trait = warlord
            OR = {
                has_realm_law = japanese_appointment_succession_law
                has_realm_law = japanese_military_appointment_succession_law
                has_realm_law = meritocratic_appointment_succession_law
                has_realm_law = meritocratic_military_appointment_succession_law
                has_realm_law = celestial_appointment_succession_law
                has_realm_law = celestial_military_appointment_succession_law
            }
        }
        primary_title = {
            if = {
                limit = { scope:rf_warlord_succession_ruler = { has_realm_law = japanese_appointment_succession_law } }
                set_variable = { name = rf_warlord_restore_japanese_civic value = yes }
            }
            else_if = {
                limit = { scope:rf_warlord_succession_ruler = { has_realm_law = japanese_military_appointment_succession_law } }
                set_variable = { name = rf_warlord_restore_japanese_military value = yes }
            }
            else_if = {
                limit = { scope:rf_warlord_succession_ruler = { has_realm_law = meritocratic_appointment_succession_law } }
                set_variable = { name = rf_warlord_restore_meritocratic_civic value = yes }
            }
            else_if = {
                limit = { scope:rf_warlord_succession_ruler = { has_realm_law = meritocratic_military_appointment_succession_law } }
                set_variable = { name = rf_warlord_restore_meritocratic_military value = yes }
            }
            else_if = {
                limit = { scope:rf_warlord_succession_ruler = { has_realm_law = celestial_appointment_succession_law } }
                set_variable = { name = rf_warlord_restore_celestial_civic value = yes }
            }
            else_if = {
                limit = { scope:rf_warlord_succession_ruler = { has_realm_law = celestial_military_appointment_succession_law } }
                set_variable = { name = rf_warlord_restore_celestial_military value = yes }
            }
        }
        add_realm_law_skip_effects = riseandfall_warlord_succession_law
    }
}

# Expected scope: new holder in on_title_gain_inheritance; scope:title is the
# inherited title. A former local crisis remains a legacy banner when that
# title becomes secondary to an already peaceful primary realm.
riseandfall_warlord_mark_inherited_legacy_se = {
    if = {
        limit = {
            is_independent_ruler = yes
            exists = primary_title
            exists = scope:title
            scope:title = { has_variable = rf_warlord_era_active }
            NOT = { primary_title = scope:title }
            primary_title = { NOT = { has_variable = rf_warlord_era_active } }
        }
        scope:title = {
            remove_variable = rf_warlord_era_active
            remove_variable = rf_warlord_loyal_victory_locked
            remove_variable = rf_warlord_era_age_initialized
        }
        every_vassal = {
            limit = {
                is_landed = yes
                riseandfall_is_any_warlord_trigger = yes
            }
            add_character_flag = rf_warlord_legacy
            set_variable = { name = rf_warlord_legacy_expiry value = 1 years = 5 }
            set_variable = { name = rf_warlord_legacy_expiry_initialized value = 1 }
            remove_variable = riseandfall_warlord_invitation_pending
        }
    }
}

# Expected scope: inherited legacy Warlord whose five-year banner has expired.
# Legacy retirement is deliberately separate from active-era suppression.
riseandfall_warlord_retire_legacy_se = {
    save_scope_as = rf_legacy_retiring_warlord
    if = {
        limit = { exists = liege }
        liege = { save_scope_as = rf_legacy_retiring_liege }
    }
    if = { limit = { has_trait = warlord } remove_trait = warlord }
    if = { limit = { has_trait = loyalwarlord } remove_trait = loyalwarlord }
    remove_character_flag = rf_warlord_legacy
    remove_variable = rf_warlord_legacy_expiry
    remove_variable = rf_warlord_legacy_expiry_initialized
    remove_variable = riseandfall_warlord_invitation_pending
    if = {
        limit = { exists = scope:rf_legacy_retiring_liege }
        scope:rf_legacy_retiring_liege = {
            remove_opinion = {
                target = scope:rf_legacy_retiring_warlord
                modifier = warlord_rebellion_opinion
            }
            remove_opinion = {
                target = scope:rf_legacy_retiring_warlord
                modifier = warlord_loyalty_opinion
            }
            if = {
                limit = { is_ai = no }
                trigger_event = { id = warlord.1014 }
            }
        }
    }
    if = {
        limit = { is_ai = no }
        trigger_event = { id = warlord.1013 }
    }
    clear_saved_scope = rf_legacy_retiring_liege
    clear_saved_scope = rf_legacy_retiring_warlord
}

# Expected scope: independent liege with an already tracked Warlord Era. The
# marker lives on the primary title so an unresolved era survives succession;
# this maintenance effect must never create a fresh era from character traits.
riseandfall_warlord_mark_era_active_se = {
    if = {
        limit = {
            is_independent_ruler = yes
            exists = primary_title
            primary_title = { has_variable = rf_warlord_era_active }
            any_vassal = {
                is_landed = yes
                riseandfall_is_active_warlord_trigger = yes
            }
        }
        primary_title = { save_scope_as = rf_warlord_era_primary_title }
        every_held_title = {
            limit = {
                has_variable = rf_warlord_era_active
                NOT = { this = scope:rf_warlord_era_primary_title }
            }
            remove_variable = rf_warlord_era_active
            if = { limit = { has_variable = rf_warlord_loyal_victory_locked } remove_variable = rf_warlord_loyal_victory_locked }
            if = { limit = { has_variable = rf_warlord_era_age_initialized } remove_variable = rf_warlord_era_age_initialized }
        }
        primary_title = {
            # Existing saves have no age state. Start their ten-year minimum
            # once, then retain the initialized marker after the lock expires.
            if = {
                limit = { NOT = { has_variable = rf_warlord_era_age_initialized } }
                set_variable = { name = rf_warlord_loyal_victory_locked value = 1 years = 10 }
                set_variable = { name = rf_warlord_era_age_initialized value = 1 }
            }
        }
        clear_saved_scope = rf_warlord_era_primary_title
    }
}

# Expected scope: active direct Warlord. Renewing a two-year reason annually
# keeps the vanilla AI able to imprison or revoke without overriding its risk checks.
riseandfall_warlord_renew_liege_reason_se = {
    if = {
        limit = {
            has_trait = warlord
            is_independent_ruler = no
            exists = liege
        }
        save_scope_as = rf_warlord_reason_target
        liege = {
            remove_opinion = {
                target = scope:rf_warlord_reason_target
                modifier = warlord_rebellion_opinion
            }
            add_opinion = {
                target = scope:rf_warlord_reason_target
                modifier = warlord_rebellion_opinion
            }
        }
    }
}

# Expected scope: independent liege whose Warlord Era has ended.
riseandfall_warlord_clear_era_state_se = {
    remove_variable = rf_warlord_crisis_count
    remove_variable = rf_warlord_crisis_territory_pct
    remove_variable = rf_warlord_rebel_count
    remove_variable = rf_warlord_loyal_count
    remove_variable = rf_warlord_rebel_territory_pct
    remove_variable = rf_warlord_loyal_territory_pct
    every_held_title = {
        if = { limit = { has_variable = rf_warlord_era_active } remove_variable = rf_warlord_era_active }
        if = { limit = { has_variable = rf_warlord_loyal_victory_locked } remove_variable = rf_warlord_loyal_victory_locked }
        if = { limit = { has_variable = rf_warlord_era_age_initialized } remove_variable = rf_warlord_era_age_initialized }
    }
    every_vassal = {
        limit = { is_landed = yes }
        remove_variable = riseandfall_warlord_invitation_pending
    }
}

# Expected scope: independent liege. Only a tracked era can produce the ending event.
riseandfall_warlord_end_era_suppressed_se = {
    if = {
        limit = {
            exists = primary_title
            is_at_war = no
            primary_title = { has_variable = rf_warlord_era_active }
        }
        riseandfall_warlord_clear_era_state_se = yes
        trigger_event = { id = warlord.1010 }
    }
}

# Expected scope: independent liege. Called after individual Warlord removals.
riseandfall_warlord_try_end_era_se = {
    if = {
        limit = {
            exists = primary_title
            is_at_war = no
            primary_title = { has_variable = rf_warlord_era_active }
            NOT = {
                any_vassal = {
                    is_landed = yes
                    OR = {
                        riseandfall_is_active_warlord_trigger = yes
                        has_variable = riseandfall_warlord_invitation_pending
                    }
                }
            }
        }
        riseandfall_warlord_end_era_suppressed_se = yes
    }
}

# Expected scope: ruler being split. Fragmentation ends the era without showing
# the suppression ending; the Realm Split event already provides that narrative.
riseandfall_warlord_end_era_realm_split_se = {
    save_scope_as = rf_warlord_era_liege
    riseandfall_warlord_clear_era_state_se = yes
    every_vassal = {
        limit = { is_landed = yes }
        save_scope_as = rf_warlord_era_vassal
        scope:rf_warlord_era_liege = {
            remove_opinion = {
                target = scope:rf_warlord_era_vassal
                modifier = warlord_rebellion_opinion
            }
        }
        if = {
            limit = { riseandfall_is_active_warlord_trigger = yes }
            if = { limit = { has_trait = warlord } remove_trait = warlord }
            if = { limit = { has_trait = loyalwarlord } remove_trait = loyalwarlord }
        }
    }
    clear_saved_scope = rf_warlord_era_vassal
    riseandfall_warlord_reconcile_succession_se = yes
}

# Expected scope: independent liege. Loyal victory is checked on the annual
# pulse after every AI alignment has been reconsidered.
riseandfall_warlord_end_era_loyal_victory_se = {
    if = {
        limit = {
            exists = primary_title
            is_at_war = no
            primary_title = { has_variable = rf_warlord_era_active }
            primary_title = { has_variable = rf_warlord_era_age_initialized }
            primary_title = { NOT = { has_variable = rf_warlord_loyal_victory_locked } }
            has_variable = rf_warlord_loyal_territory_pct
            var:rf_warlord_loyal_territory_pct >= 51
        }
        save_scope_as = rf_warlord_era_liege
        every_vassal = {
            limit = {
                is_landed = yes
                riseandfall_is_active_warlord_trigger = yes
            }
            if = {
                limit = { is_ai = no }
                trigger_event = { id = warlord.1012 }
            }
            save_scope_as = rf_warlord_era_vassal
            scope:rf_warlord_era_liege = {
                remove_opinion = {
                    target = scope:rf_warlord_era_vassal
                    modifier = warlord_rebellion_opinion
                }
            }
            if = { limit = { has_trait = warlord } remove_trait = warlord }
            if = { limit = { has_trait = loyalwarlord } remove_trait = loyalwarlord }
        }
        riseandfall_warlord_clear_era_state_se = yes
        riseandfall_warlord_reconcile_succession_se = yes
        trigger_event = { id = warlord.1011 }
        clear_saved_scope = rf_warlord_era_vassal
    }
}

riseandfall_warlord_try_loyal_victory_se = {
    if = {
        limit = {
            exists = primary_title
            is_at_war = no
            primary_title = { has_variable = rf_warlord_era_active }
            primary_title = { has_variable = rf_warlord_era_age_initialized }
            primary_title = { NOT = { has_variable = rf_warlord_loyal_victory_locked } }
            has_variable = rf_warlord_loyal_territory_pct
            var:rf_warlord_loyal_territory_pct >= 51
        }
        riseandfall_warlord_end_era_loyal_victory_se = yes
    }
}

# Called from on_imprison when a liege imprisons one of their Warlord vassals.
riseandfall_warlord_imprisoned_se = {
    if = {
        limit = {
            has_trait = warlord
            target_is_liege_or_above = scope:imprisoner
        }
        riseandfall_warlord_abandon_path = yes
    }
}

# Expected scope: liege with Warlord vassals. Legitimacy loss scales with the
# share of direct landed vassals participating in the Warlord movement.
riseandfall_warlord_legitimacy_loss_se = {
    if = {
        limit = {
            has_legitimacy = yes
            any_vassal = {
                primary_title.tier >= tier_county
                has_trait = warlord
            }
        }
        set_variable = { name = rf_total_vassals value = 0 }
        set_variable = { name = rf_warlord_vassals value = 0 }
        every_vassal = {
            limit = { primary_title.tier >= tier_county }
            prev = { change_variable = { name = rf_total_vassals add = 1 } }
            if = {
                limit = { has_trait = warlord }
                prev = { change_variable = { name = rf_warlord_vassals add = 1 } }
            }
        }
        if = {
            limit = { var:rf_total_vassals > 0 }
            set_variable = { name = rf_legitimacy_loss value = var:rf_warlord_vassals }
            change_variable = { name = rf_legitimacy_loss multiply = -100 }
            change_variable = { name = rf_legitimacy_loss divide = var:rf_total_vassals }
            if = {
                limit = { var:rf_legitimacy_loss < 0 }
                add_legitimacy = var:rf_legitimacy_loss
            }
            remove_variable = rf_legitimacy_loss
        }
        remove_variable = rf_total_vassals
        remove_variable = rf_warlord_vassals
    }
}
