## Contents
# set_leader = yes
# update_set_politics = yes
# set_correct_ideology_trait = yes
# remove_set_ideology_flag = yes
# set_failsafe_values = yes #for general politics
# set_failsafe_change_leader = yes #only for on_action



#Main Function


update_set_politics = {
	#western
	if = {
		limit = {
			OR = {
				has_country_flag = set_conservatism
				has_country_flag = set_liberalism
				has_country_flag = set_socialism
				has_country_flag = set_Western_Autocracy
			}
		}
		if = { limit = { has_elections = yes }
			hidden_effect = {
				set_politics = { 
					ruling_party = democratic
					elections_allowed = yes
				}
			}
		}
		else_if = { limit = { has_elections = no }
			hidden_effect = {
				set_politics = { 
					ruling_party = democratic
					elections_allowed = no
				}
			}
		}
		hidden_effect = {
			meta_effect = {
				text = {
					set_party_name = { 
						ideology = [OUTLOOK]
						name = [SUBIDEOLOGY_NAME]
						long_name = [SUBIDEOLOGY_NAME]
					}
				}
				OUTLOOK = "[meta_ideology_names]"
				SUBIDEOLOGY_NAME = "[show_ruling_party]"
			}
		}
	}
	#Emerging
	else_if = {
		limit = {
			OR = {
				has_country_flag = set_Communist-State
				has_country_flag = set_Conservative
				has_country_flag = set_Autocracy
				has_country_flag = set_Vilayat_e_Faqih
				has_country_flag = set_Mod_Vilayat_e_Faqih
				has_country_flag = set_anarchist_communism
			}
		}
		if = { limit = { has_elections = yes }
			hidden_effect = {
				set_politics = {
					ruling_party = communism
					elections_allowed = yes
				}
			}
		}
		else_if = { limit = { has_elections = no }
			hidden_effect = {
				set_politics = { 
					ruling_party = communism
					elections_allowed = no
				}
			}
		}
		hidden_effect = {
			meta_effect = {
				text = {
					set_party_name = { 
						ideology = [OUTLOOK]
						name = [SUBIDEOLOGY_NAME]
						long_name = [SUBIDEOLOGY_NAME]
					}
				}
				OUTLOOK = "[meta_ideology_names]"
				SUBIDEOLOGY_NAME = "[show_ruling_party]"
			}
		}
	}
	#Non-Aligned
	else_if = {
		limit = {
			OR = {
				has_country_flag = set_Neutral_conservatism
				has_country_flag = set_oligarchism
				has_country_flag = set_neutral_Social
				has_country_flag = set_Neutral_Libertarian
				has_country_flag = set_Neutral_Autocracy
				has_country_flag = set_Neutral_Communism
				has_country_flag = set_Neutral_Muslim_Brotherhood
				has_country_flag = set_Neutral_green
			}
		}
		if = { limit = { has_elections = yes }
			hidden_effect = {
				set_politics = { 
					ruling_party = neutrality
					elections_allowed = yes
				}
			}
		}
		else_if = { limit = { has_elections = no }
			hidden_effect = {
				set_politics = { 
					ruling_party = neutrality
					elections_allowed = no
				}
			}
		}
		hidden_effect = {
			meta_effect = {
				text = {
					set_party_name = { 
						ideology = [OUTLOOK]
						name = [SUBIDEOLOGY_NAME]
						long_name = [SUBIDEOLOGY_NAME]
					}
				}
				OUTLOOK = "[meta_ideology_names]"
				SUBIDEOLOGY_NAME = "[show_ruling_party]"
			}
		}
	}
	#Salafist
	else_if = {
		limit = {
			OR = {
				has_country_flag = set_Kingdom
				has_country_flag = set_Caliphate
			}
		}
		hidden_effect = {
			set_politics = { 
				ruling_party = fascism
				elections_allowed = no
			}
			meta_effect = {
				text = {
					set_party_name = { 
						ideology = [OUTLOOK]
						name = [SUBIDEOLOGY_NAME]
						long_name = [SUBIDEOLOGY_NAME]
					}
				}
				OUTLOOK = "[meta_ideology_names]"
				SUBIDEOLOGY_NAME = "[show_ruling_party]"
			}
		}
	}
	#Nationalist
	else_if = {
		limit = {
			OR = {
				has_country_flag = set_Nat_Autocracy
				has_country_flag = set_Nat_Fascism
				has_country_flag = set_Nat_Populism
				has_country_flag = set_Monarchist
			}
		}
		if = { limit = { has_elections = yes }
			hidden_effect = {
				set_politics = { 
					ruling_party = nationalist
					elections_allowed = yes
				}
			}
		}
		else_if = { limit = { has_elections = no }
			hidden_effect = {
				set_politics = { 
					ruling_party = nationalist
					elections_allowed = no
				}
			}
		}
		hidden_effect = {
			meta_effect = {
				text = {
					set_party_name = { 
						ideology = [OUTLOOK]
						name = [SUBIDEOLOGY_NAME]
						long_name = [SUBIDEOLOGY_NAME]
					}
				}
				OUTLOOK = "[meta_ideology_names]"
				SUBIDEOLOGY_NAME = "[show_ruling_party]"
			}
		}
	}
}

