############################
#
# Necroids Events
#
# Written by Pierre du Plessis
#
############################

namespace = necroids

# Consumption event for necrophage
carrier_event = {
	id = necroids.1
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		OR = {
			has_building = building_necrophage_elevation_chamber
			has_building = building_necrophage_house_of_apotheosis
		}
		exists = owner
		owner = {
			has_origin = origin_necrophage
			has_trait = trait_necrophage
		}
		num_assigned_jobs = {
			job = necro_apprentice
			value >= 1
		}
	}

	immediate = {
		set_timed_carrier_flag = {
			flag = necrophage_alert_timer
			days = 2880
		}
		set_saved_date = {
			key = necrophage_elevation_timer
			days_from_present = 3600
			expires = 3600
		}
		weighted_random_owned_pop_group = {
			limit = {
				is_same_species = owner_species
				has_trait = trait_necrophage
			}
			species = { save_event_target_as = necrophage_pop_species }
		}
		export_trigger_value_to_variable = {
			trigger = num_assigned_jobs
			parameters = {
				job = necro_apprentice
			}
			variable = var_necrophaged_pops
		}
		set_variable = {
			which = var_necrophaged_pops_100
			value = var_necrophaged_pops
		}
		divide_variable = {
			which = var_necrophaged_pops_100
			value = 100
		}
		while = {
			count = var_necrophaged_pops_100
			root.owner = {
				if = {
					limit = { has_ascension_perk = ap_become_the_crisis }
					complete_crisis_objective = crisobj_purge_pops
				}
			}
			if = {
				limit = {
					owner = {
						OR = {
							has_active_tradition = tr_harmony_unity_of_self
							has_active_tradition = tr_synchronicity_unity_of_mind
						}
					}
				}
				owner = {
					add_monthly_resource_mult = {
						resource = unity
						value = 1.5
						min = 5
						max = 100
					}
				}
			}
		}
		if = {
			limit = {
				exists = event_target:necrophage_pop_species
			}
			create_pop_group = {
				species = event_target:necrophage_pop_species
				size = var_necrophaged_pops
				effect = {
					save_event_target_as = last_created_pop_group
				}
			}
		}
		else = {
			create_pop_group = {
				species = owner_species
				size = var_necrophaged_pops
				effect = {
					save_event_target_as = last_created_pop_group
				}
			}
		}
		# root is the carrier, which is the Arkship ship for Nomads - scope to the colony for the planet-class work below.
		colony = {
			save_event_target_as = necrophage_apotheosis_colony
		}
		if = {
			limit = {
				event_target:last_created_pop_group = {
					NOT = { ideal_planet_class = event_target:necrophage_apotheosis_colony }
				}
				event_target:necrophage_apotheosis_colony = {
					NOR = {
						uses_district_set = habitat
						uses_district_set = ring_world
						uses_district_set = city_world
						uses_district_set = hive_world
						uses_district_set = machine_world
						is_planet_class = pc_gaia
						is_planet_class = pc_relic
					}
				}
				owner = {
					has_technology = tech_glandular_acclimation
					NOT = { ideal_planet_class = event_target:necrophage_apotheosis_colony }
					has_mutation_environmental_integration_tradition = no
				}
			}
			event_target:last_created_pop_group = {
				modify_species = {
					ideal_planet_class = event_target:necrophage_apotheosis_colony
				}
			}
		}
		every_owned_pop_job = {
			limit = {
				has_job_type = necro_apprentice
			}
			kill_assigned_pop_amount = {
				percentage = 1
			}
		}
		# Colony message targets are not implemented (AUG-55014) - target the planet, or the Arkship fleet for Nomads
		if = {
			limit = { is_planet_class = pc_ark }
			fleet = {
				save_event_target_as = necrophage_toast_target
			}
		}
		else = {
			save_event_target_as = necrophage_toast_target
		}
		create_message = {
			type = MESSAGE_NECROPHAGE_POPS_NECROPHAGED
			localization = MESSAGE_NECROPHAGE_POPS_NECROPHAGED
			days = 30
			target = event_target:necrophage_toast_target
			variable = {
				type = name
				localization = PLANET
				scope = event_target:necrophage_apotheosis_colony
			}
			variable = {
				type = variable
				varname = var_necrophaged_pops
				localization = VALUE
				scope = this
			}
		}
		set_variable = {
			which = var_necrophaged_pops
			value = 0
		}
		set_variable = {
			which = var_necrophaged_pops_100
			value = 0
		}
	}
}

