destroy_star_system = {
	if = {
		limit = { exists = event_target:crisis_country }
		fire_on_action = {
			on_action = on_destroy_star_system
			scopes = { from = event_target:crisis_country }
		}
	}
	else = {
		fire_on_action = { on_action = on_destroy_star_system }
	}
	set_star_flag = star_cracked
	set_star_class = sc_black_hole
	every_system_ambient_object = {
		limit = {
			# Preserve Unbidden crisis effects
			NOR = {
				has_ambient_object_flag = extradimensional_system_effect
				has_ambient_object_flag = extradimensional_system_effect_2
			}
		}
		destroy_ambient_object = this
	}
	every_system_planet = {
		limit = { is_star = yes }
		create_ambient_object = {
			type = "star_explosion"
			play_animation_once = yes
			location = this
		}
		last_created_ambient_object = {
			set_location = {
				target = prev
				distance = 0
				angle = random
			}
		}
		change_pc = pc_black_hole
	}
	# Handle Contingency Machine Worlds
	every_system_planet = {
		limit = {
			is_planet_class = pc_ai
			NOT = { has_planet_flag = machine_lair }
		}
		destroy_machine_world = yes
	}
	random_system_planet = {
		limit = {
			is_planet_class = pc_ai
			has_planet_flag = machine_lair
		}
		from.owner = { save_event_target_as = final_machine_world_destroyer }
		stop_crisis_sound = yes
		planet_event = { id = crisis.2046 }
	}
	every_system_planet = {
		limit = {
			is_star = no
			is_asteroid = no
			NOR = {
				is_planet_class = pc_shattered
				is_planet_class = pc_shattered_2
				is_planet_class = pc_shielded
				is_planet_class = pc_ringworld_habitable_damaged
				is_planet_class = pc_ringworld_tech_damaged
				is_planet_class = pc_ringworld_seam_damaged
			}
		}
		if = {
			limit = { is_planet_class = pc_habitat } # uncolonized
			spawn_habitat_cracker_effect = yes
		}
		if = {
			limit = { is_colony = yes }
			if = {
				limit = {
					exists = orbital_defence
				}
				destroy_fleet = orbital_defence
			}
			remove_all_buildings = yes
			destroy_colony = yes
			if = {
				limit = { is_planet_class = pc_habitat }
				spawn_habitat_cracker_effect = yes
			}
			else_if = {
				limit = { is_ringworld = yes }
				spawn_ringworld_cracker_effect = yes
				change_pc = pc_ringworld_habitable_damaged
			}
			else = {
				change_pc = pc_shattered
			}
		}
		else_if = {
			limit = { is_ringworld = yes }
			spawn_ringworld_cracker_effect = yes
			change_pc = pc_ringworld_habitable_damaged
		}
		else_if = {
			limit = { is_planet_class = pc_ringworld_tech }
			spawn_ringworld_cracker_effect = yes
			change_pc = pc_ringworld_tech_damaged
		}
		else_if = {
			limit = { is_planet_class = pc_ringworld_seam }
			spawn_ringworld_cracker_effect = yes
			change_pc = pc_ringworld_seam_damaged
		}
		else_if = {
			limit = { is_planet_class = pc_oeo_planetary_ringworld }
			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.07
			}
			change_pc = pc_oeo_planetary_ringworld_damaged
		}
		else = {
			change_pc = pc_shattered
		}
	}
	every_system_planet = {
		limit = {
			exists = orbital_station
		}
		orbital_station = { dismantle = yes }
	}
	every_system_planet = {
		clear_deposits = yes
		clear_planet_modifiers = yes
		prevent_anomaly = yes
	}
	every_system_planet = {
		limit = { is_asteroid = yes }
		clear_deposits = yes
	}
	every_fleet_in_system = {
		limit = {
			NOR = {
				is_same_value = from
				# except Extradimensionals
				AND = {
					exists = owner
					owner = {
						OR = {
							is_country_type = portal_holder
							is_country_type = extradimensional
							is_country_type = extradimensional_2
							is_country_type = extradimensional_3
						}
					}
				}
				# except biggest monsters
				any_owned_ship = {
					OR = {
						is_ship_size = behemoth_04
						is_ship_size = voidspawn_boss
					}
				}
			}
		}
		if = {
			limit = {
				exists = event_target:crisis_country #i.e. not final destruction of all matter
				is_mobile = yes
			}
			set_mia = mia_return_home
		}
		else = {
			destroy_fleet = this
		}
	}
	every_system_megastructure = {
		set_ruined_megastructure = yes
	}
	star = {
		create_ambient_object = {
			type = "destroyed_system"
			location = this
		}
		last_created_ambient_object = {
			set_ambient_object_flag = destroyed_system_effect
			set_location = {
				target = prev
				distance = 0
				angle = random
			}
		}
	}
	remove_system_terraforming_candidates = yes
}