################################
#
# Nemesis Crisis Endgame Events
#
# Written by Henrik Thyrwall
#
################################

namespace = crisis

# The Aetherophasic Engine
country_event = {
	id = crisis.6000
	title = "crisis.6000.name"
	desc = {
		trigger = {
			is_nomadic = yes
			is_gestalt = no
			owner_species = { is_psionic_species = no }
		}
		text = "crisis.6000.nomad.a.desc"
	}
	desc = {
		trigger = {
			is_nomadic = no
			is_gestalt = no
			owner_species = { is_psionic_species = no }
		}
		text = "crisis.6000.a.desc"
	}
	desc = {
		trigger = {
			is_nomadic = yes
			owner_species = { is_psionic_species = yes }
			is_robot_empire = no
			is_gestalt = no
		}
		text = "crisis.6000.nomad.b.desc.organic_non_gestalt"
	}
	desc = {
		trigger = {
			is_nomadic = no
			owner_species = { is_psionic_species = yes }
			is_robot_empire = no
			is_gestalt = no
		}
		text = "crisis.6000.b.desc.organic_non_gestalt"
	}
	desc = {
		trigger = {
			is_nomadic = yes
			owner_species = { is_psionic_species = yes }
			is_robot_empire = no
			is_gestalt = yes
		}
		text = "crisis.6000.nomad.b.desc.organic_gestalt"
	}
	desc = {
		trigger = {
			is_nomadic = no
			owner_species = { is_psionic_species = yes }
			is_robot_empire = no
			is_gestalt = yes
		}
		text = "crisis.6000.b.desc.organic_gestalt"
	}
	desc = {
		trigger = {
			is_nomadic = yes
			owner_species = { is_psionic_species = yes }
			is_robot_empire = yes
		}
		text = "crisis.6000.nomad.b.desc.machine"
	}
	desc = {
		trigger = {
			is_nomadic = no
			owner_species = { is_psionic_species = yes }
			is_robot_empire = yes
		}
		text = "crisis.6000.b.desc.machine"
	}
	desc = {
		trigger = {
			is_nomadic = yes
			is_machine_empire = yes
			NOT = { has_origin = origin_shroud_forged }
		}
		text = "crisis.6000.nomad.c.desc"
	}
	desc = {
		trigger = {
			is_nomadic = no
			is_machine_empire = yes
			NOT = { has_origin = origin_shroud_forged }
		}
		text = "crisis.6000.c.desc"
	}
	desc = {
		trigger = {
			is_nomadic = yes
			is_hive_empire = yes
		}
		text = "crisis.6000.nomad.d.desc"
	}
	desc = {
		trigger = {
			is_nomadic = no
			is_hive_empire = yes
		}
		text = "crisis.6000.d.desc"
	}
	desc = {
		trigger = {
			is_nomadic = yes
			is_machine_empire = yes
			has_origin = origin_shroud_forged
		}
		text = "crisis.6000.nomad.e.desc"
	}
	desc = {
		trigger = {
			is_nomadic = no
			is_machine_empire = yes
			has_origin = origin_shroud_forged
		}
		text = "crisis.6000.e.desc"
	}
	picture = GFX_evt_megastructure_construction
	show_sound = event_death_cult

	is_triggered_only = yes

	immediate = {
		set_country_flag = built_crisis_sphere
		if = {
			limit = { is_nomadic = yes }
			random_owned_nonprimary_starbase = {
				limit = {
					is_waystation_starbase = yes
					has_starbase_module = waystation_aetherophasic_anchor
				}
				solar_system = {
					set_star_flag = crisis_sphere_system@root
					set_star_flag = nomad_aetherophasic_anchor_system
					save_event_target_as = sphere_system
					spawn_megastructure = {
						type = "crisis_sphere_0"
						planet = star
						owner = root
						graphical_culture = root
						init_effect = {
							set_megastructure_flag = crisis_sphere_of@root
						}
					}
				}
			}
		}
		else = {
			solar_system = {
				set_star_flag = crisis_sphere_system@root
			}
			capital_scope = {
				solar_system = {
					save_event_target_as = sphere_system
					spawn_megastructure = {
						type = "crisis_sphere_0"
						planet = star
						owner = root
						graphical_culture = root
						init_effect = {
							set_megastructure_flag = crisis_sphere_of@root
						}
					}
				}
			}
		}
	}

	option = {
		name = crisis.6000.a
		trigger = {
			is_gestalt = no
			owner_species = { is_psionic_species = no }
		}
		custom_tooltip = crisis.6000.tooltip
		hidden_effect = {
			country_event = { id = crisis.6001 days = 5 }
		}
	}
	option = {
		name = crisis.6000.b
		trigger = { owner_species = { is_psionic_species = yes } }
		custom_tooltip = crisis.6000.tooltip
		hidden_effect = {
			country_event = { id = crisis.6001 days = 5 }
		}
	}
	option = {
		name = crisis.6000.c
		trigger = {
			is_machine_empire = yes
			NOT = { has_origin = origin_shroud_forged }
		}
		custom_tooltip = crisis.6000.tooltip
		hidden_effect = {
			country_event = { id = crisis.6001 days = 5 }
		}
	}
	option = {
		name = crisis.6000.d
		trigger = { is_hive_empire = yes }
		custom_tooltip = crisis.6000.tooltip
		hidden_effect = {
			country_event = { id = crisis.6001 days = 5 }
		}
	}
	option = {
		name = crisis.6000.e
		trigger = {
			is_machine_empire = yes
			has_origin = origin_shroud_forged
		}
		custom_tooltip = crisis.6000.tooltip
		hidden_effect = {
			country_event = { id = crisis.6001 days = 5 }
		}
	}
}

