namespace = oeo_planet_devolver

planet_event = {
	id = oeo_planet_devolver.1
	is_triggered_only = yes
	hide_window = yes

	immediate = {
		if = {
			limit = { 
				has_planet_flag = oeo_ring@ROOT
			}
			solar_system = {
				random_system_planet = {
					limit = {
						is_planet_class = pc_oeo_planetary_ringworld
						has_planet_flag = oeo_ring@ROOT
					}
					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
					}
					create_ambient_object = {
						location = this
						type = habitat_neutron_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
					}
					set_timed_planet_flag = {
						flag = devolved_planet
						days = 360
					}
				}
			}
		}
	}
}