namespace = planetmega_pk

#add count #unused
# planet_event = {
# 	id = planetmega_pk.1
# 	is_triggered_only = yes
# 	hide_window = yes
#
# 	immediate = {
# 		from.owner = { planetmega_colossus_add_count = yes }
# 	}
# }

# country_event = {
# 	id = planetmega_pk.2
# 	is_triggered_only = yes
# 	picture = GFX_evt_planet_beam
# 	title = planetmega_pk.2.name
# 	desc = planetmega_pk.2.desc
# 	trigger = { not = { has_country_flag = plnmg_no_pk2_anymore } }
# 	option = {
# 		name = planetmega_pk.2.OK
# 		default_hide_option = yes
# 	}
# 	option = {
# 		name = planetmega_effect.30.operation
# 		trigger = {
# 			is_variable_set = num_plnmg_colossus_fired_count
# 			check_variable = { which = num_plnmg_colossus_fired_count value > 0 }
# 		}
# 		allow = {
# 			resource_stockpile_compare = {
# 				resource = energy
# 				value >= @plntCore_rechargeBase
# 				mult = value:sv_colossus_recharge_cost_mult
# 			}
# 		}
# 		add_resource = {
# 			energy = @plntCore_rechargeBase_neg
# 			mult = value:sv_colossus_recharge_cost_mult
# 		}
# 		hidden_effect = { set_variable = { which = num_plnmg_colossus_fired_count value = 0 } }
# 	}
# 	option = {
# 		name = planetmega_pk.2.non_next
# 		hidden_effect = { set_country_flag = plnmg_no_pk2_anymore }
# 	}
# }

planet_event = {
	id = planetmega_pk.3
	is_triggered_only = yes
	hide_window = yes
	immediate = {
		save_event_target_as = fired_planet
		from = {
			save_event_target_as = fire_launcher
			controller = {
				if = {
					limit = { has_any_plnmg_aeg_supportive_structure = yes }
					if = {
						limit = {
							resource_stockpile_compare = {
								resource = energy
								value >= @plntCore_rechargeBase
								mult = value:sv_colossus_recharge_cost_mult
							}
						}
						country_event = { id = planetmega_pk.5 }
					}
					else = { country_event = { id = planetmega_pk.101 } }
				}
				else = { country_event = { id = planetmega_pk.103 } }
			}
		}
	}
}
country_event = {
	id = planetmega_pk.5
	is_triggered_only = yes
	location = event_target:fired_planet
	title = planetmega_pk.7.name
	desc = planetmega_pk.7.desc
	picture = GFX_evt_planet_beam

	diplomatic = yes
	diplomatic_title = planetmega_pk.5.name
	picture_event_data = {
		room = owner_main_species # swappable for leader (species)
		portrait = event_target:planetmega_military_leader # this will use GIF, but I am still searching
		graphical_culture = owner
	}
	immediate = { decide_planetmega_military_leader = yes }

	option = {
		name = PLANET_KILLER_CRACKER
		custom_tooltip = PLANET_KILLER_CRACKER_DESC
		trigger = { has_technology = tech_pk_cracker }
		allow = { from = { can_destroy_planet_with_PLANET_KILLER_CRACKER = yes } }
		hidden_effect = {
			event_target:fired_planet = {
				create_plnmg_colossus_hit_se = { ao = colossus_shatter_planet_hit_object }
			}
			plnmg_aeg_colossus_fire_no_check = { on_action = on_destroy_planet_with_PLANET_KILLER_CRACKER }
		}
	}
	option = {
		name = PLANET_KILLER_SHIELDER
		custom_tooltip = PLANET_KILLER_SHIELDER_DESC
		trigger = { has_technology = tech_pk_shielder }
		allow = { from = { can_destroy_planet_with_PLANET_KILLER_SHIELDER = yes } }
		hidden_effect = {
			event_target:fired_planet = {
				create_plnmg_colossus_hit_se = { ao = colossus_shield_planet_hit_object }
			}
			plnmg_aeg_colossus_fire_no_check = { on_action = on_destroy_planet_with_PLANET_KILLER_SHIELDER }
		}
	}
	option = {
		name = PLANET_KILLER_NEUTRON
		custom_tooltip = PLANET_KILLER_NEUTRON_DESC
		trigger = { has_technology = tech_pk_neutron }
		allow = { from = { can_destroy_planet_with_PLANET_KILLER_NEUTRON = yes } }
		hidden_effect = {
			event_target:fired_planet = {
				create_plnmg_colossus_hit_se = { ao = colossus_neutron_planet_hit_object }
			}
			plnmg_aeg_colossus_fire_no_check = { on_action = on_destroy_planet_with_PLANET_KILLER_NEUTRON }
		}
	}
	option = {
		name = PLANET_KILLER_GODRAY
		custom_tooltip = PLANET_KILLER_GODRAY_DESC
		trigger = { has_technology = tech_pk_godray }
		allow = { from = { can_destroy_planet_with_PLANET_KILLER_GODRAY = yes } }
		hidden_effect = {
			event_target:fired_planet = {
				if = {
					limit = { is_artificial = yes }
					create_plnmg_colossus_hit_se = { ao = colossus_god_ray_megastructure_hit_object }
				}
				else = { create_plnmg_colossus_hit_se = { ao = colossus_god_ray_hit_object } }
			}
			plnmg_aeg_colossus_fire_no_check = { on_action = on_destroy_planet_with_PLANET_KILLER_GODRAY }
		}
	}
	option = {
		name = PLANET_KILLER_NANOBOTS
		custom_tooltip = PLANET_KILLER_NANOBOTS_DESC
		trigger = { has_technology = tech_pk_nanobots }
		allow = { from = { can_destroy_planet_with_PLANET_KILLER_NANOBOTS = yes } }
		hidden_effect = {
			event_target:fired_planet = {
				if = {
					limit = { is_artificial = yes }
					create_plnmg_colossus_hit_se = { ao = colossus_assimilate_megastructure_hit_object }
				}
				else = { create_plnmg_colossus_hit_se = { ao = colossus_assimilate_hit_object } }
			}
			plnmg_aeg_colossus_fire_no_check = { on_action = on_destroy_planet_with_PLANET_KILLER_NANOBOTS }
		}
	}
	option = {
		name = PLANET_KILLER_DELUGE
		custom_tooltip = PLANET_KILLER_DELUGE_DESC
		trigger = { has_ascension_perk = ap_hydrocentric }
		allow = { from = { can_destroy_planet_with_PLANET_KILLER_DELUGE = yes } }
		hidden_effect = {
			event_target:fired_planet = {
				if = {
					limit = { is_artificial = yes }
					create_plnmg_colossus_hit_se = { ao = deluge_planet_hit_object }
				}
				else = { create_plnmg_colossus_hit_se = { ao = deluge_megastructure_hit_object } }
			}
			plnmg_aeg_colossus_fire_no_check = { on_action = on_destroy_planet_with_PLANET_KILLER_DELUGE }
		}
	}
	option = {
		name = PLANET_KILLER_TOXIFIER
		custom_tooltip = PLANET_KILLER_TOXIFIER_PLNMG_DESC
		trigger = { has_technology = tech_plnmg_pk_toxifier }
		allow = { from = { can_destroy_planet_with_PLANET_KILLER_TOXIFIER = yes } }
		hidden_effect = {
			event_target:fired_planet = {
				create_plnmg_colossus_hit_se = { ao = toxifier_planet_hit_object }
			}
			plnmg_aeg_colossus_fire_no_check = { on_action = on_destroy_planet_with_PLANET_KILLER_TOXIFIER }
		}
	}
	option = {
		name = PLANET_KILLER_DEVOLUTION
		custom_tooltip = PLANET_KILLER_DEVOLUTION_DESC
		trigger = { has_technology = tech_archaeo_pk_devolving_beam }
		allow = { from = { can_destroy_planet_with_PLANET_KILLER_DEVOLUTION = yes } }
		hidden_effect = {
			event_target:fired_planet = {
				create_plnmg_colossus_hit_se = { ao = colossus_neutron_planet_hit_object }
			}
			plnmg_aeg_colossus_fire_no_check = { on_action = on_destroy_planet_with_PLANET_KILLER_DEVOLUTION }
		}
	}
	option = {
		name = PLANET_KILLER_MAW
		custom_tooltip = tech_plnmg_pk_maw_desc
		trigger = { has_technology = tech_plnmg_pk_maw }
		allow = { from = { can_destroy_planet_with_PLANET_KILLER_MAW = yes } }
		hidden_effect = {
			# sorry but this do not have a proper entity
			plnmg_aeg_colossus_fire_no_check = { event = planetmega_pk.50 }
		}
	}
	option = {
		name = PLANET_KILLER_SMELTER
		custom_tooltip = PLANET_KILLER_SMELTER_DESC
		trigger = { has_technology = tech_pk_smelter }
		allow = { from = { can_destroy_planet_with_PLANET_KILLER_SMELTER = yes } }
		hidden_effect = {
			event_target:fired_planet = {
				if = {
					limit = { is_artificial = yes }
					create_plnmg_colossus_hit_se = { ao = infernal_colossus_planet_hit_object }
				}
				else = { create_plnmg_colossus_hit_se = { ao = infernal_colossus_megastructure_hit_object } }
			}
			plnmg_aeg_colossus_fire_no_check = { on_action = on_destroy_planet_with_PLANET_KILLER_SMELTER }
		}
	}

	option = {
		name = CUSTOM
		custom_tooltip = plnmg_pk.custom_strike
		trigger = {
			always = no
			#has_cwc_mod = yes
		}
		hidden_effect = {
			#CWC_colossus_plnt_effect = yes
		}
	}
	option = {
		name = CANCEL
		default_hide_option = yes
	}
}

# country_event = {
# 	id = planetmega_pk.7
# 	is_triggered_only = yes
# 	location = event_target:fired_planet
# 	title = planetmega_pk.7.name
# 	desc = planetmega_pk.7.desc
# 	picture = GFX_evt_planet_beam
# 	option = {
# 		name = CANCEL
# 		default_hide_option = yes
# 		hidden_effect = { country_event = { id = planetmega_pk.5 } }
# 	}
# }