#necroids.2 Tell player about necrophage mechanic 180 days before it happens
country_event = {
	id = necroids.2
	title = necroids.2.name
	desc = {
		trigger = { is_hive_empire = no }
		text = necroids.2.desc
	}
	desc = {
		trigger = { is_hive_empire = yes }
		text = necroids.2.desc.gestalt
	}
	show_sound = event_necrophage
	picture = GFX_evt_necrophage
	is_triggered_only = yes

	trigger = {
		capital_scope = {
			colony_age > 8
			has_building = building_necrophage_elevation_chamber
			num_assigned_jobs = {
				job = necro_apprentice
				value >= 1
			}
		}
	}

	immediate = {
		capital_scope = {
			random_owned_species = {
				limit = { species_can_be_necrophaged = yes }
				save_event_target_as = necrophage_species
			}
		}
	}

	option = {
		name = necroids.2.A
		custom_tooltip = necroids.2.A.tooltip
	}
}

# AI help for getting secondary necrophage species on the planet
carrier_event = {
	id = necroids.3
	is_triggered_only = yes
	hide_window = yes

	trigger = {
		exists = owner
		owner = {
			is_ai = yes
			has_origin = origin_necrophage
			has_trait = trait_necrophage
			any_owned_species = {
				species_can_be_necrophaged = yes
				NOT = {
					has_citizenship_type = { type = citizenship_purge country = root.owner }
				}
			}
		}
		OR = {
			AND = {
				any_owned_species = {
					is_same_species = root.owner_species
				}
				NOT = {
					any_owned_species = {
						species_can_be_necrophaged = yes
					}
				}
			}
			AND = {
				NOT = {
					any_owned_species = {
						is_same_species = root.owner_species
					}
				}
				any_owned_species = {
					species_can_be_necrophaged = yes
				}
			}
		}
	}

	immediate = {
		if = {
			limit = {
				any_owned_species = {
					is_same_species = root.owner_species
				}
				NOT = {
					any_owned_species = {
						species_can_be_necrophaged = yes
					}
				}
			}
			owner = {
				random_owned_planet = {
					limit = {
						any_owned_species = {
							species_can_be_necrophaged = yes
							NOT = {
								has_citizenship_type = { type = citizenship_purge country = root.owner }
							}
						}
						count_owned_pop_amount = {
							count >= 200
							limit = {
								species = {
									species_can_be_necrophaged = yes
									NOT = {
										has_citizenship_type = { type = citizenship_purge country = root.owner }
									}
								}
							}
						}
					}
					weighted_random_owned_pop_group = {
						limit = {
							species = {
								species_can_be_necrophaged = yes
								NOT = {
									has_citizenship_type = { type = citizenship_purge country = root.owner }
								}
							}
						}
						resettle_pop_group = {
							POP_GROUP = this
							PLANET = root
							AMOUNT = 100
						}
					}
				}
			}
		}
		else = {
			owner = {
				random_owned_planet = {
					limit = {
						any_owned_species = {
							is_same_species = root.owner_species
						}
						count_owned_pop_amount = {
							count >= 200
							limit = {
								species = {
									is_same_species = root.owner_species
								}
							}
						}
					}
					random_owned_pop_group = {
						limit = {
							species = {
								is_same_species = root.owner_species
							}
						}
						resettle_pop_group = {
							POP_GROUP = this
							PLANET = root
							AMOUNT = 100
						}
					}
				}
			}
		}
	}
}

# Make starting colonisables be Primitives
country_event = {
	id = necroids.4
	is_triggered_only = yes
	hide_window = yes

	trigger = {
		has_origin = origin_necrophage
		is_nomadic = no
	}

	immediate = {
		capital_scope.solar_system = {
			closest_system = {
				limit = { has_star_flag = neighbor_t1_first_colony }
				max_steps = 3
				random_system_planet = {
					limit = {
						is_colonizable = yes
						is_colony = no
						has_planet_flag = prescripted_ideal
					}
					generate_very_early_pre_ftls_on_planet = yes
					set_name = random
					change_colony_foundation_date = -461

					while = {
						count = 4
						create_army = {
							owner = last_created_country
							type = primitive_additional_army
							species = last_created_country
						}
					}
				}
			}
			reroll_random = yes
			closest_system = {
				limit = { has_star_flag = neighbor_t2_second_colony }
				max_steps = 6
				random_system_planet = {
					limit = {
						is_colonizable = yes
						is_colony = no
						has_planet_flag = prescripted_ideal
					}
					generate_very_early_pre_ftls_on_planet = yes
					set_name = random
					change_colony_foundation_date = -1275

					while = {
						count = 4
						create_army = {
							owner = last_created_country
							type = primitive_additional_army
							species = last_created_country
						}
					}
				}
			}
		}
	}
}

