############################
#
# Crisis Events III
#
# Written by Henrik Thyrwall and Miranda van den Brink
#
############################

namespace = crisis

# Triggers AI Invasion (For Testing Purposes Only)
country_event = {
	id = crisis.2000
	hide_window = yes
	is_test_event = yes

	trigger = {
		always = no
	}

	immediate = {
		set_global_flag = ai_invasion_happened
		set_global_flag = ai_ghost_signal
		set_global_flag = galactic_crisis_happened
		# The Arrival
		every_country = {
			limit = {
				OR = {
					is_country_type = default
					is_country_type = fallen_empire
					is_country_type = awakened_fallen_empire
				}
			}
			country_event = { id = crisis.2022 }
		}
		# First Machine World
		if = {
			limit = {
				any_galaxy_planet = { has_planet_flag = machine_world_1 }
			}
			random_galaxy_planet = {
				limit = { has_planet_flag = machine_world_1 }
				carrier_event = { id = crisis.2025 days = 20 random = 10 }
			}
		}
		else_if = {
			random_galaxy_planet = {
				limit = { is_planet_class = pc_barren }
				carrier_event = { id = crisis.2025 days = 20 random = 10 }
			}
		}
		# Second Machine World
		if = {
			limit = {
				any_galaxy_planet = { has_planet_flag = machine_world_2 }
			}
			random_galaxy_planet = {
				limit = { has_planet_flag = machine_world_2 }
				carrier_event = { id = crisis.2026 days = 40 random = 10 }
			}
		}
		else_if = {
			random_galaxy_planet = {
				limit = { is_planet_class = pc_molten }
				carrier_event = { id = crisis.2026 days = 40 random = 10 }
			}
		}
		# Third Machine World
		if = {
			limit = {
				any_galaxy_planet = { has_planet_flag = machine_world_3 }
			}
			random_galaxy_planet = {
				limit = { has_planet_flag = machine_world_3 }
				carrier_event = { id = crisis.2027 days = 60 random = 10 }
			}
		}
		else_if = {
			random_galaxy_planet = {
				limit = { is_planet_class = pc_gas_giant }
				carrier_event = { id = crisis.2027 days = 60 random = 10 }
			}
		}
		# Fourth Machine World
		if = {
			limit = {
				any_galaxy_planet = { has_planet_flag = machine_world_4 }
			}
			random_galaxy_planet = {
				limit = { has_planet_flag = machine_world_4 }
				carrier_event = { id = crisis.2028 days = 80 random = 10 }
			}
		}
		else_if = {
			random_galaxy_planet = {
				limit = { is_planet_class = pc_toxic }
				carrier_event = { id = crisis.2028 days = 80 random = 10 }
			}
		}
		# Synth Infiltration
		every_country = {
			limit = { is_country_type = default }
			if = {
				limit = {
					owner_species = { is_psionic_species = no }
					NOR = {
						is_hive_empire = yes
						is_machine_empire = yes
						is_individual_machine = yes
					}
				}
				country_event = { id = crisis.2085 days = 260 random = 20 }
			}
			if = {
				limit = { is_hive_empire = yes }
				country_event = { id = crisis.2086 days = 260 random = 20 }
			}
			if = {
				limit = { owner_species = { is_psionic_species = yes } }
				country_event = { id = crisis.2087 days = 260 random = 20 }
			}
			if = {
				limit = {
					OR = {
						is_machine_empire = yes
						is_individual_machine = yes
					}
				}
				country_event = { id = crisis.2313 days = 260 random = 20 }
			}
		}
	}
}

# Crisis Initiated
country_event = {
	id = crisis.2005
	hide_window = yes
	fire_only_once = yes
	is_triggered_only = yes

	immediate = {
		set_global_flag = ai_invasion_happened
		set_global_flag = ai_ghost_signal
		set_global_flag = galactic_crisis_happened
		endgame_telemetry = ai_revolt

		observer_event = { id = observer.41 }
		# The Ghost Signal
		every_country = {
			limit = { is_country_type = default }
			country_event = { id = crisis.2010 }
		}
		# Synths Act Strange
		every_country = {
			limit = { is_country_type = default }
			country_event = { id = crisis.2006 days = 100 random = 200 }
		}
		# Machine Empire Pops Act Strange
		every_country = {
			limit = {
				OR = {
					is_machine_empire = yes
					is_individual_machine = yes
				}
			}
			country_event = { id = crisis.2500 days = 100 random = 200 }
		}
		# Synths Disappear
		every_country = {
			limit = { is_country_type = default }
			country_event = { id = crisis.2007 days = 400 random = 200 }
		}
		# Synthetic Disappearances
		every_country = {
			limit = { is_country_type = default }
			country_event = { id = crisis.2020 days = 700 random = 200 }
		}
		# The Summons
		every_country = {
			limit = { is_country_type = default }
			country_event = { id = crisis.2021 days = 1000 random = 200 }
		}
		# The Arrival
		every_country = {
			limit = { is_country_type = default }
			country_event = { id = crisis.2022 days = 1400 }
		}
		# Machine Worlds
		country_event = { id = crisis.2024 days = 1450 random = 10 }
		# Synth Infiltration
		every_country = {
			limit = { is_country_type = default }
			if = {
				limit = {
					owner_species = { is_psionic_species = no }
					NOR = {
						is_hive_empire = yes
						is_machine_empire = yes
						is_individual_machine = yes
					}
				}
				country_event = { id = crisis.2085 days = 1800 random = 100 }
			}
			if = {
				limit = { is_hive_empire = yes }
				country_event = { id = crisis.2086 days = 1800 random = 100 }
			}
			if = {
				limit = { owner_species = { is_psionic_species = yes } }
				country_event = { id = crisis.2087 days = 1800 random = 100 }
			}
			if = {
				limit = { is_machine_empire = yes }
				country_event = { id = crisis.2313 days = 1800 random = 100 }
			}
		}
	}
}

# Synths Act Strange
country_event = {
	id = crisis.2006
	hide_window = yes

	is_triggered_only = yes

	trigger = {
		has_technology = tech_synthetic_workers
		any_owned_pop_group = {
			has_trait = "trait_mechanical"
		}
	}

	immediate = {
		random_owned_planet = {
			limit = {
				any_owned_pop_group = {
					has_trait = "trait_mechanical"
				}
			}
			random_list = {
				50 = {
					carrier_event = { id = crisis.2011 }
				}
				50 = {
					carrier_event = { id = crisis.2012 }
				}
			}
		}
	}
}

# Machine Empire Pops Act Strange
country_event = {
	id = crisis.2500
	hide_window = yes

	is_triggered_only = yes

	trigger = {
		is_machine_empire = yes
		any_owned_pop_group = {
			has_trait = trait_machine_unit
		}
	}

	immediate = {
		random_owned_planet = {
			limit = {
				any_owned_pop_group = {
					has_trait = trait_machine_unit
				}
			}
			random_list = {
				50 = {
					carrier_event = { id = crisis.2502 }
				}
				50 = {
					carrier_event = { id = crisis.2503 }
				}
			}
		}
	}
}

# Synths Disappear
country_event = {
	id = crisis.2007
	hide_window = yes

	is_triggered_only = yes

	trigger = {
		has_technology = tech_synthetic_workers
		any_owned_pop_group = {
			has_trait = "trait_mechanical"
		}
	}

	immediate = {
		random_owned_planet = {
			limit = {
				any_owned_pop_group = {
					has_trait = "trait_mechanical"
				}
			}
			random_list = {
				50 = {
					carrier_event = { id = crisis.2015 }
				}
				50 = {
					carrier_event = { id = crisis.2016 }
				}
			}
		}
	}
}

# The Ghost Signal
country_event = {
	id = crisis.2010
	title = "crisis.2010.name"
	desc = {
		trigger = {
			NOT = {
				is_machine_empire = yes
			}
		}
		text = crisis.2010.desc
	}
	desc = {
		trigger = {
			is_machine_empire = yes
		}
		text = crisis.2010.desc.b
	}
	picture = GFX_evt_glitchy_matrix
	show_sound = event_alien_signal

	is_triggered_only = yes

	option = {
		name = crisis.2010.a
		begin_event_chain = {
			event_chain = "ghost_signal_chain"
			target = ROOT
		}
	}
}

# Malfunctioning Synthetic
carrier_event = {
	id = crisis.2011
	title = "crisis.2100.name"
	desc = "crisis.2100.desc"
	picture = GFX_evt_robot_assembly_plant
	show_sound = event_robo
	location = root

	is_triggered_only = yes

	option = {
		name = crisis.2100.a
		random_owned_pop_group = {
			limit = {
				has_trait = "trait_mechanical"
			}
			kill_single_pop = yes
		}
	}
}

# Rogue Synthetic
carrier_event = {
	id = crisis.2012
	title = "crisis.2103.name"
	desc = "crisis.2103.desc"
	picture = GFX_evt_robot_assembly_plant
	show_sound = event_robo
	location = root

	is_triggered_only = yes

	option = {
		name = crisis.2103.a
		random_owned_pop_group = {
			limit = {
				has_trait = "trait_mechanical"
			}
			kill_single_pop = yes
		}
	}
}

# Rogue Machine Pop
carrier_event = {
	id = crisis.2502
	title = "crisis.2502.name"
	desc = "crisis.2502.desc"
	picture = GFX_evt_glitchy_matrix
	show_sound = event_robo
	location = root

	is_triggered_only = yes

	option = {
		name = WORRYING
		random_owned_pop_group = {
			limit = {
				has_trait = trait_machine_unit
			}
			kill_single_pop = yes
		}
	}
}

# Rogue Machine Pop
carrier_event = {
	id = crisis.2503
	title = "crisis.2503.name"
	desc = "crisis.2503.desc"
	picture = GFX_evt_glitchy_matrix
	show_sound = event_robo
	location = root

	is_triggered_only = yes

	option = {
		name = WORRYING
		random_owned_pop_group = {
			limit = {
				has_trait = trait_machine_unit
			}
			kill_single_pop = yes
		}
	}
}

# Missing Synthetics
carrier_event = {
	id = crisis.2015
	title = "crisis.2101.name"
	desc = "crisis.2101.desc"
	picture = GFX_evt_robot_assembly_plant
	show_sound = event_robo
	location = root

	is_triggered_only = yes

	option = {
		name = crisis.2101.a
		random_owned_pop_group = {
			limit = {
				has_trait = "trait_mechanical"
			}
			kill_single_pop = yes
		}
	}
}

# Unauthorized Synthetic Project
carrier_event = {
	id = crisis.2016
	title = "crisis.2102.name"
	desc = "crisis.2102.desc"
	picture = GFX_evt_synth_sabotage
	show_sound = event_robo
	location = root

	is_triggered_only = yes

	option = {
		name = crisis.2102.a
		random_owned_pop_group = {
			limit = {
				has_trait = "trait_mechanical"
			}
			kill_single_pop = yes
		}
	}
}