planet_event = {
	id = planetmega_pk.4
	is_triggered_only = yes
	hide_window = yes
	immediate = {
		save_event_target_as = fired_planet
		from = {
			save_event_target_as = fire_launcher
			controller = {
				if = {
					limit = { has_any_plnmg_aeg_supportive_structure = yes }
					if = {
						limit = {
							resource_stockpile_compare = {
								resource = energy
								value >= @plntCore_rechargeBase
								mult = value:sv_colossus_recharge_cost_mult
							}
						}
						country_event = { id = planetmega_pk.6 }
					}
					else = { country_event = { id = planetmega_pk.101 } }
				}
				else = { country_event = { id = planetmega_pk.103 } }
			}
		}
	}
}
country_event = {
	id = planetmega_pk.6
	is_triggered_only = yes
	location = from
	title = planetmega_pk.6.name
	desc = planetmega_pk.6.desc
	picture = GFX_evt_planet_beam

	option = {
		name = PLANET_KILLER_STAR_CRACKER
		custom_tooltip = PLANET_KILLER_STAR_CRACKER_DESC
		trigger = {
			or = {
				has_menace_perk = menp_star_eater
				has_ascension_perk = ap_become_the_crisis
				has_country_flag = can_build_star_eaters
			}
		}
		allow = { from = { can_destroy_planet_with_PLANET_KILLER_STAR_CRACKER = yes } }
		hidden_effect = {
			event_target:fired_planet = {
				create_plnmg_colossus_system_hit_se = {
					ao = star_eater_hit_object planets = yes megastructures = yes
				}
			}
			plnmg_aeg_colossus_fire_no_check = { on_action = on_destroy_planet_with_PLANET_KILLER_STAR_CRACKER }
		}
	}
	option = {
		name = PLANET_KILLER_STAR_CRACKER_LITE_PLNMG
		custom_tooltip = PLANET_KILLER_STAR_CRACKER_LITE_PLNMG_DESC
		trigger = {
			has_technology = tech_plnmg_btc_1
			nor = {
				has_menace_perk = menp_star_eater
				has_ascension_perk = ap_become_the_crisis
			}
		}
		allow = { from = { can_destroy_planet_with_PLANET_KILLER_STAR_CRACKER = yes } }
		hidden_effect = {
			event_target:fired_planet = {
				create_plnmg_colossus_system_hit_se = {
					ao = star_eater_hit_object planets = yes megastructures = yes
				}
			}
			plnmg_aeg_colossus_fire_no_check = { event = planetmega_pk.80 }
		}
	}
	option = {
		name = PLANET_KILLER_PLNMG_STAR_OSCILLATOR
		custom_tooltip = PLANET_KILLER_PLNMG_STAR_OSCILLATOR_DESC
		#trigger = { has_technology =  }
		allow = {
			from = {
				custom_tooltip = {
					fail_text = is_not_star
					is_star = yes
				}
				custom_tooltip = {
					fail_text = is_black_hole
					NOT = { is_planet_class = pc_black_hole }
				}
				custom_tooltip = {
					fail_text = star_owner_is_not_hostile
					OR = {
						not = { exists = space_owner }
						space_owner = { is_hostile = root }
					}
				}
			}
		}
		hidden_effect = {
			event_target:fired_planet = {
				create_plnmg_colossus_system_hit_se = {
					ao = star_eater_hit_object planets = yes megastructures = yes
				}
			}
			plnmg_aeg_colossus_fire_no_check = { event = planetmega_pk.20 }
		}
	}
	option = {
		name = CUSTOM
		custom_tooltip = plnmg_pk.custom_strike
		trigger = { has_cwc_mod = yes }
		hidden_effect = {
			#CWC_colossus_star_effect = yes
		}
	}
	option = {
		name = CANCEL
		default_hide_option = yes
	}
}

planet_event = {
	id = planetmega_pk.20
	is_triggered_only = yes
	hide_window = yes
	immediate = {
		star = {
			create_ambient_object = {
				type = "star_explosion"
				play_animation_once = yes
				location = this
			}
			last_created_ambient_object = {
				set_location = {
					target = prev
					distance = 0
					angle = random
				}
			}
		}
		solar_system = {
			plnmg_pk_eradicator_sys_effect = yes
			save_event_target_as = oscillated_system
			owner = { country_event = { id = planetmega_pk.21 } }
		}
	}
}
country_event = {
	id = planetmega_pk.21
	is_triggered_only = yes
	title = planetmega_pk.21.name
	desc = planetmega_pk.21.desc
	picture = GFX_evt_supernova
	show_sound = event_super_explosion
	location = event_target:oscillated_system
	option = { name = CURSES }
}

# for use with scripted_effects create_plnmg_colossus_hit_se and create_plnmg_colossus_system_hit_se
system_event = {
	id = planetmega_pk.22
	is_triggered_only = yes
	hide_window = yes
	immediate = {
		every_system_ambient_object = {
			limit = {
				has_ambient_object_flag = plnmg_colossus_hit_se
				not = { has_ambient_object_flag = should_not_be_destroyed }
			}
			destroy_ambient_object = this
		}
	}
}

# unused
# country_event = {
# 	id = planetmega_pk.30
# 	hide_window = yes
# 	is_triggered_only = yes
# 	trigger = { fromfrom = { has_component = PLNMG_CORE_WARP_BEAM } }
# 	immediate = {
# 		fromfromfrom = { save_event_target_as = destroyed_ship_location }
# 		if = {
# 			limit = { any_owned_colony = { has_deposit = d_plnmg_colossus_assist } }
# 			set_variable = { which = ship_size_value_@fromfromfrom value = fromfromfrom.trigger:ship_size_multiplier }
# 			divide_variable = { which = ship_size_value_@fromfromfrom value = 8 }
# 			round_variable = ship_size_value_@fromfromfrom
# 			planetmega_colossus_add_count = { value = ship_size_value_@fromfromfrom }
# 			clear_variable = ship_size_value_@fromfromfrom
# 			if = {
# 				limit = {
# 					check_variable = {
# 						which = num_plnmg_colossus_fired_count
# 						value > num_plnmg_colossus_fired_limit
# 					}
# 				}
# 				country_event = { id = planetmega_pk.31 }
# 				country_event = { id = planetmega_pk.32 }
# 			}
# 		}
# 		else = {
# 			country_event = { id = planetmega_pk.33 }
# 			#country_event = { id = planetmega_pk.34 }
# 		}
# 	}
# }

#reach limit for instant weapon
country_event = {
	id = planetmega_pk.31
	is_triggered_only = yes
	hide_window = yes

	immediate = {
		country_event = { id = planetmega_pk.101 }
		set_variable = { which = num_plnmg_colossus_fired_count value = 0 }
		set_variable = { which = num_plnmg_colossus_fired_limit value = 0 }
		set_country_flag = core_energy_warp_disabled
		every_owned_ship = {
			limit = { has_component = PLNMG_CORE_WARP_BEAM }
			set_disabled = yes
			set_ship_flag = disabled_for_core_energy_warp
		}
		every_owned_colony = {
			limit = { has_deposit = d_plnmg_colossus_assist }
			remove_plnmg_mega = { TYPE = plnmg_colossus_assist }
			every_owned_pop_group = { kill_all_pop = yes }
			destroy_colony = yes
			if = {
				limit = { is_artificial = no }
				change_pc = pc_frozen
			}
		}
	}
}

country_event = {
	id = planetmega_pk.32
	is_triggered_only = yes
	title = planetmega_pk.32.name
	desc = planetmega_pk.32.desc
	picture = GFX_evt_planet_beam
	location = event_target:destroyed_ship_location
	option = { name = CURSES }
}

country_event = {
	id = planetmega_pk.33
	hide_window = yes
	is_triggered_only = yes
	immediate = {
		set_country_flag = core_energy_warp_disabled
		every_owned_ship = {
			limit = { has_component = PLNMG_CORE_WARP_BEAM }
			set_disabled = yes
			set_ship_flag = disabled_for_core_energy_warp
		}
	}
}

ship_event = {
	id = planetmega_pk.34
	is_triggered_only = yes
	title = planetmega_pk.34.name
	desc = {
		trigger = {
			controller? = {
				nor = {
					any_owned_colony = { has_deposit = d_plnmg_colossus_assist }
					has_megastructure = planetmega_colossus_assistant_1
					has_megastructure = planetmega_colossus_assistant_2
					has_megastructure = planetmega_colossus_assistant_restored
				}
			}
		}
		text = planetmega_pk.34.desc
	}
	desc = {
		trigger = {
			controller? = {
				or = {
					any_owned_colony = { has_deposit = d_plnmg_colossus_assist }
					has_megastructure = planetmega_colossus_assistant_1
					has_megastructure = planetmega_colossus_assistant_2
					has_megastructure = planetmega_colossus_assistant_restored
				}
			}
		}
		text = planetmega_pk.34.desc.with_build
	}
	picture = GFX_evt_planet_beam
	#location = event_target:destroyed_ship_location
	location = this
	immediate = { add_modifier = { modifier = plnmg_pk_eradicator_ship_modifier months = 12 } }
	option = { name = CURSES }
	after = { tooltip = { add_modifier = { modifier = plnmg_pk_eradicator_ship_modifier months = 12 } } }
}

# ship_event = {
# 	id = planetmega_pk.35
# 	is_triggered_only = yes
# 	hide_window = yes
# 	trigger = {
# 		always = no
# 		# is_variable_set = temp_ship_aoe_damage_var
# 		# check_variable = { which = temp_ship_aoe_damage_var value > 0 }
# 	}
# 	immediate = {
# 		damage_ship = temp_ship_aoe_damage_var
# 		clear_variable = temp_ship_aoe_damage_var
# 	}
# }

carrier_event = {
	id = planetmega_pk.39
	is_triggered_only = yes
	hide_window = yes
	trigger = { exists = owner }
	immediate = {
		save_global_event_target_as = fire_launcher_of_@owner
		owner = { country_event = { id = planetmega_pk.40 } }
	}
}
country_event = {
	id = planetmega_pk.40
	is_triggered_only = yes
	title = planetmega_intersys_strike_title
	desc = BLANK_STRING
	picture = GFX_evt_planet_beam
	diplomatic = yes
	custom_gui = "planetmega_intersys_strike"
	trigger = { exists = event_target:fire_launcher_of_@this }
	option = {
		name = OK
		hidden_effect = {
			clear_all_plnmg_itss_weapon_flags = yes
			clear_global_event_target = fire_launcher_of_@this
			clear_variable = plnmg_itss_selection_index
		}
	}
}
country_event = {
	id = planetmega_pk.41
	is_triggered_only = yes
	picture = GFX_evt_giant_building_in_progress
	title = planetmega_pk.41.name
	desc = planetmega_pk.41.desc
	option = {
		name = OK
		hidden_effect = {
			clear_all_plnmg_itss_weapon_flags = yes
			if = {
				limit = {
					not = { has_active_event = { planetmega_pk.40 } }
					exists = event_target:fire_launcher_of_@this
				}
				event_target:fire_launcher_of_@this = {
					carrier_event = { id = planetmega_pk.39 }
				}
			}
		}
	}
}

