﻿patron_clear_variables = {
    # Daedra #
    remove_variable = patron_shown

    # Tabs # these are here to force close the interaction tab on faith change
    # remove_variable = patron_interaction_tab_toggle
}

# patron_clear_res_variables = {
#     # Daedra #
#     remove_variable = patron_shown_res

#     # Tabs # these are here to force close the interaction tab on faith change
#     remove_variable = patron_interaction_tab_toggle_res
# }

patron_faith_change_effect = {
    #patron_clear_variables = yes
    #patron_clear_res_variables = yes
    patron_faith_change_clear_traits = yes
}

# Ardor System #

# ardor_[patron] is set to zero on startup and faith change for all valid patrons
# ardor_display is used as an intermediry for decision costs and display
# on communing it is assigned to the value of the chosen patron, ardor_[patron] is updated by each decision
# for ai ardor_display is updated on patron choice decision
# initializing for the ai is done by patron choice selection - since they access one at a time only need one variable active

# try using get_patron and $PATRON$ on update effects to streamline
# need to be able to get a return value to make it useful though I think

patron_ardor_initialize = {
    if = {
        limit = {
            faith = { patron_ob_azura_valid = yes }
            NOT = { has_variable = ardor_azura }
        }
        set_variable = { name = ardor_azura value = 0 }
    }
    if = {
        limit = {
            faith = { patron_ob_boethiah_valid = yes }
            NOT = { has_variable = ardor_boethiah }
        }
        set_variable = { name = ardor_boethiah value = 0 }
    }
    if = {
        limit = {
            faith = { patron_ob_clavicus_valid = yes }
            NOT = { has_variable = ardor_clavicus }
        }
        set_variable = { name = ardor_clavicus value = 0 }
    }
    if = {
        limit = {
            faith = { patron_ob_hermaeus_valid = yes }
            NOT = { has_variable = ardor_hermaeus }
        }
        set_variable = { name = ardor_hermaeus value = 0 }
    }
    if = {
        limit = {
            faith = { patron_ob_hircine_valid = yes }
            NOT = { has_variable = ardor_hircine }
        }
        set_variable = { name = ardor_hircine value = 0 }
    }
    if = {
        limit = {
            faith = { patron_ob_malacath_valid = yes }
            NOT = { has_variable = ardor_malacath }
        }
        set_variable = { name = ardor_malacath value = 0 }
    }
    if = {
        limit = {
            faith = { patron_ob_mehrunes_valid = yes }
            NOT = { has_variable = ardor_mehrunes }
        }
        set_variable = { name = ardor_mehrunes value = 0 }
    }
    if = {
        limit = {
            faith = { patron_ob_mephala_valid = yes }
            NOT = { has_variable = ardor_mephala }
        }
        set_variable = { name = ardor_mephala value = 0 }
    }
    if = {
        limit = {
            faith = { patron_ob_meridia_valid = yes }
            NOT = { has_variable = ardor_meridia }
        }
        set_variable = { name = ardor_meridia value = 0 }
    }
    if = {
        limit = {
            faith = { patron_ob_molag_valid = yes }
            NOT = { has_variable = ardor_molag }
        }
        set_variable = { name = ardor_molag value = 0 }
    }
    if = {
        limit = {
            faith = { patron_ob_namira_valid = yes }
            NOT = { has_variable = ardor_namira }
        }
        set_variable = { name = ardor_namira value = 0 }
    }
    if = {
        limit = {
            faith = { patron_ob_nocturnal_valid = yes }
            NOT = { has_variable = ardor_nocturnal }
        }
        set_variable = { name = ardor_nocturnal value = 0 }
    }
    if = {
        limit = {
            faith = { patron_ob_peryite_valid = yes }
            NOT = { has_variable = ardor_peryite }
        }
        set_variable = { name = ardor_peryite value = 0 }
    }
    if = {
        limit = {
            faith = { patron_ob_sanguine_valid = yes }
            NOT = { has_variable = ardor_sanguine }
        }
        set_variable = { name = ardor_sanguine value = 0 }
    }
    if = {
        limit = {
            faith = { patron_ob_sheogorath_valid = yes }
            NOT = { has_variable = ardor_sheogorath }
        }
        set_variable = { name = ardor_sheogorath value = 0 }
    }
    if = {
        limit = {
            faith = { patron_ob_vaermina_valid = yes }
            NOT = { has_variable = ardor_vaermina }
        }
        set_variable = { name = ardor_vaermina value = 0 }
    }
}

