﻿### Toggle Title Selected / Deselected ###
DI_selected_title_toggle = {
    scope = character # Player Character

    saved_scopes = {
        title # Title to pin
    }

    effect = {
        save_scope_as = player

        if = {
            limit = {
                is_target_in_variable_list = {
                    name = DI_selected_titles
                    target = scope:title
                }
            }
            scope:player = {
                remove_list_variable = {
                    name = DI_selected_titles
                    target = scope:title
                }

                change_variable = { name = DI_selected_count subtract = 1 }
            }
        }
        else = {
            scope:player = {
                add_to_variable_list = {
                    name = DI_selected_titles
                    target = scope:title
                }

                change_variable = { name = DI_selected_count add = 1 }
            }
        }
    }
}

### Unpin Title ###
DI_unpin_title = {
    scope = character # Player Character

    saved_scopes = {
        title # Title to unpin
    }

    effect = {
        save_scope_as = player

        remove_list_variable = {
            name = DI_pinned_titles
            target = scope:title
        }

        change_variable = { name = DI_pinned_count subtract = 1 }

        if = {
            limit = {
                is_target_in_variable_list = {
                    name = DI_selected_titles
                    target = scope:title
                }
            }

            change_variable = { name = DI_selected_count subtract = 1 }
        }

        remove_list_variable = {
            name = DI_selected_titles
            target = scope:title
        }
    }
}

### Check if title is selected ###
DI_title_selected = {
    scope = character # Player Character

    saved_scopes = {
        title # Title to check
    }

    is_valid = {
        is_target_in_variable_list = {
            name = DI_selected_titles
            target = scope:title
        }
    }
}

### Check if hiding titles ###
DI_titles_hide_toggle_on = {
    scope = character # Player Character

    is_valid = {
        has_variable = DI_titles_hide
    }
}

### Check if selected titles exists ###
DI_selected_titles_exists = {
    scope = character # Player Character

    is_valid = {
        has_variable_list = DI_selected_titles
    }
}

### Claim Titles ###
DI_claim_titles_sgui = {
    scope = character # Player Character

    effect = {
        save_scope_as = player

        every_in_list = {
            variable = DI_selected_titles
            save_scope_as = title

            scope:player = {
                add_pressed_claim = scope:title
            }
        }
    }
}

### Take Titles ###
DI_take_titles_sgui = {
    scope = character # Player Character

    effect = {
        save_scope_as = player

        every_in_list = {
            variable = DI_selected_titles
            save_scope_as = title

            scope:player = {
                DI_get_title_effect = { TITLE = scope:title }
            }
        }
    }
}

### Destroy Titles ###
DI_destroy_titles_sgui = {
    scope = character # Player Character

    effect = {
        save_scope_as = player

        every_in_list = {
            variable = DI_selected_titles
            save_scope_as = title

            scope:player = {
                destroy_title = scope:title
            }
        }
    }
}

### Create Dejure Liege Titles ###
DI_create_dejure_liege_title_sgui = {
    scope = character # Player Character

    effect = {
        save_scope_as = player

        scope:player = {
            every_in_list = {
                variable = DI_selected_titles
                save_scope_as = title

                limit = {
                    this.de_jure_liege = {
                        is_title_created = no
                    }
                }

                scope:player = {
                    DI_get_title_effect = { TITLE = scope:title.de_jure_liege }
                }
            }
        }
    }
}

### Create direct de jure vassals of each selected title ###
DI_create_direct_dejure_vassals_sgui = {
    scope = character  # Player

    effect = {
        save_scope_as = player

        scope:player = {
            every_in_list = {
                variable = DI_selected_titles
                save_scope_as = title

                every_in_de_jure_hierarchy = {
                    save_scope_as = dj_title
                    limit = {
                        de_jure_liege = { this = scope:title }  # direct child only
                        is_title_created = no
                        tier >= county
                    }
                    scope:player = { DI_get_title_effect = { TITLE = scope:dj_title } }
                }
            }
        }
    }
}