planet_event = {
	id = planetmega_pk.42
	is_triggered_only = yes
	hide_window = yes
	immediate = {
		if = {
			limit = { has_planet_flag = not_terraforming_candidate_temporarily }
			add_modifier = { modifier = terraforming_candidate }
			remove_planet_flag = not_terraforming_candidate_temporarily
		}
		if = {
			limit = { has_planet_flag = not_frozen_terraforming_candidate_temporarily }
			add_modifier = { modifier = frozen_terraforming_candidate }
			remove_planet_flag = not_frozen_terraforming_candidate_temporarily
		}
		if = {
			limit = { has_planet_flag = not_toxic_terraforming_candidate_temporarily }
			add_modifier = { modifier = toxic_terraforming_candidate }
			remove_planet_flag = not_toxic_terraforming_candidate_temporarily
		}
		###
		if = {
			limit = { has_planet_flag = cool_molten_planet_temporarily }
			remove_planet_flag = cool_molten_planet_temporarily
			change_pc = pc_molten
		}
		if = {
			limit = { has_planet_flag = cool_toxic_planet_temporarily }
			remove_planet_flag = cool_toxic_planet_temporarily
			change_pc = pc_toxic
		}
		if = {
			limit = { has_planet_flag = cool_barren_planet_temporarily }
			remove_planet_flag = cool_barren_planet_temporarily
			change_pc = pc_barren
		}
	}
}

country_event = {
	id = planetmega_pk.43
	is_triggered_only = yes
	hide_window = yes
	trigger = {
		is_primitive = yes
		has_country_flag = temporarily_regressed
	}
	immediate = {
		remove_country_flag = temporarily_regressed
		pre_ftl_progresses_one_age = yes
	}
}

carrier_event = {
	id = planetmega_pk.44
	is_triggered_only = yes
	title = planetmega_pk.44.name #temp
	desc = planetmega_effect.1.desc
	picture = GFX_evt_supernova #temp
	location = root

	option = {
		name = decision_plnmg_ca_excavation
		trigger = { exists = orbit }
		allow = {
			custom_tooltip = {
				fail_text = requires_plnmg_ca_orbiting_common_star
				orbit = {
					or = {
						is_plnmg_common_star = yes
						is_planet_class_pc_black_hole = yes
						is_planet_class = pc_rift_star
					}
				}
			}
			custom_tooltip = {
				fail_text = requires_star_not_plnmg_ca_excavated
				orbit = {
					nor = {
						has_modifier = harvested_resources_mining
						has_modifier = planetmega_ca_harvested
					}
				}
			}
			#custom_tooltip = { fail_text = need_plnmg_eg_gateway_owner_specified }
			solar_system = {
				or = {
					has_owner = no
					is_owned_by = from
					and = {
						owner = { opinion_level = { who = from level > neutral } }
						not = { any_system_planet_colony = { is_owned_by = prev.owner } }
					}
				}
			}
		}
		owner = {
			add_resource = { energy = 100000 }
			if = {
				limit = { hidden_trigger = { has_menace_perk = menp_hyperthermia_crystals } }
				add_resource = { entropy_crystals = 10000 }
			}
			if = {
				limit = { hidden_trigger = { prev.orbit = { is_planet_class_pc_black_hole = yes } } }
				add_resource = { sr_dark_matter = 1000 }
			}
			if = {
				limit = { hidden_trigger = { prev.orbit = { is_planet_class = pc_rift_star } } }
				add_resource = { astral_threads = 1000 }
			}
			if = {
				limit = {
					hidden_trigger = {
						prev.orbit = { has_resource = { type = minerals amount > 0 } }
					}
				}
				add_resource = {
					minerals = 1
					mult = prev.orbit.value:arkship_harvest_resource_deposit_size|RESOURCE|minerals|MULT|5000|
				}
			}
			if = {
				limit = {
					hidden_trigger = {
						prev.orbit = { has_resource = { type = engineering_research amount > 0 } }
					}
				}
				add_resource = {
					engineering_research = 1
					mult = prev.orbit.value:arkship_harvest_resource_deposit_size|RESOURCE|engineering_research|MULT|100|
				}
			}
		}
		hidden_effect = {
			orbit = {
				add_modifier = { modifier = harvested_resources_mining years = 15 }
				add_modifier = { modifier = planetmega_ca_harvested years = 50 }
			}
			solar_system = {
				remove_system_terraforming_candidates_temporarily = { YEARS = 50 }
				every_system_planet = {
					if = {
						limit = { is_colony = yes }
						if = {
							limit = { owner = { is_primitive = yes } }
							owner = {
								country_event = { id = planetmega_pk.43 years = 50 }
								switch = {
									trigger = has_pre_ftl_age
									bronze_age = {
										set_pre_ftl_age_effect = { PRE_FTL_AGE = stone_age }
										set_country_flag = temporarily_regressed
									}
									# iron_age = {
									# 	set_pre_ftl_age_effect = { PRE_FTL_AGE = bronze_age }
									# }
									late_medieval_age = {
										set_pre_ftl_age_effect = { PRE_FTL_AGE = iron_age }
										set_country_flag = temporarily_regressed
									}
									renaissance_age = {
										set_pre_ftl_age_effect = { PRE_FTL_AGE = late_medieval_age }
										set_country_flag = temporarily_regressed
									}
									# steam_age = {
									# 	set_pre_ftl_age_effect = { PRE_FTL_AGE = renaissance_age }
									# }
									industrial_age = {
										set_pre_ftl_age_effect = { PRE_FTL_AGE = steam_age }
										set_country_flag = temporarily_regressed
									}
									machine_age = {
										set_pre_ftl_age_effect = { PRE_FTL_AGE = industrial_age }
										set_country_flag = temporarily_regressed
									}
									atomic_age = {
										set_pre_ftl_age_effect = { PRE_FTL_AGE = machine_age }
										set_country_flag = temporarily_regressed
									}
								}
							}
						}
						#else = { add_modifier = { modifier =  } }
					}
					else_if = {
						limit = { is_planet_class = pc_molten }
						set_planet_flag = cool_molten_planet_temporarily
						change_pc = pc_frozen
						planet_event = { id = planetmega_pk.42 years = 50 }
					}
					else_if = {
						limit = { is_planet_class = pc_toxic }
						set_planet_flag = cool_toxic_planet_temporarily
						change_pc = pc_frozen
						planet_event = { id = planetmega_pk.42 years = 50 }
					}
					else_if = {
						limit = { is_planet_class = pc_barren }
						set_planet_flag = cool_barren_planet_temporarily
						change_pc = pc_barren_cold
						planet_event = { id = planetmega_pk.42 years = 50 }
					}
				}
				# every_system_ship_colony = {
				# 	limit = { owner = { is_primitive = yes } }
				# 	add_planet_devastation = 50
				# }
				if = {
					limit = { exempt_from_harvest_resources_opinion_reduction_trigger = no }
					space_owner? = {
						add_opinion_modifier = {
							modifier = opinion_plnmg_ca_excavator
							who = root.controller
						}
					}
				}
			}
		}
	}

	option = { name = REFRESH hidden_effect = { carrier_event = { id = planetmega_pk.44 } } }
	option = { name = CANCEL default_hide_option = yes }
}

country_event = {
	id = planetmega_pk.45
	is_triggered_only = yes
	picture = GFX_evt_megastructure_construction
	title = planetmega_pk.45.name
	desc = planetmega_pk.45.desc
	location = event_target:location_system
	option = { name = CURSES }
}

#BEHEMOTH
planet_event = {
	id = planetmega_pk.50
	is_triggered_only = yes
	hide_window = yes
	immediate = {
		from.owner = {
			random_owned_ship = {
				limit = { has_component = PLANET_KILLER_MAW }
				fleet = { save_event_target_as = behemoth_the_world_eater }
			}
		}
		fire_on_action = {
			on_action = on_destroy_planet_with_PLANET_KILLER_MAW_queued
			scopes = { from = event_target:behemoth_the_world_eater }
		}
		fire_on_action = {
			on_action = on_destroy_planet_with_PLANET_KILLER_MAW
			scopes = { from = event_target:behemoth_the_world_eater }
		}
	}
}

# devolution (a complex one)
planet_event = {
	id = planetmega_pk.71
	is_triggered_only = yes
	hide_window = yes

	trigger = {
		from.owner = {
			#has_ascension_perk = ap_archaeoengineers
			check_variable = {
				which = num_plnmg_colossus_fired_count
				value <= num_plnmg_colossus_fired_limit
			}
		}
	}

	immediate = {
		#planet_event = {
		#	id = planet_destruction.900
		#	scopes = { from = from }
		#}
		if = {
			limit = {
				NOT = { is_planet_class = pc_ai }
				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 }
			}
			set_planet_flag = destroyed_by_colossus
			set_planet_flag = planet_devolved
			carrier_event = { id = crisis.2040 }
			create_ambient_object = {
				location = this
				type = habitat_cracker_object
				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_planet_flag = destroyed_by_colossus
			set_planet_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
				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 }
				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
								has_authority = auth_hive_mind
							}
						}
					}
				}
				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 = { is_gestalt = no }
					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
					}
				}
			}
		}

		#custom (partially)
		every_owned_pop_group = {
			limit = {
				OR = {
					has_trait = trait_mechanical
					has_trait = trait_machine_unit
				}
			}
			while = {
				limit = { pop_group_size > 0 }
				kill_single_pop = yes
				from.owner = {
					if = {
						limit = { has_ascension_perk = ap_become_the_crisis }
						complete_crisis_objective = crisobj_purge_pops
					}
				}
			}
		}
		while = {
			limit = { any_owned_pop_group = { is_sapient = yes } }
			random_owned_pop_group = {
				limit = { is_sapient = yes }
				species = { save_event_target_as = species_target }
				random_list = {
					2 = { create_devolved_species = yes }
					1 = {
						modify_species = {
							species = event_target:species_target
							add_trait = trait_presapient_unintelligent
							add_traits_at_start_of_list = yes
							effect = {
								change_species_characteristics = { sapient = no }
								save_event_target_as = devolved_species_target
							}
						}
					}
				}
			}
			every_owned_pop_group = {
				limit = {
					is_same_species = event_target:species_target
					is_sapient = yes
				}
				change_species = event_target:devolved_species_target
			}
		}
		check_planet_employment = yes
		remove_all_armies = yes
		set_timed_planet_flag = {
			flag = devolved_planet
			days = 360
		}
		# Special graphics for Habitats and Ring Worlds
		if = {
			limit = { is_planet_class = pc_habitat }
			spawn_habitat_neutron_effect = yes
		}
		if = {
			limit = { is_planet_class = pc_ringworld_habitable }
			spawn_ringworld_neutron_effect = yes
		}

		#compatibility with vanilla
		carrier_event = { id = origin.3245 scopes = { from = from } }
		carrier_event = { id = awareness.150 scopes = { from = from } }
	}
}

