xit_convert_one_pop = {
	if = {
		limit = {
			exists = owner
			owner = { has_valid_civic = civic_xit_biosecurity_directorate }
		}
		random_list = {
			45 = {
				if = {
					limit = {
						any_owned_pop_group = {
							pop_group_size >= 100
							species = {
								is_robotic = no
								NOT = { has_trait = trait_mechanical }
								NOT = { has_trait = trait_xenomorph }
							}
						}
						any_owned_pop_group = {
							species = { has_trait = trait_xenomorph }
						}
					}
					weighted_random_owned_pop_group = {
						limit = {
							species = { has_trait = trait_xenomorph }
						}
						species = { save_event_target_as = xit_xenomorph_species }
					}
					weighted_random_owned_pop_group = {
						limit = {
							pop_group_size >= 100
							species = {
								is_robotic = no
								NOT = { has_trait = trait_mechanical }
								NOT = { has_trait = trait_xenomorph }
							}
						}
						kill_single_pop = yes
					}
					create_pop_group = {
						species = event_target:xit_xenomorph_species
						size = 100
					}
				}
			}
			55 = { }
		}
	}
	else_if = {
		limit = {
			exists = owner
			owner = { has_valid_civic = civic_xit_quarantine_authority }
		}
		random_list = {
			60 = {
				if = {
					limit = {
						any_owned_pop_group = {
							pop_group_size >= 100
							species = {
								is_robotic = no
								NOT = { has_trait = trait_mechanical }
								NOT = { has_trait = trait_xenomorph }
							}
						}
						any_owned_pop_group = {
							species = { has_trait = trait_xenomorph }
						}
					}
					weighted_random_owned_pop_group = {
						limit = {
							species = { has_trait = trait_xenomorph }
						}
						species = { save_event_target_as = xit_xenomorph_species }
					}
					weighted_random_owned_pop_group = {
						limit = {
							pop_group_size >= 100
							species = {
								is_robotic = no
								NOT = { has_trait = trait_mechanical }
								NOT = { has_trait = trait_xenomorph }
							}
						}
						kill_single_pop = yes
					}
					create_pop_group = {
						species = event_target:xit_xenomorph_species
						size = 100
					}
				}
			}
			40 = { }
		}
	}
	else_if = {
		limit = {
			any_owned_pop_group = {
				pop_group_size >= 100
				species = { has_trait = trait_xit_biosealed_lineage }
			}
		}
		random_list = {
			55 = {
				if = {
					limit = {
						any_owned_pop_group = {
							pop_group_size >= 100
							species = {
								is_robotic = no
								NOT = { has_trait = trait_mechanical }
								NOT = { has_trait = trait_xenomorph }
							}
						}
						any_owned_pop_group = {
							species = { has_trait = trait_xenomorph }
						}
					}
					weighted_random_owned_pop_group = {
						limit = {
							species = { has_trait = trait_xenomorph }
						}
						species = { save_event_target_as = xit_xenomorph_species }
					}
					weighted_random_owned_pop_group = {
						limit = {
							pop_group_size >= 100
							species = {
								is_robotic = no
								NOT = { has_trait = trait_mechanical }
								NOT = { has_trait = trait_xenomorph }
							}
						}
						kill_single_pop = yes
					}
					create_pop_group = {
						species = event_target:xit_xenomorph_species
						size = 100
					}
				}
			}
			45 = { }
		}
	}
	else_if = {
		limit = {
			any_owned_pop_group = {
				pop_group_size >= 100
				species = { has_trait = trait_xit_quarantine_adapted }
			}
		}
		random_list = {
			70 = {
				if = {
					limit = {
						any_owned_pop_group = {
							pop_group_size >= 100
							species = {
								is_robotic = no
								NOT = { has_trait = trait_mechanical }
								NOT = { has_trait = trait_xenomorph }
							}
						}
						any_owned_pop_group = {
							species = { has_trait = trait_xenomorph }
						}
					}
					weighted_random_owned_pop_group = {
						limit = {
							species = { has_trait = trait_xenomorph }
						}
						species = { save_event_target_as = xit_xenomorph_species }
					}
					weighted_random_owned_pop_group = {
						limit = {
							pop_group_size >= 100
							species = {
								is_robotic = no
								NOT = { has_trait = trait_mechanical }
								NOT = { has_trait = trait_xenomorph }
							}
						}
						kill_single_pop = yes
					}
					create_pop_group = {
						species = event_target:xit_xenomorph_species
						size = 100
					}
				}
			}
			30 = { }
		}
	}
	else_if = {
		limit = {
			any_owned_pop_group = {
				pop_group_size >= 100
				species = { has_trait = trait_xit_xenobiology_screened }
			}
		}
		random_list = {
			75 = {
				if = {
					limit = {
						any_owned_pop_group = {
							pop_group_size >= 100
							species = {
								is_robotic = no
								NOT = { has_trait = trait_mechanical }
								NOT = { has_trait = trait_xenomorph }
							}
						}
						any_owned_pop_group = {
							species = { has_trait = trait_xenomorph }
						}
					}
					weighted_random_owned_pop_group = {
						limit = {
							species = { has_trait = trait_xenomorph }
						}
						species = { save_event_target_as = xit_xenomorph_species }
					}
					weighted_random_owned_pop_group = {
						limit = {
							pop_group_size >= 100
							species = {
								is_robotic = no
								NOT = { has_trait = trait_mechanical }
								NOT = { has_trait = trait_xenomorph }
							}
						}
						kill_single_pop = yes
					}
					create_pop_group = {
						species = event_target:xit_xenomorph_species
						size = 100
					}
				}
			}
			25 = { }
		}
	}
	else = {
		if = {
			limit = {
				any_owned_pop_group = {
					pop_group_size >= 100
					species = {
						is_robotic = no
						NOT = { has_trait = trait_mechanical }
						NOT = { has_trait = trait_xenomorph }
					}
				}
				any_owned_pop_group = {
					species = { has_trait = trait_xenomorph }
				}
			}
			weighted_random_owned_pop_group = {
				limit = {
					species = { has_trait = trait_xenomorph }
				}
				species = { save_event_target_as = xit_xenomorph_species }
			}
			weighted_random_owned_pop_group = {
				limit = {
					pop_group_size >= 100
					species = {
						is_robotic = no
						NOT = { has_trait = trait_mechanical }
						NOT = { has_trait = trait_xenomorph }
					}
				}
				kill_single_pop = yes
			}
			create_pop_group = {
				species = event_target:xit_xenomorph_species
				size = 100
			}
		}
	}
}