# Necrophage Purge

# on_pop_purged
# This = Planet scope
# From = Country
# FromFrom = Pop
carrier_event = {
	id = necroids.5
	is_triggered_only = yes
	hide_window = yes

	trigger = {
		NOT = {
			has_designation = col_cosmogenesis_world
		}
		exists = fromfrom #crisis.202 can remove the last pop
		fromfrom = {
			has_purge_type = {
				type = purge_necrophage
			}
		}
	}

	immediate = {
		if = {
			limit = {
				from = {
					has_origin = origin_nomadic_purger
					exists = overlord
				}
			}
			weighted_random_owned_pop_group = {
				limit = {
					is_same_species = owner_species
				}
				species = { save_event_target_as = necrophage_pop_species }
			}
			if = {
				limit = {
					exists = event_target:necrophage_pop_species
				}
				create_pop_group = {
					species = event_target:necrophage_pop_species
					size = local_pop_amount 
					effect = {
						save_event_target_as = last_created_pop_group
					}
				}
			}
			else = {
				create_pop_group = {
					species = owner_species
					size = local_pop_amount 
					effect = {
						save_event_target_as = last_created_pop_group
					}
				}
			}
			if = {
				limit = {
					owner = {
						has_technology = tech_glandular_acclimation
						NOT = { ideal_planet_class = root }
					}
					event_target:last_created_pop_group = {
						NOT = { ideal_planet_class = root }
					}
					NOR = {
						uses_district_set = habitat
						uses_district_set = ring_world
						uses_district_set = city_world
						uses_district_set = hive_world
						uses_district_set = machine_world
						is_planet_class = pc_gaia
						is_planet_class = pc_relic
					}
				}
				event_target:last_created_pop_group = {
					modify_species = {
						ideal_planet_class = root
					}
				}
			}
			
			if = {
				limit = {
					from = {
						OR = {
							has_active_tradition = tr_harmony_unity_of_self
							has_active_tradition = tr_synchronicity_unity_of_mind
						}
					}
				}
				# nomad purge-vassal: hand the unity up to the necrophage overlord, flat per 100 pops to match the other purge civics
				from.overlord = {
					add_resource = {
						unity = 1.5
						mult = root.value:per_100_local_pop_amount
					}
				}
			}
			every_country = {
				limit = {
					has_communications = FROM.overlord
					NOT = { is_country = FROM.overlord }
					NOT = { is_same_species = FROMFROM }
					NOT = { has_ai_personality_behaviour = purger }
					NOT = { has_origin = origin_necrophage }
					NOT = { has_ethic = ethic_gestalt_consciousness }
					NOR = {
						has_valid_civic = civic_death_cult
						has_valid_civic = civic_death_cult_corporate
					}
					OR = {
						is_country_type = default
						has_ai_personality = awakened_fallen_empire_xenophile
					}
					has_intel_level = {
						who = FROM.overlord
						category = economy
						level >= 2
					}
				}
				while = {
					count = root.local_pop_amount
					add_opinion_modifier = { who = FROM.overlord modifier = opinion_necrophage }
				}
			}
			every_country = {
				limit = {
					has_communications = FROM.overlord
					NOT = { is_country = FROM.overlord }
					is_same_species = FROMFROM
					has_intel_level = {
						who = FROM.overlord
						category = economy
						level >= 1 #lower because you'd hear from a friend of a friend
					}
				}
				while = {
					count = root.local_pop_amount
					add_opinion_modifier = { who = FROM.overlord modifier = opinion_necrophage_our_species }
				}
			}
		}
		else_if = {
			limit = {
				from? = {
					NOT = {
						has_origin = origin_nomadic_purger
					}
				}
			}
			weighted_random_owned_pop_group = {
				limit = {
					is_same_species = owner_species
				}
				species = { save_event_target_as = necrophage_pop_species }
			}

			if = {
				limit = {
					exists = event_target:necrophage_pop_species
				}
				create_pop_group = {
					species = event_target:necrophage_pop_species
					size = 0
					effect = {
						save_event_target_as = last_created_pop_group
					}
				}
			}
			else = {
				create_pop_group = {
					species = owner_species
					size = 0
					effect = {
						save_event_target_as = last_created_pop_group
					}
				}
			}
			if = {
				limit = {
					owner = {
						has_technology = tech_glandular_acclimation
						NOT = { ideal_planet_class = root }
					}
					event_target:last_created_pop_group = {
						NOT = { ideal_planet_class = root }
					}
					NOR = {
						uses_district_set = habitat
						uses_district_set = ring_world
						uses_district_set = city_world
						uses_district_set = hive_world
						uses_district_set = machine_world
						is_planet_class = pc_gaia
						is_planet_class = pc_relic
					}
				}
				event_target:last_created_pop_group = {
					modify_species = {
						ideal_planet_class = root
					}
				}
			}
			event_target:last_created_pop_group = {
				add_pop_amount = {
					amount = root.local_pop_amount
				}
			}
			while = {
				count = value:per_100_local_pop_amount
				if = {
					limit = {
						from = {
							OR = {
								has_active_tradition = tr_harmony_unity_of_self
								has_active_tradition = tr_synchronicity_unity_of_mind
							}
						}
					}
					from = {
						add_monthly_resource_mult = {
							resource = unity
							value = 1.5
							min = 5
							max = 100
						}
					}
				}
			}
			every_country = {
				limit = {
					has_communications = FROM
					NOT = { is_country = FROM }
					NOT = { is_same_species = FROMFROM }
					NOT = { has_ai_personality_behaviour = purger }
					NOT = { has_origin = origin_necrophage }
					NOT = { has_ethic = ethic_gestalt_consciousness }
					NOR = {
						has_valid_civic = civic_death_cult
						has_valid_civic = civic_death_cult_corporate
					}
					OR = {
						is_country_type = default
						has_ai_personality = awakened_fallen_empire_xenophile
					}
					has_intel_level = {
						who = FROM
						category = economy
						level >= 2
					}
				}
				while = {
					count = root.local_pop_amount
					add_opinion_modifier = { who = FROM modifier = opinion_necrophage }
				}
			}
			every_country = {
				limit = {
					has_communications = FROM
					NOT = { is_country = FROM }
					is_same_species = FROMFROM
					has_intel_level = {
						who = FROM
						category = economy
						level >= 1 #lower because you'd hear from a friend of a friend
					}
				}
				while = {
					count = root.local_pop_amount
					add_opinion_modifier = { who = FROM modifier = opinion_necrophage_our_species }
				}
			}
		}
	}
}

