﻿### Councillors ###
### Origin Selection ###
DI_char_spawner_initialize_origin = {
    scope = character
    effect = {
        random_owned_story = {
            limit = { story_type = DI_misc_variables_story }
            if = {
                limit = { NOT = { has_variable = DI_char_spawner_culture_mode } }
                set_variable = { name = DI_char_spawner_culture_mode value = flag:target }
            }
            if = {
                limit = { NOT = { has_variable = DI_char_spawner_faith_mode } }
                set_variable = { name = DI_char_spawner_faith_mode value = flag:target }
            }
            if = {
                limit = { NOT = { has_variable = DI_char_spawner_random_culture_mode } }
                set_variable = { name = DI_char_spawner_random_culture_mode value = flag:global }
            }
            if = {
                limit = { NOT = { has_variable = DI_char_spawner_random_faith_mode } }
                set_variable = { name = DI_char_spawner_random_faith_mode value = flag:global }
            }
            if = {
                limit = { NOT = { has_variable = DI_char_spawner_selected_culture } }
                set_variable = { name = DI_char_spawner_selected_culture value = root.culture }
            }
            if = {
                limit = { NOT = { has_variable = DI_char_spawner_selected_faith } }
                set_variable = { name = DI_char_spawner_selected_faith value = root.faith }
            }
            if = {
                limit = { NOT = { exists = var:DI_char_spawner_selected_rite } }
                if = {
                    limit = { exists = var:DI_char_spawner_selected_faith.main_rite }
                    set_variable = { name = DI_char_spawner_selected_rite value = var:DI_char_spawner_selected_faith.main_rite }
                }
                else = {
                    set_variable = { name = DI_char_spawner_selected_rite value = root.rite }
                }
            }
        }
    }
}

DI_char_spawner_culture_target = {
    scope = character
    effect = {
        random_owned_story = {
            limit = { story_type = DI_misc_variables_story }
            set_variable = { name = DI_char_spawner_culture_mode value = flag:target }
        }
    }
}

DI_char_spawner_culture_random = {
    scope = character
    effect = {
        random_owned_story = {
            limit = { story_type = DI_misc_variables_story }
            set_variable = { name = DI_char_spawner_culture_mode value = flag:random }
        }
    }
}

DI_char_spawner_random_culture_global = {
    scope = character
    effect = {
        random_owned_story = {
            limit = { story_type = DI_misc_variables_story }
            set_variable = { name = DI_char_spawner_random_culture_mode value = flag:global }
            set_variable = { name = DI_char_spawner_culture_mode value = flag:random }
        }
    }
}

DI_char_spawner_random_culture_heritage = {
    scope = character
    effect = {
        random_owned_story = {
            limit = { story_type = DI_misc_variables_story }
            set_variable = { name = DI_char_spawner_random_culture_mode value = flag:heritage }
            set_variable = { name = DI_char_spawner_culture_mode value = flag:random }
        }
    }
}

DI_char_spawner_random_culture_language = {
    scope = character
    effect = {
        random_owned_story = {
            limit = { story_type = DI_misc_variables_story }
            set_variable = { name = DI_char_spawner_random_culture_mode value = flag:language }
            set_variable = { name = DI_char_spawner_culture_mode value = flag:random }
        }
    }
}

DI_char_spawner_random_culture_realm = {
    scope = character
    effect = {
        random_owned_story = {
            limit = { story_type = DI_misc_variables_story }
            set_variable = { name = DI_char_spawner_random_culture_mode value = flag:realm }
            set_variable = { name = DI_char_spawner_culture_mode value = flag:random }
        }
    }
}

DI_char_spawner_random_culture_low_acceptance = {
    scope = character
    effect = {
        random_owned_story = {
            limit = { story_type = DI_misc_variables_story }
            set_variable = { name = DI_char_spawner_random_culture_mode value = flag:low_acceptance }
            set_variable = { name = DI_char_spawner_culture_mode value = flag:random }
        }
    }
}

