# CONSOLE COMMAND: effect <scripted_effect> <culture/religion_id> <percentage_decimal> <state>
# Emit state for country change

# usage without the "d_" in the console
d_eoanb_console_commands = {
	## Money Console Commands
	log = "   darkest_hour_console_commands"
	log = "   add_money"
	log = "   add_inflation"
	log = "   set_unemployment"
	log = "   add_unemployment"
	log = "   change_unemployment_impact"
}

# MONEY SYSTEM #
## Money Console Commands
d_add_money = {
	if = {
		limit = {
			exists = yes
		}
		set_temp_variable = { money_to_gain = args^0?1000 }
		add_money_with_tooltip_effect = yes
		if = {
			limit = {
				check_variable = { money_to_gain > 0 }
			}
			log = D_ADD_MONEY_TT
		}
		else = {
			log = D_SUBTRACT_MONEY_TT
		}
	}
	else = {
		log = "Command was used in wrong scope!"
	}
}

d_add_inflation = {
	if = {
		limit = {
			exists = yes
		}
		set_temp_variable = { inflation_to_gain = args^0?0.1 }
		add_inflation_with_tooltip_effect = yes
		log = D_ADD_INFLATION_TT
	}
	else = {
		log = "Command was used in wrong scope!"
	}
}

d_set_unemployment = {
	if = {
		limit = {
			exists = yes
		}
		set_temp_variable = { unemployment_value = args^0?0.1 }
		set_unemployment_effect = yes
		log = D_SET_UNEMPLOYMENT_TT
	}
	else = {
		log = "Command was used in wrong scope!"
	}
}

d_add_unemployment = {
	if = {
		limit = {
			exists = yes
		}
		set_temp_variable = { unemployment_value = args^0?0.1 }
		add_unemployment_effect = yes
		log = D_ADD_UNEMPLOYMENT_TT
	}
	else = {
		log = "Command was used in wrong scope!"
	}
}

d_change_unemployment_impact = {
	if = {
		limit = {
			exists = yes
		}
		set_temp_variable = { modifier_value = args^0?0.1 }
		change_unemployment_value_modifier = yes
		log = D_CHANGE_UNEMPLOYMENT_IMPACT_TT
		log = D_CHANGE_UNEMPLOYMENT_IMPACT_TT_2
	}
	else = {
		log = "Command was used in wrong scope!"
	}
}

d_add_culture = {
	##add_culture ID PERCENTAGE STATE
	if = {
		limit = {
			check_variable = { args^2 > 0 }
		}
		var:args^2 = {
			set_temp_variable = { type_temp = args^0 }
			set_temp_variable = { percentage_temp = args^1 }

			pops_increase_culture = yes
		}
	}
	##add_culture ID PERCENTAGE COUNTRY
	else = {
		ROOT = {
			set_temp_variable = { type_temp = args^0 }
			set_temp_variable = { percentage_temp = args^1 }

			pops_increase_culture = yes
		}
	}
}

d_add_religion = {
	##add_culture ID PERCENTAGE STATE
	if = {
		limit = {
			check_variable = { args^2 > 0 }
		}
		var:args^2 = {
			set_temp_variable = { type_temp = args^0 }
			set_temp_variable = { percentage_temp = args^1 }

			pops_increase_culture = yes
		}
	}
	##add_culture ID PERCENTAGE COUNTRY
	else = {
		ROOT={
			set_temp_variable = {type_temp = args^0}
			set_temp_variable = {percentage_temp = args^1}

			pops_increase_religion = yes
		}
	}
}

