﻿##########################
# Rise and Fall - Low Stability Event Availability Triggers
# Keep per-event eligibility logic here so the selector stays simple.

riseandfall_low_stability_warlord_available_trigger = {
    NOT = { exists = global_var:riseandfall_low_stability_event_warlord_disabled }
    # must have at least one landed vassals of county tier or higher
    any_vassal = {
        count >= 1
        is_landed = yes
        primary_title.tier >= tier_county
        # A local Warlord Era cannot be rekindled solely by inherited legacy banners.
        NOT = { has_character_flag = rf_warlord_legacy }
    }
}

# Character helper shared by both Warlord camps.
riseandfall_is_any_warlord_trigger = {
    OR = {
        has_trait = warlord
        has_trait = loyalwarlord
    }
}

# Inherited Warlords can remain as historical legacy banners without activating
# a new top-level realm crisis.
riseandfall_is_active_warlord_trigger = {
    riseandfall_is_any_warlord_trigger = yes
    NOT = { has_character_flag = rf_warlord_legacy }
}

# A Warlord may forge an uncreated title only when their realm controls a
# majority of its de-jure counties, matching the vanilla creation threshold.
riseandfall_warlord_has_majority_creatable_title_trigger = {
    OR = {
        any_realm_de_jure_duchy = {
            is_title_created = no
            any_de_jure_county = {
                percent > 0.5
                holder = {
                    OR = {
                        this = root
                        any_liege_or_above = { this = root }
                    }
                }
            }
        }
        any_realm_de_jure_kingdom = {
            is_title_created = no
            any_de_jure_county = {
                percent > 0.5
                holder = {
                    OR = {
                        this = root
                        any_liege_or_above = { this = root }
                    }
                }
            }
        }
        any_realm_de_jure_empire = {
            is_title_created = no
            any_de_jure_county = {
                percent > 0.5
                holder = {
                    OR = {
                        this = root
                        any_liege_or_above = { this = root }
                    }
                }
            }
        }
    }
}

riseandfall_low_stability_palace_coup_available_trigger = {
    NOT = { exists = global_var:riseandfall_low_stability_event_palace_coup_disabled }
    NOT = { government_has_flag = government_is_theocracy }
    NOT = { government_has_flag = government_is_republic }
    NOT = { has_diarchy_type = puppet_regency }
}

riseandfall_low_stability_realm_split_available_trigger = {
    riseandfall_can_realm_split_trigger = yes
    NOT = { exists = global_var:riseandfall_low_stability_event_realm_split_disabled }
}

# Nationalist/Religious Revolt: fires when significant cultural/faith diversity exists (component <= -10).
# This maps to 30%+ foreign counties in the realm (tiers: 30-50% = -10, 50-70% = -20, 70%+ = -30).
riseandfall_low_stability_nationalist_revolt_available_trigger = {
    NOT = { exists = global_var:riseandfall_low_stability_event_nationalist_revolt_disabled }
    # Require notable diversity — culture_faith_component at -10 or worse
    has_variable = riseandfall_rs_culture_faith_component
    NOT = { var:riseandfall_rs_culture_faith_component >= -9 }
    NOT = {
        any_realm_county = {
            has_county_modifier = riseandfall_nationalist_revolt
        }
    }
}

# Vassal Authority Crisis: fires when powerful vassals challenge the crown OR overall vassal opinion is very low.
riseandfall_low_stability_vassal_crisis_available_trigger = {
    NOT = { exists = global_var:riseandfall_low_stability_event_vassal_crisis_disabled }
    OR = {
        AND = {
            has_variable = riseandfall_rs_strong_vassal_component
            NOT = { var:riseandfall_rs_strong_vassal_component >= -9 }
        }
        AND = {
            has_variable = riseandfall_rs_vassals_component
            NOT = { var:riseandfall_rs_vassals_component >= -14 }
        }
    }
    NOT = {
        OR = {
            has_character_modifier = riseandfall_vassal_crisis_full_concessions
            has_character_modifier = riseandfall_vassal_crisis_limited_concessions
            has_character_modifier = riseandfall_vassal_crisis_refusal
        }
    }
}

# Total Collapse is a two-strike finale. Active crisis modifiers cannot be used for escalation
# because the low-stability cooldown suppresses new events while those modifiers are active.
# The persistent recent-crisis markers therefore require both crises to remain unresolved after
# their cooldowns, alongside catastrophic identity and vassal pressure.
riseandfall_low_stability_authority_collapse_available_trigger = {
    NOT = { exists = global_var:riseandfall_low_stability_event_authority_collapse_disabled }
    NOT = { has_variable = riseandfall_recent_total_collapse }
    NOT = {
        any_realm_county = {
            has_county_modifier = riseandfall_authority_collapse
        }
    }
    has_variable = riseandfall_recent_nationalist_revolt
    has_variable = riseandfall_recent_vassal_crisis
    has_variable = riseandfall_realm_stability_score
    NOT = { var:riseandfall_realm_stability_score >= 11 }
    has_variable = riseandfall_rs_culture_faith_component
    NOT = { var:riseandfall_rs_culture_faith_component >= -19 }
    OR = {
        AND = {
            has_variable = riseandfall_rs_strong_vassal_component
            NOT = { var:riseandfall_rs_strong_vassal_component >= -19 }
        }
        AND = {
            has_variable = riseandfall_rs_vassals_component
            NOT = { var:riseandfall_rs_vassals_component >= -19 }
        }
    }
}
