namespace = vassalize_all

character_event = {
    id = vassalize_all.1
    desc = vassalize_all.1.desc
    picture = "GFX_evt_bloodlines"
    
    is_triggered_only = yes
    
    # All baronies
    option = {
        name = vassalize_all.1.opta
        # Gives away all baronies first
        while = {
            limit = {
                OR = {
                    # Feudal / Iqta
                    any_demesne_title = {
                        is_feudal = yes
                        tier = baron
                        NOT = { tier = count }
                        is_capital = no
                    }
                    # Republics
                    any_demesne_title = {
                        is_republic = yes
                        tier = baron
                        NOT = { tier = count }
                        NOT = { holding_type = family_palace } # Prevents game over from giving away family palace
                        is_capital = no
                    }
                    # Theocracies
                    any_demesne_title = {
                        is_theocracy = yes
                        tier = baron
                        NOT = { tier = count }
                        is_capital = no
                    }
                    # Tribal
                    any_demesne_title = {
                        is_tribal = yes
                        tier = baron
                        NOT = { tier = count }
                        is_capital = no
                    }
                }
            }
            create_character = {
                random_traits = yes
                dynasty = random
                female = no
                religion = ROOT
                culture = ROOT
            }
            new_character = {
                set_defacto_liege = ROOT 
                remove_trait = ambitious
                liege = {
                    random_demesne_title = {
                        limit = {
                            OR = {
                                AND = {
                                    is_feudal = yes
                                    tier = baron
                                    NOT = { tier = count }
                                    is_capital = no
                                }
                                AND = {
                                    is_republic = yes
                                    tier = baron
                                    NOT = { tier = count }
                                    NOT = { holding_type = family_palace }
                                    is_capital = no
                                }
                                AND = {
                                    is_theocracy = yes
                                    tier = baron
                                    NOT = { tier = count }
                                    is_capital = no
                                }
                                AND = {
                                    is_tribal = yes
                                    tier = baron
                                    NOT = { tier = count }
                                    is_capital = no
                                }
                            }
                        }
                        grant_title = PREVPREV
                    }
                }
            }
        }
        
        # Gives away counties that are unflagged
        while = {
            limit = {
                any_demesne_province = {
                    NOT = { has_province_flag = keep_it }
                }
                any_demesne_province = {
                    has_province_flag = keep_it
                }
                NOT = {
                    any_demesne_title = {
                        is_feudal = yes
                        tier = baron
                        is_capital = no
                    }
                }
                NOT = {
                    any_demesne_title = {
                        is_republic = yes
                        tier = baron
                        NOT = { holding_type = family_palace }
                        is_capital = no
                    }
                }
                NOT = {
                    any_demesne_title = {
                        is_theocracy = yes
                        tier = baron
                        is_capital = no
                    }
                }
                NOT = {
                    any_demesne_title = {
                        is_tribal = yes
                        tier = baron
                        is_capital = no
                    }
                }
            }
            create_character = {
                random_traits = yes
                dynasty = random
                female = no
                religion = ROOT
                culture = ROOT
            }
            new_character = {
                set_defacto_liege = ROOT 
                remove_trait = ambitious
                liege = {
                    random_demesne_province = {
                        limit = {
                            NOT = { has_province_flag = keep_it }
                        }
                        county = {
                            grant_title = PREVPREVPREV
                        }
                    }
                }
            }
        }
        
        character_event = { id = vassalize_all.100 }    
    }
    
    # Keep flagged baronies
    option = {
        name = vassalize_all.1.optb
        # Gives away all baronies first
        while = {
            limit = {
                OR = {
                    # Feudal / Iqta
                    any_demesne_title = {
                        is_feudal = yes
                        tier = baron
                        NOT = {
                            OR = { 
                                tier = count
                                location = { has_province_flag = keep_it }
                            }
                        }
                        is_capital = no
                    }
                    # Republics
                    any_demesne_title = {
                        is_republic = yes
                        tier = baron
                        NOT = {
                            OR = { 
                                holding_type = family_palace # Prevents game over from giving away family palace
                                tier = count
                                location = { has_province_flag = keep_it }
                            }
                        }
                        is_capital = no
                    }
                    # Theocracies
                    any_demesne_title = {
                        is_theocracy = yes
                        tier = baron
                        NOT = {
                            OR = { 
                                tier = count
                                location = { has_province_flag = keep_it }
                            }
                        }
                        is_capital = no
                    }
                    # Tribal
                    any_demesne_title = {
                        is_tribal = yes
                        tier = baron
                        NOT = {
                            OR = { 
                                tier = count
                                location = { has_province_flag = keep_it }
                            }
                        }
                        is_capital = no
                    }
                }
            }
            create_character = {
                random_traits = yes
                dynasty = random
                female = no
                religion = ROOT
                culture = ROOT
            }
            new_character = {
                set_defacto_liege = ROOT 
                remove_trait = ambitious
                liege = {
                    random_demesne_title = {
                        limit = {
                            OR = {
                                AND = {
                                    is_feudal = yes
                                    tier = baron
                                    NOT = {
                                        OR = { 
                                            tier = count
                                            location = { has_province_flag = keep_it }
                                        }
                                    }
                                    is_capital = no
                                }
                                AND = {
                                    is_republic = yes
                                    tier = baron
                                    NOT = {
                                        OR = { 
                                            holding_type = family_palace # Prevents game over from giving away family palace
                                            tier = count
                                            location = { has_province_flag = keep_it }
                                        }
                                    }
                                    is_capital = no
                                }
                                AND = {
                                    is_theocracy = yes
                                    tier = baron
                                    NOT = {
                                        OR = { 
                                            tier = count
                                            location = { has_province_flag = keep_it }
                                        }
                                    }
                                    is_capital = no
                                }
                                AND = {
                                    is_tribal = yes
                                    tier = baron
                                    NOT = {
                                        OR = { 
                                            tier = count
                                            location = { has_province_flag = keep_it }
                                        }
                                    }
                                    is_capital = no
                                }
                            }
                        }
                        grant_title = PREVPREV
                    }
                }
            }
        }
        
        # Gives away counties that are unflagged
        while = {
            limit = {
                any_demesne_province = {
                    NOT = { has_province_flag = keep_it }
                }
                any_demesne_province = {
                    has_province_flag = keep_it
                }
                NOT = {
                    any_demesne_title = {
                        is_feudal = yes
                        tier = baron
                        NOT = { 
                            location = { has_province_flag = keep_it } 
                        }
                        is_capital = no
                    }
                }
                NOT = {
                    any_demesne_title = {
                        is_republic = yes
                        tier = baron
                        NOT = {
                            OR = { 
                                holding_type = family_palace 
                                location = { has_province_flag = keep_it }
                            }
                        }
                        is_capital = no
                    }
                }
                NOT = {
                    any_demesne_title = {
                        is_theocracy = yes
                        tier = baron
                        NOT = { 
                            location = { has_province_flag = keep_it } 
                        }
                        is_capital = no
                    }
                }
                NOT = {
                    any_demesne_title = {
                        is_tribal = yes
                        tier = baron
                        NOT = { 
                            location = { has_province_flag = keep_it } 
                        }
                        is_capital = no
                    }
                }
            }
            create_character = {
                random_traits = yes
                dynasty = random
                female = no
                religion = ROOT
                culture = ROOT
            }
            new_character = {
                set_defacto_liege = ROOT 
                remove_trait = ambitious
                liege = {
                    random_demesne_province = {
                        limit = {
                            NOT = { has_province_flag = keep_it }
                        }
                        county = {
                            grant_title = PREVPREVPREV
                        }
                    }
                }
            }
        }
        
        character_event = { id = vassalize_all.100 }
    }
    
    option = {
        name = vassalize_all.1.optc
    }
}