# AI help event
country_event = {
	id = necroids.6
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		is_ai = yes
	}

	immediate = {
		every_owned_planet = {
			limit = {
				has_modifier = planet_culture_shock
				count_owned_pop_amount = {
					count <= 100
					limit = {
						is_same_species = root
					}
				}
			}
			while = {
				count = 3
				limit = {
					count_owned_pop_amount = {
						count <= 200
						limit = {
							is_same_species = root
						}
					}
				}
				root = {
					capital_scope = {
						random_owned_pop_group = {
							limit = {
								species = { is_same_species = root }
							}
							resettle_pop_group = {
								POP_GROUP = this
								PLANET = prevprevprev
								AMOUNT = 100
							}
						}
					}
				}
			}
			add_district = district_city
		}
		every_planet_within_border = {
			limit = {
				is_colony = yes
				owner = {
					is_country_type = primitive
				}
			}
			if = {
				limit = {
					has_observation_outpost = yes
				}
				observation_outpost = {
					dismantle = yes
				}
			}
			add_modifier = {
				modifier = planet_culture_shock
				years = 10
			}
			set_owner = ROOT

			while = {
				count = 3
				root = {
					capital_scope = {
						random_owned_pop_group = {
							limit = {
								species = { is_same_species = root }
							}
							resettle_pop_group = {
								POP_GROUP = this
								PLANET = prevprevprev
								AMOUNT = 100
							}
						}
					}
				}
			}
			add_district = district_city
		}
	}
}

#FromFrom = Former Owner
#From = Country scope (new owner)
#This = Planet scope
carrier_event = {
	id = necroids.10
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		from = {
			has_origin = origin_necrophage
			is_hive_empire = yes
		}
		any_owned_species = {
			has_trait = trait_hive_mind
			has_citizenship_type = { country = from type = citizenship_purge }
		}
	}

	immediate = {
		every_owned_species = {
			limit = {
				has_trait = trait_hive_mind
				has_citizenship_type = { country = from type = citizenship_purge }
			}
			species = {
				set_citizenship_type = { country = from type = citizenship_full }
			}
		}
	}
}


#Reanimated Armies: Reanimating the enemy's army as your own
#An army has been killed in ground combat
# This = owner
# From = army
# FromFrom = opponent
# FromFromFrom = planet
country_event = {
	id = necroids.15
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		exists = fromfrom
		fromfrom = {
			has_valid_civic = civic_reanimated_armies
		}
		from = {
			exists = species
			species = {
				is_robotic = no
			}
		}
		NOT = { is_country_type = "gate_builders" }
	}

	immediate = {
		random_list = {
			2 = { }
			1 = {
				fromfromfrom = {
					create_army = {
						owner = root.fromfrom
						type = undead_army
						species = root.from.species
					}
				}
			}
		}
	}
}

