﻿namespace = realm_split

##########################
# Rise and Fall - Realm Split Events
# Event chain for when a realm fractures into multiple independent realms

# Main event: The Great Fracturing
# Ruler faces the splitting of their realm into multiple parts
realm_split.1001 = {
    type = character_event
    title = realm_split.1001.t
    desc = realm_split.1001.d
    theme = realm
    
    left_portrait = {
        character = root
        animation = stress
    }
    
    trigger = {
        riseandfall_can_realm_split_trigger = yes
    }
    
    immediate = {
        # Count vassals for description (use every_vassal for governments without powerful vassals)
        # All governments use county tier or above
        set_variable = { name = rf_display_vassal_count value = 0 }
        if = {
            limit = { riseandfall_no_powerful_vassals_trigger = yes }
            # Administrative governments
            every_vassal = {
                limit = {
                    is_ai = yes
                    is_landed = yes
                    primary_title.tier >= tier_county
                }
                root = { change_variable = { name = rf_display_vassal_count add = 1 } }
            }
        }
        else = {
            # Feudal/Clan - powerful vassals
            every_powerful_vassal = {
                limit = {
                    is_ai = yes
                    is_landed = yes
                    primary_title.tier >= tier_county
                }
                root = { change_variable = { name = rf_display_vassal_count add = 1 } }
            }
        }
        
        # Save some vassals for the portrait display
        if = {
            limit = { riseandfall_no_powerful_vassals_trigger = yes }
            # Administrative governments
            random_vassal = {
                limit = {
                    is_ai = yes
                    is_landed = yes
                    primary_title.tier >= tier_county
                }
                weight = {
                    base = 1
                    modifier = {
                        add = 10
                        current_military_strength > root.current_military_strength
                    }
                }
                save_scope_as = ambitious_vassal_1
            }
            random_vassal = {
                limit = {
                    is_ai = yes
                    is_landed = yes
                    primary_title.tier >= tier_county
                    NOT = { this = scope:ambitious_vassal_1 }
                }
                weight = {
                    base = 1
                    modifier = {
                        add = 10
                        current_military_strength > root.current_military_strength
                    }
                }
                save_scope_as = ambitious_vassal_2
            }
        }
        else = {
            # Feudal/Clan - powerful vassals
            random_powerful_vassal = {
                limit = {
                    is_ai = yes
                    is_landed = yes
                    primary_title.tier >= tier_county
                }
                weight = {
                    base = 1
                    modifier = {
                        add = 10
                        current_military_strength > root.current_military_strength
                    }
                }
                save_scope_as = ambitious_vassal_1
            }
            random_powerful_vassal = {
                limit = {
                    is_ai = yes
                    is_landed = yes
                    primary_title.tier >= tier_county
                    NOT = { this = scope:ambitious_vassal_1 }
                }
                weight = {
                    base = 1
                    modifier = {
                        add = 10
                        current_military_strength > root.current_military_strength
                    }
                }
                save_scope_as = ambitious_vassal_2
            }
        }
    }
    
    # Only option: Accept the split - vassals break away and become tributaries
    option = {
        name = realm_split.1001.a
        
        # Execute the realm split
        riseandfall_realm_split_se = yes

        # The realm has already lost land and vassals; release part of the
        # accumulated Story Mode collapse pressure as a structural relief valve.
        riseandfall_story_collapse_relief_se = yes
        
        # Stability improvement from reduced realm size
        if = {
            limit = { has_variable = riseandfall_realm_stability_score }
            change_variable = { name = riseandfall_realm_stability_score add = 10 }
        }
        
        # Set title-based cooldown so it survives succession.
        riseandfall_set_low_stability_cooldown_se = yes
        
        ai_chance = {
            base = 100
        }
    }
    
    after = {
        remove_variable = rf_display_vassal_count
    }
}

realm_split.2001 = {
    type = character_event
    title = realm_split.2001.t
    desc = realm_split.2001.d
    theme = realm

    immediate = {
        if = {
            limit = { has_variable = rf_split_defection_defector }
            var:rf_split_defection_defector = { save_scope_as = split_defector }
        }
        if = {
            limit = { has_variable = rf_split_defection_new_liege }
            var:rf_split_defection_new_liege = { save_scope_as = split_new_liege }
        }
        remove_variable = rf_split_defection_defector
        remove_variable = rf_split_defection_new_liege
    }

    left_portrait = {
        character = scope:split_defector
        animation = personality_bold
    }
    right_portrait = {
        character = scope:split_new_liege
        animation = personality_bold
    }

    option = {
        name = realm_split.2001.a
    }
}
