############################
#
# Crisis Events
#
# Written by Henrik Thyrwall
#
############################

namespace = crisis

### PRETHORYN SCOURGE

### Triggering Event
# WARNING: May cause galactic mass extinction and/or loss of appetite
country_event = {
	id = crisis.199
	hide_window = yes
	is_test_event = yes

	trigger = { always = no }

	immediate = {
		set_global_flag = prethoryn_invasion_happened
		set_global_flag = prethoryn_transmission
		set_global_flag = galactic_crisis_happened
		begin_event_chain = {
			event_chain = "coming_storm_chain"
			target = ROOT
		}
		random_rim_system = {
			set_star_flag = swarm_invasion_target_1
			save_event_target_as = prethoryn_invasion_system
		}
		create_point_of_interest = {
			id = coming_storm_poi.1
			name = "coming_storm_poi_1_poi"
			desc = "coming_storm_poi_1_poi_desc"
			event_chain = "coming_storm_chain"
			location = event_target:prethoryn_invasion_system
		}
		country_event = { id = crisis.17 days = 10 }
	}
}

### Invasion Events

# Prethoryn Invasion Begins (HIDDEN)
country_event = {
	id = crisis.10
	hide_window = yes
	fire_only_once = yes
	is_triggered_only = yes

	immediate = {
		endgame_telemetry = prethoryn
		set_global_flag = prethoryn_subspace_echoes
		set_global_flag = prethoryn_invasion_happened
		set_global_flag = galactic_crisis_happened
		observer_event = { id = observer.27 }
		every_country = {
			limit = { is_country_type = default }
			country_event = { id = crisis.11 }
		}
	}
}

# Approaching Subspace Echoes
country_event = {
	id = crisis.11
	title = "crisis.11.name"
	desc = "crisis.11.desc"
	picture = GFX_evt_physics_research
	show_sound = event_alien_nature

	is_triggered_only = yes

	option = {
		name = crisis.11.a
		begin_event_chain = {
			event_chain = "coming_storm_chain"
			target = ROOT
		}
	}
}

# Approaching the Outer Rim (HIDDEN)
country_event = {
	id = crisis.12
	hide_window = yes
	fire_only_once = yes

	mean_time_to_happen = {
		months = 600
	}

	trigger = {
		is_country_type = default
		has_global_flag = prethoryn_subspace_echoes
	}

	immediate = {
		remove_global_flag = prethoryn_subspace_echoes
		set_global_flag = prethoryn_approaching_rim
		random_rim_system = {
			limit = {
				OR = {
					# No owner or neighbors
					NOR = {
						any_neighbor_system = {
							exists = space_owner
						}
						exists = space_owner
					}
					# Owned but not by fallen empire
					AND = {
						exists = space_owner
						space_owner = {
							NOR = {
								is_country_type = fallen_empire
								is_country_type = awakened_fallen_empire
							}
						}
					}
				}
				# No neighboring fallen empires
				NOT = {
					any_neighbor_system = {
						exists = space_owner
						space_owner = {
							OR = {
								is_country_type = fallen_empire
								is_country_type = awakened_fallen_empire
							}
						}
					}
				}
				NOT = { has_star_flag = crisis_spawn_exclude }
			}
			set_star_flag = swarm_invasion_target_1
		}
		observer_event = { id = observer.28 }
		every_country = {
			limit = { is_country_type = default }
			country_event = { id = crisis.13 }
		}
	}
}

# Approaching the Outer Rim
country_event = {
	id = crisis.13
	title = "crisis.13.name"
	desc = "crisis.13.desc"
	picture = GFX_evt_physics_research
	show_sound = event_alien_nature

	is_triggered_only = yes

	immediate = {
		random_rim_system = {
			limit = { has_star_flag = swarm_invasion_target_1 }
			save_event_target_as = prethoryn_invasion_system
			closest_system = {
				limit = {
					NOR = {
						has_star_flag = swarm_invasion_warning
						has_star_flag = swarm_invasion_target_1
					}
				}
				set_star_flag = swarm_invasion_warning
				save_event_target_as = swarm_invasion_warning_1
			}
			closest_system = {
				limit = {
					NOR = {
						has_star_flag = swarm_invasion_warning
						has_star_flag = swarm_invasion_target_1
					}
				}
				set_star_flag = swarm_invasion_warning
				save_event_target_as = swarm_invasion_warning_2
			}
			closest_system = {
				limit = {
					NOR = {
						has_star_flag = swarm_invasion_warning
						has_star_flag = swarm_invasion_target_1
					}
				}
				set_star_flag = swarm_invasion_warning
				save_event_target_as = swarm_invasion_warning_3
			}
			closest_system = {
				limit = {
					NOR = {
						has_star_flag = swarm_invasion_warning
						has_star_flag = swarm_invasion_target_1
					}
				}
				set_star_flag = swarm_invasion_warning
				save_event_target_as = swarm_invasion_warning_4
			}
			closest_system = {
				limit = {
					NOR = {
						has_star_flag = swarm_invasion_warning
						has_star_flag = swarm_invasion_target_1
					}
				}
				set_star_flag = swarm_invasion_warning
				save_event_target_as = swarm_invasion_warning_5
			}
		}

	}

	option = {
		name = crisis.13.a
		create_point_of_interest = {
			id = coming_storm_poi.1
			name = "coming_storm_poi_1_poi"
			desc = "coming_storm_poi_1_poi_desc"
			event_chain = "coming_storm_chain"
			category = coming_storm
			location = event_target:prethoryn_invasion_system
		}
		hidden_effect = {
			create_point_of_interest = {
				id = coming_storm_poi.2
				name = "coming_storm_poi_1_poi"
				desc = "coming_storm_poi_1_poi_desc"
				event_chain = "coming_storm_chain"
				category = coming_storm
				location = event_target:swarm_invasion_warning_1
			}
			create_point_of_interest = {
				id = coming_storm_poi.3
				name = "coming_storm_poi_1_poi"
				desc = "coming_storm_poi_1_poi_desc"
				event_chain = "coming_storm_chain"
				category = coming_storm
				location = event_target:swarm_invasion_warning_2
			}
			create_point_of_interest = {
				id = coming_storm_poi.4
				name = "coming_storm_poi_1_poi"
				desc = "coming_storm_poi_1_poi_desc"
				event_chain = "coming_storm_chain"
				category = coming_storm
				location = event_target:swarm_invasion_warning_3
			}
			create_point_of_interest = {
				id = coming_storm_poi.5
				name = "coming_storm_poi_1_poi"
				desc = "coming_storm_poi_1_poi_desc"
				event_chain = "coming_storm_chain"
				category = coming_storm
				location = event_target:swarm_invasion_warning_4
			}
			create_point_of_interest = {
				id = coming_storm_poi.6
				name = "coming_storm_poi_1_poi"
				desc = "coming_storm_poi_1_poi_desc"
				event_chain = "coming_storm_chain"
				category = coming_storm
				location = event_target:swarm_invasion_warning_5
			}
		}
	}
}

# Subspace Signal (HIDDEN)
country_event = {
	id = crisis.14
	hide_window = yes
	fire_only_once = yes

	mean_time_to_happen = {
		months = 120
	}

	trigger = {
		has_global_flag = prethoryn_approaching_rim
	}

	immediate = {
		remove_global_flag = prethoryn_approaching_rim
		set_global_flag = prethoryn_transmission
		every_country = {
			limit = { is_country_type = default }
			country_event = { id = crisis.15 }
		}
	}
}

# Subspace Signal
country_event = {
	id = crisis.15
	title = "crisis.15.name"
	desc = "crisis.15.desc"
	picture = GFX_evt_physics_research
	show_sound = event_alien_nature

	is_triggered_only = yes

	option = {
		name = crisis.15.a
		hidden_effect = {
			country_event = { id = crisis.16 }
		}
	}
}

# Alien Transmission
country_event = {
	id = crisis.16
	title = "TRANSMISSION"
	#desc = "crisis.16.desc"

	desc = {
		text = "crisis.16.desc"
		trigger = {
			can_talk_to_prethoryn = no
		}
	}
	desc = {
		text = "crisis.16.b.desc"
		trigger = {
			owner_species = { is_psionic_species = yes }
		}
	}
	desc = {
		text = "crisis.16.c.desc"
		trigger = {
			owner_species = { has_trait = trait_hive_mind }
		}
	}

	diplomatic = yes

	picture_event_data = {
		portrait = swarm1
		room = "swarm_room"
	}

	is_triggered_only = yes

	option = {
		name = crisis.16.a
		trigger = {
			owner_species = { NOT = { has_trait = trait_hive_mind } }
		}
	}

	option = {
		name = crisis.16.b
		trigger = {
			owner_species = { has_trait = trait_hive_mind }
		}
	}
}

