﻿########################################
# Global marshal story effects
########################################

wld_global_marshal_story_pulse = {
    save_temporary_scope_as = this_story
    every_ruler = {
        if = {
            limit = { exists = cp:councillor_marshal }
            cp:councillor_marshal = {
                scope:this_story = {
                    add_to_variable_list = {
                        name = marshals
                        target = prev
                    }
                }
            }
        }
    }
    every_in_list = {
        variable = marshals
        wld_update_marshal = yes
    }
}

wld_update_marshal = {
    set_local_variable = {
        name = liege_tier
        value = liege.highest_held_title_tier
    }
    if = {
        limit = {
            exists = this
            is_alive = yes
            local_var:liege_tier >= tier_county
            liege = {
                exists = this
                is_alive = yes
                exists = cp:councillor_marshal
                cp:councillor_marshal = prev
            }
        }
        if = {
            limit = {
                OR = {
                    NOT = { has_variable = marshal_tier }
                    var:marshal_tier != local_var:liege_tier
                }
            }
            if = {
                limit = { local_var:liege_tier >= tier_empire }
                remove_character_modifier = wld_marshal_modifier_1
                remove_character_modifier = wld_marshal_modifier_2
                remove_character_modifier = wld_marshal_modifier_3
                add_character_modifier = wld_marshal_modifier_4
            }
            else_if = {
                limit = { local_var:liege_tier = tier_kingdom }
                remove_character_modifier = wld_marshal_modifier_1
                remove_character_modifier = wld_marshal_modifier_2
                add_character_modifier = wld_marshal_modifier_3
                remove_character_modifier = wld_marshal_modifier_4
            }
            else_if = {
                limit = { local_var:liege_tier = tier_duchy }
                remove_character_modifier = wld_marshal_modifier_1
                add_character_modifier = wld_marshal_modifier_2
                remove_character_modifier = wld_marshal_modifier_3
                remove_character_modifier = wld_marshal_modifier_4
            }
            else = {
                add_character_modifier = wld_marshal_modifier_1
                remove_character_modifier = wld_marshal_modifier_2
                remove_character_modifier = wld_marshal_modifier_3
                remove_character_modifier = wld_marshal_modifier_4
            }
            set_variable = {
                name = marshal_tier
                value = local_var:liege_tier
            }
        }
    }
    else = {
        remove_character_modifier = wld_marshal_modifier_1
        remove_character_modifier = wld_marshal_modifier_2
        remove_character_modifier = wld_marshal_modifier_3
        remove_character_modifier = wld_marshal_modifier_4
        if = {
            limit = {
                is_alive = yes
                has_variable = marshal_tier
            }
            remove_variable = marshal_tier
        }
        if = {
            limit = {
                exists = scope:this_story
                scope:this_story = { has_variable_list = marshals }
            }
            scope:this_story = {
                remove_list_variable = {
                    name = marshals
                    target = prev
                }
            }
        }
    }
}

########################################
# Global story effects
########################################

# Scope: Global
wld_try_setup_global_story = {
    if = {
        limit = { NOT = { exists = global_var:wld_gv_global_story } }
        set_global_variable = wld_gv_global_story
        wld_create_random_character = yes
        scope:new_char = {
            create_story = wld_global_story
            death = natural
        }
    }
}

wld_global_story_pulse = {
    # Try to initialize in case mod was loaded mid-save
    wld_try_initialize = yes
}

########################################
# Initialize
########################################

wld_try_initialize = {
    # Setup null character
    if = {
        limit = { NOT = { exists = global_var:wld_null_marshal } }
        wld_create_random_character = yes
        scope:new_char = {
            set_global_variable = {
                name = wld_null_marshal
                value = this
            }
            death = natural
        }
    }

    # Setup ruler stories
    # if = {
    #     limit = { NOT = { exists = global_var:wld_gv_ruler_stories } }
    #     set_global_variable = wld_gv_ruler_stories
    #     every_ruler = {
    #         wld_try_setup_ruler_story = yes
    #     }
    # }
}

########################################
# Util
########################################

# Creates random character with scope "new_char"
wld_create_random_character = {
    random_province = { save_temporary_scope_as = tmp_province }
    random_culture_global = { save_temporary_scope_as = tmp_culture }
    random_religion_global = {
        random_faith = {
            save_temporary_scope_as = tmp_faith
        }
    }
    create_character = {
        location = scope:tmp_province
        culture = scope:tmp_culture
        faith = scope:tmp_faith
        save_temporary_scope_as = new_char
        gender = male
    }
}