DI_char_spawner_random_culture_moderate_acceptance = {
    scope = character
    effect = {
        random_owned_story = {
            limit = { story_type = DI_misc_variables_story }
            set_variable = { name = DI_char_spawner_random_culture_mode value = flag:moderate_acceptance }
            set_variable = { name = DI_char_spawner_culture_mode value = flag:random }
        }
    }
}

DI_char_spawner_random_culture_high_acceptance = {
    scope = character
    effect = {
        random_owned_story = {
            limit = { story_type = DI_misc_variables_story }
            set_variable = { name = DI_char_spawner_random_culture_mode value = flag:high_acceptance }
            set_variable = { name = DI_char_spawner_culture_mode value = flag:random }
        }
    }
}

DI_char_spawner_culture_selected = {
    scope = character
    effect = {
        random_owned_story = {
            limit = { story_type = DI_misc_variables_story }
            set_variable = { name = DI_char_spawner_culture_mode value = flag:selected }
        }
    }
}

DI_char_spawner_select_culture = {
    scope = character
    saved_scopes = { selected_culture }
    effect = {
        random_owned_story = {
            limit = { story_type = DI_misc_variables_story }
            set_variable = { name = DI_char_spawner_selected_culture value = scope:selected_culture }
            set_variable = { name = DI_char_spawner_culture_mode value = flag:selected }
        }
    }
}

DI_char_spawner_faith_target = {
    scope = character
    effect = {
        random_owned_story = {
            limit = { story_type = DI_misc_variables_story }
            set_variable = { name = DI_char_spawner_faith_mode value = flag:target }
        }
    }
}

DI_char_spawner_faith_random = {
    scope = character
    effect = {
        random_owned_story = {
            limit = { story_type = DI_misc_variables_story }
            set_variable = { name = DI_char_spawner_faith_mode value = flag:random }
        }
    }
}

DI_char_spawner_random_faith_global = {
    scope = character
    effect = {
        random_owned_story = {
            limit = { story_type = DI_misc_variables_story }
            set_variable = { name = DI_char_spawner_random_faith_mode value = flag:global }
            set_variable = { name = DI_char_spawner_faith_mode value = flag:random }
        }
    }
}

DI_char_spawner_random_faith_religion = {
    scope = character
    effect = {
        random_owned_story = {
            limit = { story_type = DI_misc_variables_story }
            set_variable = { name = DI_char_spawner_random_faith_mode value = flag:religion }
            set_variable = { name = DI_char_spawner_faith_mode value = flag:random }
        }
    }
}

DI_char_spawner_random_faith_family = {
    scope = character
    effect = {
        random_owned_story = {
            limit = { story_type = DI_misc_variables_story }
            set_variable = { name = DI_char_spawner_random_faith_mode value = flag:family }
            set_variable = { name = DI_char_spawner_faith_mode value = flag:random }
        }
    }
}

DI_char_spawner_random_faith_realm = {
    scope = character
    effect = {
        random_owned_story = {
            limit = { story_type = DI_misc_variables_story }
            set_variable = { name = DI_char_spawner_random_faith_mode value = flag:realm }
            set_variable = { name = DI_char_spawner_faith_mode value = flag:random }
        }
    }
}

DI_char_spawner_random_faith_accepted = {
    scope = character
    effect = {
        random_owned_story = {
            limit = { story_type = DI_misc_variables_story }
            set_variable = { name = DI_char_spawner_random_faith_mode value = flag:accepted }
            set_variable = { name = DI_char_spawner_faith_mode value = flag:random }
        }
    }
}

DI_char_spawner_random_faith_astray = {
    scope = character
    effect = {
        random_owned_story = {
            limit = { story_type = DI_misc_variables_story }
            set_variable = { name = DI_char_spawner_random_faith_mode value = flag:astray }
            set_variable = { name = DI_char_spawner_faith_mode value = flag:random }
        }
    }
}

