## HRE scripted effects ##

# BASE = 50.00
#
#   CENTRALISATION AND COUNCIL LAWS +- AUTH CAP ->
# FULL COUNCIL POWER:25.00 -> PATH TO GERMAN FEDERATION???
# PER EXTRA COUNCIL AND CENTRALISATION LAW:+5.00 ???
# MAX COUNCIL POWER: 80.00 -> VASSALS MORE MAD THAN USUAL???
# MAX CENTRALISATION:100.00
# 
#   BI-YEARLY ->
# IMPERIAL REGALIA:0.25 (REICHSKRONE) - global_hre_reichskrone
# DEJURE VASSALS OUTSIDE REALM:-3.00*VASSAL OUTSIDE -1.00 IF TRIBUTE - global_hre_dejure_debuff
# CONTROL IN ITALIA:0.50 (HOLDS ATLEAST X AMOUNT OF ITALIA) - global_hre_italia_count / global_hre_italia
# CONTROL IN ARLES:0.50 (HOLD ATLEAST X AMOUNT OF ARLES) - global_hre_arles_count / global_hre_arles
# FREE INVESTITURE:1.00 - global_hre_investiture
# ANTI-POPE:0.50 - global_hre_antipope
# PAPAL INVESTITURE:0 - global_hre_investiture
# HERETIC COUNTIES:-0.25/COUNTY - global_hre_heresy # HERESY ALSO INCLUDES OTHER DENOMINATIONS (ORTHODOX/NESTORIAN/MIAHPHYSITE)
# HERETIC VASSALS:-0.10 COUNT / -0.25 DUKE / -1.00 KING - global_hre_heresy - (ONLY COUNT IF EMPEROR'S RELIGION ISN'T HERESY??? (e.g. fraticelli is heresy to catholic)) 
# TRIBUTARIES:0.2/TRIBUTE - global_hre_tributaries
# VASSAL COUNTS:0.02 - global_hre_vassals
# VASSAL DUKES:0.05 - global_hre_vassals
# VASSAL KINGS:0.10 - global_hre_vassals
# CRUSADER TRAIT:0.50 (?) - global_hre_crusader
# STATE DIPLOMACY:STATE_DIPLOMACY/20 (8?) - global_hre_state_diplomacy
# WIFE IS LOWBORN: -2.00 - global_hre_lowborn_spouse
# REGENCY: -3.00 - global_hre_regency
# EXCOMMUNICATED: -5.00 - global_hre_excommunication
# 
# COUNCIL POWER: FROM 2.50 TO -2.5? -5.00 TO 0? - global_hre_council
# PRESTIGE:1K PRESTIGE/0.5 MAX 5K PRESTIGE 1000/2000=0.5 - global_hre_prestige
# CROWNED: -2 UNCROWNED, -1 BAD CORONATION, 0 OK, 0.5 POPE CORONATION - global_hre_coronation
#
#   ACTIONS ->
# LOSE WAR:-5.00 - HRE.100 HRE.101
# WIN WAR:2.00 (EXCLUDES PEASANT WARS) - HRE.102 HRE.103
# GHIBBELINES WIN GGH WAR:15.00 -
# GUELPHS WIN:-15.00 -
# LOST ANTI-PAPAL/ANTI-KING WAR:-20 -
# NEW EMPEROR:-5.00 - inside grant_hre_trait
# CHANGE COUNCIL LAW: +5.00 / -10.00 - VIA laws/ze_council_power_laws

setup_imperial_authority = {
    set_variable = {
        which = global_imperial_auth_hre
        value = 50 #base
    }
}

grant_hre_trait = {
    change_variable = { # new emperor
        which = global_imperial_auth_hre
        value = -5
    }
    if = {
        limit = {
            has_hre_trait = yes
        }
        remove_hre_trait = yes
    }
    if = {
        limit = {
            check_variable = {
                which = global_imperial_auth_hre
                value < 20
            }
        }
        add_trait = emperor_hre_very_low
    }
    else_if = {
        limit = {
            check_variable = {
                which = global_imperial_auth_hre
                value >= 20
            }
            check_variable = {
                which = global_imperial_auth_hre
                value < 40
            }
        }
        add_trait = emperor_hre_low
    }
    else_if = {
        limit = {
            check_variable = {
                which = global_imperial_auth_hre
                value >= 40
            }
            check_variable = {
                which = global_imperial_auth_hre
                value < 60
            }
        }
        add_trait = emperor_hre_average
    }
    else_if = {
        limit = {
            check_variable = {
                which = global_imperial_auth_hre
                value >= 60
            }
            check_variable = {
                which = global_imperial_auth_hre
                value < 80
            }
        }
        add_trait = emperor_hre_high
    }
    else_if = {
        limit = {
            check_variable = {
                which = global_imperial_auth_hre
                value >= 80
            }
        }
        add_trait = emperor_hre_very_high
    }
}

