namespace = planet_destruction

#carrier_event = {
#	id = planet_destruction.1
#	is_triggered_only = yes
#	hide_window = yes
#
#	immediate = {
#		remove_planet = yes
#	}
#}

# Add static war exhaustion for these planet destruction events with:
# add_static_war_exhaustion = {
#				attacker = <country>						# attacker in this battle
#				location = <planet>							# location for battle
#				value_for_planet_destruction = <0.0-1.0>	# scale amount of WE added to this battle
#			}

# Cracked
carrier_event = {
	id = planet_destruction.100
	is_triggered_only = yes
	hide_window = yes

	immediate = {
		# Don't fire the colossus narrative events if the planet is destroyed by an arkship.
		# However we still use this event as it handles all of the relations and flags we need.
		if = {
			limit = {
				from = {
					is_arkship_ship = no
				}
			}
			if = {
				limit = {
					is_planet_class = pc_frozen
				}
				from.owner = { country_event = { id = planet_destruction.102 } }
			}
			else_if = {
				limit = {
					NOT = { is_planet_class = pc_ai }
				}
				from.owner = { country_event = { id = planet_destruction.101 } }
			}
		}

		#Dismantle observation post
		if = {
			limit = {
				has_observation_outpost = yes
			}
			observation_outpost_owner = {
				country_event = { id = planet_destruction.1000 days = 1 }
			}
		}

		# Contingency Machine World
		if = {
			limit = {
				is_planet_class = pc_ai
				NOT = { has_planet_flag = machine_lair }
			}
			set_carrier_flag = destroyed_by_colossus
			set_carrier_flag = planet_cracked
			carrier_event = { id = crisis.2040 }
		}

		# Contingency Final Machine World
		if = {
			limit = {
				is_planet_class = pc_ai
				has_planet_flag = machine_lair
			}
			set_carrier_flag = destroyed_by_colossus
			set_carrier_flag = planet_cracked
			from.owner = { save_event_target_as = final_machine_world_destroyer }
			stop_crisis_sound = yes
			carrier_event = { id = crisis.2046 }
		}

		# Swarm Situation Log counter
		if = {
			limit = {
				exists = owner
				owner = { is_country_type = swarm }
			}
			every_country = {
				limit = { has_event_chain = "prethoryn_scourge_chain" }
				add_event_chain_counter = {
					event_chain = "prethoryn_scourge_chain"
					counter = "infested_worlds"
					amount = -1
				}
				add_event_chain_counter = {
					event_chain = "prethoryn_scourge_chain"
					counter = "infested_worlds_cleansed"
					amount = 1
				}
			}
		}

		if = {
			limit = {
				OR = {
					is_active_resolution = "resolution_rulesofwar_reverence_for_life"
					is_active_resolution = "resolution_rulesofwar_independent_tribunals"
					is_active_resolution = "resolution_rulesofwar_last_resort_doctrine"
					is_active_resolution = "resolution_rulesofwar_demobilization_initiative"
				}
			}
			from.owner = {
				set_timed_country_flag = { flag = resolution_breached_fired_cracker days = 3600 }
			}
		}

		# Exterminatus Achievement
		if = {
			limit = { is_capital = yes }
			from.owner = { set_country_flag = exterminatus }
		}
		if = {
			limit = {
				exists = owner
				owner = {
					NOT = { is_same_value = from.owner }
					OR = {
						is_country_type = default
						is_country_type = fallen_empire
						is_country_type = awakened_fallen_empire
					}
				}
			}
			# Generate threat
			if = {
				limit = {
					is_colony = yes
				}
				add_threat = { who = from.owner amount = 3 }
				# modifier for allies + those upset by genocide
				every_country = {
					limit = {
						NOR = {
							is_same_value = from.owner
							is_same_value = root.owner
							AND = {
								has_federation = yes
								is_in_federation_with = from.owner
							}
						}
						OR = {
							has_communications = from.owner
							has_communications = root.owner
						}
						OR = {
							AND = {
								has_federation = yes
								is_in_federation_with = root.owner
							}
							has_ai_personality = awakened_fallen_empire_xenophile
							AND = {
								is_country_type = default
								OR = {
									is_egalitarian = yes
									is_xenophile = yes
								}
							}
						}
					}
					if = {
						limit = { root = { is_colony = no } }
						add_opinion_modifier = {
							modifier = opinion_cracked_an_uninhabited_world
							who = from.owner
						}
					}
					else = {
						add_opinion_modifier = {
							modifier = opinion_cracked_a_world
							who = from.owner
						}
					}
				}
			}
			else = {
				add_threat = { who = from.owner amount = 1 }
			}
			# modifiers for victim
			owner = {
				if = {
					limit = { root = { is_colony = no } }
					add_opinion_modifier = {
						modifier = opinion_cracked_my_uninhabited_world
						who = from.owner
					}
				}
				else = {
					add_opinion_modifier = {
						modifier = opinion_cracked_my_world
						who = from.owner
					}
				}
				if = {
					limit = { NOT = { has_ethic = ethic_gestalt_consciousness } }
					add_modifier = {
						modifier = colossus_victim
						days = 10800 # 30 years
					}
				}
			}
			if = {
				limit = {
					exists = owner
					owner = { is_country_type = primitive }
				}
				# modifier for allies + those upset by genocide
				every_country = {
					limit = {
						NOT = { is_same_value = from.owner }
						OR = {
							has_communications = from.owner
							has_communications = root.owner
						}
						OR = {
							has_ai_personality = awakened_fallen_empire_xenophile
							AND = {
								is_country_type = default
								NOR = {
									is_xenophobe = yes
									is_homicidal = yes
									is_xenophile = yes
								}
							}
						}
					}
					add_opinion_modifier = {
						modifier = opinion_cracked_a_primitive_world
						who = from.owner
					}
				}
				every_country = {
					limit = {
						NOT = { is_same_value = from.owner }
						OR = {
							has_communications = from.owner
							has_communications = root.owner
						}
						OR = {
							has_ai_personality = awakened_fallen_empire_xenophile
							AND = {
								is_country_type = default
								is_xenophile = yes
							}
						}
					}
					add_opinion_modifier = {
						modifier = opinion_cracked_a_primitive_world_phile
						who = from.owner
					}
				}
			}
		}
		# Add war exhaustion to planet owner
		if = {
			limit = {
				exists = owner
			}
			owner = {
				if = {
					limit = { #isn't the case when the crisis bombards planets, for instance
						is_at_war_with = from.fleet.owner
					}
					add_static_war_exhaustion = {
						attacker = from.fleet.owner
						location = root
						value_for_planet_destruction = 1.0
					}
				}
			}
		}
		every_owned_pop_group = {
			kill_all_pop = yes
		}
		if = {
			limit = {
				exists = orbital_defence
			}
			destroy_fleet = orbital_defence
		}
		remove_all_buildings = yes
		# destroy habitats and ringworlds
		if = {
			limit = {
				is_planet_class = pc_habitat
			}
			clear_consecration_effect = yes
			spawn_habitat_cracker_effect = yes
		}
		else_if = {
			limit = {
				is_planet_class = pc_cosmogenesis_world
			}
			spawn_lathe_cracker_effect = yes
		}
		else_if = {
			limit = {
				is_ringworld = yes
			}
			spawn_ringworld_cracker_effect = yes
			change_pc = pc_ringworld_habitable_damaged
		}
		else_if = {
			limit = { habitable_planet = yes }
			change_pc = pc_shattered
			clear_deposits = yes
			random_list = {
				1 = {
					add_deposit = d_minerals_10
					add_deposit = d_minerals_6
				}
				1 = {
					add_deposit = d_minerals_10
					add_deposit = d_minerals_2
				}
				1 = {
					add_deposit = d_minerals_8
				}
				1 = {
					add_deposit = d_minerals_4
				}
			}
		}
		else = {
			change_pc = pc_shattered
			clear_deposits = yes
		}
	}
}


country_event = {
	id = planet_destruction.101
	title = planet_destruction.101.name
	desc = planet_destruction.101.desc
	picture = GFX_evt_exploding_planet
	show_sound = event_super_explosion
	location = from


	is_triggered_only = yes

	trigger = {
		NOT = { has_country_flag = fired_cracker }
		FROM = {
			NOR = {
				is_planet_class = pc_habitat
				is_planet_class = pc_ringworld_habitable
				is_planet_class = pc_shattered_ring_habitable
			}
		}
	}

	immediate = {
		set_country_flag = fired_cracker
	}

	option = {
		name = OK
		trigger = {
			NOR = {
				has_valid_civic = civic_fanatic_purifiers
				has_valid_civic = civic_machine_terminator
			}
		}
	}

	option = {
		name = planet_destruction.101.a.purifexter
		trigger = {
			OR = {
				has_valid_civic = civic_fanatic_purifiers
				has_valid_civic = civic_machine_terminator
			}
		}
		add_monthly_resource_mult = {
			resource = unity
			value = 60
			min = 600
			max = 3000
		}
	}
}

