﻿create_searched_character_list = {
    if = {
        limit = {
            NOT = { has_global_variable = is_acs_building_list }
        }
        set_global_variable = is_acs_building_list
        
        acs_se_create_religion_lists = yes
        
        clear_acs_list = yes
        random_player = {
            save_scope_as = acs_filter_diplomatic_range_root
            save_scope_as = acs_filter_hooks_root
            faith = { save_scope_as = acs_filter_faith }
            religion = { save_scope_as = acs_filter_religion }
            save_scope_as = acs_filter_culture_root
            save_scope_as = acs_filter_dynasty_root
            save_scope_as = acs_sort_root
            save_scope_as = acs_invite_to_court
            acs_make_reduced_and_count = yes
            if = {
                limit = { global_var:acs_gv_main_filters.var:acs_v_filter_relation = 0 }
                if = {
                    limit = {
                        any_in_list = {
                            variable =  acs_vl_filter_inspiration
                            save_temporary_scope_value_as = { name = to_get_modulo value = this }
                            save_temporary_scope_value_as = { name = to_test value = acs_modulo_2 }
                            scope:to_test = 0
                            count > 0
                        }
                    }
                    every_inspired_character = {
                        acs_add_to_sort_list = yes
                    }
                }
                else = {
                    every_living_character = { acs_add_to_sort_list = yes }
                }          
            }
            else_if = {
                limit = { 
                    NOR = { 
                        global_var:acs_gv_main_filters.var:acs_v_filter_relation = 6
                        global_var:acs_gv_main_filters.var:acs_v_filter_relation = 7
                    }
                }
                if = {
                    limit = { 
                        OR = { 
                            global_var:acs_gv_main_filters.var:acs_v_filter_relation = 1
                            global_var:acs_gv_main_filters.var:acs_v_filter_relation = 2
                            global_var:acs_gv_main_filters.var:acs_v_filter_relation = 3
                            global_var:acs_gv_main_filters.var:acs_v_filter_relation = 4
                        }
                    }
                    top_liege = { add_to_temporary_list = acs_to_sort_courts }
                }

                if = {
                    limit = {
                        OR = { 
                            global_var:acs_gv_main_filters.var:acs_v_filter_relation = 1
                            global_var:acs_gv_main_filters.var:acs_v_filter_relation = 8
                            global_var:acs_gv_main_filters.var:acs_v_filter_relation = 12
                        }
                    }
                    top_liege = { #close realms
                        save_temporary_scope_as = top_liege_scope
                        every_neighboring_and_across_water_top_liege_realm_owner = {
                            add_to_temporary_list = acs_to_sort_courts
                            set_variable = { name = acs_distance value = realm_size }
                        }

                        set_local_variable = { name = new_entrie_found value = 1 }
                        while = {
                            limit = { local_var:new_entrie_found = 1 }
                            set_local_variable = { name = new_entrie_found value = 0 }
                            every_in_list = {
                                list = acs_to_sort_courts
                                if = { 
                                    limit = { var:acs_distance < 14 }
                                    every_neighboring_and_across_water_top_liege_realm_owner = {
                                        set_local_variable = { name = distance value = { value = prev.var:acs_distance add = realm_size }  }
                                        if = {
                                            limit = {
                                                trigger_if = {
                                                    limit = { exists = var:acs_distance }
                                                    acs_distance > 0
                                                }
                                                trigger_else = { 
                                                    always = yes
                                                }
                                                NOT = { scope:top_liege_scope = this }
                                            }
                                            add_to_temporary_list = acs_to_sort_courts
                                            set_variable = { name = acs_distance value = local_var:distance }
                                            set_local_variable = { name = new_entrie_found value = 1 }
                                        }  
                                    }
                                }
                            }
                        }
                        every_in_list = {
                            list = acs_to_sort_courts
                            remove_variable = acs_distance
                        }
                    }
                }
                else_if = {
                    limit = {
                        OR = { 
                            global_var:acs_gv_main_filters.var:acs_v_filter_relation = 2
                            global_var:acs_gv_main_filters.var:acs_v_filter_relation = 9
                            global_var:acs_gv_main_filters.var:acs_v_filter_relation = 13
                        }
                    }
                    top_liege = { 
                        every_neighboring_and_across_water_top_liege_realm_owner = {
                            add_to_temporary_list = acs_to_sort_courts
                        }
                    }
                }
                else_if = {
                    limit = {
                        OR = { 
                            global_var:acs_gv_main_filters.var:acs_v_filter_relation = 3
                            global_var:acs_gv_main_filters.var:acs_v_filter_relation = 10
                            global_var:acs_gv_main_filters.var:acs_v_filter_relation = 14
                        }
                    }
                    top_liege = { 
                        every_neighboring_top_liege_realm_owner = {
                            add_to_temporary_list = acs_to_sort_courts
                        }
                    }
                }
                else_if = {
                    limit = {
                        global_var:acs_gv_main_filters.var:acs_v_filter_relation = 11
                    }
                   
                    top_liege = { 
                        every_independent_ruler = {
                            add_to_temporary_list = acs_to_sort_courts
                        }
                    }
                }
                else_if = {
                    limit = { 
                        global_var:acs_gv_main_filters.var:acs_v_filter_relation = 5
                    }
                    add_to_temporary_list = acs_to_sort_courts
                }
                if = {
                    limit = {
                        OR = { 
                            global_var:acs_gv_main_filters.var:acs_v_filter_relation = 1
                            global_var:acs_gv_main_filters.var:acs_v_filter_relation = 2
                            global_var:acs_gv_main_filters.var:acs_v_filter_relation = 3
                            global_var:acs_gv_main_filters.var:acs_v_filter_relation = 4
                            global_var:acs_gv_main_filters.var:acs_v_filter_relation = 5
                            global_var:acs_gv_main_filters.var:acs_v_filter_relation = 8
                            global_var:acs_gv_main_filters.var:acs_v_filter_relation = 9
                            global_var:acs_gv_main_filters.var:acs_v_filter_relation = 10
                        }
                    }
                    if = {
                        limit = {
                            any_in_list = {
                                variable =  acs_vl_filter_inspiration
                                save_temporary_scope_value_as = { name = to_get_modulo value = this }
                                save_temporary_scope_value_as = { name = to_test value = acs_modulo_2 }
                                scope:to_test = 0
                                count > 0
                            }
                        }
                        every_inspired_character = {
                            save_temporary_scope_as = acs_ts_inspired_candidate
                            if = {
                                limit = {
                                    any_in_list = {
                                        list = acs_to_sort_courts
                                        OR = {
                                            any_vassal_or_below = {
                                                save_temporary_scope_as = acs_ts_candidate_owner
                                                OR = {
                                                    scope:acs_ts_candidate_owner = scope:acs_ts_inspired_candidate.court_owner
                                                    trigger_if = {
                                                        limit = { scope:acs_ts_inspired_candidate = { exists = imprisoner } }
                                                        scope:acs_ts_candidate_owner = scope:acs_ts_inspired_candidate.court_owner
                                                    }
                                                    trigger_else = { always = no }
                                                }
                                            }
                                            save_temporary_scope_as = acs_ts_candidate_owner
                                            OR = {
                                                scope:acs_ts_candidate_owner = scope:acs_ts_inspired_candidate.court_owner
                                                trigger_if = {
                                                    limit = { scope:acs_ts_inspired_candidate = { exists = imprisoner } }
                                                    scope:acs_ts_candidate_owner = scope:acs_ts_inspired_candidate.court_owner
                                                }
                                                trigger_else = { always = no }
                                            }
                                        }
                                    }
                                }
                                acs_add_to_sort_list = yes
                            }
                        }
                
                    }
                    else = {
                        every_in_list = {
                            list = acs_to_sort_courts
                            every_vassal_or_below = {
                                acs_add_court_to_list = yes
                            }
                            acs_add_court_to_list = yes
                        }
                    }  
                }
                else = {
                    if = {
                        limit = {
                            any_in_list = {
                                variable =  acs_vl_filter_inspiration
                                save_temporary_scope_value_as = { name = to_get_modulo value = this }
                                save_temporary_scope_value_as = { name = to_test value = acs_modulo_2 }
                                scope:to_test = 0
                                count > 0
                            }
                        }
                        every_inspired_character = {
                            save_temporary_scope_as = acs_ts_inspired_candidate
                            if = {
                                limit = {
                                    any_in_list = {
                                        list = acs_to_sort_courts
                                        OR = {
                                            save_temporary_scope_as = acs_ts_candidate_owner
                                            OR = {
                                                scope:acs_ts_candidate_owner = scope:acs_ts_inspired_candidate.court_owner
                                                trigger_if = {
                                                    limit = { scope:acs_ts_inspired_candidate = { exists = imprisoner } }
                                                    scope:acs_ts_candidate_owner = scope:acs_ts_inspired_candidate.court_owner
                                                }
                                                trigger_else = { always = no }
                                            }
                                        }
                                    }
                                }
                                acs_add_to_sort_list = yes
                            }
                        }
                    }
                    else = {
                        every_in_list = {
                            list = acs_to_sort_courts
                            acs_add_court_to_list = yes
                        }
                    } 
                    
                }
                
            }
            else_if = {
                limit = { global_var:acs_gv_main_filters.var:acs_v_filter_relation = 6 }
                if = {
                    limit = {
                        any_in_list = {
                            variable =  acs_vl_filter_inspiration
                            save_temporary_scope_value_as = { name = to_get_modulo value = this }
                            save_temporary_scope_value_as = { name = to_test value = acs_modulo_2 }
                            scope:to_test = 0
                            count > 0
                        }
                    }
                    every_inspired_character = {
                        save_temporary_scope_as = acs_ts_inspired_candidate
                        if = {
                            limit = {
                                any_vassal = {
                                    OR = {
                                        save_temporary_scope_as = acs_ts_candidate_owner
                                        OR = {
                                            scope:acs_ts_candidate_owner = scope:acs_ts_inspired_candidate.court_owner
                                            trigger_if = {
                                                limit = { scope:acs_ts_inspired_candidate = { exists = imprisoner } }
                                                scope:acs_ts_candidate_owner = scope:acs_ts_inspired_candidate.court_owner
                                            }
                                            trigger_else = { always = no }
                                        }
                                    }
                                }
                            }
                            acs_add_to_sort_list = yes
                        }
                    }
                }
                else = {
                    every_vassal = { acs_add_court_to_list = yes }
                } 
                
            }
            else = {
                acs_add_court_to_list = yes
            }
        }
          
        set_global_variable = {  
            name = acs_selected_count 
            value = {
                value = 0
                every_in_list = { list = acs_to_sort add = 1 }
            }  
        } 
        set_global_variable = {
            name = acs_total_number_of_characters
            value = acs_total_number_of_characters
        }

        ordered_in_list = {
            list = acs_to_sort
            order_by = acs_sort_value
            max = global_var:acs_select_count
            check_range_bounds = no
            add_to_global_variable_list = { name = acs_list target = this }
            set_claim_data = yes
        }    
        
        remove_global_variable = is_acs_building_list
    }
}