DI_char_spawner_random_faith_hostile = {
    scope = character
    effect = {
        random_owned_story = {
            limit = { story_type = DI_misc_variables_story }
            set_variable = { name = DI_char_spawner_random_faith_mode value = flag:hostile }
            set_variable = { name = DI_char_spawner_faith_mode value = flag:random }
        }
    }
}

DI_char_spawner_faith_selected = {
    scope = character
    effect = {
        random_owned_story = {
            limit = { story_type = DI_misc_variables_story }
            set_variable = { name = DI_char_spawner_faith_mode value = flag:selected }
        }
    }
}

DI_char_spawner_select_faith = {
    scope = character
    saved_scopes = { selected_faith }
    effect = {
        random_owned_story = {
            limit = { story_type = DI_misc_variables_story }
            set_variable = { name = DI_char_spawner_selected_faith value = scope:selected_faith }
            set_variable = { name = DI_char_spawner_selected_rite value = scope:selected_faith.main_rite }
            set_variable = { name = DI_char_spawner_faith_mode value = flag:selected }
        }
    }
}

DI_char_spawner_select_rite = {
    scope = character
    saved_scopes = { selected_rite }
    is_valid = { exists = scope:selected_rite }
    effect = {
        if = {
            limit = { exists = scope:selected_rite }
            random_owned_story = {
                limit = { story_type = DI_misc_variables_story }
                set_variable = { name = DI_char_spawner_selected_rite value = scope:selected_rite }
                set_variable = { name = DI_char_spawner_selected_faith value = scope:selected_rite.faith }
                set_variable = { name = DI_char_spawner_faith_mode value = flag:selected }
            }
        }
    }
}

### Councillors ###
DI_martial_councillor = {
    scope = character

    effect = {
        save_scope_as = root_target
        DI_martial_traits_effect = yes

        random_owned_story = {
            limit = {
                story_type = DI_misc_variables_story
            }
            save_scope_as = DI_story
        }

        if = {
            limit = {
                scope:DI_story = { has_variable = DI_pinned_char_var }
            }
            every_in_list = {
                variable = pinned_characters

                save_scope_as = target

                DI_spawn_court_character_effect = yes

                if = {
                    limit = {
                        exists = cp:councillor_marshal
                    }
                    fire_councillor = cp:councillor_marshal
                }
                assign_councillor_type = {
                    type = councillor_marshal
                    target = scope:new_courtier
                }
            }
        }
        else = {
            save_scope_as = target

            DI_spawn_court_character_effect = yes

            if = {
                limit = {
                    exists = cp:councillor_marshal
                }
                fire_councillor = cp:councillor_marshal
            }
            assign_councillor_type = {
                type = councillor_marshal
                target = scope:new_courtier
            }
        }

        if = {
            limit = {
                scope:root_target.var:DI_cs_random_traits ?= no
            }
            DI_no_random_traits_effect = yes
        }
        else_if = {
            limit = {
                scope:root_target.var:DI_cs_random_traits ?= yes
            }
            DI_random_traits_effect = yes
        }
    }
}

DI_diplomacy_councillor = {
    scope = character

    effect = {
        save_scope_as = root_target
        DI_diplomacy_traits_effect = yes

        random_owned_story = {
            limit = {
                story_type = DI_misc_variables_story
            }
            save_scope_as = DI_story
        }

        if = {
            limit = {
                scope:DI_story = { has_variable = DI_pinned_char_var }
            }
            every_in_list = {
                variable = pinned_characters

                save_scope_as = target

                DI_spawn_court_character_effect = yes

                if = {
                    limit = {
                        exists = cp:councillor_chancellor
                    }
                    fire_councillor = cp:councillor_chancellor
                }
                assign_councillor_type = {
                    type = councillor_chancellor
                    target = scope:new_courtier
                }
            }
        }
        else = {
            save_scope_as = target

            DI_spawn_court_character_effect = yes

            if = {
                limit = {
                    exists = cp:councillor_chancellor
                }
                fire_councillor = cp:councillor_chancellor
            }
            assign_councillor_type = {
                type = councillor_chancellor
                target = scope:new_courtier
            }
        }

        if = {
            limit = {
                scope:root_target.var:DI_cs_random_traits ?= no
            }
            DI_no_random_traits_effect = yes
        }
        else_if = {
            limit = {
                scope:root_target.var:DI_cs_random_traits ?= yes
            }
            DI_random_traits_effect = yes
        }
    }
}