# Mechromancy AP event: Machine Empire can now be Reanimators

country_event = {
	id = necroids.20
	title = necroids.20.name
	show_sound = event_necrophage
	picture = GFX_evt_mechromancy
	is_triggered_only = yes

	desc = necroids.20.desc

	option = {
		name = necroids.20.a
	}
}

# on_pop_purged
# This = Planet scope
# From = Country
# FromFrom = Pop group
# local_pop_amount = variable of how many pops were purged
# Mechromancy - CyberZombies
carrier_event = {
	id = necroids.25
	is_triggered_only = yes
	hide_window = yes
	pre_triggers = {
		has_owner = yes
	}
	trigger = {
		from = {
			is_machine_empire = yes
			OR = {
				has_ascension_perk = ap_mechromancy
				AND = {
					has_origin = origin_nomadic_purger
					overlord? = {
						has_ascension_perk = ap_mechromancy
					}
				}
			}
		}
		exists = fromfrom #crisis.202 can remove the last pop
		fromfrom = {
			is_sapient = yes
			NOR = {
				is_robotic_species = yes
				has_purge_type = {
					type = purge_displacement
				}
				has_purge_type = {
					type = purge_cosmogenesis
				}
				has_purge_type = {
					type = purge_cosmogenesis_lathe_resettle
				}
			}
		}
	}

	immediate = {
		random = {
			chance = 33
			# 33% Chance to create a cyber-zombie per pop.

			# If a modified species hasn't been created, create one
			# and save them as an event target
			fromfrom = {
				species = {
					save_event_target_as = old_species
				}
				if = {
					limit = {
						NOT = {
							any_galaxy_species = {
								has_species_flag = cyber_zombie_species_of@event_target:old_species
							}
						}
					}
					create_species = {
						is_mod = yes
						name = this
						plural = this
						class = this
						portrait = this
						traits = this
						can_be_modified = this
						homeworld = this
						namelist = this
						gender = this
						traits = {
							trait = trait_zombie
							trait = trait_cybernetic
						}
						effect = {
							modify_species = {
								species = this
								remove_trait = trait_self_modified
								remove_trait = trait_latent_psionic
								remove_trait = trait_robot_latent_psionic
								remove_trait = trait_psionic
								remove_trait = trait_robot_psionic
								change_scoped_species = no
								effect = {
									set_species_flag = cyber_zombie_species_of@event_target:old_species
									save_event_target_as = zombie_species
								}
							}
						}
					}
				}
				# If a modified species does exist, save them as the event target
				else = {
					random_galaxy_species = {
						limit = {
							has_species_flag = cyber_zombie_species_of@event_target:old_species
						}
						save_event_target_as = zombie_species
					}
				}
			}
			if = {
				limit = {
					from = {
						has_origin = origin_nomadic_purger
						exists = overlord
					}
				}
				from.overlord.capital_scope = {
					create_pop_group = {
						pop_group = fromfrom
						species = event_target:zombie_species
						size = local_pop_amount
						category = maintenance_drone
						effect = {
							set_citizenship_type = {
								country = from.overlord
								type = citizenship_full_machine
							}
						}
					}
					if = { # assimilator empire
						limit = {
							from.overlord = {
								is_machine_empire = yes
								has_organo_machine_interfacing_assimilator_synergy = yes
							}
						}
						divide_variable = {
							which = local_pop_amount
							value = 5
						}
						round_variable_to_closest = {
							which = local_pop_amount
							value = 1
						}
						while = {
							count = local_pop_amount
							from.overlord = { generate_cyborg_treats = yes } # scripted effect
						}
					}
				}
			}
			else = {
				create_pop_group = {
					pop_group = fromfrom
					species = event_target:zombie_species
					size = local_pop_amount
					category = maintenance_drone
					effect = {
						set_citizenship_type = {
							country = root.owner
							type = citizenship_full_machine
						}
					}
				}
				if = { # assimilator empire
					limit = {
						root.owner = {
							is_machine_empire = yes
							has_organo_machine_interfacing_assimilator_synergy = yes
						}
					}
					divide_variable = {
						which = local_pop_amount
						value = 5
					}
					round_variable_to_closest = {
						which = local_pop_amount
						value = 1
					}
					while = {
						count = local_pop_amount
						root.owner = { generate_cyborg_treats = yes } # scripted effect
					}
				}
			}
		}
	}
}