d_check_culture = {
	set_temp_variable = { amount = 0 }
	every_state = {
		limit = {
			check_variable = { state_culture_array^num > 0 }
		}
		set_temp_variable = { cult_total = 0 }
		set_temp_variable = { previous = 1.0 }
		set_temp_variable = { check = 0 }
		set_temp_variable = { check2 = 0 }
		for_each_loop = {
			array = state_culture_array_num
			value = v
			add_to_temp_variable = { cult_total = v }
			if = {
				limit = {
					check_variable = { previous < v }
				}
				set_temp_variable = { check = 1 }
			}
			set_temp_variable = { previous = v }
		}
		for_each_loop = {
			array = state_culture_array
			value = v
			if = {
				limit = {
					check_variable = { v > 559 }					# MAKE SURE TO UPDATE THIS WHEN ADDING NEW CULTURES
				}
				set_temp_variable = { check2 = 1 }
			}
		}
		if = {
			limit = {
				NOT = {
					check_variable = { cult_total = 1.00 }
				}
			}
			add_to_temp_variable = { amount = 1 }
			log = "Culture error found at state [THIS.GetID]. The total is equal to [?cult_total], not 1."
		}
		if = {
			limit = {
				check_variable = { check = 1 }
			}
			add_to_temp_variable = { amount = 1 }
			log = "Culture error found at state [THIS.GetID]. The cultures are not in descending order."
		}
		if = {
			limit = {
				check_variable = { check2 = 1 }
			}
			add_to_temp_variable = { amount = 1 }
			log = "Culture error found at state [THIS.GetID]. Unknown culture(s) found. If it's an existing culture, please update the console command."
		}
		if = {
			limit = {
				NOT = {
					check_variable = { state_culture_array^num = state_culture_array_num^num }
				}
			}
			add_to_temp_variable = { amount = 1 }
			log = "Culture error found at state [THIS.GetID]. Amount of cultures does not correspond to the numbers of cultures."
		}
		add_to_temp_array = {
			array = temp_state_array
			value = THIS
		}
	}
	log = "Culture error checking finished. Total amount of errors within states: [?amount]"
	set_temp_variable = { amount = 0 }
	for_each_scope_loop = {
		array = temp_state_array
		set_temp_variable = { check = 0 }
		
		if={
			limit={
				is_island_state = no
			}
			for_each_scope_loop = {
				array = temp_state_array
				break = temp_break
				if = {
					limit = {
						NOT = {
							state = PREV
						}
						OR = {
							distance_to = {
								value < 1000
								target = PREV
							}
							any_neighbor_state = {
								state = PREV.PREV
							}
						}
					}
					for_each_loop = {
						array = PREV.state_culture_array
						value = v
						break = temp_break_2
						if = {
							limit = {
								is_in_array = {
									array = THIS.state_culture_array
									value = v
								}
							}
							set_temp_variable = { check = 1 }
							set_temp_variable = { temp_break = 1 }
							set_temp_variable = { temp_break_2 = 1 }
						}
					}
				}
			}
			if = {
				limit = {
					check_variable = { check = 0 }
				}
				add_to_temp_variable = { amount = 1 }
				log = "Culture warning found at state [THIS.GetID]: Isolated from any other states with the same culture."
			}
		}
	}
	log = "Culture warning checking finished. Total amount of warnings within states: [?amount]"
}
d_check_religion = {
	set_temp_variable = { amount = 0 }
	every_state = {
		limit = {
			check_variable = { state_religion_array^num > 0 }
		}
		set_temp_variable = { rel_total = 0 }
		set_temp_variable = { previous = 1.0 }
		set_temp_variable = { check = 0 }
		set_temp_variable = { check2 = 0 }
		for_each_loop = {
			array = state_religion_array_num
			value = v
			add_to_temp_variable = { rel_total = v }
			if = {
				limit = {
					check_variable = { previous < v }
				}
				set_temp_variable = { check = 1 }
			}
			set_temp_variable = { previous = v }
		}
		for_each_loop = {
			array = state_religion_array
			value = v
			if = {
				limit = {
					check_variable = { v > 55 }					# MAKE SURE TO UPDATE THIS WHEN ADDING NEW RELIGIONS
				}
				set_temp_variable = { check2 = 1 }
			}
		}
		if = {
			limit = {
				NOT = {
					check_variable = { rel_total = 1.00 }
				}
			}
			add_to_temp_variable = { amount = 1 }
			log = "Religion error found at state [THIS.GetID]. The total is equal to [?rel_total], not 1."
		}
		if = {
			limit = {
				check_variable = { check = 1 }
			}
			add_to_temp_variable = { amount = 1 }
			log = "Religion error found at state [THIS.GetID]. The religions are not in descending order."
		}
		if = {
			limit = {
				check_variable = { check2 = 1 }
			}
			add_to_temp_variable = { amount = 1 }
			log = "Religion error found at state [THIS.GetID]. Unknown religion found. If it's an existing religion, please update the console command."
		}
		if = {
			limit = {
				NOT = {
					check_variable = { state_religion_array^num = state_religion_array_num^num }
				}
			}
			add_to_temp_variable = { amount = 1 }
			log = "Religion error found at state [THIS.GetID]. Amount of religions does not correspond to the numbers of religions."
		}
		add_to_temp_array = {
			array = temp_state_array
			value = THIS
		}
	}
	log = "Religion error checking finished. Total amount of errors within states: [?amount]" 
	set_temp_variable = { amount = 0 }
	for_each_scope_loop = {
		array = temp_state_array
		set_temp_variable = { check = 0 }
		if={
			limit={
				is_island_state = no
			}
			for_each_scope_loop = {
				array = temp_state_array
				break = temp_break
				if = {
					limit = {
						NOT = {
							state = PREV
						}
						OR = {
							distance_to = {
								value < 1000
								target = PREV
							}
							any_neighbor_state = {
								state = PREV.PREV
							}
						}
					}
					for_each_loop = {
						array = PREV.state_religion_array
						value = v
						break = temp_break_2
						if = {
							limit = {
								is_in_array = {
									array = THIS.state_religion_array
									value = v
								}
							}
							set_temp_variable = { check = 1 }
							set_temp_variable = { temp_break = 1 }
							set_temp_variable = { temp_break_2 = 1 }
						}
					}
				}
			}
			if = {
				limit = {
					check_variable = { check = 0 }
				}
				add_to_temp_variable = { amount = 1 }
				log = "Religion warning found at state [THIS.GetID]: Isolated from any other states with the same religion."
			}
		}
	}
	log = "Religion warning checking finished. Total amount of warnings within states: [?amount]"
}

