﻿### 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
            }
        }
    }
}

DI_court_physician = {
    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_physician_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_low_court_character_effect = yes

                    scope:new_courtier = {
                        add_trait_xp = { trait = lifestyle_physician value = 100 }
                        add_trait_xp = { trait = lifestyle_mystic value = 100 }
                    }

                    if = {
                        limit = {
                            NOT = { can_employ_court_position_type = court_physician_court_position }
                        }

                        random_court_position_holder = {
                            type = court_physician_court_position
                            save_scope_as = physician
                        }

                        revoke_court_position = {
                            court_position = court_physician_court_position
                        }
                    }
                    appoint_court_position = {
                        recipient = scope:new_courtier
                        court_position = court_physician_court_position
                    }
                }
            }
        }
        else = {
            save_scope_as = target

            while = {
                count = scope:DI_char_spawner_count_increment_value
                DI_spawn_low_court_character_effect = yes

                scope:new_courtier = {
                    add_trait_xp = { trait = lifestyle_physician value = 100 }
                    add_trait_xp = { trait = lifestyle_mystic value = 100 }
                }

                if = {
                    limit = {
                        NOT = { can_employ_court_position_type = court_physician_court_position }
                    }

                    random_court_position_holder = {
                        type = court_physician_court_position
                        save_scope_as = physician
                    }

                    revoke_court_position = {
                        court_position = court_physician_court_position
                    }
                }
                appoint_court_position = {
                    recipient = scope:new_courtier
                    court_position = court_physician_court_position
                }
            }
        }

        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_court_antiquarian = {
    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_antiquarian_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_low_court_character_effect = yes

                    scope:new_courtier = {
                        create_inspiration = book_inspiration
                    }

                    if = {
                        limit = {
                            NOT = { can_employ_court_position_type = antiquarian_court_position }
                        }

                        random_court_position_holder = {
                            type = antiquarian_court_position
                            save_scope_as = antiquarian
                        }

                        revoke_court_position = {
                            court_position = antiquarian_court_position
                        }
                    }
                    appoint_court_position = {
                        recipient = scope:new_courtier
                        court_position = antiquarian_court_position
                    }
                }
            }
        }
        else = {
            save_scope_as = target

            while = {
                count = scope:DI_char_spawner_count_increment_value
                DI_spawn_low_court_character_effect = yes

                scope:new_courtier = {
                    create_inspiration = book_inspiration
                }

                if = {
                    limit = {
                        NOT = { can_employ_court_position_type = antiquarian_court_position }
                    }

                    random_court_position_holder = {
                        type = antiquarian_court_position
                        save_scope_as = antiquarian
                    }

                    revoke_court_position = {
                        court_position = antiquarian_court_position
                    }
                }
                appoint_court_position = {
                    recipient = scope:new_courtier
                    court_position = antiquarian_court_position
                }
            }
        }

        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_travel_leader = {
    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_travel_leader_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_low_court_character_effect = yes

                    scope:new_courtier = {
                        DI_add_commander_traits_effect = yes
                        add_trait_xp = {
                            trait = lifestyle_traveler
                            track = travel
                            value = 100
                        }
                        add_trait_xp = {
                            trait = lifestyle_traveler
                            track = danger
                            value = 100
                        }
                        learn_language = language_arabic
                        learn_language = language_greek
                        learn_language = language_latin
                        learn_language = language_norse
                    }

                    if = {
                        limit = {
                            NOT = { can_employ_court_position_type = travel_leader_court_position }
                        }

                        random_court_position_holder = {
                            type = travel_leader_court_position
                            save_scope_as = travel_leader
                        }

                        revoke_court_position = {
                            court_position = travel_leader_court_position
                        }
                    }
                    appoint_court_position = {
                        recipient = scope:new_courtier
                        court_position = travel_leader_court_position
                    }
                }
            }
        }
        else = {
            save_scope_as = target

            while = {
                count = scope:DI_char_spawner_count_increment_value
                DI_spawn_low_court_character_effect = yes

                scope:new_courtier = {
                    DI_add_commander_traits_effect = yes
                    add_trait_xp = {
                        trait = lifestyle_traveler
                        track = travel
                        value = 100
                    }
                    add_trait_xp = {
                        trait = lifestyle_traveler
                        track = danger
                        value = 100
                    }
                    learn_language = language_arabic
                    learn_language = language_greek
                    learn_language = language_latin
                    learn_language = language_norse
                }

                if = {
                    limit = {
                        NOT = { can_employ_court_position_type = travel_leader_court_position }
                    }

                    random_court_position_holder = {
                        type = travel_leader_court_position
                        save_scope_as = travel_leader
                    }

                    revoke_court_position = {
                        court_position = travel_leader_court_position
                    }
                }
                appoint_court_position = {
                    recipient = scope:new_courtier
                    court_position = travel_leader_court_position
                }
            }
        }

        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_wet_nurse = {
    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_wet_nurse_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_low_court_character_effect = yes

                    scope:new_courtier = {}

                    if = {
                        limit = {
                            NOT = { can_employ_court_position_type = wet_nurse_court_position }
                        }

                        random_court_position_holder = {
                            type = wet_nurse_court_position
                            save_scope_as = wet_nurse
                        }

                        revoke_court_position = {
                            court_position = wet_nurse_court_position
                        }
                    }
                    appoint_court_position = {
                        recipient = scope:new_courtier
                        court_position = wet_nurse_court_position
                    }
                }
            }
        }
        else = {
            save_scope_as = target

            while = {
                count = scope:DI_char_spawner_count_increment_value
                DI_spawn_low_court_character_effect = yes

                scope:new_courtier = {}

                if = {
                    limit = {
                        NOT = { can_employ_court_position_type = wet_nurse_court_position }
                    }

                    random_court_position_holder = {
                        type = wet_nurse_court_position
                        save_scope_as = wet_nurse
                    }

                    revoke_court_position = {
                        court_position = wet_nurse_court_position
                    }
                }
                appoint_court_position = {
                    recipient = scope:new_courtier
                    court_position = wet_nurse_court_position
                }
            }
        }

        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_court_tutor = {
    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_court_tutor_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_low_court_character_effect = yes

                    scope:new_courtier = {}

                    if = {
                        limit = {
                            NOT = { can_employ_court_position_type = court_tutor_court_position }
                        }

                        random_court_position_holder = {
                            type = court_tutor_court_position
                            save_scope_as = court_tutor
                        }

                        revoke_court_position = {
                            court_position = court_tutor_court_position
                        }
                    }
                    appoint_court_position = {
                        recipient = scope:new_courtier
                        court_position = court_tutor_court_position
                    }
                }
            }
        }
        else = {
            save_scope_as = target

            while = {
                count = scope:DI_char_spawner_count_increment_value
                DI_spawn_low_court_character_effect = yes

                scope:new_courtier = {}

                if = {
                    limit = {
                        NOT = { can_employ_court_position_type = court_tutor_court_position }
                    }

                    random_court_position_holder = {
                        type = court_tutor_court_position
                        save_scope_as = court_tutor
                    }

                    revoke_court_position = {
                        court_position = court_tutor_court_position
                    }
                }
                appoint_court_position = {
                    recipient = scope:new_courtier
                    court_position = court_tutor_court_position
                }
            }
        }

        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_master_of_hunt = {
    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_master_of_hunt_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_low_court_character_effect = yes

                    scope:new_courtier = {
                        add_trait_xp = {
                            trait = lifestyle_hunter
                            track = hunter
                            value = 100
                        }
                    }

                    if = {
                        limit = {
                            NOT = { can_employ_court_position_type = master_of_hunt_court_position }
                        }

                        random_court_position_holder = {
                            type = master_of_hunt_court_position
                            save_scope_as = master_of_hunt
                        }

                        revoke_court_position = {
                            court_position = master_of_hunt_court_position
                        }
                    }
                    appoint_court_position = {
                        recipient = scope:new_courtier
                        court_position = master_of_hunt_court_position
                    }
                }
            }
        }
        else = {
            save_scope_as = target

            while = {
                count = scope:DI_char_spawner_count_increment_value
                DI_spawn_low_court_character_effect = yes

                scope:new_courtier = {
                    add_trait_xp = {
                        trait = lifestyle_hunter
                        track = hunter
                        value = 100
                    }
                }

                if = {
                    limit = {
                        NOT = { can_employ_court_position_type = master_of_hunt_court_position }
                    }

                    random_court_position_holder = {
                        type = master_of_hunt_court_position
                        save_scope_as = master_of_hunt
                    }

                    revoke_court_position = {
                        court_position = master_of_hunt_court_position
                    }
                }
                appoint_court_position = {
                    recipient = scope:new_courtier
                    court_position = master_of_hunt_court_position
                }
            }
        }

        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_seneschal = {
    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_seneschal_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_low_court_character_effect = yes

                    scope:new_courtier = {}

                    if = {
                        limit = {
                            NOT = { can_employ_court_position_type = seneschal_court_position }
                        }

                        random_court_position_holder = {
                            type = seneschal_court_position
                            save_scope_as = seneschal
                        }

                        revoke_court_position = {
                            court_position = seneschal_court_position
                        }
                    }
                    appoint_court_position = {
                        recipient = scope:new_courtier
                        court_position = seneschal_court_position
                    }
                }
            }
        }
        else = {
            save_scope_as = target

            while = {
                count = scope:DI_char_spawner_count_increment_value
                DI_spawn_low_court_character_effect = yes

                scope:new_courtier = {}

                if = {
                    limit = {
                        NOT = { can_employ_court_position_type = seneschal_court_position }
                    }

                    random_court_position_holder = {
                        type = seneschal_court_position
                        save_scope_as = seneschal
                    }

                    revoke_court_position = {
                        court_position = seneschal_court_position
                    }
                }
                appoint_court_position = {
                    recipient = scope:new_courtier
                    court_position = seneschal_court_position
                }
            }
        }

        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_court_jester = {
    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_court_jester_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_low_court_character_effect = yes

                    scope:new_courtier = {}

                    if = {
                        limit = {
                            NOT = { can_employ_court_position_type = court_jester_court_position }
                        }

                        random_court_position_holder = {
                            type = court_jester_court_position
                            save_scope_as = jester
                        }

                        revoke_court_position = {
                            court_position = court_jester_court_position
                        }
                    }
                    appoint_court_position = {
                        recipient = scope:new_courtier
                        court_position = court_jester_court_position
                    }
                }
            }
        }
        else = {
            save_scope_as = target

            while = {
                count = scope:DI_char_spawner_count_increment_value
                DI_spawn_low_court_character_effect = yes

                scope:new_courtier = {}

                if = {
                    limit = {
                        NOT = { can_employ_court_position_type = court_jester_court_position }
                    }

                    random_court_position_holder = {
                        type = court_jester_court_position
                        save_scope_as = jester
                    }

                    revoke_court_position = {
                        court_position = court_jester_court_position
                    }
                }
                appoint_court_position = {
                    recipient = scope:new_courtier
                    court_position = court_jester_court_position
                }
            }
        }

        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_bodyguard = {
    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_bodyguard_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_low_court_character_effect = yes

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

                    if = {
                        limit = {
                            NOT = { can_employ_court_position_type = bodyguard_court_position }
                        }

                        random_court_position_holder = {
                            type = bodyguard_court_position
                            save_scope_as = bodyguard
                        }

                        revoke_court_position = {
                            court_position = bodyguard_court_position
                        }
                    }
                    appoint_court_position = {
                        recipient = scope:new_courtier
                        court_position = bodyguard_court_position
                    }
                }
            }
        }
        else = {
            save_scope_as = target

            while = {
                count = scope:DI_char_spawner_count_increment_value
                DI_spawn_low_court_character_effect = yes

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

                if = {
                    limit = {
                        NOT = { can_employ_court_position_type = bodyguard_court_position }
                    }

                    random_court_position_holder = {
                        type = bodyguard_court_position
                        save_scope_as = bodyguard
                    }

                    revoke_court_position = {
                        court_position = bodyguard_court_position
                    }
                }
                appoint_court_position = {
                    recipient = scope:new_courtier
                    court_position = bodyguard_court_position
                }
            }
        }

        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_champion = {
    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_champion_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_low_court_character_effect = yes

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

                    if = {
                        limit = {
                            NOT = { can_employ_court_position_type = champion_court_position }
                        }

                        random_court_position_holder = {
                            type = champion_court_position
                            save_scope_as = champion
                        }

                        revoke_court_position = {
                            court_position = champion_court_position
                        }
                    }
                    appoint_court_position = {
                        recipient = scope:new_courtier
                        court_position = champion_court_position
                    }
                }
            }
        }
        else = {
            save_scope_as = target

            while = {
                count = scope:DI_char_spawner_count_increment_value
                DI_spawn_low_court_character_effect = yes

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

                if = {
                    limit = {
                        NOT = { can_employ_court_position_type = champion_court_position }
                    }

                    random_court_position_holder = {
                        type = champion_court_position
                        save_scope_as = champion
                    }

                    revoke_court_position = {
                        court_position = champion_court_position
                    }
                }
                appoint_court_position = {
                    recipient = scope:new_courtier
                    court_position = champion_court_position
                }
            }
        }

        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_food_taster = {
    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_food_taster_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_low_court_character_effect = yes

                    scope:new_courtier = {}

                    if = {
                        limit = {
                            NOT = { can_employ_court_position_type = food_taster_court_position }
                        }

                        random_court_position_holder = {
                            type = food_taster_court_position
                            save_scope_as = food_taster
                        }

                        revoke_court_position = {
                            court_position = food_taster_court_position
                        }
                    }
                    appoint_court_position = {
                        recipient = scope:new_courtier
                        court_position = food_taster_court_position
                    }
                }
            }
        }
        else = {
            save_scope_as = target

            while = {
                count = scope:DI_char_spawner_count_increment_value
                DI_spawn_low_court_character_effect = yes

                scope:new_courtier = {}

                if = {
                    limit = {
                        NOT = { can_employ_court_position_type = food_taster_court_position }
                    }

                    random_court_position_holder = {
                        type = food_taster_court_position
                        save_scope_as = food_taster
                    }

                    revoke_court_position = {
                        court_position = food_taster_court_position
                    }
                }
                appoint_court_position = {
                    recipient = scope:new_courtier
                    court_position = food_taster_court_position
                }
            }
        }

        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_master_of_horse = {
    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_master_of_horse_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_low_court_character_effect = yes

                    scope:new_courtier = {}

                    if = {
                        limit = {
                            NOT = { can_employ_court_position_type = master_of_horse_court_position }
                        }

                        random_court_position_holder = {
                            type = master_of_horse_court_position
                            save_scope_as = master_of_horse
                        }

                        revoke_court_position = {
                            court_position = master_of_horse_court_position
                        }
                    }
                    appoint_court_position = {
                        recipient = scope:new_courtier
                        court_position = master_of_horse_court_position
                    }
                }
            }
        }
        else = {
            save_scope_as = target

            while = {
                count = scope:DI_char_spawner_count_increment_value
                DI_spawn_low_court_character_effect = yes

                scope:new_courtier = {}

                if = {
                    limit = {
                        NOT = { can_employ_court_position_type = master_of_horse_court_position }
                    }

                    random_court_position_holder = {
                        type = master_of_horse_court_position
                        save_scope_as = master_of_horse
                    }

                    revoke_court_position = {
                        court_position = master_of_horse_court_position
                    }
                }
                appoint_court_position = {
                    recipient = scope:new_courtier
                    court_position = master_of_horse_court_position
                }
            }
        }

        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_royal_architect = {
    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_royal_architect_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_low_court_character_effect = yes

                    scope:new_courtier = {}

                    if = {
                        limit = {
                            NOT = { can_employ_court_position_type = royal_architect_court_position }
                        }

                        random_court_position_holder = {
                            type = royal_architect_court_position
                            save_scope_as = royal_architect
                        }

                        revoke_court_position = {
                            court_position = royal_architect_court_position
                        }
                    }
                    appoint_court_position = {
                        recipient = scope:new_courtier
                        court_position = royal_architect_court_position
                    }
                }
            }
        }
        else = {
            save_scope_as = target

            while = {
                count = scope:DI_char_spawner_count_increment_value
                DI_spawn_low_court_character_effect = yes

                scope:new_courtier = {}

                if = {
                    limit = {
                        NOT = { can_employ_court_position_type = royal_architect_court_position }
                    }

                    random_court_position_holder = {
                        type = royal_architect_court_position
                        save_scope_as = royal_architect
                    }

                    revoke_court_position = {
                        court_position = royal_architect_court_position
                    }
                }
                appoint_court_position = {
                    recipient = scope:new_courtier
                    court_position = royal_architect_court_position
                }
            }
        }

        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_high_almoner = {
    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_high_almoner_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_low_court_character_effect = yes

                    scope:new_courtier = {}

                    if = {
                        limit = {
                            NOT = { can_employ_court_position_type = high_almoner_court_position }
                        }

                        random_court_position_holder = {
                            type = high_almoner_court_position
                            save_scope_as = high_almoner
                        }

                        revoke_court_position = {
                            court_position = high_almoner_court_position
                        }
                    }
                    appoint_court_position = {
                        recipient = scope:new_courtier
                        court_position = high_almoner_court_position
                    }
                }
            }
        }
        else = {
            save_scope_as = target

            while = {
                count = scope:DI_char_spawner_count_increment_value
                DI_spawn_low_court_character_effect = yes

                scope:new_courtier = {}

                if = {
                    limit = {
                        NOT = { can_employ_court_position_type = high_almoner_court_position }
                    }

                    random_court_position_holder = {
                        type = high_almoner_court_position
                        save_scope_as = high_almoner
                    }

                    revoke_court_position = {
                        court_position = high_almoner_court_position
                    }
                }
                appoint_court_position = {
                    recipient = scope:new_courtier
                    court_position = high_almoner_court_position
                }
            }
        }

        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_cupbearer = {
    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_cupbearer_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_low_court_character_effect = yes

                    scope:new_courtier = {}

                    if = {
                        limit = {
                            NOT = { can_employ_court_position_type = cupbearer_court_position }
                        }

                        random_court_position_holder = {
                            type = cupbearer_court_position
                            save_scope_as = cupbearer
                        }

                        revoke_court_position = {
                            court_position = cupbearer_court_position
                        }
                    }
                    appoint_court_position = {
                        recipient = scope:new_courtier
                        court_position = cupbearer_court_position
                    }
                }
            }
        }
        else = {
            save_scope_as = target

            while = {
                count = scope:DI_char_spawner_count_increment_value
                DI_spawn_low_court_character_effect = yes

                scope:new_courtier = {}

                if = {
                    limit = {
                        NOT = { can_employ_court_position_type = cupbearer_court_position }
                    }

                    random_court_position_holder = {
                        type = cupbearer_court_position
                        save_scope_as = cupbearer
                    }

                    revoke_court_position = {
                        court_position = cupbearer_court_position
                    }
                }
                appoint_court_position = {
                    recipient = scope:new_courtier
                    court_position = cupbearer_court_position
                }
            }
        }

        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_court_poet = {
    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_court_poet_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_low_court_character_effect = yes

                    scope:new_courtier = {}

                    if = {
                        limit = {
                            NOT = { can_employ_court_position_type = court_poet_court_position }
                        }

                        random_court_position_holder = {
                            type = court_poet_court_position
                            save_scope_as = court_poet
                        }

                        revoke_court_position = {
                            court_position = court_poet_court_position
                        }
                    }
                    appoint_court_position = {
                        recipient = scope:new_courtier
                        court_position = court_poet_court_position
                    }
                }
            }
        }
        else = {
            save_scope_as = target

            while = {
                count = scope:DI_char_spawner_count_increment_value
                DI_spawn_low_court_character_effect = yes

                scope:new_courtier = {}

                if = {
                    limit = {
                        NOT = { can_employ_court_position_type = court_poet_court_position }
                    }

                    random_court_position_holder = {
                        type = court_poet_court_position
                        save_scope_as = court_poet
                    }

                    revoke_court_position = {
                        court_position = court_poet_court_position
                    }
                }
                appoint_court_position = {
                    recipient = scope:new_courtier
                    court_position = court_poet_court_position
                }
            }
        }

        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_court_musician = {
    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_court_musician_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_low_court_character_effect = yes

                    scope:new_courtier = {}

                    if = {
                        limit = {
                            NOT = { can_employ_court_position_type = court_musician_court_position }
                        }

                        random_court_position_holder = {
                            type = court_musician_court_position
                            save_scope_as = court_musician
                        }

                        revoke_court_position = {
                            court_position = court_musician_court_position
                        }
                    }
                    appoint_court_position = {
                        recipient = scope:new_courtier
                        court_position = court_musician_court_position
                    }
                }
            }
        }
        else = {
            save_scope_as = target

            while = {
                count = scope:DI_char_spawner_count_increment_value
                DI_spawn_low_court_character_effect = yes

                scope:new_courtier = {}

                if = {
                    limit = {
                        NOT = { can_employ_court_position_type = court_musician_court_position }
                    }

                    random_court_position_holder = {
                        type = court_musician_court_position
                        save_scope_as = court_musician
                    }

                    revoke_court_position = {
                        court_position = court_musician_court_position
                    }
                }
                appoint_court_position = {
                    recipient = scope:new_courtier
                    court_position = court_musician_court_position
                }
            }
        }

        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_court_artificer = {
    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_court_artificer_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_low_court_character_effect = yes

                    scope:new_courtier = {
                        create_inspiration = { type = weapon_inspiration gold = 0 }
                    }

                    if = {
                        limit = {
                            NOT = { can_employ_court_position_type = court_artificer_court_position }
                        }

                        random_court_position_holder = {
                            type = court_artificer_court_position
                            save_scope_as = court_artificer
                        }

                        revoke_court_position = {
                            court_position = court_artificer_court_position
                        }
                    }
                    appoint_court_position = {
                        recipient = scope:new_courtier
                        court_position = court_artificer_court_position
                    }
                }
            }
        }
        else = {
            save_scope_as = target

            while = {
                count = scope:DI_char_spawner_count_increment_value
                DI_spawn_low_court_character_effect = yes

                scope:new_courtier = {
                    create_inspiration = { type = weapon_inspiration gold = 0 }
                }

                if = {
                    limit = {
                        NOT = { can_employ_court_position_type = court_artificer_court_position }
                    }

                    random_court_position_holder = {
                        type = court_artificer_court_position
                        save_scope_as = court_artificer
                    }

                    revoke_court_position = {
                        court_position = court_artificer_court_position
                    }
                }
                appoint_court_position = {
                    recipient = scope:new_courtier
                    court_position = court_artificer_court_position
                }
            }
        }

        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_lady_in_waiting = {
    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_lady_in_waiting_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_low_court_character_effect = yes

                    scope:new_courtier = {}

                    if = {
                        limit = {
                            NOT = { can_employ_court_position_type = lady_in_waiting_court_position }
                        }

                        random_court_position_holder = {
                            type = lady_in_waiting_court_position
                            save_scope_as = lady_in_waiting
                        }

                        revoke_court_position = {
                            court_position = lady_in_waiting_court_position
                        }
                    }
                    appoint_court_position = {
                        recipient = scope:new_courtier
                        court_position = lady_in_waiting_court_position
                    }
                }
            }
        }
        else = {
            save_scope_as = target

            while = {
                count = scope:DI_char_spawner_count_increment_value
                DI_spawn_low_court_character_effect = yes

                scope:new_courtier = {}

                if = {
                    limit = {
                        NOT = { can_employ_court_position_type = lady_in_waiting_court_position }
                    }

                    random_court_position_holder = {
                        type = lady_in_waiting_court_position
                        save_scope_as = lady_in_waiting
                    }

                    revoke_court_position = {
                        court_position = lady_in_waiting_court_position
                    }
                }
                appoint_court_position = {
                    recipient = scope:new_courtier
                    court_position = lady_in_waiting_court_position
                }
            }
        }

        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_chief_eunuch = {
    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_chief_eunuch_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_low_court_character_effect = yes

                    scope:new_courtier = {}

                    if = {
                        limit = {
                            NOT = { can_employ_court_position_type = chief_eunuch_court_position }
                        }

                        random_court_position_holder = {
                            type = chief_eunuch_court_position
                            save_scope_as = chief_eunuch
                        }

                        revoke_court_position = {
                            court_position = chief_eunuch_court_position
                        }
                    }
                    appoint_court_position = {
                        recipient = scope:new_courtier
                        court_position = chief_eunuch_court_position
                    }
                }
            }
        }
        else = {
            save_scope_as = target

            while = {
                count = scope:DI_char_spawner_count_increment_value
                DI_spawn_low_court_character_effect = yes

                scope:new_courtier = {}

                if = {
                    limit = {
                        NOT = { can_employ_court_position_type = chief_eunuch_court_position }
                    }

                    random_court_position_holder = {
                        type = chief_eunuch_court_position
                        save_scope_as = chief_eunuch
                    }

                    revoke_court_position = {
                        court_position = chief_eunuch_court_position
                    }
                }
                appoint_court_position = {
                    recipient = scope:new_courtier
                    court_position = chief_eunuch_court_position
                }
            }
        }

        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_executioner = {
    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_executioner_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_low_court_character_effect = yes

                    scope:new_courtier = {}

                    if = {
                        limit = {
                            NOT = { can_employ_court_position_type = executioner_court_position }
                        }

                        random_court_position_holder = {
                            type = executioner_court_position
                            save_scope_as = executioner
                        }

                        revoke_court_position = {
                            court_position = executioner_court_position
                        }
                    }
                    appoint_court_position = {
                        recipient = scope:new_courtier
                        court_position = executioner_court_position
                    }
                }
            }
        }
        else = {
            save_scope_as = target

            while = {
                count = scope:DI_char_spawner_count_increment_value
                DI_spawn_low_court_character_effect = yes

                scope:new_courtier = {}

                if = {
                    limit = {
                        NOT = { can_employ_court_position_type = executioner_court_position }
                    }

                    random_court_position_holder = {
                        type = executioner_court_position
                        save_scope_as = executioner
                    }

                    revoke_court_position = {
                        court_position = executioner_court_position
                    }
                }
                appoint_court_position = {
                    recipient = scope:new_courtier
                    court_position = executioner_court_position
                }
            }
        }

        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_cultural_emissary = {
    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_cultural_emissary_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_low_court_character_effect = yes

                    scope:new_courtier = {
                        learn_language = language_arabic
                        learn_language = language_greek
                        learn_language = language_latin
                        learn_language = language_norse
                    }

                    if = {
                        limit = {
                            NOT = { can_employ_court_position_type = cultural_emissary_court_position }
                        }

                        random_court_position_holder = {
                            type = cultural_emissary_court_position
                            save_scope_as = cultural_emissary
                        }

                        revoke_court_position = {
                            court_position = cultural_emissary_court_position
                        }
                    }
                    appoint_court_position = {
                        recipient = scope:new_courtier
                        court_position = cultural_emissary_court_position
                    }
                }
            }
        }
        else = {
            save_scope_as = target

            while = {
                count = scope:DI_char_spawner_count_increment_value
                DI_spawn_low_court_character_effect = yes

                scope:new_courtier = {
                    learn_language = language_arabic
                    learn_language = language_greek
                    learn_language = language_latin
                    learn_language = language_norse
                }

                if = {
                    limit = {
                        NOT = { can_employ_court_position_type = cultural_emissary_court_position }
                    }

                    random_court_position_holder = {
                        type = cultural_emissary_court_position
                        save_scope_as = cultural_emissary
                    }

                    revoke_court_position = {
                        court_position = cultural_emissary_court_position
                    }
                }
                appoint_court_position = {
                    recipient = scope:new_courtier
                    court_position = cultural_emissary_court_position
                }
            }
        }

        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_keeper_of_swans = {
    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_keeper_of_swans_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_low_court_character_effect = yes

                    scope:new_courtier = {
                        add_trait_xp = {
                            trait = lifestyle_hunter
                            track = hunter
                            value = 100
                        }
                    }

                    if = {
                        limit = {
                            NOT = { can_employ_court_position_type = keeper_of_swans_court_position }
                        }

                        random_court_position_holder = {
                            type = keeper_of_swans_court_position
                            save_scope_as = keeper_of_swans
                        }

                        revoke_court_position = {
                            court_position = keeper_of_swans_court_position
                        }
                    }
                    appoint_court_position = {
                        recipient = scope:new_courtier
                        court_position = keeper_of_swans_court_position
                    }
                }
            }
        }
        else = {
            save_scope_as = target

            while = {
                count = scope:DI_char_spawner_count_increment_value
                DI_spawn_low_court_character_effect = yes

                scope:new_courtier = {
                    add_trait_xp = {
                        trait = lifestyle_hunter
                        track = hunter
                        value = 100
                    }
                }

                if = {
                    limit = {
                        NOT = { can_employ_court_position_type = keeper_of_swans_court_position }
                    }

                    random_court_position_holder = {
                        type = keeper_of_swans_court_position
                        save_scope_as = keeper_of_swans
                    }

                    revoke_court_position = {
                        court_position = keeper_of_swans_court_position
                    }
                }
                appoint_court_position = {
                    recipient = scope:new_courtier
                    court_position = keeper_of_swans_court_position
                }
            }
        }

        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_chief_qadi = {
    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_chief_qadi_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_low_court_character_effect = yes

                    scope:new_courtier = {}

                    if = {
                        limit = {
                            NOT = { can_employ_court_position_type = chief_qadi_court_position }
                        }

                        random_court_position_holder = {
                            type = chief_qadi_court_position
                            save_scope_as = chief_qadi
                        }

                        revoke_court_position = {
                            court_position = chief_qadi_court_position
                        }
                    }
                    appoint_court_position = {
                        recipient = scope:new_courtier
                        court_position = chief_qadi_court_position
                    }
                }
            }
        }
        else = {
            save_scope_as = target

            while = {
                count = scope:DI_char_spawner_count_increment_value
                DI_spawn_low_court_character_effect = yes

                scope:new_courtier = {}

                if = {
                    limit = {
                        NOT = { can_employ_court_position_type = chief_qadi_court_position }
                    }

                    random_court_position_holder = {
                        type = chief_qadi_court_position
                        save_scope_as = chief_qadi
                    }

                    revoke_court_position = {
                        court_position = chief_qadi_court_position
                    }
                }
                appoint_court_position = {
                    recipient = scope:new_courtier
                    court_position = chief_qadi_court_position
                }
            }
        }

        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_garuda = {
    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_garuda_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_low_court_character_effect = yes

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

                    if = {
                        limit = {
                            NOT = { can_employ_court_position_type = garuda_court_position }
                        }

                        random_court_position_holder = {
                            type = garuda_court_position
                            save_scope_as = garuda
                        }

                        revoke_court_position = {
                            court_position = garuda_court_position
                        }
                    }
                    appoint_court_position = {
                        recipient = scope:new_courtier
                        court_position = garuda_court_position
                    }
                }
            }
        }
        else = {
            save_scope_as = target

            while = {
                count = scope:DI_char_spawner_count_increment_value
                DI_spawn_low_court_character_effect = yes

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

                if = {
                    limit = {
                        NOT = { can_employ_court_position_type = garuda_court_position }
                    }

                    random_court_position_holder = {
                        type = garuda_court_position
                        save_scope_as = garuda
                    }

                    revoke_court_position = {
                        court_position = garuda_court_position
                    }
                }
                appoint_court_position = {
                    recipient = scope:new_courtier
                    court_position = garuda_court_position
                }
            }
        }

        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_court_gardener = {
    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_court_gardener_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_low_court_character_effect = yes

                    scope:new_courtier = {}

                    if = {
                        limit = {
                            NOT = { can_employ_court_position_type = court_gardener_court_position }
                        }

                        random_court_position_holder = {
                            type = court_gardener_court_position
                            save_scope_as = court_gardener
                        }

                        revoke_court_position = {
                            court_position = court_gardener_court_position
                        }
                    }
                    appoint_court_position = {
                        recipient = scope:new_courtier
                        court_position = court_gardener_court_position
                    }
                }
            }
        }
        else = {
            save_scope_as = target

            while = {
                count = scope:DI_char_spawner_count_increment_value
                DI_spawn_low_court_character_effect = yes

                scope:new_courtier = {}

                if = {
                    limit = {
                        NOT = { can_employ_court_position_type = court_gardener_court_position }
                    }

                    random_court_position_holder = {
                        type = court_gardener_court_position
                        save_scope_as = court_gardener
                    }

                    revoke_court_position = {
                        court_position = court_gardener_court_position
                    }
                }
                appoint_court_position = {
                    recipient = scope:new_courtier
                    court_position = court_gardener_court_position
                }
            }
        }

        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_cave_hermit = {
    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_cave_hermit_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_low_court_character_effect = yes

                    scope:new_courtier = {
                        add_trait_xp = { trait = lifestyle_mystic value = 50 }
                    }

                    if = {
                        limit = {
                            NOT = { can_employ_court_position_type = cave_hermit_court_position }
                        }

                        random_court_position_holder = {
                            type = cave_hermit_court_position
                            save_scope_as = cave_hermit
                        }

                        revoke_court_position = {
                            court_position = cave_hermit_court_position
                        }
                    }
                    appoint_court_position = {
                        recipient = scope:new_courtier
                        court_position = cave_hermit_court_position
                    }
                }
            }
        }
        else = {
            save_scope_as = target

            while = {
                count = scope:DI_char_spawner_count_increment_value
                DI_spawn_low_court_character_effect = yes

                scope:new_courtier = {
                    add_trait_xp = { trait = lifestyle_mystic value = 50 }
                }

                if = {
                    limit = {
                        NOT = { can_employ_court_position_type = cave_hermit_court_position }
                    }

                    random_court_position_holder = {
                        type = cave_hermit_court_position
                        save_scope_as = cave_hermit
                    }

                    revoke_court_position = {
                        court_position = cave_hermit_court_position
                    }
                }
                appoint_court_position = {
                    recipient = scope:new_courtier
                    court_position = cave_hermit_court_position
                }
            }
        }

        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_master_assassin = {
    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_master_assassin_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_low_court_character_effect = yes

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

                    if = {
                        limit = {
                            NOT = { can_employ_court_position_type = master_assassin_court_position }
                        }

                        random_court_position_holder = {
                            type = master_assassin_court_position
                            save_scope_as = master_assassin
                        }

                        revoke_court_position = {
                            court_position = master_assassin_court_position
                        }
                    }
                    appoint_court_position = {
                        recipient = scope:new_courtier
                        court_position = master_assassin_court_position
                    }
                }
            }
        }
        else = {
            save_scope_as = target

            while = {
                count = scope:DI_char_spawner_count_increment_value
                DI_spawn_low_court_character_effect = yes

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

                if = {
                    limit = {
                        NOT = { can_employ_court_position_type = master_assassin_court_position }
                    }

                    random_court_position_holder = {
                        type = master_assassin_court_position
                        save_scope_as = master_assassin
                    }

                    revoke_court_position = {
                        court_position = master_assassin_court_position
                    }
                }
                appoint_court_position = {
                    recipient = scope:new_courtier
                    court_position = master_assassin_court_position
                }
            }
        }

        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_court_scholar = {
    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_court_scholar_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_low_court_character_effect = yes

                    scope:new_courtier = {
                        add_trait_xp = { trait = lifestyle_physician value = 100 }
                    }

                    if = {
                        limit = {
                            NOT = { can_employ_court_position_type = court_scholar_court_position }
                        }

                        random_court_position_holder = {
                            type = court_scholar_court_position
                            save_scope_as = court_scholar
                        }

                        revoke_court_position = {
                            court_position = court_scholar_court_position
                        }
                    }
                    appoint_court_position = {
                        recipient = scope:new_courtier
                        court_position = court_scholar_court_position
                    }
                }
            }
        }
        else = {
            save_scope_as = target

            while = {
                count = scope:DI_char_spawner_count_increment_value
                DI_spawn_low_court_character_effect = yes

                scope:new_courtier = {
                    add_trait_xp = { trait = lifestyle_physician value = 100 }
                }

                if = {
                    limit = {
                        NOT = { can_employ_court_position_type = court_scholar_court_position }
                    }

                    random_court_position_holder = {
                        type = court_scholar_court_position
                        save_scope_as = court_scholar
                    }

                    revoke_court_position = {
                        court_position = court_scholar_court_position
                    }
                }
                appoint_court_position = {
                    recipient = scope:new_courtier
                    court_position = court_scholar_court_position
                }
            }
        }

        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_chronicler = {
    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_chronicler_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_low_court_character_effect = yes

                    scope:new_courtier = {}

                    if = {
                        limit = {
                            NOT = { can_employ_court_position_type = chronicler_court_position }
                        }

                        random_court_position_holder = {
                            type = chronicler_court_position
                            save_scope_as = chronicler
                        }

                        revoke_court_position = {
                            court_position = chronicler_court_position
                        }
                    }
                    appoint_court_position = {
                        recipient = scope:new_courtier
                        court_position = chronicler_court_position
                    }
                }
            }
        }
        else = {
            save_scope_as = target

            while = {
                count = scope:DI_char_spawner_count_increment_value
                DI_spawn_low_court_character_effect = yes

                scope:new_courtier = {}

                if = {
                    limit = {
                        NOT = { can_employ_court_position_type = chronicler_court_position }
                    }

                    random_court_position_holder = {
                        type = chronicler_court_position
                        save_scope_as = chronicler
                    }

                    revoke_court_position = {
                        court_position = chronicler_court_position
                    }
                }
                appoint_court_position = {
                    recipient = scope:new_courtier
                    court_position = chronicler_court_position
                }
            }
        }

        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_charioteer = {
    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_charioteer_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_low_court_character_effect = yes

                    scope:new_courtier = {
                        if = {
                            limit = {
                                has_trait = charioteer_blue
                            }
                            add_trait_xp = { trait = charioteer_blue value = 100 }
                        }
                        else_if = {
                            limit = {
                                has_trait = charioteer_green
                            }
                            add_trait_xp = { trait = charioteer_green value = 100 }
                        }
                        else_if = {
                            limit = {
                                has_trait = charioteer_red
                            }
                            add_trait_xp = { trait = charioteer_red value = 100 }
                        }
                        else_if = {
                            limit = {
                                has_trait = charioteer_white
                            }
                            add_trait_xp = { trait = charioteer_white value = 100 }
                        }
                    }

                    if = {
                        limit = {
                            NOT = { can_employ_court_position_type = charioteer_court_position }
                        }

                        random_court_position_holder = {
                            type = charioteer_court_position
                            save_scope_as = charioteer
                        }

                        revoke_court_position = {
                            court_position = charioteer_court_position
                        }
                    }
                    appoint_court_position = {
                        recipient = scope:new_courtier
                        court_position = charioteer_court_position
                    }
                }
            }
        }
        else = {
            save_scope_as = target

            while = {
                count = scope:DI_char_spawner_count_increment_value
                DI_spawn_low_court_character_effect = yes

                scope:new_courtier = {
                    if = {
                        limit = {
                            has_trait = charioteer_blue
                        }
                        add_trait_xp = { trait = charioteer_blue value = 100 }
                    }
                    else_if = {
                        limit = {
                            has_trait = charioteer_green
                        }
                        add_trait_xp = { trait = charioteer_green value = 100 }
                    }
                    else_if = {
                        limit = {
                            has_trait = charioteer_red
                        }
                        add_trait_xp = { trait = charioteer_red value = 100 }
                    }
                    else_if = {
                        limit = {
                            has_trait = charioteer_white
                        }
                        add_trait_xp = { trait = charioteer_white value = 100 }
                    }
                }

                if = {
                    limit = {
                        NOT = { can_employ_court_position_type = charioteer_court_position }
                    }

                    random_court_position_holder = {
                        type = charioteer_court_position
                        save_scope_as = charioteer
                    }

                    revoke_court_position = {
                        court_position = charioteer_court_position
                    }
                }
                appoint_court_position = {
                    recipient = scope:new_courtier
                    court_position = charioteer_court_position
                }
            }
        }

        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_bookmaker = {
    scope = character # Player 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_bookmaker_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_low_court_character_effect = yes

                    scope:new_courtier = {}

                    if = {
                        limit = {
                            NOT = { can_employ_court_position_type = bookmaker_court_position }
                        }

                        random_court_position_holder = {
                            type = bookmaker_court_position
                            save_scope_as = bookmaker
                        }

                        revoke_court_position = {
                            court_position = bookmaker_court_position
                        }
                    }
                    appoint_court_position = {
                        recipient = scope:new_courtier
                        court_position = bookmaker_court_position
                    }
                }
            }
        }
        else = {
            save_scope_as = target

            while = {
                count = scope:DI_char_spawner_count_increment_value
                DI_spawn_low_court_character_effect = yes

                scope:new_courtier = {}

                if = {
                    limit = {
                        NOT = { can_employ_court_position_type = bookmaker_court_position }
                    }

                    random_court_position_holder = {
                        type = bookmaker_court_position
                        save_scope_as = bookmaker
                    }

                    revoke_court_position = {
                        court_position = bookmaker_court_position
                    }
                }
                appoint_court_position = {
                    recipient = scope:new_courtier
                    court_position = bookmaker_court_position
                }
            }
        }

        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_akolouthos = {
    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_akolouthos_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_low_court_character_effect = yes

                    scope:new_courtier = {}

                    if = {
                        limit = {
                            NOT = { can_employ_court_position_type = akolouthos_court_position }
                        }

                        random_court_position_holder = {
                            type = akolouthos_court_position
                            save_scope_as = akolouthos
                        }

                        revoke_court_position = {
                            court_position = akolouthos_court_position
                        }
                    }
                    appoint_court_position = {
                        recipient = scope:new_courtier
                        court_position = akolouthos_court_position
                    }
                }
            }
        }
        else = {
            save_scope_as = target

            while = {
                count = scope:DI_char_spawner_count_increment_value
                DI_spawn_low_court_character_effect = yes

                scope:new_courtier = {}

                if = {
                    limit = {
                        NOT = { can_employ_court_position_type = akolouthos_court_position }
                    }

                    random_court_position_holder = {
                        type = akolouthos_court_position
                        save_scope_as = akolouthos
                    }

                    revoke_court_position = {
                        court_position = akolouthos_court_position
                    }
                }
                appoint_court_position = {
                    recipient = scope:new_courtier
                    court_position = akolouthos_court_position
                }
            }
        }

        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
        }
    }
}