acs_add_court_to_list = {
    every_courtier_or_guest = { acs_add_to_sort_list = yes }
    every_prisoner = { acs_add_to_sort_list = yes }
    acs_add_to_sort_list = yes   
}

acs_add_to_sort_list = {
    if = { 
        limit = { 
            save_temporary_scope_as = candidate
            global_var:acs_gv_main_filters = {
                any_in_list = {
                    variable = acs_vl_filter_active
                    save_temporary_scope_as = tag
                    acs_switch_filter = { FILTER = scope:tag CANDIDATE = scope:candidate }
                    count = all
                }
            }
            
        }
        add_to_temporary_list = acs_to_sort
    }
}

clear_acs_list = {
    if = {
        limit = { has_global_variable_list = acs_list }
        every_in_global_list = {
            list = acs_list
            clear_claim_data = yes
        }
        clear_global_variable_list = acs_list
    }
}



acs_change_sort_order = {
    if = {
        limit = { has_global_variable = acs_sort_by_ascending }
        remove_global_variable = acs_sort_by_ascending
    }
    else = {
        set_global_variable = acs_sort_by_ascending
    }
}

acs_se_auto_apply_sorting_and_filters = {
    acs_se_set_current_slot = yes
    if = {
        limit = { has_global_variable = acs_gv_auto_apply_sorting_and_filters }
        create_searched_character_list = yes
    }
}