set_correct_ideology_trait = {
	if = {
		limit = {
			has_country_flag = set_conservatism
			NOT = { has_country_leader_with_trait = western_conservatism }
		}
		add_country_leader_trait = western_conservatism
	}
	else_if = {
		limit = {
			has_country_flag = set_liberalism
			NOT = { has_country_leader_with_trait = western_liberalism }
		}
		add_country_leader_trait = western_liberalism
	}
	else_if = {
		limit = {
			has_country_flag = set_socialism
			NOT = { has_country_leader_with_trait = western_socialism }
		}
		add_country_leader_trait = western_socialism
	}
	else_if = {
		limit = {
			has_country_flag = set_Western_Autocracy
			NOT = { has_country_leader_with_trait = western_Western_Autocracy }
		}
		add_country_leader_trait = western_Western_Autocracy
	}
	else_if = {
		limit = {
			has_country_flag = set_Communist-State
			NOT = { has_country_leader_with_trait = emerging_Communist-State }
		}
		add_country_leader_trait = emerging_Communist-State
	}
	else_if = {
		limit = {
			has_country_flag = set_Conservative
			NOT = { has_country_leader_with_trait = emerging_Conservative }
		}
		add_country_leader_trait = emerging_Conservative
	}
	else_if = {
		limit = {
			has_country_flag = set_Autocracy
			NOT = { has_country_leader_with_trait = emerging_Autocracy }
		}
		add_country_leader_trait = emerging_Autocracy
	}
	else_if = {
		limit = {
			has_country_flag = set_Vilayat_e_Faqih
			NOT = { has_country_leader_with_trait = emerging_Vilayat_e_Faqih }
		}
		add_country_leader_trait = emerging_Vilayat_e_Faqih
	}
	else_if = {
		limit = {
			has_country_flag = set_Mod_Vilayat_e_Faqih
			NOT = { has_country_leader_with_trait = emerging_Mod_Vilayat_e_Faqih }
		}
		add_country_leader_trait = emerging_Mod_Vilayat_e_Faqih
	}
	else_if = {
		limit = {
			has_country_flag = set_anarchist_communism
			NOT = { has_country_leader_with_trait = emerging_anarchist_communism }
		}
		add_country_leader_trait = emerging_anarchist_communism
	}
	else_if = {
		limit = {
			has_country_flag = set_Neutral_conservatism
			NOT = { has_country_leader_with_trait = neutrality_Neutral_conservatism }
		}
		add_country_leader_trait = neutrality_Neutral_conservatism
	}
	else_if = {
		limit = {
			has_country_flag = set_oligarchism
			NOT = { has_country_leader_with_trait = neutrality_oligarchism }
		}
		add_country_leader_trait = neutrality_oligarchism
	}
	else_if = {
		limit = {
			has_country_flag = set_neutral_Social
			NOT = { has_country_leader_with_trait = neutrality_neutral_Social }
		}
		add_country_leader_trait = neutrality_neutral_Social
	}
	else_if = {
		limit = {
			has_country_flag = set_Neutral_Libertarian
			NOT = { has_country_leader_with_trait = neutrality_Neutral_Libertarian }
		}
		add_country_leader_trait = neutrality_Neutral_Libertarian
	}
	else_if = {
		limit = {
			has_country_flag = set_Neutral_Autocracy
			NOT = { has_country_leader_with_trait = neutrality_Neutral_Autocracy }
		}
		add_country_leader_trait = neutrality_Neutral_Autocracy
	}
	else_if = {
		limit = {
			has_country_flag = set_Neutral_Communism
			NOT = { has_country_leader_with_trait = neutrality_Neutral_Communism }
		}
		add_country_leader_trait = neutrality_Neutral_Communism
	}
	else_if = {
		limit = {
			has_country_flag = set_Neutral_Muslim_Brotherhood
			NOT = { has_country_leader_with_trait = neutrality_Neutral_Muslim_Brotherhood }
		}
		add_country_leader_trait = neutrality_Neutral_Muslim_Brotherhood
	}
	else_if = {
		limit = {
			has_country_flag = set_Neutral_green
			NOT = { has_country_leader_with_trait = neutrality_Neutral_green }
		}
		add_country_leader_trait = neutrality_Neutral_green
	}
	else_if = {
		limit = {
			has_country_flag = set_Kingdom
			NOT = { has_country_leader_with_trait = salafist_Kingdom }
		}
		add_country_leader_trait = salafist_Kingdom
	}
	else_if = {
		limit = {
			has_country_flag = set_Caliphate
			NOT = { has_country_leader_with_trait = salafist_Caliphate }
		}
		add_country_leader_trait = salafist_Caliphate
	}
	else_if = {
		limit = {
			has_country_flag = set_Nat_Autocracy
			NOT = { has_country_leader_with_trait = nationalist_Nat_Autocracy }
		}
		add_country_leader_trait = nationalist_Nat_Autocracy
	}
	else_if = {
		limit = {
			has_country_flag = set_Nat_Fascism
			NOT = { has_country_leader_with_trait = nationalist_Nat_Fascism }
		}
		add_country_leader_trait = nationalist_Nat_Fascism
	}
	else_if = {
		limit = {
			has_country_flag = set_Nat_Populism
			NOT = { has_country_leader_with_trait = nationalist_Nat_Populism }
		}
		add_country_leader_trait = nationalist_Nat_Populism
	}
	else_if = {
		limit = {
			has_country_flag = set_Monarchist
			NOT = { has_country_leader_with_trait = nationalist_Monarchist }
		}
		add_country_leader_trait = nationalist_Monarchist
	}
}