### Recursively create all dejure titles attached to selected title ###
DI_create_dejure_tree_sgui = {
    scope = character  # Player

    effect = {
        save_scope_as = player

        scope:player = {
            every_in_list = {
                variable = DI_selected_titles
                save_scope_as = title

                every_this_title_or_de_jure_above = {
                    save_scope_as = up_title

                    if = {
                        limit = {
                            is_title_created = no
                        }

                        scope:player = { DI_get_title_effect = { TITLE = scope:up_title } }
                    }

                    every_in_de_jure_hierarchy = {
                        save_scope_as = down_title
                        limit = {
                            NOT = { this = scope:up_title }
                            is_title_created = no
                            tier >= tier_county
                        }

                        scope:player = { DI_get_title_effect = { TITLE = scope:down_title } }
                    }
                }
            }
        }
    }
}


### Load Title Manager Dropdown ###
DI_load_title_manager_dropdown = {
    scope = character # Player Character

    is_valid = {
        OR = {
            NOT = { has_global_variable_list = DI_title_manager_filter_choices }
            NOT = { has_global_variable = DI_title_filters_3f8a92c4e1b7_loaded }
        }
    }

    effect = {
        save_scope_as = player

        clear_global_variable_list = DI_title_manager_filter_choices
        set_global_variable = DI_title_filters_3f8a92c4e1b7_loaded

        add_to_global_variable_list = { name = DI_title_manager_filter_choices target = flag:DI_title_filter_sort_rank }
        add_to_global_variable_list = { name = DI_title_manager_filter_choices target = flag:DI_title_filter_all }
        add_to_global_variable_list = { name = DI_title_manager_filter_choices target = flag:DI_title_filter_hide }
        add_to_global_variable_list = { name = DI_title_manager_filter_choices target = flag:DI_title_filter_hegemony }
        add_to_global_variable_list = { name = DI_title_manager_filter_choices target = flag:DI_title_filter_empire }
        add_to_global_variable_list = { name = DI_title_manager_filter_choices target = flag:DI_title_filter_kingdom }
        add_to_global_variable_list = { name = DI_title_manager_filter_choices target = flag:DI_title_filter_duchy }
        add_to_global_variable_list = { name = DI_title_manager_filter_choices target = flag:DI_title_filter_county }
        add_to_global_variable_list = { name = DI_title_manager_filter_choices target = flag:DI_title_filter_pinned_characters }
        add_to_global_variable_list = { name = DI_title_manager_filter_choices target = flag:DI_title_filter_unpin_selected }
        add_to_global_variable_list = { name = DI_title_manager_filter_choices target = flag:DI_title_filter_clear_pinned }
    }
}