xit_clean_one_remnant_pop = {
	if = {
		limit = {
			NOT = {
				any_owned_pop_group = {
					pop_group_size >= 100
					species = {
						is_robotic = no
						NOT = { has_trait = trait_mechanical }
						NOT = { has_trait = trait_xenomorph }
					}
				}
			}
			any_owned_pop_group = {
				pop_group_size > 0
				pop_group_size < 100
				species = {
					is_robotic = no
					NOT = { has_trait = trait_mechanical }
					NOT = { has_trait = trait_xenomorph }
				}
			}
		}
		weighted_random_owned_pop_group = {
			limit = {
				pop_group_size > 0
				pop_group_size < 100
				species = {
					is_robotic = no
					NOT = { has_trait = trait_mechanical }
					NOT = { has_trait = trait_xenomorph }
				}
			}
			kill_single_pop = yes
		}
	}
}

xit_create_one_local_xenomorph_pop = {
	if = {
		limit = {
			any_owned_pop_group = {
				species = { has_trait = trait_xenomorph }
			}
		}
		weighted_random_owned_pop_group = {
			limit = {
				species = { has_trait = trait_xenomorph }
			}
			species = { save_event_target_as = xit_xenomorph_species }
		}
		create_pop_group = {
			species = event_target:xit_xenomorph_species
			size = 100
		}
	}
}

xit_create_hidden_xenomorph_seed_pop = {
	if = {
		limit = {
			NOT = {
				any_owned_pop_group = {
					species = { has_trait = trait_xenomorph }
				}
			}
		}
		create_species = {
			name = "NAME_XIT_Hidden_Xenomorph"
			plural = "NAME_XIT_Hidden_Xenomorph_Plural"
			class = "XENO"
			portrait = "xenomorph"
			traits = {
				trait = trait_organic
				trait = trait_xenomorph
			}
			homeworld = this
		}
		create_pop_group = {
			species = last_created_species
			size = 100
		}
	}
	else = {
		xit_create_one_local_xenomorph_pop = yes
	}
}