remove_set_ideology_flag = {
	if = {
		limit = { has_country_flag = set_conservatism }
		clr_country_flag = set_conservatism
	}
	if = {
		limit = { has_country_flag = set_liberalism }
		clr_country_flag = set_liberalism
	}
	if = {
		limit = { has_country_flag = set_socialism }
		clr_country_flag = set_socialism
	}
	if = {
		limit = { has_country_flag = set_Western_Autocracy }
		clr_country_flag = set_Western_Autocracy
	}
	if = {
		limit = { has_country_flag = set_Communist-State }
		clr_country_flag = set_Communist-State
	}
	if = {
		limit = { has_country_flag = set_Conservative }
		clr_country_flag = set_Conservative
	}
	if = {
		limit = { has_country_flag = set_Autocracy }
		clr_country_flag = set_Autocracy
	}
	if = {
		limit = { has_country_flag = set_Vilayat_e_Faqih }
		clr_country_flag = set_Vilayat_e_Faqih
	}
	if = {
		limit = { has_country_flag = set_Mod_Vilayat_e_Faqih }
		clr_country_flag = set_Mod_Vilayat_e_Faqih
	}
	if = {
		limit = { has_country_flag = set_anarchist_communism }
		clr_country_flag = set_anarchist_communism
	}
	if = {
		limit = { has_country_flag = set_Kingdom }
		clr_country_flag = set_Kingdom
	}
	if = {
		limit = { has_country_flag = set_Caliphate }
		clr_country_flag = set_Caliphate
	}
	if = {
		limit = { has_country_flag = set_Neutral_conservatism }
		clr_country_flag = set_Neutral_conservatism
	}
	if = {
		limit = { has_country_flag = set_oligarchism }
		clr_country_flag = set_oligarchism
	}
	if = {
		limit = { has_country_flag = set_neutral_Social }
		clr_country_flag = set_neutral_Social
	}
	if = {
		limit = { has_country_flag = set_Neutral_Libertarian }
		clr_country_flag = set_Neutral_Libertarian
	}
	if = {
		limit = { has_country_flag = set_Neutral_Autocracy }
		clr_country_flag = set_Neutral_Autocracy
	}
	if = {
		limit = { has_country_flag = set_Neutral_Communism }
		clr_country_flag = set_Neutral_Communism
	}
	if = {
		limit = { has_country_flag = set_Neutral_green }
		clr_country_flag = set_Neutral_green
	}
	if = {
		limit = { has_country_flag = set_Neutral_Muslim_Brotherhood }
		clr_country_flag = set_Neutral_Muslim_Brotherhood
	}
	if = {
		limit = { has_country_flag = set_Nat_Autocracy }
		clr_country_flag = set_Nat_Autocracy
	}
	if = {
		limit = { has_country_flag = set_Nat_Fascism }
		clr_country_flag = set_Nat_Fascism
	}
	if = {
		limit = { has_country_flag = set_Nat_Populism }
		clr_country_flag = set_Nat_Populism
	}
	if = {
		limit = { has_country_flag = set_Monarchist }
		clr_country_flag = set_Monarchist
	}
}