remove_hre_trait = {
    remove_trait = emperor_hre_very_low
    remove_trait = emperor_hre_low
    remove_trait = emperor_hre_average
    remove_trait = emperor_hre_high
    remove_trait = emperor_hre_very_high
}

check_imperial_authority = {
    if = {
        limit = {
            any_artifact = {
                artifact_type = crown_hre
                is_artifact_equipped = yes
            }
        }
        set_variable = {
            which = global_hre_reichskrone
            value = 0.25
        }
    }
    else = {
        set_variable = {
            which = global_hre_reichskrone
            value = 0
        }
    }

    set_variable = { #reset value
        which = global_hre_dejure_debuff
        value = 0
    }
    e_hre = {
        any_de_jure_vassal_title = {
            limit = { 
                real_tier = COUNT 
                holder_scope = { 
                    NOR = { 
                        is_vassal_or_below_of = ROOT 
                        character = ROOT # not the emperor himself
                    }
                }
            }
            
            if = {
                limit = {
                    holder_scope = {
                        OR = {
                            is_tributary = {
                                suzerain = ROOT
                            }
                            any_liege = {
                                is_tributary = {
                                    suzerain = ROOT
                                }
                            }
                        }
                    }
                }
                log = "[This.GetName] is not a part of the HRE, but (their top liege) is a tributary"
                change_variable = {
                    which = global_hre_dejure_debuff
                    value = -0.25
                }
            }
            else = {
                log = "[This.GetName] is not a part of the HRE"
                change_variable = {
                    which = global_hre_dejure_debuff
                    value = -0.75
                }
            }
        }
    }
    

    set_variable = {
        which = global_hre_italia_count
        value = 0
    }
    e_italy = {
        any_de_jure_vassal_title = {
            limit = {
                real_tier = COUNT
                owner = { top_liege = { character = ROOT } }
            }
            change_variable = {
                which = global_hre_italia_count
                value = 1 #Hold x% of italia?
            }
        }
    }
    if = {
        limit = {
            check_variable = {
                which = global_hre_italia_count
                value = 20 #placeholder!!!!!!!!!!!!!!!
            }
        }
        set_variable = {
            which = global_hre_italia
            value = 0.5
        }
    }
    else = {
        set_variable = {
            which = global_hre_italia
            value = 0
        }
    }

    set_variable = {
        which = global_hre_arles_count
        value = 0
    }
    k_burgundy = {
        any_de_jure_vassal_title = {
            limit = {
                real_tier = COUNT
                owner = { top_liege = { character = ROOT }  }
            }
            change_variable = {
                which = global_hre_arles_count
                value = 1
            }
        }
    }
    if = {
        limit = {
            check_variable = {
                which = global_hre_arles_count
                value = 15 #placeholder!!!!!!!!!!!!!!!
            }
        }
        set_variable = {
            which = global_hre_arles
            value = 0.5
        }
    }
    else = {
        set_variable = {
            which = global_hre_arles
            value = 0
        }
    }

    if = {
        limit = {
            has_law = investiture_law_1
        }
        set_variable = {
            which = global_hre_investiture
            value = 1
        }
    }
    else_if = {
        limit = {
            has_law = investiture_law_0
        }
        set_variable = {
            which = global_hre_investiture
            value = 0
        }
    }

    if = {
        limit = {
            NOT = { religion_head = { character = rightful_religious_head_scope } }
        }
        set_variable = {
            which = global_hre_antipope
            value = 0.5
        }
    }
    else = {
        set_variable = {
            which = global_hre_antipope
            value = 0
        }
    }


    set_variable = {
        which = global_hre_heresy
        value = 0
    }
    if = {
        any_title_under = {
            limit = {
                real_tier = COUNT
                location = {
                    NOT = { religion = ROOT }
                    religion_group = ROOT
                }
                ROOT = { religion = { is_heretic = no } }
            }
            change_variable = {
                which = global_hre_heresy
                value = -0.25
            }
        }
    }

    if = {
        limit = {
            any_realm_character = {
                NOT = { religion = ROOT }
                religion_group = ROOT
            }
        }
        any_realm_character = {
            primary_title = {
                trigger_switch = {
                    on_trigger = tier
                    KING = {
                        change_variable = {
                            which = global_hre_heresy
                            value = -1
                        }
                    }
                    DUKE = {
                        change_variable = {
                            which = global_hre_heresy
                            value = -0.25
                        }
                    }
                    COUNT = {
                        change_variable = {
                            which = global_hre_heresy
                            value = -0.1
                        }
                    }
                }
            }
        }
    }

    set_variable = {
        which = global_hre_tributaries
        value = 0
    }

    any_tributary = {
        change_variable = {
            which = global_hre_tributaries
            value = 0.2 #low to prevent meta? too low?
        }
    }

    set_variable = {
        which = global_hre_vassals
        value = 0
    }
    any_vassal = {
        primary_title = {
            trigger_switch = {
                on_trigger = real_tier
                KING = {
                    change_variable = {
                        which = global_hre_vassals
                        value = 0.10
                    }
                }
                DUKE = {
                    change_variable = {
                        which = global_hre_vassals
                        value = 0.05
                    }
                }
                COUNT = {
                    change_variable = {
                        which = global_hre_vassals
                        value = 0.02
                    }
                }
            }
        }
    }

    if = {
        limit = {
            trait = crusader
        }
        set_variable = {
            which = global_hre_crusader
            value = 0.5
        }
    }
    else = {
        set_variable = {
            which = global_hre_crusader
            value = 0
        }
    }

    set_variable = {
        which = global_hre_state_diplomacy
        value = 0
    }
    export_to_variable = {
        which = global_hre_state_diplomacy
        value = realm_diplomacy
    }
    divide_variable = {
        which = global_hre_state_diplomacy
        value = 20
    }

    if = {
        limit = {
            any_spouse = { dynasty = none }
        }
        set_variable = {
            which = global_hre_lowborn_spouse
            value = -2
        }
    }
    else = {
        set_variable = {
            which = global_hre_lowborn_spouse
            value = 0
        }
    }


    set_variable = {
        which = global_hre_council
        value = 0
    }
    if = {
        limit = { has_law = succession_voting_power_1 }
        change_variable = {
            which = global_hre_council
            value = -1.5
        }
    }
    if = {
        limit = { has_law = execution_voting_power_1 }
        change_variable = {
            which = global_hre_council
            value = -0.5
        }
    }
    if = {
        limit = { has_law = war_voting_power_1 }
        change_variable = {
            which = global_hre_council
            value = -0.5
        }
    }
    if = {
        limit = { has_law = banish_voting_power_1 }
        change_variable = {
            which = global_hre_council
            value = -0.5
        }
    }
    if = {
        limit = { has_law = revoke_title_voting_power_1 }
        change_variable = {
            which = global_hre_council
            value = -0.5
        }
    }
    if = {
        limit = { has_law = imprison_voting_power_1 }
        change_variable = {
            which = global_hre_council
            value = -0.5
        }
    }
    if = {
        limit = { has_law = law_voting_power_1 }
        change_variable = {
            which = global_hre_council
            value = -0.5
        }
    }
    if = {
        limit = { has_law = grant_title_voting_power_1 }
        change_variable = {
            which = global_hre_council
            value = -0.5
        }
    }

    set_variable = {
        which = global_hre_prestige
        value = 0
    }
    export_to_variable = {
        which = global_hre_prestige
        value = prestige
    }
    divide_variable = {
        which = global_hre_prestige
        value = 2500
    }
    if = {
        limit = {
            check_variable = {
                which = global_hre_prestige
                value > 2
            }
        }
        set_variable = {
            which = global_hre_prestige
            value = 2
        }
    }

    if = {
        limit = {
            OR = {
                religion = catholic
                religion = fraticelli
            }
        }
        trigger_switch = {
            on_trigger = trait
            crowned_by_priest = {
                set_variable = {
                    which = global_hre_coronation
                    value = -1
                }
            }
            crowned_by_bishop = {
                set_variable = {
                    which = global_hre_coronation
                    value = 0
                }
            }
            crowned_by_pope = {
                set_variable = {
                    which = global_hre_coronation
                    value = 0.5
                }
            }
            crowned_by_myself = {
                set_variable = {
                    which = global_hre_coronation
                    value = 1
                }
            }
            fallback = { # no coronation trait
                set_variable = {
                    which = global_hre_coronation
                    value = -2
                }
            }
        }
    }

    set_variable = {
        which = global_hre_regency
        value = 0
    }
    if = {
        limit = {
            has_regent = yes
        }
        set_variable = {
            which = global_hre_regency
            value = -3
        }
    }

    set_variable = {
        which = global_hre_excommunication
        value = 0
    }
    if = {
        limit = {
            trait = excommunicated
        }
        set_variable = {
            which = global_hre_excommunication
            value = -5
        }
    }

    addition_hre = yes
}

