TR_AI_PP_store = {

	### find the highest law cost and store enough pp for it

	set_variable = { TR_AI_PP_max_stored_pp = 1 }

	set_temp_variable = { TR_AI_eco_law_cost = modifier@economy_cost_factor }
	add_to_temp_variable = { TR_AI_eco_law_cost = 1 }
	multiply_temp_variable = { TR_AI_eco_law_cost = 150 }

	set_temp_variable = { TR_AI_man_law_cost = modifier@mobilization_laws_cost_factor }
	add_to_temp_variable = { TR_AI_man_law_cost = 1 }
	multiply_temp_variable = { TR_AI_man_law_cost = 150 }

	if = { limit = { check_variable = { TR_AI_eco_law_cost > TR_AI_PP_max_stored_pp } } set_variable = { TR_AI_PP_max_stored_pp = TR_AI_eco_law_cost } }
	if = { limit = { check_variable = { TR_AI_man_law_cost > TR_AI_PP_max_stored_pp } } set_variable = { TR_AI_PP_max_stored_pp = TR_AI_man_law_cost } }

	clamp_variable = { var = TR_AI_PP_max_stored_pp min = 100 } # For decisions

	### Special modifiers

	if = {
		limit = {
			tag = FIN
			NOT = { has_idea = war_economy has_idea = all_adults_serve }
			OR = {
				TR_AI_LAW_wants_war_economy = yes
				TR_AI_LAW_wants_all_adults_serve = yes
			}
		}

		set_variable = { TR_AI_PP_max_stored_pp = TR_AI_man_law_cost }
		add_to_variable = { TR_AI_PP_max_stored_pp = TR_AI_eco_law_cost }
		add_to_variable = { TR_AI_PP_max_stored_pp = 350 }
	}

	###

    if = {
		limit = {
			has_political_power > 30
			check_variable = { TR_AI_PP_stored_pp < TR_AI_PP_max_stored_pp }
			OR = {
				TR_AI_LAW_wants_economy_law = yes
				TR_AI_LAW_wants_manpower_law = yes
			}
		}

		set_temp_variable = { add_pp = TR_AI_PP_max_stored_pp }
		subtract_from_temp_variable = { add_pp = TR_AI_PP_stored_pp }
		set_temp_variable = { add_pp_max = political_power }
		subtract_from_temp_variable = { add_pp_max = 5 }
		clamp_temp_variable = { var = add_pp min = 0 max = add_pp_max }

		if = { limit = { has_country_flag = TR_AI_pp_logging } log = "[GetYear] [GetMonth] | AI | [Root.GetName] | PP: PP = [?political_power], storing = [?add_pp], TR_AI_PP_stored_pp = [?TR_AI_PP_stored_pp], TR_AI_PP_max_stored_pp = [?TR_AI_PP_max_stored_pp]" } 
		add_to_variable = { TR_AI_PP_stored_pp = add_pp }
		multiply_temp_variable = { add_pp = -1 }
		add_political_power = add_pp
	}
	else_if = { limit = { check_variable = { TR_AI_PP_stored_pp > TR_AI_PP_max_stored_pp } }

		set_temp_variable = { return_pp = TR_AI_PP_stored_pp }
		subtract_from_temp_variable = { return_pp = TR_AI_PP_max_stored_pp }
		subtract_from_variable = { TR_AI_PP_stored_pp = return_pp }
		add_political_power = return_pp
	}
	else_if = { limit = { check_variable = { political_power < 0 } check_variable = { TR_AI_PP_stored_pp > 0 } }
	
		set_temp_variable = { return_pp = political_power }
		multiply_temp_variable = { return_pp = -1 }
		clamp_temp_variable = { var = return_pp min = 0 max = TR_AI_PP_stored_pp }
		subtract_from_variable = { TR_AI_PP_stored_pp = return_pp }
		add_political_power = return_pp
	}

}

TR_AI_PP_return_stored = {

	if = { limit = { has_country_flag = TR_AI_pp_logging } log = "[GetYear] [GetMonth] | AI | [Root.GetName] | PP: returning [?TR_AI_PP_stored_pp], PP = [?political_power]" } 

	add_political_power = TR_AI_PP_stored_pp
	set_variable = { TR_AI_PP_stored_pp = 0 }
}


TR_AI_PP_pay_pp_cost = {

	TR_AI_PP_store = yes
	if = { limit = { check_variable = { _pp_cost > 0 } }
		multiply_temp_variable = { _pp_cost = -1 }
	}
	if = { limit = { has_country_flag = TR_AI_pp_logging } log = "[GetYear] [GetMonth] | AI | [Root.GetName] | TR_AI_PP_pay_pp_cost: _pp_cost=[?_pp_cost]" } 
	add_political_power = _pp_cost
	TR_AI_PP_return_stored = yes
}