# Power ranking effects by Yard1

# THIS is whatever
# Do the ranking - fired every month
set_power_ranking_ideas_for_every_country = {
	get_global_factories = yes
	determine_regional_powers = yes
	every_country = {
		get_percentage_of_global_factories = yes
		determine_power_ranking = yes
	}
}

# THIS is whatever
# Sum of world's factories (civ, mil, nav) saved in global_num_of_factories variable
get_global_factories = {
	clear_array = global.european_nations
	clear_array = global.sub_saharan_nations
	clear_array = global.middle_eastern_nations
	clear_array = global.asian_nations
	clear_array = global.american_nations
	set_variable = { global.global_num_of_factories = 0 }
	every_country = {
		clr_country_flag = regional_power
		set_variable = { global.tmp = num_of_factories }
		multiply_variable = { global.tmp = domestic_influence_amount_calc?0.5 }
		add_to_variable = { global.global_num_of_factories = global.tmp }
		if = { 
			limit = { is_european_nation = yes }
			set_variable = { global.idx = global.european_nations^num }
			for_each_loop = {
				array = global.european_nations
				if = {
					limit = {
						var:v = {
							set_temp_variable = { num_of_factories_tmp = num_of_factories }
							multiply_temp_variable = { num_of_factories_tmp = domestic_influence_amount_calc?0.5 }
							check_variable = { global.tmp > num_of_factories_tmp }
						}
					}
					set_variable = { global.idx = i }
					set_temp_variable = { break = 1 }
				}
			}
			add_to_array = {
				array = global.european_nations
				value = THIS
				index = global.idx
			}
		}
		else_if = { 
			limit = { is_sub_saharan_nation = yes }
			set_variable = { global.idx = global.sub_saharan_nations^num }
			for_each_loop = {
				array = global.sub_saharan_nations
				if = {
					limit = {
						var:v = {
							set_temp_variable = { num_of_factories_tmp = num_of_factories }
							multiply_temp_variable = { num_of_factories_tmp = domestic_influence_amount_calc?0.5 }
							check_variable = { global.tmp > num_of_factories_tmp }
						}
					}
					set_variable = { global.idx = i }
					set_temp_variable = { break = 1 }
				}
			}
			add_to_array = {
				array = global.sub_saharan_nations
				value = THIS
				index = global.idx
			}
		}
		else_if = { 
			limit = { is_middle_eastern_nation = yes }
			set_variable = { global.idx = global.middle_eastern_nations^num }
			for_each_loop = {
				array = global.middle_eastern_nations
				if = {
					limit = {
						var:v = {
							set_temp_variable = { num_of_factories_tmp = num_of_factories }
							multiply_temp_variable = { num_of_factories_tmp = domestic_influence_amount_calc?0.5 }
							check_variable = { global.tmp > num_of_factories_tmp }
						}
					}
					set_variable = { global.idx = i }
					set_temp_variable = { break = 1 }
				}
			}
			add_to_array = {
				array = global.middle_eastern_nations
				value = THIS
				index = global.idx
			}
		}
		else_if = { 
			limit = { is_asian_nation = yes }
			set_variable = { global.idx = global.asian_nations^num }
			for_each_loop = {
				array = global.asian_nations
				if = {
					limit = {
						var:v = {
							set_temp_variable = { num_of_factories_tmp = num_of_factories }
							multiply_temp_variable = { num_of_factories_tmp = domestic_influence_amount_calc?0.5 }
							check_variable = { global.tmp > num_of_factories_tmp }
						}
					}
					set_variable = { global.idx = i }
					set_temp_variable = { break = 1 }
				}
			}
			add_to_array = {
				array = global.asian_nations
				value = THIS
				index = global.idx
			}
		}
		else_if = { 
			limit = { is_american_nation = yes }
			set_variable = { global.idx = global.american_nations^num }
			for_each_loop = {
				array = global.american_nations
				if = {
					limit = {
						var:v = {
							set_temp_variable = { num_of_factories_tmp = num_of_factories }
							multiply_temp_variable = { num_of_factories_tmp = domestic_influence_amount_calc?0.5 }
							check_variable = { global.tmp > num_of_factories_tmp }
						}
					}
					set_variable = { global.idx = i }
					set_temp_variable = { break = 1 }
				}
			}
			add_to_array = {
				array = global.american_nations
				value = THIS
				index = global.idx
			}
		}
	}
	clear_variable = global.idx
	clear_variable = global.tmp
}

