﻿on_game_start = {
    on_actions = {
        rd_on_game_start
    }
}
rd_on_game_start = {
 #   trigger = {
 #        OR = {
 #            exists = title:k_france
 #            exists = title:k_england
 #       }
 #   }
    effect = {
        start_situation = {
			type = regional_destiny_situation
			start_phase = starting_era
		}
    }
}

yearly_playable_pulse = {
	on_actions = {
        gpt_rd_yearly_pulse
    }
    on_actions = {
        delay = { days = { 5 15 } }
        gpt_rd_yearly_pulse_transition_trait
    }
    on_actions = {
        delay = { days = { 5 15 } }
        gpt_rd_yearly_pulse_conquest_trait
    }
    on_actions = {
        delay = { days = { 5 15 } }
        gpt_rd_yearly_pulse_prosperity_Trait
        gpt_rd_yearly_pulse_prosperity_Trait_bugfix
    }
}

# Downfall Mechanism
on_war_won_defender = {
	on_actions = {
        rd_on_war_won_defender
    }
}
rd_on_war_won_defender = {
    trigger = {
        has_game_rule = rd_conqueror_situation_rd_downfall_enabled
        scope:attacker = { has_trait = rd_gpt_conqueror }
    }
    effect = {
        scope:attacker = {
            add_character_flag = rd_downfall_flag
            remove_trait = rd_gpt_conqueror
        }
    }
}