character_event = {
    id = vassalize_all.2
    desc = vassalize_all.1.desc
    picture = "GFX_evt_bloodlines"
    
    is_triggered_only = yes
    
    # All baronies
    option = {
        name = vassalize_all.1.opta
        # Gives away all baronies first
        while = {
            limit = {
                OR = {
                    # Feudal / Iqta
                    any_demesne_title = {
                        is_feudal = yes
                        tier = baron
                        NOT = { tier = count }
                        is_capital = no
                    }
                    # Republics
                    any_demesne_title = {
                        is_republic = yes
                        tier = baron
                        NOT = { tier = count }
                        NOT = { holding_type = family_palace } # Prevents game over from giving away family palace
                        is_capital = no
                    }
                    # Theocracies
                    any_demesne_title = {
                        is_theocracy = yes
                        tier = baron
                        NOT = { tier = count }
                        is_capital = no
                    }
                    # Tribal
                    any_demesne_title = {
                        is_tribal = yes
                        tier = baron
                        NOT = { tier = count }
                        is_capital = no
                    }
                }
            }
            create_character = {
                random_traits = yes
                dynasty = random
                female = yes
                religion = ROOT
                culture = ROOT
            }
            new_character = {
                set_defacto_liege = ROOT 
                remove_trait = ambitious
                liege = {
                    random_demesne_title = {
                        limit = {
                            OR = {
                                AND = {
                                    is_feudal = yes
                                    tier = baron
                                    NOT = { tier = count }
                                    is_capital = no
                                }
                                AND = {
                                    is_republic = yes
                                    tier = baron
                                    NOT = { tier = count }
                                    NOT = { holding_type = family_palace }
                                    is_capital = no
                                }
                                AND = {
                                    is_theocracy = yes
                                    tier = baron
                                    NOT = { tier = count }
                                    is_capital = no
                                }
                                AND = {
                                    is_tribal = yes
                                    tier = baron
                                    NOT = { tier = count }
                                    is_capital = no
                                }
                            }
                        }
                        grant_title = PREVPREV
                    }
                }
            }
        }
        
        # Gives away counties that are unflagged
        while = {
            limit = {
                any_demesne_province = {
                    NOT = { has_province_flag = keep_it }
                }
                any_demesne_province = {
                    has_province_flag = keep_it
                }
                NOT = {
                    any_demesne_title = {
                        is_feudal = yes
                        tier = baron
                        is_capital = no
                    }
                }
                NOT = {
                    any_demesne_title = {
                        is_republic = yes
                        tier = baron
                        NOT = { holding_type = family_palace }
                        is_capital = no
                    }
                }
                NOT = {
                    any_demesne_title = {
                        is_theocracy = yes
                        tier = baron
                        is_capital = no
                    }
                }
                NOT = {
                    any_demesne_title = {
                        is_tribal = yes
                        tier = baron
                        is_capital = no
                    }
                }
            }
            create_character = {
                random_traits = yes
                dynasty = random
                female = yes
                religion = ROOT
                culture = ROOT
            }
            new_character = {
                set_defacto_liege = ROOT 
                remove_trait = ambitious
                liege = {
                    random_demesne_province = {
                        limit = {
                            NOT = { has_province_flag = keep_it }
                        }
                        county = {
                            grant_title = PREVPREVPREV
                        }
                    }
                }
            }
        }
        
        character_event = { id = vassalize_all.100 }    
    }
    
    # Keep flagged baronies
    option = {
        name = vassalize_all.1.optb
        # Gives away all baronies first
        while = {
            limit = {
                OR = {
                    # Feudal / Iqta
                    any_demesne_title = {
                        is_feudal = yes
                        tier = baron
                        NOT = {
                            OR = { 
                                tier = count
                                location = { has_province_flag = keep_it }
                            }
                        }
                        is_capital = no
                    }
                    # Republics
                    any_demesne_title = {
                        is_republic = yes
                        tier = baron
                        NOT = {
                            OR = { 
                                holding_type = family_palace # Prevents game over from giving away family palace
                                tier = count
                                location = { has_province_flag = keep_it }
                            }
                        }
                        is_capital = no
                    }
                    # Theocracies
                    any_demesne_title = {
                        is_theocracy = yes
                        tier = baron
                        NOT = {
                            OR = { 
                                tier = count
                                location = { has_province_flag = keep_it }
                            }
                        }
                        is_capital = no
                    }
                    # Tribal
                    any_demesne_title = {
                        is_tribal = yes
                        tier = baron
                        NOT = {
                            OR = { 
                                tier = count
                                location = { has_province_flag = keep_it }
                            }
                        }
                        is_capital = no
                    }
                }
            }
            create_character = {
                random_traits = yes
                dynasty = random
                female = yes
                religion = ROOT
                culture = ROOT
            }
            new_character = {
                set_defacto_liege = ROOT 
                remove_trait = ambitious
                liege = {
                    random_demesne_title = {
                        limit = {
                            OR = {
                                AND = {
                                    is_feudal = yes
                                    tier = baron
                                    NOT = {
                                        OR = { 
                                            tier = count
                                            location = { has_province_flag = keep_it }
                                        }
                                    }
                                    is_capital = no
                                }
                                AND = {
                                    is_republic = yes
                                    tier = baron
                                    NOT = {
                                        OR = { 
                                            holding_type = family_palace # Prevents game over from giving away family palace
                                            tier = count
                                            location = { has_province_flag = keep_it }
                                        }
                                    }
                                    is_capital = no
                                }
                                AND = {
                                    is_theocracy = yes
                                    tier = baron
                                    NOT = {
                                        OR = { 
                                            tier = count
                                            location = { has_province_flag = keep_it }
                                        }
                                    }
                                    is_capital = no
                                }
                                AND = {
                                    is_tribal = yes
                                    tier = baron
                                    NOT = {
                                        OR = { 
                                            tier = count
                                            location = { has_province_flag = keep_it }
                                        }
                                    }
                                    is_capital = no
                                }
                            }
                        }
                        grant_title = PREVPREV
                    }
                }
            }
        }
        
        # Gives away counties that are unflagged
        while = {
            limit = {
                any_demesne_province = {
                    NOT = { has_province_flag = keep_it }
                }
                any_demesne_province = {
                    has_province_flag = keep_it
                }
                NOT = {
                    any_demesne_title = {
                        is_feudal = yes
                        tier = baron
                        NOT = { 
                            location = { has_province_flag = keep_it } 
                        }
                        is_capital = no
                    }
                }
                NOT = {
                    any_demesne_title = {
                        is_republic = yes
                        tier = baron
                        NOT = {
                            OR = { 
                                holding_type = family_palace 
                                location = { has_province_flag = keep_it }
                            }
                        }
                        is_capital = no
                    }
                }
                NOT = {
                    any_demesne_title = {
                        is_theocracy = yes
                        tier = baron
                        NOT = { 
                            location = { has_province_flag = keep_it } 
                        }
                        is_capital = no
                    }
                }
                NOT = {
                    any_demesne_title = {
                        is_tribal = yes
                        tier = baron
                        NOT = { 
                            location = { has_province_flag = keep_it } 
                        }
                        is_capital = no
                    }
                }
            }
            create_character = {
                random_traits = yes
                dynasty = random
                female = yes
                religion = ROOT
                culture = ROOT
            }
            new_character = {
                set_defacto_liege = ROOT 
                remove_trait = ambitious
                liege = {
                    random_demesne_province = {
                        limit = {
                            NOT = { has_province_flag = keep_it }
                        }
                        county = {
                            grant_title = PREVPREVPREV
                        }
                    }
                }
            }
        }
        
        character_event = { id = vassalize_all.100 }
    }
    
    option = {
        name = vassalize_all.1.optc
    }
}