set_failsafe_values = {
	#Western
	if = {
		limit = {
			check_variable = { party_popularity@democratic > 0 }
			NOT = {
				OR = {
					check_variable = { party_pop_array^1 > 0 }
					check_variable = { party_pop_array^2 > 0 }
					check_variable = { party_pop_array^3 > 0 }
					check_variable = { party_pop_array^0 > 0 }
				}
			}
		}
		if = { limit = { has_elections = yes }
			random_list = {
				2 = {
					set_variable = { party_pop_array^3 = 0.3 }
					set_variable = { party_pop_array^1 = 0.1 }
					set_variable = { party_pop_array^2 = 0.1 }
				
				}
				2 = {
					set_variable = { party_pop_array^1 = 0.3 }
					set_variable = { party_pop_array^3 = 0.1 }
					set_variable = { party_pop_array^2 = 0.1 }
				}
			}
			else = {
				random_list = {
					2 = {
						set_variable = { party_pop_array^0 = 0.3 }
						set_variable = { party_pop_array^1 = 0.1 }
						set_variable = { party_pop_array^3 = 0.1 }
					}
					2 = {
						set_variable = { party_pop_array^0 = 0.5 }
						set_variable = { party_pop_array^2 = 0.1 }
						set_variable = { party_pop_array^3 = 0.1 }
					}
				}
			}
		}
	}
	#Emerging
	if = {
		limit = {
			check_variable = { party_popularity@communism > 0 }
			NOT = {
				OR = {
					check_variable = { party_pop_array^4 > 0 }
					check_variable = { party_pop_array^6 > 0 }
					check_variable = { party_pop_array^7 > 0 }
					check_variable = { party_pop_array^9 > 0 }
					check_variable = { party_pop_array^8 > 0 }
					check_variable = { party_pop_array^5 > 0 }
				}
			}
		}
		if = { limit = { has_elections = yes }
		
			if = { limit = { has_idea = shia }
				random_list = {
					2 = {
						set_variable = { party_pop_array^9 = 0.5 } #iranian revolution is spreading
						set_variable = { party_pop_array^8 = 0.2 }
						set_variable = { party_pop_array^5 = 0.1 }
					
					}
					1 = {
						set_variable = { party_pop_array^9 = 0.4 } #iranian revolution is spreading
						set_variable = { party_pop_array^8 = 0.3 }
						set_variable = { party_pop_array^5 = 0.2 }
					}
				}
			}
			if = { limit = { has_idea = western_country }
				set_variable = { party_pop_array^5 = 0.5 } #left-wing radicalism can rise
				set_variable = { party_pop_array^4 = 0.01 } #communism is dead
			}
			else_if = { limit = { OR = { Russian_Proficient = yes is_asian_nation = yes } } #soviet block and asia
				random_list = {
					2 = {
						set_variable = { party_pop_array^6 = 0.3 } #reactionary
						set_variable = { party_pop_array^4 = 0.1 } #communism can be a potent force still here
					}
					2 = {
						set_variable = { party_pop_array^6 = 0.1 } #reactionary
						set_variable = { party_pop_array^4 = 0.2 } #communism can be a potent force still here
					}
				}
			}
			else_if = { limit = { OR = { is_Central_America = yes is_caribbean_nation = yes is_South_America = yes } }
				set_variable = { party_pop_array^5 = 0.5 } #revolutionary socialism is major force
				set_variable = { party_pop_array^4 = 0.1 } #more hard core castroism is muted
			}
			else_if = { limit = { always = yes }
				set_variable = { party_pop_array^5 = 0.5 } #generic left-radical movement
			}
		}
		else_if = { limit = { has_elections = no }
		
			if = { limit = { has_idea = shia }
				set_variable = { party_pop_array^9 = 0.5 } #iranian revolution is spreading
			}
			if = { limit = { has_idea = western_country }
				set_variable = { party_pop_array^4 = 0.5 } #communism is only viable western emerging autocratic force
				set_variable = { party_pop_array^5 = 0.1 } #revolutionary socialism is supporting role
			}
			else_if = { limit = { OR = { Russian_Proficient = yes is_asian_nation = yes } } #soviet block and asia
				random_list = {
					2 = {
						set_variable = { party_pop_array^7 = 0.3 } #reactionary autocrat
						set_variable = { party_pop_array^4 = 0.1 } #communism can be a potent force still here
					}
					2 = {
						set_variable = { party_pop_array^7 = 0.1 } #reactionary autocrat
						set_variable = { party_pop_array^4 = 0.3 } #communism can be a potent force still here
					}
				}
			}
			else_if = { limit = { OR = { is_Central_America = yes is_caribbean_nation = yes is_South_America = yes } }
				set_variable = { party_pop_array^4 = 0.5 } #communism! viva Che!
			}
			else_if = { limit = { always = yes }
				set_variable = { party_pop_array^7 = 0.5 } #generic autocracy
			}
		}
	}
	#Non-aligned
	if = {
		limit = {
			check_variable = { party_popularity@neutrality > 0 }
			NOT = {
				OR = {
					check_variable = { party_pop_array^14 > 0 }
					check_variable = { party_pop_array^15 > 0 }
					check_variable = { party_pop_array^18 > 0 }
					check_variable = { party_pop_array^16 > 0 }
					check_variable = { party_pop_array^13 > 0 }
					check_variable = { party_pop_array^19 > 0 }
					check_variable = { party_pop_array^17 > 0 }
					check_variable = { party_pop_array^12 > 0 }
				}
			}
		}
		if = { limit = { has_elections = yes }
		
			if = { limit = { Is_Possible_Muslim_Brotherhood = yes }
				set_variable = { party_pop_array^12 = 0.5 } #Islamism!
			}
			else_if = { limit = { has_idea = western_country }
				random_list = {
					2 = {
						set_variable = { party_pop_array^16 = 0.3 } #libertarian
						set_variable = { party_pop_array^17 = 0.1 } #Green party
					}
					2 = {
						set_variable = { party_pop_array^17 = 0.3 } #Green party
						set_variable = { party_pop_array^16 = 0.1 } #libertarian
						set_variable = { party_pop_array^18 = 0.02 }
					}
					2 = {
						set_variable = { party_pop_array^17 = 0.2 } #Green party
						set_variable = { party_pop_array^16 = 0.1 } #libertarian
						set_variable = { party_pop_array^18 = 0.15 }
					}
				}
			}
			else_if = { limit = { OR = { Russian_Proficient = yes } } #soviet block
				random_list = {
					2 = {
						set_variable = { party_pop_array^15 = 0.3 }
						set_variable = { party_pop_array^19 = 0.1 } #communism can be a potent force still here
					}
					2 = {
						set_variable = { party_pop_array^15 = 0.1 } #reactionary
						set_variable = { party_pop_array^19 = 0.2 } #communism can be a potent force still here
					}
				}
			}
			else_if = { limit = { OR = { is_Central_America = yes is_caribbean_nation = yes is_South_America = yes } }
				set_variable = { party_pop_array^14 = 0.5 }
				set_variable = { party_pop_array^17 = 0.1 }
			}
			else_if = { limit = { always = yes }
				set_variable = { party_pop_array^14 = 0.5 }
			}
		}
		else_if = { limit = { has_elections = no }
			
			if = { limit = { Is_Possible_Muslim_Brotherhood = yes }
				set_variable = { party_pop_array^12 = 0.5 } #Islamism!
			}
			if = { limit = { has_idea = western_country }
				set_variable = { party_pop_array^13 = 0.5 }
			}
			else_if = { limit = { OR = { Russian_Proficient = yes is_asian_nation = yes } } #soviet block and asia
				set_variable = { party_pop_array^19 = 0.5 }
			}
			else_if = { limit = { OR = { is_asian_nation = yes } } #asia
				random_list = {
					2 = {
						set_variable = { party_pop_array^13 = 0.3 }
					}
					2 = {
						set_variable = { party_pop_array^19 = 0.3 }
					}
				}
			}
			else_if = { limit = { OR = { is_Central_America = yes is_caribbean_nation = yes is_South_America = yes } }
				set_variable = { party_pop_array^13 = 0.5 }
			}
			else_if = { limit = { always = yes }
				set_variable = { party_pop_array^13 = 0.5 }
			}
		}
	}
	#Salafism
	if = {
		limit = {
			check_variable = { party_popularity@fascism > 0 }
			NOT = {
				OR = {
					check_variable = { party_pop_array^10 > 0 }
					check_variable = { party_pop_array^11 > 0 }
				}
			}
		}
		set_variable = { party_pop_array^11 = 0.5 }
	}
	#Nationalist
	if = {
		limit = {
			check_variable = { party_popularity@nationalist > 0 }
			NOT = {
				OR = {
					check_variable = { party_pop_array^22 > 0 }
					check_variable = { party_pop_array^21 > 0 }
					check_variable = { party_pop_array^20 > 0 }
					check_variable = { party_pop_array^23 > 0 }
				}
			}
		}
		if = { limit = { has_elections = yes }
			set_variable = { party_pop_array^20 = 0.5 }
		}
		else_if = { limit = { has_elections = no }
			
			if = { limit = { OR = { has_idea = western_country Russian_Proficient = yes } } #soviet block
				set_variable = { party_pop_array^21 = 0.5 }
			}
			else_if = { limit = { OR = { is_asian_nation = yes } } #asia
				random_list = {
					5 = {
						set_variable = { party_pop_array^22 = 0.3 }
					}
					1 = {
						set_variable = { party_pop_array^23 = 0.3 }
					}
				}
			}
			else_if = { limit = { always = yes }
				set_variable = { party_pop_array^22 = 0.5 }
			}
		}
	}
}