### Apply Title Manager Filter Choice ###
DI_apply_title_manager_filter_choice = {
    scope = character # Player Character

    saved_scopes = {
        filter_choice # Filter choice to apply
    }

    effect = {
        save_scope_as = player

        if = {
            limit = { scope:filter_choice = flag:DI_title_filter_sort_rank }

            # Ordered iterators default to one result; sort the entire pinned list.
            DI_recount_title_counters = yes
            clear_variable_list = DI_sorted_titles

            if = {
                limit = {
                    NOT = { has_variable = DI_sort_descending }
                    NOT = { has_variable = DI_sort_ascending }
                }

                set_variable = DI_sort_descending
            }

            if = {
                limit = { has_variable = DI_sort_descending }

                ordered_in_list = {
                    variable = DI_pinned_titles
                    max = scope:player.var:DI_pinned_count
                    check_range_bounds = no
                    order_by = tier
                    save_scope_as = title
                    scope:player = {
                        add_to_variable_list = {
                            name = DI_sorted_titles
                            target = scope:title
                        }
                    }
                }

                remove_variable = DI_sort_descending
                set_variable = DI_sort_ascending
            }
            else_if = {
                limit = { has_variable = DI_sort_ascending }

                ordered_in_list = {
                    variable = DI_pinned_titles
                    max = scope:player.var:DI_pinned_count
                    check_range_bounds = no
                    order_by = {
                        value = tier
                        multiply = -1
                    }
                    save_scope_as = title
                    scope:player = {
                        add_to_variable_list = {
                            name = DI_sorted_titles
                            target = scope:title
                        }
                    }
                }

                remove_variable = DI_sort_ascending
                set_variable = DI_sort_descending
            }

            clear_variable_list = DI_pinned_titles
            every_in_list = {
                variable = DI_sorted_titles
                save_scope_as = title

                scope:player = {
                    add_to_variable_list = {
                        name = DI_pinned_titles
                        target = scope:title
                    }
                }
            }
            clear_variable_list = DI_sorted_titles
        }
        else_if = {
            limit = { scope:filter_choice = flag:DI_title_filter_all }

            if = {
                limit = { has_variable_list = DI_selected_titles }

                clear_variable_list = DI_selected_titles
            }
            else = {
                every_in_list = {
                    variable = DI_pinned_titles
                    save_scope_as = title

                    scope:player = {
                        add_to_variable_list = {
                            name = DI_selected_titles
                            target = scope:title
                        }
                    }
                }
            }
        }
        else_if = {
            limit = { scope:filter_choice = flag:DI_title_filter_hide }

            if = {
                limit = { has_variable = DI_titles_hide }

                remove_variable = DI_titles_hide
            }
            else = {
                set_variable = DI_titles_hide
            }
        }
        else_if = {
            limit = { scope:filter_choice = flag:DI_title_filter_hegemony }

            clear_variable_list = DI_selected_titles

            # Select all hegemony titles
            every_in_list = {
                variable = DI_pinned_titles
                limit = {
                    tier = tier_hegemony
                }
                save_scope_as = title

                scope:player = {
                    add_to_variable_list = {
                        name = DI_selected_titles
                        target = scope:title
                    }
                }
            }
        }
        else_if = {
            limit = { scope:filter_choice = flag:DI_title_filter_empire }

            clear_variable_list = DI_selected_titles

            # Select all empire titles
            every_in_list = {
                variable = DI_pinned_titles
                limit = {
                    tier = tier_empire
                }
                save_scope_as = title

                scope:player = {
                    add_to_variable_list = {
                        name = DI_selected_titles
                        target = scope:title
                    }
                }
            }
        }
        else_if = {
            limit = { scope:filter_choice = flag:DI_title_filter_kingdom }

            clear_variable_list = DI_selected_titles

            # Select all kingdom titles
            every_in_list = {
                variable = DI_pinned_titles
                limit = {
                    tier = tier_kingdom
                }
                save_scope_as = title

                scope:player = {
                    add_to_variable_list = {
                        name = DI_selected_titles
                        target = scope:title
                    }
                }
            }
        }
        else_if = {
            limit = { scope:filter_choice = flag:DI_title_filter_duchy }

            clear_variable_list = DI_selected_titles

            # Select all duchy titles
            every_in_list = {
                variable = DI_pinned_titles
                limit = {
                    tier = tier_duchy
                }
                save_scope_as = title

                scope:player = {
                    add_to_variable_list = {
                        name = DI_selected_titles
                        target = scope:title
                    }
                }
            }
        }
        else_if = {
            limit = { scope:filter_choice = flag:DI_title_filter_county }

            clear_variable_list = DI_selected_titles

            # Select all county titles
            every_in_list = {
                variable = DI_pinned_titles
                limit = {
                    tier = tier_county
                }
                save_scope_as = title

                scope:player = {
                    add_to_variable_list = {
                        name = DI_selected_titles
                        target = scope:title
                    }
                }
            }
        }
        else_if = {
            limit = { scope:filter_choice = flag:DI_title_filter_pinned_characters }

            clear_variable_list = DI_selected_titles

            # Select all titles held by pinned characters
            every_living_character = {
                limit = { has_character_flag = pinned_for_edit }

                every_held_title = {
                    limit = {
                        tier > tier_barony
                    }
                    save_scope_as = title

                    scope:player = {
                        add_to_variable_list = {
                            name = DI_pinned_titles
                            target = scope:title
                        }
                        add_to_variable_list = {
                            name = DI_selected_titles
                            target = scope:title
                        }
                    }
                }
            }
        }
        else_if = {
            limit = { scope:filter_choice = flag:DI_title_filter_unpin_selected }

            every_in_list = {
                variable = DI_selected_titles
                save_scope_as = title

                scope:player = {
                    remove_list_variable = {
                        name = DI_pinned_titles
                        target = scope:title
                    }
                    remove_list_variable = {
                        name = DI_selected_titles
                        target = scope:title
                    }
                }
            }
        }
        else_if = {
            limit = { scope:filter_choice = flag:DI_title_filter_clear_pinned }

            clear_variable_list = DI_pinned_titles
            clear_variable_list = DI_selected_titles
        }

        DI_recount_title_counters = yes
    }
}