# THIS is whatever
# Determine which countries are eligible to become a regional power
determine_regional_powers = {
	set_temp_variable = {
		end_idx = global.european_nations^num
	}
	if = {
		limit = { check_variable = { end_idx > 7 } }
		set_temp_variable = { end_idx = 7 }
	}
	for_loop_effect = {
		start = 0
		end = end_idx
		value = i
		var:global.european_nations^i = { set_country_flag = regional_power }
	}

	set_temp_variable = {
		end_idx = global.sub_saharan_nations^num
	}
	if = {
		limit = { check_variable = { end_idx > 3 } }
		set_temp_variable = { end_idx = 3 }
	}
	for_loop_effect = {
		start = 0
		end = end_idx
		value = i
		var:global.sub_saharan_nations^i = { set_country_flag = regional_power }
	}

	set_temp_variable = {
		end_idx = global.middle_eastern_nations^num
	}
	if = {
		limit = { check_variable = { end_idx > 3 } }
		set_temp_variable = { end_idx = 3 }
	}
	for_loop_effect = {
		start = 0
		end = end_idx
		value = i
		var:global.middle_eastern_nations^i = { set_country_flag = regional_power }
	}

	set_temp_variable = {
		end_idx = global.asian_nations^num
	}
	if = {
		limit = { check_variable = { end_idx > 7 } }
		set_temp_variable = { end_idx = 7 }
	}
	for_loop_effect = {
		start = 0
		end = end_idx
		value = i
		var:global.asian_nations^i = { set_country_flag = regional_power }
	}

	set_temp_variable = {
		end_idx = global.american_nations^num
	}
	if = {
		limit = { check_variable = { end_idx > 4 } }
		set_temp_variable = { end_idx = 4 }
	}
	for_loop_effect = {
		start = 0
		end = end_idx
		value = i
		var:global.american_nations^i = { set_country_flag = regional_power }
	}
}

#for debug purposes
print_region_arrays = {
	log = "EUROPEAN NATIONS"
	for_each_scope_loop = {
		array = global.european_nations
		log = "[?this.GetName]: [?this.num_of_factories] infl [?this.domestic_influence_amount_calc]"
	}
	log = "SUB SAHARAN NATIONS"
	for_each_scope_loop = {
		array = global.sub_saharan_nations
		log = "[?this.GetName]: [?this.num_of_factories] infl [?this.domestic_influence_amount_calc]"
	}
	log = "MIDDLE EASTERN NATIONS"
	for_each_scope_loop = {
		array = global.middle_eastern_nations
		log = "[?this.GetName]: [?this.num_of_factories] infl [?this.domestic_influence_amount_calc]"
	}
	log = "ASIAN NATIONS"
	for_each_scope_loop = {
		array = global.asian_nations
		log = "[?this.GetName]: [?this.num_of_factories] infl [?this.domestic_influence_amount_calc]"
	}
	log = "AMERICAN NATIONS"
	for_each_scope_loop = {
		array = global.american_nations
		log = "[?this.GetName]: [?this.num_of_factories] infl [?this.domestic_influence_amount_calc]"
	}
}

# THIS is a country
# Calculates what percentage of global factories the country's factories (civ, mil, nav) make up
# Saved in percentage_of_global_factories variable
get_percentage_of_global_factories = {
	set_variable = { percentage_of_global_factories = num_of_factories }
	multiply_variable = { percentage_of_global_factories = domestic_influence_amount_calc?0.5 }
	divide_variable = { percentage_of_global_factories = global.global_num_of_factories }
}

# THIS is a country
# Determine what kind of power they are
determine_power_ranking = {
	remove_ideas = {
		superpower
		great_power
		large_power
		regional_power
		minor_power
		non_power
		
		superpower_hidden
		great_power_hidden
		large_power_hidden
		regional_power_hidden
		non_power_hidden
	}
	if = {
		limit = {
			check_variable = { percentage_of_global_factories < 0.002 }
		}
		add_ideas = non_power
		add_ideas = non_power_hidden
		set_variable = { influence_multiplier = 0.75 }
	}
	else_if = {
		limit = {
			check_variable = { percentage_of_global_factories > 0.08 }
		}
		add_ideas = superpower
		add_ideas = superpower_hidden
		set_variable = { influence_multiplier = 2 }
	}
	else_if = {
		limit = {
			check_variable = { percentage_of_global_factories > 0.035 }
		}
		add_ideas = great_power
		add_ideas = great_power_hidden
		set_variable = { influence_multiplier = 1.6 }
	}
	else_if = {
		limit = {
			check_variable = { percentage_of_global_factories > 0.018 }
		}
		add_ideas = large_power
		add_ideas = large_power_hidden
		set_variable = { influence_multiplier = 1.4 }
	}
	else_if = {
		limit = {
			has_country_flag = regional_power
		}
		add_ideas = regional_power
		add_ideas = regional_power_hidden
		clr_country_flag = regional_power
		set_variable = { influence_multiplier = 1.2 }
	}
	else_if = {
		add_ideas = minor_power
		set_variable = { influence_multiplier = 1.0 }
	}
}