# Vanguard Arrives (HIDDEN)
country_event = {
	id = crisis.17
	hide_window = yes
	fire_only_once = yes

	mean_time_to_happen = {
		months = 30
	}

	trigger = {
		has_global_flag = prethoryn_transmission
	}

	immediate = {
		set_crisis_sound = swarm_crisis_ambient_stage_1
		set_crisis_stage_1 = yes
		awaken_guardians_of_the_galaxy = yes

		remove_global_flag = prethoryn_approaching_rim
		set_global_flag = prethoryn_arrival
		observer_event = { id = observer.29 }
		every_country = {
			limit = { is_country_type = default }
			country_event = { id = crisis.18 }
		}
		random_rim_system = {
			limit = { has_star_flag = swarm_invasion_target_1 }
			random_system_planet = {
				create_species = {
					name = "NAME_Prethoryn"
					class = SWARM
					namelist = Prethoryn
					portrait = random
					traits = random
					immortal = yes

					effect = { save_event_target_as = prethoryn_species }
				}
				create_country = {
					name = "NAME_Prethoryn_Scourge"
					type = "swarm"
					species = event_target:prethoryn_species
					name_list = "Prethoryn"
					flag = {
						icon= {
							category = "special"
							file = "the_swarm.dds"
						}
						background= {
							category = "backgrounds"
							file = "new_dawn.dds"
						}
						colors={
							"orange"
							"grey"
							"null"
							"null"
						}
					}
					effect = {
						save_event_target_as = prethoryn
						save_event_target_as = prethoryn_vgrd
						create_ship_design = { design = "NAME_Swarm_Transport" }
						add_ship_design = last_created_design
						create_ship_design = { design = "NAME_Swarm_Colonizer" }
						add_ship_design = last_created_design
						create_ship_design = { design = "NAME_Swarm_Constructor" }
						add_ship_design = last_created_design
						create_ship_design = { design = "NAME_Swarm_Starbase" }
						add_ship_design = last_created_design
					}
				}
				solar_system = {
					if = {
						limit = {
							any_fleet_in_system = {
								is_ship_class = shipclass_starbase
							}
						}
						random_fleet_in_system = {
							limit = {
								any_controlled_ship = {
									is_ship_class = shipclass_starbase
								}
							}
							destroy_fleet = this
						}
					}
					create_starbase = {
						size = starbase_swarm
						owner = event_target:prethoryn_vgrd
					}
					star = {
						create_ambient_object = {
							type = "swarm_1"
							location = this
						}
						last_created_ambient_object = {
							set_ambient_object_flag = swarm_system_effect
							set_location = {
								target = prev
								distance = 0
								angle = random
							}
						}
					}
				}
				every_country = {
					limit = {
						OR = {
							is_country_type = default
							is_country_type = fallen_empire
							is_country_type = awakened_fallen_empire
						}
					}
					establish_communications_no_message = event_target:prethoryn
					add_opinion_modifier = {
						who = event_target:prethoryn
						modifier = opinion_swarm
					}
					event_target:prethoryn = {
						add_opinion_modifier = {
							who = PREV
							modifier = opinion_prey
						}
					}
					if = {
						limit = { has_technology = tech_gravity_wells }
						country_event = {
							id = grand_archive.7220
							days = 30
						}
					}
				}
				event_target:prethoryn = {

					### FIRST SYSTEM

					swarm_vanguard = yes
					swarm_vanguard = yes
					if = {
						limit = {
							is_difficulty >= 3
						}
						swarm_vanguard = yes
					}
					if = {
						limit = {
							is_difficulty >= 5
						}
						swarm_vanguard = yes
					}
					if = {
						limit = {
							is_difficulty >= 6
						}
						swarm_vanguard = yes
					}


					# Constructor
					create_fleet = {
						name = "NAME_Prethoryn_Constructor"
						effect = {
							set_owner = PREV
							create_ship = {
								name = random
								design = "NAME_Swarm_Constructor"
								graphical_culture = "swarm_01"
							}
							set_location = {
								target = PREVPREV
								distance = 10
								angle = random
							}
						}
					}
				}
			}

			### SECOND SYSTEM
			random_system = {
				limit = {
					distance = {
						source = PREV
						max_distance <= 60
						min_distance >= 5
					}
				}
				set_star_flag = swarm_invasion_target_2
				random_system_planet = {
					event_target:prethoryn = {

						swarm_vanguard = yes
						swarm_vanguard = yes
						if = {
							limit = {
								is_difficulty >= 3
							}
							swarm_vanguard = yes
						}
						if = {
							limit = {
								is_difficulty >= 5
							}
							swarm_vanguard = yes
						}
						if = {
							limit = {
								is_difficulty >= 6
							}
							swarm_vanguard = yes
						}

						# Constructor
						create_fleet = {
							name = "NAME_Prethoryn_Constructor"
							effect = {
								set_owner = PREV
								create_ship = {
									name = random
									design = "NAME_Swarm_Constructor"
									graphical_culture = "swarm_01"
								}
								set_location = {
									target = PREVPREV
									distance = 10
									angle = random
								}
							}
						}
					}
				}
			}

			### THIRD SYSTEM
			random_system = {
				limit = {
					distance = {
						source = PREV
						max_distance <= 60
						min_distance >= 5
					}
					NOR = {
						has_star_flag = swarm_invasion_target_1
						has_star_flag = swarm_invasion_target_2
					}
				}
				set_star_flag = swarm_invasion_target_3
				random_system_planet = {
					event_target:prethoryn = {

						swarm_vanguard = yes
						swarm_vanguard = yes
						if = {
							limit = {
								is_difficulty >= 3
							}
							swarm_vanguard = yes
						}
						if = {
							limit = {
								is_difficulty >= 5
							}
							swarm_vanguard = yes
						}
						if = {
							limit = {
								is_difficulty >= 6
							}
							swarm_vanguard = yes
						}

						# Constructor
						create_fleet = {
							name = "NAME_Prethoryn_Constructor"
							effect = {
								set_owner = PREV
								create_ship = {
									name = random
									design = "NAME_Swarm_Constructor"
									graphical_culture = "swarm_01"
								}
								set_location = {
									target = PREVPREV
									distance = 10
									angle = random
								}
							}
						}
					}
				}
			}

			### FOURTH SYSTEM
			random_system = {
				limit = {
					distance = {
						source = PREV
						max_distance <= 60
						min_distance >= 5
					}
					NOR = {
						has_star_flag = swarm_invasion_target_1
						has_star_flag = swarm_invasion_target_2
						has_star_flag = swarm_invasion_target_3
					}
				}
				set_star_flag = swarm_invasion_target_4
				random_system_planet = {
					event_target:prethoryn = {

						swarm_vanguard = yes
						swarm_vanguard = yes
						if = {
							limit = {
								is_difficulty >= 3
							}
							swarm_vanguard = yes
						}
						if = {
							limit = {
								is_difficulty >= 5
							}
							swarm_vanguard = yes
						}
						if = {
							limit = {
								is_difficulty >= 6
							}
							swarm_vanguard = yes
						}

						# Constructor
						create_fleet = {
							name = "NAME_Prethoryn_Constructor"
							effect = {
								set_owner = PREV
								create_ship = {
									name = random
									design = "NAME_Swarm_Constructor"
									graphical_culture = "swarm_01"
								}
								set_location = {
									target = PREVPREV
									distance = 10
									angle = random
								}
							}
						}
					}
				}
			}
		}
		random_country = {
			limit = { is_country_type = global_event }
			country_event = { id = crisis.19 days = 400 random = 200 }
		}
	}
	after = {
		every_playable_country = {
			country_event = { id = timeline.70 } # Timeline Prethoryn Scourge
		}
	}
}

# Vanguard Arrives
country_event = {
	id = crisis.18
	title = "crisis.18.name"
	desc = {
		trigger = {
			owner = {
				country_uses_bio_ships = no
			}
		}
		text = "crisis.18.desc"
	}
	desc = {
		trigger = {
			owner = {
				country_uses_bio_ships = yes
			}
		}
		text = "crisis.18.desc.bio"
	}
	picture = GFX_evt_physics_research
	show_sound = event_alien_nature
	location = event_target:prethoryn_invasion_system

	is_triggered_only = yes

	immediate = {
		random_rim_system = {
			limit = { has_star_flag = swarm_invasion_target_1 }
			save_event_target_as = prethoryn_invasion_system
		}
		if = {
			limit = {
				any_planet_within_border = {
					has_planet_flag = fotd_seperatist_planet@root
					owner = {
						has_country_flag = fotd_seperatist_country@root
					}
				}
			}
			country_event = { id = origin.6125 days = 5 random = 2 }
		}
	}

	option = {
		name = crisis.18.a
		trigger = {
			has_spiritualist_government = yes
			NOR = {
				has_ethic = "ethic_fanatic_egalitarian"
				has_ethic = "ethic_fanatic_authoritarian"
				has_ethic = "ethic_fanatic_xenophile"
				has_ethic = "ethic_fanatic_xenophobe"
			}
		}
		end_event_chain = "coming_storm_chain"
		begin_event_chain = {
			event_chain = "prethoryn_scourge_chain"
			target = ROOT
		}
	}
	option = {
		name = crisis.18.b
		trigger = {
			has_militarist_government = yes
			NOR = {
				has_ethic = "ethic_fanatic_egalitarian"
				has_ethic = "ethic_fanatic_authoritarian"
				has_ethic = "ethic_fanatic_xenophile"
				has_ethic = "ethic_fanatic_xenophobe"
			}
		}
		end_event_chain = "coming_storm_chain"
		begin_event_chain = {
			event_chain = "prethoryn_scourge_chain"
			target = ROOT
		}
	}
	option = {
		name = crisis.18.c
		trigger = {
			has_materialist_government = yes
			NOR = {
				has_ethic = "ethic_fanatic_egalitarian"
				has_ethic = "ethic_fanatic_authoritarian"
				has_ethic = "ethic_fanatic_xenophile"
				has_ethic = "ethic_fanatic_xenophobe"
			}
		}
		end_event_chain = "coming_storm_chain"
		begin_event_chain = {
			event_chain = "prethoryn_scourge_chain"
			target = ROOT
		}
	}
	option = {
		name = crisis.18.d
		trigger = {
			has_pacifist_government = yes
			NOR = {
				has_ethic = "ethic_fanatic_egalitarian"
				has_ethic = "ethic_fanatic_authoritarian"
				has_ethic = "ethic_fanatic_xenophile"
				has_ethic = "ethic_fanatic_xenophobe"
			}
		}
		end_event_chain = "coming_storm_chain"
		begin_event_chain = {
			event_chain = "prethoryn_scourge_chain"
			target = ROOT
		}
	}
	option = {
		name = crisis.18.e
		trigger = {
			OR = {
				has_government = gov_megacorporation
				has_government = gov_trade_league
				is_megacorp = yes
			}
			NOR = {
				has_ethic = "ethic_fanatic_egalitarian"
				has_ethic = "ethic_fanatic_authoritarian"
				has_ethic = "ethic_fanatic_xenophile"
				has_ethic = "ethic_fanatic_xenophobe"
			}
		}
		end_event_chain = "coming_storm_chain"
		begin_event_chain = {
			event_chain = "prethoryn_scourge_chain"
			target = ROOT
		}
	}
	option = {
		name = crisis.18.f
		trigger = {
			OR = {
				has_generic_government = yes
				has_government = gov_enlightened_monarchy
				has_government = gov_elective_monarchy
				has_government = gov_hive_mind
				has_government = gov_wilderness
				has_government = gov_parasitic_overmind
				has_government = gov_successor_khanate
				has_government = gov_diadochi
				has_ethic = ethic_gestalt_consciousness
			}
			NOR = {
				has_ethic = "ethic_fanatic_egalitarian"
				has_ethic = "ethic_fanatic_authoritarian"
				has_ethic = "ethic_fanatic_xenophile"
				has_ethic = "ethic_fanatic_xenophobe"
			}
		}
		end_event_chain = "coming_storm_chain"
		begin_event_chain = {
			event_chain = "prethoryn_scourge_chain"
			target = ROOT
		}
	}
	option = {
		name = crisis.18.g
		trigger = {
			has_ethic = "ethic_fanatic_xenophobe"
		}
		end_event_chain = "coming_storm_chain"
		begin_event_chain = {
			event_chain = "prethoryn_scourge_chain"
			target = ROOT
		}
	}
	option = {
		name = crisis.18.h
		trigger = {
			has_ethic = "ethic_fanatic_xenophile"
		}
		end_event_chain = "coming_storm_chain"
		begin_event_chain = {
			event_chain = "prethoryn_scourge_chain"
			target = ROOT
		}
	}
	option = {
		name = crisis.18.i
		trigger = {
			has_ethic = "ethic_fanatic_authoritarian"
		}
		end_event_chain = "coming_storm_chain"
		begin_event_chain = {
			event_chain = "prethoryn_scourge_chain"
			target = ROOT
		}
	}
	option = {
		name = crisis.18.j
		trigger = {
			has_ethic = "ethic_fanatic_egalitarian"
		}
		end_event_chain = "coming_storm_chain"
		begin_event_chain = {
			event_chain = "prethoryn_scourge_chain"
			target = ROOT
		}
	}
}

