############################
#
# Crisis Events
#
# Written by Henrik Eklund
#
############################

namespace = crisis

@tier1unityreward = 6
@tier1unitymin = 100
@tier1unitymax = 500
@tier2unityreward = 12
@tier2unitymin = 150
@tier2unitymax = 1000
@tier3unityreward = 18
@tier3unitymin = 250
@tier3unitymax = 1500
@tier4unityreward = 24
@tier4unitymin = 350
@tier4unitymax = 2000
@tier5unityreward = 48
@tier5unitymin = 700
@tier5unitymax = 4000

### PRETHORYN SCOURGE

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

	trigger = { always = no }

	immediate = {
		set_global_flag = prethoryn_invasion_happened
		set_global_flag = prethoryn_transmission
		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
		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 = 60
	}
	
	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
					NOT = {
						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
							}
						}
					}
				}
			}
			set_star_flag = swarm_invasion_target_1
		}
		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 = {
					NOT = { 
						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 = {
					NOT = { 
						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 = {
					NOT = { 
						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 = {
					NOT = { 
						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 = {
					NOT = { 
						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
				event_chain = "coming_storm_chain"
				category = coming_storm
				location = event_target:swarm_invasion_warning_1
			}
			create_point_of_interest = {
				id = coming_storm_poi.3
				event_chain = "coming_storm_chain"
				category = coming_storm
				location = event_target:swarm_invasion_warning_2
			}
			create_point_of_interest = {
				id = coming_storm_poi.4
				event_chain = "coming_storm_chain"
				category = coming_storm
				location = event_target:swarm_invasion_warning_3
			}
			create_point_of_interest = {
				id = coming_storm_poi.5
				event_chain = "coming_storm_chain"
				category = coming_storm
				location = event_target:swarm_invasion_warning_4
			}
			create_point_of_interest = {
				id = coming_storm_poi.6
				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 = 12
	}
	
	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 = {
			NOT = { owner_species = { OR = { has_trait = trait_psionic has_trait = trait_hive_mind } } }
		}
	}
	desc = {
		text = "crisis.16.b.desc"
		trigger = {
			owner_species = { has_trait = trait_psionic }
		}
	}
	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 = 3
	}
	
	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
		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
					portrait = random
					traits = random
					immortal = yes
				}
				create_country = {
					name = "NAME_Prethoryn_Scourge"
					type = "swarm"
					species = last_created
					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_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_ship_in_system = {
								is_ship_class = shipclass_starbase
							}
						}
						random_fleet_in_system = {
							limit = {
								any_owned_ship = {
									is_ship_class = shipclass_starbase
								}
							}
							destroy_fleet = this
						}
					}
					create_starbase = {
						size = starbase_swarm
						owner = event_target:prethoryn_vgrd
					}
				}
				every_country = {
					limit = { 
						OR = { 
							is_country_type = default
							is_country_type = fallen_empire
							is_country_type = awakened_fallen_empire
						}
					}
					establish_communications_no_message = last_created_country
					add_opinion_modifier = {
						who = last_created_country
						modifier = opinion_swarm
					}
					last_created_country = {
						add_opinion_modifier = {
							who = PREV
							modifier = opinion_prey
						}
					}
				}
				last_created_country = {
					save_event_target_as = prethoryn
					owner_species = { save_event_target_as = prethoryn_species }
					
					### FIRST SYSTEM

					swarm_vanguard = yes
					swarm_vanguard = yes
					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 = {
					last_created_country = {

						swarm_vanguard = yes
						swarm_vanguard = yes
						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
					}
					NOT = { 
						has_star_flag = swarm_invasion_target_1
						has_star_flag = swarm_invasion_target_2
					}
				}
				set_star_flag = swarm_invasion_target_3
				random_system_planet = {
					last_created_country = {

						swarm_vanguard = yes
						swarm_vanguard = yes
						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
					}
					NOT = { 
						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 = {
					last_created_country = {

						swarm_vanguard = yes
						swarm_vanguard = yes
						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 = default }
			country_event = { id = crisis.19 days = 60 random = 20 }
		}
	}
}

# Vanguard Arrives
country_event = {
	id = crisis.18
	title = "crisis.18.name"
	desc = "crisis.18.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.18.a
		trigger = {
			has_spiritualist_government = yes
			NOT = {
				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
			NOT = {
				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
			NOT = {
				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
			NOT = {
				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 = {
			has_government = gov_megacorporation
			NOT = {
				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_ethic = ethic_gestalt_consciousness
			}
			NOT = {
				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
		every_country = {
			limit = { is_country_type = default }
			country_event = { id = crisis.20 }
		}
		random_rim_system = {
			limit = { has_star_flag = swarm_invasion_target_1 }
			#if = {
			#	limit = {
			#		any_ship_in_system = {
			#			OR = {
			#				is_ship_class = shipclass_starbase
			#			}
			#		}
			#	}
			#	random_fleet_in_system = {
			#		limit = {
			#			any_owned_ship = {
			#				is_ship_class = shipclass_starbase
			#			}
			#		}
			#		destroy_fleet = this
			#	}
			#}
			#create_starbase = {
			#	size = starbase_outpost
			#	owner = event_target:prethoryn
			#}
			random_system_planet = {
				save_event_target_as = spawn_location
				event_target:prethoryn = {
					
					### FIRST SYSTEM
					swarm_brood = yes
					swarm_brood = yes
					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_ship_in_system = {
							is_ship_class = shipclass_starbase
						}
					}
					random_fleet_in_system = {
						limit = {
							any_owned_ship = {
								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
						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_ship_in_system = {
							is_ship_class = shipclass_starbase
						}
					}
					random_fleet_in_system = {
						limit = {
							any_owned_ship = {
								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
						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_ship_in_system = {
							is_ship_class = shipclass_starbase
						}
					}
					random_fleet_in_system = {
						limit = {
							any_owned_ship = {
								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
						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 = {
			NOT = { owner_species = { OR = { has_trait = trait_psionic has_trait = trait_hive_mind } } }
		}
	}
	desc = {
		text = crisis.30.desc_02
		trigger = {
			NOT = { owner_species = { OR = { has_trait = trait_psionic has_trait = trait_hive_mind } } }
		}
	}
	desc = {
		text = crisis.30.desc_03
		trigger = {
			NOT = { owner_species = { OR = { has_trait = trait_psionic has_trait = trait_hive_mind } } }
		}
	}
	desc = {
		text = crisis.30.desc_04
		trigger = {
			owner_species = { has_trait = trait_psionic }
		}
	}
	desc = {
		text = crisis.30.desc_05
		trigger = {
			owner_species = { has_trait = trait_psionic }
		}
	}
	desc = {
		text = crisis.30.desc_06
		trigger = {
			owner_species = { has_trait = trait_psionic }
		}
	}
	desc = {
		text = crisis.30.desc_07
		trigger = {
			owner_species = { has_trait = trait_hive_mind }
		}
	}
	desc = {
		text = crisis.30.desc_08
		trigger = {
			owner_species = { has_trait = trait_hive_mind }
		}
	}	
	desc = {
		text = crisis.30.desc_09
		trigger = {
			owner_species = { has_trait = trait_hive_mind }
		}
	}
	
	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 = {
			NOT = { owner_species = { OR = { has_trait = trait_psionic has_trait = trait_hive_mind } } }
		}
		response_text = crisis.30.a.response
	}	
	option = {
		name = crisis.30.b
		trigger = {
			OR = {
				has_ethic = "ethic_pacifist"
				has_ethic = "ethic_fanatic_pacifist"
			}
			NOT = { owner_species = { OR = { has_trait = trait_psionic has_trait = trait_hive_mind } } }
		}
		response_text = crisis.30.b.response
	}
	option = {
		name = crisis.30.c
		trigger = {
			OR = {
				has_ethic = "ethic_militarist"
				has_ethic = "ethic_fanatic_militarist"
			}
			NOT = { owner_species = { OR = { has_trait = trait_psionic has_trait = trait_hive_mind } } }
		}
		response_text = crisis.30.c.response
	}
	option = {
		name = crisis.30.d
		trigger = {
			OR = {
				has_ethic = "ethic_xenophobe"
				has_ethic = "ethic_fanatic_xenophobe"
			}
			NOT = { owner_species = { OR = { has_trait = trait_psionic has_trait = trait_hive_mind } } }
		}
		response_text = crisis.30.d.response
	}
	option = {
		name = crisis.30.e
		trigger = {
			OR = {
				has_ethic = "ethic_xenophile"
				has_ethic = "ethic_fanatic_xenophile"
			}
			NOT = { owner_species = { OR = { has_trait = trait_psionic has_trait = trait_hive_mind } } }
		}
		response_text = crisis.30.e.response
	}
	option = {
		name = crisis.30.f
		trigger = {
			OR = {
				has_ethic = "ethic_spiritualist"
				has_ethic = "ethic_fanatic_spiritualist"
			}
			NOT = { owner_species = { OR = { has_trait = trait_psionic has_trait = trait_hive_mind } } }
		}
		response_text = crisis.30.f.response
	}
	option = {
		name = crisis.30.g
		trigger = {
			owner_species = { has_trait = trait_psionic }
		}
		response_text = crisis.30.g.response
		is_dialog_only = yes
	}	
	option = {
		name = crisis.30.h
		trigger = {
			owner_species = { has_trait = trait_psionic }
		}
		response_text = crisis.30.h.response
		is_dialog_only = yes
	}
	option = {
		name = crisis.30.i
		trigger = {
			owner_species = { has_trait = trait_psionic }
		}
		response_text = crisis.30.i.response
		is_dialog_only = yes
	}
	option = {
		name = crisis.30.j
		trigger = {
			owner_species = { has_trait = trait_psionic }
		}
		response_text = crisis.30.j.response
		is_dialog_only = yes
	}
	option = {
		name = crisis.30.a
		trigger = {
			owner_species = { has_trait = trait_psionic }
		}
		response_text = crisis.30.k.response
	}
	option = {
		name = crisis.30.a.2
		trigger = {
			owner_species = { has_trait = trait_hive_mind }
		}
		response_text = crisis.30.k.response
	}	
	option = {
		name = crisis.30.g.2
		trigger = {
			owner_species = { has_trait = trait_hive_mind }
		}
		response_text = crisis.30.g.2.response
		is_dialog_only = yes
	}
	option = {
		name = crisis.30.h
		trigger = {
			owner_species = { has_trait = trait_hive_mind }
		}
		response_text = crisis.30.h.response
		is_dialog_only = yes
	}
	option = {
		name = crisis.30.i
		trigger = {
			owner_species = { has_trait = trait_hive_mind }
		}
		response_text = crisis.30.i.response
		is_dialog_only = yes
	}
	option = {
		name = crisis.30.j.b
		trigger = {
			owner_species = { has_trait = trait_hive_mind }
		}
		response_text = crisis.30.j.response
		is_dialog_only = yes
	}
}


# 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
		galaxy_percentage > 0.20
	}
	
	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 }
				}
			}
			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
			}
		}
		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: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
	}

	mean_time_to_happen = {
		months = 10
	}
	
	immediate = {
		random_country = {
			limit = { is_country_type = default }
			owner_species = { save_event_target_as = random_species }
		}
		owner = {
			create_leader = {
				type = admiral
				species = event_target:random_species
				name = random
				skill = 3
				traits = {
					trait = leader_trait_sentinel
					trait = random_trait
				}
			}
			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:sentinels
		room = "ethic_spaceship_room"
	}
	
	trigger = {
		is_country_type = default
		has_global_flag = sentinels_founded
		NOT = { has_country_flag = sentinel_donation }
		check_variable = {
			which = "swarm_fleet_kills" 
			value > 6 
		}
		any_country = {
			is_country_type = sentinels
			count_owned_ships = {
				count > 30
			}
			ROOT = {
				NOT = { is_at_war_with = PREV }
			}
		}
	}
	
	mean_time_to_happen = {
		months = 10
	}
	
	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: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_owned_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: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: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 = {
		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 = 82
	}
	
	immediate = {
		set_country_flag = injured_swarm_queen
		random_system = {
			limit = {
				any_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
	
	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 = {
					type = admiral
					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_fleet_flag = pet_queen
		num_ships < 30
		any_ship = { 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_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_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
							portrait = random
							traits = random
							immortal = yes
						}
						create_country = {
							name = "NAME_Feral_Prethoryn" 
							type = "feral_prethoryn"
							species = last_created
							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_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_owned_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 = last_created
				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
planet_event = {
	id = crisis.200
	hide_window = yes
	
	is_triggered_only = yes
	
	trigger = {
		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 }
		planet_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
			purge = yes	
		}
	}
}

# Purge Complete
planet_event = {
	id = crisis.202
	hide_window = yes
	
	is_triggered_only = yes
	
	trigger = {
		num_pops = 1 # Last Pop is still alive when this event is fired
		controller = { is_country_type = "swarm" }
	}
	
	immediate = {
		if = {
			limit = {
				NOR = {
					is_planet_class = pc_ringworld_habitable
					is_planet_class = pc_habitat # habitats are currently uninfestable
				}
			}
			change_pc = pc_infested
			set_owner = controller
			else = {
				if = { # Habitable Ring Worlds can't be infested... they are destroyed instead
					limit = { is_planet_class = pc_ringworld_habitable }
					destroy_colony = yes
					change_pc = pc_ringworld_habitable_damaged
					reset_planet = yes
				}
				if = {
					limit = { is_planet_class = pc_habitat }
					remove_planet = yes
				}
			}
			planet_event = { id = crisis.215 }
		}
	}
}

# Planet Colonized
planet_event = {
	id = crisis.203
	hide_window = yes
	
	is_triggered_only = yes
	
	trigger = {
		owner = { is_country_type = "swarm" }
	}
	
	immediate = {
		every_owned_pop = { kill_pop = yes }
		if = {
			limit = {
				NOR = {
					is_planet_class = pc_ringworld_habitable
					is_planet_class = pc_habitat # habitats are currently uninfestable
				}
			}
			change_pc = pc_infested
			reset_planet = yes
			else = {
				if = { # Habitable Ring Worlds can't be infested... they are destroyed instead
					limit = { is_planet_class = pc_ringworld_habitable }
					change_pc = pc_ringworld_habitable_damaged
					reset_planet = yes
				}
				if = {
					limit = { is_planet_class = pc_habitat }
					remove_planet = yes
				}
			}
			planet_event = { id = crisis.215 }
		}
	}
}

# 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_owned_ships = {
				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_owned_ship = {
				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_owned_ships = {
				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_owned_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

	immediate = {
		if = {
			limit = {
				any_country = {
					is_country_type = swarm
					num_owned_planets > 0
					num_ships < 2000
					any_system_within_border = { exists = starbase }
				}
			}
			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 = {
				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_owned_ships = {
					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_owned_ships = {
					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 only 5 or less 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 < 2
					NOT = {
						any_system = {
							any_planet = {
								exists = controller
								controller = { is_country_type = swarm }
							}
						}
					}
					NOT = {
						any_system = {
							any_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
	}
	
	immediate = {
		stop_crisis_sound = yes
		end_crisis = yes
		remove_global_flag = ongoing_prethoryn_invasion
		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:sentinels }
			event_target: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_metropolis
	show_sound = event_celebration
	
	is_triggered_only = yes
	
	option = { # Spiritualist Response
		name = crisis.211.a
		trigger = {
			has_spiritualist_government = yes
			NOT = {
				has_ethic = "ethic_fanatic_xenophile"
				has_ethic = "ethic_fanatic_xenophobe"
			}
		}
		add_modifier = {
			modifier = "prethoryn_defeated"
			days = 3600
		}
		add_monthly_resource_mult = {
			resource = unity
			value = @tier5unityreward
		}
		end_event_chain = "prethoryn_scourge_chain"
	}
	option = { # Militarist Response
		name = crisis.211.b
		trigger = {
			has_militarist_government = yes
			NOT = {
				has_ethic = "ethic_fanatic_xenophile"
				has_ethic = "ethic_fanatic_xenophobe"
			}
		}
		add_modifier = {
			modifier = "prethoryn_defeated"
			days = 3600
		}
		add_monthly_resource_mult = {
			resource = unity
			value = @tier5unityreward
		}
		end_event_chain = "prethoryn_scourge_chain"
	}
	option = { # Materialist Response
		name = crisis.211.c
		trigger = {
			has_materialist_government = yes
			NOT = {
				has_ethic = "ethic_fanatic_xenophile"
				has_ethic = "ethic_fanatic_xenophobe"
			}
		}
		add_modifier = {
			modifier = "prethoryn_defeated"
			days = 3600
		}
		add_monthly_resource_mult = {
			resource = unity
			value = @tier5unityreward
		}
		end_event_chain = "prethoryn_scourge_chain"
	}
	option = { # Pacifist Response
		name = crisis.211.d
		trigger = {
			has_pacifist_government = yes
			NOT = {
				has_ethic = "ethic_fanatic_xenophile"
				has_ethic = "ethic_fanatic_xenophobe"
			}
		}
		add_modifier = {
			modifier = "prethoryn_defeated"
			days = 3600
		}
		add_monthly_resource_mult = {
			resource = unity
			value = @tier5unityreward
		}
		end_event_chain = "prethoryn_scourge_chain"
	}
	option = { # Corporate Response
		name = crisis.211.e
		trigger = {
			has_government = gov_megacorporation
			NOT = {
				has_ethic = "ethic_fanatic_xenophile"
				has_ethic = "ethic_fanatic_xenophobe"
			}
		}
		add_modifier = {
			modifier = "prethoryn_defeated"
			days = 3600
		}
		add_monthly_resource_mult = {
			resource = unity
			value = @tier5unityreward
		}
		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_ethic = ethic_gestalt_consciousness
			}
			NOT = {
				has_ethic = "ethic_fanatic_xenophile"
				has_ethic = "ethic_fanatic_xenophobe"
			}
		}
		add_modifier = {
			modifier = "prethoryn_defeated"
			days = 3600
		}
		add_monthly_resource_mult = {
			resource = unity
			value = @tier5unityreward
		}
		end_event_chain = "prethoryn_scourge_chain"
	}
	option = { # Xenophobe Response
		name = crisis.211.g
		trigger = {
			has_ethic = "ethic_fanatic_xenophobe"
		}
		add_modifier = {
			modifier = "prethoryn_defeated"
			days = 3600
		}
		add_monthly_resource_mult = {
			resource = unity
			value = @tier5unityreward
		}
		end_event_chain = "prethoryn_scourge_chain"
	}
	option = { # Xenophile Response
		name = crisis.211.h
		trigger = {
			has_ethic = "ethic_fanatic_xenophile"
		}
		add_modifier = {
			modifier = "prethoryn_defeated"
			days = 3600
		}
		add_monthly_resource_mult = {
			resource = unity
			value = @tier5unityreward
		}
		end_event_chain = "prethoryn_scourge_chain"
	}
}

# World Infested (HIDDEN)
planet_event = {
	id = crisis.215
	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 
			}
		}
	}
}

# Infested World Cleansed (HIDDEN)
planet_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 = {
			purge = no
		}
	}
}

# 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.15
	}
	
	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_01
		set_crisis_stage_4 = yes
	}
}

country_event = {
	id = crisis.234
	title = "crisis.234.name"
	desc = "crisis.234.desc"
	picture = GFX_evt_physics_research
	show_sound = event_alien_nature	
	
	trigger = {
		is_country_type = default
		has_crisis = no
		owner_species = { has_trait = trait_psionic }
		any_owned_leader = {
			has_leader_flag = captured_queen
		}		
		NOT = { has_country_flag = crisis.234 }
	}
	
	mean_time_to_happen = {
		years = 10
	}	
	
	option = {
		name = crisis.234.a
		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 
						}
					}
				}
			}
		}
	}
}