#star eater lite
planet_event = {
	id = planetmega_pk.80
	is_triggered_only = yes
	hide_window = yes

	# trigger = {
	# 	from.owner = {
	# 		check_variable = {
	# 			which = num_plnmg_colossus_fired_count
	# 			value <= num_plnmg_colossus_fired_limit
	# 		}
	# 	}
	# }

	immediate = {
		# planet_event = {
		# 	id = crisis.4550
		# 	scopes = { from = from }
		# }
		add_threat = { who = from.owner amount = 10 }
		from.owner = {
			save_event_target_as = crisis_country # compatible with destroy_star_system
			add_victory_score = { source = destroyed_stars_score score = 10 }
			if = {
				limit = { NOT = { has_country_flag = cracked_first_star } }
				set_country_flag = cracked_first_star
			}
		}
		solar_system = { save_event_target_as = destroyed_system }
		every_playable_country = {
			limit = {
				is_ai = no
				NOR = {
					is_same_value = event_target:crisis_country
					OR = {
						has_country_flag = can_build_star_eaters
						has_technology = tech_btc_1
						has_technology = tech_plnmg_btc_1
					}
					has_country_flag = star_eater_alert@event_target:crisis_country
					has_country_flag = star_cracker_alert@event_target:crisis_country
				}
				intel_level = {
					system = event_target:destroyed_system
					level = none
				}
			}
			country_event = { id = planetmega_pk.81 }
		}
		every_playable_country = {
			limit = {
				is_ai = no
				has_communications = event_target:crisis_country
				NOR = {
					is_same_value = event_target:crisis_country
					OR = {
						has_country_flag = can_build_star_eaters
						has_technology = tech_btc_1
						has_technology = tech_plnmg_btc_1
					}
					has_country_flag = star_eater_alert@event_target:crisis_country
					has_country_flag = star_cracker_alert@event_target:crisis_country
				}
				intel_level = {
					system = event_target:destroyed_system
					level > none
				}
			}
			country_event = { id = planetmega_pk.82 }
		}
		every_playable_country = {
			limit = {
				is_ai = no
				has_communications = event_target:crisis_country
				NOR = {
					is_same_value = event_target:crisis_country
					has_country_flag = star_eater_alert@event_target:crisis_country
					has_country_flag = star_cracker_alert@event_target:crisis_country
				}
				OR = {
					has_country_flag = can_build_star_eaters
					has_technology = tech_btc_1
					has_technology = tech_plnmg_btc_1
				}
			}
			country_event = { id = planetmega_pk.83 }
		}
		every_playable_country = {
			limit = {
				has_communications = event_target:crisis_country
				NOT = {
					has_opinion_modifier = {
						who = event_target:crisis_country
						modifier = opinion_destroying_systems
					}
				}
				NOR = {
					is_in_federation_with = event_target:crisis_country
					has_overlord = event_target:crisis_country
				}
			}
			add_opinion_modifier = {
				who = event_target:crisis_country
				modifier = opinion_destroying_systems
			}
		}
		solar_system = {
			random_system_planet = {
				limit = { has_modifier = holy_planet }
				carrier_event = { id = planet_destruction.605 }
			}
			destroy_star_system = yes
		}

		#vanilla compatibility
		carrier_event = { id = awareness.150 scopes = { from = from } }
	}
}

country_event = {
	id = planetmega_pk.81
	title = "crisis.6180.name"
	desc = {
		trigger = { has_communications = event_target:crisis_country }
		text = planetmega_pk.81.desc
	}
	desc = {
		trigger = { not = { has_communications = event_target:crisis_country } }
		text = planetmega_pk.81.desc.unknown
	}
	picture = GFX_evt_supernova
	show_sound = event_super_explosion
	location = event_target:destroyed_system
	is_triggered_only = yes

	immediate = {
		set_country_flag = star_cracker_alert@event_target:crisis_country
	}

	option = {
		name = crisis.6180.a
		trigger = { has_communications = event_target:crisis_country }
	}
	option = {
		name = crisis.6180.b
		trigger = { not = { has_communications = event_target:crisis_country } }
	}
}