patron_ardor_update_display = { # will be redundant when flag replacement done
    switch = {
        trigger = var:patron_shown
        flag:azura = { set_variable = { name = ardor_display value = var:ardor_azura } }
        flag:boethiah = { set_variable = { name = ardor_display value = var:ardor_boethiah } }
        flag:clavicus = { set_variable = { name = ardor_display value = var:ardor_clavicus } }
        flag:hermaeus = { set_variable = { name = ardor_display value = var:ardor_hermaeus } }
        flag:hircine = { set_variable = { name = ardor_display value = var:ardor_hircine } }
        flag:malacath = { set_variable = { name = ardor_display value = var:ardor_malacath } }
        flag:mehrunes = { set_variable = { name = ardor_display value = var:ardor_mehrunes } }
        flag:mephala = { set_variable = { name = ardor_display value = var:ardor_mephala } }
        flag:meridia = { set_variable = { name = ardor_display value = var:ardor_meridia } }
        flag:molag = { set_variable = { name = ardor_display value = var:ardor_molag } }
        flag:namira = { set_variable = { name = ardor_display value = var:ardor_namira } }
        flag:nocturnal = { set_variable = { name = ardor_display value = var:ardor_nocturnal } }
        flag:peryite = { set_variable = { name = ardor_display value = var:ardor_peryite } }
        flag:sanguine = { set_variable = { name = ardor_display value = var:ardor_sanguine } }
        flag:sheogorath = { set_variable = { name = ardor_display value = var:ardor_sheogorath } }
        flag:vaermina = { set_variable = { name = ardor_display value = var:ardor_vaermina } }
    }
    # else = { #this should never fire, in for debugging when it does
    #     set_variable = { name = ardor_display value = 0 }
    #     set_variable = ardor_display_error
    # }
}

patron_ardor_update_actual = {
    switch = {
        trigger = var:patron_shown
        flag:azura = { set_variable = { name = ardor_azura value = var:ardor_display } }
        flag:boethiah = { set_variable = { name = ardor_boethiah value = var:ardor_display } }
        flag:clavicus = { set_variable = { name = ardor_clavicus value = var:ardor_display } }
        flag:hermaeus = { set_variable = { name = ardor_hermaeus value = var:ardor_display } }
        flag:hircine = { set_variable = { name = ardor_hircine value = var:ardor_display } }
        flag:malacath = { set_variable = { name = ardor_malacath value = var:ardor_display } }
        flag:mehrunes = { set_variable = { name = ardor_mehrunes value = var:ardor_display } }
        flag:mephala = { set_variable = { name = ardor_mephala value = var:ardor_display } }
        flag:meridia = { set_variable = { name = ardor_meridia value = var:ardor_display } }
        flag:molag = { set_variable = { name = ardor_molag value = var:ardor_display } }
        flag:namira = { set_variable = { name = ardor_namira value = var:ardor_display } }
        flag:nocturnal = { set_variable = { name = ardor_nocturnal value = var:ardor_display } }
        flag:peryite = { set_variable = { name = ardor_peryite value = var:ardor_display } }
        flag:sanguine = { set_variable = { name = ardor_sanguine value = var:ardor_display } }
        flag:sheogorath = { set_variable = { name = ardor_sheogorath value = var:ardor_display } }
        flag:vaermina = { set_variable = { name = ardor_vaermina value = var:ardor_display } }
    }
}