DI_stewardship_councillor = {
    scope = character

    effect = {
        save_scope_as = root_target
        DI_stewardship_traits_effect = yes

        random_owned_story = {
            limit = {
                story_type = DI_misc_variables_story
            }
            save_scope_as = DI_story
        }

        if = {
            limit = {
                scope:DI_story = { has_variable = DI_pinned_char_var }
            }
            every_in_list = {
                variable = pinned_characters

                save_scope_as = target

                DI_spawn_court_character_effect = yes

                if = {
                    limit = {
                        exists = cp:councillor_steward
                    }
                    fire_councillor = cp:councillor_steward
                }
                assign_councillor_type = {
                    type = councillor_steward
                    target = scope:new_courtier
                }
            }
        }
        else = {
            save_scope_as = target

            DI_spawn_court_character_effect = yes

            if = {
                limit = {
                    exists = cp:councillor_steward
                }
                fire_councillor = cp:councillor_steward
            }
            assign_councillor_type = {
                type = councillor_steward
                target = scope:new_courtier
            }
        }

        if = {
            limit = {
                scope:root_target.var:DI_cs_random_traits ?= no
            }
            DI_no_random_traits_effect = yes
        }
        else_if = {
            limit = {
                scope:root_target.var:DI_cs_random_traits ?= yes
            }
            DI_random_traits_effect = yes
        }
    }
}

DI_intrigue_councillor = {
    scope = character

    effect = {
        save_scope_as = root_target
        DI_intrigue_traits_effect = yes

        random_owned_story = {
            limit = {
                story_type = DI_misc_variables_story
            }
            save_scope_as = DI_story
        }

        if = {
            limit = {
                scope:DI_story = { has_variable = DI_pinned_char_var }
            }
            every_in_list = {
                variable = pinned_characters

                save_scope_as = target

                DI_spawn_court_character_effect = yes

                if = {
                    limit = {
                        exists = cp:councillor_spymaster
                    }
                    fire_councillor = cp:councillor_spymaster
                }
                assign_councillor_type = {
                    type = councillor_spymaster
                    target = scope:new_courtier
                }
            }
        }
        else = {
            save_scope_as = target

            DI_spawn_court_character_effect = yes

            if = {
                limit = {
                    exists = cp:councillor_spymaster
                }
                fire_councillor = cp:councillor_spymaster
            }
            assign_councillor_type = {
                type = councillor_spymaster
                target = scope:new_courtier
            }
        }

        if = {
            limit = {
                scope:root_target.var:DI_cs_random_traits ?= no
            }
            DI_no_random_traits_effect = yes
        }
        else_if = {
            limit = {
                scope:root_target.var:DI_cs_random_traits ?= yes
            }
            DI_random_traits_effect = yes
        }
    }
}