country_event = {
	id = planetmega_pk.82
	title = "crisis.6181.name"
	desc = "planetmega_pk.82.desc"
	picture = GFX_evt_supernova
	show_sound = event_super_explosion
	location = event_target:destroyed_system

	is_triggered_only = yes

	immediate = {
		set_country_flag = star_cracker_alert@event_target:crisis_country
	}

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

country_event = {
	id = planetmega_pk.83
	title = "planetmega_pk.83.name"
	desc = "planetmega_pk.83.desc"
	picture = GFX_evt_supernova
	show_sound = event_super_explosion
	location = event_target:destroyed_system

	is_triggered_only = yes

	immediate = {
		set_country_flag = star_cracker_alert@event_target:crisis_country
	}

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

#supercom & brain
planet_event = {
	id = planetmega_pk.90
	hide_window = yes
	is_triggered_only = yes
	immediate = {
		save_event_target_as = pop_merged_planet
		from = {
			save_event_target_as = pop_merger_fleet
			if = {
				limit = {
					any_owned_ship = {
						or = {
							has_component = PLANET_KILLER_PLNMG_MIND_INTEGRATOR
							has_component = PLANET_KILLER_PLNMG_MIND_VANILLA
						}
					}
				}
				root = { set_planet_flag = plnmg_supercom_absorption }
			}
			if = {
				limit = {
					any_owned_ship = {
						or = {
							has_component = PLANET_KILLER_PLNMG_BRAIN_MERGER
							has_component = PLANET_KILLER_PLNMG_BRAIN_VANILLA
						}
					}
				}
				root = { set_planet_flag = plnmg_brain_absorption }
			}
			if = {
				limit = {
					any_owned_ship = {
						or = {
							has_component = PLANET_KILLER_PLNMG_BRAIN_MERGER
							has_component = PLANET_KILLER_PLNMG_MIND_INTEGRATOR
						}
					}
				}
				root = { set_planet_flag = AEG_enabled }
			}
		}
		from.owner = {
			save_event_target_as = pop_merger_country
			if = {
				limit = {
					or = {
						and = {
							any_owned_colony = { has_deposit = d_plnmg_supercom }
							root = { has_planet_flag = plnmg_supercom_absorption }
						}
						and = {
							any_owned_colony = { has_deposit = d_plnmg_fusion_brain }
							root = { has_planet_flag = plnmg_brain_absorption }
						}
					}
				}
				country_event = { id = planetmega_pk.91 }
			}
			else = { country_event = { id = planetmega_pk.94 } }
		}
	}
}

country_event = {
	id = planetmega_pk.91
	is_triggered_only = yes
	title = planetmega_pk.91.name
	desc = {
		trigger = { event_target:pop_merged_planet = { has_planet_flag = plnmg_supercom_absorption } }
		text = planetmega_pk.91.supercom
	}
	desc = {
		trigger = { event_target:pop_merged_planet = { has_planet_flag = plnmg_brain_absorption } }
		text = planetmega_pk.91.brain
	}
	picture = GFX_evt_planet_beam
	location = event_target:pop_merged_planet

	option = {
		name = {
			trigger = { event_target:pop_merged_planet = { has_planet_flag = plnmg_supercom_absorption } }
			text = planetmega_pk.91.all_supercom
		}
		name = {
			trigger = { event_target:pop_merged_planet = { has_planet_flag = plnmg_brain_absorption } }
			text = planetmega_pk.91.all_brain
		}
		allow = { event_target:pop_merged_planet = { pop_amount >= 100 } }
		hidden_effect = {
			event_target:pop_merged_planet = { set_planet_flag = plnmg_all_absorb }
			country_event = { id = planetmega_pk.92 }
		}
	}

	option = {
		name = planetmega_pk.91.1out
		allow = { event_target:pop_merged_planet = { pop_amount >= 100 } }
		hidden_effect = {
			event_target:pop_merged_planet = { set_planet_flag = plnmg_1out_absorb }
			country_event = { id = planetmega_pk.92 }
		}
	}

	option = {
		name = {
			trigger = { event_target:pop_merged_planet = { has_planet_flag = plnmg_supercom_absorption } }
			text = planetmega_pk.91.half_supercom
		}
		name = {
			trigger = { event_target:pop_merged_planet = { has_planet_flag = plnmg_brain_absorption } }
			text = planetmega_pk.91.half_brain
		}
		allow = { event_target:pop_merged_planet = { pop_amount >= 100 } }
		hidden_effect = {
			event_target:pop_merged_planet = { set_planet_flag = plnmg_half_absorb }
			country_event = { id = planetmega_pk.92 }
		}
	}

	option = {
		name = planetmega_pk.91.backspace
		hidden_effect = {
			event_target:pop_merged_planet = {
				if = {
					limit = { has_planet_flag = plnmg_supercom_absorption }
					remove_planet_flag = plnmg_supercom_absorption
				}
				if = {
					limit = { has_planet_flag = plnmg_brain_absorption }
					remove_planet_flag = plnmg_brain_absorption
				}
				if = {
					limit = { has_planet_flag = AEG_enabled }
					remove_planet_flag = AEG_enabled
				}
			}
		}
	}
}

country_event = {
	id = planetmega_pk.92
	hide_window = yes
	is_triggered_only = yes

	immediate = {
		event_target:pop_merger_fleet = { save_event_target_as = fire_launcher }
		event_target:pop_merged_planet = { save_event_target_as = fired_planet }
		if = {
			limit = { event_target:pop_merged_planet = { has_planet_flag = AEG_enabled } }
			plnmg_aeg_colossus_fire_with_check = { event = planetmega_pk.95 }
		}
		else = {
			event_target:pop_merged_planet = { planet_event = { id = planetmega_pk.95 } }
		}
	}
}

planet_event = {
	id = planetmega_pk.95
	is_triggered_only = yes
	hide_window = yes
	immediate = {
		set_variable = { which = num_plnmg_absorbed_pops value = 0 }
		###below is partially inherited from planet_destruction.300###
		#Dismantle observation post
		if = {
			limit = {
				has_observation_outpost = yes
				has_planet_flag = plnmg_all_absorb
			}
			observation_outpost_owner = {
				country_event = {
					id = planet_destruction.1000
					days = 1
				}
			}
		}
		# Swarm Situation Log counter
		if = {
			limit = {
				owner? = { is_country_type = swarm }
				has_planet_flag = plnmg_all_absorb
			}
			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 = {
				has_planet_flag = plnmg_all_absorb
				OR = {
					is_active_resolution = "resolution_rulesofwar_independent_tribunals"
					is_active_resolution = "resolution_rulesofwar_last_resort_doctrine"
					is_active_resolution = "resolution_rulesofwar_demobilization_initiative"
				}
			}
			event_target:pop_merger_country = {
				set_timed_country_flag = { flag = resolution_breached_fired_neutron days = 3600 }
			}
		}

		if = {
			limit = {
				owner? = {
					NOT = { is_same_value = event_target:pop_merger_country }
					OR = {
						is_country_type = default
						is_country_type = fallen_empire
						is_country_type = awakened_fallen_empire
					}
				}
			}
			# Generate threat
			if = {
				limit = { has_planet_flag = plnmg_all_absorb }
				add_threat = { who = event_target:pop_merger_country amount = 3 }
			}
			if = {
				limit = { has_planet_flag = plnmg_1out_absorb }
				add_threat = { who = event_target:pop_merger_country amount = 2 }
			}
			if = {
				limit = { has_planet_flag = plnmg_half_absorb }
				add_threat = { who = event_target:pop_merger_country amount = 1 }
			}
			# modifier for allies + those upset by genocide
			every_country = {
				limit = {
					NOR = {
						is_same_value = event_target:pop_merger_country
						is_same_value = prev.owner
						AND = {
							has_federation = yes
							is_in_federation_with = event_target:pop_merger_country
						}
					}
					OR = {
						has_communications = event_target:pop_merger_country
						has_communications = prev.owner
					}
					OR = {
						AND = {
							has_federation = yes
							is_in_federation_with = prev.owner
						}
						has_ai_personality = awakened_fallen_empire_xenophile
						AND = {
							is_country_type = default
							OR = {
								is_egalitarian = yes
								is_xenophile = yes
								is_spiritualist = yes
							}
						}
					}
				}
				if = {
					limit = { event_target:pop_merged_planet = { has_planet_flag = plnmg_all_absorb } }
					add_opinion_modifier = {
						modifier = opinion_swept_a_world
						who = event_target:pop_merger_country
					}
				}
				if = {
					limit = { event_target:pop_merged_planet = { has_planet_flag = plnmg_1out_absorb } }
					add_opinion_modifier = {
						modifier = opinion_plnmg_absorbed_a_world
						who = event_target:pop_merger_country
					}
				}
				if = {
					limit = { event_target:pop_merged_planet = { has_planet_flag = plnmg_half_absorb } }
					add_opinion_modifier = {
						modifier = opinion_plnmg_absorbed_a_world_sustainable
						who = event_target:pop_merger_country
					}
				}
			}
			# modifiers and effects for victim
			if = {
				limit = { owner? = { not = { is_same_value = event_target:pop_merger_country } } }
				owner = {
					if = {
						limit = { is_gestalt = no }
						add_modifier = {
							modifier = colossus_victim
							days = 10800 # 30 years
						}
					}
					if = {
						limit = { event_target:pop_merged_planet = { has_planet_flag = plnmg_all_absorb } }
						add_opinion_modifier = {
							modifier = opinion_swept_my_world
							who = event_target:pop_merger_country
						}
					}
					if = {
						limit = { event_target:pop_merged_planet = { has_planet_flag = plnmg_1out_absorb } }
						add_opinion_modifier = {
							modifier = opinion_plnmg_absorbed_my_world
							who = event_target:pop_merger_country
						}
					}
					if = {
						limit = { event_target:pop_merged_planet = { has_planet_flag = plnmg_half_absorb } }
						add_opinion_modifier = {
							modifier = opinion_plnmg_absorbed_a_world_sustainable
							who = event_target:pop_merger_country
						}
					}
				}
			}
		}
		if = {
			limit = {
				not = { is_within_borders_of = event_target:pop_merger_country }
				owner? = { is_country_type = primitive }
			}
			# modifier for allies + those upset by genocide
			every_country = {
				limit = {
					NOT = { is_same_value = event_target:pop_merger_country }
					OR = {
						has_communications = event_target:pop_merger_country
						has_communications = event_target:pop_merged_planet.owner
					}
					is_country_type = default
					NOR = {
						is_xenophobe = yes
						is_homicidal = yes
						is_xenophile = yes
					}
				}
				if = {
					limit = { event_target:pop_merged_planet = { has_planet_flag = plnmg_all_absorb } }
					add_opinion_modifier = {
						modifier = opinion_swept_a_primitive_world
						who = event_target:pop_merger_country
					}
				}
				if = {
					limit = {
						event_target:pop_merged_planet = {
							or = {
								has_planet_flag = plnmg_half_absorb
								has_planet_flag = plnmg_1out_absorb
							}
						}
					}
					add_opinion_modifier = {
						modifier = opinion_plnmg_absorbed_a_primitive_world
						who = event_target:pop_merger_country
					}
				}
			}
			every_country = {
				limit = {
					NOT = { is_same_value = event_target:pop_merger_country }
					OR = {
						has_communications = event_target:pop_merger_country
						has_communications = event_target:pop_merged_planet.owner
					}
					OR = {
						has_ai_personality = awakened_fallen_empire_xenophile
						AND = {
							is_country_type = default
							is_xenophile = yes
						}
					}
				}
				if = {
					limit = { event_target:pop_merged_planet = { has_planet_flag = plnmg_all_absorb } }
					add_opinion_modifier = {
						modifier = opinion_swept_a_primitive_world_phile
						who = event_target:pop_merger_country
					}
				}
				if = {
					limit = {
						event_target:pop_merged_planet = {
							or = {
								has_planet_flag = plnmg_half_absorb
								has_planet_flag = plnmg_1out_absorb
							}
						}
					}
					add_opinion_modifier = {
						modifier = opinion_plnmg_absorbed_a_primitive_world_phile
						who = event_target:pop_merger_country
					}
				}
			}
		}
		# 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 = event_target:pop_merger_country
					}
					add_static_war_exhaustion = {
						attacker = event_target:pop_merger_country
						location = event_target:pop_merged_planet
						value_for_planet_destruction = 1.0
					}
				}
			}
		}
		add_modifier = {
			modifier = planetmega_absorbed
			days = 3600
		}
		# Special graphics for Habitats and Ring Worlds
		if = {
			limit = {
				has_planet_flag = plnmg_all_absorb
				is_planet_class = pc_habitat
			}
			spawn_habitat_neutron_effect = yes
		}
		if = {
			limit = {
				has_planet_flag = plnmg_all_absorb
				is_planet_class = pc_ringworld_habitable
			}
			spawn_ringworld_neutron_effect = yes
		}
		if = {
			limit = {
				has_planet_flag = plnmg_all_absorb
				is_planet_class = pc_cosmogenesis_world
			}
			spawn_lathe_neutron_effect = yes
		}
		#if = {
		#	limit = {
		#		#has_planet_flag = plnmg_all_absorb
		#		OR = {
		#			is_planet_class = pc_habitat
		#			is_planet_class = pc_ringworld_habitable
		#		}
		#	}
		#	# nothing
		#}
		#else = {
		#	change_pc = this
		#}
		# For Infested worlds
		if = {
			limit = {
				has_planet_flag = plnmg_all_absorb
				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 }
		}
		event_target:pop_merger_country = { plnmg_purge_with_mwc = { VARIABLE = prev.num_plnmg_absorbed_pops } }

		###original part###
		this = {
			#all absorb
			if = {
				limit = { has_planet_flag = plnmg_all_absorb }
				if = {
					limit = {
						or = {
							has_planet_flag = plnmg_supercom_absorption
							and = {
								has_planet_flag = plnmg_brain_absorption
								event_target:pop_merger_country = { has_technology = tech_planetmega_junik }
							}
						}
					}
					export_trigger_value_to_variable = {
						trigger = pop_amount
						variable = num_plnmg_absorbed_pops
					}
					every_owned_pop_group = { kill_all_pop = yes }
				}
				else_if = {
					limit = {
						has_planet_flag = plnmg_brain_absorption
						event_target:pop_merger_country = { not = { has_technology = tech_planetmega_junik } }
					}
					export_trigger_value_to_variable = {
						trigger = count_owned_pop_amount
						parameters = { limit = { is_robotic_species = no } }
						variable = num_plnmg_absorbed_pops
					}
					every_owned_pop_group = {
						limit = { is_robotic_species = no }
						kill_all_pop = yes
					}
				}
			}
			#all absorb but left one
			else_if = {
				limit = { has_planet_flag = plnmg_1out_absorb }
				if = {
					limit = {
						or = {
							has_planet_flag = plnmg_supercom_absorption
							and = {
								has_planet_flag = plnmg_brain_absorption
								event_target:pop_merger_country = { has_technology = tech_planetmega_junik }
							}
						}
					}
					export_trigger_value_to_variable = {
						trigger = pop_amount
						variable = num_plnmg_absorbed_pops
					}
					subtract_variable = { which = num_plnmg_absorbed_pops value = 100 }
					plnmg_kill_with_mwc = { VARIABLE = num_plnmg_absorbed_pops }
				}
				else_if = {
					limit = {
						has_planet_flag = plnmg_brain_absorption
						event_target:pop_merger_country = { not = { has_technology = tech_planetmega_junik } }
					}
					export_trigger_value_to_variable = {
						trigger = count_owned_pop_amount
						parameters = { limit = { is_robotic_species = no } }
						variable = num_plnmg_absorbed_pops
					}
					subtract_variable = { which = num_plnmg_absorbed_pops value = 100 }
					plnmg_kill_bio_with_mwc = { VARIABLE = num_plnmg_absorbed_pops }
				}
			}
			#half absorb
			else_if = {
				limit = { has_planet_flag = plnmg_half_absorb }
				if = {
					limit = {
						or = {
							has_planet_flag = plnmg_supercom_absorption
							and = {
								has_planet_flag = plnmg_brain_absorption
								event_target:pop_merger_country = { has_technology = tech_planetmega_junik }
							}
						}
					}
					export_trigger_value_to_variable = {
						trigger = pop_amount
						variable = num_plnmg_absorbed_pops
					}
					divide_variable = { which = num_plnmg_absorbed_pops value = 2 }
					plnmg_round_down_with_checking = { VARIABLE = num_plnmg_absorbed_pops }
					plnmg_kill_with_mwc = { VARIABLE = num_plnmg_absorbed_pops }
				}
				else_if = {
					limit = {
						has_planet_flag = plnmg_brain_absorption
						event_target:pop_merger_country = { not = { has_technology = tech_planetmega_junik } }
					}
					export_trigger_value_to_variable = {
						trigger = count_owned_pop_amount
						parameters = { limit = { is_robotic_species = no } }
						variable = num_plnmg_absorbed_pops
					}
					divide_variable = { which = num_plnmg_absorbed_pops value = 2 }
					plnmg_round_down_with_checking = { VARIABLE = num_plnmg_absorbed_pops }
					plnmg_kill_bio_with_mwc = { VARIABLE = num_plnmg_absorbed_pops }
				}
			}
		}
		###original part is done###
		from = { country_event = { id = planetmega_pk.93 } }

		###below is partially inherited from neutron beam's on_action###
		carrier_event = { id = awareness.150 scopes = { from = event_target:pop_merger_fleet } }
		if = {
			limit = { not = { has_planet_flag = plnmg_all_absorb } }
			carrier_event = { id = origin.3245 scopes = { from = event_target:pop_merger_fleet } }
		}
		if = {
			limit = { has_planet_flag = plnmg_all_absorb }
			planet_event = {
				id = planetmega_pk.97
				scopes = { from = event_target:pop_merger_fleet }
			}
		}
	}
	# picture = GFX_evt_gaia
	# title = planetmega_pk.95.name
	# desc = planetmega_pk.95.desc

	# option = {
	# 	name = YES
	# 	hidden_effect = {
	# 		event_target:pop_merged_planet = {
	# 			change_variable = {
	# 				which = num_plnmg_absorbed_pops
	# 				value = PR_var_plnt_MAN
	# 			}
	# 			event_target:pop_merger_country = { plnmg_purge_with_mwc = { VARIABLE = prev.PR_var_plnt_MAN } }
	# 			set_variable = { which = PR_var_plnt_MAN value = 0 }
	# 		}
	# 	}
	# }
	# option = {
	# 	name = planetmega_pk.95.always
	# 	hidden_effect = {
	# 		set_country_flag = plnmg_always_absorb_PRMAN
	# 		event_target:pop_merged_planet = {
	# 			change_variable = {
	# 				which = num_plnmg_absorbed_pops
	# 				value = PR_var_plnt_MAN
	# 			}
	# 			event_target:pop_merger_country = { plnmg_purge_with_mwc = { VARIABLE = prev.PR_var_plnt_MAN } }
	# 			set_variable = { which = PR_var_plnt_MAN value = 0 }
	# 		}
	# 	}
	# }
	#
	# option = { name = NO }
	# option = {
	# 	name = planetmega_pk.95.never
	# 	hidden_effect = { set_country_flag = plnmg_never_absorb_PRMAN }
	# }
	#
	# after = { hidden_effect = { country_event = { id = planetmega_pk.93 } } }
}