country_event = {
	id = planet_destruction.102
	title = planet_destruction.102.name
	desc = planet_destruction.101.desc
	picture = GFX_evt_exploding_planet
	show_sound = event_super_explosion
	location = from


	is_triggered_only = yes

	trigger = {
		NOT = { has_country_flag = fired_cracker }
	}

	immediate = {
		set_country_flag = fired_cracker
	}

	option = {
		name = OK
		trigger = {
			NOR = {
				has_valid_civic = civic_fanatic_purifiers
				has_valid_civic = civic_machine_terminator
			}
		}
	}

	option = {
		name = planet_destruction.101.a.purifexter
		trigger = {
			OR = {
				has_valid_civic = civic_fanatic_purifiers
				has_valid_civic = civic_machine_terminator
			}
		}
		add_monthly_resource_mult = {
			resource = unity
			value = 60
			min = 600
			max = 3000
		}
	}
}


# Hyperspace Bypass
carrier_event = {
	id = planet_destruction.110
	hide_window = yes

	trigger = {
		solar_system = { has_star_flag = sol }
		from.owner = {
			is_human_species = no
		}
		OR = {
			has_planet_flag = planet_earth
			has_planet_flag = nuked_earth
			AND = {
				exists = owner
				owner = {
					is_primitive = yes
					is_human_species = yes
				}
			}
		}
	}

	is_triggered_only = yes

	immediate = {
		random_list = {
			50 = { } # null
			50 = {
				solar_system = { save_event_target_as = sol_sys }
				from.owner = { country_event = { id = planet_destruction.111 } }
			}
		}
	}
}

country_event = {
	id = planet_destruction.111
	title = planet_destruction.111.name
	desc = planet_destruction.111.desc
	picture = GFX_evt_wormhole
	show_sound = event_mystic_reveal
	location = event_target:sol_sys

	is_triggered_only = yes

	immediate = {
		random_system = {
			limit = {
				has_natural_wormhole = no
				NOT = { has_star_flag = chosen_system }
				distance = {
					source = event_target:sol_sys
					min_distance >= 50
					max_distance <= 300
				}
			}
			event_target:sol_sys = {
				# done this way to ensure we don't spawn "orphan" wormholes (wormholes that aren't linked to anything)
					spawn_natural_wormhole = {
					bypass_type = wormhole
					random_pos = yes
					orbit_angle = 360
				}
			}
			spawn_natural_wormhole = {
				bypass_type = wormhole
				random_pos = yes
				orbit_angle = 360
			}
			link_wormholes = event_target:sol_sys
		}
	}

	option = {
		name = planet_destruction.111.a
		custom_tooltip = planet_destruction.111.a.tooltip
	}
}

# Pacifier
carrier_event = {
	id = planet_destruction.200
	hide_window = yes

	is_triggered_only = yes

	immediate = {
		if = {
			limit = {
				exists = owner
				owner = { NOT = { is_same_value = from.owner } }
				NOT = { is_planet_class = pc_ai }
			}
			from.owner = {
				country_event = { id = planet_destruction.210 }
			}
		}

		#Dismantle observation post
		if = {
			limit = {
				has_observation_outpost = yes
			}
			observation_outpost_owner = {
				country_event = { id = planet_destruction.1000 days = 1 }
			}
		}

		# Contingency Machine World
		if = {
			limit = {
				is_planet_class = pc_ai
				NOT = { has_planet_flag = machine_lair }
			}
			set_carrier_flag = destroyed_by_colossus
			set_carrier_flag = planet_shielded
			carrier_event = { id = crisis.2040 }
		}

		# Contingency Final Machine World
		if = {
			limit = {
				is_planet_class = pc_ai
				has_planet_flag = machine_lair
			}
			set_carrier_flag = destroyed_by_colossus
			set_carrier_flag = planet_shielded
			stop_crisis_sound = yes
			from.owner = { save_event_target_as = final_machine_world_destroyer }
			carrier_event = { id = crisis.2046 }
		}

		# Swarm Situation Log counter
		if = {
			limit = {
				exists = owner
				owner = { is_country_type = swarm }
			}
			every_country = {
				limit = { has_event_chain = "prethoryn_scourge_chain" }
				add_event_chain_counter = {
					event_chain = "prethoryn_scourge_chain"
					counter = "infested_worlds"
					amount = -1
				}
				add_event_chain_counter = {
					event_chain = "prethoryn_scourge_chain"
					counter = "infested_worlds_cleansed"
					amount = 1
				}
			}
		}

		if = {
			limit = {
				exists = owner
				owner = {
					NOT = { is_same_value = from.owner }
					OR = {
						is_country_type = default
						is_country_type = fallen_empire
						is_country_type = awakened_fallen_empire
					}
				}
			}
			# Generate threat
			add_threat = { who = from.owner amount = 3 }
			# modifier for allies + those upset by the shield
			every_country = {
				limit = {
					NOR = {
						is_same_value = from.owner
						is_same_value = root.owner
						AND = {
							has_federation = yes
							is_in_federation_with = from.owner
						}
					}
					OR = {
						has_communications = from.owner
						has_communications = root.owner
					}
					OR = {
						AND = {
							has_federation = yes
							is_in_federation_with = root.owner
						}
						has_ai_personality = awakened_fallen_empire_xenophile
						AND = {
							is_country_type = default
							OR = {
								is_egalitarian = yes
								is_xenophile = yes
								is_militarist = yes
							}
						}
					}
				}
				add_opinion_modifier = {
					modifier = opinion_shielded_a_world
					who = from.owner
				}
			}
			# modifiers for victim
			owner = {
				add_opinion_modifier = {
					modifier = opinion_shielded_my_world
					who = from.owner
				}
				if = {
					limit = { NOT = { has_ethic = ethic_gestalt_consciousness } }
					add_modifier = {
						modifier = colossus_victim
						days = 10800 # 30 years
					}
				}
			}
		}
		# Pandora's World Achievement
		from.owner = { country_event = { id = achievement.3 } }
		# Add war exhaustion to planet owner
		if = {
			limit = {
				exists = orbital_defence
			}
			destroy_fleet = orbital_defence
		}
		owner = {
			if = {
				limit = { #isn't the case when the crisis bombards planets, for instance
					is_at_war_with = from.fleet.owner
				}
				add_static_war_exhaustion = {
					attacker = from.fleet.owner
					location = root
					value_for_planet_destruction = 1.0
				}
			}
		}
		if = {
			limit = { is_planet_class = pc_habitat }
			spawn_habitat_pacifier_effect = yes
			change_pc = {
				class = pc_habitat_shielded
				inherit_entity = yes
			}
		}
		else_if = {
			limit = { is_planet_class = pc_cosmogenesis_world }
			spawn_lathe_pacifier_effect = yes
			change_pc = {
				class = pc_habitat_shielded
				inherit_entity = yes
			}
		}
		else_if = {
			limit = { is_planet_class = pc_ringworld_habitable }
			spawn_ringworld_pacifier_effect = yes
			change_pc = {
				class = pc_ringworld_shielded
				inherit_entity = yes
			}
		}
		else = {
			change_pc = {
				class = pc_shielded
				inherit_entity = yes
			}
		}
		clear_deposits = yes
		add_deposit = d_society_10
	}
}

country_event = {
	id = planet_destruction.210
	title = planet_destruction.210.name
	desc = {
		text = planet_destruction.210.desc
		trigger = {
			is_pacifist = no
			is_egalitarian = no
		}
	}
	desc = {
		text = planet_destruction.210.desc.alt
		trigger = {
			OR = {
				is_pacifist = yes
				is_egalitarian = yes
			}
		}
	}
	picture = {
		trigger = {
			from = {
				NOT = { is_planet_class = pc_habitat_shielded }
			}
		}
		picture = GFX_evt_shielded_planet
	}
	picture = {
		trigger = {
			from = {
				is_planet_class = pc_habitat_shielded
			}
		}
		picture = GFX_evt_strategic_coordination_center
	}
	show_sound = event_mystic_reveal
	location = from

	is_triggered_only = yes

	trigger = {
		NOT = { has_country_flag = fired_pacifier }
	}

	immediate = {
		set_country_flag = fired_pacifier
	}

	option = {
		name = planet_destruction.210.a
		trigger = {
			is_pacifist = no
			is_egalitarian = no
		}
	}

	option = {
		name = planet_destruction.210.b
		trigger = {
			OR = {
				is_pacifist = yes
				is_egalitarian = yes
			}
		}
	}
}