# Watchdog in case of sub_region_pulse malfunctional
regional_destiny_situation_pulse = {
	effect = {
        every_situation_sub_region = {
            limit = {
                NOT = {
                    any_situation_sub_region_participant_group = {
                        participant_group_type = regional_destiny_conqueror
                        any_situation_group_participant = {
                            exists = this
                        }
                    }
                }
                trigger_if = {
                    limit = {
                        has_game_rule = rd_conqueror_disabled
                    }
                    always = no
                }
            }
            every_situation_sub_region_participant_group = {
                limit = { participant_group_type = regional_destiny_involved }
                if = {
                    limit = {
                        has_game_rule = rd_conqueror_chosen_weight_military_strength
                    }
                    ordered_situation_group_participant = {
                        limit = {
                            is_ai = yes
                         #    is_independent_ruler = yes
                            highest_held_title_tier <= 3
                            NOT = { government_has_flag = government_is_theocracy }
                            NOT = { has_character_flag = rd_downfall_flag }
                        }
                        order_by = {
                            value = 1
                            add = {
                                value = martial
                                multiply = 5
                                divide = highest_held_title_tier
                            }
                        }
                        max = 10
                        check_range_bounds = no
                        random_list = {
                            50 = {
                            }
                            50 = {
                                add_character_flag = {
                                    flag = rd_potential_conqueror
                                    days = 10
                                }
                            }
                        }
                    }
                    ordered_situation_group_participant = {
                        limit = {
                            has_character_flag = rd_potential_conqueror
                        }
                        order_by = {
                            value = 1
                            add = {
                                value = martial
                                multiply = 5
                                divide = highest_held_title_tier
                            }
                        }
                        if = { # Starter package!
                            limit = {
                                is_ai = yes
                            }
                            add_gold = 500
                            add_prestige = 800
                        }
                        add_trait = rd_gpt_conqueror
                        if = {
                            limit = {
                                is_independent_ruler = no
                            }
                         #   add_trait = disloyal
                        }
                        spawn_army = {
                            levies = {
                                value = 0
                                add = {
                                    value = 1000
                                    add = {
                                        value = years_from_game_start
                                        multiply = 5
                                    }
                                    multiply = highest_held_title_tier
                                    subtract = 1000
                                }
                            }
                            location = capital_province
                            inheritable = no
                            name = rd_gpt_army
                        }
                    }
                }
                else = {
                    random_situation_group_participant = {
                        limit = {
                            is_ai = yes
                            highest_held_title_tier <= 3
                            NOT = { government_has_flag = government_is_theocracy }
                            NOT = { has_character_flag = rd_downfall_flag }
                        }
                        if = { # Starter package!
                            limit = {
                                is_ai = yes
                            }
                            add_gold = 500
                            add_prestige = 800
                        }
                        add_trait = rd_gpt_conqueror
                        if = {
                            limit = {
                                is_independent_ruler = no
                            }
                         #   add_trait = disloyal
                        }
                        spawn_army = {
                            levies = {
                                value = 0
                                add = {
                                    value = 1000
                                    add = {
                                        value = years_from_game_start
                                        multiply = 5
                                    }
                                    multiply = highest_held_title_tier
                                    subtract = 1000
                                }
                            }
                            location = capital_province
                            inheritable = no
                            name = rd_gpt_army
                        }
                    }
                }
            }
        }

        trigger_event = {
            on_action = regional_destiny_situation_pulse
            years = RD_CONQUEROR_CHOSEN_SPAN_WATCHDOG
        }
	}
}
# Contend mechanism
regional_destiny_situation_sub_region_pulse = {
    trigger = {
        NOT = {
            any_situation_sub_region_participant_group = {
                participant_group_type = regional_destiny_conqueror
                any_situation_group_participant = {
                    exists = this
                }
            }
        }
        trigger_if = {
            limit = {
                has_game_rule = rd_conqueror_disabled
            }
            always = no
        }

    }
    effect = {
        every_situation_sub_region_participant_group = {
            limit = { participant_group_type = regional_destiny_involved }
            if = {
                limit = {
                    has_game_rule = rd_conqueror_chosen_weight_military_strength
                }
                ordered_situation_group_participant = {
                    limit = {
                        is_ai = yes
                      #   is_independent_ruler = yes
                        highest_held_title_tier <= 3
                        NOT = { government_has_flag = government_is_theocracy }
                        NOT = { has_character_flag = rd_downfall_flag }
                    }
                    order_by = {
                        value = 1
                        add = {
                            value = martial
                            multiply = 5
                            divide = highest_held_title_tier
                        }
                    }
                    max = 10
                    check_range_bounds = no
                    random_list = {
                        50 = {
                        }
                        50 = {
                            add_character_flag = {
                                flag = rd_potential_conqueror
                                days = 10
                            }
                        }
                    }
                }
                ordered_situation_group_participant = {
                    limit = {
                        has_character_flag = rd_potential_conqueror
                    }
                    order_by = {
                        value = 1
                        add = {
                            value = martial
                            multiply = 5
                            divide = highest_held_title_tier
                        }
                    }
                    if = { # Starter package!
                            limit = {
                                is_ai = yes
                            }
                            add_gold = 500
                            add_prestige = 800
                        }
                        add_trait = rd_gpt_conqueror
                        if = {
                            limit = {
                                is_independent_ruler = no
                            }
                         #   add_trait = disloyal
                        }
                        spawn_army = {
                            levies = {
                                value = 0
                                add = {
                                    value = 1000
                                    add = {
                                        value = years_from_game_start
                                        multiply = 5
                                    }
                                    multiply = highest_held_title_tier
                                    subtract = 1000
                                }
                            }
                            location = capital_province
                            inheritable = no
                            name = rd_gpt_army
                        }
                }
            }
            else = {
                random_situation_group_participant = {
                    limit = {
                        is_ai = yes
                        highest_held_title_tier <= 3
                        NOT = { government_has_flag = government_is_theocracy }
                        NOT = { has_character_flag = rd_downfall_flag }
                    }
                    if = { # Starter package!
                            limit = {
                                is_ai = yes
                            }
                            add_gold = 500
                            add_prestige = 800
                        }
                        add_trait = rd_gpt_conqueror
                        if = {
                            limit = {
                                is_independent_ruler = no
                            }
                         #   add_trait = disloyal
                        }
                        spawn_army = {
                            levies = {
                                value = 0
                                add = {
                                    value = 1000
                                    add = {
                                        value = years_from_game_start
                                        multiply = 5
                                    }
                                    multiply = highest_held_title_tier
                                    subtract = 1000
                                }
                            }
                            location = capital_province
                            inheritable = no
                            name = rd_gpt_army
                        }
                }
            }
        }
    }
}







gpt_rd_yearly_pulse_transition_trait = {
    trigger = {
     #  years_from_game_start >= 12
        any_character_situation = {
            this = situation:regional_destiny_situation
            any_situation_sub_region = {
                sub_region_current_phase = undefined_era
                any_situation_sub_region_participant = {
                    this = root
                }
            }
        }
    }
    effect = {
        if = {
            limit = {
                NOT = { has_trait = rd_gpt_transition_era }
            }
            add_trait = rd_gpt_transition_era
        }
        if = {
            limit = {
                has_trait = rd_gpt_conquest_era
            }
            remove_trait = rd_gpt_conquest_era
        }
        if = {
            limit = {
                has_trait = rd_gpt_prosperity_era
            }
            remove_trait = rd_gpt_prosperity_era
        }
    }
}