country_event = {
	id = planetmega_pk.96
	is_triggered_only = yes
	hide_window = yes
	trigger = { always = no }
	# immediate = {
	# 	if = {
	# 		limit = { has_country_flag = plnmg_always_absorb_PRMAN }
	# 		event_target:pop_merged_planet = {
	# 			change_variable = {
	# 				which = num_plnmg_absorbed_pops
	# 				value = PR_var_plnt_MAN
	# 			}
	# 			event_target:pop_merger_country = { plnmg_purge_with_mwc = { VARIABLE = prev.PR_var_plnt_MAN } }
	# 			set_variable = { which = PR_var_plnt_MAN value = 0 }
	# 		}
	# 	}
	# 	if = {
	# 		limit = { has_country_flag = plnmg_never_absorb_PRMAN }
	# 		# do nothing
	# 	}
	# 	country_event = { id = planetmega_pk.93 }
	# }
}

country_event = {
	id = planetmega_pk.93
	is_triggered_only = yes
	hide_window = yes
	# title = planetmega_pk.93.name
	# desc = {
	# 	trigger = { event_target:pop_merged_planet = { has_planet_flag = plnmg_supercom_absorption } }
	# 	text = planetmega_pk.93.supercom
	# }
	# desc = {
	# 	trigger = { event_target:pop_merged_planet = { has_planet_flag = plnmg_brain_absorption } }
	# 	text = planetmega_pk.93.brain
	# }
	# picture = GFX_evt_planet_beam
	# location = event_target:pop_merged_planet

	immediate = {
		# if = {
		# 	limit = { event_target:pop_merged_planet = { has_planet_flag = plnmg_supercom_absorption } }
		# 	randomly_choose_plnmg_supercom_planet = { NUMBER = 1 }
		# 	randomly_choose_plnmg_supercom_planet = { NUMBER = 2 }
		# 	randomly_choose_plnmg_supercom_planet = { NUMBER = 3 }
		# }
		# if = {
		# 	limit = { event_target:pop_merged_planet = { has_planet_flag = plnmg_brain_absorption } }
		# 	randomly_choose_plnmg_brain_planet = { NUMBER = 1 }
		# 	randomly_choose_plnmg_brain_planet = { NUMBER = 2 }
		# 	randomly_choose_plnmg_brain_planet = { NUMBER = 3 }
		# }
		# event_target:pop_merged_planet = {
		# 	if = {
		# 		limit = { has_planet_flag = AEG_enabled }
		# 		remove_planet_flag = AEG_enabled
		# 	}
		# }
		if = {
			limit = {
				count_owned_colony = {
					count = 1
					limit = { has_carrier_flag = plnmg_absorption_chosen }
				}
			}
			random_owned_colony = {
				limit = { has_carrier_flag = plnmg_absorption_chosen }
				if = {
					limit = { event_target:pop_merged_planet = { has_planet_flag = plnmg_supercom_absorption } }
					initialize_mys_variable = { VAR = num_plnmg_supercom_sacrifice }
					change_variable = {
						which = num_plnmg_supercom_sacrifice
						value = event_target:pop_merged_planet.num_plnmg_absorbed_pops
					}
					event_target:pop_merged_planet = { remove_planet_flag = plnmg_supercom_absorption }
				}
				if = {
					limit = { event_target:pop_merged_planet = { has_planet_flag = plnmg_brain_absorption } }
					initialize_mys_variable = { VAR = num_plnmg_brain_sacrifice }
					change_variable = {
						which = num_plnmg_brain_sacrifice
						value = event_target:pop_merged_planet.num_plnmg_absorbed_pops
					}
					event_target:pop_merged_planet = { remove_planet_flag = plnmg_brain_absorption }
				}
				event_target:pop_merged_planet = {
					clear_variable = num_plnmg_absorbed_pops
					if = {
						limit = { has_planet_flag = plnmg_all_absorb }
						remove_planet_flag = plnmg_all_absorb
					}
					if = {
						limit = { has_planet_flag = plnmg_1out_absorb }
						remove_planet_flag = plnmg_1out_absorb
					}
					if = {
						limit = { has_planet_flag = plnmg_half_absorb }
						remove_planet_flag = plnmg_half_absorb
					}
					if = {
						limit = { has_planet_flag = AEG_enabled }
						remove_planet_flag = AEG_enabled
					}
				}
			}
		}
		else = { country_event = { id = planetmega_pk.98 } }
	}

	# option = {
	# 	name = planetmega_pk.93.planet1
	# 	trigger = { exists = event_target:plnmg_chosen_planet_1 }
	# 	event_target:plnmg_chosen_planet_1 = {
	# 		custom_tooltip = planetmega_pk.93.planet_effect_desc
	# 		hidden_effect = {
	# 			if = {
	# 				limit = { event_target:pop_merged_planet = { has_planet_flag = plnmg_supercom_absorption } }
	# 				change_variable = {
	# 					which = num_plnmg_supercom_sacrifice
	# 					value = event_target:pop_merged_planet.num_plnmg_absorbed_pops
	# 				}
	# 				event_target:pop_merged_planet = { remove_planet_flag = plnmg_supercom_absorption }
	# 			}
	# 			if = {
	# 				limit = { event_target:pop_merged_planet = { has_planet_flag = plnmg_brain_absorption } }
	# 				change_variable = {
	# 					which = num_plnmg_brain_sacrifice
	# 					value = event_target:pop_merged_planet.num_plnmg_absorbed_pops
	# 				}
	# 				event_target:pop_merged_planet = { remove_planet_flag = plnmg_brain_absorption }
	# 			}
	# 			event_target:pop_merged_planet = {
	# 				clear_variable = num_plnmg_absorbed_pops
	# 				if = {
	# 					limit = { has_planet_flag = plnmg_all_absorb }
	# 					remove_planet_flag = plnmg_all_absorb
	# 				}
	# 				if = {
	# 					limit = { has_planet_flag = plnmg_1out_absorb }
	# 					remove_planet_flag = plnmg_1out_absorb
	# 				}
	# 				if = {
	# 					limit = { has_planet_flag = plnmg_half_absorb }
	# 					remove_planet_flag = plnmg_half_absorb
	# 				}
	# 			}
	# 			event_target:pop_merger_country = {
	# 				every_owned_colony = {
	# 					limit = { has_planet_flag = plnmg_absorption_chosen }
	# 					remove_planet_flag = plnmg_absorption_chosen
	# 				}
	# 			}
	# 		}
	# 	}
	# }
	#
	# option = {
	# 	name = planetmega_pk.93.planet2
	# 	trigger = { exists = event_target:plnmg_chosen_planet_2 }
	# 	event_target:plnmg_chosen_planet_2 = {
	# 		custom_tooltip = planetmega_pk.93.planet_effect_desc
	# 		hidden_effect = {
	# 			if = {
	# 				limit = { event_target:pop_merged_planet = { has_planet_flag = plnmg_supercom_absorption } }
	# 				change_variable = {
	# 					which = num_plnmg_supercom_sacrifice
	# 					value = event_target:pop_merged_planet.num_plnmg_absorbed_pops
	# 				}
	# 				event_target:pop_merged_planet = { remove_planet_flag = plnmg_supercom_absorption }
	# 			}
	# 			if = {
	# 				limit = { event_target:pop_merged_planet = { has_planet_flag = plnmg_brain_absorption } }
	# 				change_variable = {
	# 					which = num_plnmg_brain_sacrifice
	# 					value = event_target:pop_merged_planet.num_plnmg_absorbed_pops
	# 				}
	# 				event_target:pop_merged_planet = { remove_planet_flag = plnmg_brain_absorption }
	# 			}
	# 			event_target:pop_merged_planet = {
	# 				clear_variable = num_plnmg_absorbed_pops
	# 				if = {
	# 					limit = { has_planet_flag = plnmg_all_absorb }
	# 					remove_planet_flag = plnmg_all_absorb
	# 				}
	# 				if = {
	# 					limit = { has_planet_flag = plnmg_1out_absorb }
	# 					remove_planet_flag = plnmg_1out_absorb
	# 				}
	# 				if = {
	# 					limit = { has_planet_flag = plnmg_half_absorb }
	# 					remove_planet_flag = plnmg_half_absorb
	# 				}
	# 			}
	# 			event_target:pop_merger_country = {
	# 				every_owned_colony = {
	# 					limit = { has_planet_flag = plnmg_absorption_chosen }
	# 					remove_planet_flag = plnmg_absorption_chosen
	# 				}
	# 			}
	# 		}
	# 	}
	# }
	#
	# option = {
	# 	name = planetmega_pk.93.planet3
	# 	trigger = { exists = event_target:plnmg_chosen_planet_3 }
	# 	event_target:plnmg_chosen_planet_3 = {
	# 		custom_tooltip = planetmega_pk.93.planet_effect_desc
	# 		hidden_effect = {
	# 			if = {
	# 				limit = { event_target:pop_merged_planet = { has_planet_flag = plnmg_supercom_absorption } }
	# 				change_variable = {
	# 					which = num_plnmg_supercom_sacrifice
	# 					value = event_target:pop_merged_planet.num_plnmg_absorbed_pops
	# 				}
	# 				event_target:pop_merged_planet = { remove_planet_flag = plnmg_supercom_absorption }
	# 			}
	# 			if = {
	# 				limit = { event_target:pop_merged_planet = { has_planet_flag = plnmg_brain_absorption } }
	# 				change_variable = {
	# 					which = num_plnmg_brain_sacrifice
	# 					value = event_target:pop_merged_planet.num_plnmg_absorbed_pops
	# 				}
	# 				event_target:pop_merged_planet = { remove_planet_flag = plnmg_brain_absorption }
	# 			}
	# 			event_target:pop_merged_planet = {
	# 				clear_variable = num_plnmg_absorbed_pops
	# 				if = {
	# 					limit = { has_planet_flag = plnmg_all_absorb }
	# 					remove_planet_flag = plnmg_all_absorb
	# 				}
	# 				if = {
	# 					limit = { has_planet_flag = plnmg_1out_absorb }
	# 					remove_planet_flag = plnmg_1out_absorb
	# 				}
	# 				if = {
	# 					limit = { has_planet_flag = plnmg_half_absorb }
	# 					remove_planet_flag = plnmg_half_absorb
	# 				}
	# 			}
	# 			event_target:pop_merger_country = {
	# 				every_owned_colony = {
	# 					limit = { has_planet_flag = plnmg_absorption_chosen }
	# 					remove_planet_flag = plnmg_absorption_chosen
	# 				}
	# 			}
	# 		}
	# 	}
	# }
	#
	# option = {
	# 	name = planetmega_pk.93.refresh
	# 	hidden_effect = {
	# 		country_event = { id = planetmega_pk.93 }
	# 	}
	# }
	#
	# option = {
	# 	name = planetmega_pk.93.cancel
	# 	custom_tooltip = planetmega_pk.93.cancel.desc
	# 	hidden_effect = {
	# 		event_target:pop_merged_planet = {
	# 			clear_variable = num_plnmg_absorbed_pops
	# 			if = {
	# 				limit = { has_planet_flag = plnmg_supercom_absorption }
	# 				remove_planet_flag = plnmg_supercom_absorption
	# 			}
	# 			if = {
	# 				limit = { has_planet_flag = plnmg_brain_absorption }
	# 				remove_planet_flag = plnmg_brain_absorption
	# 			}
	# 			if = {
	# 				limit = { has_planet_flag = plnmg_all_absorb }
	# 				remove_planet_flag = plnmg_all_absorb
	# 			}
	# 			if = {
	# 				limit = { has_planet_flag = plnmg_1out_absorb }
	# 				remove_planet_flag = plnmg_1out_absorb
	# 			}
	# 			if = {
	# 				limit = { has_planet_flag = plnmg_half_absorb }
	# 				remove_planet_flag = plnmg_half_absorb
	# 			}
	# 		}
	# 		event_target:pop_merger_country = {
	# 			every_owned_colony = {
	# 				limit = { has_planet_flag = plnmg_absorption_chosen }
	# 				remove_planet_flag = plnmg_absorption_chosen
	# 			}
	# 		}
	# 	}
	# }
}