# Neutron Beam
carrier_event = {
	id = planet_destruction.300
	hide_window = yes

	is_triggered_only = yes

	immediate = {
		if = {
			limit = {
				exists = owner
				owner = { NOT = { is_same_value = from.owner } }
			}
			from.owner = {
				country_event = { id = planet_destruction.301 }
			}
		}

		#Dismantle observation post
		if = {
			limit = {
				has_observation_outpost = yes
			}
			observation_outpost_owner = {
				country_event = { id = planet_destruction.1000 days = 1 }
			}
		}

		# Swarm Situation Log counter
		if = {
			limit = {
				exists = owner
				owner = { is_country_type = swarm }
			}
			every_country = {
				limit = { has_event_chain = "prethoryn_scourge_chain" }
				add_event_chain_counter = {
					event_chain = "prethoryn_scourge_chain"
					counter = "infested_worlds"
					amount = -1
				}
				add_event_chain_counter = {
					event_chain = "prethoryn_scourge_chain"
					counter = "infested_worlds_cleansed"
					amount = 1
				}
			}
		}

		if = {
			limit = {
				OR = {
					is_active_resolution = "resolution_rulesofwar_independent_tribunals"
					is_active_resolution = "resolution_rulesofwar_last_resort_doctrine"
					is_active_resolution = "resolution_rulesofwar_demobilization_initiative"
				}
			}
			from.owner = {
				set_timed_country_flag = { flag = resolution_breached_fired_neutron days = 3600 }
			}
		}

		if = {
			limit = {
				exists = owner
				owner = {
					NOT = { is_same_value = from.owner }
					OR = {
						is_country_type = default
						is_country_type = fallen_empire
						is_country_type = awakened_fallen_empire
					}
				}
			}
			# Generate threat
			add_threat = { who = from.owner amount = 3 }
			# modifier for allies + those upset by genocide
			every_country = {
				limit = {
					NOR = {
						is_same_value = from.owner
						is_same_value = root.owner
						AND = {
							has_federation = yes
							is_in_federation_with = from.owner
						}
					}
					OR = {
						has_communications = from.owner
						has_communications = root.owner
					}
					OR = {
						AND = {
							has_federation = yes
							is_in_federation_with = root.owner
						}
						has_ai_personality = awakened_fallen_empire_xenophile
						AND = {
							is_country_type = default
							OR = {
								is_egalitarian = yes
								is_xenophile = yes
								is_spiritualist = yes
							}
						}
					}
				}
				add_opinion_modifier = {
					modifier = opinion_swept_a_world
					who = from.owner
				}
			}
			# modifiers for victim
			owner = {
				add_opinion_modifier = {
					modifier = opinion_swept_my_world
					who = from.owner
				}
				if = {
					limit = { NOT = { has_ethic = ethic_gestalt_consciousness } }
					add_modifier = {
						modifier = colossus_victim
						days = 10800 # 30 years
					}
				}
			}
		}
		else_if = {
			limit = {
				exists = owner
				owner = { is_country_type = primitive }
			}
			# modifier for allies + those upset by genocide
			every_country = {
				limit = {
					NOT = { is_same_value = from.owner }
					OR = {
						has_communications = from.owner
						has_communications = root.owner
					}
					is_country_type = default
					NOR = {
						is_xenophobe = yes
						is_homicidal = yes
						is_xenophile = yes
					}
				}
				add_opinion_modifier = {
					modifier = opinion_swept_a_primitive_world
					who = from.owner
				}
			}
			every_country = {
				limit = {
					NOT = { is_same_value = from.owner }
					OR = {
						has_communications = from.owner
						has_communications = root.owner
					}
					OR = {
						has_ai_personality = awakened_fallen_empire_xenophile
						AND = {
							is_country_type = default
							is_xenophile = yes
						}
					}
				}
				add_opinion_modifier = {
					modifier = opinion_swept_a_primitive_world_phile
					who = from.owner
				}
			}
		}
		# Add war exhaustion to planet owner
		owner = {
			if = {
				limit = { #isn't the case when the crisis bombards planets, for instance
					is_at_war_with = from.fleet.owner
				}
				add_static_war_exhaustion = {
					attacker = from.fleet.owner
					location = root
					value_for_planet_destruction = 1.0
				}
			}
		}
		while = {
			count = value:per_100_pop_amount #For every 100 pops iterate through this loop once
			from.owner = {
				if = {
					limit = { has_ascension_perk = ap_become_the_crisis }
					complete_crisis_objective = crisobj_purge_pops
				}
			}
		}
		destroy_colony = yes
		add_modifier = {
			modifier = neutron_bombarded
			days = 3600
		}
		# Special graphics for Habitats and Ring Worlds
		if = {
			limit = {
				is_planet_class = pc_habitat
			}
			spawn_habitat_neutron_effect = yes
		}
		else_if = {
			limit = {
				is_planet_class = pc_cosmogenesis_world
			}
			spawn_lathe_neutron_effect = yes
		}
		else_if = {
			limit = {
				is_planet_class = pc_ringworld_habitable
			}
			spawn_ringworld_neutron_effect = yes
		}
		if = {
			limit = {
				OR = {
					is_planet_class = pc_habitat
					is_planet_class = pc_ringworld_habitable
					is_planet_class = pc_cosmogenesis_world
				}
			}
			# nothing
		}
		else = {
			change_pc = this
		}
		# For Infested worlds
		if = {
			limit = { is_planet_class = pc_infested }
			random_list = {
				50 = { change_pc = pc_barren }
				50 = { change_pc = pc_barren_cold }
			}
			add_modifier = { modifier = "terraforming_candidate" days = -1 }
		}
	}
}

country_event = {
	id = planet_destruction.301
	title = planet_destruction.301.name
	desc = {
		text = planet_destruction.301.nobots.desc
		trigger = {
			from = {
				NOT = {
					OR = {
						is_planet_class = pc_infested
						is_planet_class = pc_barren
						is_planet_class = pc_barren_cold
					}
				}
				count_owned_pop_amount = {
					count = 0
				}
			}
		}
	}
	desc = {
		text = planet_destruction.301.bots.desc
		trigger = {
			from = {
				count_owned_pop_amount = {
					count > 0
				}
			}
		}
	}
	desc = {
		text = planet_destruction.301.infested.desc
		trigger = {
			from = {
				OR = {
					is_planet_class = pc_infested
					is_planet_class = pc_barren
					is_planet_class = pc_barren_cold
				}
			}
		}
	}
	picture = GFX_evt_planet_beam
	show_sound = event_solar_fusion
	location = from


	is_triggered_only = yes

	trigger = {
		NOT = { has_country_flag = fired_neutron }
	}

	immediate = {
		set_country_flag = fired_neutron
	}

	option = {
		name = OK
		trigger = {
			NOR = {
				has_valid_civic = civic_fanatic_purifiers
				has_valid_civic = civic_machine_terminator
			}
		}
	}

	option = {
		name = planet_destruction.101.a.purifexter
		trigger = {
			OR = {
				has_valid_civic = civic_fanatic_purifiers
				has_valid_civic = civic_machine_terminator
			}
		}
		add_monthly_resource_mult = {
			resource = unity
			value = 60
			min = 600
			max = 3000
		}
	}
}

# God Ray
carrier_event = {
	id = planet_destruction.400
	hide_window = yes

	is_triggered_only = yes

	immediate = {
		if = {
			limit = {
				NOT = { is_planet_class = pc_ai }
				exists = owner
				owner = { NOT = { is_same_value = from.owner } }
			}
			from.owner = {
				country_event = { id = planet_destruction.401 }
			}
		}

		# Contingency Machine World
		if = {
			limit = {
				is_planet_class = pc_ai
				NOT = { has_planet_flag = machine_lair }
			}
			set_carrier_flag = destroyed_by_colossus
			set_carrier_flag = planet_godrayed
			carrier_event = { id = crisis.2040 }
			create_ambient_object = {
				location = this
				type = habitat_cracker_object
				play_animation_once = yes
				duration = 5

				use_3d_location = yes
				base_angle_towards = star
				entity_face_object = star

				entity_offset = { min = 0 max = 0 }

				entity_scale_to_size = yes
				scale = 0.5
			}
		}

		# Contingency Final Machine World
		if = {
			limit = {
				is_planet_class = pc_ai
				has_planet_flag = machine_lair
			}
			set_carrier_flag = destroyed_by_colossus
			set_carrier_flag = planet_godrayed
			from.owner = { save_event_target_as = final_machine_world_destroyer }
			stop_crisis_sound = yes
			carrier_event = { id = crisis.2046 }
			create_ambient_object = {
				location = this
				type = habitat_cracker_object
				play_animation_once = yes
				duration = 5

				use_3d_location = yes
				base_angle_towards = star
				entity_face_object = star

				entity_offset = { min = 0 max = 0 }

				entity_scale_to_size = yes
				scale = 0.5
			}
		}

		if = {
			limit = {
				OR = {
					is_active_resolution = "resolution_rulesofwar_independent_tribunals"
					is_active_resolution = "resolution_rulesofwar_last_resort_doctrine"
					is_active_resolution = "resolution_rulesofwar_demobilization_initiative"
				}
			}
			from.owner = {
				set_timed_country_flag = { flag = resolution_breached_fired_godray days = 3600 }
			}
		}

		if = {
			limit = {
				NOT = { is_planet_class = pc_ai }
				exists = owner
				owner = {
					NOT = { is_same_value = from.owner }
					OR = {
						is_country_type = default
						is_country_type = fallen_empire
						is_country_type = awakened_fallen_empire
					}
				}
			}
			# Generate threat
			add_threat = { who = from.owner amount = 3 }
			# modifier for allies + those upset by conversion
			every_country = {
				limit = {
					NOR = {
						is_same_value = from.owner
						is_same_value = root.owner
						AND = {
							has_federation = yes
							is_in_federation_with = from.owner
						}
					}
					OR = {
						has_communications = from.owner
						has_communications = root.owner
					}
					OR = {
						AND = {
							has_federation = yes
							is_in_federation_with = root.owner
						}
						has_ai_personality = awakened_fallen_empire_materialist
						AND = {
							is_country_type = default
							OR = {
								is_materialist = yes
								is_egalitarian = yes
								is_machine_empire = yes
							}
						}
					}
				}
				add_opinion_modifier = {
					modifier = opinion_converted_a_world
					who = from.owner
				}
			}
			# modifiers for victim
			owner = {
				add_opinion_modifier = {
					modifier = opinion_converted_my_world
					who = from.owner
				}
				if = {
					limit = { NOT = { has_ethic = ethic_gestalt_consciousness } }
					add_modifier = {
						modifier = colossus_victim
						days = 10800 # 30 years
					}
				}
			}
			# Add war exhaustion to planet owner
			owner = {
				if = {
					limit = { #isn't the case when the crisis bombards planets, for instance
						is_at_war_with = from.fleet.owner
					}
					add_static_war_exhaustion = {
						attacker = from.fleet.owner
						location = root
						value_for_planet_destruction = 1.0
					}
				}
			}
		}
		every_owned_pop_group = {
			limit = {
				has_any_ethic = no
			}
			while = {
				count = value:per_100_pop_amount #For every 100 pops iterate through this loop once
				from.owner = {
					if = {
						limit = { has_ascension_perk = ap_become_the_crisis }
						complete_crisis_objective = crisobj_purge_pops
					}
				}
			}
			kill_all_pop = yes
		}
		every_owned_pop_group = {
			limit = {
				has_any_ethic = yes
			}
			pop_group_add_ethic_effect = {
				POP_GROUP = this
				ETHOS = ethic_spiritualist
				PERCENTAGE = 1 # 100%
				RANDOM = 25
			}
		}
		# Special graphics for Habitats and Ring Worlds
		if = {
			limit = {
				is_planet_class = pc_habitat
			}
			spawn_habitat_divine_effect = yes
		}
		else_if = {
			limit = {
				is_planet_class = pc_cosmogenesis_world
			}
			spawn_lathe_divine_effect = yes
		}
		else_if = {
			limit = {
				is_planet_class = pc_ringworld_habitable
			}
			spawn_ringworld_divine_effect = yes
		}
		if = {
			limit = {
				OR = {
					is_planet_class = pc_habitat
					is_planet_class = pc_ringworld_habitable
					is_planet_class = pc_cosmogenesis_world
				}
			}
			# nothing
		}
		else = {
			change_pc = this
		}
		add_modifier = {
			modifier = god_rayed
			days = 3600
		}
		# self-raying
		if = {
			limit = {
				exists = owner
				owner = { is_same_value = from.owner }
			}
			add_modifier = {
				modifier = uneasy_faith
				days = 3600
			}
		}
	}
}

