
pie_chart_setup = {
    set_temp_variable = { national_st = 0 }
    set_temp_variable = { national_end = national_pct }


    set_variable = { national_frame = national_st } 
    round_variable = national_frame
    multiply_variable = { national_frame = 1000 }
    add_to_variable = { national_frame = national_end }


    set_temp_variable = { nobilty_st = national_end }
    set_temp_variable = { nobilty_end = nobilty_st }
    add_to_temp_variable = { nobilty_end = nobilty_pct }

    set_variable = { nobilty_frame = nobilty_st } 
    round_variable = nobilty_frame 
    multiply_variable = { nobilty_frame = 1000 }
    add_to_variable = { nobilty_frame = nobilty_end }

    set_temp_variable = { peasants_st = nobilty_end }
    set_temp_variable = { peasants_end = peasants_st }
    add_to_temp_variable = { peasants_end = peasants_pct }

    set_variable = { peasants_frame = peasants_st } 
    round_variable = peasants_frame
    multiply_variable = { peasants_frame = 1000 }
    add_to_variable = { peasants_frame = peasants_end }

	set_temp_variable = { oligarchs_st = peasants_end }
    set_temp_variable = { oligarchs_end = oligarchs_st }
    add_to_temp_variable = { oligarchs_end = oligarchs_pct }

    set_variable = { oligarchs_frame = oligarchs_st } 
    round_variable = oligarchs_frame
    multiply_variable = { oligarchs_frame = 1000 }
    add_to_variable = { oligarchs_frame = oligarchs_end }
}

pie_chart_update = {
	custom_effect_tooltip = RUS_reconstruction_region_emigrate_tt # auto generated tooltip

    set_temp_variable = { temp_national_pct = national_pct } # pulls percentage value of region population types
    set_temp_variable = { temp_nobilty_pct = nobilty_pct }
    set_temp_variable = { temp_peasants_pct = peasants_pct }
    set_temp_variable = { temp_oligarchs_pct = oligarchs_pct }

    divide_temp_variable = { temp_national_pct = 100 } # converts into actual percentile values
    divide_temp_variable = { temp_nobilty_pct = 100 } 
    divide_temp_variable = { temp_peasants_pct = 100 } 
    divide_temp_variable = { temp_oligarchs_pct = 100 } 

    multiply_temp_variable = { temp_national_pct_add = temp_national_pct } # adds desired percentage values
    multiply_temp_variable = { temp_nobilty_pct_add = temp_nobilty_pct }
    multiply_temp_variable = { temp_peasants_pct_add = temp_peasants_pct }
    multiply_temp_variable = { temp_oligarchs_pct_add = temp_oligarchs_pct }

    add_to_variable = { national_pct = temp_national_pct_add }
    subtract_from_variable = { rus_muscovy_germanpop = temp_pop_remove }
    add_to_variable = { nobilty_pct = temp_nobilty_pct_add }
    add_to_variable = { peasants_pct = temp_peasants_pct }
    add_to_variable = { oligarchs_pct = temp_oligarchs_pct }
    set_temp_variable = { temp_pop_100_checker = temp_pop_remove }
    set_temp_variable = { temp_pop_delta = temp_national_pct_add }
    set_temp_variable = { temp_pop_delta = temp_nobilty_pct_add }
    set_temp_variable = { temp_pop_delta = temp_peasants_pct }
    set_temp_variable = { temp_pop_delta = temp_oligarchs_pct }


    ## Rounds all variables, to make sure the delta is below 100 if the numbers wouldn't add up on the pie chart

    round_variable = national_pct
    round_variable = rus_muscovy_germanpop
    round_variable = nobilty_pct
    round_variable = peasants_pct
    round_variable = oligarchs_pct

    ## Calculate the difference between the rounded variables and 100

    set_temp_variable = { temp_pop_pct = 100 }
    set_temp_variable = { temp_pop_delta = national_pct }
    add_to_temp_variable = { temp_pop_delta = rus_muscovy_germanpop }
    add_to_temp_variable = { temp_pop_delta = nobilty_pct }
    add_to_temp_variable = { temp_pop_delta = peasants_pct }
    add_to_temp_variable = { temp_pop_delta = oligarchs_pct }

    if = { # Add the difference back to Russian pops (largest)
        limit = {
            check_variable = { temp_pop_delta < 100 }
        }
        subtract_from_temp_variable = { temp_pop_pct = temp_pop_delta }
        add_to_variable = { national_pct = temp_pop_pct }
    }
}