gpt_rd_yearly_pulse_conquest_trait = {
    trigger = {
     #  years_from_game_start >= 12
        any_character_situation = {
            this = situation:regional_destiny_situation
            any_situation_sub_region = {
                sub_region_current_phase = unification_chaotic
                any_situation_sub_region_participant = {
                    this = root
                }
            }
        }
    }
    effect = {
        if = {
            limit = {
                NOT = { has_trait = rd_gpt_conquest_era }
            }
            add_trait = rd_gpt_conquest_era
        }
        if = {
            limit = {
                has_trait = rd_gpt_transition_era
            }
            remove_trait = rd_gpt_transition_era
        }
        if = {
            limit = {
                has_trait = rd_gpt_prosperity_era
            }
            remove_trait = rd_gpt_prosperity_era
        }
    }
}

gpt_rd_yearly_pulse_prosperity_trait = {
    trigger = {
     #  years_from_game_start >= 12
        any_character_situation = {
            this = situation:regional_destiny_situation
            any_situation_sub_region = {
                sub_region_current_phase = unification_ordered
                any_situation_sub_region_participant = {
                    this = root
                }
            }
        }
    }
    effect = {
        if = {
            limit = {
                NOT = { has_trait = rd_gpt_prosperity_era}
            }
            add_trait = rd_gpt_prosperity_era
        }
        if = {
            limit = {
                has_trait = rd_gpt_transition_era
            }
            remove_trait = rd_gpt_transition_era
        }
        if = {
            limit = {
                has_trait = rd_gpt_conquest_era
            }
            remove_trait = rd_gpt_conquest_era
        }
    }
}

gpt_rd_yearly_pulse_prosperity_Trait_bugfix = {
    trigger = {
     #  years_from_game_start >= 12
        any_character_situation = {
            this = situation:regional_destiny_situation
            any_situation_sub_region = {
                sub_region_current_phase = unification_ordered
                any_situation_sub_region_participant = {
                    this = root
                }
            }
        }
    }
    effect = {
        if = {
            limit = {
                NOT = { has_trait = rd_gpt_prosperity_era}
            }
            add_trait = rd_gpt_prosperity_era
        }
        if = {
            limit = {
                has_trait = rd_gpt_transition_era
            }
            remove_trait = rd_gpt_transition_era
        }
        if = {
            limit = {
                has_trait = rd_gpt_conquest_era
            }
            remove_trait = rd_gpt_conquest_era
        }
    }
}