country_event = {
	id = planet_destruction.401
	title = planet_destruction.401.name
	desc = planet_destruction.401.desc
	picture = GFX_evt_planet_beam
	show_sound = event_mystic_reveal
	location = from

	is_triggered_only = yes

	trigger = {
		NOT = { has_country_flag = fired_godray }
	}

	immediate = {
		set_country_flag = fired_godray
	}

	option = {
		name = OK
		from = {
			custom_tooltip = planet_destruction.401.tooltip
		}
	}
}

# Nanobot Assimilation
carrier_event = {
	id = planet_destruction.500
	hide_window = yes

	is_triggered_only = yes

	immediate = { # remove hive mind?
		from.owner = { country_event = { id = planet_destruction.501 } }

		if = {
			limit = {
				OR = {
					is_active_resolution = "resolution_rulesofwar_independent_tribunals"
					is_active_resolution = "resolution_rulesofwar_last_resort_doctrine"
					is_active_resolution = "resolution_rulesofwar_demobilization_initiative"
				}
			}
			from.owner = {
				set_timed_country_flag = { flag = resolution_breached_fired_assimilator days = 3600 }
			}
		}

		if = {
			limit = {
				exists = owner
				owner = {
					NOT = { is_same_value = from.owner }
					OR = {
						is_country_type = default
						is_country_type = fallen_empire
						is_country_type = awakened_fallen_empire
					}
				}
			}
			# Generate threat
			add_threat = { who = from.owner amount = 3 }
			# modifier for allies + those upset by robots
			every_country = {
				limit = {
					NOR = {
						is_same_value = from.owner
						is_same_value = root.owner
						AND = {
							has_federation = yes
							is_in_federation_with = from.owner
						}
					}
					OR = {
						has_communications = from.owner
						has_communications = root.owner
					}
					OR = {
						AND = {
							has_federation = yes
							is_in_federation_with = root.owner
						}
						has_ai_personality = awakened_fallen_empire_spiritualist
						AND = {
							is_country_type = default
							OR = {
								is_egalitarian = yes
								is_spiritualist = yes
							}
						}
					}
				}
				add_opinion_modifier = {
					modifier = opinion_assimilated_a_world
					who = from.owner
				}
			}
			# modifiers for victim
			owner = {
				add_opinion_modifier = {
					modifier = opinion_assimilated_my_world
					who = from.owner
				}
				if = {
					limit = { NOT = { has_ethic = ethic_gestalt_consciousness } }
					add_modifier = {
						modifier = colossus_victim
						days = 10800 # 30 years
					}
				}
			}
		}
		else_if = {
			limit = {
				exists = owner
				owner = { is_country_type = primitive }
			}
			# modifier for allies + those upset by genocide
			every_country = {
				limit = {
					NOR = { is_same_value = from.owner }
					OR = {
						has_communications = from.owner
						has_communications = root.owner
					}
					is_country_type = default
					NOR = {
						is_xenophobe = yes
						is_homicidal = yes
						is_xenophile = yes
					}
				}
				add_opinion_modifier = {
					modifier = opinion_assimilated_a_primitive_world
					who = from.owner
				}
			}
			every_country = {
				limit = {
					NOR = { is_same_value = from.owner }
					OR = {
						has_communications = from.owner
						has_communications = root.owner
					}
					OR = {
						has_ai_personality = awakened_fallen_empire_xenophile
						AND = {
							is_country_type = default
							is_xenophile = yes
						}
					}
				}
				add_opinion_modifier = {
					modifier = opinion_assimilated_a_primitive_world_phile
					who = from.owner
				}
			}
		}
		# Add war exhaustion to planet owner
		owner = {
			if = {
				limit = { #isn't the case when the crisis bombards planets, for instance
					is_at_war_with = from.fleet.owner
				}
				add_static_war_exhaustion = {
					attacker = from.fleet.owner
					location = root
					value_for_planet_destruction = 1.0
				}
			}
		}
		if = {
			limit = {
				exists = owner
				owner = { is_same_value = from.owner }
			}
			# already owned
		}
		else = {
			set_owner = from.owner
		}
		every_owned_pop_group = {
			limit = {
				is_valid_pop_for_PLANET_KILLER_NANOBOTS = yes
			}
			modify_species = {
				species = this
				add_trait = trait_cybernetic
				add_traits_at_start_of_list = yes
				change_scoped_species = yes
			}
		}
		every_owned_pop_group = {
			limit = {
				OR = {
					has_trait = trait_hive_mind
					has_trait = trait_mechanical
					has_trait = trait_machine_unit
				}
			}
			kill_all_pop = yes
		}
		every_owned_pop_group = {
			limit = {
				has_modifier = pop_recently_conquered
			}
			remove_modifier = pop_recently_conquered
		}
		every_owned_pop_group = {
			set_citizenship_type = {
				type = citizenship_full_machine
				cooldown = yes
			}
		}
		# Special graphics for Habitats and Ring Worlds
		if = {
			limit = {
				is_planet_class = pc_habitat
			}
			spawn_habitat_nanobot_effect = yes
		}
		else_if = {
			limit = {
				is_planet_class = pc_cosmogenesis_world
			}
			spawn_lathe_nanobot_effect = yes
		}
		else_if = {
			limit = {
				is_planet_class = pc_ringworld_habitable
			}
			spawn_ringworld_nanobot_effect = yes
		}
		if = {
			limit = {
				OR = {
					is_planet_class = pc_habitat
					is_planet_class = pc_ringworld_habitable
					is_planet_class = pc_cosmogenesis_world
				}
			}
			# nothing
		}
		else = {
			change_pc = this
		}
	}
}

country_event = {
	id = planet_destruction.501
	title = planet_destruction.501.name
	desc = planet_destruction.501.desc
	picture = GFX_evt_circuitry_modification
	show_sound = event_mystic_reveal
	location = from

	is_triggered_only = yes

	trigger = {
		NOT = { has_country_flag = fired_nanobots }
		FROM = {
			NOR = {
				is_planet_class = pc_habitat
				is_planet_class = pc_ringworld_habitable
			}
		}
	}

	immediate = {
		set_country_flag = fired_nanobots
	}

	option = {
		name = OK
		from = {
			custom_tooltip = planet_destruction.501.tooltip
		}
	}
}

# Fragged/shielded a holy world
carrier_event = {
	id = planet_destruction.600
	hide_window = yes

	is_triggered_only = yes

	trigger = {
		has_modifier = holy_planet
		any_country = {
			has_ai_personality_behaviour = holy_planets # not awakened
			NOT = { is_at_war_with = from.owner }
		}
	}

	immediate = {
		random_country = {
			limit = {
				has_ai_personality_behaviour = holy_planets # not awakened
				NOT = { is_at_war_with = from.owner }
			}
			save_event_target_as = outraged_FE
		}
		from.owner = {
			save_event_target_as = FE_outrager
			country_event = { id = planet_destruction.610 days = 5 }
		}
	}
}

carrier_event = {
	id = planet_destruction.605
	hide_window = yes

	is_triggered_only = yes

	trigger = {
		has_modifier = holy_planet
		any_country = {
			has_ai_personality_behaviour = holy_planets # not awakened
			NOT = { is_at_war_with = from.from.owner }
		}
	}

	immediate = {
		random_country = {
			limit = {
				has_ai_personality_behaviour = holy_planets # not awakened
				NOT = { is_at_war_with = from.from.owner }
			}
			save_event_target_as = outraged_FE
		}
		from.from.owner = {
			save_event_target_as = FE_outrager
			country_event = { id = planet_destruction.610 days = 5 }
		}
	}
}