xit_hatch_dormant_ovomorph_cluster = {
	add_modifier = { modifier = xit_xenomorph_infestation days = 720 }
	xit_create_hidden_xenomorph_seed_pop = yes
}

xit_apply_xenomorph_exposure_seed = {
	add_modifier = { modifier = xit_xenomorph_infestation days = 720 }
	if = {
		limit = {
			exists = from
			from = {
				is_country_type = default
				owner_species = { has_trait = trait_xenomorph_brood_parasitism }
			}
		}
		set_timed_carrier_flag = { flag = xit_ovomorph_bearer_pressure days = 3600 }
	}
	if = {
		limit = {
			exists = from
			from = {
				is_country_type = default
				owner_species = { has_trait = trait_xenomorph_stalker_caste }
			}
		}
		set_timed_carrier_flag = { flag = xit_stalker_caste_pressure days = 3600 }
	}
	if = {
		limit = {
			exists = from
			from = {
				is_country_type = default
				has_technology = tech_xit_brood_host_selection
			}
		}
		set_timed_carrier_flag = { flag = xit_brood_host_selection_pressure days = 3600 }
	}
	if = {
		limit = {
			exists = from
			from = {
				is_country_type = default
				has_technology = tech_xit_xenomorph_biocontainment_subversion
			}
		}
		set_timed_carrier_flag = { flag = xit_biocontainment_subversion_pressure days = 3600 }
	}
	if = {
		limit = {
			exists = from
			from = {
				is_country_type = default
				has_technology = tech_xit_macro_ovomorph_seeding
			}
		}
		set_timed_carrier_flag = { flag = xit_macro_ovomorph_seeding_pressure days = 3600 }
	}
	if = {
		limit = {
			any_owned_pop_group = {
				species = { has_trait = trait_xenomorph }
			}
		}
		xit_convert_one_pop = yes
	}
	else_if = {
		limit = {
			exists = from
			from = {
				is_country_type = default
				owner_species = { has_trait = trait_xenomorph }
			}
		}
		from = { owner_species = { save_event_target_as = xit_xenomorph_attacker_species } }
		create_pop_group = {
			species = event_target:xit_xenomorph_attacker_species
			size = 100
		}
	}
	else = {
		xit_create_hidden_xenomorph_seed_pop = yes
	}
}

xit_apply_perfect_host_cycle_exposure_seed = {
	set_timed_carrier_flag = { flag = xit_perfect_host_cycle_conversion_pressure days = 3600 }
	xit_apply_xenomorph_exposure_seed = yes
}

xit_suppress_one_xenomorph_pop = {
	if = {
		limit = {
			any_owned_pop_group = {
				pop_group_size >= 100
				species = { has_trait = trait_xenomorph }
			}
		}
		weighted_random_owned_pop_group = {
			limit = {
				pop_group_size >= 100
				species = { has_trait = trait_xenomorph }
			}
			kill_single_pop = yes
		}
	}
}