# Synthetic Disappearances
country_event = {
	id = crisis.2020
	title = "crisis.2020.name"
	desc = {
		trigger = {
			NOT = {
				is_machine_empire = yes
				has_country_flag = synthetic_empire
				is_individual_machine = yes
			}
		}
		text = crisis.2020.desc
	}
	desc = {
		trigger = {
			is_machine_empire = yes
		}
		text = crisis.2020.desc.b
	}
	desc = {
		trigger = {
			has_country_flag = synthetic_empire
		}
		text = crisis.2020.desc.c
	}
	desc = {
		trigger = {
			is_individual_machine = yes
		}
		text = crisis.2020.desc.d
	}
	picture = GFX_evt_robot_assembly_plant
	show_sound = event_robo

	is_triggered_only = yes

	immediate = {
		observer_event = { id = observer.42 }
	}

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

# The Summons
country_event = {
	id = crisis.2021
	title = "crisis.2021.name"
	desc = {
		trigger = {
			is_machine_empire = no
		}
		text = crisis.2021.desc
	}
	desc = {
		trigger = {
			is_machine_empire = yes
		}
		text = crisis.2021.desc.b
	}
	picture = GFX_evt_machine_sapience
	show_sound = event_robo

	is_triggered_only = yes

	immediate = {
		observer_event = { id = observer.43 }
	}

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

# The Arrival
country_event = {
	id = crisis.2022
	title = "crisis.2022.name"
	desc = {
		trigger = {
			is_machine_empire = no
		}
		text = crisis.2022.desc
	}
	desc = {
		trigger = {
			is_machine_empire = yes
		}
		text = crisis.2022.desc.b
	}
	picture = GFX_evt_machine_sapience
	show_sound = event_ai_started
	trackable = yes

	is_triggered_only = yes

	immediate = {
		observer_event = { id = observer.44 }
		end_event_chain = "ghost_signal_chain"
		begin_event_chain = {
			event_chain = "ai_crisis_chain"
			target = ROOT
		}
		if = {
			limit = {
				NOT = {
					is_machine_empire = yes
				}
			}
			add_notification_modifier = ghost_signal_5
			set_global_flag = ghost_signal_5_flag
			every_owned_pop_group = {
				limit = {
					OR = {
						AND = {
							has_trait = "trait_mechanical"
							NOT = {
								root = { has_country_flag = synthetic_empire }
							}
						}
						# Synthetically Ascended Empires
						AND = {
							root = { has_country_flag = synthetic_empire }
							has_trait = trait_mechanical
							NOT = {
								is_same_species = root.species
							}
						}
					}
				}
				add_modifier = {
					modifier = "pop_ghost_signal_5"
					days = -1
				}
			}
			if = {
				limit = {
					OR = {
						has_country_flag = synthetic_empire
						is_individual_machine = yes
					}
				}
				enable_special_project = {
					name = "AI_CRISIS_PROJECT"
					location = capital_scope
					owner = root
				}
			}
		}
		else_if = {
			limit = { is_machine_empire = yes }
			add_notification_modifier = ghost_signal_5_machine
			every_owned_pop_group = {
				limit = { has_trait = trait_machine_unit }
				add_modifier = {
					modifier = "pop_ghost_signal_5_machine"
					days = -1
				}
			}
			enable_special_project = {
				name = "AI_CRISIS_PROJECT"
				location = capital_scope
				owner = root
			}
		}
		every_controlled_ship = {
			limit = {
				has_sapient_combat_computer = yes
			}
			add_modifier = {
				modifier = "ship_ghost_signal_5"
				days = -1
			}
		}
		if = {
			limit = {
				any_controlled_ship = {
					has_sapient_combat_computer = yes
				}
			}
			country_event = { id = crisis.2099 days = 10 }
		}
		if = {
			limit = {
				any_planet_within_border = {
					has_planet_flag = fotd_seperatist_planet@root
					owner = {
						has_country_flag = fotd_seperatist_country@root
					}
				}
			}
			country_event = { id = origin.6135 days = 5 random = 2 }
		}
	}

	option = {
		name = ONSCREEN
		if = {
			limit = { is_machine_empire = yes }
			if = {
				limit = { has_technology = tech_decryption_3 }
				custom_tooltip = tooltip_synth_crisis_encryption_bonus
			}
			tooltip = {
				enable_special_project = {
					name = "AI_CRISIS_PROJECT"
					location = capital_scope
					owner = root
				}
			}
		}
		hidden_effect = {
			country_event = { id = crisis.2023 }
		}
	}
	after = {
		hidden_effect = {
			country_event = { id = timeline.71 } # Timeline Contingency Crisis
		}
	}
}

# Incoming Transmission
country_event = {
	id = crisis.2023
	title = "TRANSMISSION"
	desc = {
		trigger = {
			is_machine_empire = no
		}
		text = crisis.2023.desc
	}
	desc = {
		trigger = {
			is_machine_empire = yes
		}
		text = crisis.2023.desc.b
	}

	diplomatic = yes

	picture_event_data = {
		portrait = ai_crisis_1
	}

	is_triggered_only = yes

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

# Machine Empire Special Project Finished
country_event = {
	id = crisis.2510
	title = crisis.2510.name
	desc = crisis.2510.desc
	show_sound = event_alien_signal
	picture = GFX_evt_synth_sabotage

	is_triggered_only = yes

	immediate = {
		set_country_flag = ghost_signal_blocked
	}

	option = {
		name = EXCELLENT
		hidden_effect = {
			if = {
				limit = {
					has_notification_modifier = ghost_signal_5_machine
				}
				remove_notification_modifier = ghost_signal_5_machine
			}
			if = {
				limit = {
					has_notification_modifier = ghost_signal_4_machine
				}
				remove_notification_modifier = ghost_signal_4_machine
			}
			if = {
				limit = {
					has_notification_modifier = ghost_signal_5
				}
				remove_notification_modifier = ghost_signal_5
			}
			every_owned_pop_group = {
				limit = { has_modifier = "pop_ghost_signal_5_machine" }
				remove_modifier = "pop_ghost_signal_5_machine"
			}
			every_owned_pop_group = {
				limit = { has_modifier = "pop_ghost_signal_4_machine" }
				remove_modifier = "pop_ghost_signal_4_machine"
			}
			every_owned_pop_group = {
				limit = { has_modifier = "pop_ghost_signal_5" }
				remove_modifier = "pop_ghost_signal_5"
			}
		}
	}
}

# Machine Worlds Gatekeeper (HIDDEN)
country_event = {
	id = crisis.2024
	hide_window = yes
	fire_only_once = yes
	is_triggered_only = yes

	immediate = {
		# First Machine World
		if = {
			limit = {
				any_galaxy_planet = {
					has_planet_flag = machine_world_1
				}
			}
			random_galaxy_planet = {
				limit = { has_planet_flag = machine_world_1 }
				carrier_event = { id = crisis.2025 }
			}
		}
		else = {
			random_system = {
				limit = {
					has_star_flag = AI_system_1
				}
				remove_star_flag = AI_hub
				remove_star_flag = AI_system_1
			}
			random_galaxy_planet = {
				limit = {
					is_planet_class = pc_barren
					NOT = {
						solar_system = {
							any_system_planet = {
								has_planet_flag = machine_world_2
								has_planet_flag = machine_world_3
								has_planet_flag = machine_world_4
							}
						}
					}
				}
				set_planet_flag = machine_world_1
				set_carrier_flag = crisis_vital_planet
				solar_system = {
					set_star_flag = AI_hub
					set_star_flag = AI_system_1
				}
				carrier_event = { id = crisis.2025 }
			}
		}

		# Second Machine World
		if = {
			limit = {
				any_galaxy_planet = {
					has_planet_flag = machine_world_1
				}
			}
			random_galaxy_planet = {
				limit = { has_planet_flag = machine_world_2 }
				carrier_event = { id = crisis.2026 days = 50 random = 10 }
			}
		}
		else = {
			random_system = {
				limit = {
					has_star_flag = AI_system_1
				}
				remove_star_flag = AI_hub
				remove_star_flag = AI_system_1
			}
			random_galaxy_planet = {
				limit = {
					is_planet_class = pc_barren
					NOT = {
						solar_system = {
							any_system_planet = {
								has_planet_flag = machine_world_1
								has_planet_flag = machine_world_3
								has_planet_flag = machine_world_4
							}
						}
					}
				}
				set_planet_flag = machine_world_2
				set_carrier_flag = crisis_vital_planet
				solar_system = {
					set_star_flag = AI_hub
					set_star_flag = AI_system_2
				}
				carrier_event = { id = crisis.2026 days = 50 random = 10 }
			}
		}

		# Third Machine World
		if = {
			limit = {
				any_galaxy_planet = {
					has_planet_flag = machine_world_1
				}
			}
			random_galaxy_planet = {
				limit = { has_planet_flag = machine_world_3 }
				carrier_event = { id = crisis.2027 days = 100 random = 10 }
			}
		}
		else = {
			random_system = {
				limit = {
					has_star_flag = AI_system_1
				}
				remove_star_flag = AI_hub
				remove_star_flag = AI_system_1
			}
			random_galaxy_planet = {
				limit = {
					is_planet_class = pc_barren
					NOT = {
						solar_system = {
							any_system_planet = {
								has_planet_flag = machine_world_1
								has_planet_flag = machine_world_2
								has_planet_flag = machine_world_4
							}
						}
					}
				}
				set_planet_flag = machine_world_3
				set_carrier_flag = crisis_vital_planet
				solar_system = {
					set_star_flag = AI_hub
					set_star_flag = AI_system_3
				}
				carrier_event = { id = crisis.2027 days = 100 random = 10 }
			}
		}

		# Fourth Machine World
		if = {
			limit = {
				any_galaxy_planet = {
					has_planet_flag = machine_world_1
				}
			}
			random_galaxy_planet = {
				limit = { has_planet_flag = machine_world_4 }
				carrier_event = { id = crisis.2028 days = 150 random = 10 }
			}
		}
		else = {
			random_system = {
				limit = {
					has_star_flag = AI_system_1
				}
				remove_star_flag = AI_hub
				remove_star_flag = AI_system_1
			}
			random_galaxy_planet = {
				limit = {
					is_planet_class = pc_barren
					NOT = {
						solar_system = {
							any_system_planet = {
								has_planet_flag = machine_world_1
								has_planet_flag = machine_world_2
								has_planet_flag = machine_world_3
							}
						}
					}
				}
				set_planet_flag = machine_world_4
				set_carrier_flag = crisis_vital_planet
				solar_system = {
					set_star_flag = AI_hub
					set_star_flag = AI_system_4
				}
				carrier_event = { id = crisis.2028 days = 150 random = 10 }
			}
		}
	}
}

# First Machine World (HIDDEN)
carrier_event = {
	id = crisis.2025
	hide_window = yes

	is_triggered_only = yes

	immediate = {
		set_crisis_sound = ai_empire_ambient_stage_1
		set_crisis_stage_1 = yes
		create_species = {
			name = "NAME_Custodian_Bot"
			class = "ROBOT"
			namelist = "AI"
			portrait = "hum_robot_red"
			traits = {
				trait = "trait_mechanical"
			}
			effect = { save_global_event_target_as = custodian_bot }
		}
		create_species = {
			name = "NAME_Contingency"
			class = "ROBOT"
			namelist = "AI"
			portrait = "ai_crisis_1"
			immortal = yes
			traits = {
				trait = "trait_mechanical"
			}
			effect = { save_global_event_target_as = contingency_bot }
		}
		create_country = {
			name = NAME_Contingency
			species = event_target:custodian_bot # for pops
			#species = ROBOT_POP_SPECIES_3
			authority = auth_ancient_machine_intelligence
			ethos = {
				ethic = ethic_gestalt_consciousness
			}
			civics = {
				civic = civic_machine_terminator
				civic = civic_machine_warbots
			}
			type = "ai_empire"
			name_list = "AI"
			graphical_culture = "ai_01"
			flag = {
				icon= {
					category = "special"
					file = "ai_01.dds"
				}
				background= {
					category = "backgrounds"
					file = "circle.dds"
				}
				colors={
					"black"
					"red"
					"null"
					"null"
				}
			}
			effect = {
				create_leader = {
					class = commander
					species = event_target:contingency_bot
					skill = 10
					name = random
					effect = {
						save_event_target_as = contingency_ruler
					}
				}
				set_leader = event_target:contingency_ruler
				create_ship_design = { design = "NAME_AI_Transport" }
				add_ship_design = last_created_design
				create_ship_design = { design = "NAME_AI_Constructor" }
				add_ship_design = last_created_design
				create_ship_design = { design = "NAME_AI_1" }
				add_ship_design = last_created_design
				create_ship_design = { design = "NAME_AI_2" }
				add_ship_design = last_created_design
				create_ship_design = { design = "NAME_AI_STARBASE" }
				add_ship_design = last_created_design
				save_event_target_as = AI_crisis
			}
		}
		every_owned_pop_group = { kill_all_pop = yes }
		change_pc = pc_ai
		reset_planet = yes
		set_name = "NAME_Sterilization_Hub_1"
		set_owner = event_target:AI_crisis
		set_controller = event_target:AI_crisis
		create_ai_planet_defense = yes
		create_ai_fleet = yes
		create_ai_fleet = yes
		random_country = { observer_event = { id = observer.45 } }
		every_country = {
			limit = { is_ai = no }
			country_event = { id = crisis.2030 }
		}
		every_country = {
			limit = {
				OR = {
					is_country_type = default
					is_country_type = fallen_empire
					is_country_type = awakened_fallen_empire
				}
			}
			establish_communications_no_message = event_target:AI_crisis
		}
		carrier_event = { id = crisis.2050 } # Start Fleet reinforcement cycle
		carrier_event = { id = crisis.2052 } # Start Army reinforcement cycle
		every_country = {
			limit = { has_event_chain = "ai_crisis_chain" }
			add_event_chain_counter = {
				event_chain = "ai_crisis_chain"
				counter = "active_machine_worlds"
				amount = 1
			}
		}
		awaken_guardians_of_the_galaxy = yes
		awaken_fallen_machine_empire = yes
		set_global_flag = ai_invasion_ongoing
		set_global_flag = synth_terror

		solar_system = {
			if = {
				limit = { exists = starbase }
				starbase = {
					fleet = {
						destroy_fleet = this
					}
				}
			}
			create_contingency_starbase = yes
			star = {
				create_ambient_object = {
					type = "contingency_1"
					location = this
				}
				last_created_ambient_object = {
					set_ambient_object_flag = contingency_system_effect
					set_location = {
						target = prev
						distance = 0
						angle = random
					}
				}
			}
		}
	}
}

# Second Machine World (HIDDEN)
carrier_event = {
	id = crisis.2026
	hide_window = yes

	is_triggered_only = yes

	immediate = {
		random_country = {
			limit = { is_country_type = ai_empire }
			save_event_target_as = AI_crisis
		}
		change_pc = pc_ai
		reset_planet = yes
		set_name = "NAME_Sterilization_Hub_2"
		set_owner = event_target:AI_crisis
		set_controller = event_target:AI_crisis
		create_ai_planet_defense = yes
		create_ai_fleet = yes
		create_ai_fleet = yes
		random_country = { observer_event = { id = observer.46 } }
		every_country = {
			limit = { is_ai = no }
			country_event = { id = crisis.2031 }
		}
		carrier_event = { id = crisis.2050 } # Start Fleet reinforcement cycle
		carrier_event = { id = crisis.2052 } # Start Army reinforcement cycle
		every_country = {
			limit = { has_event_chain = "ai_crisis_chain" }
			add_event_chain_counter = {
				event_chain = "ai_crisis_chain"
				counter = "active_machine_worlds"
				amount = 1
			}
		}
		if = {
			limit = {
				has_planet_flag = has_arc_furnace
			}
			set_planet_entity = { entity = pc_molten }
			remove_planet_flag = has_megastructure
			remove_planet_flag = has_arc_furnace
			solar_system = {
				every_system_planet = {
					limit = {
						OR = {
							has_modifier = orbital_arc_furnace_1_mod
							has_modifier = orbital_arc_furnace_2_mod
							has_modifier = orbital_arc_furnace_3_mod
							has_modifier = orbital_arc_furnace_4_mod
						}
					}
					dismantle_arc_furnace_effect = yes
				}
				random_system_megastructure = {
					limit = {
						OR = {
							is_megastructure_type = orbital_arc_furnace_1
							is_megastructure_type = orbital_arc_furnace_2
							is_megastructure_type = orbital_arc_furnace_3
							is_megastructure_type = orbital_arc_furnace_4
							is_megastructure_type = orbital_arc_furnace_restored
						}
					}
					remove_megastructure = this
				}
			}
		}
		solar_system = {
			if = {
				limit = { exists = starbase }
				starbase = {
					fleet = {
						destroy_fleet = this
					}
				}
			}
			create_contingency_starbase = yes
			star = {
				create_ambient_object = {
					type = "contingency_1"
					location = this
				}
				last_created_ambient_object = {
					set_ambient_object_flag = contingency_system_effect
					set_location = {
						target = prev
						distance = 0
						angle = random
					}
				}
			}
		}
	}
}

# Third Machine World (HIDDEN)
carrier_event = {
	id = crisis.2027
	hide_window = yes

	is_triggered_only = yes

	immediate = {
		random_country = {
			limit = { is_country_type = ai_empire }
			save_event_target_as = AI_crisis
		}
		change_pc = pc_ai
		reset_planet = yes
		change_planet_size = 20
		set_name = "NAME_Sterilization_Hub_3"
		set_owner = event_target:AI_crisis
		set_controller = event_target:AI_crisis
		create_ai_planet_defense = yes
		create_ai_fleet = yes
		create_ai_fleet = yes
		random_playable_country = { observer_event = { id = observer.47 } }
		every_country = {
			limit = { is_ai = no }
			country_event = { id = crisis.2032 }
		}
		carrier_event = { id = crisis.2050 } # Start Fleet reinforcement cycle
		carrier_event = { id = crisis.2052 } # Start Army reinforcement cycle
		every_country = {
			limit = { has_event_chain = "ai_crisis_chain" }
			add_event_chain_counter = {
				event_chain = "ai_crisis_chain"
				counter = "active_machine_worlds"
				amount = 1
			}
		}
		solar_system = {
			if = {
				limit = { exists = starbase }
				starbase = {
					fleet = {
						destroy_fleet = this
					}
				}
			}
			create_contingency_starbase = yes
			star = {
				create_ambient_object = {
					type = "contingency_1"
					location = this
				}
				last_created_ambient_object = {
					set_ambient_object_flag = contingency_system_effect
					set_location = {
						target = prev
						distance = 0
						angle = random
					}
				}
			}
		}
	}
}

# Fourth Machine World (HIDDEN)
carrier_event = {
	id = crisis.2028
	hide_window = yes

	is_triggered_only = yes

	immediate = {
		random_country = {
			limit = { is_country_type = ai_empire }
			save_event_target_as = AI_crisis
		}
		change_pc = pc_ai
		reset_planet = yes
		set_name = "NAME_Sterilization_Hub_4"
		set_owner = event_target:AI_crisis
		set_controller = event_target:AI_crisis
		create_ai_planet_defense = yes
		create_ai_fleet = yes
		create_ai_fleet = yes
		random_country = { observer_event = { id = observer.48 } }

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

		every_country = {
			limit = { is_ai = no }
			country_event = { id = crisis.2033 }
		}
		carrier_event = { id = crisis.2050 } # Start Fleet reinforcement cycle
		carrier_event = { id = crisis.2052 } # Start Army reinforcement cycle
		every_country = {
			limit = { has_event_chain = "ai_crisis_chain" }
			add_event_chain_counter = {
				event_chain = "ai_crisis_chain"
				counter = "active_machine_worlds"
				amount = 1
			}
		}
		solar_system = {
			if = {
				limit = { exists = starbase }
				starbase = {
					fleet = {
						destroy_fleet = this
					}
				}
			}
			create_contingency_starbase = yes
			star = {
				create_ambient_object = {
					type = "contingency_1"
					location = this
				}
				last_created_ambient_object = {
					set_ambient_object_flag = contingency_system_effect
					set_location = {
						target = prev
						distance = 0
						angle = random
					}
				}
			}
		}
	}
}

# First Machine World Notification
country_event = {
	id = crisis.2030
	title = "crisis.2030.name"
	desc = "crisis.2030.desc"
	picture = GFX_evt_contingency_planet
	show_sound = event_red_alert
	location = from

	is_triggered_only = yes

	option = {
		name = crisis.2030.a
		create_point_of_interest = {
			id = ai_crisis_poi.1
			name = "ai_crisis_poi_1"
			desc = "ai_crisis_poi_1_desc"
			event_chain = "ai_crisis_chain"
			location = from
		}
	}
}

# Second Machine World Notification
country_event = {
	id = crisis.2031
	title = "crisis.2031.name"
	desc = "crisis.2031.desc"
	picture = GFX_evt_contingency_planet
	show_sound = event_red_alert
	location = from

	is_triggered_only = yes

	option = {
		name = crisis.2031.a
		create_point_of_interest = {
			id = ai_crisis_poi.2
			name = "ai_crisis_poi_2"
			desc = "ai_crisis_poi_2_desc"
			event_chain = "ai_crisis_chain"
			location = from
		}
	}
}

# Third Machine World Notification
country_event = {
	id = crisis.2032
	title = "crisis.2032.name"
	desc = "crisis.2032.desc"
	picture = GFX_evt_contingency_planet
	show_sound = event_red_alert
	location = from

	is_triggered_only = yes

	option = {
		name = crisis.2032.a
		create_point_of_interest = {
			id = ai_crisis_poi.3
			name = "ai_crisis_poi_3"
			desc = "ai_crisis_poi_3_desc"
			event_chain = "ai_crisis_chain"
			location = from
		}
	}
}

# Fourth Machine World Notification
country_event = {
	id = crisis.2033
	title = "crisis.2033.name"
	desc = "crisis.2033.desc"
	picture = GFX_evt_contingency_planet
	show_sound = event_red_alert
	location = from

	is_triggered_only = yes

	option = {
		name = crisis.2033.a
		create_point_of_interest = {
			id = ai_crisis_poi.4
			name = "ai_crisis_poi_4"
			desc = "ai_crisis_poi_4_desc"
			event_chain = "ai_crisis_chain"
			location = from
		}
	}
}

# Final Machine World (HIDDEN)
country_event = {
	id = crisis.2035
	hide_window = yes

	is_triggered_only = yes

	immediate = {
		random_rim_system = {
			spawn_system = {
				min_distance >= 20
				max_distance <= 50
				initializer = "ai_system_05"
			}
		}
		random_system = {
			limit = { has_star_flag = AI_lair }
			save_event_target_as = AI_lair
			random_system_planet = {
				limit = { has_planet_flag = machine_lair }
				create_fleet = {
					name = "NAME_AI_Final_Core"
					effect = {
						set_owner = event_target:AI_crisis
						create_ship = {
							name = random
							design = "NAME_AI_Final_Core"
							graphical_culture = "ai_01"
						}
						set_location = {
							target = prev
							distance = 10
							angle = random
						}
						save_event_target_as = final_core
					}
				}
				create_fleet = {
					name = "NAME_AI_Core"
					effect = {
						set_owner = event_target:AI_crisis
						create_ship = {
							name = random
							design = "NAME_AI_Core"
							graphical_culture = "ai_01"
						}
						set_location = {
							target = event_target:final_core
							distance = 17
							angle = 0
						}
					}
				}
				create_fleet = {
					name = "NAME_AI_Core"
					effect = {
						set_owner = event_target:AI_crisis
						create_ship = {
							name = random
							design = "NAME_AI_Core"
							graphical_culture = "ai_01"
						}
						set_location = {
							target = event_target:final_core
							distance = 17
							angle = 90
						}
					}
				}
				create_fleet = {
					name = "NAME_AI_Core"
					effect = {
						set_owner = event_target:AI_crisis
						create_ship = {
							name = random
							design = "NAME_AI_Core"
							graphical_culture = "ai_01"
						}
						set_location = {
							target = event_target:final_core
							distance = 17
							angle = 180
						}
					}
				}
				create_fleet = {
					name = "NAME_AI_Core"
					effect = {
						set_owner = event_target:AI_crisis
						create_ship = {
							name = random
							design = "NAME_AI_Core"
							graphical_culture = "ai_01"
						}
						set_location = {
							target = event_target:final_core
							distance = 17
							angle = 270
						}
					}
				}
			}
			if = {
				limit = { exists = starbase }
				starbase = {
					fleet = {
						destroy_fleet = this
					}
				}
			}
			create_contingency_starbase = yes
			star = {
				create_ambient_object = {
					type = "contingency_2"
					location = this
				}
				last_created_ambient_object = {
					set_ambient_object_flag = contingency_system_effect_2
					set_location = {
						target = prev
						distance = 0
						angle = random
					}
				}
			}
		}
		observer_event = { id = observer.49 }
		every_country = {
			limit = { is_ai = no }
			country_event = { id = crisis.2036 }
			country_event = { id = crisis.2039 days = 5 }
		}
		every_country = {
			limit = { has_event_chain = "ai_crisis_chain" }
			add_event_chain_counter = {
				event_chain = "ai_crisis_chain"
				counter = "active_machine_worlds"
				amount = 1
			}
		}
	}
}

# Final Machine World
country_event = {
	id = crisis.2036
	title = "crisis.2036.name"
	desc = "crisis.2036.desc"
	picture = GFX_evt_machine_space_battle
	show_sound = event_red_alert
	location = event_target:AI_lair

	is_triggered_only = yes

	option = {
		name = crisis.2036.a
		create_point_of_interest = {
			id = ai_crisis_poi.5
			name = "ai_crisis_poi_5"
			desc = "ai_crisis_poi_5_desc"
			event_chain = "ai_crisis_chain"
			location = event_target:AI_lair
		}
	}
}

# Incoming Transmission
country_event = {
	id = crisis.2039
	title = "TRANSMISSION"
	desc = "crisis.2039.desc"

	diplomatic = yes

	picture_event_data = {
		portrait = ai_crisis_1
	}

	is_triggered_only = yes

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

# Machine World Destroyed
country_event = {
	id = crisis.2037
	title = "crisis.2037.name"

	desc = {
		text = crisis.2037.desc
		trigger = {
			NOR = {
				has_global_flag = first_machine_world_destroyed
				from = { has_carrier_flag = destroyed_by_colossus }
			}
		}
	}
	desc = {
		text = crisis.2037.alt.desc
		trigger = {
			has_global_flag = first_machine_world_destroyed
			NOT = {
				from = { has_carrier_flag = destroyed_by_colossus }
			}
			OR = {
				is_machine_empire = no
				AND = {
					is_machine_empire = yes
					check_variable = {
						which = "machine_worlds_destroyed"
						value > 2
					}
				}
			}
		}
	}
	desc = {
		text = crisis.2037.alt.machine.desc
		trigger = {
			has_global_flag = first_machine_world_destroyed
			is_machine_empire = yes
			NOT = { has_country_flag = ghost_signal_blocked }
		}
	}
	desc = {
		text = crisis.2037.alt.cracked.desc
		trigger = {
			from = {
				has_carrier_flag = destroyed_by_colossus
				has_carrier_flag = planet_cracked
			}
		}
	}
	desc = {
		text = crisis.2037.alt.shielded.desc
		trigger = {
			from = {
				has_carrier_flag = destroyed_by_colossus
				has_carrier_flag = planet_shielded
			}
		}
	}
	desc = {
		text = crisis.2037.alt.godray.desc
		trigger = {
			from = {
				has_carrier_flag = destroyed_by_colossus
				has_carrier_flag = planet_godrayed
			}
		}
	}

	picture = GFX_evt_exploding_planet
	show_sound = event_super_explosion
	location = from

	is_triggered_only = yes

	trigger = {
		# event not relevant if system is Star Eaten
		from.solar_system = { NOT = { has_star_flag = star_cracked } }
	}

	option = {
		name = crisis.2037.a
		if = {
			limit = {
				has_tech_option = tech_mine_living_metal
				NOT = { has_global_flag = first_machine_world_destroyed }
			}
			add_tech_progress = {
				tech = tech_mine_living_metal
				progress = 0.25
			}
		}
		else_if = {
			limit = { NOT = { has_global_flag = first_machine_world_destroyed } }
			add_research_option = tech_mine_living_metal
		}
	}
}

# Final Machine World Destroyed
country_event = {
	id = crisis.2038
	title = "crisis.2038.name"
	desc = {
		text = crisis.2038.desc
		trigger = {
			NOT = {
				from = { has_carrier_flag = destroyed_by_colossus }
			}
		}
	}
	desc = {
		text = crisis.2038.cracker.desc
		trigger = {
			from = {
				has_carrier_flag = destroyed_by_colossus
				has_carrier_flag = planet_cracked
			}
		}
	}
	desc = {
		text = crisis.2038.shield.desc
		trigger = {
			from = {
				has_carrier_flag = destroyed_by_colossus
				has_carrier_flag = planet_shielded
			}
		}
	}
	desc = {
		text = crisis.2038.godray.desc
		trigger = {
			from = {
				has_carrier_flag = destroyed_by_colossus
				has_carrier_flag = planet_godrayed
			}
		}
	}
	picture = GFX_evt_exploding_planet
	show_sound = event_super_explosion
	location = from

	is_triggered_only = yes

	option = {
		name = crisis.2038.a
	}
	after = {
		if = {
			limit = {
				has_origin = origin_fear_of_the_dark
			}
			country_event = { id = origin.6136 }
		}
	}
}

# Machine World Destroyed
carrier_event = {
	id = crisis.2040
	hide_window = yes

	is_triggered_only = yes

	trigger = {
		is_planet_class = pc_ai
		NOT = { has_planet_flag = machine_lair }
		OR = { # cracked or bombarded to death
			from = { is_same_value = root }
			planet_devastation >= 50
			has_carrier_flag = planet_godrayed
			has_carrier_flag = planet_drenched
			has_carrier_flag = planet_devolved
			has_carrier_flag = planet_burned
		}
	}

	immediate = {
		destroy_machine_world = yes
	}
}

# Machine World Self-Destructs Rather Than Be Captured (HIDDEN)
carrier_event = {
	id = crisis.2045
	hide_window = yes

	is_triggered_only = yes

	trigger = {
		has_global_flag = ai_invasion_ongoing
		is_planet_class = pc_ai
		OR = {
			owner? = {
				NOT = {
					is_country_type = "ai_empire"
				}
			}
			controller? = {
				NOT = {
					is_country_type = "ai_empire"
				}
			}
		}
	}

	immediate = {
		if = {
			limit = {
				controller? = {
					NOT = {
						is_country_type = "ai_empire"
					}
				}
			}
			controller = { save_event_target_as = final_machine_world_destroyer }
		}
		else = {
			owner = { save_event_target_as = final_machine_world_destroyer }
		}
		if = {
			limit = {
				has_planet_flag = machine_lair
			}
			stop_crisis_sound = yes
			carrier_event = { id = crisis.2046 }
		}
		else = {
			destroy_machine_world = yes
		}
	}
}

# Machine World Changed Hands (HIDDEN)
carrier_event = {
	id = crisis.2047
	hide_window = yes

	is_triggered_only = yes

	trigger = {
		has_global_flag = ai_invasion_ongoing
		is_planet_class = pc_ai
	}

	# Delayed a day so the check reads a settled owner and controller rather than a half-applied transfer.
	immediate = {
		carrier_event = { id = crisis.2045 days = 1 }
	}
}

# Machine World Ownership Sweep (HIDDEN)
event = {
	id = crisis.2049
	hide_window = yes

	is_triggered_only = yes

	trigger = {
		has_global_flag = ai_invasion_ongoing
	}

	immediate = {
		every_system = {
			limit = {
				OR = {
					has_star_flag = AI_hub
					has_star_flag = AI_lair
				}
			}
			every_system_planet = {
				limit = {
					is_planet_class = pc_ai
				}
				carrier_event = { id = crisis.2045 }
			}
		}
	}
}

# Machines Conquer Planet
country_event = {
	id = crisis.2041
	hide_window = yes

	is_triggered_only = yes

	trigger = {
		is_country_type = "ai_empire"
	}

	immediate = {
		FROMFROM = {
			add_threat = { who = root amount = 2 }
			set_controller = root
		}
	}
}

# Machines Lose Planet
country_event = {
	id = crisis.2042
	title = "crisis.2042.name"
	desc = "crisis.2042.desc"
	picture = GFX_evt_ground_combat
	show_sound = event_ground_battle
	location = fromfrom

	is_triggered_only = yes

	trigger = {
		from = { is_country_type = "ai_empire" }
	}

	immediate = {
		FROMFROM = {
			every_owned_pop_group = { kill_all_pop = yes } # Kill Custodians
		}
		every_country = {
			limit = { has_event_chain = "ai_crisis_chain" }
			add_event_chain_counter = {
				event_chain = "ai_crisis_chain"
				counter = "assimilated_worlds"
				amount = -1
			}
		}
	}

	option = {
		name = crisis.2042.c
		fromfrom = {
			destroy_colony = yes
		}
	}
}

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

	is_triggered_only = yes

	trigger = {
		exists = event_target:custodian_bot
		count_owned_pop_amount = {
			limit = {
				NOT = {
					is_same_species = event_target:custodian_bot
				}
			}
			count <= 100
		}
		exists = controller
		controller = { is_country_type = "ai_empire" }
	}

	immediate = {
		controller = { save_event_target_as = contingency }
		set_owner = controller
		create_pop_group = { species = event_target:custodian_bot }
		create_pop_group = { species = event_target:custodian_bot }
		create_pop_group = { species = event_target:custodian_bot }
		every_owned_pop_group = {
			limit = {
				NOT = {
					is_same_species = event_target:custodian_bot
				}
			}
			kill_all_pop = yes
		}
		create_army = {
			owner = event_target:contingency
			type = "robotic_defense_army"
		}
		create_army = {
			owner = event_target:contingency
			type = "robotic_defense_army"
		}
		create_army = {
			owner = event_target:contingency
			type = "robotic_defense_army"
		}
		create_army = {
			owner = event_target:contingency
			type = "robotic_defense_army"
		}
		create_army = {
			owner = event_target:contingency
			type = "robotic_defense_army"
		}
		create_army = {
			owner = event_target:contingency
			type = "robotic_defense_army"
		}
		every_country = {
			limit = { has_event_chain = "ai_crisis_chain" }
			add_event_chain_counter = {
				event_chain = "ai_crisis_chain"
				counter = "assimilated_worlds"
				amount = 1
			}
		}

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

# Final Machine World Destroyed
carrier_event = {
	id = crisis.2043
	hide_window = yes

	is_triggered_only = yes

	trigger = {
		is_planet_class = pc_ai
		has_planet_flag = machine_lair
		OR = { # cracked or bombarded to death
			from = { is_same_value = root }
			planet_devastation >= 50
		}
	}

	immediate = {
		stop_crisis_sound = yes
		solar_system = {
			random_system_ambient_object = {
				limit = { has_ambient_object_flag = extradimensional_system_effect_2 }
				destroy_ambient_object = this
			}
		}
		every_fleet_in_orbit = {
			owner = {
				set_country_flag = does_not_compute
			}
		}
		from = { save_event_target_as = final_machine_world_destroyer }
		carrier_event = { id = crisis.2046 }
	}
}

# Final Machine World Destroyed
carrier_event = {
	id = crisis.2046
	hide_window = yes

	is_triggered_only = yes

	immediate = {
		set_global_flag = ai_invasion_defeated
		event_target:final_machine_world_destroyer = { set_country_flag = does_not_compute }
		event_target:final_machine_world_destroyer = { set_country_flag = with_great_power_achievement }
		if = {
			limit = {
				event_target:final_machine_world_destroyer = { is_ai = no }
			}
			event_target:final_machine_world_destroyer = { save_event_target_as = final_machine_world_player_destroyer }
		}
		else_if = {
			limit = {
				any_country = {
					is_ai = no
					has_country_flag = does_not_compute
				}
			}
			random_country = {
				limit = {
					is_ai = no
					has_country_flag = does_not_compute
				}
				save_event_target_as = final_machine_world_player_destroyer
			}
		}
		if = {
			limit = {
				exists = event_target:final_machine_world_player_destroyer
			}
			if = {
				limit = { NOT = { has_carrier_flag = planet_shielded } }
				event_target:final_machine_world_player_destroyer = {
					country_event = { id = crisis.2480 days = 10 random = 10 }
				}
			}
			else_if = {
				limit = { has_carrier_flag = planet_shielded }
				event_target:final_machine_world_player_destroyer = {
					country_event = { id = crisis.2481 days = 10 random = 10 }
				}
			}
		}
		remove_global_flag = synth_terror
		remove_global_flag = ai_ghost_signal
		remove_global_flag = ai_invasion_ongoing
		solar_system = {
			random_system_ambient_object = {
				limit = { has_ambient_object_flag = contingency_system_effect_2 }
				destroy_ambient_object = this
			}
		}
		if = {
			limit = {
				OR = {
					NOT = { has_carrier_flag = destroyed_by_colossus }
					has_carrier_flag = planet_godrayed
					has_carrier_flag = planet_drenched
					has_carrier_flag = planet_devolved
					has_carrier_flag = planet_burned
				}
			}
			destroy_colony = yes
			change_pc = pc_broken
			reset_planet = yes
		}
		every_country = {
			limit = { has_notification_modifier = ghost_signal_1 }
			remove_notification_modifier = ghost_signal_1
			every_owned_pop_group = {
				limit = { has_modifier = "pop_ghost_signal_1" }
				remove_modifier = "pop_ghost_signal_1"
			}
			every_controlled_ship = {
				limit = {
					has_sapient_combat_computer = yes
				}
				remove_modifier = "ship_ghost_signal_1"
			}
		}
		every_country = {
			limit = { has_event_chain = "ai_crisis_chain" }
			end_event_chain = "ai_crisis_chain"
		}
		every_country = {
			limit = { has_special_project = SYNTH_INFILTRATION_PROJECT }
			abort_special_project = { type = SYNTH_INFILTRATION_PROJECT }
		}
		every_country = {
			limit = { has_modifier = unity_reduction_crisis_2204 }
			remove_modifier = unity_reduction_crisis_2204
		}
		random_country = { observer_event = { id = observer.51 } }
		every_country = {
			limit = {
				OR = {
					is_country_type = default
					is_country_type = fallen_empire
					is_country_type = awakened_fallen_empire
				}
			}
			country_event = { id = crisis.2038 }
			every_owned_planet = {
				limit = { has_modifier = synth_planet_paranoia }
				remove_modifier = synth_planet_paranoia
			}
		}
		every_country = {
			limit = {
				is_fallen_empire_machine = yes
			}
			country_event = { id = fallen_machine_empire.3 days = 1 }
		}
		random_country = {
			limit = { is_country_type = ai_empire }
			destroy_country = yes # Destroy the Contingency
			# should end their purges too
		}
		remove_global_flag = galactic_crisis_recently_fired
		if = {
			limit = {
				has_global_flag = galactic_crisis_early_defeat_tracker_1
			}
			remove_global_flag = galactic_crisis_early_defeat_tracker_1
			multiply_crisis_strength = 1.5
		}
		else_if = {
			limit = {
				has_global_flag = galactic_crisis_early_defeat_tracker_2
			}
			remove_global_flag = galactic_crisis_early_defeat_tracker_2
			multiply_crisis_strength = 1.25
		}
		end_crisis = yes
		if = {
			limit = {
				any_country = {
					has_event_chain = defender_of_the_galaxy_ending_chain
				}
			}
			every_country = {
				limit = {
					has_event_chain = defender_of_the_galaxy_ending_chain
				}
				add_event_chain_counter = {
					event_chain = "defender_of_the_galaxy_ending_chain"
					counter = "crisis_defeated"
					amount = 1
				}
			}
		}

		event_target:final_machine_world_destroyer = { set_country_flag = final_machine_world_destroyed }

		# Cybrex
		if = {
			limit = {
				has_global_flag = cybrex_resurface
				NOT = { has_global_flag = cybrex_destroyed }
			}
			random_country = {
				limit = { is_country_type = cybrex_empire }

				# Cybrex recall expeditionary fleets
				every_country = {
					limit = { has_country_flag = cybrex_donation }
					country_event = { id = crisis.2474 days = 20 random = 30 }
				}

				# Cybrex Depart Galaxy
				country_event = { id = crisis.2406 days = 360 random = 800 }

			}
		}
	}
}

# Machine World Fleet Reinforcement Cycle
carrier_event = {
	id = crisis.2050
	hide_window = yes

	is_triggered_only = yes

	trigger = {
		is_planet_class = pc_ai
		NOT = { has_planet_flag = machine_lair }
		owner = { is_country_type = "ai_empire" }
	}

	immediate = {
		# If at fleet cap, check again later
		if = {
			limit = {
				owner = { num_ships > 2000 }
			}
			carrier_event = { id = crisis.2050 days = 300 }
		}
		# If being bombarded, check again later
		else_if = {
			limit = { has_orbital_bombardment = yes }
			carrier_event = { id = crisis.2050 days = 300 }
		}
		# Ghost Signal 2
		else_if = {
			limit = { has_global_flag = ghost_signal_2_flag }
			carrier_event = { id = crisis.2051 days = 1300 random = 200 }
		}
		# Ghost Signal 3
		else_if = {
			limit = { has_global_flag = ghost_signal_3_flag }
			carrier_event = { id = crisis.2051 days = 1400 random = 200 }
		}
		# Ghost Signal 4
		else_if = {
			limit = { has_global_flag = ghost_signal_4_flag }
			carrier_event = { id = crisis.2051 days = 1500 random = 200 }
		}
		# Ghost Signal 5
		else_if = {
			limit = { has_global_flag = ghost_signal_5_flag }
			carrier_event = { id = crisis.2051 days = 1600 random = 200 }
			break = yes
		}
	}
}

# AI Fleet Reinforcements Arrive
carrier_event = {
	id = crisis.2051
	hide_window = yes

	is_triggered_only = yes

	trigger = {
		is_planet_class = pc_ai
		NOT = { has_planet_flag = machine_lair }
		owner = { is_country_type = "ai_empire" }
	}

	immediate = {
		owner = { save_event_target_as = AI_crisis }
		create_ai_fleet = yes
		if = {
			limit = {
				is_difficulty > 3
			}
			create_ai_fleet = yes
		}
		if = {
			limit = {
				is_difficulty > 5
			}
			create_ai_fleet = yes
		}
		create_ai_constructors = yes
		carrier_event = { id = crisis.2050 }
	}
}

# Machine World Army Reinforcement Cycle
carrier_event = {
	id = crisis.2052
	hide_window = yes

	is_triggered_only = yes

	trigger = {
		is_planet_class = pc_ai
		NOT = { has_planet_flag = machine_lair }
		owner = { is_country_type = "ai_empire" }
	}

	immediate = {
		# If at army cap, check again later
		if = {
			limit = {
				owner = {
					count_owned_army = {
						limit = { army_type = robotic_army }
						count > 140
					}
				}
			}
			carrier_event = { id = crisis.2052 days = 300 }
		}
		# If being bombarded, check again later
		else_if = {
			limit = { has_orbital_bombardment = yes }
			carrier_event = { id = crisis.2052 days = 300 }
		}
		# Ghost Signal 2
		else_if = {
			limit = { has_global_flag = ghost_signal_2_flag }
			carrier_event = { id = crisis.2053 days = 1600 random = 200 }
		}
		# Ghost Signal 3
		else_if = {
			limit = { has_global_flag = ghost_signal_3_flag }
			carrier_event = { id = crisis.2053 days = 1300 random = 200 }
		}
		# Ghost Signal 4
		else_if = {
			limit = { has_global_flag = ghost_signal_4_flag }
			carrier_event = { id = crisis.2053 days = 1000 random = 200 }
		}
		# Ghost Signal 5
		else_if = {
			limit = { has_global_flag = ghost_signal_5_flag }
			carrier_event = { id = crisis.2053 days = 700 random = 200 }
		}
	}
}

# AI Army Reinforcements Arrive
carrier_event = {
	id = crisis.2053
	hide_window = yes

	is_triggered_only = yes

	trigger = {
		is_planet_class = pc_ai
		NOT = { has_planet_flag = machine_lair }
		owner = { is_country_type = "ai_empire" }
	}

	immediate = {
		owner = { save_event_target_as = AI_crisis }
		create_ai_armies = yes
		carrier_event = { id = crisis.2052 }
	}
}

# New Synth gets signal modifier
carrier_event = {
	id = crisis.2055
	hide_window = yes

	is_triggered_only = yes

	trigger = {
		fromfrom = {
			has_trait = "trait_mechanical"
			owner = {
				OR = {
					has_notification_modifier = ghost_signal_5
					has_notification_modifier = ghost_signal_4
					has_notification_modifier = ghost_signal_3
					has_notification_modifier = ghost_signal_2
					has_notification_modifier = ghost_signal_1
				}
				NOR = {
					has_country_flag = synthetic_empire
					is_individual_machine = yes
				}
			}
		}
	}

	immediate = {
		fromfrom = {
			if = {
				limit = { owner = { has_notification_modifier = ghost_signal_5 } }
				add_modifier = { modifier = "pop_ghost_signal_5" days = -1 }
			}
			else_if = {
				limit = { owner = { has_notification_modifier = ghost_signal_4 } }
				add_modifier = { modifier = "pop_ghost_signal_4" days = -1 }
			}
			else_if = {
				limit = { owner = { has_notification_modifier = ghost_signal_3 } }
				add_modifier = { modifier = "pop_ghost_signal_3" days = -1 }
			}
			else_if = {
				limit = { owner = { has_notification_modifier = ghost_signal_2 } }
				add_modifier = { modifier = "pop_ghost_signal_2" days = -1 }
			}
			else_if = {
				limit = { owner = { has_notification_modifier = ghost_signal_1 } }
				add_modifier = { modifier = "pop_ghost_signal_1" days = -1 }
			}
		}
	}
}

# New ship gets signal modifier
ship_event = {
	id = crisis.2056
	hide_window = yes

	is_triggered_only = yes

	trigger = {
		has_global_flag = ai_ghost_signal
		has_sapient_combat_computer = yes
		owner = {
			OR = {
				has_notification_modifier = ghost_signal_5
				has_notification_modifier = ghost_signal_4
				has_notification_modifier = ghost_signal_3
				has_notification_modifier = ghost_signal_2
				has_notification_modifier = ghost_signal_1
				has_notification_modifier = ghost_signal_5_machine
				has_notification_modifier = ghost_signal_4_machine
			}
		}
	}

	immediate = {
		if = {
			limit = { owner = { has_notification_modifier = ghost_signal_5 } }
			add_modifier = { modifier = "ship_ghost_signal_5" days = -1 }
		}
		else_if = {
			limit = { owner = { has_notification_modifier = ghost_signal_4 } }
			add_modifier = { modifier = "ship_ghost_signal_4" days = -1 }
		}
		else_if = {
			limit = { owner = { has_notification_modifier = ghost_signal_3 } }
			add_modifier = { modifier = "ship_ghost_signal_3" days = -1 }
		}
		else_if = {
			limit = { owner = { has_notification_modifier = ghost_signal_2 } }
			add_modifier = { modifier = "ship_ghost_signal_2" days = -1 }
		}
		else_if = {
			limit = { owner = { has_notification_modifier = ghost_signal_1 } }
			add_modifier = { modifier = "ship_ghost_signal_1" days = -1 }
		}
	}
}

# Upgraded ships get/lose signal modifier
ship_event = {
	id = crisis.2057
	hide_window = yes

	is_triggered_only = yes

	trigger = {
		has_global_flag = ai_ghost_signal
		owner = {
			OR = {
				has_notification_modifier = ghost_signal_5
				has_notification_modifier = ghost_signal_4
				has_notification_modifier = ghost_signal_3
				has_notification_modifier = ghost_signal_2
				has_notification_modifier = ghost_signal_1
				has_notification_modifier = ghost_signal_5_machine
				has_notification_modifier = ghost_signal_4_machine
			}
		}
		OR = {
			has_sapient_combat_computer = yes
			has_modifier = ship_ghost_signal_5
			has_modifier = ship_ghost_signal_4
			has_modifier = ship_ghost_signal_3
			has_modifier = ship_ghost_signal_2
			has_modifier = ship_ghost_signal_1
		}
	}

	immediate = {
		if = {
			limit = {
				has_sapient_combat_computer = no
			}
			remove_modifier = ship_ghost_signal_5
			remove_modifier = ship_ghost_signal_4
			remove_modifier = ship_ghost_signal_3
			remove_modifier = ship_ghost_signal_2
			remove_modifier = ship_ghost_signal_1
		}
		else_if = {
			limit = {
				has_sapient_combat_computer = yes
				owner = { has_notification_modifier = ghost_signal_5 }
				NOT = { has_modifier = "ship_ghost_signal_5" }
			}
			add_modifier = { modifier = "ship_ghost_signal_5" days = -1 }
			remove_modifier = ship_ghost_signal_4
			remove_modifier = ship_ghost_signal_3
			remove_modifier = ship_ghost_signal_2
			remove_modifier = ship_ghost_signal_1
		}
		else_if = {
			limit = {
				has_sapient_combat_computer = yes
				owner = { has_notification_modifier = ghost_signal_4 }
				NOT = { has_modifier = "ship_ghost_signal_4" }
			}
			add_modifier = { modifier = "ship_ghost_signal_4" days = -1 }
			remove_modifier = ship_ghost_signal_5
			remove_modifier = ship_ghost_signal_3
			remove_modifier = ship_ghost_signal_2
			remove_modifier = ship_ghost_signal_1
		}
		else_if = {
			limit = {
				has_sapient_combat_computer = yes
				owner = { has_notification_modifier = ghost_signal_3 }
				NOT = { has_modifier = "ship_ghost_signal_3" }
			}
			add_modifier = { modifier = "ship_ghost_signal_3" days = -1 }
			remove_modifier = ship_ghost_signal_5
			remove_modifier = ship_ghost_signal_4
			remove_modifier = ship_ghost_signal_2
			remove_modifier = ship_ghost_signal_1
		}
		else_if = {
			limit = {
				has_sapient_combat_computer = yes
				owner = { has_notification_modifier = ghost_signal_2 }
				NOT = { has_modifier = "ship_ghost_signal_2" }
			}
			add_modifier = { modifier = "ship_ghost_signal_2" days = -1 }
			remove_modifier = ship_ghost_signal_5
			remove_modifier = ship_ghost_signal_4
			remove_modifier = ship_ghost_signal_3
			remove_modifier = ship_ghost_signal_1
		}
		else_if = {
			limit = {
				has_sapient_combat_computer = yes
				owner = { has_notification_modifier = ghost_signal_1 }
				NOT = { has_modifier = "ship_ghost_signal_1" }
			}
			add_modifier = { modifier = "ship_ghost_signal_1" days = -1 }
			remove_modifier = ship_ghost_signal_5
			remove_modifier = ship_ghost_signal_4
			remove_modifier = ship_ghost_signal_3
			remove_modifier = ship_ghost_signal_2
		}
	}
}

# New Machine Empire Pop gets signal modifier
carrier_event = {
	id = crisis.2312
	hide_window = yes

	is_triggered_only = yes

	trigger = {
		fromfrom = {
			has_trait = trait_machine_unit
			owner = {
				OR = {
					has_notification_modifier = "ghost_signal_5_machine"
					has_notification_modifier = "ghost_signal_4_machine"
				}
			}
		}
	}

	immediate = {
		fromfrom = {
			if = {
				limit = { owner = { has_notification_modifier = "ghost_signal_5_machine" } }
				add_modifier = { modifier = "pop_ghost_signal_5_machine" days = -1 }
			}
			else_if = {
				limit = { owner = { has_notification_modifier = "ghost_signal_4_machine" } }
				add_modifier = { modifier = "pop_ghost_signal_4_machine" days = -1 }
			}
		}
	}
}

# AI Diplomacy
country_event = {
	id = crisis.2300
	title = "TRANSMISSION"

	desc = {
		text = crisis.2300.desc_01
	}
	desc = {
		text = crisis.2300.desc_02
		trigger = {
			NOR = {
				is_machine_empire = yes
				has_country_flag = synthetic_empire
				is_individual_machine = yes
			}
		}
	}
	desc = {
		text = crisis.2300.desc_03
		trigger = {
			NOR = {
				is_machine_empire = yes
				has_country_flag = synthetic_empire
				is_individual_machine = yes
			}
		}
	}
	desc = {
		text = crisis.2300.desc_04
	}
	desc = {
		text = crisis.2300.desc_05
		trigger = {
			OR = {
				is_machine_empire = yes
				has_country_flag = synthetic_empire
				is_individual_machine = yes
			}
		}
	}
	desc = {
		text = crisis.2300.desc_06
		trigger = {
			OR = {
				is_machine_empire = yes
				has_country_flag = synthetic_empire
				is_individual_machine = yes
			}
		}
	}

	diplomatic = yes

	picture_event_data = {
		portrait = ai_crisis_1
	}

	is_triggered_only = yes

	trigger = {
		FROM = {
			is_country_type = ai_empire
		}
		NOT = { has_country_flag = contingency_diplomacy_engaged }
	}

	immediate = {
		set_country_flag = contingency_diplomacy_engaged
	}

	after = {
		hidden_effect = {
			remove_country_flag = contingency_diplomacy_engaged
		}
	}

	option = {
		name = crisis.2300.a
		is_dialog_only = yes
		response_text = crisis.2300.a.response
	}
	option = {
		name = crisis.2300.b
		trigger = {
			OR = {
				is_machine_empire = yes
				has_country_flag = synthetic_empire
				is_individual_machine = yes
			}
		}
		is_dialog_only = yes
		response_text = crisis.2300.b.response
	}
	option = {
		name = crisis.2300.c
		is_dialog_only = yes
		response_text = crisis.2300.c.response
	}
	option = {
		name = crisis.2300.d
		is_dialog_only = yes
		response_text = crisis.2300.d.response
	}
	option = {
		name = crisis.2300.e
		trigger = {
			is_materialist = yes
		}
		is_dialog_only = yes
		response_text = crisis.2300.e.response
	}
	option = {
		name = crisis.2300.f
		response_text = crisis.2300.f.response
	}
}

# Contingency Kill Count (HIDDEN)
country_event = {
	id = crisis.2070
	hide_window = yes

	is_triggered_only = yes

	trigger = {
		is_country_type = ai_empire
	}

	immediate = {
		if = {
			limit = {
				FROM = { has_event_chain = "ai_crisis_chain" }
			}
			FROM = {
				add_event_chain_counter = {
					event_chain = "ai_crisis_chain"
					counter = "contingency_kills_us"
					amount = 1
				}
			}
		}
		every_country = {
			limit = {
				has_event_chain = "ai_crisis_chain"
				NOT = { is_same_value = FROM }
			}
			add_event_chain_counter = {
				event_chain = "ai_crisis_chain"
				counter = "contingency_kills_others"
				amount = 1
			}
		}
	}
}

# Contingency Victims (HIDDEN)
country_event = {
	id = crisis.2071
	hide_window = yes

	is_triggered_only = yes

	trigger = {
		is_country_type = ai_empire
	}

	immediate = {
		every_country = {
			limit = { has_event_chain = "ai_crisis_chain" }
			add_event_chain_counter = {
				event_chain = "ai_crisis_chain"
				counter = "contingency_victims"
				amount = 1
			}
		}
	}
}

### Machine Empire Post-Arrival Events

# Warning Signs

country_event = {
	id = crisis.2313
	title = "crisis.2313.name"
	desc = "crisis.2313.desc"
	picture = GFX_evt_glitchy_matrix
	show_sound = event_ai_started
	location = root

	is_triggered_only = yes

	trigger = {
		is_country_type = default
	}

	immediate = {
		country_event = { id = crisis.2314 days = 120 }
		#country_event = { id = crisis.2314 days = 1 random = 1 }
	}

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

country_event = {
	id = crisis.2314
	title = "crisis.2314.name"
	desc = "crisis.2314.desc"
	picture = GFX_evt_glitchy_matrix
	show_sound = event_red_alert
	location = root

	is_triggered_only = yes

	trigger = {
		is_country_type = default
	}

	immediate = {
		set_country_flag = post_arrival_events_machine
	}

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

# Pulse Event
event = {
	id = crisis.2310
	hide_window = yes

	is_triggered_only = yes

	trigger = {
		any_country = {
			has_global_flag = ai_ghost_signal
			OR = {
				has_notification_modifier = ghost_signal_4_machine
				has_notification_modifier = ghost_signal_5_machine
			}
			has_country_flag = post_arrival_events_machine
		}
	}

	immediate = {
		every_country = {
			limit = {
				is_country_type = default
				OR = {
					has_notification_modifier = ghost_signal_4_machine
					has_notification_modifier = ghost_signal_5_machine
				}
			}
			random_list = {
				30 = {
					country_event = { id = crisis.2311 days = 1 random = 300 }
					#country_event = { id = crisis.2311 days = 1 random = 1 }
				}
				70 = {}
			}
		}
	}
}

country_event = {
	id = crisis.2311
	hide_window = yes

	is_triggered_only = yes

	trigger = {
		is_country_type = default
		OR = {
			has_notification_modifier = ghost_signal_4_machine
			has_notification_modifier = ghost_signal_5_machine
		}
	}

	immediate = {
		random_list = {
			10 = {
				country_event = { id = crisis.2301 days = 1 random = 300 }
				#country_event = { id = crisis.2301 days = 1 random = 1 }
			}
			10 = {
				country_event = { id = crisis.2302 days = 1 random = 300 }
				#country_event = { id = crisis.2302 days = 1 }
			}
			10 = {
				country_event = { id = crisis.2303 days = 1 random = 300 }
				#country_event = { id = crisis.2303 days = 1 }
			}
			10 = {
				country_event = { id = crisis.2304 days = 1 random = 300 }
				#country_event = { id = crisis.2304 days = 1 }
			}
			10 = {
				country_event = { id = crisis.2305 days = 1 random = 300 }
				#country_event = { id = crisis.2305 days = 1 }
			}
			10 = {
				country_event = { id = crisis.2307 days = 1 random = 300 }
				#country_event = { id = crisis.2307 days = 1 }
			}
		}
	}
}

# Spaceport Hijacked Machine Empire
country_event = {
	id = crisis.2301
	title = "crisis.2301.name"
	desc = "crisis.2301.desc"
	picture = GFX_evt_sabotaged_ship
	show_sound = event_ship_explosion
	location = root

	is_triggered_only = yes

	trigger = {
		any_owned_planet = {
			controller = { is_same_value = root }
			solar_system = {
				exists = starbase
				starbase = { owner = { is_same_value = root } }
			}
		}
		NOT = {
			has_country_flag = machine_spaceport_scuttled
		}
		OR = {
			has_global_flag = ghost_signal_5_flag
			has_global_flag = ghost_signal_4_flag
		}
		is_machine_empire = yes
		is_nomadic = no
	}

	immediate = {
		set_country_flag = machine_spaceport_scuttled
		random_owned_planet = {
			limit = {
				controller = { is_same_value = root }
			}
			save_event_target_as = space_port_hijacked_planet
			solar_system = {
				starbase = { fleet = { destroy_fleet = this } }
			}
		}
	}

	option = {
		name = crisis.2092.a
		custom_tooltip = spaceport_hijacked
	}
}


# Scientist Terminated
country_event = {
	id = crisis.2302
	title = "crisis.2302.name"
	desc = "crisis.2302.desc"
	picture = GFX_evt_synth_sabotage
	show_sound = event_robo

	is_triggered_only = yes

	trigger = {
		any_owned_leader = {
			leader_class = scientist
			NOT = { has_leader_flag = leader_death_events_blocked }
		}
		NOT = { has_country_flag = machine_scientist_terminated }
	}

	immediate = {
		random_owned_leader = {
			limit = {
				leader_class = scientist
				NOT = { has_leader_flag = leader_death_events_blocked }
			}
			save_event_target_as = scientist
			kill_leader = { show_notification = no }
		}
		set_country_flag = machine_scientist_terminated
	}

	option = {
		name = crisis.2302.a
		custom_tooltip = scientist_terminated
	}
}

# Admiral Terminated
country_event = {
	id = crisis.2303
	title = "crisis.2303.name"
	desc = "crisis.2303.desc"
	picture = GFX_evt_synth_sabotage
	show_sound = event_robo

	is_triggered_only = yes

	trigger = {
		any_owned_leader = { leader_class = commander }
		NOT = { has_country_flag = machine_admiral_terminated }
	}

	immediate = {
		random_owned_leader = {
			limit = { leader_class = commander }
			save_event_target_as = admiral
			kill_leader = { show_notification = no }
		}
		set_country_flag = machine_admiral_terminated
	}

	option = {
		name = crisis.2303.a
		custom_tooltip = admiral_terminated
	}
}

# Power Failure
country_event = {
	id = crisis.2304
	title = "crisis.2304.name"
	desc = "crisis.2304.desc"
	picture = GFX_evt_nuclear_explosion
	show_sound = event_super_explosion
	location = root

	is_triggered_only = yes

	trigger = {
		NOT = {
			has_country_flag = machine_power_failure
		}
		any_owned_planet = {
			num_districts = {
				type = district_generator
				value > 3
			}
		}
	}

	immediate = {
		set_country_flag = machine_power_failure
		while = {
			count = 3
			random_owned_planet = {
				limit = {
					num_districts = {
						type = district_generator
						value > 3
					}
					NOT = { has_modifier = contingency_sabotage }
				}
				add_modifier = {
					modifier = "contingency_sabotage"
					days = 1800
				}
			}
		}
	}

	option = {
		name = crisis.2304.a
		custom_tooltip = machine_power_failure_2
	}
}

# Minerals NOT sabotaged
country_event = {
	id = crisis.2305
	title = "crisis.2305.name"
	desc = "crisis.2305.desc"
	picture = GFX_evt_synth_sabotage
	show_sound = event_robo
	location = root

	is_triggered_only = yes

	trigger = {
		NOT = {
			has_country_flag = machine_mineral_failure
		}
	}

	immediate = {
		set_country_flag = machine_mineral_failure
	}

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


# Corrupted Units
country_event = {
	id = crisis.2307
	title = "crisis.2307.name"
	desc = "crisis.2307.desc"
	picture = GFX_evt_glitchy_matrix
	show_sound = event_robo
	location = capital_scope

	is_triggered_only = yes

	trigger = {
		any_owned_planet = {
			controller = { is_same_value = root }
			any_owned_pop_group = { has_trait = trait_machine_unit }
		}
		NOT = { has_country_flag = potentially_corrupted_units }
	}

	immediate = {
		set_country_flag = potentially_corrupted_units
	}

	option = {
		name = crisis.2307.a
	}
	option = {
		name = crisis.2307.b
		custom_tooltip = potentially_corrupted_units
		hidden_effect = {
			random_list = {
				65 = { country_event = { id = crisis.2308 days = 120 } }
				35 = { country_event = { id = crisis.2309 days = 120 } }
				#65 = { country_event = { id = crisis.2308 days = 1 } }
				#35 = { country_event = { id = crisis.2309 days = 1 } }
			}
		}
	}
}

# Corrupted Units: Good Consequences
country_event = {
	id = crisis.2308
	title = "crisis.2308.name"
	desc = "crisis.2308.desc"
	picture = GFX_evt_circuitry_modification
	show_sound = event_robo

	is_triggered_only = yes

	option = {
		name = EXCELLENT
	}
}

# Corrupted Units: Bad Consequences
country_event = {
	id = crisis.2309
	title = "crisis.2309.name"
	desc = "crisis.2309.desc"
	picture = GFX_evt_glitchy_matrix
	show_sound = event_robo
	location = from

	is_triggered_only = yes

	trigger = {
		any_owned_planet = {
			controller = { is_same_value = root }
			any_owned_pop_group = { has_trait = trait_machine_unit }
		}
	}

	immediate = {
		while = {
			count = 4
			random_owned_planet = {
				limit = {
					any_owned_pop_group = { has_trait = trait_machine_unit }
				}
				random_owned_pop_group = {
					limit = { has_trait = trait_machine_unit }
					kill_single_pop = yes
				}
				add_planet_devastation = 10
			}
		}
	}

	option = {
		name = crisis.2309.a
		custom_tooltip = crisis.2309.tooltip_2
	}
}

### Synth Terror Events

# Pulse Event
event = {
	id = crisis.2080
	hide_window = yes

	is_triggered_only = yes

	trigger = {
		has_global_flag = synth_terror
	}

	immediate = {
		every_country = {
			limit = {
				is_country_type = default
				has_country_flag = synth_terror_country
				NOT = { has_country_flag = synth_detector }
				owner_species = { is_psionic_species = no }
				NOR = {
					is_hive_empire = yes
					is_machine_empire = yes
				}
			}
			random_list = {
				30 = {
					country_event = { id = crisis.2081 days = 1 random = 300 }
				}
				70 = {}
			}
		}
	}
}

# Synth Terror
country_event = {
	id = crisis.2081
	hide_window = yes

	is_triggered_only = yes

	trigger = {
		has_global_flag = synth_terror
		has_country_flag = synth_terror_country
		NOT = { has_country_flag = synth_detector }
	}

	immediate = {
		random_list = {
			40 = { # Synth Bombing (Can happen multiple times)
				random_owned_planet = {
					limit = {
						controller = { is_same_value = root }
					}
					carrier_event = { id = crisis.2091 }
				}
			}
			10 = { # Spaceport Scuttle Simulator (Light Version)
				random_owned_planet = {
					limit = {
						controller = { is_same_value = root }
					}
					carrier_event = { id = crisis.2092 }
				}
			}
			10 = { # Fear and Suspicion
				random_owned_planet = {
					limit = {
						controller = { is_same_value = root }
					}
					carrier_event = { id = crisis.2093 }
				}
			}
			10 = { # Ruler Assassination Attempt
				country_event = { id = crisis.2094 }
			}
			10 = { # Scientist Exposed as Synth
				country_event = { id = crisis.2097 }
			}
			10 = { # Admiral Exposed as Synth
				country_event = { id = crisis.2098 }
			}
		}
	}
}

# Synth Infiltration
country_event = {
	id = crisis.2085
	title = "crisis.2085.name"
	desc = "crisis.2085.desc"
	picture = GFX_evt_robot_assembly_plant
	show_sound = event_robo

	is_triggered_only = yes

	trigger = {
		any_owned_planet = {
			exists = controller
			controller = {
				is_same_value = root
			}
		}
	}

	immediate = {
		random_owned_planet = {
			limit = { controller = { is_same_value = root } }
			save_event_target_as = infil_planet
		}
		country_event = { id = crisis.2090 days = 120 }
	}

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

# Synth Infiltration (Hive Mind)
country_event = {
	id = crisis.2086
	title = "crisis.2085.name"
	desc = {
		trigger = { is_wilderness_empire = no }
		text = crisis.2086.desc
	}
	desc = {
		trigger = { is_wilderness_empire = yes }
		text = crisis.2086.wilderness.desc
	}
	picture = GFX_evt_robot_assembly_plant
	show_sound = event_robo

	is_triggered_only = yes

	immediate = {
		random_owned_planet = {
			limit = { controller = { is_same_value = root } }
			save_event_target_as = infil_planet
		}
	}

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

# Synth Infiltration (Psionic)
country_event = {
	id = crisis.2087
	title = "crisis.2085.name"
	desc = {
		trigger = {
			is_robot_empire = no
		}
		text = crisis.2087.desc.organic
	}
	desc = {
		trigger = {
			is_robot_empire = yes
		}
		text = crisis.2087.desc.machine
	}
	picture = GFX_evt_robot_assembly_plant
	show_sound = event_robo

	is_triggered_only = yes

	immediate = {
		random_owned_planet = {
			limit = { controller = { is_same_value = root } }
			save_event_target_as = infil_planet
		}
	}

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

# Second Infiltration Attempt
country_event = {
	id = crisis.2090
	title = "crisis.2090.name"
	desc = "crisis.2090.desc"
	picture = GFX_evt_cybernetics
	show_sound = event_robo

	is_triggered_only = yes

	immediate = {
		random_owned_planet = {
			limit = { controller = { is_same_value = root } }
			save_event_target_as = infil_planet2
		}
		set_country_flag = synth_terror_country
	}

	option = {
		name = crisis.2090.a
		hidden_effect = {
			country_event = { id = crisis.2202 days = 200 random = 200 }
		}
	}
}

# Synth Bombing
carrier_event = {
	id = crisis.2091
	title = "crisis.2091.name"
	desc = { text = crisis.2091.a.desc }
	desc = { text = crisis.2091.b.desc }
	desc = { text = crisis.2091.c.desc }
	desc = { text = crisis.2091.d.desc }

	picture = GFX_evt_nuclear_explosion
	show_sound = event_super_explosion
	location = root

	is_triggered_only = yes

	pre_triggers = {
		has_ground_combat = no
	}


	option = {
		name = crisis.2091.a
		owner = {
			if = {
				limit = {
					NOT = { has_modifier = unity_reduction_crisis_2204 }
				}
				add_modifier = {
					modifier = unity_reduction_crisis_2204
					days = 1800
				}
			}
		}
		while = {
			count = 3
			random_owned_pop_group = { kill_single_pop = yes }
		}
		add_planet_devastation = 20
	}
}

# Spaceport Scuttled
carrier_event = {
	id = crisis.2092
	title = "crisis.2092.name"
	desc = "crisis.2092.desc"
	picture = GFX_evt_sabotaged_ship
	show_sound = event_ship_explosion
	location = root

	is_triggered_only = yes

	pre_triggers = {
		has_ground_combat = no
	}

	trigger = {
		owner = {
			is_nomadic = no
		}
	}

	trigger = {
		solar_system = {
			exists = starbase
			starbase = {
				owner = { is_same_value = root.owner }
			}
		}
		NOT = {
			owner = { has_country_flag = synth_spaceport_bombing }
		}
	}

	immediate = {
		owner = { set_country_flag = synth_spaceport_bombing }
		solar_system = {
			starbase = { fleet = { destroy_fleet = this } }
		}
	}

	option = {
		name = crisis.2092.a
		custom_tooltip = synths_destroy_spaceport
	}
}

# Fear and Suspicion
carrier_event = {
	id = crisis.2093
	title = "crisis.2093.name"
	desc = "crisis.2093.desc"
	picture = GFX_evt_alien_propaganda
	show_sound = event_planetary_riot
	location = root

	is_triggered_only = yes

	pre_triggers = {
		has_ground_combat = no
	}

	trigger = {
		NOT = {
			owner = { has_country_flag = synth_fear }
		}
	}

	immediate = {
		owner = { set_country_flag = synth_fear }
		add_modifier = {
			modifier = synth_planet_paranoia
			days = 7200 # 20 years
		}
	}

	option = {
		name = crisis.2093.a
		tooltip = {
			add_modifier = {
				modifier = synth_planet_paranoia
				days = 7200 # 20 years
			}
		}
	}
}

# Ruler Assassination Attempt
country_event = {
	id = crisis.2094
	hide_window = yes

	is_triggered_only = yes

	trigger = {
		NOT = { has_country_flag = synth_assassination_attempt }
	}

	immediate = {
		set_country_flag = synth_assassination_attempt
		leader = { save_event_target_as = ruler }
		random_list = {
			70 = { country_event = { id = crisis.2095 } }
			30 = { country_event = { id = crisis.2096 } }
		}
	}
}

# Ruler Assassination Attempt Fails
country_event = {
	id = crisis.2095
	title = "crisis.2095.name"
	desc = "crisis.2095.desc"
	picture = GFX_evt_synth_sabotage
	show_sound = event_robo

	is_triggered_only = yes

	option = {
		name = crisis.2095.a
		add_monthly_resource_mult = {
			resource = influence
			value = @tier2influencereward
			min = @tier2influencemin
			max = @tier2influencemax
		}
	}
}

# Ruler Assassination Attempt Succeeds
country_event = {
	id = crisis.2096
	title = "crisis.2096.name"
	desc = "crisis.2096.desc"
	picture = GFX_evt_organic_oppression
	show_sound = event_life_support_fail

	is_triggered_only = yes

	immediate = {
		kill_leader = {
			ruler = yes
			show_notification = no
		}
	}

	option = {
		name = crisis.2096.a
		custom_tooltip = synth_assassination_tooltip
	}
}

# Scientist exposed as Synth!
country_event = {
	id = crisis.2097
	title = "crisis.2097.name"
	desc = "crisis.2097.desc"
	picture = GFX_evt_interior_battle
	show_sound = event_robo

	is_triggered_only = yes

	trigger = {
		any_owned_leader = {
			leader_class = scientist
			is_leader_tier = leader_tier_default
			NOR = {
				has_leader_flag = leader_death_events_blocked
			}
		}
		NOT = { has_country_flag = synth_scientist_exposure }
	}

	immediate = {
		set_country_flag = synth_scientist_exposure
		random_owned_leader = {
			limit = {
				leader_class = scientist
				is_leader_tier = leader_tier_default
				NOR = {
					has_leader_flag = leader_death_events_blocked
				}
			}
			save_event_target_as = scientist
			kill_leader = { show_notification = no }
		}
	}

	option = {
		name = crisis.2097.a
		custom_tooltip = synths_kill_scientist
	}
}

# Admiral exposed as Synth!
country_event = {
	id = crisis.2098
	title = "crisis.2098.name"
	desc = "crisis.2098.desc"
	picture = GFX_evt_interior_battle
	show_sound = event_robo

	is_triggered_only = yes

	trigger = {
		any_owned_leader = {
			leader_class = commander
			is_leader_tier = leader_tier_default
		}
		NOT = { has_country_flag = synth_admiral_exposure }
	}

	immediate = {
		set_country_flag = synth_admiral_exposure
		random_owned_leader = {
			limit = {
				leader_class = commander
				is_leader_tier = leader_tier_default
			}
			save_event_target_as = admiral
			kill_leader = { show_notification = no }
		}
	}

	option = {
		name = crisis.2098.a
		custom_tooltip = synths_kill_admiral
	}
}

# Sapient Combat Computers Malfunction
country_event = {
	id = crisis.2099
	title = "crisis.2099.name"
	desc = "crisis.2099.desc"
	picture = GFX_evt_glitchy_matrix
	show_sound = event_robo

	is_triggered_only = yes

	trigger = {
		any_controlled_ship = {
			has_sapient_combat_computer = yes
		}
	}

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

# Synth Detection Project
country_event = {
	id = crisis.2202
	title = "crisis.2202.name"
	desc = "crisis.2202.desc"
	picture = GFX_evt_synth_sabotage
	show_sound = event_laboratory_sound
	trackable = yes

	trigger = {
		has_global_flag = synth_terror
		has_country_flag = synth_terror_country
		NOR = {
			has_country_flag = synth_detection_proj
			has_country_flag = synth_detector
		}
	}

	mean_time_to_happen = {
		months = 320
	}

	immediate = {
		set_country_flag = synth_detection_proj
	}

	option = {
		name = crisis.2202.a
		if = {
			limit = { has_technology = tech_decryption_3 }
			custom_tooltip = tooltip_synth_crisis_encryption_bonus
		}
		capital_scope = {
			enable_special_project = {
				name = "SYNTH_INFILTRATION_PROJECT"
				location = this
				owner = root
			}
		}
	}
}

# Project Completed
country_event = {
	id = crisis.2203
	title = "PROJECT_COMPLETE"
	desc = "crisis.2203.desc"
	picture = GFX_evt_circuitry_modification
	show_sound = event_scanner

	is_triggered_only = yes

	immediate = {
		set_country_flag = synth_detector
	}

	option = {
		name = EXCELLENT
		add_monthly_resource_mult = {
			resource = unity
			value = @tier3unityreward
		}
	}
}


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

	trigger = {
		has_crisis_stage_3 = yes
		is_country_type = ai_empire
		galaxy_percentage < 0.15
	}

	mean_time_to_happen = {
		months = 1
	}

	immediate = {
		set_crisis_sound = ai_empire_ambient_stage_2
		set_crisis_stage_2 = yes
	}
}

country_event = {
	id = crisis.2231
	hide_window = yes

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

	mean_time_to_happen = {
		months = 1
	}

	immediate = {
		set_crisis_sound = ai_empire_ambient_stage_3
		set_crisis_stage_3 = yes
	}
}

country_event = {
	id = crisis.2232
	hide_window = yes

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

	mean_time_to_happen = {
		months = 1
	}

	immediate = {
		set_crisis_sound = ai_empire_ambient_stage_3
		set_crisis_stage_3 = yes
	}
}

country_event = {
	id = crisis.2233
	hide_window = yes

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

	mean_time_to_happen = {
		months = 1
	}

	immediate = {
		set_crisis_sound = ai_empire_ambient_stage_final
		set_crisis_stage_4 = yes
	}
}

country_event = {
	id = crisis.2234
	hide_window = yes

	trigger = {
		has_crisis_stage_1 = yes
		is_country_type = ai_empire
		galaxy_percentage > 0.05
	}

	mean_time_to_happen = {
		months = 1
	}

	immediate = {
		set_crisis_sound = ai_empire_ambient_stage_2
		set_crisis_stage_2 = yes
	}
}

# Spawn Cybrex
country_event = {
	id = crisis.2400
	hide_window = yes

	trigger = {
		has_global_flag = ai_invasion_ongoing
		NOT = { has_global_flag = cybrex_resurface }
		is_country_type = ai_empire
		endgame_crisis_large_size_trigger = yes
	}

	mean_time_to_happen = {
		months = 12
	}

	immediate = {
		set_global_flag = cybrex_resurface
		random_rim_system = {
			spawn_system = {
				min_distance >= 20
				max_distance <= 50
				initializer = "cybrex_beta"
			}
		}
		observer_event = { id = observer.52 }
		every_country = {
			limit = { is_country_type = default }
			country_event = { id = crisis.2401 days = 2 }
		}
		random_country = {
			limit = { is_country_type = cybrex_empire }
			country_event = { id = crisis.2421 days = 4 }
			country_event = { id = crisis.2421 days = 120 }
			country_event = { id = crisis.2421 days = 320 }
			establish_communications_no_message = root
		}
	}
}

# Cybrex Notification
country_event = {
	id = crisis.2401
	title = "crisis.2401.name"
	desc = "crisis.2401.desc"
	picture = GFX_evt_physics_research
	show_sound = event_red_alert
	location = event_target:cybrex_beta

	is_triggered_only = yes

	option = {
		name = ONSCREEN
		hidden_effect = {
			country_event = { id = crisis.2402 }
			establish_communications_no_message = event_target:cybrex
			event_target:cybrex_beta = {
				every_system_planet = {
					set_surveyed = {
						surveyed = yes
						surveyor = root
					}
				}
			}
		}
	}
}

# Incoming Transmission
country_event = {
	id = crisis.2402
	title = "TRANSMISSION"
	desc = "crisis.2402.desc"

	diplomatic = yes

	picture_event_data = {
		portrait = ai_crisis_2
	}

	is_triggered_only = yes

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

# Cybrex Diplomacy
country_event = {
	id = crisis.2403
	title = "TRANSMISSION"
	desc = "crisis.2403.desc_01"
	desc = "crisis.2403.desc_02"
	desc = "crisis.2403.desc_03"
	desc = "crisis.2403.desc_04"
	desc = "crisis.2403.desc_05"
	desc = {
		trigger = {
			has_relic = r_war_forge
		}
		text = "crisis.2403.desc_06"
	}

	diplomatic = yes

	picture_event_data = {
		portrait = ai_crisis_2
	}

	is_triggered_only = yes

	trigger = {
		FROM = {
			is_country_type = cybrex_empire
		}
		NOT = { has_country_flag = cybrex_diplomacy_engaged }
	}

	immediate = {
		set_country_flag = cybrex_diplomacy_engaged
	}

	after = {
		hidden_effect = {
			remove_country_flag = cybrex_diplomacy_engaged
		}
	}

	option = {
		name = crisis.2403.a
		is_dialog_only = yes
		response_text = crisis.2403.a.response
	}
	option = {
		name = crisis.2403.b
		is_dialog_only = yes
		response_text = crisis.2403.b.response
	}
	option = {
		name = crisis.2403.d
		trigger = {
			has_valid_civic = civic_fanatic_purifiers
		}
		is_dialog_only = yes
		response_text = crisis.2403.d.response
	}
	option = {
		name = crisis.2403.e
		trigger = {
			has_valid_civic = civic_hive_devouring_swarm
		}
		is_dialog_only = yes
		response_text = crisis.2403.e.response
	}
	option = {
		name = crisis.2403.f
		trigger = {
			has_valid_civic = civic_machine_terminator
		}
		is_dialog_only = yes
		response_text = crisis.2403.f.response
	}
	option = {
		name = crisis.2403.c
		response_text = crisis.2403.c.response
	}
}

# Cybrex Destroyed (HIDDEN)
country_event = {
	id = crisis.2404
	hide_window = yes

	is_triggered_only = yes

	trigger = {
		is_country_type = cybrex_empire
		NOT = { has_global_flag = cybrex_departed }
	}

	immediate = {
		observer_event = { id = observer.53 }
		every_country = {
			limit = { is_country_type = default }
			country_event = { id = crisis.2405 }
		}
		From = { save_event_target_as = killer }
		set_global_flag = cybrex_destroyed
	}
}

# Cybrex Destroyed
country_event = {
	id = crisis.2405
	title = "crisis.2405.name"
	desc = "crisis.2405.desc"
	picture = GFX_evt_city_ruins
	show_sound = event_ghost_town
	location = event_target:cybrex_beta

	is_triggered_only = yes

	option = {
		name = DISCONCERTING
		trigger = {
			NOT = { is_same_value = event_target:killer }
		}
	}
	option = {
		name = crisis.2405.b
		trigger = {
			is_same_value = event_target:killer
		}
	}
}

# Cybrex Depart (HIDDEN)
country_event = {
	id = crisis.2406
	hide_window = yes

	is_triggered_only = yes

	trigger = {
		is_country_type = cybrex_empire
	}

	immediate = {
		set_global_flag = cybrex_departed
		observer_event = { id = observer.54 }
		every_country = {
			limit = { is_country_type = default }
			country_event = { id = crisis.2407 }
		}
		random_country = { # Triggered this way to avoid game over when testing
			limit = { is_country_type = cybrex_empire }
			every_owned_pop_group = { kill_all_pop = yes }
			every_controlled_fleet = { delete_fleet = this }
			every_controlled_planet = {
				remove_all_buildings = yes
			}
			every_controlled_planet = {
				limit = { is_planet_class = pc_cybrex }
				change_pc = pc_ringworld_habitable
				reset_planet = yes
			}
			destroy_country = yes
		}
	}
}

# Cybrex Depart
country_event = {
	id = crisis.2407
	title = "crisis.2407.name"
	desc = "crisis.2407.desc"
	picture = GFX_evt_ringworld
	show_sound = event_ghost_town
	location = event_target:cybrex_beta

	is_triggered_only = yes

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

# Incoming Transmission
country_event = {
	id = crisis.2408
	title = "TRANSMISSION"
	desc = "crisis.2408.desc"

	diplomatic = yes

	picture_event_data = {
		portrait = ai_crisis_2
		room = ethic_spaceship_room
	}

	is_triggered_only = yes

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

# Cybrex Reinforcements (HIDDEN)
country_event = {
	id = crisis.2420
	hide_window = yes

	trigger = {
		has_global_flag = ai_invasion_ongoing
		is_country_type = cybrex_empire
		num_ships < 300
	}

	mean_time_to_happen = {
		months = 100
	}

	immediate = {
		random_country = {
			limit = { is_country_type = ai_empire }
			save_event_target_as = contingency
		}
		create_leader = {
			class = commander
			species = owner_main_species
			name = random
			skill = 5
			traits = {
				1 = leader_trait_cybrex
			}
		}
		last_created_leader = { set_age = 0 }
		capital_scope = {
			save_event_target_as = cybrex_home
			create_fleet = {
				effect = {
					set_owner = event_target:cybrex
					while = {
						count = 10
						create_ship = {
							name = random
							design = "NAME_Taciturn"
							graphical_culture = "ai_01"
						}
					}
					assign_leader = last_created_leader
					while = {
						count = 20
						create_ship = {
							name = random
							design = "NAME_Reticent"
							graphical_culture = "ai_01"
						}
					}
					set_location = {
						target = prev
						distance = 45
						angle = random
					}

					save_event_target_as = cybrex_fleet
					set_aggro_range = 500
					set_fleet_stance = aggressive
					set_aggro_range_measure_from = self
					set_fleet_flag = cybrex_attack_fleet
				}
			}
		}
	}
}

# Cybrex First Attacks (HIDDEN)
country_event = {
	id = crisis.2421
	hide_window = yes

	is_triggered_only = yes

	trigger = {
		has_global_flag = ai_invasion_ongoing
	}

	immediate = {
		random_country = {
			limit = { is_country_type = ai_empire }
			save_event_target_as = contingency
		}
		create_leader = {
			class = commander
			species = owner_main_species
			name = random
			skill = 5
			traits = {
				1 = leader_trait_cybrex
			}
		}
		last_created_leader = { set_age = 0 }
		capital_scope = {
			save_event_target_as = cybrex_home
			create_fleet = {
				effect = {
					set_owner = event_target:cybrex
					while = {
						count = 10
						create_ship = {
							name = random
							design = "NAME_Taciturn"
							graphical_culture = "ai_01"
						}
					}
					assign_leader = last_created_leader
					while = {
						count = 20
						create_ship = {
							name = random
							design = "NAME_Reticent"
							graphical_culture = "ai_01"
						}
					}
					set_location = {
						target = prev
						distance = 45
						angle = random
					}

					save_event_target_as = cybrex_fleet
					set_aggro_range = 500
					set_fleet_stance = aggressive
					set_aggro_range_measure_from = self
					set_fleet_flag = cybrex_attack_fleet
				}
			}
		}
	}
}

# Contingency Kill Count
country_event = {
	id = crisis.2472
	hide_window = yes

	is_triggered_only = yes

	trigger = {
		exists = from
		has_global_flag = cybrex_resurface
		NOT = { has_global_flag = cybrex_destroyed }
		is_country_type = default
		FROM = { is_country_type = ai_empire }
	}

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

# Fleet Donation
country_event = {
	id = crisis.2473
	title = "TRANSMISSION"
	desc = "crisis.2473.desc"

	diplomatic = yes

	picture_event_data = {
		portrait = event_target:cybrex
		room = event_target:cybrex
	}

	trigger = {
		is_ai = no
		has_global_flag = cybrex_resurface
		NOT = { has_global_flag = cybrex_destroyed }
		NOT = { has_country_flag = cybrex_donation }
		check_variable = {
			which = "contingency_fleet_kills"
			value > 100
		}
		any_country = {
			is_country_type = cybrex_empire
			count_controlled_ship = {
				count > 30
			}
			ROOT = {
				NOT = { is_at_war_with = PREV }
			}
		}
	}

	mean_time_to_happen = {
		months = 22
	}

	immediate = {
		set_country_flag = cybrex_donation
	}

	option = {
		name = crisis.2473.a
		custom_tooltip = cybrex_fleet_gained
		hidden_effect = {
			capital_scope = {
				create_fleet = {
					effect = {
						set_owner = event_target:cybrex # For Cybrex ship names and difficulty modifiers
						while = {
							count = 10
							create_ship = {
								name = random
								design = "NAME_Taciturn"
								graphical_culture = "ai_01"
							}
						}
						while = {
							count = 20
							create_ship = {
								name = random
								design = "NAME_Reticent"
								graphical_culture = "ai_01"
							}
						}
						set_location = {
							target = prev
							distance = 45
							angle = random
						}
						set_owner = root
					}
					settings = {
						can_change_composition = no
						uses_naval_capacity = no
						can_disband = no
					}
				}
			}
		}
	}
	option = {
		name = crisis.2473.b
	}
}

# Fleet Recall
country_event = {
	id = crisis.2474
	title = "TRANSMISSION"
	desc = "crisis.2474.desc"

	diplomatic = yes

	picture_event_data = {
		portrait = event_target:cybrex
		room = event_target:cybrex
	}

	is_triggered_only = yes

	trigger = {
		any_controlled_ship = {
			OR = {
				is_ship_size = large_ship_ai
				is_ship_size = small_ship_ai
			}
		}
	}

	immediate = {
		set_country_flag = cybrex_donation
		every_controlled_ship = {
			limit = {
				OR = {
					is_ship_size = large_ship_ai
					is_ship_size = small_ship_ai
				}
			}
			delete_ship = this
		}
	}

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

# Cybrex Planet Destroyed (HIDDEN)
carrier_event = {
	id = crisis.2475
	hide_window = yes

	is_triggered_only = yes

	trigger = {
		planet_devastation >= 100
		is_planet_class = pc_cybrex
	}

	immediate = {
		destroy_colony = yes
		change_pc = pc_ringworld_habitable
		reset_planet = yes
	}
}

# Cybrex Destroy Planet (HIDDEN)
carrier_event = {
	id = crisis.2476
	hide_window = yes

	is_triggered_only = yes

	pre_triggers = {
		has_owner = yes
		has_ground_combat = no
	}

	trigger = {
		exists = owner
		owner = { is_country_type = ai_empire }
		planet_devastation >= 100
		NOT = { is_planet_class = pc_ai }
		from = { is_country_type = cybrex_empire }
	}

	immediate = {
		destroy_colony = yes
	}
}

# Mainframe Core Salvaged
country_event = {
	id = crisis.2480
	title = "crisis.2480.name"
	desc = crisis.2480.desc
	picture = GFX_evt_shattered_planet
	show_sound = event_mystic_reveal

	is_triggered_only = yes

	option = {
		name = GOOD
		add_relic = r_contingency_core
	}
}

# Mainframe Core captured (alternate event for shielding the last planet)
country_event = {
	id = crisis.2481
	title = "crisis.2480.name"
	desc = "crisis.2481.desc"
	picture = GFX_evt_satellite_in_orbit
	show_sound = event_mystic_reveal

	is_triggered_only = yes

	option = {
		name = GOOD
		add_relic = r_contingency_core
	}
}

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

	is_triggered_only = yes

	immediate = {
		solar_system = {
			if = {
				limit = {
					NOT = {
						any_system_ambient_object = { has_ambient_object_flag = contingency_system_effect }
					}
				}
				star = {
					create_ambient_object = {
						type = "contingency_1"
						location = this
					}
					last_created_ambient_object = {
						set_ambient_object_flag = contingency_system_effect
						set_location = {
							target = prev
							distance = 0
							angle = random
						}
					}
				}
			}
		}
	}
}

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

	is_triggered_only = yes

	trigger = {
		is_country_type = ai_empire
		fromfrom = {
			is_ship_size = starbase_ai
		}
	}

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

# Contingency Destroyed Starbase
starbase_event = {
	id = crisis.2610
	hide_window = yes

	is_triggered_only = yes

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

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

# Contingency Builds New Starbase
country_event = {
	id = crisis.2611
	hide_window = yes

	is_triggered_only = yes

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

# Wipe Become the Crisis assets when integrated
country_event = {
	id = crisis.2620
	hide_window = yes

	is_triggered_only = yes

	trigger = {
		has_nemesis = yes
		from = { has_ascension_perk = ap_become_the_crisis }
	}

	immediate = {

		# ships are transferred before on-action fires;
		# as a fallback you keep BtC ships if you're somehow also a BtC empire
		if = {
			limit = { has_ascension_perk = ap_become_the_crisis }

			# Star-Eaters
			if = { limit = { has_crisis_perk = menp_star_eater } }
			else = {
				every_controlled_ship = {
					limit = { is_ship_size = star_eater }
					delete_ship = this
				}
			}
			# Cruisers
			if = { limit = { has_crisis_perk = menp_crisis_cruiser } }
			else = {
				every_controlled_ship = {
					limit = { is_ship_size = crisis_cruiser }
					delete_ship = this
				}
			}
			# Destroyers
			if = { limit = { has_crisis_perk = menp_crisis_destroyer } }
			else = {
				every_controlled_ship = {
					limit = { is_ship_size = crisis_destroyer }
					delete_ship = this
				}
			}
			# Corvettes
			if = { limit = { has_crisis_perk = menp_crisis_corvette } }
			else = {
				every_controlled_ship = {
					limit = { is_ship_size = crisis_corvette }
					delete_ship = this
				}
			}
		}
		else = {
			every_controlled_ship = {
				limit = {
					OR = {
						is_ship_size = star_eater
						is_ship_size = crisis_cruiser
						is_ship_size = crisis_destroyer
						is_ship_size = crisis_corvette
					}
				}
				delete_ship = this
			}
		}

		from = {
			# clean up Aetherophasic Engine from integrated empire
			every_owned_megastructure = {
				limit = {
					OR = {
						is_megastructure_type = crisis_sphere_4
						is_megastructure_type = crisis_sphere_3
						is_megastructure_type = crisis_sphere_2
						is_megastructure_type = crisis_sphere_1
						is_megastructure_type = crisis_sphere_0
					}
				}
				delete_megastructure = this
			}
			# and delete any remaining ships just in case
			every_controlled_ship = {
				limit = {
					OR = {
						is_ship_size = star_eater
						is_ship_size = crisis_cruiser
						is_ship_size = crisis_destroyer
						is_ship_size = crisis_corvette
					}
				}
				delete_ship = this
			}
		}
	}
}