DI_learning_councillor = {
    scope = character

    effect = {
        save_scope_as = root_target
        DI_learning_traits_effect = yes

        random_owned_story = {
            limit = {
                story_type = DI_misc_variables_story
            }
            save_scope_as = DI_story
        }

        if = {
            limit = {
                scope:DI_story = { has_variable = DI_pinned_char_var }
            }
            every_in_list = {
                variable = pinned_characters

                save_scope_as = target

                if = {
                    limit = {
                        NOR = {
                            faith = {
                                has_doctrine_parameter = clerical_appointment_head_of_faith
                                has_doctrine_parameter = clerical_appointment_fixed
                            }
                        }
                    }
                    DI_spawn_court_character_effect = yes

                    if = {
                        limit = {
                            exists = cp:councillor_court_chaplain
                        }
                        fire_councillor = cp:councillor_court_chaplain
                    }
                    assign_councillor_type = {
                        type = councillor_court_chaplain
                        target = scope:new_courtier
                    }
                }
                else_if = {
                    limit = {
                        exists = cp:councillor_court_chaplain
                        cp:councillor_court_chaplain = {
                            NOT = { has_character_flag = upgraded_chaplain }
                        }
                    }
                    cp:councillor_court_chaplain = {
                        custom_tooltip = improved_stats_effect_TT
                        hidden_effect = {
                            save_scope_as = new_courtier
                            DI_remove_health_conflict_effect = yes
                            DI_remove_education_traits_effect = yes
                            DI_clear_personality_traits_effect = yes
                            add_martial_skill = {5 10}
                            add_prowess_skill = {5 10}
                            add_diplomacy_skill = {8 12}
                            add_stewardship_skill = {8 12}
                            add_intrigue_skill = {5 10}
                            add_learning_skill = {8 12}
                            add_character_flag = upgraded_chaplain
                            add_trait = education_learning_4
                            add_trait = content
                            add_trait = trusting
                            add_trait = calm
                        }
                    }
                }
            }
        }
        else = {
            save_scope_as = target

            if = {
                limit = {
                    NOR = {
                        faith = {
                            has_doctrine_parameter = clerical_appointment_head_of_faith
                            has_doctrine_parameter = clerical_appointment_fixed
                        }
                    }
                }
                DI_spawn_court_character_effect = yes

                if = {
                    limit = {
                        exists = cp:councillor_court_chaplain
                    }
                    fire_councillor = cp:councillor_court_chaplain
                }
                assign_councillor_type = {
                    type = councillor_court_chaplain
                    target = scope:new_courtier
                }
            }
            else_if = {
                limit = {
                    exists = cp:councillor_court_chaplain
                    cp:councillor_court_chaplain = {
                        NOT = { has_character_flag = upgraded_chaplain }
                    }
                }
                cp:councillor_court_chaplain = {
                    custom_tooltip = improved_stats_effect_TT
                    hidden_effect = {
                        save_scope_as = new_courtier
                        DI_remove_health_conflict_effect = yes
                        DI_remove_education_traits_effect = yes
                        DI_clear_personality_traits_effect = yes
                        add_martial_skill = {5 10}
                        add_prowess_skill = {5 10}
                        add_diplomacy_skill = {8 12}
                        add_stewardship_skill = {8 12}
                        add_intrigue_skill = {5 10}
                        add_learning_skill = {8 12}
                        add_character_flag = upgraded_chaplain
                        add_trait = education_learning_4
                        add_trait = content
                        add_trait = trusting
                        add_trait = calm
                    }
                }
            }
        }

        if = {
            limit = {
                scope:root_target.var:DI_cs_random_traits ?= no
            }
            DI_no_random_traits_effect = yes
        }
        else_if = {
            limit = {
                scope:root_target.var:DI_cs_random_traits ?= yes
            }
            DI_random_traits_effect = yes
        }
    }
}