xit_apply_xenomorph_assault_exposure = {
	if = {
		limit = {
			exists = from
			from = {
				is_country_type = default
				is_xenomorph_empire = yes
			}
		}
		random_list = {
			25 = { xit_apply_xenomorph_exposure_seed = yes }
			75 = { }
		}
	}
	if = {
		limit = {
			exists = from
			from = {
				is_country_type = default
				has_technology = tech_xit_macro_ovomorph_seeding
			}
		}
		random_list = {
			20 = { xit_apply_xenomorph_exposure_seed = yes }
			80 = {
			}
		}
	}
	if = {
		limit = { has_carrier_flag = xit_perfect_host_cycle_ground_assault }
		if = {
			limit = { owner = { has_ascension_perk = ap_xit_extragalactic_biosecurity_mandate } }
			if = {
				limit = { owner = { xit_biosecurity_science_tier_3 = yes } }
				random_list = {
					5 = { xit_apply_xenomorph_exposure_seed = yes }
					95 = {
					}
				}
			}
			else_if = {
				limit = { owner = { xit_biosecurity_science_tier_2 = yes } }
				random_list = {
					10 = { xit_apply_xenomorph_exposure_seed = yes }
					90 = {
					}
				}
			}
			else_if = {
				limit = { owner = { xit_biosecurity_science_tier_1 = yes } }
				random_list = {
					15 = { xit_apply_xenomorph_exposure_seed = yes }
					85 = {
					}
				}
			}
			else = {
				random_list = {
					25 = { xit_apply_xenomorph_exposure_seed = yes }
					75 = {
					}
				}
			}
		}
		else_if = {
			limit = { owner = { has_ascension_perk = ap_xit_xenomorph_research_protocols } }
			random_list = {
				80 = { xit_apply_xenomorph_exposure_seed = yes }
				20 = {
				}
			}
		}
		else_if = {
			limit = { owner = { OR = { has_tradition = tr_xit_biosecurity_finish has_tradition = tr_xit_containment_command_finish } } }
			random_list = {
				45 = { xit_apply_xenomorph_exposure_seed = yes }
				55 = {
				}
			}
		}
		else_if = {
			limit = { owner = { OR = { has_tradition = tr_xit_biosecurity_sterile_logistics has_tradition = tr_xit_containment_orbital_cordon } } }
			random_list = {
				65 = { xit_apply_xenomorph_exposure_seed = yes }
				35 = {
				}
			}
		}
		else = {
			xit_apply_xenomorph_exposure_seed = yes
		}
	}
	else_if = {
		limit = { has_carrier_flag = xit_perfect_host_cycle_bombardment }
		if = {
			limit = { owner = { has_ascension_perk = ap_xit_extragalactic_biosecurity_mandate } }
			if = {
				limit = { owner = { xit_biosecurity_science_tier_3 = yes } }
				random_list = {
					3 = { xit_apply_xenomorph_exposure_seed = yes }
					97 = {
					}
				}
			}
			else_if = {
				limit = { owner = { xit_biosecurity_science_tier_2 = yes } }
				random_list = {
					6 = { xit_apply_xenomorph_exposure_seed = yes }
					94 = {
					}
				}
			}
			else_if = {
				limit = { owner = { xit_biosecurity_science_tier_1 = yes } }
				random_list = {
					10 = { xit_apply_xenomorph_exposure_seed = yes }
					90 = {
					}
				}
			}
			else = {
				random_list = {
					20 = { xit_apply_xenomorph_exposure_seed = yes }
					80 = {
					}
				}
			}
		}
		else_if = {
			limit = { owner = { has_ascension_perk = ap_xit_xenomorph_research_protocols } }
			random_list = {
				80 = { xit_apply_xenomorph_exposure_seed = yes }
				20 = {
				}
			}
		}
		else_if = {
			limit = { owner = { OR = { has_tradition = tr_xit_biosecurity_finish has_tradition = tr_xit_containment_command_finish } } }
			random_list = {
				35 = { xit_apply_xenomorph_exposure_seed = yes }
				65 = {
				}
			}
		}
		else_if = {
			limit = { owner = { OR = { has_tradition = tr_xit_biosecurity_sterile_logistics has_tradition = tr_xit_containment_orbital_cordon } } }
			random_list = {
				55 = { xit_apply_xenomorph_exposure_seed = yes }
				45 = {
				}
			}
		}
		else = {
			xit_apply_xenomorph_exposure_seed = yes
		}
	}
	else_if = {
		limit = { owner = { has_ascension_perk = ap_xit_extragalactic_biosecurity_mandate } }
		if = {
			limit = { owner = { xit_biosecurity_science_tier_3 = yes } }
			random_list = {
				1 = { xit_apply_xenomorph_exposure_seed = yes }
				199 = {
				}
			}
		}
		else_if = {
			limit = { owner = { xit_biosecurity_science_tier_2 = yes } }
			random_list = {
				1 = { xit_apply_xenomorph_exposure_seed = yes }
				149 = {
				}
			}
		}
		else_if = {
			limit = { owner = { xit_biosecurity_science_tier_1 = yes } }
			random_list = {
				1 = { xit_apply_xenomorph_exposure_seed = yes }
				99 = {
				}
			}
		}
		else = {
			random_list = {
				3 = { xit_apply_xenomorph_exposure_seed = yes }
				97 = {
				}
			}
		}
	}
	else_if = {
		limit = { owner = { has_ascension_perk = ap_xit_xenomorph_research_protocols } }
		random_list = {
			3 = { xit_apply_xenomorph_exposure_seed = yes }
			97 = {
			}
		}
	}
	else_if = {
		limit = { owner = { OR = { has_tradition = tr_xit_biosecurity_finish has_tradition = tr_xit_containment_command_finish } } }
		random_list = {
			15 = { xit_apply_xenomorph_exposure_seed = yes }
			85 = {
			}
		}
	}
	else_if = {
		limit = { owner = { OR = { has_tradition = tr_xit_biosecurity_sterile_logistics has_tradition = tr_xit_containment_orbital_cordon } } }
		random_list = {
			35 = { xit_apply_xenomorph_exposure_seed = yes }
			65 = {
			}
		}
	}
	else = {
		xit_apply_xenomorph_exposure_seed = yes
	}
}