### Load Province Dropdown ###
### Apply Filter Choice ###
### Deselect Province ###
DI_toggle_action_province = {
    scope = character # Player Character

    saved_scopes = {
        province # Province to deselect
    }

    effect = {
        save_scope_as = player
        # Else, toggle the province in DI_action_provinces
        if = {
            limit = {
                is_target_in_variable_list = {
                    name = DI_action_provinces
                    target = scope:province
                }
            }
            scope:player = {
                remove_list_variable = {
                    name = DI_action_provinces
                    target = scope:province
                }
                if = {
                    limit = {
                        has_variable = DI_provinces_hide
                    }
                    remove_list_variable = {
                        name = DI_shown_provinces
                        target = scope:province
                    }
                }
            }
        }
        else = {
            scope:player = {
                add_to_variable_list = {
                    name = DI_action_provinces
                    target = scope:province
                }
                if = {
                    limit = {
                        has_variable = DI_provinces_hide
                    }
                    remove_list_variable = {
                        name = DI_shown_provinces
                        target = scope:province
                    }
                }
            }
        }

        if = {
            limit = {
                has_variable = DI_provinces_hide
                NOT = { has_variable_list = DI_shown_provinces }
            }
            remove_variable = DI_provinces_hide

            every_in_list = {
                variable = DI_selected_titles_provinces
                save_scope_as = province

                scope:player = {
                    add_to_variable_list = {
                        name = DI_shown_provinces
                        target = scope:province
                    }
                }
            }
        }
    }
}

### Check Province In List ###
DI_is_action_province = {
    scope = character # Player Character

    saved_scopes = {
        province # Province to check
    }

    is_valid = {
        is_target_in_variable_list = {
            name = DI_action_provinces
            target = scope:province
        }
    }
}

### Select Culture ###
DI_title_manager_culture = {
    scope = character # Player Character

    saved_scopes = {
        selected_culture # Culture to select
    }

    effect = {
        save_scope_as = player

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

        scope:DI_story = {
            set_variable = {
                name = DI_title_manager_culture
                value = scope:selected_culture
            }
        }
    }
}

### Select Religion ###
DI_title_manager_religion = {
    scope = character # Player Character

    saved_scopes = {
        selected_faith # Religion to select
    }

    effect = {
        save_scope_as = player

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

        scope:DI_story = {
            set_variable = {
                name = DI_title_manager_religion
                value = scope:selected_faith
            }
        }
    }
}

### Convert Religion ###
DI_convert_titles_faith_sgui = {
    scope = character # Player Character

    is_valid = { exists = var:DI_misc_variables_story.var:DI_title_manager_rite }

    effect = {
        save_scope_as = player

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

        scope:DI_story = {
            var:DI_title_manager_rite = { save_scope_as = DI_selected_rite }
        }

        if = {
            limit = { exists = scope:DI_selected_rite }
            every_in_list = {
                variable = DI_selected_titles
                save_scope_as = title
                if = {
                    limit = {
                        tier = tier_county
                    }
                    set_county_rite = scope:DI_selected_rite
                }
                else_if = {
                    limit = {
                        tier > tier_county
                    }
                    every_de_jure_county = {
                        set_county_rite = scope:DI_selected_rite
                    }
                }
            }
        }
    }
}

### Convert Culture ###
DI_convert_titles_culture_sgui = {
    scope = character # Player Character

    effect = {
        save_scope_as = player

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

        scope:DI_story = {
            var:DI_title_manager_culture = { save_scope_as = DI_selected_culture }
        }

        every_in_list = {
            variable = DI_selected_titles
            save_scope_as = title
            if = {
                limit = {
                    tier = tier_county
                }
                set_county_culture = scope:DI_selected_culture
            }
            else_if = {
                limit = {
                    tier > tier_county
                }
                every_de_jure_county = {
                    set_county_culture = scope:DI_selected_culture
                }
            }
        }
    }
}