country_event = {
	id = planetmega_pk.98
	title = planetmega_pk.98.name
	desc = planetmega_pk.98.desc
	picture = GFX_evt_planet_beam
	location = event_target:pop_merged_planet
	is_triggered_only = yes
	option = {
		name = planetmega_try_again
		hidden_effect = { country_event = { id = planetmega_pk.93 } }
	}
	option = {
		name = planetmega_pk.93.cancel
		custom_tooltip = planetmega_pk.93.cancel.desc
		hidden_effect = {
			event_target:pop_merged_planet = {
				clear_variable = num_plnmg_absorbed_pops
				if = {
					limit = { has_planet_flag = plnmg_all_absorb }
					remove_planet_flag = plnmg_all_absorb
				}
				if = {
					limit = { has_planet_flag = plnmg_1out_absorb }
					remove_planet_flag = plnmg_1out_absorb
				}
				if = {
					limit = { has_planet_flag = plnmg_half_absorb }
					remove_planet_flag = plnmg_half_absorb
				}
				if = {
					limit = { has_planet_flag = AEG_enabled }
					remove_planet_flag = AEG_enabled
				}
			}
		}
	}
}

country_event = {
	id = planetmega_pk.94
	title = planetmega_pk.94.name
	desc = planetmega_pk.94.desc
	picture = GFX_evt_planet_beam
	is_triggered_only = yes
	location = event_target:pop_merged_planet
	option = { name = CURSES }
	after = {
		hidden_effect = {
			event_target:pop_merged_planet = {
				if = {
					limit = { has_planet_flag = plnmg_supercom_absorption }
					remove_planet_flag = plnmg_supercom_absorption
				}
				if = {
					limit = { has_planet_flag = plnmg_brain_absorption }
					remove_planet_flag = plnmg_brain_absorption
				}
			}
		}
	}
}

planet_event = {
	id = planetmega_pk.97
	title = origin.3245.name
	desc = origin.3245.desc
	picture = GFX_evt_progenitor_throne
	show_sound = evn_ove_progenitor_throne
	is_triggered_only = yes

	trigger = {
		is_capital = yes
		has_owner = yes
		space_owner? = { has_origin = origin_progenitor_hive }
		has_deposit = d_progenitor
	}

	immediate = {
		remove_deposit = d_progenitor
		add_deposit = d_progenitor_empty
	}
	option = {
		name = origin.3245.a
	}
	option = {
		name = origin.3245.b
	}
	option = {
		name = origin.3245.c
	}
	option = {
		name = origin.3245.d
	}
	after = {
		owner = {
			if = {
				limit = {
					NOT = { has_modifier = missing_progenitor_modifier }
				}
				add_modifier = {
					modifier = missing_progenitor_modifier
					days = -1
				}
			}
		}
	}
}

#reach limit
planet_event = {
	id = planetmega_pk.100
	is_triggered_only = yes
	hide_window = yes

	trigger = {
		from.owner = {
			any_owned_colony = { has_deposit = d_plnmg_colossus_assist }
			check_variable = {
				which = num_plnmg_colossus_fired_count
				value > num_plnmg_colossus_fired_limit
			}
		}
	}

	immediate = {
		from.owner = {
			country_event = { id = planetmega_pk.101 }
			set_variable = { which = num_plnmg_colossus_fired_count value = 0 }
			set_variable = { which = num_plnmg_colossus_fired_limit value = 0 }
			every_owned_colony = {
				limit = { has_deposit = d_plnmg_colossus_assist }
				remove_plnmg_mega = { TYPE = plnmg_colossus_assist }
				every_owned_pop_group = { kill_all_pop = yes }
				destroy_colony = yes
				if = {
					limit = { is_artificial = no }
					change_pc = pc_frozen
				}
			}
		}
	}
}
country_event = {
	id = planetmega_pk.101
	title = planetmega_pk.101.name
	desc = planetmega_pk.101.desc
	picture = GFX_evt_frozen
	is_triggered_only = yes
	option = { name = CURSES }
}

planet_event = {
	id = planetmega_pk.102
	hide_window = yes
	is_triggered_only = yes
	trigger = {
		from.owner = { not = { any_owned_colony = { has_deposit = d_plnmg_colossus_assist } } }
	}
	immediate = {
		from.owner = { country_event = { id = planetmega_pk.103 } }
	}
}
country_event = {
	id = planetmega_pk.103
	title = planetmega_pk.102.name
	desc = planetmega_pk.102.desc
	picture = GFX_evt_planet_beam
	location = from
	is_triggered_only = yes
	option = { name = OK }
}