### COUNCILLOR MENU CONTINUED MISC ###
DI_knight = {
    scope = character

    effect = {
        save_scope_as = root_target
        random_owned_story = {
            limit = {
                story_type = DI_misc_variables_story
            }
            save_scope_as = DI_story
        }

        scope:DI_story = {
            save_scope_value_as = {
                name = DI_char_spawner_count_increment_value
                value = var:DI_char_spawner_count_increment_value
            }
        }
        DI_knight_traits_effect = yes

        if = {
            limit = {
                scope:DI_story = { has_variable = DI_pinned_char_var }
            }
            every_in_list = {
                variable = pinned_characters

                save_scope_as = target

                while = {
                    count = scope:DI_char_spawner_count_increment_value
                    DI_spawn_court_character_effect = yes

                    scope:new_courtier = {
                        add_trait_xp = { trait = lifestyle_blademaster value = 100 }
                        add_perk = stalwart_leader_perk
                    }
                }
            }
        }
        else = {
            save_scope_as = target

            while = {
                count = scope:DI_char_spawner_count_increment_value
                DI_spawn_court_character_effect = yes

                scope:new_courtier = {
                    add_trait_xp = { trait = lifestyle_blademaster value = 100 }
                    add_perk = stalwart_leader_perk
                }
            }
        }

        if = {
            limit = {
                scope:root_target.var:DI_cs_random_traits ?= no
            }
            DI_no_random_traits_effect = yes
        }
        else_if = {
            limit = {
                scope:root_target.var:DI_cs_random_traits ?= yes
            }
            DI_random_traits_effect = yes
        }
    }
}

DI_commander = {
    scope = character

    effect = {
        save_scope_as = root_target
        random_owned_story = {
            limit = {
                story_type = DI_misc_variables_story
            }
            save_scope_as = DI_story
        }

        scope:DI_story = {
            save_scope_value_as = {
                name = DI_char_spawner_count_increment_value
                value = var:DI_char_spawner_count_increment_value
            }
        }
        DI_knight_traits_effect = yes

        if = {
            limit = {
                scope:DI_story = { has_variable = DI_pinned_char_var }
            }
            every_in_list = {
                variable = pinned_characters

                save_scope_as = target

                while = {
                    count = scope:DI_char_spawner_count_increment_value
                    DI_spawn_court_character_effect = yes

                    scope:new_courtier = {
                        add_trait_xp = { trait = lifestyle_blademaster value = 100 }
                        add_perk = stalwart_leader_perk
                        DI_add_commander_traits_effect = yes
                    }
                }
            }
        }
        else = {
            save_scope_as = target

            while = {
                count = scope:DI_char_spawner_count_increment_value
                DI_spawn_court_character_effect = yes

                scope:new_courtier = {
                    add_trait_xp = { trait = lifestyle_blademaster value = 100 }
                    add_perk = stalwart_leader_perk
                    DI_add_commander_traits_effect = yes
                }
            }
        }

        if = {
            limit = {
                scope:root_target.var:DI_cs_random_traits ?= no
            }
            DI_no_random_traits_effect = yes
        }
        else_if = {
            limit = {
                scope:root_target.var:DI_cs_random_traits ?= yes
            }
            DI_random_traits_effect = yes
        }
    }
}

### FAMILY ###
# parent
DI_parent = {
    scope = character

    effect = {
        save_scope_as = root_target
        random_owned_story = {
            limit = {
                story_type = DI_misc_variables_story
            }
            save_scope_as = DI_story
        }

        scope:DI_story = {
            save_scope_value_as = {
                name = DI_char_spawner_count_increment_value
                value = var:DI_char_spawner_count_increment_value
            }
        }

        if = {
            limit = {
                scope:DI_story = { has_variable = DI_pinned_char_var }
            }
            every_in_list = {
                variable = pinned_characters

                save_scope_as = target

                DI_spawn_family_character_effect = yes

                if = {
                    limit = {
                        is_ruler = no
                    }
                    scope:target.liege = {
                        add_courtier = scope:new_courtier
                    }
                }
                if = {
                    limit = {
                        scope:DI_story = {
                            has_variable = DI_char_spawner_female
                        }
                    }

                    scope:target = {
                        every_sibling = {
                            set_mother = scope:new_courtier
                        }
                        set_mother = scope:new_courtier
                    }
                }
                else = {
                    scope:target = {
                        every_sibling = {
                            set_father = scope:new_courtier
                        }
                        set_father = scope:new_courtier
                    }
                }
            }
        }
        else = {
            save_scope_as = target

            DI_spawn_family_character_effect = yes

            if = {
                limit = {
                    is_ruler = no
                }
                scope:target.liege = {
                    add_courtier = scope:new_courtier
                }
            }
            if = {
                limit = {
                    scope:DI_story = {
                        has_variable = DI_char_spawner_female
                    }
                }

                scope:target = {
                    every_sibling = {
                        set_mother = scope:new_courtier
                    }
                    set_mother = scope:new_courtier
                }
            }
            else = {
                scope:target = {
                    every_sibling = {
                        set_father = scope:new_courtier
                    }
                    set_father = scope:new_courtier
                }
            }
        }
    }
}