### DI_select_dejure_title ###
DI_select_dejure_title = {
    scope = character # Player Character

    saved_scopes = {
        title # Title to select
    }

    effect = {
        save_scope_as = player

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

        scope:DI_story = {
            set_variable = {
                name = DI_title_manager_dejure
                value = scope:title
            }
        }
    }
}

### DI_titles_dejure_sgui ###
DI_convert_titles_dejure_sgui = {
    scope = character # Player Character

    effect = {
        save_scope_as = player

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

        scope:DI_story = {
            var:DI_title_manager_dejure = { save_scope_as = DI_selected_dejure }
        }

        every_in_list = {
            variable = DI_selected_titles
            save_scope_as = title
            if = {
                limit = {
                    tier = {
                        value = scope:DI_selected_dejure.tier
                        subtract = 1
                    }
                }
                set_de_jure_liege_title = scope:DI_selected_dejure
            }
        }
    }
}

### DI_province_list_load ###
DI_province_list_load = {
    scope = character # Player Character

    effect = {
        save_scope_as = player

        clear_variable_list = DI_selected_titles_provinces
        clear_variable_list = DI_shown_provinces
        clear_variable_list = DI_action_provinces

        every_in_list = {
            variable = DI_selected_titles
            save_scope_as = title
            if = {
                limit = {
                    tier = tier_county
                }
                every_county_province = {
                    save_scope_as = province

                    scope:player = {
                        add_to_variable_list = {
                            name = DI_selected_titles_provinces
                            target = scope:province
                        }

                        add_to_variable_list = {
                            name = DI_action_provinces
                            target = scope:province
                        }

                        add_to_variable_list = {
                            name = DI_shown_provinces
                            target = scope:province
                        }
                    }
                }
            }
            else_if = {
                limit = {
                    tier < tier_empire
                }
                every_de_jure_county = {
                    every_county_province = {
                        save_scope_as = province

                        scope:player = {
                            add_to_variable_list = {
                                name = DI_selected_titles_provinces
                                target = scope:province
                            }

                            add_to_variable_list = {
                                name = DI_action_provinces
                                target = scope:province
                            }

                            add_to_variable_list = {
                                name = DI_shown_provinces
                                target = scope:province
                            }
                        }
                    }
                }
            }
        }
    }
}

DI_title_manager_county_stats_load = {
    scope = character
    effect = {
        # Versioned initialization mirrors the province manager and replaces old saved lists.
        if = {
            limit = { NOT = { has_global_variable = DI_county_filters_standard_loaded } }
            clear_global_variable_list = DI_county_filter_choices
            set_global_variable = DI_county_filters_standard_loaded
            add_to_global_variable_list = { name = DI_county_filter_choices target = flag:DI_tm_filter_all }
            add_to_global_variable_list = { name = DI_county_filter_choices target = flag:DI_tm_filter_control }
            add_to_global_variable_list = { name = DI_county_filter_choices target = flag:DI_tm_filter_development }
            add_to_global_variable_list = { name = DI_county_filter_choices target = flag:DI_tm_filter_domain }
        }
        DI_tm_refresh_counties_effect = yes
    }
}

DI_title_manager_county_stats_available = {
    scope = character
    is_valid = {
        any_in_list = {
            variable = DI_selected_titles_counties
            always = yes
        }
    }
}