patron_ardor_change = {
    custom_description = {
        text = patron_ardor_change
        value = $ARDOR$ #doesnt auto detect value
        change_variable = { name = ardor_display add = $ARDOR$ }
    }
    patron_ardor_update_actual = yes
}

patron_status_initialize = {
    set_global_variable = { name = status_azura value = flag:nominal }
    set_global_variable = { name = status_boethiah value = flag:nominal }
    set_global_variable = { name = status_clavicus value = flag:nominal }
    set_global_variable = { name = status_hermaeus value = flag:nominal }
    set_global_variable = { name = status_hircine value = flag:nominal }
    set_global_variable = { name = status_malacath value = flag:nominal }
    set_global_variable = { name = status_mehrunes value = flag:nominal }
    set_global_variable = { name = status_mephala value = flag:nominal }
    set_global_variable = { name = status_meridia value = flag:nominal }
    set_global_variable = { name = status_molag value = flag:nominal }
    set_global_variable = { name = status_namira value = flag:nominal }
    set_global_variable = { name = status_nocturnal value = flag:nominal }
    set_global_variable = { name = status_peryite value = flag:nominal }
    set_global_variable = { name = status_sanguine value = flag:nominal }
    set_global_variable = { name = status_sheogorath value = flag:nominal }
    set_global_variable = { name = status_vaermina value = flag:nominal }
}