gpt_rd_yearly_pulse = {
    trigger = {
       # highest_held_title_tier >= 2
        any_character_situation = { this = situation:regional_destiny_situation }
	}
	effect = {

        every_character_situation = {
            limit = {
                this = situation:regional_destiny_situation
            }
            every_situation_sub_region = {
                limit = {
                    any_situation_sub_region_participant = {
                        count > 100
                    }
                    any_situation_sub_region_participant = {
                        this = root
                    }
                }
                add_takeover_phase_points = {
                    phase = undefined_era
                    points = -1
                }
                add_takeover_phase_points = {
                    phase = unification_chaotic
                    points = -1
                }
                add_takeover_phase_points = {
                    phase = unification_ordered
                    points = -1
                }
            }
        }

        every_character_situation = {
            limit = {
                this = situation:regional_destiny_situation
            }

            every_situation_sub_region = {
                limit = {
                    sub_region_current_phase = undefined_era
                    any_situation_sub_region_participant = {
                        this = root
                    }
                }
                add_takeover_phase_points = {
                    phase = undefined_era
                    points = -1
                }
            }
            every_situation_sub_region = {
                limit = {
                    sub_region_current_phase = undefined_era
                    any_situation_sub_region_participant = {
                        this = root
                        OR = {
                            has_trait = brave
                            has_trait = diligent
                            has_trait = ambitious
                        }
                    }
                }
                add_takeover_phase_points = {
                    phase = undefined_era
                    points = -1
                }
            }

            every_situation_sub_region = {
                limit = {
                    sub_region_current_phase = unification_chaotic
                    any_situation_sub_region_participant = {
                        this = root
                    }
                }
                add_takeover_phase_points = {
                    phase = unification_chaotic
                    points = -1
                }
            }
            every_situation_sub_region = {
                limit = {
                    sub_region_current_phase = unification_chaotic
                    any_situation_sub_region_participant = {
                        this = root
                        OR = {
                            is_at_war = no
                            has_trait = compassionate
                        }
                    }
                }
                add_takeover_phase_points = {
                    phase = unification_chaotic
                    points = -1
                }
            }

            every_situation_sub_region = {
                limit = {
                    sub_region_current_phase = unification_ordered
                    any_situation_sub_region_participant = {
                        this = root
                    }
                }
                add_takeover_phase_points = {
                    phase = unification_ordered
                    points = -1
                }
            }
            every_situation_sub_region = {
                limit = {
                    sub_region_current_phase = unification_ordered
                    any_situation_sub_region_participant = {
                        this = root
                        OR = {
                            is_at_war = yes
                            has_trait = brave
                            has_trait = sadistic
                            has_trait = callous
                            has_trait = ambitious
                        }
                    }
                }
                add_takeover_phase_points = {
                    phase = unification_ordered
                    points = -1
                }
            }
        }







        # Transition
        every_character_situation = {
            limit = {
                this = situation:regional_destiny_situation
            }
            every_situation_sub_region = {
                limit = {
                    any_situation_sub_region_participant = {
                        this = root
                    }
                }
                add_takeover_phase_points = {
                    phase = undefined_era
                    points = {
                        value = root.highest_held_title_tier
                    }
                }
            }
            every_situation_sub_region = {
                limit = {
                    any_situation_sub_region_participant = {
                        this = root
                        OR = {
                            intrigue >= 12
                            government_has_flag = government_has_treasury
                            AND = { has_royal_court = yes OR = { has_court_type = court_intrigue has_court_type = court_scholarly } }
                            culture = { has_cultural_pillar = ethos_bureaucratic }
                            AND = {
                                has_trait = deceitful
                                ai_energy > 0
                                ai_boldness > 0
                            }
                        }
                    }
                }
                add_takeover_phase_points = {
                    phase = undefined_era
                    points = 1
                }
            }
            every_situation_sub_region = {
                limit = {
                    any_situation_sub_region_participant = {
                        this = root
                        OR = {
                            diplomacy >= 12
                            government_has_flag = government_is_republic
                            AND = { has_royal_court = yes OR = { has_court_type = court_diplomatic has_court_type = court_scholarly } }
                            culture = { has_cultural_pillar = ethos_courtly }
                            AND = {
                                has_trait = gregarious
                                ai_energy > 0
                                ai_boldness > 0
                            }
                        }
                    }
                }
                add_takeover_phase_points = {
                    phase = undefined_era
                    points = 1
                }
            }
            every_situation_sub_region = {
                limit = {
                    any_situation_sub_region_participant = {
                        this = root
                        OR = {
                            learning >= 12
                            exists = faith.religious_head
                            government_has_flag = government_is_theocracy
                            culture = { has_cultural_pillar = ethos_spiritual }
                            AND = { has_royal_court = yes has_court_type = court_scholarly }
                            AND = {
                                has_trait = zealous
                                ai_energy > 0
                                ai_boldness > 0
                            }
                        }
                    }
                }
                add_takeover_phase_points = {
                    phase = undefined_era
                    points = 1
                }
            }
        }




        # Stability
        if = {
            limit = {
                NOR = {
                    AND = {
                        has_trait = compassionate
                        ai_energy > 0
                        ai_boldness > 0
                    }
                    piety_level = 0
                    prestige_level = 0
                    num_sinful_traits >= 2
                    debt_level >= 1
                    is_imprisoned = yes
                    government_has_flag = government_is_tribal
                    government_has_flag = government_is_nomadic
                    faith = { has_doctrine_parameter = unreformed }
                    culture = { has_cultural_pillar = ethos_bellicose }
                    culture_tradition:tradition_quarrelsome = { is_in_list = traits }
                    culture_tradition:tradition_malleable_invaders = { is_in_list = traits }
                    culture_tradition:tradition_warrior_culture = { is_in_list = traits }
                    culture_tradition:tradition_martial_admiration = { is_in_list = traits }
                    faith = { has_doctrine = tenet_warmonger }
                    faith = { has_doctrine = tenet_ritual_cannibalism }
                    faith = { has_doctrine = tenet_human_sacrifice }
                    faith = { has_doctrine = tenet_sacrificial_ceremonies }
                    faith = { has_doctrine = tenet_pursuit_of_power }
                    faith = { has_doctrine = tenet_gruesome_festivals }
                    faith = { has_doctrine = tenet_exaltation_of_pain }
                    is_a_faction_member = yes
                    has_targeting_faction = yes
                }
            }
            every_character_situation = {
                limit = {
                    this = situation:regional_destiny_situation
                }
                every_situation_sub_region = {
                    limit = {
                        any_situation_sub_region_participant = {
                            this = root
                            OR = {
                                is_independent_ruler = yes
                                is_tributary = yes
                            }
                        }
                    }
                    add_takeover_phase_points = {
                        phase = unification_ordered
                        points = {
                            value = root.highest_held_title_tier
                            divide = 2
                        }
                    }
                }
                every_situation_sub_region = {
                    limit = {
                        any_situation_sub_region_participant = {
                            this = root
                            OR = {
                                is_independent_ruler = yes
                                is_tributary = yes
                            }
                            stewardship >= 24
                        }
                    }
                    add_takeover_phase_points = {
                        phase = unification_ordered
                        points = 1
                    }
                }
                every_situation_sub_region = {
                    limit = {
                        any_situation_sub_region_participant = {
                            this = root
                            is_independent_ruler = no
                        }
                    }
                    add_takeover_phase_points = {
                        phase = unification_ordered
                        points = {
                            value = root.highest_held_title_tier
                        }
                    }
                }
                every_situation_sub_region = {
                    limit = {
                        any_situation_sub_region_participant = {
                            this = root
                            is_independent_ruler = no
                            stewardship >= 12
                        }
                    }
                    add_takeover_phase_points = {
                        phase = unification_ordered
                        points = 1
                    }
                }
            }
        }
        if = {
            limit = {
                NOR = {
                    is_a_faction_leader = yes
                    any_scheme = { is_hostile = yes }
                    AND = { is_at_war = yes any_character_war = { is_war_leader = root } }
                    AND = { is_at_war = yes any_character_war = { is_civil_war = yes } }
                    any_army = { is_army_in_raid = yes }
                    any_targeting_faction = { target_of_powerful_faction_trigger = yes }
                    AND = { exists = house house ?= { has_house_unity_stage = antagonistic } }
                    any_directly_owned_province = {
                        OR = {
                            is_occupied = yes
                            is_raided = yes
                            any_province_epidemic = {
                                outbreak_intensity >= minor
                            }
                        }
                    }
                    any_held_county = {
                        percent >= 0.4
                        OR = {
                            county_control < 50
                            county_opinion < -50
                        }
                    }
                    AND = {
                        OR = {
                                is_independent_ruler = yes
                                is_tributary = yes
                            }
                        any_neighboring_top_liege_realm_owner = {
                            percent >= 0.4
                            OR = {
                                has_relation_rival = root
                                has_relation_nemesis = root
                                opinion = {
                                    target = root
                                    value <= -40
                                }
                                faith = {
                                    faith_hostility_level = {
                                        target = root.faith
                                        value >= 2
                                    }
                                }
                                culture = {
                                    cultural_acceptance = {
                                        target = root.culture
                                        value <= 25
                                    }
                                }
                            }
                        }
                    }
                    AND = {
                        is_independent_ruler = no
                        any_neighboring_realm_same_rank_owner = {
                            percent >= 0.4
                            OR = {
                                has_relation_rival = root
                                has_relation_nemesis = root
                                opinion = {
                                    target = root
                                    value <= -40
                                }
                                faith = {
                                    faith_hostility_level = {
                                        target = root.faith
                                        value >= 2
                                    }
                                }
                                culture = {
                                    cultural_acceptance = {
                                        target = root.culture
                                        value <= 25
                                    }
                                }
                            }
                        }
                    }
                    AND = {
                        highest_held_title_tier = 2
                        legitimacy_level <= 0
                    }
                    AND = {
                        highest_held_title_tier = 3
                        legitimacy_level <= 1
                    }
                    AND = {
                        highest_held_title_tier = 4
                        legitimacy_level <= 2
                    }
                    AND = {
                        highest_held_title_tier = 5
                        legitimacy_level <= 3
                    }            
                }
            }
            every_character_situation = {
                limit = {
                    this = situation:regional_destiny_situation
                }
                every_situation_sub_region = {
                    limit = {
                        any_situation_sub_region_participant = {
                            this = root
                            OR = {
                                is_independent_ruler = yes
                                is_tributary = yes
                            }
                        }
                    }
                    add_takeover_phase_points = {
                        phase = unification_ordered
                        points = {
                            value = root.highest_held_title_tier
                        }
                    }
                }
                every_situation_sub_region = {
                    limit = {
                        any_situation_sub_region_participant = {
                            this = root
                            OR = {
                                is_independent_ruler = yes
                                is_tributary = yes
                            }
                            stewardship >= 12
                        }
                    }
                    add_takeover_phase_points = {
                        phase = unification_ordered
                        points = 1
                    }
                }
                every_situation_sub_region = {
                    limit = {
                        any_situation_sub_region_participant = {
                            this = root
                            is_independent_ruler = no
                        }
                    }
                    add_takeover_phase_points = {
                        phase = unification_ordered
                        points = {
                            value = root.highest_held_title_tier
                            multiply = 2
                        }
                    }
                }
                every_situation_sub_region = {
                    limit = {
                        any_situation_sub_region_participant = {
                            this = root
                            is_independent_ruler = no
                            stewardship >= 6
                        }
                    }
                    add_takeover_phase_points = {
                        phase = unification_ordered
                        points = 1
                    }
                }
            }
        }
        if = {
            limit = {
                OR = {
                    is_confederation_member = yes
                    AND = {
                        OR = {
                            is_independent_ruler = yes
                            is_tributary = yes
                        }
                        any_ally = { highest_held_title_tier > root.highest_held_title_tier }
                    }
                    AND = {
                        OR = {
                            is_independent_ruler = yes
                            is_tributary = yes
                        }
                        prestige_level >= 4
                        days_of_continuous_peace >= 1825
                    }
                    AND = {
                        number_of_powerful_vassals >= 3
                        NOT = {
                            any_vassal = {
                                is_powerful_vassal = yes
                                opposes_succession_law_change_trigger = yes
                            }
                        }
                    }
                    AND = {
                        highest_held_title_tier = 2
                        legitimacy_level >= 2
                    }
                    AND = {
                        highest_held_title_tier = 3
                        legitimacy_level >= 3
                    }
                    AND = {
                        highest_held_title_tier = 4
                        legitimacy_level >= 4
                    }
                    AND = {
                        highest_held_title_tier = 5
                        legitimacy_level >= 5
                    }            
                }
            }
            every_character_situation = {
                limit = {
                    this = situation:regional_destiny_situation
                }
                every_situation_sub_region = {
                    limit = {
                        any_situation_sub_region_participant = {
                            this = root
                            OR = {
                                is_independent_ruler = yes
                                is_tributary = yes
                            }
                        }
                    }
                    add_takeover_phase_points = {
                        phase = unification_ordered
                        points = {
                            value = root.highest_held_title_tier
                            divide = 2
                        }
                    }
                }
                every_situation_sub_region = {
                    limit = {
                        any_situation_sub_region_participant = {
                            this = root
                            OR = {
                                is_independent_ruler = yes
                                is_tributary = yes
                            }
                            stewardship >= 24
                        }
                    }
                    add_takeover_phase_points = {
                        phase = unification_ordered
                        points = 1
                    }
                }
                every_situation_sub_region = {
                    limit = {
                        any_situation_sub_region_participant = {
                            this = root
                            is_independent_ruler = no
                        }
                    }
                    add_takeover_phase_points = {
                        phase = unification_ordered
                        points = {
                            value = root.highest_held_title_tier
                        }
                    }
                }
                every_situation_sub_region = {
                    limit = {
                        any_situation_sub_region_participant = {
                            this = root
                            is_independent_ruler = no
                            stewardship >= 12
                        }
                    }
                    add_takeover_phase_points = {
                        phase = unification_ordered
                        points = 1
                    }
                }
            }
        }



        # Chaos
        if = {
            limit = {
                OR = {
                    government_has_flag = government_is_tribal
                    government_has_flag = government_is_nomadic
                    faith = { has_doctrine_parameter = unreformed }
                    culture = { has_cultural_pillar = ethos_bellicose }
                    culture_tradition:tradition_quarrelsome = { is_in_list = traits }
                    culture_tradition:tradition_malleable_invaders = { is_in_list = traits }
                    culture_tradition:tradition_warrior_culture = { is_in_list = traits }
                    culture_tradition:tradition_martial_admiration = { is_in_list = traits }
                    faith = { has_doctrine = tenet_warmonger }
                    faith = { has_doctrine = tenet_ritual_cannibalism }
                    faith = { has_doctrine = tenet_human_sacrifice }
                    faith = { has_doctrine = tenet_sacrificial_ceremonies }
                    faith = { has_doctrine = tenet_pursuit_of_power }
                    faith = { has_doctrine = tenet_gruesome_festivals }
                    faith = { has_doctrine = tenet_exaltation_of_pain }
                }
            }
            every_character_situation = {
                limit = {
                    this = situation:regional_destiny_situation
                }
                every_situation_sub_region = {
                    limit = {
                        any_situation_sub_region_participant = {
                            this = root
                            is_independent_ruler = no
                        }
                    }
                    add_takeover_phase_points = {
                        phase = unification_chaotic
                        points = {
                            value = root.highest_held_title_tier
                            divide = 2
                        }
                    }
                }
                every_situation_sub_region = {
                    limit = {
                        any_situation_sub_region_participant = {
                            this = root
                            is_independent_ruler = no
                            martial >= 24
                        }
                    }
                    add_takeover_phase_points = {
                        phase = unification_chaotic
                        points = 1
                    }
                }
                every_situation_sub_region = {
                    limit = {
                        any_situation_sub_region_participant = {
                            this = root
                            OR = {
                                is_independent_ruler = yes
                                is_tributary = yes
                            }
                        }
                    }
                    add_takeover_phase_points = {
                        phase = unification_chaotic
                        points = {
                            value = root.highest_held_title_tier
                        }
                    }
                }
                every_situation_sub_region = {
                    limit = {
                        any_situation_sub_region_participant = {
                            this = root
                            OR = {
                                is_independent_ruler = yes
                                is_tributary = yes
                            }
                            martial >= 12
                        }
                    }
                    add_takeover_phase_points = {
                        phase = unification_chaotic
                        points = 1
                    }
                }
            }
        }
        if = {
            limit = {
                OR = {
                    piety_level = 0
                    prestige_level = 0
                    num_sinful_traits >= 2
                    debt_level >= 1
                    is_imprisoned = yes
                    is_a_faction_member = yes
                    has_targeting_faction = yes
                    num_of_relation_rival >= 3
                    AND = {
                        has_trait = sadistic
                        ai_energy > 0
                        ai_boldness > 0
                    }
                }
            }
            every_character_situation = {
                limit = {
                    this = situation:regional_destiny_situation
                }
                every_situation_sub_region = {
                    limit = {
                        any_situation_sub_region_participant = {
                            this = root
                            is_independent_ruler = no
                        }
                    }
                    add_takeover_phase_points = {
                        phase = unification_chaotic
                        points = {
                            value = root.highest_held_title_tier
                            divide = 2
                        }
                    }
                }
                every_situation_sub_region = {
                    limit = {
                        any_situation_sub_region_participant = {
                            this = root
                            is_independent_ruler = no
                            martial >= 24
                        }
                    }
                    add_takeover_phase_points = {
                        phase = unification_chaotic
                        points = 1
                    }
                }
                every_situation_sub_region = {
                    limit = {
                        any_situation_sub_region_participant = {
                            this = root
                            OR = {
                                is_independent_ruler = yes
                                is_tributary = yes
                            }
                        }
                    }
                    add_takeover_phase_points = {
                        phase = unification_chaotic
                        points = {
                            value = root.highest_held_title_tier
                        }
                    }
                }
                every_situation_sub_region = {
                    limit = {
                        any_situation_sub_region_participant = {
                            this = root
                            OR = {
                                is_independent_ruler = yes
                                is_tributary = yes
                            }
                            martial >= 12
                        }
                    }
                    add_takeover_phase_points = {
                        phase = unification_chaotic
                        points = 1
                    }
                }
            }
        }
        if = {
            limit = {
                OR = {
                    is_a_faction_leader = yes
                    any_scheme = { is_hostile = yes }
                    AND = { is_at_war = yes any_character_war = { is_war_leader = root } }
                    AND = { is_at_war = yes any_character_war = { is_civil_war = yes } }
                    any_army = { is_army_in_raid = yes }
                    any_targeting_faction = { target_of_powerful_faction_trigger = yes }
                    AND = { exists = house house ?= { has_house_unity_stage = antagonistic } }
                    any_directly_owned_province = {
                        OR = {
                            is_occupied = yes
                            is_raided = yes
                            any_province_epidemic = {
                                outbreak_intensity >= minor
                            }
                        }
                    }
                    any_held_county = {
                        percent >= 0.4
                        OR = {
                            county_control < 50
                            county_opinion < -50
                        }
                    }
                    AND = {
                        OR = {
                            is_independent_ruler = yes
                            is_tributary = yes
                        }
                        any_neighboring_top_liege_realm_owner = {
                            percent >= 0.4
                            OR = {
                                has_relation_rival = root
                                has_relation_nemesis = root
                                opinion = {
                                    target = root
                                    value <= -40
                                }
                                faith = {
                                    faith_hostility_level = {
                                        target = root.faith
                                        value >= 2
                                    }
                                }
                                culture = {
                                    cultural_acceptance = {
                                        target = root.culture
                                        value <= 25
                                    }
                                }
                            }
                        }
                    }
                    AND = {
                        is_independent_ruler = no
                        any_neighboring_realm_same_rank_owner = {
                            percent >= 0.4
                            OR = {
                                has_relation_rival = root
                                has_relation_nemesis = root
                                opinion = {
                                    target = root
                                    value <= -40
                                }
                                faith = {
                                    faith_hostility_level = {
                                        target = root.faith
                                        value >= 2
                                    }
                                }
                                culture = {
                                    cultural_acceptance = {
                                        target = root.culture
                                        value <= 25
                                    }
                                }
                            }
                        }
                    }
                    AND = {
                        highest_held_title_tier = 2
                        legitimacy_level <= 0
                    }
                    AND = {
                        highest_held_title_tier = 3
                        legitimacy_level <= 1
                    }
                    AND = {
                        highest_held_title_tier = 4
                        legitimacy_level <= 2
                    }
                    AND = {
                        highest_held_title_tier = 5
                        legitimacy_level <= 3
                    }            
                }
            }
            every_character_situation = {
                limit = {
                    this = situation:regional_destiny_situation
                }
                every_situation_sub_region = {
                    limit = {
                        any_situation_sub_region_participant = {
                            this = root
                            is_independent_ruler = no
                        }
                    }
                    add_takeover_phase_points = {
                        phase = unification_chaotic
                        points = {
                            value = root.highest_held_title_tier
                        }
                    }
                }
                every_situation_sub_region = {
                    limit = {
                        any_situation_sub_region_participant = {
                            this = root
                            is_independent_ruler = no
                            martial >= 12
                        }
                    }
                    add_takeover_phase_points = {
                        phase = unification_chaotic
                        points = 1
                    }
                }
                every_situation_sub_region = {
                    limit = {
                        any_situation_sub_region_participant = {
                            this = root
                            OR = {
                                is_independent_ruler = yes
                                is_tributary = yes
                            }
                        }
                    }
                    add_takeover_phase_points = {
                        phase = unification_chaotic
                        points = {
                            value = root.highest_held_title_tier
                            multiply = 2
                        }
                    }
                }
                every_situation_sub_region = {
                    limit = {
                        any_situation_sub_region_participant = {
                            this = root
                            OR = {
                                is_independent_ruler = yes
                                is_tributary = yes
                            }
                            martial >= 6
                        }
                    }
                    add_takeover_phase_points = {
                        phase = unification_chaotic
                        points = 1
                    }
                }
            }
        }
        if = {
            limit = {
                OR = {
                    has_trait = excommunicated
                    AND = { is_at_war_as_defender = yes any_character_war = { is_war_leader = root } }
                    AND = { is_at_war = yes any_character_war = { is_war_leader = root is_civil_war = yes } }
                    AND = {
                        domain_size > 3
                        any_directly_owned_province = {
                            count >= 3
                            OR = {
                                is_occupied = yes
                                is_raided = yes
                                any_province_epidemic = {
                                    outbreak_intensity >= minor
                                }
                            }
                        }
                    }
                    any_directly_owned_province = {
                        any_province_epidemic = {
                            outbreak_intensity >= major
                        }
                    }
                    AND = {
                        highest_held_title_tier = 3
                        legitimacy_level <= 0
                    }
                    AND = {
                        highest_held_title_tier = 4
                        legitimacy_level <= 1
                    }
                    AND = {
                        highest_held_title_tier = 5
                        legitimacy_level <= 2
                    }            
                }
            }
            every_character_situation = {
                limit = {
                    this = situation:regional_destiny_situation
                }
                every_situation_sub_region = {
                    limit = {
                        any_situation_sub_region_participant = {
                            this = root
                            is_independent_ruler = no
                        }
                    }
                    add_takeover_phase_points = {
                        phase = unification_chaotic
                        points = {
                            value = root.highest_held_title_tier
                        }
                    }
                }
                every_situation_sub_region = {
                    limit = {
                        any_situation_sub_region_participant = {
                            this = root
                            is_independent_ruler = no
                            martial >= 12
                        }
                    }
                    add_takeover_phase_points = {
                        phase = unification_chaotic
                        points = 1
                    }
                }
                every_situation_sub_region = {
                    limit = {
                        any_situation_sub_region_participant = {
                            this = root
                            OR = {
                                is_independent_ruler = yes
                                is_tributary = yes
                            }
                        }
                    }
                    add_takeover_phase_points = {
                        phase = unification_chaotic
                        points = {
                            value = root.highest_held_title_tier
                            multiply = 2
                        }
                    }
                }
                every_situation_sub_region = {
                    limit = {
                        any_situation_sub_region_participant = {
                            this = root
                            OR = {
                                is_independent_ruler = yes
                                is_tributary = yes
                            }
                            martial >= 6
                        }
                    }
                    add_takeover_phase_points = {
                        phase = unification_chaotic
                        points = 1
                    }
                }
            }
        }
    }
}