xit_apply_perfect_host_cycle_seed = {
	set_timed_carrier_flag = { flag = xit_recent_perfect_host_cycle_seed days = 1800 }

	if = {
		limit = {
			is_colony = yes
			exists = owner
			owner = { xit_is_xenomorph_safe_owner = yes }
		}
		add_modifier = { modifier = xit_managed_xenomorph_brood_world days = 3600 }
		if = {
			limit = {
				is_colonizable = yes
				NOT = { has_deposit = d_pacified_xenomorphs }
				NOT = { has_deposit = d_xenomorph_remains }
			}
			random_list = {
				70 = { add_deposit = d_pacified_xenomorphs }
				30 = { add_deposit = d_xenomorph_remains }
			}
		}
	}
	else_if = {
		limit = {
			is_colony = yes
			exists = owner
			owner = { NOT = { xit_is_xenomorph_safe_owner = yes } }
			any_owned_pop_group = {
				pop_group_size >= 100
				species = {
					is_robotic = no
					NOT = { has_trait = trait_mechanical }
					NOT = { has_trait = trait_xenomorph }
				}
			}
		}
		if = {
			limit = { owner = { has_ascension_perk = ap_xit_extragalactic_biosecurity_mandate } }
			if = {
				limit = { owner = { xit_biosecurity_science_tier_3 = yes } }
				random_list = {
					2 = {
						set_timed_carrier_flag = { flag = xit_perfect_host_cycle_conversion_pressure days = 3600 }
						xit_apply_xenomorph_exposure_seed = yes
					}
					98 = {
						add_modifier = { modifier = xit_ovomorph_quarantine_zone days = 1800 }
						if = {
							limit = {
								is_colonizable = yes
								NOT = { has_deposit = d_xit_dormant_ovomorph_cluster }
							}
							add_deposit = d_xit_dormant_ovomorph_cluster
						}
					}
				}
			}
			else_if = {
				limit = { owner = { xit_biosecurity_science_tier_2 = yes } }
				random_list = {
					5 = {
						set_timed_carrier_flag = { flag = xit_perfect_host_cycle_conversion_pressure days = 3600 }
						xit_apply_xenomorph_exposure_seed = yes
					}
					95 = {
						add_modifier = { modifier = xit_ovomorph_quarantine_zone days = 1800 }
						if = {
							limit = {
								is_colonizable = yes
								NOT = { has_deposit = d_xit_dormant_ovomorph_cluster }
							}
							add_deposit = d_xit_dormant_ovomorph_cluster
						}
					}
				}
			}
			else_if = {
				limit = { owner = { xit_biosecurity_science_tier_1 = yes } }
				random_list = {
					10 = {
						set_timed_carrier_flag = { flag = xit_perfect_host_cycle_conversion_pressure days = 3600 }
						xit_apply_xenomorph_exposure_seed = yes
					}
					90 = {
						add_modifier = { modifier = xit_ovomorph_quarantine_zone days = 1800 }
						if = {
							limit = {
								is_colonizable = yes
								NOT = { has_deposit = d_xit_dormant_ovomorph_cluster }
							}
							add_deposit = d_xit_dormant_ovomorph_cluster
						}
					}
				}
			}
			else = {
				random_list = {
					20 = {
						set_timed_carrier_flag = { flag = xit_perfect_host_cycle_conversion_pressure days = 3600 }
						xit_apply_xenomorph_exposure_seed = yes
					}
					80 = {
						add_modifier = { modifier = xit_ovomorph_quarantine_zone days = 1800 }
						if = {
							limit = {
								is_colonizable = yes
								NOT = { has_deposit = d_xit_dormant_ovomorph_cluster }
							}
							add_deposit = d_xit_dormant_ovomorph_cluster
						}
					}
				}
			}
		}
		else = {
			set_timed_carrier_flag = { flag = xit_perfect_host_cycle_conversion_pressure days = 3600 }
			xit_apply_xenomorph_exposure_seed = yes
		}
	}
	else_if = {
		limit = {
			is_colonizable = yes
			NOT = { has_deposit = d_xit_dormant_ovomorph_cluster }
			NOT = { has_deposit = d_feral_xenomorphs }
			NOT = { has_deposit = d_xenomorph_remains }
			NOT = { has_deposit = d_pacified_xenomorphs }
		}
		random_list = {
			50 = { add_deposit = d_xit_dormant_ovomorph_cluster }
			25 = { add_deposit = d_xenomorph_remains }
			15 = { add_deposit = d_feral_xenomorphs }
			10 = { add_modifier = { modifier = xit_ovomorph_quarantine_zone days = -1 } }
		}
	}
}