# Deluged a holy world
carrier_event = {
	id = planet_destruction.607
	hide_window = yes

	is_triggered_only = yes

	trigger = {
		has_modifier = holy_planet
		any_country = {
			has_ai_personality_behaviour = holy_planets # not awakened
			NOT = { is_at_war_with = from.owner }
		}
	}

	immediate = {
		random_country = {
			limit = {
				has_ai_personality_behaviour = holy_planets # not awakened
				NOT = { is_at_war_with = from.owner }
			}
			save_event_target_as = outraged_FE
		}
		from.owner = {
			set_country_flag = holy_water_achievement
			save_event_target_as = FE_outrager
			country_event = { id = planet_destruction.615 days = 5 }
		}
		remove_modifier = holy_planet
	}
}

country_event = {
	id = planet_destruction.610
	title = planet_destruction.610.name
	desc = planet_destruction.610.desc
	show_sound = event_ship_bridge
	location = event_target:outraged_FE.capital_scope

	is_triggered_only = yes

	diplomatic = yes

	picture_event_data = {
		portrait = event_target:outraged_FE
		planet_background = event_target:outraged_FE
		graphical_culture = event_target:outraged_FE
		city_level = event_target:outraged_FE
		room = event_target:outraged_FE.ruler
	}

	after = {
		hidden_effect = {
			# wake up FE and make it start a war
			event_target:outraged_FE = {
				set_country_flag = holy_world_killed
				country_event = { id = fallen_empires_awakening.1 }
			}
		}
	}

	option = {
		name = planet_destruction.610.spiri
		trigger = {
			is_spiritualist = yes
		}
	}

	option = {
		name = planet_destruction.610.mater
		trigger = {
			is_materialist = yes
		}
	}

	option = {
		name = planet_destruction.610.milit
		trigger = {
			is_militarist = yes
		}
	}

	option = {
		name = planet_destruction.610.pacif
		trigger = {
			is_pacifist = yes
		}
	}

	option = {
		name = planet_destruction.610.autho
		trigger = {
			is_authoritarian = yes
		}
	}

	option = {
		name = planet_destruction.610.egali
		trigger = {
			is_egalitarian = yes
		}
	}

	option = {
		name = planet_destruction.610.phile
		trigger = {
			is_xenophile = yes
		}
	}

	option = {
		name = planet_destruction.610.phobe
		trigger = {
			is_xenophobe = yes
		}
	}

	option = {
		name = planet_destruction.610.gesta # What is a single planet?
		trigger = {
			has_ethic = ethic_gestalt_consciousness
		}
	}

	option = {
		name = planet_destruction.610.devou # HAK
		trigger = {
			has_valid_civic = civic_hive_devouring_swarm
		}
	}

	option = {
		name = planet_destruction.610.termi
		trigger = {
			has_valid_civic = civic_machine_terminator
		}
	}

	option = {
		name = planet_destruction.610.purif
		trigger = {
			has_valid_civic = civic_fanatic_purifiers
		}
	}
}

country_event = {
	id = planet_destruction.611
	hide_window = yes

	is_triggered_only = yes

	immediate = {
		declare_war = {
			target = event_target:FE_outrager
			name = {
				key = "NAME_Holy_World_War"
				variable_string = "[This.MainAttacker.GetAdj]"
			}
			# "[This.MainAttacker.GetAdj] Holy World War"
			attacker_war_goal = wg_subjugation
		}
	}
}

country_event = {
	id = planet_destruction.615
	base = planet_destruction.610
	option_clear = yes

	option = {
		name = planet_destruction.615.A
		name = {
			text = planet_destruction.615.spir
			exclusive_trigger = {
				owner = { is_spiritualist = yes }
			}
		}
	}
}



# Deluge
carrier_event = {
	id = planet_destruction.700
	hide_window = yes

	is_triggered_only = yes

	immediate = {
		if = {
			limit = {
				NOT = { is_planet_class = pc_ai }
			}
			from.owner = { country_event = { id = planet_destruction.701 } }
		}

		#Dismantle observation post
		if = {
			limit = {
				has_observation_outpost = yes
			}
			observation_outpost_owner = {
				country_event = { id = planet_destruction.1000 days = 1 }
			}
		}
		if = {
			limit = {
				is_planet_class = pc_ai
				NOT = { has_planet_flag = machine_lair }
			}
			#set_carrier_flag = destroyed_by_colossus
			set_carrier_flag = planet_drenched
			carrier_event = { id = crisis.2040 }
			create_ambient_object = {
				location = this
				type = habitat_cracker_object
				play_animation_once = yes
				duration = 5

				use_3d_location = yes
				base_angle_towards = star
				entity_face_object = star

				entity_offset = { min = 0 max = 0 }

				entity_scale_to_size = yes
				scale = 0.5
			}
		}

		# Contingency Final Machine World
		if = {
			limit = {
				is_planet_class = pc_ai
				has_planet_flag = machine_lair
			}
			#set_carrier_flag = destroyed_by_colossus
			set_carrier_flag = planet_drenched
			from.owner = { save_event_target_as = final_machine_world_destroyer }
			stop_crisis_sound = yes
			carrier_event = { id = crisis.2046 }
			create_ambient_object = {
				location = this
				type = habitat_cracker_object
				play_animation_once = yes
				duration = 5

				use_3d_location = yes
				base_angle_towards = star
				entity_face_object = star

				entity_offset = { min = 0 max = 0 }

				entity_scale_to_size = yes
				scale = 0.5
			}
		}

		# Swarm Situation Log counter
		if = {
			limit = {
				exists = owner
				owner = { is_country_type = swarm }
			}
			every_country = {
				limit = { has_event_chain = "prethoryn_scourge_chain" }
				add_event_chain_counter = {
					event_chain = "prethoryn_scourge_chain"
					counter = "infested_worlds"
					amount = -1
				}
				add_event_chain_counter = {
					event_chain = "prethoryn_scourge_chain"
					counter = "infested_worlds_cleansed"
					amount = 1
				}
			}
			destroy_colony = yes
		}

		if = {
			limit = {
				exists = owner
				owner = {
					NOT = { is_same_value = from.owner }
				}
				OR = {
					is_active_resolution = "resolution_rulesofwar_independent_tribunals"
					is_active_resolution = "resolution_rulesofwar_last_resort_doctrine"
					is_active_resolution = "resolution_rulesofwar_demobilization_initiative"
				}
			}
			from.owner = {
				set_timed_country_flag = { flag = resolution_breached_fired_deluge days = 3600 }
			}
		}
		if = { #you've basically shortcutted terraforming. Yep, that's a breach
			limit = {
				exists = space_owner
				space_owner = {
					is_same_value = from.owner
				}
				OR = {
					NOT = { exists = owner }
					owner = { is_same_value = from.owner }
				}
				OR = {
					is_active_resolution = "resolution_ecology_environmental_control_board"
					is_active_resolution = "resolution_ecology_paradise_initiative"
				}
			}
			from.owner = {
				set_timed_country_flag = { flag = resolution_breached_terraformed days = 3600 }
			}
		}

		if = {
			limit = {
				exists = owner
				owner = {
					NOT = { is_same_value = from.owner }
					OR = {
						is_country_type = default
						is_country_type = fallen_empire
						is_country_type = awakened_fallen_empire
					}
				}
			}
			# Generate threat
			add_threat = { who = from.owner amount = 3 }
			# modifier for allies + those upset by robots
			every_country = {
				limit = {
					NOR = {
						is_same_value = from.owner
						is_same_value = root.owner
						AND = {
							has_federation = yes
							is_in_federation_with = from.owner
						}
					}
					OR = {
						has_communications = from.owner
						has_communications = root.owner
					}
				}
				add_opinion_modifier = {
					modifier = opinion_drenched_a_world
					who = from.owner
				}
			}
			# modifiers for victim
			owner = {
				add_opinion_modifier = {
					modifier = opinion_drenched_my_world
					who = from.owner
				}
				if = {
					limit = { NOT = { has_ethic = ethic_gestalt_consciousness } }
					add_modifier = {
						modifier = colossus_victim
						days = 10800 # 30 years
					}
				}
			}
		}
		else_if = {
			limit = {
				exists = owner
				owner = { is_country_type = primitive }
			}
			# modifier for allies + those upset by genocide
			every_country = {
				limit = {
					NOR = { is_same_value = from.owner }
					OR = {
						has_communications = from.owner
						has_communications = root.owner
					}
					is_country_type = default
					NOR = {
						is_xenophobe = yes
						is_homicidal = yes
						is_xenophile = yes
					}
				}
				add_opinion_modifier = {
					modifier = opinion_drenched_a_primitive_world
					who = from.owner
				}
			}
			every_country = {
				limit = {
					NOR = { is_same_value = from.owner }
					OR = {
						has_communications = from.owner
						has_communications = root.owner
					}
					OR = {
						has_ai_personality = awakened_fallen_empire_xenophile
						AND = {
							is_country_type = default
							is_xenophile = yes
						}
					}
				}
				add_opinion_modifier = {
					modifier = opinion_drenched_a_primitive_world_phile
					who = from.owner
				}
			}
		}
		# Add war exhaustion to planet owner
		if = {
			limit = {
				exists = owner
				owner = { NOT = { is_same_value = from.owner } }
			}
			owner = {
				if = {
					limit = { #isn't the case when the crisis bombards planets, for instance
						is_at_war_with = from.owner
					}
					add_static_war_exhaustion = {
						attacker = from.owner
						location = root
						value_for_planet_destruction = 1.0
					}
				}
			}
		}

		every_owned_pop_group = {
			limit = {
				has_aquatic_trait = no
			}
			while = {
				count = value:per_100_pop_amount #For every 100 pops iterate through this loop once
				from.owner = {
					if = {
						limit = { has_ascension_perk = ap_become_the_crisis }
						complete_crisis_objective = crisobj_purge_pops
					}
				}
			}
			kill_all_pop = yes
		}
		if = {
			limit = { #were there any Aquatic pops? If so, the ownership needs to be flipped
				any_owned_pop_group = {
					is_scope_valid = yes
					exists = this
				}
				owner = {
					NOT = { is_same_value = from.owner }
				}
			}
			set_owner = from.owner
		}
		if = {
			limit = {
				NOT = { is_planet_class = pc_broken }
			}
			start_terraform_process = pc_ocean
			finish_terraformation = yes
		}

		# from = { #feedback suggests this isn't fun, but leaving it here in case we need it
		# 	ice_mining_depletion_effect = yes

		# 	if = {
		# 		limit = {
		# 			root = { has_carrier_flag = temp_ice_mine_exhausted_flag }
		# 		}
		# 		event_target:mined_system.starbase = {
		# 			remove_starbase_building = {
		# 				building = ice_mining_station
		# 			}
		# 		}
		# 		create_message = {
		# 			type = DELUGE_EXHAUSTED_ICE
		# 			localization = DELUGE_EXHAUSTED_ICE_DESC
		# 			days = 20
		# 			target = root
		# 			variable = {
		# 				type = name
		# 				localization = SYSTEM
		# 				scope = event_target:mined_system
		# 			}
		# 		}
		# 	}
		# }
	}
}