patron_trait_master_effect = {
    # Divines
	$EFFECT$ = { TRAIT = _dv_talos }
    $EFFECT$ = { TRAIT = _dv_akatosh }
    $EFFECT$ = { TRAIT = _dv_arkay }
    $EFFECT$ = { TRAIT = _dv_dibella }
    $EFFECT$ = { TRAIT = _dv_julianos }
    $EFFECT$ = { TRAIT = _dv_kynareth }
    $EFFECT$ = { TRAIT = _dv_mara }
    $EFFECT$ = { TRAIT = _dv_stendarr }
    $EFFECT$ = { TRAIT = _dv_zenithar }
    $EFFECT$ = { TRAIT = _dv_shezarr }
    # Aedra
    $EFFECT$ = { TRAIT = _ae_auriel }
    $EFFECT$ = { TRAIT = _ae_jephre }
    $EFFECT$ = { TRAIT = _ae_magnus }
    $EFFECT$ = { TRAIT = _ae_phynaster }
    $EFFECT$ = { TRAIT = _ae_syrabane }
    $EFFECT$ = { TRAIT = _ae_trinimac }
    $EFFECT$ = { TRAIT = _ae_xarxes }
    $EFFECT$ = { TRAIT = _ae_xen }
    # Daedra
    $EFFECT$ = { TRAIT = _ob_azura }
    $EFFECT$ = { TRAIT = _ob_boethiah }
    $EFFECT$ = { TRAIT = _ob_clavicus }
    $EFFECT$ = { TRAIT = _ob_hermaeus }
    $EFFECT$ = { TRAIT = _ob_hircine }
    $EFFECT$ = { TRAIT = _ob_malacath }
    $EFFECT$ = { TRAIT = _ob_mehrunes }
    $EFFECT$ = { TRAIT = _ob_mephala }
    $EFFECT$ = { TRAIT = _ob_meridia }
    $EFFECT$ = { TRAIT = _ob_molag }
    $EFFECT$ = { TRAIT = _ob_namira }
    $EFFECT$ = { TRAIT = _ob_nocturnal }
    $EFFECT$ = { TRAIT = _ob_peryite }
    $EFFECT$ = { TRAIT = _ob_sanguine }
    $EFFECT$ = { TRAIT = _ob_sheogorath }
    $EFFECT$ = { TRAIT = _ob_vaermina }
    # Dwemer Ancestors
    $EFFECT$ = { TRAIT = _da_architect }
    $EFFECT$ = { TRAIT = _da_giant }
    $EFFECT$ = { TRAIT = _da_lost_king }
    $EFFECT$ = { TRAIT = _da_warrior }
    # Atmoran Totems
    $EFFECT$ = { TRAIT = _at_bear }
    $EFFECT$ = { TRAIT = _at_dragon }
    $EFFECT$ = { TRAIT = _at_fox }
    $EFFECT$ = { TRAIT = _at_hawk }
    $EFFECT$ = { TRAIT = _at_moth }
    $EFFECT$ = { TRAIT = _at_owl }
    $EFFECT$ = { TRAIT = _at_snake }
    $EFFECT$ = { TRAIT = _at_whale }
    $EFFECT$ = { TRAIT = _at_wolf }
    $EFFECT$ = { TRAIT = _at_hare }
    # Elemental Gods
    $EFFECT$ = { TRAIT = _eg_myn }
    $EFFECT$ = { TRAIT = _eg_zisa }
    $EFFECT$ = { TRAIT = _eg_nyfa }
    $EFFECT$ = { TRAIT = _eg_ilni }
    $EFFECT$ = { TRAIT = _eg_tam }
    # Cyrodiilic Heroes
    $EFFECT$ = { TRAIT = _ch_alessia }
    $EFFECT$ = { TRAIT = _ch_pelinal }
    $EFFECT$ = { TRAIT = _ch_morihaus }
    $EFFECT$ = { TRAIT = _ch_reman }
    $EFFECT$ = { TRAIT = _ch_bendu }
    # Thousand Cults
    $EFFECT$ = { TRAIT = _tc_aka }
	$EFFECT$ = { TRAIT = _tc_missing }
	$EFFECT$ = { TRAIT = _tc_land }
	$EFFECT$ = { TRAIT = _tc_moth }
	$EFFECT$ = { TRAIT = _tc_beasts }
	$EFFECT$ = { TRAIT = _tc_love }
	$EFFECT$ = { TRAIT = _tc_guilds }
	$EFFECT$ = { TRAIT = _tc_heavens }
    # Yokudan Pantheon
    $EFFECT$ = { TRAIT = _yp_diagna }
    $EFFECT$ = { TRAIT = _yp_morwha }
    $EFFECT$ = { TRAIT = _yp_onsi }
    $EFFECT$ = { TRAIT = _yp_ruptga }
    $EFFECT$ = { TRAIT = _yp_satakal }
    $EFFECT$ = { TRAIT = _yp_tava }
    $EFFECT$ = { TRAIT = _yp_tuwhacca }
    $EFFECT$ = { TRAIT = _yp_zeht }
    $EFFECT$ = { TRAIT = _yp_leki }
    $EFFECT$ = { TRAIT = _yp_hoonding }
    # Tribunal Pantheon
    $EFFECT$ = { TRAIT = _tr_vivec }
    $EFFECT$ = { TRAIT = _tr_almalexia }
    $EFFECT$ = { TRAIT = _tr_sotha }
    # Velothi Saints
    $EFFECT$ = { TRAIT = _vl_aralor }
    $EFFECT$ = { TRAIT = _vl_felms }
    $EFFECT$ = { TRAIT = _vl_meris }
    $EFFECT$ = { TRAIT = _vl_nerevar }
    $EFFECT$ = { TRAIT = _vl_olms }
    $EFFECT$ = { TRAIT = _vl_rilms }
    $EFFECT$ = { TRAIT = _vl_seryn }
    $EFFECT$ = { TRAIT = _vl_veloth }
    $EFFECT$ = { TRAIT = _vl_vorys }
    $EFFECT$ = { TRAIT = _vl_llothis }
    # Khajiiti Pantheon
    $EFFECT$ = { TRAIT = _kj_baandar }
    # Forgotten Gods
    $EFFECT$ = { TRAIT = _fg_notorgo }
    $EFFECT$ = { TRAIT = _fg_raen }
    $EFFECT$ = { TRAIT = _fg_vigryl }
    $EFFECT$ = { TRAIT = _fg_shandar }
    $EFFECT$ = { TRAIT = _fg_jhimsei }
    $EFFECT$ = { TRAIT = _fg_druaaga }
    $EFFECT$ = { TRAIT = _fg_qolwen }
    $EFFECT$ = { TRAIT = _fg_sai }
    $EFFECT$ = { TRAIT = _fg_ebonarm }
    $EFFECT$ = { TRAIT = _fg_ius }
    # Magna-Ge
    $EFFECT$ = { TRAIT = _mg_mnemoli }
    $EFFECT$ = { TRAIT = _mg_xerolyg }
    # Reman
    $EFFECT$ = { TRAIT = _rm_light_of_man }
    $EFFECT$ = { TRAIT = _rm_the_cyrodiil }
    $EFFECT$ = { TRAIT = _rm_worldly_god }
    # Trinimac
    $EFFECT$ = { TRAIT = _tm_paragon }
    $EFFECT$ = { TRAIT = _tm_gold_champ }
    $EFFECT$ = { TRAIT = _tm_blade_of_courage }
}

