# HOI4 Roguelike v3.7.0 - setup from game rules
# Replaces the former in-game setup events. Must run in the scope of a human country.

rl_apply_game_rule_ruleset = {
    set_global_flag = rl_mode_configured
    set_global_flag = rl_ruleset_configured
    if = {
        limit = { has_game_rule = { rule = rl_rule_ruleset option = RL_HARDCORE } }
        set_global_flag = rl_ruleset_hardcore
        clr_global_flag = rl_ruleset_standard
    }
    else = {
        set_global_flag = rl_ruleset_standard
        clr_global_flag = rl_ruleset_hardcore
    }
    if = {
        limit = { has_game_rule = { rule = rl_rule_recipients option = RL_PLAYER_AND_AI } }
        set_global_flag = rl_mode_all
        clr_global_flag = rl_mode_player_only
    }
    else = {
        set_global_flag = rl_mode_player_only
        clr_global_flag = rl_mode_all
    }
}

rl_apply_game_rule_frequency = {
    set_global_flag = rl_frequency_configured
    clr_global_flag = rl_freq_weekly
    clr_global_flag = rl_freq_biweekly
    clr_global_flag = rl_freq_monthly
    clr_global_flag = rl_freq_quarterly
    clr_global_flag = rl_freq_semiannual
    if = {
        limit = { has_game_rule = { rule = rl_rule_frequency option = RL_WEEKLY } }
        set_global_flag = rl_freq_weekly
    }
    else_if = {
        limit = { has_game_rule = { rule = rl_rule_frequency option = RL_BIWEEKLY } }
        set_global_flag = rl_freq_biweekly
    }
    else_if = {
        limit = { has_game_rule = { rule = rl_rule_frequency option = RL_QUARTERLY } }
        set_global_flag = rl_freq_quarterly
    }
    else = {
        set_global_flag = rl_freq_monthly
    }
}

rl_apply_game_rule_rarity = {
    set_global_flag = rl_rarity_mode_configured
    every_country = { clr_country_flag = rl_rarity_synced }
    rl_clear_global_chaos_profiles = yes
    if = {
        limit = { has_game_rule = { rule = rl_rule_rarity option = RL_RARITY_CHAOS } }
        set_global_flag = rl_rarity_chaos
        clr_global_flag = rl_rarity_normal
        rl_select_global_chaos_distribution = yes
    }
    else = {
        set_global_flag = rl_rarity_normal
        clr_global_flag = rl_rarity_chaos
    }
}

rl_apply_game_rule_hc_replace = {
    set_global_flag = rl_hc_replace_mode_configured
    if = {
        limit = {
            has_global_flag = rl_ruleset_hardcore
            has_game_rule = { rule = rl_rule_hc_replace option = RL_HC_AUTO_WEAKEST }
        }
        set_global_flag = rl_hc_auto_replace_weakest
        clr_global_flag = rl_hc_manual_replace
    }
    else = {
        set_global_flag = rl_hc_manual_replace
        clr_global_flag = rl_hc_auto_replace_weakest
    }
}

rl_apply_game_rule_counterpower = {
    if = {
        limit = { NOT = { has_global_flag = rl_mode_player_only } }
        rl_set_counterpower_none = yes
    }
    else_if = {
        limit = { has_game_rule = { rule = rl_rule_counterpower option = RL_CP_NONE } }
        rl_set_counterpower_none = yes
    }
    else_if = {
        limit = { has_game_rule = { rule = rl_rule_counterpower option = RL_CP_LIGHT } }
        rl_set_counterpower_light = yes
    }
    else_if = {
        limit = { has_game_rule = { rule = rl_rule_counterpower option = RL_CP_STRONG } }
        rl_set_counterpower_strong = yes
    }
    else_if = {
        limit = { has_game_rule = { rule = rl_rule_counterpower option = RL_CP_WORLD } }
        rl_set_counterpower_world = yes
    }
    else = {
        rl_set_counterpower_normal = yes
    }
}

rl_apply_game_rule_setup = {
    if = {
        limit = { NOT = { has_global_flag = rl_setup_complete } }
        rl_apply_game_rule_ruleset = yes
        rl_apply_game_rule_frequency = yes
        rl_apply_game_rule_rarity = yes
        rl_apply_game_rule_hc_replace = yes
        rl_apply_game_rule_counterpower = yes
        rl_finalize_roguelike_setup = yes
    }
}
