﻿namespace = necro_pops

#I still want pops from war if possible, but this is going on the backburner
necro_pops.1 = {
	type = country_event
	hidden = no
	title = "necro_pops.1.t"
	desc = "necro_pops.1.d"
	#placeholder image
	picture = crucifixion
	
	trigger = {
		religion = undead_pantheon
	}
	option = {
		name = "necro_pops.1.a"
		add_treasury = 0
	}
}

#kill non-undead pops in undead cities, raise at 50 percent, and kill undead in all other provinces. Now that the tithe is in place, consolidation really isn't necessary.
necro_pops.2 = {
	type = country_event
	hidden = yes
	title = "necro_pops.2.t"
	desc = "necro_pops.2.d"
	#placeholder image
	picture = crucifixion
	
	trigger = {
		#This is meant to ensure the pop management is run once on the year's end, not for every country. This has replaced checking for nations with undead_pantheon, because that means it could run multiple times and will stop working if the last undead nation falls. Check to make sure this is still the case later on, if lag builds up.
		NOT = {
			has_global_variable = necropops2checker
		}
	}
	
	immediate = {
		set_global_variable = {
			name = necropops2checker
			days = 30
		}
		name = "necro_pops.2.a"
		#find all non-undead pops in undead cities, kill or convert at 50-50, kill undead outside undead cities
		every_province = {
			limit = {
			}
			if = {
				limit = {
					has_province_modifier = undead_city
				}
				every_pops_in_province = {
					limit = {
						NOT = { pop_culture = undead }
					}
					random_list = {
						1 = {
							kill_pop = yes
						}
						1 = {
							set_pop_culture = undead
							set_pop_religion = undead_pantheon
						}
					}
				}
			}
			else if = {
				limit = {
					NOT = { has_province_modifier = undead_city }
				}
				every_pops_in_province = {
					limit = {
						pop_culture = undead
					}
					kill_pop = yes
				}
			}
		}
	}
}

#Pop cycling. Visible for now, with a goto for the province pops move to.
necro_pops.3 = {
	type = country_event
	hidden = no
	title = "necro_pops.3.t"
	desc = "necro_pops.3.d"
	#placeholder image
	picture = crucifixion
	
	goto_location = scope:undead_migration_target

	trigger = {
		religion = undead_pantheon
		any_owned_province = {
			has_province_modifier = undead_city
			total_population < population_cap
		}
		any_owned_province = {
			has_province_modifier = undead_city
			total_population > population_cap
		}
	}
	
	immediate = {
		random_owned_province = {
			limit = {
				has_province_modifier = undead_city
				total_population < population_cap
			}
			save_scope_as = undead_migration_target
		}
	}

	option = {
		name = "necro_pops.3.a"
		every_owned_province = {
			limit = {
				has_province_modifier = undead_city
				total_population > population_cap
			}
			random_pops_in_province = { move_pop = scope:undead_migration_target }
		}
	}
}

#Generating tithed undead for Necromantic nations with an undead city. 1 per 200 non-undead pops, managed by subtracting population of undead cities from country pop. TODO: consider tithing based on Necromantic pops instead of total living and increasing tithe as necromancy level rises.
necro_pops.4 = {
	type = country_event
	hidden = yes
	title = "necro_pops.4.t"
	desc = "necro_pops.4.d"
	#placeholder image
	picture = crucifixion
	
	trigger = {
		religion = undead_pantheon
		any_owned_province = { has_province_modifier = undead_city}
	}
	
	immediate = {
		while = {
			count = {
				value = 0
				add = country_population
				every_owned_province = {
					limit = { has_province_modifier = undead_city }
					subtract = total_population
				}
				divide = 200
			}
			random_owned_province = {
				limit = { has_province_modifier = undead_city }
				define_pop = {
					type = slaves
					culture = undead
					religion = undead_pantheon
				}
			}
		}
	}
	
	option = {
		name = "necro_pops.4.a"
	}
}

#Annual pop management is tedious, so we're letting players know what the deal is and then not giving constant updates
necro_pops.5 = {
	type = country_event
	hidden = no
	title = "necro_pops.5.t"
	desc = "necro_pops.5.d"
	#placeholder image
	picture = crucifixion
	
	trigger = {
		religion = undead_pantheon
		any_owned_province = { has_province_modifier = undead_city}
		NOT = { has_variable = necro_first_city }
	}
	
	immediate = {
		set_variable = { name = necro_first_city }
	}
	
	option = {
		name = "necro_pops.5.a"
	}
}

#Necrostorm, a final casting to tear down enemies
necro_pops.6 = {
	type = country_event
	hidden = yes
	title = "necro_pops.6.t"
	desc = "necro_pops.6.d"
	#placeholder image
	picture = crucifixion
	
	trigger = {
		religion = undead_pantheon
		has_country_modifier = necrostorm
	}
	
	immediate = {
		name = "necro_pops.6.a"
		random_country = {
			limit = {
				NOT = { religion = undead_pantheon }
			}
			random_owned_province = {
				every_pops_in_province = {
					kill_pop = yes
				}
			}
		}
	}
}