# The Star-Eaters
country_event = {
	id = crisis.6001
	title = "crisis.6001.name"
	desc = "crisis.6001.desc"
	picture = GFX_evt_binary_stars
	show_sound = event_default

	is_triggered_only = yes

	immediate = {
		set_country_flag = can_build_star_eaters
		add_modifier = { modifier = crisis_dark_matter_cap }
		# Make sure empire has all Star Eater techs
		hidden_effect = {
			if = {
				limit = { country_uses_bio_ships = no }
				give_tech_no_error_effect = { MESSAGE = no TECH = tech_destroyers }
				give_tech_no_error_effect = { MESSAGE = no TECH = tech_cruisers }
				give_tech_no_error_effect = { MESSAGE = no TECH = tech_battleships }
			}
			else = {
				give_all_biogenesis_techs_effect = yes
			}
			give_tech_no_error_effect = { MESSAGE = no TECH = tech_btc_1 }
			give_tech_no_error_effect = { MESSAGE = no TECH = tech_strike_craft_1 }
			give_tech_no_error_effect = { MESSAGE = no TECH = tech_strike_craft_2 }
			give_tech_no_error_effect = { MESSAGE = no TECH = tech_strike_craft_3 }
			give_tech_no_error_effect = { MESSAGE = no TECH = tech_lasers_2 }
			give_tech_no_error_effect = { MESSAGE = no TECH = tech_lasers_3 }
			give_tech_no_error_effect = { MESSAGE = no TECH = tech_lasers_4 }
			give_tech_no_error_effect = { MESSAGE = no TECH = tech_lasers_5 }
			give_tech_no_error_effect = { MESSAGE = no TECH = tech_shields_1 }
			give_tech_no_error_effect = { MESSAGE = no TECH = tech_shields_2 }
			give_tech_no_error_effect = { MESSAGE = no TECH = tech_shields_3 }
			give_tech_no_error_effect = { MESSAGE = no TECH = tech_shields_4 }
			give_tech_no_error_effect = { MESSAGE = no TECH = tech_shields_5 }
			give_tech_no_error_effect = { MESSAGE = no TECH = tech_shield_rechargers_1 }
			give_tech_no_error_effect = { MESSAGE = no TECH = tech_regenerative_hull_tissue }
			give_tech_no_error_effect = { MESSAGE = no TECH = tech_sensors_2 }
			give_tech_no_error_effect = { MESSAGE = no TECH = tech_sensors_3 }
			give_tech_no_error_effect = { MESSAGE = no TECH = tech_sensors_4 }
			give_tech_no_error_effect = { MESSAGE = no TECH = tech_fusion_power }
			give_tech_no_error_effect = { MESSAGE = no TECH = tech_cold_fusion_power }
			give_tech_no_error_effect = { MESSAGE = no TECH = tech_antimatter_power }
			give_tech_no_error_effect = { MESSAGE = no TECH = tech_zero_point_power }
			give_tech_no_error_effect = { MESSAGE = no TECH = tech_dark_matter_power_core }
			give_tech_no_error_effect = { MESSAGE = no TECH = tech_jump_drive_1 }
			give_tech_no_error_effect = { MESSAGE = no TECH = tech_administrative_ai }
			give_tech_no_error_effect = { MESSAGE = no TECH = tech_combat_computers_1 }
			give_tech_no_error_effect = { MESSAGE = no TECH = tech_self_aware_logic }
			give_tech_no_error_effect = { MESSAGE = no TECH = tech_combat_computers_2 }
			give_tech_no_error_effect = { MESSAGE = no TECH = tech_thrusters_2 }
			give_tech_no_error_effect = { MESSAGE = no TECH = tech_thrusters_3 }
			give_tech_no_error_effect = { MESSAGE = no TECH = tech_thrusters_4 }
			give_tech_no_error_effect = { MESSAGE = no TECH = tech_flak_batteries_2 }
			give_tech_no_error_effect = { MESSAGE = no TECH = tech_flak_batteries_3 }
			give_tech_no_error_effect = { MESSAGE = no TECH = tech_pd_tracking_2 }
			give_tech_no_error_effect = { MESSAGE = no TECH = tech_pd_tracking_3 }
			give_tech_no_error_effect = { MESSAGE = no TECH = tech_ship_armor_2 }
			give_tech_no_error_effect = { MESSAGE = no TECH = tech_ship_armor_3 }
			give_tech_no_error_effect = { MESSAGE = no TECH = tech_ship_armor_4 }
			give_tech_no_error_effect = { MESSAGE = no TECH = tech_ship_armor_5 }
		}
	}

	option = {
		name = crisis.6001.a
		custom_tooltip = crisis.6001.tooltip
		hidden_effect = {
			create_ship_design = { design = "NAME_Crisis_Star_Eater" }
			add_ship_design = last_created_design
			capital_scope = {
				create_fleet = {
					settings = {
						spawn_debris = no
						can_change_composition = no
						uses_naval_capacity = no
					}
					effect = {
						set_owner = root
						create_ship = {
							name = NAME_Crisis_Star_Eater_1
							design = "NAME_Crisis_Star_Eater"
						}
						set_location = {
							target = prev
							distance = 20
							angle = 0
						}
						set_fleet_stance = passive
						set_aggro_range_measure_from = self
						set_aggro_range = 350
					}
				}
				create_fleet = {
					settings = {
						spawn_debris = no
						can_change_composition = no
						uses_naval_capacity = no
					}
					effect = {
						set_owner = root
						create_ship = {
							name = NAME_Crisis_Star_Eater_2
							design = "NAME_Crisis_Star_Eater"
						}
						set_location = {
							target = prev
							distance = 20
							angle = 90
						}
						set_fleet_stance = passive
						set_aggro_range_measure_from = self
						set_aggro_range = 350
					}
				}
			}
		}
	}
}