d_check_culture_use={
	
	every_state = {
		for_each_loop = {
			array = state_culture_array
			value = current_culture
			# add_to_variable = {
				# var = global.cultures_used^v
				# value = 1
			# }
			add_to_array = {
				array = global.cultures_used
				value = current_culture
				#index = v
			}
		}
	}
	set_variable = { v = 1 }
	while_loop_effect = {
		limit = { check_variable = { v < 559 } }
		if = {
			limit = {
				NOT={
					is_in_array = {
						array = global.cultures_used
						value = v 
					}
				}
			}
			log = "culture id [?v] ([v.GetSubcultureName]) is unused"
		}
		add_to_variable = { v = 1 }
	}
}
d_check_religion_use={
	
	every_state = {
		for_each_loop = {
			array = state_religion_array
			value = current_religion_t
			# add_to_variable = {
				# var = global.cultures_used^v
				# value = 1
			# }
			add_to_array = {
				array = global.religion_used
				value = current_religion_t
				#index = v
			}
		}
	}
	set_variable = { v = 1 }
	while_loop_effect = {
		limit = { check_variable = { v < 55 } }
		if = {
			limit = {
				NOT={
					is_in_array = {
						array = global.religion_used
						value = v 
					}
				}
			}
			log = "religion id [?v] ([current_religion.GetNationalReligion2]) is unused"
		}
		add_to_variable = { v = 1 }
	}
}