country_event = {
	id = planet_destruction.701
	title = planet_destruction.701.name
	desc = planet_destruction.701.desc
	picture = GFX_evt_death_from_above
	show_sound = event_mystic_reveal
	location = from

	is_triggered_only = yes

	trigger = {
		NOT = { has_country_flag = fired_deluge }
	}

	immediate = {
		set_country_flag = fired_deluge
	}

	option = {
		name = EXCELLENT
		from = {
			custom_tooltip = planet_destruction.701.tooltip
		}
	}
}

# # Deluge Queued
# carrier_event = {
# 	id = planet_destruction.705
# 	hide_window = yes

# 	is_triggered_only = yes

# 	immediate = {
# 		from.owner = {
# 			random_owned_starbase = {
# 				limit = {
# 					has_starbase_building = ice_mining_station
# 					solar_system = {
# 						NOT = { has_star_flag = ice_mining_in_progress }
# 					}
# 				}
# 				solar_system = {
# 					set_star_flag = ice_mining_in_progress
# 					set_star_flag = ice_mining@root.from
# 				}
# 			}
# 		}
# 	}
# }

# # Deluge Unqueued or Cancelled
# carrier_event = {
# 	id = planet_destruction.710
# 	hide_window = yes

# 	is_triggered_only = yes

# 	immediate = {
# 		from.owner = {
# 			random_owned_starbase = {
# 				limit = {
# 					solar_system = {
# 						has_star_flag = ice_mining@root.from
# 					}
# 				}
# 				solar_system = {
# 					remove_star_flag = ice_mining@root.from
# 					remove_star_flag = ice_mining_in_progress
# 				}
# 			}
# 		}
# 	}
# }


# Toxified
carrier_event = {
	id = planet_destruction.800
	is_triggered_only = yes
	hide_window = yes

	immediate = {
		if = {
			limit = {
				colonizable_planet = yes
				is_artificial = no
			}
			set_carrier_flag = toxic_candidate
		}
		if = {
			limit = {
				NOT = { is_planet_class = pc_ai }
			}
			from.owner = { country_event = { id = planet_destruction.801 } }
		}

		#Dismantle observation post
		if = {
			limit = {
				has_observation_outpost = yes
			}
			observation_outpost_owner = {
				country_event = { id = planet_destruction.1000 days = 1 }
			}
		}

		# Contingency Machine World
		if = {
			limit = {
				is_planet_class = pc_ai
				NOT = { has_planet_flag = machine_lair }
			}
			set_carrier_flag = destroyed_by_colossus
			set_carrier_flag = planet_cracked #doesn't matter
			carrier_event = { id = crisis.2040 }
		}

		# Contingency Final Machine World
		if = {
			limit = {
				is_planet_class = pc_ai
				has_planet_flag = machine_lair
			}
			set_carrier_flag = destroyed_by_colossus
			set_carrier_flag = planet_cracked
			from.owner = { save_event_target_as = final_machine_world_destroyer }
			stop_crisis_sound = yes
			carrier_event = { id = crisis.2046 }
		}

		# Swarm Situation Log counter
		if = {
			limit = {
				exists = owner
				owner = { is_country_type = swarm }
			}
			every_country = {
				limit = { has_event_chain = "prethoryn_scourge_chain" }
				add_event_chain_counter = {
					event_chain = "prethoryn_scourge_chain"
					counter = "infested_worlds"
					amount = -1
				}
				add_event_chain_counter = {
					event_chain = "prethoryn_scourge_chain"
					counter = "infested_worlds_cleansed"
					amount = 1
				}
			}
		}

		if = {
			limit = {
				exists = owner
				owner = {
					NOT = { is_same_value = from.owner }
					OR = {
						is_country_type = default
						is_country_type = fallen_empire
						is_country_type = awakened_fallen_empire
					}
				}
			}
			# Generate threat
			if = {
				limit = {
					is_colony = yes
				}
				add_threat = { who = from.owner amount = 3 }
				# modifier for allies + those upset by genocide
				every_country = {
					limit = {
						NOR = {
							is_same_value = from.owner
							is_same_value = root.owner
							AND = {
								has_federation = yes
								is_in_federation_with = from.owner
							}
						}
						OR = {
							has_communications = from.owner
							has_communications = root.owner
						}
						OR = {
							AND = {
								has_federation = yes
								is_in_federation_with = root.owner
							}
							has_ai_personality = awakened_fallen_empire_xenophile
							AND = {
								is_country_type = default
								OR = {
									is_egalitarian = yes
									is_xenophile = yes
								}
							}
						}
					}
					if = {
						limit = { root = { is_colony = no } }
						add_opinion_modifier = {
							modifier = opinion_toxified_an_uninhabited_world
							who = from.owner
						}
					}
					else = {
						add_opinion_modifier = {
							modifier = opinion_toxified_a_world
							who = from.owner
						}
					}
				}
			}
			else = {
				add_threat = { who = from.owner amount = 1 }
			}
			# modifiers for victim
			owner = {
				if = {
					limit = { root = { is_colony = no } }
					add_opinion_modifier = {
						modifier = opinion_toxified_my_uninhabited_world
						who = from.owner
					}
				}
				else = {
					add_opinion_modifier = {
						modifier = opinion_toxified_my_world
						who = from.owner
					}
				}
				if = {
					limit = { NOT = { has_ethic = ethic_gestalt_consciousness } }
					add_modifier = {
						modifier = colossus_victim
						days = 10800 # 30 years
					}
				}
			}
			if = {
				limit = {
					exists = owner
					owner = { is_country_type = primitive }
				}
				# modifier for allies + those upset by genocide
				every_country = {
					limit = {
						NOT = { is_same_value = from.owner }
						OR = {
							has_communications = from.owner
							has_communications = root.owner
						}
						OR = {
							has_ai_personality = awakened_fallen_empire_xenophile
							AND = {
								is_country_type = default
								NOR = {
									is_xenophobe = yes
									is_homicidal = yes
									is_xenophile = yes
								}
							}
						}
					}
					add_opinion_modifier = {
						modifier = opinion_toxified_a_primitive_world
						who = from.owner
					}
				}
				every_country = {
					limit = {
						NOT = { is_same_value = from.owner }
						OR = {
							has_communications = from.owner
							has_communications = root.owner
						}
						OR = {
							has_ai_personality = awakened_fallen_empire_xenophile
							AND = {
								is_country_type = default
								is_xenophile = yes
							}
						}
					}
					add_opinion_modifier = {
						modifier = opinion_toxified_a_primitive_world_phile
						who = from.owner
					}
				}
			}
		}
		# Add war exhaustion to planet owner
		if = {
			limit = {
				exists = owner
			}
			owner = {
				if = {
					limit = { #isn't the case when the crisis bombards planets, for instance
						is_at_war_with = from.fleet.owner
					}
					add_static_war_exhaustion = {
						attacker = from.fleet.owner
						location = root
						value_for_planet_destruction = 1.0
					}
				}
			}
		}
		destroy_colony = yes
		if = {
			limit = {
				exists = orbital_defence
			}
			destroy_fleet = orbital_defence
		}
		remove_all_buildings = yes

		change_pc = pc_toxic
		clear_deposits = yes

		if = {
			limit = { has_modifier = holy_planet }
			remove_modifier = holy_planet
		}

		if = {
			limit = {
				has_carrier_flag = toxic_candidate
			}
			add_modifier = { modifier = toxic_terraforming_candidate }
			remove_carrier_flag = toxic_candidate
		}
	}
}