# raiding bombardment stance
# main variable is num_plnmg_unfiltered_bodies
# This = Colony where the pop was bombed to death
# From = Country which is raining down fire and brimstone
# FromFrom = Biggest fleet bombarding the planet (the one we used the bombardment stance from)
# local_kill_pop_amount = amount of pop killed
carrier_event = {
	id = planetmega_pk.110
	is_triggered_only = yes
	hide_window = yes
	trigger = { has_orbital_bombardment_stance = planetmega_body_raiding }
	immediate = {
		save_event_target_as = plnmg_body_raided_planet
		from = { save_event_target_as = plnmg_body_raider_country }
		if = {
			limit = { pop_amount > 200 }
			if = {
				limit = { from = { any_owned_colony = { has_carrier_flag = plnmg_bodyraid_tt } } }
				raid_pop_to_our_planetmega = { variable = local_kill_pop_amount }
			}
			else = { from = { country_event = { id = planetmega_pk.111 } } }
		}
		else = {
			from = {
				if = {
					limit = {
						is_gestalt = no
						root.owner = { is_gestalt = no }
					}
					random_list = {
						3 = { country_event = { id = planetmega_pk.113 } }
						1 = {
                            modifier = {
                                mult = 5
                                any_owned_colony = { has_deposit = d_hakkou_ichiu_tower }
                            }
                            country_event = { id = planetmega_pk.114 }
                        }
					}
                    reroll_random = yes
				}
				else = { country_event = { id = planetmega_pk.113 } }
			}
		}
	}
}

country_event = {
	id = planetmega_pk.111
	title = planetmega_pk.111.name
	desc = planetmega_pk.111.desc
	picture = GFX_evt_night_raid
	is_triggered_only = yes
	#location = event_target:plnmg_body_raided_planet

	option = { name = planetmega_try_again }
}

#unused
# country_event = {
# 	id = planetmega_pk.112
# 	title = planetmega_pk.95.name
# 	desc = planetmega_pk.112.desc
# 	picture = GFX_evt_night_raid
# 	is_triggered_only = yes
# 	location = event_target:plnmg_body_raided_planet
#
# 	immediate = {
# 		event_target:plnmg_body_raided_planet = { set_planet_flag = plnmg_raiding_stopped }
# 	}
#
# 	option = {
# 		name = OK
# 		hidden_effect = {
# 			random_owned_colony = {
# 				limit = { has_planet_flag = plnmg_bodyraid_tt }
# 				if = {
# 					limit = { not = { is_variable_set = num_plnmg_usable_bodies } }
# 					set_variable = { which = num_plnmg_usable_bodies value = 0 }
# 				}
# 				change_variable = {
# 					which = num_plnmg_usable_bodies
# 					value = event_target:plnmg_body_raided_planet.PR_var_plnt_MAN
# 				}
# 				event_target:plnmg_body_raided_planet = {
# 					set_variable = { which = PR_var_plnt_MAN value = 0 }
# 				}
# 			}
# 		}
# 	}
# 	option = {
# 		name = planetmega_pk.111.cancel
# 		default_hide_option = yes
# 		hidden_effect = {
# 			event_target:plnmg_body_raided_planet = {
# 				set_timed_planet_flag = { flag = plnmg_dont_touch years = 1 }
# 			}
# 		}
# 	}
#
# 	after = {
# 		hidden_effect = {
# 			event_target:plnmg_body_raided_planet = { remove_planet_flag = plnmg_raiding_stopped }
# 		}
# 	}
# }

country_event = {
	id = planetmega_pk.113
	title = planetmega_pk.113.name
	desc = planetmega_pk.113.desc
	picture = GFX_evt_night_raid
	is_triggered_only = yes
	location = event_target:plnmg_body_raided_planet
	# trigger = {
	# 	event_target:plnmg_body_raided_planet = {
	# 		not = { has_planet_flag = already_plnmg_organic_fully_raided }
	# 	}
	# }

	option = {
		name = OK
		# hidden_effect = {
		# 	event_target:plnmg_body_raided_planet = {
		# 		set_timed_planet_flag = { flag = already_plnmg_organic_fully_raided years = 1 }
		# 	}
		# }
	}
}

country_event = {
	id = planetmega_pk.114
	title = planetmega_pk.114.name
	desc = planetmega_pk.114.desc
	picture = GFX_evt_night_raid
	is_triggered_only = yes
	location = event_target:plnmg_body_raided_planet
	# trigger = {
	# 	event_target:plnmg_body_raided_planet = {
	# 		not = { has_planet_flag = already_plnmg_organic_fully_raided }
	# 	}
	# }

	option = {
		name = planetmega_pk.114.OK
		add_resource = {
			consumer_goods = 1
			mult = event_target:plnmg_body_raided_planet.value:plnmg_raid_goods_mult
		}
		# hidden_effect = {
		# 	event_target:plnmg_body_raided_planet = {
		# 		set_timed_planet_flag = { flag = already_plnmg_organic_fully_raided years = 1 }
		# 	}
		# }
	}
}

# this = planet, from = country, fromfrom = pop
carrier_event = {
	id = planetmega_pk.115
	is_triggered_only = yes
	hide_window = yes
	trigger = { fromfrom = { has_purge_type = { country = root.from type = purge_plnmg_organic } } }
	immediate = {
		from = {
			if = {
				limit = { any_owned_colony = { has_carrier_flag = plnmg_bodyraid_tt } }
				random_owned_colony = {
					limit = { has_carrier_flag = plnmg_bodyraid_tt }
					initialize_mys_variable = { VAR = num_plnmg_unfiltered_bodies }
					planet = {
                        change_variable = { which = num_plnmg_unfiltered_bodies value = local_pop_amount }
                    }
				}
			}
			else = { country_event = { id = planetmega_pk.111 } }
		}
	}
}

carrier_event = {
    id = planetmega_pk.120
    hide_window = yes
    is_triggered_only = yes
    trigger = { from.owner = { has_plnmg_hakkou_ichiu = yes } }
    immediate = {
        from.owner = {
            initialize_mys_variable = { VAR = var_plnmg_cracked_planets }
            owner = { change_variable = { which = var_plnmg_cracked_planets value = 1 } }
            if = {
                limit = {
                    has_ascension_perk = ap_hydrocentric
                    root.from = {
                        any_owned_ship = { has_component = PLANET_KILLER_DELUGE }
                    }
                }
                change_variable = { which = var_plnmg_cracked_planets value = 1 }
            }
            else_if = {
                limit = {
                    has_ascension_perk = ap_archaeoengineers
                    root.from = {
                        any_owned_ship = { has_component = PLANET_KILLER_DEVOLUTION }
                    }
                }
                change_variable = { which = var_plnmg_cracked_planets value = 1 }
            }
        }
    }
}

planet_event = {
    id = planetmega_pk.121
    hide_window = yes
    is_triggered_only = yes
    trigger = { from.owner = { has_plnmg_hakkou_ichiu = yes } }
    immediate = {
        from.owner = {
            save_event_target_as = vpcp_saver_country
            initialize_mys_variable = { VAR = var_plnmg_cracked_planets }
        }
        solar_system = {
            every_system_planet = {
                limit = {
                    or = {
                        is_planet_class = pc_black_hole
                        is_planet_class = pc_broken
                        is_planet_class = pc_ringworld_habitable_damaged
                        is_planet_class = pc_shattered
                    }
                }
                event_target:vpcp_saver_country = {
                    change_variable = { which = var_plnmg_cracked_planets value = 1 }
                }
            }
            every_system_megastructure = {
                limit = { is_megastructure_type = habitat_central_complex_ruined }
                event_target:vpcp_saver_country = {
                    change_variable = { which = var_plnmg_cracked_planets value = 1 }
                }
            }
        }
    }
}

planet_event = {
    id = planetmega_pk.122
    is_triggered_only = yes
    hide_window = yes
    trigger = { from.owner = { has_plnmg_hakkou_ichiu = yes } }
    immediate = {
		export_trigger_value_to_variable = {
			trigger = count_owned_pop_amount
			parameters = { limit = { is_spiritualist = yes } }
			variable = temp_spiritualist_pop_amount
		}
		multiply_variable = { which = temp_spiritualist_pop_amount value = 0.01 }
		floor_variable = temp_spiritualist_pop_amount
        from.owner = {
            initialize_mys_variable = { VAR = var_plnmg_purged_pops }
			change_variable = { which = var_plnmg_purged_pops value = root.temp_spiritualist_pop_amount }
        }
		clear_variable = temp_spiritualist_pop_amount
    }
}

planet_event = {
    id = planetmega_pk.123
    is_triggered_only = yes
    hide_window = yes
    trigger = { from.owner = { has_plnmg_hakkou_ichiu = yes } }
    immediate = {
        from.owner = {
            save_event_target_as = vpcp_saver_country
            initialize_mys_variable = { VAR = var_plnmg_purged_pops }
        }
        export_trigger_value_to_variable = {
            trigger = count_owned_pop_amount
            parameters = {
                limit = {
                    has_trait = trait_cybernetic
                    or = {
                        has_citizenship_type = {
                            type = citizenship_full_machine
                            country = event_target:vpcp_saver_country
                        }
                        has_citizenship_type = {
                            type = citizenship_full_machine
                            country = owner
                        }
                    }
                }
            }
            variable = var_pop_with_nanobots_temp
        }
        divide_variable = { which = var_pop_with_nanobots_temp value = 100 }
        floor_variable = var_pop_with_nanobots_temp
        event_target:vpcp_saver_country = {
            change_variable = { which = var_plnmg_purged_pops value = prev.var_pop_with_nanobots_temp }
        }
        clear_variable = var_pop_with_nanobots_temp
    }
}

planet_event = {
    id = planetmega_pk.124
    is_triggered_only = yes
    hide_window = yes
    trigger = { from.owner = { has_plnmg_hakkou_ichiu = yes } }
    immediate = {
        from.owner = {
            save_event_target_as = vpcp_saver_country
            initialize_mys_variable = { VAR = var_plnmg_purged_pops }
            owner = {
                change_variable = {
					which = var_plnmg_purged_pops
					value = root.value:per_100_plnmg_devolved_pop_amount
				}
            }
        }
    }
}


###TEST###

country_event = {
	id = planetmega_pk.999
	is_triggered_only = yes
	hide_window = yes
	trigger = { always = no }
	immediate = {
		if = {
			limit = { exists = fromfrom }
			log = "fromfrom is checked"
		}
		else = { log = "only country is checked" }
	}
}