patron_get_list = {
    patron_trait_master_effect = { EFFECT = patron_add_to_list }
}

patron_add_to_list = { #variant of this for local list for ai to select
    if = {
        limit = { patron$TRAIT$_valid = yes }
        #scope:player_s = { add_to_variable_list = { name = faith_patrons target = flag:$TRAIT$ } }
        add_to_variable_list = { name = faith_patrons target = flag:$TRAIT$ }
    }
}

patron_set_trait = { #need 1.4 commands to pass flag/scope:trait from gui
    patron_trait_master_effect = { EFFECT = patron_add_trait }
}

patron_add_trait = { #need 1.4 commands to pass flag/scope:trait from gui
    if = {
        limit = { scope:trait = flag:$TRAIT$ }
        add_trait = p$TRAIT$
        if = {
            limit = { is_ai = no }
            set_variable = { name = recent_patron_selected value = yes years = 5 }
        }
    }
    else = { remove_trait = p$TRAIT$ }
}

#need remove trait before grant. also need to replace the faith change one, also the modifier blocking recent

#this one only clears if the patron is no longer valid so nord cult -> imperial cult keeps akatosh move to master
patron_faith_change_clear_traits = {
    patron_trait_master_effect = { EFFECT = patron_faith_change_clear_trait }
    if = {
        limit = { is_ai = no }
        set_variable = { name = recent_patron_selected value = yes years = 5 }
    }
}

patron_faith_change_clear_trait = {
    if = {
        limit = { faith = { patron$TRAIT$_valid = no } }
        remove_trait = p$TRAIT$
    }
}

patron_clear_traits_effect = { #for when you need to clear all existing patron traits
    if = {
        limit = {
            has_trait_with_flag = patron_trait
        }
        every_character_trait = {
            limit = {
                has_trait_flag = patron_trait
            }
            prev = {
                remove_trait = prev
            }
        }
    }
}

#EK NOTE: Commenting these out since they cause errors, they don't seem to actively be used, and from some testing of them I don't think they actually work as intended anymore.
#patron_get_list_ai = {
#    patron_trait_master_effect = { EFFECT = patron_add_to_list_ai }
#}

#patron_add_to_list_ai = { #variant of this for local list for ai to select
#    if = {
#        limit = { faith = { patron$TRAIT$_valid = yes } }
#        flag:$TRAIT$ = { add_to_list = faith_patrons } #ignore what error.log & ck3-tiger say, this works (in 1.11.5)
#    }
#}

#patron_assign_trait_ai = { #very wierd behaviour in events. it works and assigns removes existing trait then assigns the flag one, but localization says you gain a completely different one. doesnt really matter as the ai wont notice
#    patron_get_list_ai = yes
#    random_in_list = { #can possibly use weight block here to modify chances of selection
#        list = faith_patrons
#        save_scope_as = trait
#    }
#    patron_set_trait = yes
#}