country_event = {
	id = planet_destruction.801
	title = planet_destruction.801.name
	desc = planet_destruction.801.desc
	picture = GFX_evt_toxic_god
	show_sound = event_super_explosion
	location = from

	is_triggered_only = yes

	trigger = {
		NOT = { has_country_flag = fired_toxifier }
		FROM = {
			NOR = {
				is_planet_class = pc_habitat
				is_planet_class = pc_ringworld_habitable
			}
		}
	}

	immediate = {
		set_country_flag = fired_toxifier
	}

	option = {
		name = OK
		trigger = {
			NOR = {
				has_valid_civic = civic_fanatic_purifiers
				has_valid_civic = civic_machine_terminator
			}
		}
	}

	option = {
		name = planet_destruction.101.a.purifexter
		trigger = {
			OR = {
				has_valid_civic = civic_fanatic_purifiers
				has_valid_civic = civic_machine_terminator
			}
		}
		add_monthly_resource_mult = {
			resource = unity
			value = 60
			min = 600
			max = 3000
		}
	}
}

# Devolving Beam
carrier_event = {
	id = planet_destruction.900
	hide_window = yes

	is_triggered_only = yes

	immediate = {
		if = {
			limit = {
				NOT = { is_planet_class = pc_ai }
				exists = owner
				owner = { NOT = { is_same_value = from.owner } }
			}
			from.owner = {
				country_event = { id = planet_destruction.901 }
			}
		}

		# Contingency Machine World ### Maybe block these if no AP
		if = {
			limit = {
				is_planet_class = pc_ai
				NOT = { has_planet_flag = machine_lair }
				from.owner = {
					has_ascension_perk = ap_archaeoengineers
				}
			}
			set_carrier_flag = destroyed_by_colossus
			set_carrier_flag = planet_devolved
			carrier_event = { id = crisis.2040 }
			create_ambient_object = {
				location = this
				type = habitat_cracker_object
				play_animation_once = yes
				duration = 5

				use_3d_location = yes
				base_angle_towards = star
				entity_face_object = star

				entity_offset = { min = 0 max = 0 }

				entity_scale_to_size = yes
				scale = 0.5
			}
		}

		# Contingency Final Machine World
		if = {
			limit = {
				is_planet_class = pc_ai
				has_planet_flag = machine_lair
				from.owner = {
					has_ascension_perk = ap_archaeoengineers
				}
			}
			set_carrier_flag = destroyed_by_colossus
			set_carrier_flag = planet_devolved
			from.owner = { save_event_target_as = final_machine_world_destroyer }
			stop_crisis_sound = yes
			carrier_event = { id = crisis.2046 }
			create_ambient_object = {
				location = this
				type = habitat_cracker_object
				play_animation_once = yes
				duration = 5

				use_3d_location = yes
				base_angle_towards = star
				entity_face_object = star

				entity_offset = { min = 0 max = 0 }

				entity_scale_to_size = yes
				scale = 0.5
			}
		}

		if = {
			limit = {
				OR = {
					is_active_resolution = "resolution_rulesofwar_independent_tribunals"
					is_active_resolution = "resolution_rulesofwar_last_resort_doctrine"
					is_active_resolution = "resolution_rulesofwar_demobilization_initiative"
				}
			}
			from.owner = {
				set_timed_country_flag = { flag = resolution_breached_fired_devolution_beam days = 3600 }
			}
		}

		if = {
			limit = {
				NOT = { is_planet_class = pc_ai }
				exists = owner
				owner = {
					NOT = { is_same_value = from.owner }
					OR = {
						is_country_type = default
						is_country_type = fallen_empire
						is_country_type = awakened_fallen_empire
					}
				}
			}
			# Generate threat
			add_threat = { who = from.owner amount = 3 }
			# modifier for allies + those upset by conversion
			every_country = {
				limit = {
					NOR = {
						is_same_value = from.owner
						is_same_value = root.owner
						AND = {
							has_federation = yes
							is_in_federation_with = from.owner
						}
					}
					OR = {
						has_communications = from.owner
						has_communications = root.owner
					}
					OR = {
						AND = {
							has_federation = yes
							is_in_federation_with = root.owner
						}
						has_ai_personality = awakened_fallen_empire_spiritualist
						AND = {
							is_country_type = default
							OR = {
								is_spiritualist = yes
								is_xenophile = yes
								is_hive_empire = yes
							}
						}
					}
				}
				add_opinion_modifier = {
					modifier = opinion_devolved_a_world
					who = from.owner
				}
			}
			# modifiers for victim
			owner = {
				add_opinion_modifier = {
					modifier = opinion_devolved_my_world
					who = from.owner
				}
				if = {
					limit = {
						NOT = { has_ethic = ethic_gestalt_consciousness }
					}
					add_modifier = {
						modifier = colossus_victim
						days = 10800 # 30 years
					}
				}
			}
			# Add war exhaustion to planet owner
			owner = {
				if = {
					limit = { #isn't the case when the crisis bombards planets, for instance
						is_at_war_with = from.fleet.owner
					}
					add_static_war_exhaustion = {
						attacker = from.fleet.owner
						location = root
						value_for_planet_destruction = 1.0
					}
				}
			}
		}
		every_owned_pop_group = {
			limit = {
				from.owner = {
					has_ascension_perk = ap_archaeoengineers
				}
				OR = {
					has_trait = trait_mechanical
					has_trait = trait_machine_unit
				}
			}
			while = {
				count = value:per_100_pop_amount #For every 100 pops iterate through this loop once
				from.owner = {
					if = {
						limit = { has_ascension_perk = ap_become_the_crisis }
						complete_crisis_objective = crisobj_purge_pops
					}
				}
			}
			kill_all_pop = yes
		}
		if = {
			limit = {
				from.owner = {
					has_ascension_perk = ap_archaeoengineers
				}
			}
			planet = {
				every_planet_army = {
					limit = {
						OR = {
							army_type = occupation_army
							army_type = cybrex_warform
							army_type = care_defensive_protocols
							army_type = gray_army
							army_type = robot_uprising_army
							army_type = robotic_army
							army_type = machine_assault_1
							army_type = machine_assault_2
							army_type = machine_assault_3
							army_type = robotic_defense_army
							army_type = machine_defense
							army_type = robotic_occupation_army
							army_type = machine_occupation_army
						}
					}
					remove_army = yes
				}
			}
		}
		while = {
			limit = {
				any_owned_pop_group = {
					NOR = {
						has_trait = trait_mechanical
						has_trait = trait_machine_unit
					}
					is_sapient = yes
				}
			}
			weighted_random_owned_pop_group = {
				limit = {
					NOR = {
						has_trait = trait_mechanical
						has_trait = trait_machine_unit
					}
					is_sapient = yes
				}
				species = { save_event_target_as = species_target }
				create_devolved_species = yes
			}
			every_owned_pop_group = {
				limit = {
					NOR = {
						has_trait = trait_mechanical
						has_trait = trait_machine_unit
					}
					is_same_species = event_target:species_target
					is_sapient = yes
				}
				change_species = event_target:devolved_species_target
			}
		}

		weighted_random_owned_pop_group = {
			change_species = event_target:species_target
		}
		random_list = {
			33 = {}
			33 = {
				weighted_random_owned_pop_group = {
					change_species = event_target:species_target
				}
			}
			33 = {
				weighted_random_owned_pop_group = {
					change_species = event_target:species_target
				}
				weighted_random_owned_pop_group = {
					change_species = event_target:species_target
				}
			}
		}
		planet = {
			every_planet_army = {
				limit = {
					OR = {
						army_type = occupation_army
						army_type = offspring_defense
						army_type = undead_defense_army
						army_type = fanatic_guardian
						army_type = defense_army
						army_type = assault_army
						army_type = slave_army
						army_type = clone_army
						army_type = psionic_army
						army_type = xenomorph_army
						army_type = gene_warrior_army
						army_type = undead_army
						army_type = rebel_army
						army_type = rebel_slave_defense_army
						army_type = primitive_army
						army_type = primitive_additional_army
						army_type = industrial_army
						army_type = postatomic_army
						army_type = subterranean_industrial_army
						army_type = subterranean_postatomic_army
						army_type = mutant_army
						army_type = swarm_army
						army_type = tree_army
						army_type = enraged_army
						army_type = ketling_army
						army_type = titanic_assault_army
						army_type = titanic_azizian_assault_army
						army_type = titanic_guardian_army
						army_type = imperial_legion
						army_type = giant_undead_army
						army_type = ice_army
						army_type = mercenary_army
						army_type = mecha_army
						army_type = subterranean_defense_army
						army_type = new_world_order_defensive_army
						army_type = new_world_order_offensive_army
						army_type = warpling_army
						army_type = flamestorm_troopers_army
						army_type = mindwarden_army
					}
				}
				remove_army = yes
			}
			check_planet_employment = yes
		}
		# Special graphics for Habitats and Ring Worlds
		if = {
			limit = {
				is_planet_class = pc_habitat
			}
			spawn_habitat_neutron_effect = yes
		}
		else_if = {
			limit = {
				is_planet_class = pc_cosmogenesis_world
			}
			spawn_lathe_neutron_effect = yes
		}
		else_if = {
			limit = {
				is_planet_class = pc_ringworld_habitable
			}
			spawn_ringworld_neutron_effect = yes
		}
		if = {
			limit = {
				OR = {
					is_planet_class = pc_habitat
					is_planet_class = pc_ringworld_habitable
					is_planet_class = pc_cosmogenesis_world
				}
			}
			# nothing
		}
		else = {
			change_pc = this
		}
		set_timed_planet_flag = {
			flag = devolved_planet
			days = 360
		}
	}
}