# Engine - First Stage Complete
country_event = {
	id = crisis.6005
	title = "crisis.6005.name"
	desc = "crisis.6005.desc"
	picture = GFX_evt_surreal_visions
	show_sound = event_default

	is_triggered_only = yes

	immediate = {
		set_country_flag = building_crisis_sphere
		random_system = {
			limit = { has_star_flag = crisis_sphere_system@root }
			save_event_target_as = crisis_sphere_system
		}
		every_playable_country = {
			limit = {
				is_ai = no
				NOT = { is_same_value = root }
				OR = {
					owner_species = { is_psionic_species = yes }
					has_country_flag = built_crisis_sphere
				}
			}
			country_event = { id = crisis.6010 }
		}
	}

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

# Engine - Second Stage Complete
country_event = {
	id = crisis.6006
	title = "crisis.6005.name"
	desc = "crisis.6006.desc"
	picture = GFX_evt_psionics
	show_sound = event_default

	is_triggered_only = yes

	immediate = {
		set_country_flag = crisis_sphere_revealed
		random_system = {
			limit = { has_star_flag = crisis_sphere_system@root }
			save_event_target_as = crisis_sphere_system

			random_system_ambient_object = {
				limit = { has_ambient_object_flag = crisis_sphere_1_system_effect }
				destroy_ambient_object = this
			}
			star = {
				create_ambient_object = {
					type = "crisis_sphere_2"
					location = this
				}
				last_created_ambient_object = {
					set_ambient_object_flag = crisis_sphere_2_system_effect
					set_location = {
						target = prev
						distance = 0
						angle = random
					}
				}
			}
		}
		add_threat = { who = root amount = 1000 }
		every_playable_country = {
			limit = {
				NOT = { is_same_value = root }
			}
			establish_communications_no_message = root
			if = {
				limit = {
					has_opinion_modifier = {
						who = root
						modifier = opinion_destroying_systems
					}
				}
				remove_opinion_modifier = {
					who = root
					modifier = opinion_destroying_systems
				}
			}
			add_opinion_modifier = {
				who = root
				modifier = opinion_destroying_galaxy
			}
		}
		every_country = {
			limit = { is_fallen_empire = yes }
			add_opinion_modifier = {
				who = root
				modifier = opinion_destroying_galaxy
			}
		}
		every_playable_country = {
			limit = {
				is_ai = no
				NOT = { has_country_flag = built_crisis_sphere }
			}
			country_event = { id = crisis.6015 }
		}
	}

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

# Engine - Third Stage Complete
country_event = {
	id = crisis.6007
	title = "crisis.6005.name"
	desc = "crisis.6007.desc"
	picture = GFX_evt_space_monster
	show_sound = event_default

	is_triggered_only = yes

	immediate = {
		random_system = {
			limit = { has_star_flag = crisis_sphere_system@root }
			random_system_ambient_object = {
				limit = { has_ambient_object_flag = crisis_sphere_2_system_effect }
				destroy_ambient_object = this
			}
			star = {
				create_ambient_object = {
					type = "crisis_sphere_3"
					location = this
				}
				last_created_ambient_object = {
					set_ambient_object_flag = crisis_sphere_3_system_effect
					set_location = {
						target = prev
						distance = 0
						angle = random
					}
				}
			}
		}
	}

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

# Psionic Alert
country_event = {
	id = crisis.6010
	title = "crisis.6010.name"
	desc = {
		trigger = {
			NOR = {
				has_country_flag = crisis_psionic_alert
				has_country_flag = built_crisis_sphere
			}
			is_gestalt = no
		}
		text = "crisis.6010.a.desc.non_gestalt"
	}
	desc = {
		trigger = {
			NOR = {
				has_country_flag = crisis_psionic_alert
				has_country_flag = built_crisis_sphere
			}
			is_gestalt = yes
		}
		text = "crisis.6010.a.desc.gestalt"
	}
	desc = {
		trigger = { has_country_flag = crisis_psionic_alert }
		text = "crisis.6010.b.desc"
	}
	desc = {
		trigger = { has_country_flag = built_crisis_sphere }
		text = "crisis.6010.c.desc"
	}
	picture = GFX_evt_surreal_visions
	show_sound = event_default

	is_triggered_only = yes

	after = {
		if = {
			limit = {
				NOR = {
					has_country_flag = crisis_psionic_alert
					has_country_flag = built_crisis_sphere
				}
			}
			set_country_flag = crisis_psionic_alert
		}
	}

	option = {
		name = {
			trigger = {
				if = {
					limit = { is_robot_empire = yes }
					text = crisis.6010.a.machine
				}
				else = {
					text = crisis.6010.a.organic
				}
			}
		}
	}
}

# General Alert
country_event = {
	id = crisis.6015
	title = "crisis.6005.name"
	desc = {
		trigger = {
			owner_species = { is_psionic_species = no }
			NOT = { has_country_flag = crisis_general_alert }
		}
		text = "crisis.6015.a.desc"
	}
	desc = {
		trigger = {
			owner_species = { is_psionic_species = yes }
			NOT = { has_country_flag = crisis_general_alert }
		}
		text = "crisis.6015.b.desc"
	}
	desc = {
		trigger = { has_country_flag = crisis_general_alert }
		text = "crisis.6015.c.desc"
	}
	picture = GFX_evt_space_monster
	show_sound = event_default

	is_triggered_only = yes

	after = {
		if = {
			limit = {
				NOT = { has_country_flag = crisis_general_alert }
			}
			set_country_flag = crisis_general_alert
		}
	}

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

# Ascension!
country_event = {
	id = crisis.6050
	title = "crisis.6050.name"
	desc = {
		trigger = { is_gestalt = no }
		text = "crisis.6050.a.desc"
	}
	desc = {
		trigger = {
			is_gestalt = yes
			NOT = { has_origin = origin_shroud_forged }
		}
		text = "crisis.6050.b.desc"
	}
	desc = {
		trigger = {
			is_gestalt = yes
			has_origin = origin_shroud_forged
		}
		text = "crisis.6050.c.desc"
	}
	picture = GFX_evt_vortex
	show_sound = event_default

	is_triggered_only = yes

	immediate = {
		add_victory_score = {
			source = destroyed_galaxy_score
			score = 500000
		}
		hidden_effect = {
			country_event = { id = timeline.34 } # Milestone Destroyed Galaxy
		}
	}

	option = {
		name = crisis.6050.a
		custom_tooltip = crisis.6050.a.tooltip
		hidden_effect = {
			set_country_flag = big_red_button_achievement
			country_event = { id = crisis.6052 days = 1 } # win delay to get cheevo
		}
		tooltip = { win = yes }
	}
}

# Game Over
country_event = {
	id = crisis.6051
	title = "crisis.6051.name"
	desc = "crisis.6051.desc"
	picture = GFX_evt_supernova
	show_sound = event_default

	is_triggered_only = yes

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

# Crisis Wins
country_event = {
	id = crisis.6052
	hide_window = yes

	is_triggered_only = yes

	immediate = {
		every_playable_country = {
			limit = {
				is_ai = no
				NOT = { is_same_value = root }
			}
			country_event = { id = crisis.6051 }
		}
		set_update_modifiers_batch = begin
		every_system = { destroy_star_system = yes }
		set_update_modifiers_batch = end
		every_country = {
			limit = {
				NOT = {
					is_country_type = default
				}
			}
			destroy_country = yes
		}
		win = yes
	}
}

# Standard Crisis Destroys Megastructure
starbase_event = {
	id = crisis.6099
	hide_window = yes

	is_triggered_only = yes

	trigger = {
		exists = from
		from.owner = {
			OR = {
				is_country_type = swarm
				is_country_type = extradimensional
				is_country_type = extradimensional_2
				is_country_type = extradimensional_3
				is_country_type = ai_empire
				is_country_type = gray_goo
			}
		}
		solar_system = {
			has_star_flag = crisis_sphere_system@root.owner
			any_system_megastructure = { has_megastructure_flag = crisis_sphere }
		}
	}

	immediate = {
		solar_system = {
			system_event = { id = crisis.6100 scopes = { from = root.owner } }
		}
	}
}


# Crisis Megastructure Destroyed (HIDDEN)
system_event = {
	id = crisis.6100
	hide_window = yes

	is_triggered_only = yes

	trigger = {
		any_system_megastructure = {
			has_megastructure_flag = crisis_sphere
			OR = {
				NOT = { exists = fromfrom }
				has_megastructure_flag = crisis_sphere_of@fromfrom
			}
		}
	}

	immediate = {
		save_event_target_as = crisis_sphere_system
		random_system_megastructure = {
			limit = { has_megastructure_flag = crisis_sphere }
			prev = {
				spawn_megastructure = {
					type = crisis_sphere_ruined
					coords_from = prev
					init_effect = {
						if = {
							limit = { exists = fromfrom }
							set_megastructure_flag = crisis_sphere_of@fromfrom
						}
					}
				}
			}
			switch = {
				trigger = is_megastructure_type
				crisis_sphere_0 = {
					from = {
						set_variable = {
							which = crisis_sphere_destroy_reward
							value = 1
						}
					}
				}
				crisis_sphere_1 = {
					from = {
						set_variable = {
							which = crisis_sphere_destroy_reward
							value = 1.5
						}
					}
				}
				crisis_sphere_2 = {
					from = {
						set_variable = {
							which = crisis_sphere_destroy_reward
							value = 2
						}
					}
				}
				crisis_sphere_3 = {
					from = {
						set_variable = {
							which = crisis_sphere_destroy_reward
							value = 3
						}
					}
				}
			}
			remove_megastructure = this
		}
		random_system_ambient_object = {
			limit = {
				OR = {
					has_ambient_object_flag = crisis_sphere_1_system_effect
					has_ambient_object_flag = crisis_sphere_2_system_effect
					has_ambient_object_flag = crisis_sphere_3_system_effect
				}
			}
			destroy_ambient_object = this
		}
		from = { save_event_target_as = sphere_destroyers }
		fromfrom = { save_event_target_as = sphere_builders }
		from = { country_event = { id = crisis.6102 } }
		fromfrom = { country_event = { id = crisis.6101 } }
		every_playable_country = {
			limit = {
				is_ai = no
				NOR = {
					is_same_value = from
					is_same_value = fromfrom
				}
			}
			country_event = { id = crisis.6103 }
		}
	}
}

# Crisis Megastructure Destroyed (Owner)
country_event = {
	id = crisis.6101
	title = "crisis.6101.name"
	desc = "crisis.6101.desc"
	picture = GFX_evt_ruined_system
	show_sound = event_default

	is_triggered_only = yes

	immediate = {
		remove_country_flag = building_crisis_sphere
	}

	option = {
		name = crisis.6101.a
		custom_tooltip = crisis.6101.a.tooltip
	}
}

# Crisis Megastructure Destroyed (Destroyer)
country_event = {
	id = crisis.6102
	title = "crisis.6101.name"
	desc = {
		trigger = {
			NOT = { has_country_flag = built_crisis_sphere }
		}
		text = "crisis.6102.a.desc"
	}
	desc = {
		trigger = { has_country_flag = built_crisis_sphere }
		text = "crisis.6102.b.desc"
	}
	picture = GFX_evt_crisis_defeated
	show_sound = event_default

	is_triggered_only = yes

	option = {
		name = crisis.6102.a
		trigger = {
			NOT = { has_country_flag = built_crisis_sphere }
		}
		add_resource = {
			influence = 200
			alloys = 5000
			mult = crisis_sphere_destroy_reward
		}
		custom_tooltip = aetherophasic_engine_ruined_tt
	}
	option = {
		name = crisis.6102.b
		trigger = { has_country_flag = built_crisis_sphere }
		add_resource = {
			sr_dark_matter = 10000
			mult = crisis_sphere_destroy_reward
		}
		custom_tooltip = aetherophasic_engine_ruined_tt
	}
	after = {
		hidden_effect = {
			clear_variable = crisis_sphere_destroy_reward
		}
	}
}
# Crisis Megastructure Destroyed (Others)
country_event = {
	id = crisis.6103
	title = "crisis.6101.name"
	desc = "crisis.6103.desc"
	picture = GFX_evt_crisis_defeated
	show_sound = event_default

	is_triggered_only = yes

	option = {
		name = GOOD
		trigger = {
			NOT = { has_country_flag = built_crisis_sphere }
		}
	}
	option = {
		name = crisis.6103.b
		trigger = { has_country_flag = built_crisis_sphere }
	}
}

# Shroud Entities Spawn (HIDDEN)
country_event = {
	id = crisis.6150
	hide_window = yes

	is_triggered_only = yes

	trigger = {
		owns_crisis_sphere_system = yes
	}

	immediate = {
		every_playable_country = {
			limit = { is_ai = no }
			country_event = { id = crisis.6151 }
		}
		every_system = {
			random_list = {
				95 = {}
				5 = {
					if = {
						limit = { NOT = { exists = event_target:shroud_country } }
						create_country = {
							name = "NAME_Creatures_of_the_Shroud"
							type = shroud_spirits
							flag = {
								icon = {
									category = "special"
									file = "the_shroud.dds"
								}
								background= {
									category = "backgrounds"
									file = "00_solid.dds"
								}
								colors = {
									"dark_purple"
									"black"
									"null"
									"null"
								}
							}
						}
						last_created_country = {
							save_global_event_target_as = shroud_country
						}
					}
					create_fleet = {
						name = "NAME_Psionic_Entity"
						settings = {
							spawn_debris = no
							is_boss = yes
						}
						effect = {
							set_owner = event_target:shroud_country
							set_fleet_flag = crisis_shroud_entity
							create_ship = {
								name = "NAME_Psionic_Entity"
								design = "NAME_Corrupted_Avatar"
							}
							set_location = {
								target = prev.star
								distance = 100
								angle = random
							}
							set_fleet_stance = aggressive
							set_aggro_range_measure_from = return_point
							set_aggro_range = 2000
						}
					}
					star = {
						create_ambient_object = {
							type = "psi_storm_1"
							location = this
						}
						last_created_ambient_object = {
							set_ambient_object_flag = psi_storm_object
							set_location = {
								target = prev
								distance = 0
								angle = random
							}
						}
					}
				}
			}
		}
		every_playable_country = {
			establish_communications_no_message = event_target:shroud_country
		}
	}
}

# Shroud Entities Spawn
country_event = {
	id = crisis.6151
	title = "crisis.6151.name"
	desc = "crisis.6151.desc"
	picture = GFX_evt_surreal_visions
	show_sound = event_default

	is_triggered_only = yes

	trigger = { is_ai = no }

	option = {
		name = crisis.6151.a
		trigger = {
			NOT = { is_same_value = from }
		}
	}
	option = {
		name = crisis.6151.b
		trigger = { is_same_value = from }
	}
}

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

	is_triggered_only = yes

	trigger = {
		is_same_value = event_target:shroud_country
		fromfrom = { has_fleet_flag = crisis_shroud_entity }
	}

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

# Second Wave (HIDDEN)
country_event = {
	id = crisis.6160
	hide_window = yes

	is_triggered_only = yes

	trigger = {
		owns_crisis_sphere_system = yes
	}

	immediate = {
		every_playable_country = {
			limit = { is_ai = no }
			country_event = { id = crisis.6161 }
		}
		every_system = {
			random_list = {
				95 = {}
				10 = {
					if = {
						limit = { NOT = { exists = event_target:shroud_country } }
						create_country = {
							name = "NAME_Creatures_of_the_Shroud"
							type = shroud_spirits
							flag = {
								icon = {
									category = "special"
									file = "the_shroud.dds"
								}
								background= {
									category = "backgrounds"
									file = "00_solid.dds"
								}
								colors = {
									"dark_purple"
									"black"
									"null"
									"null"
								}
							}
						}
						last_created_country = {
							save_global_event_target_as = shroud_country
						}
					}
					create_fleet = {
						name = "NAME_Psionic_Entity"
						settings = {
							spawn_debris = no
							is_boss = yes
						}
						effect = {
							set_owner = event_target:shroud_country
							create_ship = {
								name = "NAME_Psionic_Entity"
								design = "NAME_Corrupted_Avatar"
							}
							set_location = {
								target = prev.star
								distance = 100
								angle = random
							}
							set_fleet_stance = aggressive
							set_aggro_range_measure_from = return_point
							set_aggro_range = 2000
						}
					}
				}
			}
		}
		every_playable_country = {
			establish_communications_no_message = event_target:shroud_country
		}
	}
}

# Second Wave
country_event = {
	id = crisis.6161
	title = "crisis.6161.name"
	desc = {
		trigger = {
			NOT = { is_same_value = from }
		}
		text = "crisis.6161.a.desc"
	}
	desc = {
		trigger = { is_same_value = from }
		text = "crisis.6161.b.desc"
	}
	picture = GFX_evt_surreal_visions
	show_sound = event_default

	is_triggered_only = yes

	trigger = { is_ai = no }

	option = {
		name = crisis.6161.a
		trigger = {
			NOT = { is_same_value = from }
		}
	}
	option = {
		name = crisis.6161.b
		trigger = { is_same_value = from }
	}
}

# Psionic Pulses (HIDDEN)
country_event = {
	id = crisis.6170
	hide_window = yes

	is_triggered_only = yes

	trigger = {
		owns_crisis_sphere_system = yes
	}

	immediate = {
		every_playable_country = {
			limit = {
				any_owned_pop_group = { is_psionic_species = yes }
			}
			country_event = { id = crisis.6171 }
		}
	}
}

# Psionic Pulses
country_event = {
	id = crisis.6171
	title = "crisis.6171.name"
	desc = {
		trigger = {
			NOT = { is_same_value = from }
			is_robot_empire = no
			is_gestalt = no
		}
		text = "crisis.6171.a.desc.organic_non_gestalt"
	}
	desc = {
		trigger = {
			NOT = { is_same_value = from }
			is_robot_empire = yes
			is_gestalt = no
		}
		text = "crisis.6171.a.desc.machine_non_gestalt"
	}
	desc = {
		trigger = {
			NOT = { is_same_value = from }
			is_gestalt = yes
		}
		text = "crisis.6171.a.desc.gestalt"
	}
	desc = {
		trigger = {
			is_same_value = from
			is_robot_empire = no
			is_gestalt = no
		}
		text = "crisis.6171.b.desc.organic_non_gestalt"
	}
	desc = {
		trigger = {
			is_same_value = from
			is_robot_empire = yes
			is_gestalt = no
		}
		text = "crisis.6171.b.desc.machine_non_gestalt"
	}
	desc = {
		trigger = {
			is_same_value = from
			is_gestalt = yes
		}
		text = "crisis.6171.b.desc.gestalt"
	}
	picture = GFX_evt_psionics
	show_sound = event_default

	is_triggered_only = yes

	trigger = { is_ai = no }

	option = {
		name = {
			trigger = {
				if = {
					limit = {
						is_robot_empire = no
						is_gestalt = no
					}
					text = crisis.6171.a.organic_non_gestalt
				}
				else = {
					text = crisis.6171.a.machine_or_gestalt
				}
			}
		}
		trigger = {
			NOT = { is_same_value = from }
		}

		# While loop messes up tooltip so doing it old school
		random_owned_pop_group = {
			limit = { is_psionic_species = yes }
			kill_single_pop = yes
		}
		random_owned_pop_group = {
			limit = { is_psionic_species = yes }
			kill_single_pop = yes
		}
		random_owned_pop_group = {
			limit = { is_psionic_species = yes }
			kill_single_pop = yes
		}
		random_owned_pop_group = {
			limit = { is_psionic_species = yes }
			kill_single_pop = yes
		}
		random_owned_pop_group = {
			limit = { is_psionic_species = yes }
			kill_single_pop = yes
		}
		random_owned_pop_group = {
			limit = { is_psionic_species = yes }
			kill_single_pop = yes
		}
		random_owned_leader = {
			limit = {
				has_trait = leader_trait_psionic
				NOT = { has_leader_flag = leader_death_events_blocked }
			}
			kill_leader = { show_notification = no }
		}
		random_owned_leader = {
			limit = {
				has_trait = leader_trait_psionic
				NOT = { has_leader_flag = leader_death_events_blocked }
			}
			kill_leader = { show_notification = no }
		}
	}
	option = {
		name = GOOD
		trigger = { is_same_value = from }
	}
}

# First Star Destroyed (No Intel)
country_event = {
	id = crisis.6180
	title = "crisis.6180.name"
	desc = "crisis.6180.desc"
	picture = GFX_evt_supernova
	show_sound = event_super_explosion
	location = event_target:destroyed_system

	is_triggered_only = yes

	immediate = {
		set_country_flag = star_eater_alert@event_target:crisis_country
	}

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

# First Star Destroyed (Some Intel)
country_event = {
	id = crisis.6181
	title = "crisis.6181.name"
	desc = "crisis.6181.desc"
	picture = GFX_evt_supernova
	show_sound = event_super_explosion
	location = event_target:destroyed_system

	is_triggered_only = yes

	immediate = {
		set_country_flag = star_eater_alert@event_target:crisis_country
	}

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

# First Star Destroyed (Also Crisis)
country_event = {
	id = crisis.6182
	title = "crisis.6182.name"
	desc = "crisis.6182.desc"
	picture = GFX_evt_supernova
	show_sound = event_super_explosion
	location = event_target:destroyed_system

	is_triggered_only = yes

	immediate = {
		set_country_flag = star_eater_alert@event_target:crisis_country
	}

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

# Aetherophasic Engine Captured
country_event = {
	id = crisis.6200
	title = crisis.6200.name
	desc = crisis.6200.desc
	show_sound = event_super_explosion
	picture = GFX_evt_supernova
	location = from

	trigger = {
		from = { has_megastructure_flag = crisis_sphere }
		if = {
			limit = { has_crisis_level = crisis_level_5 }
			NOT = { from = { has_megastructure_flag = crisis_sphere_of@root } }
		}
	}

	abort_trigger = {
		OR = {
			NOT = { exists = from }
			NOT = { exists = from.owner }
			from.owner = { NOT = { is_same_value = root } }
		}
	}

	is_triggered_only = yes

	immediate = {
		from = {
			solar_system = {
				save_event_target_as = crisis_sphere_system
			}
			switch = {
				trigger = is_megastructure_type
				crisis_sphere_0 = {
					root = {
						set_variable = {
							which = crisis_sphere_destroy_reward
							value = 1
						}
					}
				}
				crisis_sphere_1 = {
					root = {
						set_variable = {
							which = crisis_sphere_destroy_reward
							value = 1.5
						}
					}
				}
				crisis_sphere_2 = {
					root = {
						set_variable = {
							which = crisis_sphere_destroy_reward
							value = 2
						}
					}
				}
				crisis_sphere_3 = {
					root = {
						set_variable = {
							which = crisis_sphere_destroy_reward
							value = 3
						}
					}
				}
			}
			set_halted = -1 #no completing while still deciding!
		}
		if = {
			limit = {
				exists = fromfrom
				fromfrom = { has_megastructure_flag = crisis_sphere_of@prev }
			}
			fromfrom = {
				save_event_target_as = sphere_builders
			}
		}
		else = { #fromfrom is not reliable
			random_country = {
				limit = {
					from = { has_megastructure_flag = crisis_sphere_of@prev }
				}
				save_event_target_as = sphere_builders
			}
		}
	}

	#It must be destroyed
	option = {
		name = crisis.6200.A

		if = {
			limit = {
				has_crisis_level = crisis_level_5
			}
			add_resource = {
				sr_dark_matter = 10000
				mult = crisis_sphere_destroy_reward
			}
		}
		else = {
			add_resource = {
				influence = 200
				alloys = 5000
				mult = crisis_sphere_destroy_reward
			}
		}
		custom_tooltip = aetherophasic_engine_ruined_tt

		hidden_effect = {
			from = {
				solar_system = {
					spawn_megastructure = {
						type = crisis_sphere_ruined
						coords_from = prev
						init_effect = {
							if = {
								limit = { exists = event_target:crisis_sphere_owner }
								set_megastructure_flag = crisis_sphere_of@event_target:crisis_sphere_owner
							}
						}
					}
					random_system_ambient_object = {
						limit = {
							OR = {
								has_ambient_object_flag = crisis_sphere_1_system_effect
								has_ambient_object_flag = crisis_sphere_2_system_effect
								has_ambient_object_flag = crisis_sphere_3_system_effect
							}
						}
						destroy_ambient_object = this
					}
				}
				remove_megastructure = this
			}
			if = {
				limit = { exists = event_target:sphere_builders }
				event_target:sphere_builders = { country_event = { id = crisis.6101 } }
			}
			every_playable_country = {
				limit = {
					is_ai = no
					NOR = {
						is_same_value = root
						AND = {
							exists = event_target:sphere_builders
							is_same_value = event_target:sphere_builders
						}
					}
				}
				country_event = { id = crisis.6103 }
			}
		}

		ai_chance = {
			factor = 1
		}
	}

	#Option to replace your own one with it if you have lost yours
	option = {
		name = crisis.6200.B
		custom_tooltip = crisis.6200.B.tooltip

		trigger = {
			has_crisis_level = crisis_level_5
			NOT = {
				any_owned_megastructure = {
					OR = {
						has_megastructure_flag = crisis_sphere
						AND = {
							is_megastructure_type = crisis_sphere_ruined
							is_upgrading = yes
						}
					}
				}
			}
		}
		hidden_effect = {
			random_megastructure = {
				limit = { has_megastructure_flag = crisis_sphere_of@root }
				remove_megastructure_flag = crisis_sphere_of@root
			}
			random_system = {
				limit = { has_star_flag = crisis_sphere_system@root }
				remove_star_flag = crisis_sphere_system@root
			}
			from = {
				set_megastructure_flag = crisis_sphere_of@root
				set_halted = 0
				solar_system = {
					set_star_flag = crisis_sphere_system@root
				}
			}
		}

		ai_chance = {
			factor = 1000
		}
	}
}

# Nomad Crisis - Anchor Waystation Disabled (HIDDEN)
# Mirrors the on_system_occupied path for regular empires.
# When the anchor Waystation is disabled, the crisis sphere is destroyed.
starbase_event = {
	id = crisis.6205
	hide_window = yes

	is_triggered_only = yes

	trigger = {
		exists = from
		is_waystation_starbase = yes
		solar_system = {
			has_star_flag = nomad_aetherophasic_anchor_system
			any_system_megastructure = {
				has_megastructure_flag = crisis_sphere
				has_megastructure_flag = crisis_sphere_of@root.owner
			}
		}
	}

	immediate = {
		owner = { save_event_target_as = nomad_crisis }
		from = { owner = { save_event_target_as = anchor_attackers } }
		solar_system = {
			system_event = {
				id = crisis.6100
				scopes = {
					from = event_target:anchor_attackers
					fromfrom = event_target:nomad_crisis
				}
			}
		}
	}
}