xit_abandon_terminal_xenomorph_world = {
	if = {
		limit = {
			is_colony = yes
			exists = owner
			has_ground_combat = no
			any_owned_pop_group = {
				species = { has_trait = trait_xenomorph }
			}
			NOT = {
				any_owned_pop_group = {
					pop_group_size > 0
					species = {
						is_robotic = no
						NOT = { has_trait = trait_mechanical }
						NOT = { has_trait = trait_xenomorph }
					}
				}
			}
		}
		if = {
			limit = {
				NOT = { has_deposit = d_xit_dormant_ovomorph_cluster }
			}
			add_deposit = d_xit_dormant_ovomorph_cluster
		}
		set_carrier_flag = xit_abandoned_xenomorph_world
		set_carrier_flag = xit_xenomorph_terminal_world_transferred
		remove_all_buildings = yes
		destroy_colony = yes
	}
}

xit_galactic_infestation_seed_target = {
	if = {
		limit = {
			is_colony = yes
			exists = owner
			owner = { NOT = { xit_is_xenomorph_safe_owner = yes } }
			NOT = { has_deposit = d_xit_dormant_ovomorph_cluster }
			NOT = { has_modifier = xit_xenomorph_infestation }
			NOT = { has_modifier = xit_xenomorph_outbreak }
			NOT = { has_modifier = xit_xenomorph_overrun }
			NOT = { has_modifier = xit_xenomorph_terminal_collapse }
		}
		add_deposit = d_xit_dormant_ovomorph_cluster
		if = {
			limit = {
				root = {
					is_country_type = default
					has_technology = tech_xit_brood_host_selection
				}
			}
			set_timed_carrier_flag = { flag = xit_brood_host_selection_pressure days = 3600 }
		}
		if = {
			limit = {
				root = {
					is_country_type = default
					has_technology = tech_xit_xenomorph_biocontainment_subversion
				}
			}
			set_timed_carrier_flag = { flag = xit_biocontainment_subversion_pressure days = 3600 }
		}
		if = {
			limit = {
				root = {
					is_country_type = default
					is_xenomorph_empire = yes
				}
				is_capital = no
			}
			set_timed_carrier_flag = { flag = xit_stalker_caste_pressure days = 3600 }
			random_list = {
				25 = {
					add_modifier = { modifier = xit_xenomorph_infestation days = 720 }
					xit_create_hidden_xenomorph_seed_pop = yes
				}
				75 = {
				}
			}
		}
		if = {
			limit = {
				root = {
					is_country_type = default
					has_technology = tech_xit_macro_ovomorph_seeding
				}
				is_capital = no
			}
			set_timed_carrier_flag = { flag = xit_macro_ovomorph_seeding_pressure days = 3600 }
			random_list = {
				35 = {
					add_modifier = { modifier = xit_xenomorph_infestation days = 720 }
					xit_create_hidden_xenomorph_seed_pop = yes
				}
				65 = {
				}
			}
		}
		if = {
			limit = { is_capital = no }
			add_modifier = { modifier = xit_ovomorph_quarantine_zone days = 1800 }
		}
		save_event_target_as = xit_galactic_infestation_target_planet
	}
}

