# 
# Yes, it's mostly taken from EaW mod. If you a EaW creator and want me to not use this code, please contact me. Otherwise thank you.
#
decrease_population_on_nuke = {
	log = "[GetDateText]: Nuke dropped on [This.GetName], owner [This.owner.GetName], dropped by [Root.GetName]"
	set_temp_variable = { temp_var = random } #0 to 1
	owner = { set_temp_variable = { PREV.prenuke_country_manpower = manpower_k } }
	if = {
		limit = { state_population > 999999 }
		set_temp_variable = { pop_to_kill = state_population_k }
		set_state_flag = state_population_thousands
	}
	else = {
		set_temp_variable = { pop_to_kill = state_population }
	}
	if = {
		limit = { has_state_category = megalopolis } #20 kt bomb in a very dense urban region - 250,000 - 300,000 casualties
		multiply_temp_variable = { temp_var = 50000 } # 300,000 - 250,000
		add_to_temp_variable = { temp_var = 250000 }
		set_temp_variable = { pop_to_kill = temp_var }
		clr_state_flag = state_population_thousands
		if = {
			limit = {
				OR  = {
					state_population < 800000
					has_state_flag = recently_nuked
				}
			}
			set_temp_variable = { temp_var = random } #0 to 1
			multiply_temp_variable = { temp_var = 0.05 } # 0.1 - 0.05
			add_to_temp_variable = { temp_var = 0.05 }
			multiply_temp_variable = { pop_to_kill = temp_var }
		}
	}
	else_if = {
		limit = { has_state_category = metropolis } #20 kt bomb in a dense urban region - 175,000 - 250,000 casualties
		multiply_temp_variable = { temp_var = 75000 } # 250,000 - 175,000
		add_to_temp_variable = { temp_var = 175000 }
		set_temp_variable = { pop_to_kill = temp_var }
		clr_state_flag = state_population_thousands
		if = {
			limit = {
				OR  = {
					state_population < 600000
					has_state_flag = recently_nuked
				}
			}
			set_temp_variable = { temp_var = random } #0 to 1
			multiply_temp_variable = { temp_var = 0.05 } # 0.1 - 0.05
			add_to_temp_variable = { temp_var = 0.05 }
			multiply_temp_variable = { pop_to_kill = temp_var }
		}
	}
	else_if = {
		limit = { has_state_category = large_city } #20 kt bomb in an urban region - 100,000 - 150,000 casualties
		multiply_temp_variable = { temp_var = 75000 } # 250,000 - 175,000
		add_to_temp_variable = { temp_var = 175000 }
		set_temp_variable = { pop_to_kill = temp_var }
		clr_state_flag = state_population_thousands
		if = {
			limit = {
				OR  = {
					state_population < 500000
					has_state_flag = recently_nuked
				}
			}
			set_temp_variable = { temp_var = random } #0 to 1
			multiply_temp_variable = { temp_var = 0.05 } # 0.1 - 0.05
			add_to_temp_variable = { temp_var = 0.05 }
			multiply_temp_variable = { pop_to_kill = temp_var }
		}
	}
	else_if = {
		limit = { has_state_category = city } #20 kt bomb in a sparse urban region - 50,000 - 100,000 casualties
		multiply_temp_variable = { temp_var = 50000 } # 100,000 - 50,000
		add_to_temp_variable = { temp_var = 50000 }
		set_temp_variable = { pop_to_kill = temp_var }
		clr_state_flag = state_population_thousands
		if = {
			limit = {
				OR  = {
					state_population < 500000
					has_state_flag = recently_nuked
				}
			}
			set_temp_variable = { temp_var = random } #0 to 1
			multiply_temp_variable = { temp_var = 0.05 } # 0.1 - 0.05
			add_to_temp_variable = { temp_var = 0.05 }
			multiply_temp_variable = { pop_to_kill = temp_var }
		}
	}
	else_if = {
		limit = { has_state_category = large_town } #20 kt bomb in a very sparse urban region - 20,000 - 80,000 casualties
		multiply_temp_variable = { temp_var = 60000 } # 80,000 - 20,000
		add_to_temp_variable = { temp_var = 20000 }
		set_temp_variable = { pop_to_kill = temp_var }
		clr_state_flag = state_population_thousands
		if = {
			limit = {
				OR  = {
					state_population < 400000
					has_state_flag = recently_nuked
				}
			}
			set_temp_variable = { temp_var = random } #0 to 1
			multiply_temp_variable = { temp_var = 0.05 } # 0.1 - 0.05
			add_to_temp_variable = { temp_var = 0.05 }
			multiply_temp_variable = { pop_to_kill = temp_var }
		}
	}
	else_if = {
		limit = { has_state_category = town } #20 kt bomb in a developed rural region - 20,000 - 50,000 casualties
		multiply_temp_variable = { temp_var = 30000 } # 50,000 - 20,000
		add_to_temp_variable = { temp_var = 20000 }
		set_temp_variable = { pop_to_kill = temp_var }
		clr_state_flag = state_population_thousands
		if = {
			limit = {
				OR  = {
					state_population < 300000
					has_state_flag = recently_nuked
				}
			}
			set_temp_variable = { temp_var = random } #0 to 1
			multiply_temp_variable = { temp_var = 0.05 } # 0.1 - 0.05
			add_to_temp_variable = { temp_var = 0.05 }
			multiply_temp_variable = { pop_to_kill = temp_var }
		}
	}
	else_if = {
		limit = { has_state_category = rural } #20 kt bomb in a rural region - 5,000 - 25,000 casualties
		multiply_temp_variable = { temp_var = 20000 } # 25,000 - 5,000
		add_to_temp_variable = { temp_var = 5000 }
		set_temp_variable = { pop_to_kill = temp_var }
		clr_state_flag = state_population_thousands
		if = {
			limit = {
				OR  = {
					state_population < 200000
					has_state_flag = recently_nuked
				}
			}
			set_temp_variable = { temp_var = random } #0 to 1
			multiply_temp_variable = { temp_var = 0.05 } # 0.1 - 0.05
			add_to_temp_variable = { temp_var = 0.05 }
			multiply_temp_variable = { pop_to_kill = temp_var }
		}
	}
	else_if = {
		limit = { has_state_category = pastoral } #20 kt bomb in a sparse rural region - 500 - 2,500 casualties
		multiply_temp_variable = { temp_var = 1000 } # 2500 - 500
		add_to_temp_variable = { temp_var = 500 }
		set_temp_variable = { pop_to_kill = temp_var }
		clr_state_flag = state_population_thousands
		if = {
			limit = {
				OR  = {
					state_population < 200000
					has_state_flag = recently_nuked
				}
			}
			set_temp_variable = { temp_var = random } #0 to 1
			multiply_temp_variable = { temp_var = 0.05 } # 0.1 - 0.05
			add_to_temp_variable = { temp_var = 0.05 }
			multiply_temp_variable = { pop_to_kill = temp_var }
		}
	}
	else_if = {
		limit = { has_state_category = small_island } #20 kt bomb on a small island - 20% - 50%
		multiply_temp_variable = { temp_var = 0.3 } # 0.5 - 0.2
		add_to_temp_variable = { temp_var = 0.2 }
		multiply_temp_variable = { pop_to_kill = temp_var }
		if = {
			limit = {
				has_state_flag = recently_nuked
			}
			multiply_temp_variable = { pop_to_kill = 0.2 }
		}
	}
	else_if = {
		limit = { has_state_category = tiny_island } #20 kt bomb on a tiny island - 30% - 75%
		multiply_temp_variable = { temp_var = 0.45 } # 0.75 - 0.3
		add_to_temp_variable = { temp_var = 0.3 }
		multiply_temp_variable = { pop_to_kill = temp_var }
		if = {
			limit = {
				has_state_flag = recently_nuked
			}
			multiply_temp_variable = { pop_to_kill = 0.2 }
		}
	}
	else_if = {
		limit = { has_state_category = wasteland } #20 kt bomb on a literally nothing - 0 - 1%
		multiply_temp_variable = { pop_to_kill = temp_var }
		if = {
			limit = {
				has_state_flag = recently_nuked
			}
			multiply_temp_variable = { pop_to_kill = 0.2 }
		}
	}
	else_if = {
		limit = { has_state_category = enclave } #20 kt bomb on a an enclave - 40% - 75%
		multiply_temp_variable = { temp_var = 0.35 } # 0.75 - 0.4
		add_to_temp_variable = { temp_var = 0.4 }
		multiply_temp_variable = { pop_to_kill = temp_var }
		if = {
			limit = {
				has_state_flag = recently_nuked
			}
			multiply_temp_variable = { pop_to_kill = 0.2 }
		}
	}
	
	owner = {
		set_temp_variable = {
			civil_defence_factor = modifier@industry_air_damage_factor
		}
		divide_temp_variable = {
			civil_defence_factor = 3
		}
		add_to_temp_variable = {
			civil_defence_factor = 1
		}
		clamp_temp_variable = {
			var = civil_defence_factor
			min = 0.5
			max = 1.25
		}
		multiply_temp_variable = { pop_to_kill = civil_defence_factor }
	}
	if = {
		limit = {
			has_state_flag = state_population_thousands
		}
		log = "Nuke dropped on [This.GetName], state_population_thousands, expected [?pop_to_kill]*1000 casualties"
		while_loop_effect = {
			limit = { check_variable = { pop_to_kill > 999 } }
			log = "Nuke dropped on [This.GetName], 1000000 dead"
			subtract_from_temp_variable = { pop_to_kill = 1000 }
			add_manpower = -1000000
		}
		multiply_temp_variable = { pop_to_kill = 1000 }
	}
	round_temp_variable = pop_to_kill
	log = "Nuke dropped on [This.GetName], [?pop_to_kill] dead"
	multiply_temp_variable = { pop_to_kill = -1 }
	add_manpower = pop_to_kill

	#take care of country manpower change
	owner = { set_temp_variable = { PREV.afternuke_country_manpower = manpower_k } }
	log = "Nuke dropped on [This.GetName], prenuke_country_manpower [?prenuke_country_manpower], afternuke_country_manpower [?afternuke_country_manpower]"
	subtract_from_temp_variable = { afternuke_country_manpower = prenuke_country_manpower }
	while_loop_effect = {
		limit = { check_variable = { afternuke_country_manpower > 9 } }
		log = "Nuke dropped on [This.GetName], afternuke_country_manpower adjustment, 10000 removed"
		subtract_from_temp_variable = { afternuke_country_manpower = 10 }
		owner = { add_manpower = -10000 }
	}
	multiply_temp_variable = { afternuke_country_manpower = 1000 }
	round_temp_variable = afternuke_country_manpower
	log = "Nuke dropped on [This.GetName], afternuke_country_manpower adjustment, [?afternuke_country_manpower] removed"
	multiply_temp_variable = { afternuke_country_manpower = -1 }
	owner = { add_manpower = afternuke_country_manpower }

}