# Main Invasion Arrives (HIDDEN)
country_event = {
	id = crisis.19
	hide_window = yes
	fire_only_once = yes

	is_triggered_only = yes

	immediate = {
		set_crisis_sound = swarm_crisis_ambient_stage_2
		set_crisis_stage_2 = yes
		set_global_flag = prethoryn_main_invasion
		set_global_flag = ongoing_prethoryn_invasion
		observer_event = { id = observer.30 }
		every_country = {
			limit = { is_country_type = default }
			country_event = { id = crisis.20 }
		}

		# Custodian/Emperor Announcements
		event_target:prethoryn = { save_event_target_as = crisis_country }
		if = {
			limit = { has_galactic_custodian = yes }
			random_playable_country = {
				limit = { is_galactic_custodian = yes }
				country_event = { id = custodian.50 days = 30 }
			}
		}
		if = {
			limit = { has_galactic_emperor = yes }
			event_target:gal_emperor = {
				country_event = { id = emperor.90 days = 30 }
			}
		}

		# Recreate Prethoryn if Vanguard was destroyed
		if = {
			limit = {
				NOT = { exists = event_target:prethoryn }
			}
			create_species = {
				name = "NAME_Prethoryn"
				class = SWARM
				namelist = Prethoryn
				portrait = random
				traits = random
				immortal = yes
				effect = { save_event_target_as = prethoryn_species }
			}
			create_country = {
				name = "NAME_Prethoryn_Scourge"
				type = "swarm"
				species = event_target:prethoryn_species
				name_list = "Prethoryn"
				flag = {
					icon= {
						category = "special"
						file = "the_swarm.dds"
					}
					background= {
						category = "backgrounds"
						file = "new_dawn.dds"
					}
					colors={
						"orange"
						"grey"
						"null"
						"null"
					}
				}
				effect = {
					save_event_target_as = prethoryn
					save_event_target_as = prethoryn_vgrd
					create_ship_design = { design = "NAME_Swarm_Transport" }
					add_ship_design = last_created_design
					create_ship_design = { design = "NAME_Swarm_Colonizer" }
					add_ship_design = last_created_design
					create_ship_design = { design = "NAME_Swarm_Constructor" }
					add_ship_design = last_created_design
					create_ship_design = { design = "NAME_Swarm_Starbase" }
					add_ship_design = last_created_design
				}
			}
			every_country = {
				limit = {
					OR = {
						is_country_type = default
						is_country_type = fallen_empire
						is_country_type = awakened_fallen_empire
					}
				}
				establish_communications_no_message = event_target:prethoryn
				add_opinion_modifier = {
					who = event_target:prethoryn
					modifier = opinion_swarm
				}
				event_target:prethoryn = {
					add_opinion_modifier = {
						who = PREV
						modifier = opinion_prey
					}
				}
				if = {
					limit = { has_technology = tech_gravity_wells }
					country_event = {
						id = grand_archive.7220
						days = 30
					}
				}
			}
		}

		random_rim_system = {
			limit = { has_star_flag = swarm_invasion_target_1 }

			if = {
				limit = {
					event_target:prethoryn = {
						NOT = {
							any_controlled_ship = { is_ship_class = shipclass_starbase }
						}
					}
					any_fleet_in_system = { is_ship_class = shipclass_starbase }
				}
				random_fleet_in_system = {
					limit = {
						is_ship_class = shipclass_starbase
					}
					destroy_fleet = this
				}
			}
			create_starbase = {
				size = starbase_swarm
				owner = event_target:prethoryn
			}

			random_system_planet = {
				save_event_target_as = spawn_location
				event_target:prethoryn = {

					### FIRST SYSTEM
					swarm_brood = yes
					swarm_brood = yes
					if = {
						limit = {
							is_difficulty >= 3
						}
						swarm_brood = yes
					}
					if = {
						limit = {
							is_difficulty >= 5
						}
						swarm_brood = yes
					}
					if = {
						limit = {
							is_difficulty >= 6
						}
						swarm_brood = yes
					}

					# Infestors
					while = {
						count = 3
						create_fleet = {
							name = "NAME_Prethoryn_Infestor"
							effect = {
								set_owner = event_target:prethoryn
								create_ship = {
									name = random
									design = "NAME_Swarm_Colonizer"
									graphical_culture = "swarm_01"
								}
								set_location = {
									target = PREVPREV
									distance = 10
									angle = random
								}
							}
						}
					}

					# Constructor
					create_fleet = {
						name = "NAME_Prethoryn_Constructor"
						effect = {
							set_owner = event_target:prethoryn
							create_ship = {
								name = random
								design = "NAME_Swarm_Constructor"
								graphical_culture = "swarm_01"
							}
							set_location = {
								target = PREVPREV
								distance = 10
								angle = random
							}
						}
					}

					# Army Transports
					swarm_armies = yes
					swarm_armies = yes
					swarm_armies = yes
				}
			}

			### SECOND SYSTEM
			random_system = {
				limit = { has_star_flag = swarm_invasion_target_2 }
				if = {
					limit = {
						any_fleet_in_system = {
							is_ship_class = shipclass_starbase
						}
					}
					random_fleet_in_system = {
						limit = {
							is_ship_class = shipclass_starbase
						}
						destroy_fleet = this
					}
				}
				create_starbase = {
					size = starbase_swarm
					owner = event_target:prethoryn
				}
				random_system_planet = {
					save_event_target_as = spawn_location
					event_target:prethoryn = {

						swarm_brood = yes
						swarm_brood = yes
						if = {
							limit = {
								is_difficulty >= 3
							}
							swarm_brood = yes
						}
						if = {
							limit = {
								is_difficulty >= 5
							}
							swarm_brood = yes
						}
						if = {
							limit = {
								is_difficulty >= 6
							}
							swarm_brood = yes
						}

						# Infestors
						while = {
							count = 3
							create_fleet = {
								name = "NAME_Prethoryn_Infestor"
								effect = {
									set_owner = event_target:prethoryn
									create_ship = {
										name = random
										design = "NAME_Swarm_Colonizer"
										graphical_culture = "swarm_01"
									}
									set_location = {
										target = PREVPREV
										distance = 10
										angle = random
									}
								}
							}
						}

						# Constructor
						create_fleet = {
							name = "NAME_Prethoryn_Constructor"
							effect = {
								set_owner = event_target:prethoryn
								create_ship = {
									name = random
									design = "NAME_Swarm_Constructor"
									graphical_culture = "swarm_01"
								}
								set_location = {
									target = PREVPREV
									distance = 10
									angle = random
								}
							}
						}

						# Army Transports
						swarm_armies = yes
						swarm_armies = yes
						swarm_armies = yes
					}
				}
			}

			### THIRD SYSTEM
			random_system = {
				limit = { has_star_flag = swarm_invasion_target_3 }
				if = {
					limit = {
						any_fleet_in_system = {
							is_ship_class = shipclass_starbase
						}
					}
					random_fleet_in_system = {
						limit = {
							is_ship_class = shipclass_starbase
						}
						destroy_fleet = this
					}
				}
				create_starbase = {
					size = starbase_swarm
					owner = event_target:prethoryn
				}
				random_system_planet = {
					save_event_target_as = spawn_location
					event_target:prethoryn = {

						swarm_brood = yes
						swarm_brood = yes
						if = {
							limit = {
								is_difficulty >= 3
							}
							swarm_brood = yes
						}
						if = {
							limit = {
								is_difficulty >= 5
							}
							swarm_brood = yes
						}
						if = {
							limit = {
								is_difficulty >= 6
							}
							swarm_brood = yes
						}

						# Infestors
						while = {
							count = 3
							create_fleet = {
								name = "NAME_Prethoryn_Infestor"
								effect = {
									set_owner = event_target:prethoryn
									create_ship = {
										name = random
										design = "NAME_Swarm_Colonizer"
										graphical_culture = "swarm_01"
									}
									set_location = {
										target = PREVPREV
										distance = 10
										angle = random
									}
								}
							}
						}

						# Constructor
						create_fleet = {
							name = "NAME_Prethoryn_Constructor"
							effect = {
								set_owner = event_target:prethoryn
								create_ship = {
									name = random
									design = "NAME_Swarm_Constructor"
									graphical_culture = "swarm_01"
								}
								set_location = {
									target = PREVPREV
									distance = 10
									angle = random
								}
							}
						}

						# Army Transports
						create_fleet = {
							name = "NAME_Prethoryn_Armies"
							effect = {
								set_owner = event_target:prethoryn
								while = {
									count = 3
									create_army_transport = {
										ship_name = "NAME_Prethoryn_Transport"
										graphical_culture = "swarm_01"
										army_name = "NAME_Prethoryn_Invaders"
										army_type = "swarm_army"
										species = event_target:prethoryn_species
									}
								}
								set_location = {
									target = PREVPREV
									distance = 10
									angle = random
								}
							}
						}
						swarm_armies = yes
						swarm_armies = yes
						swarm_armies = yes
					}
				}
			}

			### FOURTH SYSTEM
			random_system = {
				limit = { has_star_flag = swarm_invasion_target_4 }
				if = {
					limit = {
						any_fleet_in_system = {
							is_ship_class = shipclass_starbase
						}
					}
					random_fleet_in_system = {
						limit = {
							is_ship_class = shipclass_starbase
						}
						destroy_fleet = this
					}
				}
				create_starbase = {
					size = starbase_swarm
					owner = event_target:prethoryn
				}
				random_system_planet = {
					save_event_target_as = spawn_location
					event_target:prethoryn = {
						swarm_brood = yes
						swarm_brood = yes
						if = {
							limit = {
								is_difficulty >= 3
							}
							swarm_brood = yes
						}
						if = {
							limit = {
								is_difficulty >= 5
							}
							swarm_brood = yes
						}
						if = {
							limit = {
								is_difficulty >= 6
							}
							swarm_brood = yes
						}

						# Infestors
						while = {
							count = 3
							create_fleet = {
								name = "NAME_Prethoryn_Infestor"
								effect = {
									set_owner = event_target:prethoryn
									create_ship = {
										name = random
										design = "NAME_Swarm_Colonizer"
										graphical_culture = "swarm_01"
									}
									set_location = {
									target = PREVPREV
									distance = 10
									angle = random
								}
								}
							}
						}

						# Constructor
						create_fleet = {
							name = "NAME_Prethoryn_Constructor"
							effect = {
								set_owner = event_target:prethoryn
								create_ship = {
									name = random
									design = "NAME_Swarm_Constructor"
									graphical_culture = "swarm_01"
								}
								set_location = {
									target = PREVPREV
									distance = 10
									angle = random
								}
							}
						}

						# Army Transports
						swarm_armies = yes
						swarm_armies = yes
						swarm_armies = yes
					}
				}
			}
		}
	}
}