character_event = {
    id = vassalize_all.3
    desc = vassalize_all.1.desc
    picture = "GFX_evt_bloodlines"
    
    is_triggered_only = yes
    
    # All baronies
    option = {
        name = vassalize_all.1.opta
        # Gives away all baronies first
        while = {
            limit = {
                OR = {
                    # Feudal / Iqta
                    any_demesne_title = {
                        is_feudal = yes
                        tier = baron
                        NOT = { tier = count }
                        is_capital = no
                    }
                    # Republics
                    any_demesne_title = {
                        is_republic = yes
                        tier = baron
                        NOT = { tier = count }
                        NOT = { holding_type = family_palace } # Prevents game over from giving away family palace
                        is_capital = no
                    }
                    # Theocracies
                    any_demesne_title = {
                        is_theocracy = yes
                        tier = baron
                        NOT = { tier = count }
                        is_capital = no
                    }
                    # Tribal
                    any_demesne_title = {
                        is_tribal = yes
                        tier = baron
                        NOT = { tier = count }
                        is_capital = no
                    }
                }
            }
            create_character = {
                random_traits = yes
                dynasty = random
                female = 50
                religion = ROOT
                culture = ROOT
            }
            new_character = {
                set_defacto_liege = ROOT 
                remove_trait = ambitious
                liege = {
                    random_demesne_title = {
                        limit = {
                            OR = {
                                AND = {
                                    is_feudal = yes
                                    tier = baron
                                    NOT = { tier = count }
                                    is_capital = no
                                }
                                AND = {
                                    is_republic = yes
                                    tier = baron
                                    NOT = { tier = count }
                                    NOT = { holding_type = family_palace }
                                    is_capital = no
                                }
                                AND = {
                                    is_theocracy = yes
                                    tier = baron
                                    NOT = { tier = count }
                                    is_capital = no
                                }
                                AND = {
                                    is_tribal = yes
                                    tier = baron
                                    NOT = { tier = count }
                                    is_capital = no
                                }
                            }
                        }
                        grant_title = PREVPREV
                    }
                }
            }
        }
        
        # Gives away counties that are unflagged
        while = {
            limit = {
                any_demesne_province = {
                    NOT = { has_province_flag = keep_it }
                }
                any_demesne_province = {
                    has_province_flag = keep_it
                }
                NOT = {
                    any_demesne_title = {
                        is_feudal = yes
                        tier = baron
                        is_capital = no
                    }
                }
                NOT = {
                    any_demesne_title = {
                        is_republic = yes
                        tier = baron
                        NOT = { holding_type = family_palace }
                        is_capital = no
                    }
                }
                NOT = {
                    any_demesne_title = {
                        is_theocracy = yes
                        tier = baron
                        is_capital = no
                    }
                }
                NOT = {
                    any_demesne_title = {
                        is_tribal = yes
                        tier = baron
                        is_capital = no
                    }
                }
            }
            create_character = {
                random_traits = yes
                dynasty = random
                female = 50
                religion = ROOT
                culture = ROOT
            }
            new_character = {
                set_defacto_liege = ROOT 
                remove_trait = ambitious
                liege = {
                    random_demesne_province = {
                        limit = {
                            NOT = { has_province_flag = keep_it }
                        }
                        county = {
                            grant_title = PREVPREVPREV
                        }
                    }
                }
            }
        }
        
        character_event = { id = vassalize_all.100 }    
    }
    
    # Keep flagged baronies
    option = {
        name = vassalize_all.1.optb
        # Gives away all baronies first
        while = {
            limit = {
                OR = {
                    # Feudal / Iqta
                    any_demesne_title = {
                        is_feudal = yes
                        tier = baron
                        NOT = {
                            OR = { 
                                tier = count
                                location = { has_province_flag = keep_it }
                            }
                        }
                        is_capital = no
                    }
                    # Republics
                    any_demesne_title = {
                        is_republic = yes
                        tier = baron
                        NOT = {
                            OR = { 
                                holding_type = family_palace # Prevents game over from giving away family palace
                                tier = count
                                location = { has_province_flag = keep_it }
                            }
                        }
                        is_capital = no
                    }
                    # Theocracies
                    any_demesne_title = {
                        is_theocracy = yes
                        tier = baron
                        NOT = {
                            OR = { 
                                tier = count
                                location = { has_province_flag = keep_it }
                            }
                        }
                        is_capital = no
                    }
                    # Tribal
                    any_demesne_title = {
                        is_tribal = yes
                        tier = baron
                        NOT = {
                            OR = { 
                                tier = count
                                location = { has_province_flag = keep_it }
                            }
                        }
                        is_capital = no
                    }
                }
            }
            create_character = {
                random_traits = yes
                dynasty = random
                female = 50
                religion = ROOT
                culture = ROOT
            }
            new_character = {
                set_defacto_liege = ROOT 
                remove_trait = ambitious
                liege = {
                    random_demesne_title = {
                        limit = {
                            OR = {
                                AND = {
                                    is_feudal = yes
                                    tier = baron
                                    NOT = {
                                        OR = { 
                                            tier = count
                                            location = { has_province_flag = keep_it }
                                        }
                                    }
                                    is_capital = no
                                }
                                AND = {
                                    is_republic = yes
                                    tier = baron
                                    NOT = {
                                        OR = { 
                                            holding_type = family_palace # Prevents game over from giving away family palace
                                            tier = count
                                            location = { has_province_flag = keep_it }
                                        }
                                    }
                                    is_capital = no
                                }
                                AND = {
                                    is_theocracy = yes
                                    tier = baron
                                    NOT = {
                                        OR = { 
                                            tier = count
                                            location = { has_province_flag = keep_it }
                                        }
                                    }
                                    is_capital = no
                                }
                                AND = {
                                    is_tribal = yes
                                    tier = baron
                                    NOT = {
                                        OR = { 
                                            tier = count
                                            location = { has_province_flag = keep_it }
                                        }
                                    }
                                    is_capital = no
                                }
                            }
                        }
                        grant_title = PREVPREV
                    }
                }
            }
        }
        
        # Gives away counties that are unflagged
        while = {
            limit = {
                any_demesne_province = {
                    NOT = { has_province_flag = keep_it }
                }
                any_demesne_province = {
                    has_province_flag = keep_it
                }
                NOT = {
                    any_demesne_title = {
                        is_feudal = yes
                        tier = baron
                        NOT = { 
                            location = { has_province_flag = keep_it } 
                        }
                        is_capital = no
                    }
                }
                NOT = {
                    any_demesne_title = {
                        is_republic = yes
                        tier = baron
                        NOT = {
                            OR = { 
                                holding_type = family_palace 
                                location = { has_province_flag = keep_it }
                            }
                        }
                        is_capital = no
                    }
                }
                NOT = {
                    any_demesne_title = {
                        is_theocracy = yes
                        tier = baron
                        NOT = { 
                            location = { has_province_flag = keep_it } 
                        }
                        is_capital = no
                    }
                }
                NOT = {
                    any_demesne_title = {
                        is_tribal = yes
                        tier = baron
                        NOT = { 
                            location = { has_province_flag = keep_it } 
                        }
                        is_capital = no
                    }
                }
            }
            create_character = {
                random_traits = yes
                dynasty = random
                female = 50
                religion = ROOT
                culture = ROOT
            }
            new_character = {
                set_defacto_liege = ROOT 
                remove_trait = ambitious
                liege = {
                    random_demesne_province = {
                        limit = {
                            NOT = { has_province_flag = keep_it }
                        }
                        county = {
                            grant_title = PREVPREVPREV
                        }
                    }
                }
            }
        }
        
        character_event = { id = vassalize_all.100 }
    }
    
    option = {
        name = vassalize_all.1.optc
    }
}