## Failsafe set change ideology if none specified ##
#intended for on_action

set_failsafe_change_leader = {
	if = {
		limit = {
			NOT = {
				OR = {
					has_country_flag = set_conservatism
					has_country_flag = set_liberalism
					has_country_flag = set_socialism
					has_country_flag = set_Western_Autocracy
					has_country_flag = set_Communist-State
					has_country_flag = set_Conservative
					has_country_flag = set_Autocracy
					has_country_flag = set_Vilayat_e_Faqih
					has_country_flag = set_Mod_Vilayat_e_Faqih
					has_country_flag = set_anarchist_communism
					has_country_flag = set_Kingdom
					has_country_flag = set_Caliphate
					has_country_flag = set_Neutral_conservatism
					has_country_flag = set_oligarchism
					has_country_flag = set_neutral_Social
					has_country_flag = set_Neutral_Libertarian
					has_country_flag = set_Neutral_Autocracy
					has_country_flag = set_Neutral_Communism
					has_country_flag = set_Neutral_green
					has_country_flag = set_Neutral_Muslim_Brotherhood
					has_country_flag = set_Nat_Autocracy
					has_country_flag = set_Nat_Fascism
					has_country_flag = set_Nat_Populism
					has_country_flag = set_Monarchist
				}
			}
		}
		set_failsafe_values = yes
		clear_array = ruling_party
		clear_array = gov_coalition_array
		set_temp_variable = { prevmax_temp = 0 }
		if = { limit = { has_government = democratic }
			if = {
				limit = {
					has_variable = party_pop_array^1
					check_variable = { party_pop_array^1 > prevmax_temp }
				}
				set_temp_variable = { prevmax_temp = party_pop_array^1 }
			}
			if = {
				limit = {
					has_variable = party_pop_array^2
					check_variable = { party_pop_array^2 > prevmax_temp }
				}
				set_temp_variable = { prevmax_temp = party_pop_array^2 }
			}
			if = {
				limit = {
					has_variable = party_pop_array^3
					check_variable = { party_pop_array^3 > prevmax_temp }
				}
				set_temp_variable = { prevmax_temp = party_pop_array^3 }
			}
			if = {
				limit = {
					has_variable = party_pop_array^0
					check_variable = { party_pop_array^0 > prevmax_temp }
				}
				set_temp_variable = { prevmax_temp = party_pop_array^0 }
			}
			#Set for highest value
			if = { limit = { check_variable = { prevmax_temp = party_pop_array^1 } }
				set_country_flag = set_conservatism
				add_to_array = { ruling_party = 1 }
			}
			else_if = { limit = { check_variable = { prevmax_temp = party_pop_array^2 } }
				set_country_flag = set_liberalism
				add_to_array = { ruling_party = 2 }
			}
			else_if = { limit = { check_variable = { prevmax_temp = party_pop_array^3 } }
				set_country_flag = set_socialism
				add_to_array = { ruling_party = 3 }
			}
			else_if = { limit = { check_variable = { prevmax_temp = party_pop_array^0 } }
				set_country_flag = set_Western_Autocracy
				add_to_array = { ruling_party = 0 }
			}
		}
		
		if = { limit = { has_government = communism }
			if = {
				limit = {
					has_variable = party_pop_array^4
					check_variable = { party_pop_array^4 > prevmax_temp }
				}
				set_temp_variable = { prevmax_temp = party_pop_array^4 }
			}
			if = {
				limit = {
					has_variable = party_pop_array^6
					check_variable = { party_pop_array^6 > prevmax_temp }
				}
				set_temp_variable = { prevmax_temp = party_pop_array^6 }
			}
			if = {
				limit = {
					has_variable = party_pop_array^7
					check_variable = { party_pop_array^7 > prevmax_temp }
				}
				set_temp_variable = { prevmax_temp = party_pop_array^7 }
			}
			if = {
				limit = {
					has_variable = party_pop_array^9
					check_variable = { party_pop_array^9 > prevmax_temp }
				}
				set_temp_variable = { prevmax_temp = party_pop_array^9 }
			}
			if = {
				limit = {
					has_variable = party_pop_array^8
					check_variable = { party_pop_array^8 > prevmax_temp }
				}
				set_temp_variable = { prevmax_temp = party_pop_array^8 }
			}
			if = {
				limit = {
					has_variable = party_pop_array^5
					check_variable = { party_pop_array^5 > prevmax_temp }
				}
				set_temp_variable = { prevmax_temp = party_pop_array^5 }
			}
			
			#Set for highest value
			if = { limit = { check_variable = { prevmax_temp = party_pop_array^4 } }
				set_country_flag = set_Communist-State
				add_to_array = { ruling_party = 4 }
			}
			else_if = { limit = { check_variable = { prevmax_temp = party_pop_array^6 } }
				set_country_flag = set_Conservative
				add_to_array = { ruling_party = 6 }
			}
			else_if = { limit = { check_variable = { prevmax_temp = party_pop_array^7 } }
				set_country_flag = set_Autocracy
				add_to_array = { ruling_party = 7 }
			}
			else_if = { limit = { check_variable = { prevmax_temp = party_pop_array^9 } }
				set_country_flag = set_Vilayat_e_Faqih
				add_to_array = { ruling_party = 9 }
			}
			else_if = { limit = { check_variable = { prevmax_temp = party_pop_array^8 } }
				set_country_flag = set_Mod_Vilayat_e_Faqih
				add_to_array = { ruling_party = 8 }
			}
			else_if = { limit = { check_variable = { prevmax_temp = party_pop_array^5 } }
				set_country_flag = set_anarchist_communism
				add_to_array = { ruling_party = 5 }
			}
		}
		
		if = { limit = { has_government = neutrality }
			if = {
				limit = {
					has_variable = party_pop_array^14
					check_variable = { party_pop_array^14 > prevmax_temp }
				}
				set_temp_variable = { prevmax_temp = party_pop_array^14 }
			}
			if = {
				limit = {
					has_variable = party_pop_array^15
					check_variable = { party_pop_array^15 > prevmax_temp }
				}
				set_temp_variable = { prevmax_temp = party_pop_array^15 }
			}
			if = {
				limit = {
					has_variable = party_pop_array^18
					check_variable = { party_pop_array^18 > prevmax_temp }
				}
				set_temp_variable = { prevmax_temp = party_pop_array^18 }
			}
			if = {
				limit = {
					has_variable = party_pop_array^16
					check_variable = { party_pop_array^16 > prevmax_temp }
				}
				set_temp_variable = { prevmax_temp = party_pop_array^16 }
			}
			if = {
				limit = {
					has_variable = party_pop_array^13
					check_variable = { party_pop_array^13 > prevmax_temp }
				}
				set_temp_variable = { prevmax_temp = party_pop_array^13 }
			}
			if = {
				limit = {
					has_variable = party_pop_array^19
					check_variable = { party_pop_array^19 > prevmax_temp }
				}
				set_temp_variable = { prevmax_temp = party_pop_array^19 }
			}
			if = {
				limit = {
					has_variable = party_pop_array^17
					check_variable = { party_pop_array^17 > prevmax_temp }
				}
				set_temp_variable = { prevmax_temp = party_pop_array^17 }
			}
			if = {
				limit = {
					has_variable = party_pop_array^12
					check_variable = { party_pop_array^12 > prevmax_temp }
				}
				set_temp_variable = { prevmax_temp = party_pop_array^12 }
			}
			
			#Set for highest value
			if = { limit = { check_variable = { prevmax_temp = party_pop_array^14 } }
				set_country_flag = set_Neutral_conservatism
				add_to_array = { ruling_party = 14 }
			}
			else_if = { limit = { check_variable = { prevmax_temp = party_pop_array^15 } }
				set_country_flag = set_oligarchism
				add_to_array = { ruling_party = 15 }
			}
			else_if = { limit = { check_variable = { prevmax_temp = party_pop_array^18 } }
				set_country_flag = set_neutral_Social
				add_to_array = { ruling_party = 18 }
			}
			else_if = { limit = { check_variable = { prevmax_temp = party_pop_array^16 } }
				set_country_flag = set_Neutral_Libertarian
				add_to_array = { ruling_party = 16 }
			}
			else_if = { limit = { check_variable = { prevmax_temp = party_pop_array^13 } }
				set_country_flag = set_Neutral_Autocracy
				add_to_array = { ruling_party = 13 }
			}
			else_if = { limit = { check_variable = { prevmax_temp = party_pop_array^19 } }
				set_country_flag = set_Neutral_Communism
				add_to_array = { ruling_party = 19 }
			}
			else_if = { limit = { check_variable = { prevmax_temp = party_pop_array^17 } }
				set_country_flag = set_Neutral_green
				add_to_array = { ruling_party = 17 }
			}
			else_if = { limit = { check_variable = { prevmax_temp = party_pop_array^12 } }
				set_country_flag = set_Neutral_Muslim_Brotherhood
				add_to_array = { ruling_party = 12 }
			}
		}
		if = { limit = { has_government = nationalist }
			if = {
				limit = {
					has_variable = party_pop_array^22
					check_variable = { party_pop_array^22 > prevmax_temp }
				}
				set_temp_variable = { prevmax_temp = party_pop_array^22 }
			}
			if = {
				limit = {
					has_variable = party_pop_array^21
					check_variable = { party_pop_array^21 > prevmax_temp }
				}
				set_temp_variable = { prevmax_temp = party_pop_array^21 }
			}
			if = {
				limit = {
					has_variable = party_pop_array^20
					check_variable = { party_pop_array^20 > prevmax_temp }
				}
				set_temp_variable = { prevmax_temp = party_pop_array^20 }
			}
			if = {
				limit = {
					has_variable = party_pop_array^23
					check_variable = { party_pop_array^23 > prevmax_temp }
				}
				set_temp_variable = { prevmax_temp = party_pop_array^23 }
			}
			#Set for highest value
			if = { limit = { check_variable = { prevmax_temp = party_pop_array^22 } }
				set_country_flag = set_Nat_Autocracy
				add_to_array = { ruling_party = 22 }
			}
			else_if = { limit = { check_variable = { prevmax_temp = party_pop_array^21 } }
				set_country_flag = set_Nat_Fascism
				add_to_array = { ruling_party = 21 }
			}
			else_if = { limit = { check_variable = { prevmax_temp = party_pop_array^20 } }
				set_country_flag = set_Nat_Populism
				add_to_array = { ruling_party = 20 }
			}
			else_if = { limit = { check_variable = { prevmax_temp = party_pop_array^23 } }
				set_country_flag = set_Monarchist
				add_to_array = { ruling_party = 23 }
			}
		}
		
		if = { limit = { has_government = fascism }
			if = {
				limit = {
					has_variable = party_pop_array^10
					check_variable = { party_pop_array^10 > prevmax_temp }
				}
				set_temp_variable = { prevmax_temp = party_pop_array^10 }
			}
			if = {
				limit = {
					has_variable = party_pop_array^11
					check_variable = { party_pop_array^11 > prevmax_temp }
				}
				set_temp_variable = { prevmax_temp = party_pop_array^11 }
			}
			#Set for highest value
			if = { limit = { check_variable = { prevmax_temp = party_pop_array^10 } }
				set_country_flag = set_Kingdom
				add_to_array = { ruling_party = 10 }
			}
			else_if = { limit = { check_variable = { prevmax_temp = party_pop_array^11 } }
				set_country_flag = set_Caliphate
				add_to_array = { ruling_party = 11 }
			}
		}
		set_leader = yes
	}
}