DI_title_manager_county_stats_apply = {
    scope = character
    saved_scopes = { stat operation step }
    is_valid = {
        any_in_list = {
            variable = DI_selected_titles_counties
            always = yes
        }
    }
    effect = {
        DI_tm_refresh_counties_effect = yes
        save_scope_as = player
        every_in_list = {
            variable = DI_selected_titles_counties
            save_scope_as = county
            scope:county = {
                if = {
                    limit = { scope:stat = flag:DI_tm_county_stat_control }
                    if = {
                        limit = { scope:operation = flag:DI_tm_county_op_min }
                        change_county_control = -100
                    }
                    else_if = {
                        limit = { scope:operation = flag:DI_tm_county_op_subtract }
                        change_county_control = DI_tm_county_stats_negative_step_value
                    }
                    else_if = {
                        limit = { scope:operation = flag:DI_tm_county_op_mid }
                        change_county_control = DI_tm_county_control_mid_delta
                    }
                    else_if = {
                        limit = { scope:operation = flag:DI_tm_county_op_add }
                        change_county_control = DI_tm_county_stats_step_value
                    }
                    else_if = {
                        limit = { scope:operation = flag:DI_tm_county_op_max }
                        change_county_control = 100
                    }
                }
                else_if = {
                    limit = { scope:stat = flag:DI_tm_county_stat_development }
                    if = {
                        limit = { scope:operation = flag:DI_tm_county_op_min }
                        change_development_level = -100
                    }
                    else_if = {
                        limit = { scope:operation = flag:DI_tm_county_op_subtract }
                        change_development_level = DI_tm_county_stats_negative_step_value
                    }
                    else_if = {
                        limit = { scope:operation = flag:DI_tm_county_op_mid }
                        change_development_level = DI_tm_county_development_mid_delta
                    }
                    else_if = {
                        limit = { scope:operation = flag:DI_tm_county_op_add }
                        change_development_level = DI_tm_county_stats_step_value
                    }
                    else_if = {
                        limit = { scope:operation = flag:DI_tm_county_op_max }
                        change_development_level = 100
                    }
                }
            }
        }
        DI_tm_refresh_counties_effect = yes
    }
}

DI_set_county_capital_sgui = {
    scope = character
    saved_scopes = {
        province
        barony
    }

    is_valid = {
        scope:province = { is_county_capital = no }
    }

    effect = {
        scope:barony = { set_capital_barony = yes }
    }
}

DI_remove_holding_sgui = {
    scope = character # Player Character

    effect = {
        save_scope_as = player

        every_in_list = {
            variable = DI_action_provinces
            limit = {
                is_county_capital = no
            }

            save_scope_as = province

            scope:province = {
                remove_holding = yes

                ### City Buildings
                if = { limit = { has_building = city_04 } remove_building = city_04 }
                else_if = { limit = { has_building = city_03 } remove_building = city_03 }
                else_if = { limit = { has_building = city_02 } remove_building = city_02 }
                else_if = { limit = { has_building = city_01 } remove_building = city_01 }

                ### Castle Buildings
                if = { limit = { has_building = castle_04 } remove_building = castle_04 }
                else_if = { limit = { has_building = castle_03 } remove_building = castle_03 }
                else_if = { limit = { has_building = castle_02 } remove_building = castle_02 }
                else_if = { limit = { has_building = castle_01 } remove_building = castle_01 }

                ### Church Buildings
                if = { limit = { has_building = temple_01 } remove_building = temple_01 }
                else_if = { limit = { has_building = temple_02 } remove_building = temple_02 }
                else_if = { limit = { has_building = temple_03 } remove_building = temple_03 }
                else_if = { limit = { has_building = temple_04 } remove_building = temple_04 }

                ### Tribal Buildings
                if = { limit = { has_building = tribe_01 } remove_building = tribe_01 }
                else_if = { limit = { has_building = tribe_02 } remove_building = tribe_02 }

                ### Temple Citadel Buildings
                if = { limit = { has_building = temple_citadel_01 } remove_building = temple_citadel_01 }
                else_if = { limit = { has_building = temple_citadel_02 } remove_building = temple_citadel_02 }
                else_if = { limit = { has_building = temple_citadel_03 } remove_building = temple_citadel_03 }
                else_if = { limit = { has_building = temple_citadel_04 } remove_building = temple_citadel_04 }
            }
        }
    }
}

DI_add_building_slot_sgui = {
    scope = character # Player Character

    effect = {
        save_scope_as = player

        every_in_list = {
            variable = DI_action_provinces
            save_scope_as = province

            scope:province = {
                add_province_modifier = add_building_slot
            }
        }
    }
}

DI_remove_building_slot_sgui = {
    scope = character # Player Character

    effect = {
        save_scope_as = player

        every_in_list = {
            variable = DI_action_provinces
            save_scope_as = province

            scope:province = {
                remove_province_modifier = add_building_slot
            }
        }
    }
}

DI_generate_buildings_sgui = {
    scope = character # Player Character

    effect = {
        save_scope_as = player

        every_in_list = {
            variable = DI_action_provinces
            save_scope_as = province

            scope:province = {
                generate_building = yes
            }
        }
    }
}