# For nomads, will only distribute counties to existing khans
character_event = {
    id = vassalize_all.4
    desc = vassalize_all.4.desc
    picture = "GFX_evt_hl_steppe_mercenaries"
    
    is_triggered_only = yes
    
    # All baronies
    option = {
        name = vassalize_all.1.opta
        # Gives away all baronies first
        while = {
            limit = {
                OR = {
                    # Feudal / Iqta
                    any_demesne_title = {
                        is_feudal = yes
                        tier = baron
                        NOT = { tier = count }
                        is_capital = no
                    }
                    # Republics
                    any_demesne_title = {
                        is_republic = yes
                        tier = baron
                        NOT = { tier = count }
                        NOT = { holding_type = family_palace } # Prevents game over from giving away family palace
                        is_capital = no
                    }
                    # Theocracies
                    any_demesne_title = {
                        is_theocracy = yes
                        tier = baron
                        NOT = { tier = count }
                        is_capital = no
                    }
                    # Tribal
                    any_demesne_title = {
                        is_tribal = yes
                        tier = baron
                        NOT = { tier = count }
                        is_capital = no
                    }
                }
            }
            create_character = {
                random_traits = yes
                dynasty = random
                female = no
                religion = ROOT
                culture = ROOT
            }
            new_character = {
                set_defacto_liege = ROOT 
                remove_trait = ambitious
                liege = {
                    random_demesne_title = {
                        limit = {
                            OR = {
                                AND = {
                                    is_feudal = yes
                                    tier = baron
                                    NOT = { tier = count }
                                    is_capital = no
                                }
                                AND = {
                                    is_republic = yes
                                    tier = baron
                                    NOT = { tier = count }
                                    NOT = { holding_type = family_palace }
                                    is_capital = no
                                }
                                AND = {
                                    is_theocracy = yes
                                    tier = baron
                                    NOT = { tier = count }
                                    is_capital = no
                                }
                                AND = {
                                    is_tribal = yes
                                    tier = baron
                                    NOT = { tier = count }
                                    is_capital = no
                                }
                            }
                        }
                        grant_title = PREVPREV
                    }
                }
            }
        }
        
        # Gives away counties that are unflagged
        while = {
            limit = {
                any_demesne_province = {
                    NOT = { has_province_flag = keep_it }
                }
                any_demesne_province = {
                    has_province_flag = keep_it
                }
                NOT = {
                    any_demesne_title = {
                        is_feudal = yes
                        tier = baron
                        is_capital = no
                    }
                }
                NOT = {
                    any_demesne_title = {
                        is_republic = yes
                        tier = baron
                        NOT = { holding_type = family_palace }
                        is_capital = no
                    }
                }
                NOT = {
                    any_demesne_title = {
                        is_theocracy = yes
                        tier = baron
                        is_capital = no
                    }
                }
                NOT = {
                    any_demesne_title = {
                        is_tribal = yes
                        tier = baron
                        is_capital = no
                    }
                }
            }
            random_vassal = {
                limit = { clan = yes }
                liege = {
                    random_demesne_province = {
                        limit = {
                            NOT = { has_province_flag = keep_it }
                        }
                        county = {
                            grant_title = PREVPREVPREV
                        }
                    }
                }
            }
        }
        
        character_event = { id = vassalize_all.100 }    
    }
    
    # Keep flagged baronies
    option = {
        name = vassalize_all.1.optb
        # Gives away all baronies first
        while = {
            limit = {
                OR = {
                    # Feudal / Iqta
                    any_demesne_title = {
                        is_feudal = yes
                        tier = baron
                        NOT = {
                            OR = { 
                                tier = count
                                location = { has_province_flag = keep_it }
                            }
                        }
                        is_capital = no
                    }
                    # Republics
                    any_demesne_title = {
                        is_republic = yes
                        tier = baron
                        NOT = {
                            OR = { 
                                holding_type = family_palace # Prevents game over from giving away family palace
                                tier = count
                                location = { has_province_flag = keep_it }
                            }
                        }
                        is_capital = no
                    }
                    # Theocracies
                    any_demesne_title = {
                        is_theocracy = yes
                        tier = baron
                        NOT = {
                            OR = { 
                                tier = count
                                location = { has_province_flag = keep_it }
                            }
                        }
                        is_capital = no
                    }
                    # Tribal
                    any_demesne_title = {
                        is_tribal = yes
                        tier = baron
                        NOT = {
                            OR = { 
                                tier = count
                                location = { has_province_flag = keep_it }
                            }
                        }
                        is_capital = no
                    }
                }
            }
            create_character = {
                random_traits = yes
                dynasty = random
                female = no
                religion = ROOT
                culture = ROOT
            }
            new_character = {
                set_defacto_liege = ROOT 
                remove_trait = ambitious
                liege = {
                    random_demesne_title = {
                        limit = {
                            OR = {
                                AND = {
                                    is_feudal = yes
                                    tier = baron
                                    NOT = {
                                        OR = { 
                                            tier = count
                                            location = { has_province_flag = keep_it }
                                        }
                                    }
                                    is_capital = no
                                }
                                AND = {
                                    is_republic = yes
                                    tier = baron
                                    NOT = {
                                        OR = { 
                                            holding_type = family_palace # Prevents game over from giving away family palace
                                            tier = count
                                            location = { has_province_flag = keep_it }
                                        }
                                    }
                                    is_capital = no
                                }
                                AND = {
                                    is_theocracy = yes
                                    tier = baron
                                    NOT = {
                                        OR = { 
                                            tier = count
                                            location = { has_province_flag = keep_it }
                                        }
                                    }
                                    is_capital = no
                                }
                                AND = {
                                    is_tribal = yes
                                    tier = baron
                                    NOT = {
                                        OR = { 
                                            tier = count
                                            location = { has_province_flag = keep_it }
                                        }
                                    }
                                    is_capital = no
                                }
                            }
                        }
                        grant_title = PREVPREV
                    }
                }
            }
        }
        
        # Gives away counties that are unflagged
        while = {
            limit = {
                any_demesne_province = {
                    NOT = { has_province_flag = keep_it }
                }
                any_demesne_province = {
                    has_province_flag = keep_it
                }
                NOT = {
                    any_demesne_title = {
                        is_feudal = yes
                        tier = baron
                        NOT = { 
                            location = { has_province_flag = keep_it } 
                        }
                        is_capital = no
                    }
                }
                NOT = {
                    any_demesne_title = {
                        is_republic = yes
                        tier = baron
                        NOT = {
                            OR = { 
                                holding_type = family_palace 
                                location = { has_province_flag = keep_it }
                            }
                        }
                        is_capital = no
                    }
                }
                NOT = {
                    any_demesne_title = {
                        is_theocracy = yes
                        tier = baron
                        NOT = { 
                            location = { has_province_flag = keep_it } 
                        }
                        is_capital = no
                    }
                }
                NOT = {
                    any_demesne_title = {
                        is_tribal = yes
                        tier = baron
                        NOT = { 
                            location = { has_province_flag = keep_it } 
                        }
                        is_capital = no
                    }
                }
            }
            random_vassal = {
                limit = { clan = yes }
                liege = {
                    random_demesne_province = {
                        limit = {
                            NOT = { has_province_flag = keep_it }
                        }
                        county = {
                            grant_title = PREVPREVPREV
                        }
                    }
                }
            }
        }
        
        character_event = { id = vassalize_all.100 }
    }
    
    option = {
        name = vassalize_all.1.optc
    }
}

character_event = {
    id = vassalize_all.100
    desc = vassalize_all.100.desc
    picture = "GFX_evt_council"
    
    is_triggered_only = yes
    
    option = {
        name = vassalize_all.100.opta
    }
}