DI_child = {
    scope = character

    effect = {
        save_scope_as = root_target
        random_owned_story = {
            limit = {
                story_type = DI_misc_variables_story
            }
            save_scope_as = DI_story
        }

        scope:DI_story = {
            save_scope_value_as = {
                name = DI_char_spawner_count_increment_value
                value = var:DI_char_spawner_count_increment_value
            }
        }

        if = {
            limit = {
                scope:DI_story = { has_variable = DI_pinned_char_var }
            }
            every_in_list = {
                variable = pinned_characters

                save_scope_as = target

                DI_set_parents_of_child_effect = yes

                while = {
                    count = scope:DI_char_spawner_count_increment_value
                    DI_spawn_family_character_effect = yes

                    if = {
                        limit = {
                            scope:DI_char_spawner_count_increment_value > 1
                        }
                        scope:new_courtier = {
                            add_trait = twin
                        }
                    }
                    if = {
                        limit = {
                            is_ruler = no
                        }
                        scope:target.liege = {
                            add_courtier = scope:new_courtier
                        }
                    }

                    scope:root_target.var:DI_cs_father ?= { DI_set_father_effect = yes }
                    scope:root_target.var:DI_cs_mother ?= { DI_set_mother_effect = yes }
                }
            }
        }
        else = {
            save_scope_as = target

            DI_set_parents_of_child_effect = yes

            while = {
                count = scope:DI_char_spawner_count_increment_value
                DI_spawn_family_character_effect = yes

                if = {
                    limit = {
                        scope:DI_char_spawner_count_increment_value > 1
                    }
                    scope:new_courtier = {
                        add_trait = twin
                    }
                }
                if = {
                    limit = {
                        is_ruler = no
                    }
                    scope:target.liege = {
                        add_courtier = scope:new_courtier
                    }
                }

                scope:root_target.var:DI_cs_father ?= { DI_set_father_effect = yes }
                scope:root_target.var:DI_cs_mother ?= { DI_set_mother_effect = yes }
            }
        }
    }
}

DI_sibling = {
    scope = character

    effect = {
        save_scope_as = root_target
        random_owned_story = {
            limit = {
                story_type = DI_misc_variables_story
            }
            save_scope_as = DI_story
        }

        scope:DI_story = {
            save_scope_value_as = {
                name = DI_char_spawner_count_increment_value
                value = var:DI_char_spawner_count_increment_value
            }
        }

        if = {
            limit = {
                scope:DI_story = { has_variable = DI_pinned_char_var }
            }
            every_in_list = {
                variable = pinned_characters

                save_scope_as = target

                DI_set_parents_of_sibling_effect = yes

                while = {
                    count = scope:DI_char_spawner_count_increment_value
                    DI_spawn_family_character_effect = yes

                    if = {
                        limit = {
                            scope:DI_char_spawner_count_increment_value > 1
                        }
                        scope:new_courtier = {
                            add_trait = twin
                        }
                    }
                    if = {
                        limit = {
                            is_ruler = no
                        }
                        scope:target.liege = {
                            add_courtier = scope:new_courtier
                        }
                    }
                    scope:root_target.var:DI_cs_father ?= { DI_set_father_effect = yes }
                    scope:root_target.var:DI_cs_mother ?= { DI_set_mother_effect = yes }
                }
            }
        }
        else = {
            save_scope_as = target

            DI_set_parents_of_sibling_effect = yes
            while = {
                count = scope:DI_char_spawner_count_increment_value
                DI_spawn_family_character_effect = yes

                if = {
                    limit = {
                        scope:DI_char_spawner_count_increment_value > 1
                    }
                    scope:new_courtier = {
                        add_trait = twin
                    }
                }
                if = {
                    limit = {
                        is_ruler = no
                    }
                    scope:target.liege = {
                        add_courtier = scope:new_courtier
                    }
                }
                scope:root_target.var:DI_cs_father ?= { DI_set_father_effect = yes }
                scope:root_target.var:DI_cs_mother ?= { DI_set_mother_effect = yes }
            }
        }
    }
}