country_event = {
	id = planet_destruction.901
	title = planet_destruction.901.name
	desc = planet_destruction.901.desc
	picture = GFX_evt_planet_beam
	show_sound = event_mystic_reveal
	location = from

	is_triggered_only = yes

	trigger = {
		NOT = { has_country_flag = fired_devolving_beam }
	}

	immediate = {
		set_country_flag = fired_devolving_beam
	}

	option = {
		name = OK
	}
}

#Notification for dismantled outpost
country_event = {
	id = planet_destruction.1000
	title = planet_destruction.1000.name
	desc = {
		trigger = {
			is_same_value = fromfrom.owner
		}
		text = planet_destruction.1000.desc.self
	}
	desc = {
		trigger = {
			NOT = { is_same_value = fromfrom.owner }
			from = {
				OR = {
					is_planet_class = pc_shattered
					is_planet_class = pc_shattered_2
				}
			}
		}
		text = planet_destruction.1000.desc.shatter
	}
	desc = {
		trigger = {
			NOT = { is_same_value = fromfrom.owner }
			from = {
				OR = {
					is_planet_class = pc_shielded
					is_planet_class = pc_ringworld_shielded
					is_planet_class = pc_habitat_shielded
				}
			}
		}
		text = planet_destruction.1000.desc.shield
	}
	desc = {
		trigger = {
			NOT = { is_same_value = fromfrom.owner }
			from = {
				NOR = {
					is_planet_class = pc_shattered
					is_planet_class = pc_shattered_2
					is_planet_class = pc_shielded
					is_planet_class = pc_ringworld_shielded
					is_planet_class = pc_habitat_shielded
				}
			}
		}
		text = planet_destruction.1000.desc.neutron
	}
	picture = {
		trigger = {
			from = {
				is_planet_class = pc_shielded
			}
		}
		picture = GFX_evt_shielded_planet
	}
	picture = {
		trigger = {
			from = {
				NOR = {
					is_planet_class = pc_shielded
					is_planet_class = pc_ringworld_shielded
					is_planet_class = pc_habitat_shielded
				}
			}
		}
		picture = GFX_evt_exploding_planet
	}
	trigger = {
		from = {
			has_observation_outpost = yes
		}
	}

	is_triggered_only = yes

	option = {
		name = planet_destruction.1000.A
		trigger = {
			OR = {
				NOR = {
					is_homicidal = yes
					is_xenophobe = yes
				}
				NOT = {
					from = {
						OR = {
							is_planet_class = pc_shattered
							is_planet_class = pc_shattered_2
						}
					}
				}
			}
		}
		from = {
			observation_outpost = {
				dismantle = yes
			}
		}
	}
	option = {
		name = planet_destruction.1000.B
		trigger = {
			OR = {
				is_homicidal = yes
				is_xenophobe = yes
			}
			from = {
				OR = {
					is_planet_class = pc_shattered
					is_planet_class = pc_shattered_2
				}
			}
		}
		from = {
			observation_outpost = {
				dismantle = yes
			}
		}
		add_monthly_resource_mult = {
			resource = unity
			value = 2
		}
	}
}

# Eaten
carrier_event = {
	id = planet_destruction.1005
	is_triggered_only = yes
	hide_window = yes

	immediate = {
		#Dismantle observation post
		if = {
			limit = {
				has_observation_outpost = yes
			}
			observation_outpost_owner = {
				country_event = { id = planet_destruction.1000 days = 1 }
			}
		}

		# Contingency Machine World
		if = {
			limit = {
				is_planet_class = pc_ai
				NOT = { has_planet_flag = machine_lair }
			}
			set_carrier_flag = destroyed_by_colossus
			set_carrier_flag = planet_cracked
			carrier_event = { id = crisis.2040 }
		}

		# Contingency Final Machine World
		if = {
			limit = {
				is_planet_class = pc_ai
				has_planet_flag = machine_lair
			}
			set_carrier_flag = destroyed_by_colossus
			set_carrier_flag = planet_cracked
			from.owner = { save_event_target_as = final_machine_world_destroyer }
			stop_crisis_sound = yes
			carrier_event = { id = crisis.2046 }
		}

		# Swarm Situation Log counter
		if = {
			limit = {
				exists = owner
				owner = { is_country_type = swarm }
			}
			every_country = {
				limit = { has_event_chain = "prethoryn_scourge_chain" }
				add_event_chain_counter = {
					event_chain = "prethoryn_scourge_chain"
					counter = "infested_worlds"
					amount = -1
				}
				add_event_chain_counter = {
					event_chain = "prethoryn_scourge_chain"
					counter = "infested_worlds_cleansed"
					amount = 1
				}
			}
		}

		# Galcom Breach Status
		if = {
			limit = {
				OR = {
					is_active_resolution = "resolution_rulesofwar_reverence_for_life"
					is_active_resolution = "resolution_rulesofwar_independent_tribunals"
					is_active_resolution = "resolution_rulesofwar_last_resort_doctrine"
					is_active_resolution = "resolution_rulesofwar_demobilization_initiative"
				}
			}
			from.owner = {
				set_timed_country_flag = { flag = resolution_breached_fired_cracker days = 3600 }
			}
		}

		# Exterminatus Achievement
		if = {
			limit = { is_capital = yes }
			from.owner = { set_country_flag = exterminatus }
		}
		if = {
			limit = {
				exists = owner
				owner = {
					NOT = { is_same_value = from.owner }
					OR = {
						is_country_type = default
						is_country_type = fallen_empire
						is_country_type = awakened_fallen_empire
					}
				}
			}
			# Generate threat
			if = {
				limit = {
					is_colony = yes
				}
				add_threat = { who = from.owner amount = 3 }
				# modifier for allies + those upset by genocide
				every_country = {
					limit = {
						NOR = {
							is_same_value = from.owner
							is_same_value = root.owner
							AND = {
								has_federation = yes
								is_in_federation_with = from.owner
							}
						}
						OR = {
							has_communications = from.owner
							has_communications = root.owner
						}
						OR = {
							AND = {
								has_federation = yes
								is_in_federation_with = root.owner
							}
							has_ai_personality = awakened_fallen_empire_xenophile
							AND = {
								is_country_type = default
								OR = {
									is_egalitarian = yes
									is_xenophile = yes
								}
							}
						}
					}
					if = {
						limit = { root = { is_colony = no } }
						add_opinion_modifier = {
							modifier = opinion_cracked_an_uninhabited_world
							who = from.owner
						}
					}
					else = {
						add_opinion_modifier = {
							modifier = opinion_cracked_a_world
							who = from.owner
						}
					}
				}
			}
			else = {
				add_threat = { who = from.owner amount = 1 }
			}
			# modifiers for victim
			owner = {
				if = {
					limit = { root = { is_colony = no } }
					add_opinion_modifier = {
						modifier = opinion_cracked_my_uninhabited_world
						who = from.owner
					}
				}
				else = {
					add_opinion_modifier = {
						modifier = opinion_cracked_my_world
						who = from.owner
					}
				}
				if = {
					limit = { NOT = { has_ethic = ethic_gestalt_consciousness } }
					add_modifier = {
						modifier = colossus_victim
						days = 10800 # 30 years
					}
				}
			}
			if = {
				limit = {
					exists = owner
					owner = { is_country_type = primitive }
				}
				# modifier for allies + those upset by genocide
				every_country = {
					limit = {
						NOT = { is_same_value = from.owner }
						OR = {
							has_communications = from.owner
							has_communications = root.owner
						}
						OR = {
							has_ai_personality = awakened_fallen_empire_xenophile
							AND = {
								is_country_type = default
								NOR = {
									is_xenophobe = yes
									is_homicidal = yes
									is_xenophile = yes
								}
							}
						}
					}
					add_opinion_modifier = {
						modifier = opinion_cracked_a_primitive_world
						who = from.owner
					}
				}
				every_country = {
					limit = {
						NOT = { is_same_value = from.owner }
						OR = {
							has_communications = from.owner
							has_communications = root.owner
						}
						OR = {
							has_ai_personality = awakened_fallen_empire_xenophile
							AND = {
								is_country_type = default
								is_xenophile = yes
							}
						}
					}
					add_opinion_modifier = {
						modifier = opinion_cracked_a_primitive_world_phile
						who = from.owner
					}
				}
			}
		}
		# Add war exhaustion to planet owner
		if = {
			limit = {
				exists = owner
			}
			owner = {
				if = {
					limit = { #isn't the case when the crisis bombards planets, for instance
						is_at_war_with = from.fleet.owner
					}
					add_static_war_exhaustion = {
						attacker = from.fleet.owner
						location = root
						value_for_planet_destruction = 1.0
					}
				}
			}
		}
		every_owned_pop_group = {
			kill_pop_group = {
				pop_group = this
				percentage = 1
			}
		}
		if = {
			limit = {
				exists = orbital_defence
			}
			delete_fleet = orbital_defence
		}
		remove_all_buildings = yes
		remove_planet = yes
	}
}