addition_hre = {
    change_variable = {
        which = global_imperial_auth_hre
        which = global_hre_reichskrone
    }
    change_variable = {
        which = global_imperial_auth_hre
        which = global_hre_dejure_debuff
    }
    change_variable = {
        which = global_imperial_auth_hre
        which = global_hre_italia
    }
    change_variable = {
        which = global_imperial_auth_hre
        which = global_hre_arles
    }
    change_variable = {
        which = global_imperial_auth_hre
        which = global_hre_investiture
    }
    change_variable = {
        which = global_imperial_auth_hre
        which = global_hre_heresy
    }
    change_variable = {
        which = global_imperial_auth_hre
        which = global_hre_tributaries
    }
    change_variable = {
        which = global_imperial_auth_hre
        which = global_hre_vassals
    }
    change_variable = {
        which = global_imperial_auth_hre
        which = global_hre_crusader
    }
    change_variable = {
        which = global_imperial_auth_hre
        which = global_hre_state_diplomacy
    }
    change_variable = {
        which = global_imperial_auth_hre
        which = global_hre_lowborn_spouse
    }
    change_variable = {
        which = global_imperial_auth_hre
        which = global_hre_state_diplomacy
    }
    change_variable = {
        which = global_imperial_auth_hre
        which = global_hre_prestige
    }
    change_variable = {
        which = global_imperial_auth_hre
        which = global_hre_coronation
    }
    change_variable = {
        which = global_imperial_auth_hre
        which = global_hre_regency
    }
    change_variable = {
        which = global_imperial_auth_hre
        which = global_hre_excommunication
    }
    
    divide_variable = { #remove stupid decimals >:( (looks ugly) also rounds down automatically - save seperate variable with decimals?
        which = global_imperial_auth_hre
        value = 1000
    }
    multiply_variable = {
        which = global_imperial_auth_hre
        value = 1000
    }
    
    if = { #global_hre_council is great way to count council laws
        limit = {
            check_variable = {
                which = global_hre_council
                value == 0
            }
            check_variable = {
                which = global_imperial_auth_hre
                value = 100
            }
        }
        set_variable = {
            which = global_imperial_auth_hre
            value = 100
        }
    }
    else_if = {
        limit = {
            check_variable = {
                which = global_hre_council
                value == -0.5
            }
            check_variable = {
                which = global_imperial_auth_hre
                value = 90
            }
        }
        set_variable = {
            which = global_imperial_auth_hre
            value = 90
        }
    }
    else_if = {
        limit = {
            check_variable = {
                which = global_hre_council
                value == -1
            }
            check_variable = {
                which = global_imperial_auth_hre
                value = 80
            }
        }
        set_variable = {
            which = global_imperial_auth_hre
            value = 80
        }
    }
    else_if = {
        limit = {
            check_variable = {
                which = global_hre_council
                value == -1.5
            }
            check_variable = {
                which = global_imperial_auth_hre
                value = 70
            }
        }
        set_variable = {
            which = global_imperial_auth_hre
            value = 70
        }
    }
    else_if = {
        limit = {
            check_variable = {
                which = global_hre_council
                value == -2
            }
            check_variable = {
                which = global_imperial_auth_hre
                value = 60
            }
        }
        set_variable = {
            which = global_imperial_auth_hre
            value = 60
        }
    }
    else_if = {
        limit = {
            check_variable = {
                which = global_hre_council
                value <= -2.5
            }
            check_variable = {
                which = global_imperial_auth_hre
                value = 50
            }
        }
        set_variable = {
            which = global_imperial_auth_hre
            value = 50
        }
    }
}