# Spouse
DI_spouse = {
    scope = character

    effect = {
        save_scope_as = root_target
        random_owned_story = {
            limit = {
                story_type = DI_misc_variables_story
            }
            save_scope_as = DI_story
        }

        scope:DI_story = {
            set_variable = DI_cs_spouse
        }

        if = {
            limit = {
                scope:DI_story = { has_variable = DI_pinned_char_var }
            }
            every_in_list = {
                variable = pinned_characters

                save_scope_as = target

                DI_spawn_family_character_effect = yes

                if = {
                    limit = {
                        is_ruler = no
                    }
                    scope:target.liege = {
                        add_courtier = scope:new_courtier
                    }
                }
                DI_set_spouse_effect = yes
            }
        }
        else = {
            save_scope_as = target

            DI_spawn_family_character_effect = yes

            if = {
                limit = {
                    is_ruler = no
                }
                scope:target.liege = {
                    add_courtier = scope:new_courtier
                }
            }
            DI_set_spouse_effect = yes
        }

        scope:DI_story = {
            remove_variable = DI_cs_spouse
        }
    }
}


### COURT ###
DI_normal_courtier = {
    scope = character

    effect = {
        save_scope_as = root_target
        random_owned_story = {
            limit = {
                story_type = DI_misc_variables_story
            }
            save_scope_as = DI_story
        }

        scope:DI_story = {
            save_scope_value_as = {
                name = DI_char_spawner_count_increment_value
                value = var:DI_char_spawner_count_increment_value
            }
        }

        if = {
            limit = {
                scope:DI_story = { has_variable = DI_pinned_char_var }
            }
            every_in_list = {
                variable = pinned_characters

                save_scope_as = target

                while = {
                    count = scope:DI_char_spawner_count_increment_value
                    DI_spawn_court_character_effect = yes
                }
            }
        }
        else = {
            save_scope_as = target

            while =
            {
                count = scope:DI_char_spawner_count_increment_value
                DI_spawn_court_character_effect = yes
            }
        }
    }
}

DI_low_normal_courtier = {
    scope = character

    effect = {
        save_scope_as = root_target
        random_owned_story = {
            limit = {
                story_type = DI_misc_variables_story
            }
            save_scope_as = DI_story
        }

        scope:DI_story = {
            save_scope_value_as = {
                name = DI_char_spawner_count_increment_value
                value = var:DI_char_spawner_count_increment_value
            }
        }

        if = {
            limit = {
                scope:DI_story = { has_variable = DI_pinned_char_var }
            }
            every_in_list = {
                variable = pinned_characters

                save_scope_as = target

                while = {
                    count = scope:DI_char_spawner_count_increment_value
                    DI_spawn_low_court_character_effect = yes
                }
            }
        }
        else = {
            save_scope_as = target

            while = {
                count = scope:DI_char_spawner_count_increment_value
                DI_spawn_low_court_character_effect = yes
            }
        }
    }
}

# Court-position spawning is handled by DI_dynamic_court_position_spawner_sgui.txt.