# Main Invasion Arrives
country_event = {
	id = crisis.20
	title = "crisis.20.name"
	desc = "crisis.20.desc"
	picture = GFX_evt_physics_research
	show_sound = event_alien_nature
	location = event_target:prethoryn_invasion_system

	is_triggered_only = yes

	immediate = {
		random_rim_system = {
			limit = { has_star_flag = swarm_invasion_target_1 }
			save_event_target_as = prethoryn_invasion_system
		}
	}

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

# Swarm Diplomacy
country_event = {
	id = crisis.30
	title = "TRANSMISSION"

	desc = {
		text = crisis.30.desc_01
		trigger = {
			can_talk_to_prethoryn = no
		}
	}
	desc = {
		text = crisis.30.desc_02
		trigger = {
			can_talk_to_prethoryn = no
		}
	}
	desc = {
		text = crisis.30.desc_03
		trigger = {
			can_talk_to_prethoryn = no
		}
	}
	desc = {
		text = crisis.30.desc_07
		trigger = {
			can_talk_to_prethoryn = yes
		}
	}
	desc = {
		text = crisis.30.desc_08
		trigger = {
			can_talk_to_prethoryn = yes
		}
	}
	desc = {
		text = crisis.30.desc_09
		trigger = {
			can_talk_to_prethoryn = yes
		}
	}

	diplomatic = yes

	picture_event_data = {
		portrait = swarm1
		room = "swarm_room"
	}

	is_triggered_only = yes

	trigger = {
		FROM = {
			is_country_type = swarm
		}
	}

	option = {
		name = crisis.30.a
		trigger = {
			can_talk_to_prethoryn = no
		}
		response_text = crisis.30.a.response
	}
	option = {
		name = crisis.30.b
		trigger = {
			OR = {
				has_ethic = "ethic_pacifist"
				has_ethic = "ethic_fanatic_pacifist"
			}
			can_talk_to_prethoryn = no
		}
		response_text = crisis.30.b.response
	}
	option = {
		name = crisis.30.c
		trigger = {
			is_militarist = yes
			can_talk_to_prethoryn = no
		}
		response_text = crisis.30.c.response
	}
	option = {
		name = crisis.30.d
		trigger = {
			is_xenophobe = yes
			can_talk_to_prethoryn = no
		}
		response_text = crisis.30.d.response
	}
	option = {
		name = crisis.30.e
		trigger = {
			OR = {
				has_ethic = "ethic_xenophile"
				has_ethic = "ethic_fanatic_xenophile"
			}
			can_talk_to_prethoryn = no
		}
		response_text = crisis.30.e.response
	}
	option = {
		name = crisis.30.f
		trigger = {
			OR = {
				has_ethic = "ethic_spiritualist"
				has_ethic = "ethic_fanatic_spiritualist"
			}
			can_talk_to_prethoryn = no
		}
		response_text = crisis.30.f.response
	}
	option = {
		name = crisis.30.g
		trigger = {
			can_talk_to_prethoryn = yes
		}
		response_text = crisis.30.g.response
		is_dialog_only = yes
	}
	option = {
		name = crisis.30.h
		trigger = {
			can_talk_to_prethoryn = yes
		}
		response_text = crisis.30.h.response
		is_dialog_only = yes
		hidden_effect = {
			set_country_flag = learned_about_redacted
		}
	}
	option = {
		name = crisis.30.i
		trigger = {
			can_talk_to_prethoryn = yes
		}
		response_text = crisis.30.i.response
		is_dialog_only = yes
	}
	option = {
		name = crisis.30.j
		trigger = {
			can_talk_to_prethoryn = yes
		}
		response_text = crisis.30.j.response
		is_dialog_only = yes
		hidden_effect = {
			set_country_flag = learned_about_redacted
		}
	}
	option = {
		name = crisis.30.a
		trigger = {
			can_talk_to_prethoryn = yes
		}
		response_text = crisis.30.k.response
	}
}


# Sentinels
country_event = {
	id = crisis.50
	hide_window = yes

	trigger = {
		has_global_flag = prethoryn_main_invasion
		NOT = { has_global_flag = sentinels_founded }
		is_country_type = swarm
		endgame_crisis_large_size_trigger = yes
	}

	mean_time_to_happen = {
		months = 12
	}

	immediate = {
		set_crisis_sound = swarm_crisis_ambient_stage_3
		set_crisis_stage_3 = yes
		set_global_flag = sentinels_founded
		save_event_target_as = swarm

		random_system = {
			limit = {
				exists = space_owner
				space_owner = {
					NOT = { is_same_value = event_target:swarm }
				}
				NOR = {
					has_star_flag = sealed_system
					has_star_flag = surveillance_supercomputer_system
					has_star_flag = lcluster
				}
			}
			spawn_system = {
				min_distance >= 20
				max_distance <= 50
				initializer = "sentinel_system"
			}
		}
		if = {
			limit = {
				any_country = {
					OR = {
						is_country_type = fallen_empire
						is_country_type = awakened_fallen_empire
					}
				}
			}
			set_global_flag = sentinel_fe_support
			random_country = {
				limit = {
					OR = {
						is_country_type = fallen_empire
						is_country_type = awakened_fallen_empire
					}
				}
				save_global_event_target_as = sentinel_fallen_empire
			}
			every_country = {
				limit = { is_country_type = default }
				establish_communications = event_target:sentinel_fallen_empire
			}
		}
		observer_event = { id = observer.31 }
		every_country = {
			limit = { is_country_type = default }
			country_event = { id = crisis.51 days = 1 }
		}
		event_target:sentinel_alpha = {
			fleet_event = { id = crisis.60 }
			fleet_event = { id = crisis.60 days = 10 }
		}
	}
}

# The Sentinel Order is Founded
country_event = {
	id = crisis.51
	title = "crisis.51.2.name"
	desc = "crisis.51.desc"
	picture = GFX_evt_federation_fleet
	show_sound = event_radio_chatter
	location = event_target:sentinel_system

	is_triggered_only = yes

	option = {
		name = crisis.51.a
		hidden_effect = {
			country_event = { id = crisis.52 }
		}
	}
}

# Order Communicates
country_event = {
	id = crisis.52
	title = "sentinel_title"
	desc = "crisis.52.desc"

	diplomatic = yes

	picture_event_data = {
		portrait = event_target:crisis_sentinels
		room = "ethic_spaceship_room"
	}

	is_triggered_only = yes

	option = {
		name = crisis.52.a
		trigger = {
			exists = event_target:sentinel_fallen_empire
		}
		response_text = crisis.52.a.response
		is_dialog_only = yes
	}
	option = {
		name = crisis.52.a
		trigger = {
			NOT = { exists = event_target:sentinel_fallen_empire }
		}
		response_text = crisis.52.2.a.response
		is_dialog_only = yes
	}
	option = {
		name = crisis.52.b
		trigger = {
			exists = event_target:sentinel_fallen_empire
		}
		response_text = crisis.52.new.b.response
		is_dialog_only = yes
	}
	option = {
		name = crisis.52.b
		trigger = {
			NOT = { exists = event_target:sentinel_fallen_empire }
		}
		response_text = crisis.52.2.new.b.response
		is_dialog_only = yes
	}
	option = {
		name = crisis.52.c
		response_text = crisis.52.c.response
	}
}

# Sentinel Diplomacy
country_event = {
	id = crisis.55
	title = "sentinel_title"

	desc = {
		text = crisis.55.desc_01
	}
	desc = {
		text = crisis.55.desc_02
	}
	desc = {
		text = crisis.55.desc_03
	}
	desc = {
		text = crisis.55.desc_04
	}

	diplomatic = yes

	picture_event_data = {
		portrait = FROM
		room = "ethic_spaceship_room"
	}

	is_triggered_only = yes

	trigger = {
		FROM = {
			is_country_type = sentinels
		}
	}

	option = {
		name = crisis.55.a
		response_text = crisis.55.a.response
		is_dialog_only = yes
	}
	option = {
		name = crisis.55.d
		response_text = crisis.55.d.response
	}
}

# Sentinel Reinforcements (HIDDEN)
fleet_event = {
	id = crisis.60
	hide_window = yes

	trigger = {
		has_global_flag = ongoing_prethoryn_invasion
		has_global_flag = sentinels_founded
		owner = { is_country_type = sentinels }
		owner = { num_ships < 200 }
		is_same_value = event_target:sentinel_alpha
		any_country = {
			is_country_type = default
		}
	}

	mean_time_to_happen = {
		months = 100
	}

	immediate = {
		random_country = {
			limit = { is_country_type = default }
			owner_species = { save_event_target_as = random_species }
		}
		owner = {
			create_leader = {
				class = commander
				species = event_target:random_species
				name = random
				skill = 3
				traits = {
					0 = leader_trait_sentinel
				}
			}
			create_fleet = {
				name = "NAME_Sentinel_Battle_Fleet"
				effect = {
					set_owner = root.owner
					create_ship = {
						name = random
						design = "NAME_Spearhead"
						graphical_culture = "mammalian_01"
					}
					assign_leader = last_created_leader
					while = {
						count = 2
						create_ship = {
							name = random
							design = "NAME_Spearhead"
							graphical_culture = "reptilian_01"
						}
					}
					while = {
						count = 4
						create_ship = {
							name = random
							design = "NAME_Deaths_Head"
							graphical_culture = "mammalian_01"
						}
					}
					while = {
						count = 2
						create_ship = {
							name = random
							design = NAME_Beta
							graphical_culture = "fallen_empire_01"
						}
					}
					while = {
						count = 6
						create_ship = {
							name = random
							design = "NAME_Bug_Crusher"
							graphical_culture = "avian_01"
						}
					}
					while = {
						count = 6
						create_ship = {
							name = random
							design = NAME_Gamma
							graphical_culture = "fallen_empire_01"
						}
					}
					while = {
						count = 12
						create_ship = {
							name = random
							design = "NAME_Blade"
							graphical_culture = "reptilian_01"
						}
					}
					set_location = {
						target = root
						distance = 20
						angle = random
					}
					set_aggro_range = 500
					set_fleet_stance = aggressive
					set_aggro_range_measure_from = self
				}
			}
		}
	}
}

# Swarm Kill Count
country_event = {
	id = crisis.70
	hide_window = yes

	is_triggered_only = yes

	trigger = {
		exists = from
		has_global_flag = sentinels_founded
		is_country_type = default
		FROM = { is_country_type = swarm }
	}

	immediate = {
		change_variable = {
			which = "swarm_fleet_kills"
			value = 1
		}
	}
}

# Ship Donation
country_event = {
	id = crisis.71
	title = "sentinel_title"
	desc = "crisis.71.desc"

	diplomatic = yes

	picture_event_data = {
		portrait = event_target:crisis_sentinels
		room = "ethic_spaceship_room"
	}

	trigger = {
		has_global_flag = sentinels_founded
		is_country_type = default
		NOT = { has_country_flag = sentinel_donation }
		check_variable = {
			which = "swarm_fleet_kills"
			value > 6
		}
		any_country = {
			is_country_type = sentinels
			count_controlled_ship = {
				count > 30
			}
			ROOT = {
				NOT = { is_at_war_with = PREV }
			}
		}
	}

	mean_time_to_happen = {
		months = 100
	}

	immediate = {
		set_country_flag = sentinel_donation
	}

	option = {
		name = crisis.71.a
		response_text = crisis.71.a.response
		custom_tooltip = sentinel_fleet_gift
		hidden_effect = {
			capital_scope = {

				create_fleet = {
					name = "NAME_Sentinel_Auxiliary_Fleet"
					effect = {
						set_owner = event_target:crisis_sentinels
						while = {
							count = 1
							create_ship = {
								name = random
								design = "NAME_Spearhead"
								graphical_culture = "reptilian_01"
							}
						}
						while = {
							count = 1
							create_ship = {
								name = random
								design = "NAME_Deaths_Head"
								graphical_culture = "mammalian_01"
							}
						}
						while = {
							count = 1
							create_ship = {
								name = random
								design = NAME_Beta
								graphical_culture = "fallen_empire_01"
							}
						}
						while = {
							count = 4
							create_ship = {
								name = random
								design = "NAME_Bug_Crusher"
								graphical_culture = "avian_01"
							}
						}
						while = {
							count = 4
							create_ship = {
								name = random
								design = NAME_Gamma
								graphical_culture = "fallen_empire_01"
							}
						}
						while = {
							count = 8
							create_ship = {
								name = random
								design = "NAME_Blade"
								graphical_culture = "reptilian_01"
							}
						}
						set_owner = root
						set_location = {
							target = PREV
							distance = 45
							angle = random
						}
					}
					settings = {
						can_upgrade = no
						can_change_composition = no
						uses_naval_capacity = no
					}
				}
			}
		}
	}
	option = {
		name = crisis.71.b
		response_text = crisis.71.b.response
	}
}

# Sentinels Disband (HIDDEN)
country_event = {
	id = crisis.75
	hide_window = yes

	is_triggered_only = yes

	immediate = {
		every_country = {
			limit = { is_ai = no }
			country_event = { id = crisis.76 }
		}
		every_controlled_fleet = { delete_fleet = this }
		destroy_country = yes
	}
}

# Order Communicates
country_event = {
	id = crisis.76
	title = "sentinel_title"
	desc = "crisis.76.desc"

	diplomatic = yes

	picture_event_data = {
		portrait = event_target:crisis_sentinels
		room = "ethic_spaceship_room"
	}

	is_triggered_only = yes

	option = {
		name = crisis.76.c
	}
}

# Anatomy Files (HIDDEN)
country_event = {
	id = crisis.77
	hide_window = yes

	trigger = {
		has_global_flag = ongoing_prethoryn_invasion
		is_country_type = sentinels
		NOT = { has_country_flag = sentinel_anatomy_files }
		exists = event_target:sentinel_alpha
		any_country = {
			is_country_type = swarm
			galaxy_percentage > 0.50
		}
	}

	mean_time_to_happen = {
		months = 12
	}

	immediate = {
		set_country_flag = sentinel_anatomy_files
		every_country = {
			limit = {
				OR = {
					is_country_type = default
					is_country_type = fallen_empire
					is_country_type = awakened_fallen_empire
				}
			}
			country_event = { id = crisis.78 }
		}
	}
}

# Anatomy Files
country_event = {
	id = crisis.78
	title = "sentinel_title"
	desc = "crisis.78.desc"

	diplomatic = yes

	picture_event_data = {
		portrait = event_target:crisis_sentinels
		room = "ethic_spaceship_room"
	}

	is_triggered_only = yes

	option = {
		name = crisis.78.c
		add_modifier = {
			modifier = "sentinel_data"
			days = -1
		}
	}
}

# Fall of the Sentinels (HIDDEN)
country_event = {
	id = crisis.80
	hide_window = yes

	is_triggered_only = yes

	trigger = {
		is_country_type = sentinels
		from = { is_country_type = swarm }
		fromfrom = { is_ship_size = sentinel_station }
	}

	immediate = {
		observer_event = { id = observer.32 }
		every_country = {
			limit = { is_ai = no }
			country_event = { id = crisis.81 }
		}
	}
}

# Fall of the Sentinels
country_event = {
	id = crisis.81
	title = "crisis.81.name"
	desc = "crisis.81.desc"
	picture = GFX_evt_federation_fleet
	show_sound = event_radio_chatter
	location = event_target:sentinel_system

	is_triggered_only = yes

	option = {
		name = ONSCREEN
		hidden_effect = {
			country_event = { id = crisis.82 }
		}
	}
}

# Last Transmission
country_event = {
	id = crisis.82
	title = "sentinel_title"
	desc = "crisis.82.desc"

	diplomatic = yes

	picture_event_data = {
		room = no_video_feed_room
	}

	is_triggered_only = yes

	option = {
		name = crisis.76.c
	}
}

# Swarm Queen (HIDDEN)
country_event = {
	id = crisis.100
	hide_window = yes
	fire_only_once = yes

	trigger = {
		has_global_flag = prethoryn_main_invasion
		is_country_type = swarm
		NOT = { has_country_flag = injured_swarm_queen }
		any_system = {
			exists = owner
			owner = { is_country_type = swarm }
		}
	}

	mean_time_to_happen = {
		months = 820
	}

	immediate = {
		set_country_flag = injured_swarm_queen
		random_system = {
			limit = {
				any_system_planet = {
					exists = owner
					owner = { is_country_type = swarm }
				}
			}
			save_event_target_as = queen_system
			create_ambient_object = {
				type = "injured_queen_object"
				location = solar_system
			}
			last_created_ambient_object = { save_event_target_as = injured_queen }
			create_ambient_object = {
				type = "large_debris_object"
			}
			last_created_ambient_object = {
				set_location = {
					target = event_target:injured_queen
					distance = 5
					angle = random
				}
				set_ambient_object_flag = dead_prethoryn
			}
			create_ambient_object = {
				type = "dead_swarm_small_object"
			}
			last_created_ambient_object = {
				set_location = {
					target = event_target:injured_queen
					distance = 15
					angle = random
				}
				set_ambient_object_flag = dead_prethoryn
			}
			create_ambient_object = {
				type = "dead_swarm_small_object"
			}
			last_created_ambient_object = {
				set_location = {
					target = event_target:injured_queen
					distance = 20
					angle = random
				}
				set_ambient_object_flag = dead_prethoryn
			}
			create_ambient_object = {
				type = "dead_swarm_small_object"
			}
			last_created_ambient_object = {
				set_location = {
					target = event_target:injured_queen
					distance = 25
					angle = random
				}
				set_ambient_object_flag = dead_prethoryn
			}
			create_ambient_object = {
				type = "dead_swarm_large_object"
			}
			last_created_ambient_object = {
				set_location = {
					target = event_target:injured_queen
					distance = 10
					angle = random
				}
				set_ambient_object_flag = dead_prethoryn
			}
			create_ambient_object = {
				type = "dead_swarm_large_object"
			}
			last_created_ambient_object = {
				set_location = {
					target = event_target:injured_queen
					distance = 15
					angle = random
				}
				set_ambient_object_flag = dead_prethoryn
			}
			create_ambient_object = {
				type = "dead_swarm_large_object"
			}
			last_created_ambient_object = {
				set_location = {
					target = event_target:injured_queen
					distance = 20
					angle = random
				}
				set_ambient_object_flag = dead_prethoryn
			}
		}
		every_country = {
			limit = { is_country_type = default }
			country_event = { id = crisis.101 }
		}
	}
}

# Swarm Queen
country_event = {
	id = crisis.101
	title = "crisis.101.name"
	desc = "crisis.101.desc"
	picture = GFX_evt_small_space_battle
	show_sound = event_radio_chatter
	trackable = yes

	is_triggered_only = yes

	option = {
		name = crisis.101.a
		event_target:injured_queen = {
			enable_special_project = {
				name = "CAPTURE_QUEEN"
				location = this
				owner = ROOT
			}
		}
	}
}

# Queen Captured
ship_event = {
	id = crisis.105
	title = "crisis.105.name"
	desc = "crisis.105.desc"
	picture = GFX_evt_society_research
	show_sound = event_alien_nature

	is_triggered_only = yes

	immediate = {
		owner = {
			save_event_target_as = ship_owner
			set_country_flag = queen_captured
		}
		solar_system = { save_event_target_as = queen_system_1 }
		random_country = {
			limit = { is_country_type = swarm }
			owner_species = { save_event_target_as = swarm_species }
		}
		every_country = {
			limit = {
				is_country_type = default
				NOT = { is_same_value = event_target:ship_owner }
				has_special_project = CAPTURE_QUEEN
			}
			abort_special_project = {
				type = "CAPTURE_QUEEN"
				location = fromfrom
			}
			country_event = { id = crisis.107 }
		}
		destroy_ambient_object = fromfrom
	}

	option = {
		name = crisis.105.a
		hidden_effect = {
			owner = {
				create_leader = {
					class = commander
					species = event_target:swarm_species
					name = "NAME_Fleet_Consciousness"
					skill = 8
					event_leader = yes	# prevents this leader from leading factions and winning elections
					immortal = yes	# can't be killed (not even in battle, unless fleet is destroyed)
					effect = {
						set_leader_flag = captured_queen
					}
				}
			}
			create_fleet = {
				name = "NAME_Domesticated_Prethoryn"
				effect = {
					set_fleet_flag = pet_queen
					set_owner = event_target:ship_owner
					create_ship = {
						name = "NAME_Domesticated_Queen"
						design = "NAME_Swarm_Queen"
						graphical_culture = "swarm_01"
						upgradable = no
					}
					assign_leader = last_created_leader
					set_location = {
						target = root
						distance = 10
						angle = random
					}
				}
				settings = {
					can_upgrade = no
					can_disband = no
					can_change_composition = no
					can_change_leader = no
					uses_naval_capacity = no
				}
			}
			solar_system = {
				every_system_ambient_object = {
					limit = { has_ambient_object_flag = dead_prethoryn }
					destroy_ambient_object = this
				}
			}
		}
	}
}

# Queen Breeds
fleet_event = {
	id = crisis.106
	hide_window = yes

	trigger = {
		has_global_flag = prethoryn_main_invasion
		has_fleet_flag = pet_queen
		num_ships < 30
		is_ship_size = queen_swarm
	}

	mean_time_to_happen = {
		months = 2
	}

	immediate = {
		random_list = {
			33 = {
				create_ship = {
					name = "NAME_Domesticated_Warrior"
					design = "NAME_Swarm_Large"
					graphical_culture = "swarm_01"
					upgradable = no
				}
			}
			66 = {
				create_ship = {
					name = "NAME_Domesticated_Swarmling"
					design = "NAME_Swarm_Small"
					graphical_culture = "swarm_01"
					upgradable = no
				}
			}
		}
	}
}

# Someone else got her
country_event = {
	id = crisis.107
	title = "crisis.107.name"
	desc = "crisis.107.desc"
	picture = GFX_evt_society_research
	show_sound = event_radio_chatter

	is_triggered_only = yes

	immediate = {
		set_country_flag = queen_snatched
	}

	option = {
		name = crisis.107.a
	}
}

# Queen Dies
country_event = {
	id = crisis.108
	hide_window = yes

	trigger = {
		fromfrom = { has_fleet_flag = pet_queen }
	}

	is_triggered_only = yes

	immediate = {
		random_owned_leader = {
			limit = { has_leader_flag = captured_queen }
			kill_leader = { show_notification = no }
		}
	}
}

# Feral Prethoryn Can Spawn
country_event = {
	id = crisis.119
	hide_window = yes

	is_triggered_only = yes

	immediate = {
		set_variable = {
			which = "feral_prethoryn_spawned"
			value = 0
		}
		set_timed_global_flag = { flag = feral_prethoryn_can_spawn days = 5400 }
	}
}

# Feral Prethoryn (HIDDEN)
event = {
	id = crisis.120
	hide_window = yes

	is_triggered_only = yes

	trigger = {
		has_global_flag = feral_prethoryn_can_spawn
		any_country = {
			AND = {
				is_country_type = global_event
				check_variable = {
					which = "feral_prethoryn_spawned"
					value < 10
				}
			}
		}
		any_system = {
			AND = {
				has_owner = no
				any_system_planet = { has_planet_flag = previously_infested }
				NOT = { has_star_flag = feral_prethoryn_system }
			}
		}
	}

	immediate = {
		random_list = {
			10 = {
				random_country = {
					limit = { is_country_type = global_event }
					change_variable = {
						which = "feral_prethoryn_spawned"
						value = 1
					}
				}
				if = {
					limit = {
						NOT = {
							any_country = { is_country_type = feral_prethoryn }
						}
					}
					random_system = {
						limit = {
							has_owner = no
							any_system_planet = { has_planet_flag = previously_infested }
							NOT = { has_star_flag = feral_prethoryn_system }
						}
						set_star_flag = feral_prethoryn_system
						save_event_target_as = feral_prethoryn_system
						create_species = {
							name = "NAME_Prethoryn"
							class = SWARM
							namelist = Prethoryn
							portrait = random
							traits = random
							immortal = yes

							effect = { save_event_target_as = feral_prethoryn_species }
						}
						create_country = {
							name = "NAME_Feral_Prethoryn"
							type = "feral_prethoryn"
							species = event_target:feral_prethoryn_species
							name_list = "Prethoryn"
							flag = {
								icon= {
									category = "special"
									file = "the_swarm.dds"
								}
								background= {
									category = "backgrounds"
									file = "new_dawn.dds"
								}
								colors={
									"red_orange"
									"grey"
									"null"
									"null"
								}
							}
							effect = {
								save_global_event_target_as = feral_prethoryn
								create_ship_design = { design = "NAME_Swarm_Starbase" }
								add_ship_design = last_created_design
							}
						}
						create_starbase = {
							size = starbase_swarm
							owner = event_target:feral_prethoryn
						}
						random_system_planet = {
							feral_prethoryn_garrison_1 = yes
						}
						random_system_planet = {
							feral_prethoryn_garrison_1 = yes
						}
						random_system_planet = {
							feral_prethoryn_garrison_2 = yes
						}
						every_country = {
							establish_communications_no_message = event_target:feral_prethoryn
						}
						if = {
							limit = {
								NOT = { has_global_flag = feral_prethoryn_appeared }
							}
							every_country = {
								limit = { is_ai = no }
								country_event = { id = crisis.121 }
							}
						}
						set_global_flag = feral_prethoryn_appeared
					}
					break = yes
				}
				if = {
					limit = {
						any_country = { is_country_type = feral_prethoryn }
					}
					random_system = {
						limit = {
							has_owner = no
							any_system_planet = { has_planet_flag = previously_infested }
							NOT = { has_star_flag = feral_prethoryn_system }
						}
						set_star_flag = feral_prethoryn_system
						save_event_target_as = feral_prethoryn_system
						create_starbase = {
							size = starbase_swarm
							owner = event_target:feral_prethoryn
						}
						random_system_planet = {
							feral_prethoryn_garrison_1 = yes
						}
						random_system_planet = {
							feral_prethoryn_garrison_2 = yes
						}
					}
				}
			}
			90 = {}
		}
	}
}

# Feral Prethoryn
country_event = {
	id = crisis.121
	title = "crisis.121.name"
	desc = "crisis.121.desc"
	picture = GFX_evt_society_research
	show_sound = event_radio_chatter
	location = event_target:feral_prethoryn_system

	is_triggered_only = yes

	option = {
		name = crisis.121.a
	}
}

# Roaming Feral Prethoryn
event = {
	id = crisis.122
	hide_window = yes

	is_triggered_only = yes

	trigger = {
		exists = event_target:feral_prethoryn
		event_target:feral_prethoryn = {
			NOT = {
				any_controlled_fleet = { has_fleet_flag = roaming_prethoryn_fleet }
			}
		}
	}

	immediate = {
		random_list = {
			50 = {
				event_target:feral_prethoryn = {
					random_system_within_border = {
						random_system_planet = {
							feral_prethoryn_roaming_1 = yes
						}
					}
				}
			}
			50 = {}
		}
	}
}

# Feral Prethoryn Infighting On Action
event = {
	id = crisis.123
	hide_window = yes

	is_triggered_only = yes

	trigger = {
		any_country = {
			is_country_type = feral_prethoryn
			NOT = { has_global_flag = feral_prethoryn_infighting_ongoing }
		}
	}

	immediate = {
		random_list = {
			10 = {
				random_country = {
					limit = { is_country_type = feral_prethoryn }
					country_event = { id = crisis.124 }
				}
			}
			90 = {}
		}
	}
}

# Feral Prethoryn Infighting
country_event = {
	id = crisis.124
	hide_window = yes

	is_triggered_only = yes

	immediate = {
		set_global_flag = feral_prethoryn_infighting_ongoing
		random_planet_within_border = {
			create_fleet = {
				name = "NAME_Feral_Prethoryn"
				effect = {
					set_owner = event_target:feral_prethoryn
					while = {
						count = 56
						create_ship = {
							name = random
							design = "NAME_Swarm_Small"
							graphical_culture = "swarm_01"
						}
					}
					set_location = {
						target = prev
						distance = 80
						angle = 0
					}
				}
			}
			last_created_fleet = {
				set_fleet_flag = infighting_prethoryn
				save_event_target_as = infighting_target_1
			}
			create_country = {
				name = "NAME_Feral_Prethoryn"
				type = "feral_prethoryn_infighting"
				species = event_target:feral_prethoryn_species
				name_list = "Prethoryn"
				flag = {
					icon= {
						category = "special"
						file = "the_swarm.dds"
					}
					background= {
						category = "backgrounds"
						file = "new_dawn.dds"
					}
					colors={
						"red_orange"
						"grey"
						"null"
						"null"
					}
				}
				effect = {
					save_event_target_as = feral_prethoryn_infighting
				}
			}
			create_fleet = {
				name = "NAME_Feral_Prethoryn"
				effect = {
					set_owner = event_target:feral_prethoryn_infighting
					while = {
						count = 56
						create_ship = {
							name = random
							design = "NAME_Swarm_Small"
							graphical_culture = "swarm_01"
						}
					}
					set_location = {
						target = prev
						distance = 80
						angle = 180
					}
				}
			}
			last_created_fleet = {
				save_event_target_as = infighting_target_2
				set_fleet_flag = infighting_prethoryn
				auto_follow_fleet = {
					target = event_target:infighting_target_1
					attack_fleet = yes
				}
			}
			event_target:infighting_target_1 = {
				auto_follow_fleet = {
					target = event_target:infighting_target_2
					attack_fleet = yes
				}
			}
		}
		establish_communications_no_message = event_target:feral_prethoryn_infighting
		set_faction_hostility = {
			target = event_target:feral_prethoryn_infighting
			set_hostile = no
			set_neutral = no
			set_friendly = yes
		}
		event_target:feral_prethoryn_infighting = {
			set_faction_hostility = {
				target = root
				set_hostile = no
				set_neutral = no
				set_friendly = yes
			}
		}
		every_country = {
			limit = { is_country_type = default }
			establish_communications_no_message = event_target:feral_prethoryn_infighting
		}
	}
}

# Prethoryn Infighting Over
country_event = {
	id = crisis.125
	hide_window = yes

	is_triggered_only = yes

	trigger = {
		is_country_type = feral_prethoryn
		fromfrom = { has_fleet_flag = infighting_prethoryn }
		fromfromfrom = { has_fleet_flag = infighting_prethoryn }
	}

	immediate = {
		remove_global_flag = feral_prethoryn_infighting_ongoing
		fromfromfrom = {
			solar_system = {
				random_system_planet = { save_event_target_as = return_point }
			}
		}
		fromfromfrom = {
			auto_move_to_planet = {
				target = event_target:return_point
				destroy_on_arrival = yes
				clear_auto_move_on_arrival = yes
			}
		}
	}
}

### HIDDEN SWARM FUNCTIONALITY

# Infested to Barren after Orbital Bombardment
carrier_event = {
	id = crisis.200
	hide_window = yes

	is_triggered_only = yes

	trigger = {
		planet_devastation >= 100
		is_planet_class = pc_infested
	}

	immediate = {
		set_planet_flag = previously_infested
		destroy_colony = yes
		random_list = {
			50 = { change_pc = pc_barren }
			50 = { change_pc = pc_barren_cold }
		}
		reset_planet = yes
		add_modifier = { modifier = "terraforming_candidate" days = -1 }
		carrier_event = { id = crisis.216 }
	}
}

# Purge Begins
country_event = {
	id = crisis.201
	hide_window = yes

	is_triggered_only = yes

	trigger = {
		is_country_type = "swarm"
	}

	immediate = {
		FROMFROM = {
			add_threat = { who = root amount = 2 }
			set_controller = root
			# ^ maxes out purge job weight
		}
	}
}

# Purge Complete
carrier_event = {
	id = crisis.202
	hide_window = yes

	is_triggered_only = yes

	trigger = {
		pop_amount <= 100
		exists = controller
		controller = { is_country_type = "swarm" }
	}

	immediate = {
		from = { save_event_target_as = swarm }
		if = {
			limit = {
				is_artificial = no
			}
			change_pc = pc_infested
			set_owner = event_target:swarm
			carrier_event = { id = crisis.215 }
		}
		else_if = {
			# Habitable Ring Worlds can't be infested...
			# they are destroyed instead
			limit = {
				is_ringworld = yes
			}
			destroy_colony = yes
			change_pc = pc_ringworld_habitable_damaged
			reset_planet = yes
		}
		else_if = {
			limit = { is_planet_class = pc_habitat }
			clear_consecration_effect = yes
			spawn_habitat_cracker_effect = yes
		}

		solar_system = {
			if = { # Build starbase if no other colonies are left and no hostile fleet is in the system
				limit = {
					NOR = {
						exists = starbase
						any_system_planet = {
							has_owner = yes
							owner = {
								NOT = { is_same_value = event_target:swarm }
							}
						}
						any_fleet_in_system = {
							owner = { is_hostile = event_target:swarm }
						}
					}
				}
				create_starbase = {
					size = starbase_swarm
					owner = event_target:swarm
				}
				if = {
					limit = {
						NOT = {
							any_system_ambient_object = { has_ambient_object_flag = swarm_system_effect }
						}
					}
					star = {
						create_ambient_object = {
							type = "swarm_1"
							location = this
						}
						last_created_ambient_object = {
							set_ambient_object_flag = swarm_system_effect
							set_location = {
								target = prev
								distance = 0
								angle = random
							}
						}
					}
				}
			}
		}
	}
}

# Planet Colonized
carrier_event = {
	id = crisis.203
	hide_window = yes

	is_triggered_only = yes

	trigger = {
		owner = { is_country_type = "swarm" }
	}

	immediate = {
		every_owned_pop_group = { kill_all_pop = yes }
		if = {
			limit = {
				is_artificial = no
			}
			change_pc = pc_infested
			reset_planet = yes
			carrier_event = { id = crisis.215 }
		}
		else_if = {
			# Habitable Ring Worlds can't be infested...
			# they are destroyed instead
			limit = {
				is_ringworld = yes
			}
			change_pc = pc_ringworld_habitable_damaged
			reset_planet = yes
		}
		else_if = {
			limit = { is_planet_class = pc_habitat }
			clear_consecration_effect = yes
			spawn_habitat_cracker_effect = yes
		}
	}
}

# Spawn Infestors if less than 4 are available, on_yearly_pulse
event = {
	id = crisis.204
	hide_window = yes

	is_triggered_only = yes

	trigger = {
		any_country = {
			is_country_type = "swarm"
			count_controlled_ship = {
				limit = { is_ship_size = colony_ship_swarm }
				count < 4
			}
		}
	}

	immediate = {
		random_country = {
			limit = { is_country_type = swarm }
			if = {
				limit = {
					NOT = {
						exists = event_target:prethoryn
					}
				}
				save_event_target_as = prethoryn
			}
			random_controlled_ship = {
				limit = { exists = solar_system }
				save_event_target_as = infestor_respawn_system
			}
			while = {
				count = 5
				create_fleet = {
					name = "NAME_Prethoryn_Infestor"
					effect = {
						set_owner = event_target:prethoryn
						create_ship = {
							name = random
							design = "NAME_Swarm_Colonizer"
							graphical_culture = "swarm_01"
						}
						set_location = {
							target = event_target:infestor_respawn_system
							distance = 5
							angle = random
						}
					}
				}
			}
		}
	}
}

# Spawn Constructors if less than 4 are available, on_yearly_pulse
event = {
	id = crisis.205
	hide_window = yes

	is_triggered_only = yes

	trigger = {
		any_country = {
			is_country_type = "swarm"
			num_ships > 10
			count_controlled_ship = {
				limit = { is_ship_size = construction_ship_swarm }
				count < 4
			}
		}
	}

	immediate = {
		random_country = {
			limit = { is_country_type = swarm }
			if = {
				limit = {
					NOT = {
						exists = event_target:prethoryn
					}
				}
				save_event_target_as = prethoryn
			}
			random_controlled_ship = {
				save_event_target_as = constructor_respawn_system
			}
			while = {
				count = 3
				create_fleet = {
					name = "NAME_Prethoryn_Constructor"
					effect = {
						set_owner = event_target:prethoryn
						create_ship = {
							name = random
							design = "NAME_Swarm_Constructor"
							graphical_culture = "swarm_01"
						}
						set_location = {
							target = event_target:constructor_respawn_system
							distance = 5
							angle = random
						}
					}
				}
			}
		}
	}
}

# Build new fleet every 2 years
event = {
	id = crisis.206
	hide_window = yes

	is_triggered_only = yes

	trigger = {
		any_country = {
			is_country_type = swarm
			num_owned_planets > 0
			any_system_within_border = { exists = starbase }
		}
	}

	immediate = {
		if = {
			limit = {
				is_difficulty > 3
				any_country = {
					is_country_type = swarm
					num_ships < 2000
				}
			}
			random_country = {
				limit = {
					is_country_type = swarm
				}
				country_event = { id = crisis.207 }
			}
		}
		else_if = {
			limit = {
				is_difficulty <= 3
				any_country = {
					is_country_type = swarm
					num_ships < 750
				}
			}
			random_country = {
				limit = {
					is_country_type = swarm
				}
				country_event = { id = crisis.207 }
			}
		}
	}
}

# Build new fleet
country_event = {
	id = crisis.207
	hide_window = yes

	is_triggered_only = yes

	immediate = {
		save_event_target_as = prethoryn
		owner_species = { save_event_target_as = prethoryn_species }


		random_system_within_border = {
			limit = { exists = starbase }
			starbase = { save_event_target_as = spawn_location }
		}
		swarm_brood = yes

		random_system_within_border = {
			limit = { exists = starbase }
			starbase = { save_event_target_as = spawn_location }
		}
		swarm_brood = yes

		if = {
			limit = {
				is_difficulty >= 3
			}
			random_system_within_border = {
				limit = { exists = starbase }
				starbase = { save_event_target_as = spawn_location }
			}
			swarm_brood = yes
		}

		if = {
			limit = {
				is_difficulty >= 5
			}
			random_system_within_border = {
				limit = { exists = starbase }
				starbase = { save_event_target_as = spawn_location }
			}
			swarm_brood = yes
		}

		if = {
			limit = {
				galaxy_percentage > 0.20
			}
			random_system_within_border = {
				limit = { exists = starbase }
				starbase = { save_event_target_as = spawn_location }
			}
			swarm_brood = yes
		}

		if = {
			limit = {
				galaxy_percentage > 0.40
			}
			random_system_within_border = {
				limit = { exists = starbase }
				starbase = { save_event_target_as = spawn_location }
			}
			swarm_brood = yes
		}

		if = {
			limit = {
				count_controlled_ship = {
					limit = { is_ship_size = colony_ship_swarm }
					count < 3
				}
			}

			random_owned_planet = {
				limit = {
					has_orbital_bombardment = no
				}
				# Infestor
				create_fleet = {
					name = "NAME_Prethoryn_Infestor"
					effect = {
						set_owner = event_target:prethoryn
						create_ship = {
							name = random
							design = "NAME_Swarm_Colonizer"
							graphical_culture = "swarm_01"
						}
						set_location = {
							target = prev
							distance = 35
							angle = random
						}
					}
				}
			}
		}

		if = {
			limit = {
				count_controlled_ship = {
					limit = { is_ship_size = construction_ship_swarm }
					count < 5
				}
			}

			random_owned_planet = {
				limit = {
					has_orbital_bombardment = no
				}
				# Constructor
				create_fleet = {
					name = "NAME_Prethoryn_Constructor"
					effect = {
						set_owner = event_target:prethoryn
						create_ship = {
							name = random
							design = "NAME_Swarm_Constructor"
							graphical_culture = "swarm_01"
						}
						set_location = {
							target = prev
							distance = 35
							angle = random
						}
					}
				}
			}
		}
	}
}

# Destroy Swarm if no planets and fewer than 20 ships remain
# On yearly pulse
event = {
	id = crisis.208
	hide_window = yes

	is_triggered_only = yes

	trigger = {
		any_country = {
			is_country_type = swarm
		}
	}

	immediate = {
		if = {
			limit = {
				any_country = {
					is_country_type = swarm
					num_ships < 20
					NOT = {
						any_system = {
							any_system_planet = {
								exists = controller
								controller = { is_country_type = swarm }
							}
						}
					}
					NOT = {
						any_system = {
							any_system_planet = {
								exists = owner
								owner = { is_country_type = swarm }
							}
						}
					}
				}
			}
			random_country = {
				limit = {
					is_country_type = swarm
				}
				destroy_country = yes
			}
		}
	}
}

# Prethoryn Defeated (HIDDEN)
country_event = {
	id = crisis.210
	hide_window = yes

	is_triggered_only = yes

	trigger = {
		is_country_type = swarm
		has_global_flag = prethoryn_main_invasion
	}

	immediate = {
		stop_crisis_sound = yes
		every_controlled_planet = {
			if = {
				limit = { is_planet_class = pc_cosmogenesis_world }
				set_planet_purge_type = purge_cosmogenesis
			}
			else = {
				clear_planet_purge_type = yes
			}
		}
		end_crisis = yes
		from = { set_country_flag = with_great_power_achievement }
		remove_global_flag = ongoing_prethoryn_invasion
		set_global_flag = prethoryn_invasion_defeated
		remove_global_flag = galactic_crisis_recently_fired
		if = {
			limit = {
				has_global_flag = galactic_crisis_early_defeat_tracker_1
			}
			remove_global_flag = galactic_crisis_early_defeat_tracker_1
			multiply_crisis_strength = 1.5
		}
		else_if = {
			limit = {
				has_global_flag = galactic_crisis_early_defeat_tracker_2
			}
			remove_global_flag = galactic_crisis_early_defeat_tracker_2
			multiply_crisis_strength = 1.25
		}
		observer_event = { id = observer.33 }

		every_country = {
			limit = {
				OR = {
					is_country_type = default
					is_country_type = fallen_empire
					is_country_type = awakened_fallen_empire
				}
			}
			country_event = { id = crisis.211 }
		}

		random_country = {
			limit = { is_country_type = global_event }
			country_event = { id = crisis.119 days = 360 }
		}
		if = {
			limit = { exists = event_target:crisis_sentinels }
			event_target:crisis_sentinels = {
				country_event = { id = crisis.75 days = 15 random = 10 }
			}
		}
	}
}

# Prethoryn Defeated
country_event = {
	id = crisis.211
	title = "crisis.211.name"
	desc = "crisis.211.desc"
	picture = GFX_evt_crisis_defeated
	show_sound = event_celebration

	is_triggered_only = yes

	option = { # Spiritualist Response
		name = crisis.211.a
		trigger = {
			has_spiritualist_government = yes
			NOR = {
				has_ethic = "ethic_fanatic_xenophile"
				has_ethic = "ethic_fanatic_xenophobe"
			}
		}
		end_event_chain = "prethoryn_scourge_chain"
	}
	option = { # Militarist Response
		name = crisis.211.b
		trigger = {
			has_militarist_government = yes
			NOR = {
				has_ethic = "ethic_fanatic_xenophile"
				has_ethic = "ethic_fanatic_xenophobe"
			}
		}
		end_event_chain = "prethoryn_scourge_chain"
	}
	option = { # Materialist Response
		name = crisis.211.c
		trigger = {
			has_materialist_government = yes
			NOR = {
				has_ethic = "ethic_fanatic_xenophile"
				has_ethic = "ethic_fanatic_xenophobe"
			}
		}
		end_event_chain = "prethoryn_scourge_chain"
	}
	option = { # Pacifist Response
		name = crisis.211.d
		trigger = {
			has_pacifist_government = yes
			NOR = {
				has_ethic = "ethic_fanatic_xenophile"
				has_ethic = "ethic_fanatic_xenophobe"
			}
		}
		end_event_chain = "prethoryn_scourge_chain"
	}
	option = { # Corporate Response
		name = crisis.211.e
		trigger = {
			OR = {
				has_government = gov_megacorporation
				has_government = gov_trade_league
				is_megacorp = yes
			}
			NOR = {
				has_ethic = "ethic_fanatic_xenophile"
				has_ethic = "ethic_fanatic_xenophobe"
			}
		}
		end_event_chain = "prethoryn_scourge_chain"
	}
	option = { # Default Response
		name = crisis.211.f
		trigger = {
			OR = {
				has_generic_government = yes
				has_government = gov_enlightened_monarchy
				has_government = gov_elective_monarchy
				has_government = gov_hive_mind
				has_government = gov_wilderness
				has_government = gov_parasitic_overmind
				has_government = gov_successor_khanate
				has_government = gov_diadochi
				has_ethic = ethic_gestalt_consciousness
				is_fallen_empire = yes
			}
			NOR = {
				has_ethic = "ethic_fanatic_xenophile"
				has_ethic = "ethic_fanatic_xenophobe"
			}
		}
		end_event_chain = "prethoryn_scourge_chain"
	}
	option = { # Xenophobe Response
		name = crisis.211.g
		trigger = {
			has_ethic = "ethic_fanatic_xenophobe"
		}
		end_event_chain = "prethoryn_scourge_chain"
	}
	option = { # Xenophile Response
		name = crisis.211.h
		trigger = {
			has_ethic = "ethic_fanatic_xenophile"
		}
		end_event_chain = "prethoryn_scourge_chain"
	}
	after = {
		add_modifier = {
			modifier = "prethoryn_defeated"
			days = 3600
		}
		add_monthly_resource_mult = {
			resource = unity
			value = @tier5unityreward
		}
		hidden_effect = {
			if = {
				limit = {
					has_origin = origin_fear_of_the_dark
				}
				country_event = { id = origin.6136 }
			}
			if = {
				limit = {
					country_uses_bio_ships = yes
				}
				set_country_flag = flesh_adapts_achievement
			}
			if = {
				limit = {
					has_event_chain = defender_of_the_galaxy_ending_chain
				}
				add_event_chain_counter = {
					event_chain = "defender_of_the_galaxy_ending_chain"
					counter = "crisis_defeated"
					amount = 1
				}
			}
		}
	}
}

# World Infested (HIDDEN)
carrier_event = {
	id = crisis.215
	hide_window = yes

	is_triggered_only = yes

	immediate = {
		if = {
			limit = { exists = orbital_defence }
			destroy_fleet = orbital_defence
			set_planet_flag = ruined_orbital_ring_planet
			solar_system = {
				spawn_megastructure = {
					type = orbital_ring_ruined
					planet = prev
				}
			}
		}
		every_country = {
			limit = { has_event_chain = "prethoryn_scourge_chain" }
			add_event_chain_counter = {
				event_chain = "prethoryn_scourge_chain"
				counter = "infested_worlds"
				amount = 1
			}
		}
	}
}

# Infested World Cleansed (HIDDEN)
carrier_event = {
	id = crisis.216
	hide_window = yes

	is_triggered_only = yes

	immediate = {
		every_country = {
			limit = { has_event_chain = "prethoryn_scourge_chain" }
			add_event_chain_counter = {
				event_chain = "prethoryn_scourge_chain"
				counter = "infested_worlds"
				amount = -1
			}
			add_event_chain_counter = {
				event_chain = "prethoryn_scourge_chain"
				counter = "infested_worlds_cleansed"
				amount = 1
			}
		}
	}
}

# Prethoryn Kill Count (HIDDEN)
country_event = {
	id = crisis.217
	hide_window = yes

	is_triggered_only = yes

	trigger = {
		is_country_type = swarm
		exists = from
	}

	immediate = {
		if = {
			limit = {
				FROM = { has_event_chain = "prethoryn_scourge_chain" }
			}
			FROM = {
				add_event_chain_counter = {
					event_chain = "prethoryn_scourge_chain"
					counter = "prethoryn_kills_us"
					amount = 1
				}
			}
		}
		every_country = {
			limit = {
				has_event_chain = "prethoryn_scourge_chain"
				NOT = { is_same_value = FROM }
			}
			add_event_chain_counter = {
				event_chain = "prethoryn_scourge_chain"
				counter = "prethoryn_kills_others"
				amount = 1
			}
		}
	}
}

# Prethoryn Victims (HIDDEN)
country_event = {
	id = crisis.218
	hide_window = yes

	is_triggered_only = yes

	trigger = {
		is_country_type = swarm
	}

	immediate = {
		every_country = {
			limit = { has_event_chain = "prethoryn_scourge_chain" }
			add_event_chain_counter = {
				event_chain = "prethoryn_scourge_chain"
				counter = "prethoryn_victims"
				amount = 1
			}
		}
	}
}

# Purge Ends
country_event = {
	id = crisis.219
	hide_window = yes

	is_triggered_only = yes

	trigger = {
		NOT = { is_country_type = "swarm" }
		fromfrom = {
			controller = {
				is_country_type = "swarm"
			}
		}
	}

	immediate = {
		FROMFROM = {
			set_controller = root
			# should revert purge weights
		}
	}
}

#Star Eater destroying Infested world
system_event = {
	id = crisis.220
	hide_window = yes

	is_triggered_only = yes
	trigger = {
		any_system_planet = {
			is_planet_class = pc_infested
		}
	}

	immediate = {
		every_system_planet = {
		limit = { is_planet_class = pc_infested }
			every_country = {
				limit = { has_event_chain = "prethoryn_scourge_chain" }
				add_event_chain_counter = {
					event_chain = "prethoryn_scourge_chain"
					counter = "infested_worlds"
					amount = -1
				}
				add_event_chain_counter = {
					event_chain = "prethoryn_scourge_chain"
					counter = "infested_worlds_cleansed"
					amount = 1
				}
			}
		}
	}
}

# Events to manage crisis sound loop
country_event = {
	id = crisis.230
	hide_window = yes

	trigger = {
		has_crisis_stage_3 = yes
		is_country_type = swarm
		galaxy_percentage < 0.10
	}

	mean_time_to_happen = {
		months = 1
	}

	immediate = {
		set_crisis_sound = swarm_crisis_ambient_stage_2
		set_crisis_stage_2 = yes
	}
}

country_event = {
	id = crisis.231
	hide_window = yes

	trigger = {
		has_crisis_stage_2 = yes
		is_country_type = swarm
		galaxy_percentage > 0.20
	}

	mean_time_to_happen = {
		months = 1
	}

	immediate = {
		set_crisis_sound = swarm_crisis_ambient_stage_3
		set_crisis_stage_3 = yes
	}
}

country_event = {
	id = crisis.232
	hide_window = yes

	trigger = {
		has_crisis_stage_4 = yes
		is_country_type = swarm
		galaxy_percentage < 0.30
	}

	mean_time_to_happen = {
		months = 1
	}

	immediate = {
		set_crisis_sound = swarm_crisis_ambient_stage_3
		set_crisis_stage_3 = yes
	}
}

country_event = {
	id = crisis.233
	hide_window = yes

	trigger = {
		has_crisis_stage_3 = yes
		is_country_type = swarm
		galaxy_percentage > 0.40
	}

	mean_time_to_happen = {
		months = 1
	}

	immediate = {
		set_crisis_sound = swarm_crisis_ambient_stage_final
		set_crisis_stage_4 = yes
	}
}

# Captured Queens Grows restless
country_event = {
	id = crisis.234
	title = "crisis.234.name"
	desc = {
		trigger = {
			is_robot_empire = no
			is_gestalt = no
		}
		text = crisis.234.desc.organic_non_gestalt
	}
	desc = {
		trigger = {
			OR = {
				is_robot_empire = yes
				is_gestalt = yes
			}
		}
		text = crisis.234.desc.machine_or_gestalt
	}
	picture = GFX_evt_physics_research
	show_sound = event_alien_nature

	trigger = {
		has_global_flag = prethoryn_main_invasion
		is_country_type = default
		has_crisis = no
		owner_species = { is_psionic_species = yes }
		any_owned_leader = {
			has_leader_flag = captured_queen
		}
		NOT = { has_country_flag = crisis.234 }
	}

	mean_time_to_happen = {
		years = 100
	}

	option = {
		name = {
			trigger = {
				if = {
					limit = {
						is_robot_empire = no
						is_gestalt = no
					}
					text = crisis.234.a.organic_non_gestalt
				}
				else = {
					text = crisis.234.a.machine_or_gestalt
				}
			}
		}
		set_country_flag = crisis.234
		add_monthly_resource_mult = {
			resource = physics_research
			value = 10
			min = 1000
			max = 5000
		}
	}
}

# Prethoryn Army Reinforcements
event = {
	id = crisis.240
	hide_window = yes

	is_triggered_only = yes

	trigger = {
		any_country = {
			has_global_flag = ongoing_prethoryn_invasion
			is_country_type = swarm
			num_armies < 140
		}
	}

	immediate = {
		random_country = {
			limit = { is_country_type = swarm }
			save_event_target_as = prethoryn
			owner_species = { save_event_target_as = prethoryn_species }
			random_owned_planet = {
				limit = { is_planet_class = pc_infested }
				create_fleet = {
					name = "NAME_Prethoryn_Armies"
					effect = {
						set_owner = event_target:prethoryn
						while = {
							count = 20
							create_army_transport = {
								ship_name = "NAME_Prethoryn_Transport"
								graphical_culture = "swarm_01"
								army_name = "NAME_Prethoryn_Invaders"
								army_type = "swarm_army"
								species = event_target:prethoryn_species
							}
						}
						set_location = {
							target = PREV
							distance = 10
							angle = random
						}
					}
				}
			}
		}
	}
}

# Brood-Queen Relic Activation
country_event = {
	id = crisis.250
	title = "crisis.250.name"
	desc = "crisis.250.desc"
	picture = GFX_evt_unidentified_monster
	show_sound = event_mystic_reveal

	is_triggered_only = yes

	option = {
		name = GOOD
		hidden_effect = {
			capital_scope = {
				create_fleet = {
					name = "NAME_Loyal_Brood"
					effect = {
						set_owner = root
						while = {
							count = 6
							create_ship = {
								name = random
								prefix = no
								design = "NAME_Swarm_Large"
								graphical_culture = "swarm_01"
							}
						}
						while = {
							count = 2
							create_ship = {
								name = random
								prefix = no
								design = "NAME_Swarm_Carrier"
								graphical_culture = "swarm_01"
							}
						}
						while = {
							count = 5
							create_ship = {
								name = random
								prefix = no
								design = "NAME_Swarm_Small"
								graphical_culture = "swarm_01"
							}
						}
						set_location = {
							target = prev
							distance = 45
							angle = random
						}
					}
					settings = {
						can_change_composition = yes
						uses_naval_capacity = no
						can_disband = yes
					}
				}
			}
			if = { #Backup if you get the relic through some odd way
				limit = {
					NOT = { is_variable_set = brood_usage }
				}
				set_variable = {
					which = brood_usage
					value = 1
				}
			}
			else = {
				change_variable = {
					which = brood_usage
					value = 1
				}
			}
		}
	}
}

# Brood-Queen Captured (HIDDEN)
country_event = {
	id = crisis.251
	hide_window = yes

	is_triggered_only = yes

	trigger = {
		has_global_flag = prethoryn_main_invasion
		NOT = { has_global_flag = brood_queen_captured }
		is_country_type = swarm
		exists = from
		fromfrom = { is_ship_size = queen_swarm }
		from = { is_ai = no }
	}

	immediate = {
		random_list = {
			100 = {}
			5 = {
				set_global_flag = brood_queen_captured
				fromfromfrom = {
					solar_system = { save_event_target_as = battle_system }
				}
				from = { country_event = { id = crisis.252 } }
			}
		}
	}
}

# Brood-Queen Captured
country_event = {
	id = crisis.252
	title = "crisis.252.name"
	desc = "crisis.252.desc"
	picture = GFX_evt_unidentified_monster
	show_sound = event_mystic_reveal

	is_triggered_only = yes

	immediate = {
		set_country_flag = queen_captured
	}

	option = {
		name = EXCELLENT
		add_relic = "r_prethoryn_queen"
		hidden_effect = {
			if = {
				limit = {
					NOT = { is_variable_set = brood_usage }
				}
				set_variable = {
					which = brood_usage
					value = 0
				}
			}
		}
	}
}

# Spawn System Effect
ship_event = {
	id = crisis.260
	hide_window = yes

	is_triggered_only = yes

	immediate = {
		solar_system = {
			star = {
				create_ambient_object = {
					type = "swarm_1"
					location = this
				}
				last_created_ambient_object = {
					set_ambient_object_flag = swarm_system_effect
					set_location = {
						target = prev
						distance = 0
						angle = random
					}
				}
			}
		}
	}
}

# Remove System Effect
country_event = {
	id = crisis.261
	hide_window = yes

	is_triggered_only = yes

	trigger = {
		is_country_type = swarm
		fromfrom = { is_ship_size = starbase_swarm }
	}

	immediate = {
		fromfrom = {
			solar_system = {
				random_system_ambient_object = {
					limit = { has_ambient_object_flag = swarm_system_effect }
					destroy_ambient_object = this
				}
			}
		}
	}
}

# Swarm Destroyed Starbase
starbase_event = {
	id = crisis.265
	hide_window = yes

	is_triggered_only = yes

	trigger = {
		from.owner = { is_country_type = swarm }
		NOT = {
			solar_system = {
				any_system_planet = { is_colony = yes } # For populated systems, they need to invade first
			}
		}
	}

	immediate = {
		solar_system = { save_event_target_as = starbase_system }
		from.owner = {
			country_event = { id = crisis.266 days = 5 } # Delay so that destroyed starbase is properly cleared out
		}
	}
}

# Swarm Builds New Starbase
country_event = {
	id = crisis.266
	hide_window = yes

	is_triggered_only = yes

	immediate = {
		if = {
			limit = {
				exists = event_target:starbase_system
				event_target:starbase_system = {
					any_system_planet = {
						owner = { is_same_value = root }
					}
					NOT = {
						any_fleet_in_system = {
							owner = { is_hostile = root }
						}
					}
				}
			}
			event_target:starbase_system = {
				solar_system = {
					create_starbase = {
						size = starbase_swarm
						owner = root
					}
					star = {
						create_ambient_object = {
							type = "swarm_1"
							location = this
						}
						last_created_ambient_object = {
							set_ambient_object_flag = swarm_system_effect
							set_location = {
								target = prev
								distance = 0
								angle = random
							}
						}
					}
					every_system_planet = {
						limit = { has_orbital_station = yes }
						orbital_station = { dismantle = yes }
					}
				}
			}
		}
		else_if = {
			limit = {
				exists = event_target:starbase_system
				event_target:starbase_system = {
					any_system_planet = {
						owner = { is_same_value = root }
					}
				}
			}
			country_event = { id = crisis.266 days = 30 } # Retry until hostile fleets leave the system
		}
	}
}

# Crisis faction gains control of a planet, starts purging it
# A planets controller becomes a country not the same as the owner.
# Root = Planet
# From = Planet Owner
# FromFrom = Planet Controller (the one occupying)
carrier_event = {
	id = crisis.300
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		FromFrom = {
			OR = {
				is_country_type = swarm
				is_country_type = ai_empire
			}
		}
	}

	immediate = {
		set_planet_purge_type = purge_normal
	}
}

# Crisis faction loses control of a planet, stops purging it
# A planets controller becomes the owner of the planet.
# Root = Planet
# From = Planet Owner
# FromFrom = Previous Planet Controller (the one who was occupying)
carrier_event = {
	id = crisis.301
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		FromFrom = {
			OR = {
				is_country_type = swarm
				is_country_type = ai_empire
				is_country_type = faction
				is_country_type = mechanocalibrator
			}
		}
	}

	immediate = {
		# this may clear the purge type on planets that should have one, hence the if
		if = {
			limit = { is_planet_class = pc_cosmogenesis_world }
			set_planet_purge_type = purge_cosmogenesis
		}
		else = {
			clear_planet_purge_type = yes
		}
	}
}
