﻿aspmod_parasite_machine_relic_effect = {
	random_list = {
		18 = { add_modifier = { modifier = mod_aspmod_parasite_machine_relic_1 days = 3600 } }
		18 = { add_modifier = { modifier = mod_aspmod_parasite_machine_relic_2 days = 3600 } }
		18 = { add_modifier = { modifier = mod_aspmod_parasite_machine_relic_3 days = 3600 } }
		18 = { add_modifier = { modifier = mod_aspmod_parasite_machine_relic_4 days = 3600 } }
		18 = { add_modifier = { modifier = mod_aspmod_parasite_machine_relic_5 days = 3600 } }
		10 = {
			if = {
				limit = {
					any_owned_planet = { any_owned_pop_group = { always = yes } }
				}
				random_owned_planet = {
					limit = { any_owned_pop_group = { always = yes } }
					create_message = {
						type = ASPMOD_PARASITE_MACHINE_EXPLODED_MESSAGE_TYPE
						localization = ASPMOD_PARASITE_MACHINE_RELIC_EFFECT_BONUS_FAILURE
						days = 15
						target = this
					}
					add_planet_devastation = 100
					if = {
						limit = { has_district = yes }
						remove_random_district = yes
					}
					while = {
						limit = { any_owned_pop_group = { always = yes } }
						count = 6
						weighted_random_owned_pop_group = { kill_single_pop = yes }
					}
				}
			}
			else = {
				# A planetless country cannot receive the destructive outcome; return a valid relic bonus instead.
				add_modifier = { modifier = mod_aspmod_parasite_machine_relic_1 days = 3600 }
			}
		}
	}
}
aspmod_parasite_machine_relic_2_effect = {
	random_list = {
		20 = { add_modifier = { modifier = mod_aspmod_parasite_machine_relic_1_alt days = 3600 } }
		20 = { add_modifier = { modifier = mod_aspmod_parasite_machine_relic_2_alt days = 3600 } }
		20 = { add_modifier = { modifier = mod_aspmod_parasite_machine_relic_3_alt days = 3600 } }
		20 = { add_modifier = { modifier = mod_aspmod_parasite_machine_relic_4_alt days = 3600 } }
		20 = { add_modifier = { modifier = mod_aspmod_parasite_machine_relic_5_alt days = 3600 } }
	}
}
aspmod_geo_centralizer_colossus_effect = {
	save_event_target_as = swaped_target_world
	if = {
		limit = {
			NOR = {
				is_planet_class = pc_habitat
				is_planet_class = pc_ringworld_habitable
			}
		}
		solar_system = {
			if = {
				limit = { NOR = { has_star_flag = already_swaped_system star = { is_planet_class = pc_black_hole } } }
				if = {
					limit = {
						OR = {
							has_planet_class = pc_b_star
							has_planet_class = pc_a_star
							has_planet_class = pc_f_star
							has_planet_class = pc_g_star
							has_planet_class = pc_k_star
							has_planet_class = pc_m_star
							has_planet_class = pc_t_star
							has_planet_class = pc_pulsar
							has_planet_class = pc_m_giant_star
							has_planet_class = pc_neutron_star
						}
					}
					switch = {
						trigger = has_planet_class
						pc_b_star = { spawn_planet = { class = pc_b_star location = prev init_effect = { set_name = random set_surveyed = { surveyed = yes surveyor = from.owner } } } }
						pc_a_star = { spawn_planet = { class = pc_a_star location = prev init_effect = { set_name = random set_surveyed = { surveyed = yes surveyor = from.owner } } } }
						pc_f_star = { spawn_planet = { class = pc_f_star location = prev init_effect = { set_name = random set_surveyed = { surveyed = yes surveyor = from.owner } } } }
						pc_g_star = { spawn_planet = { class = pc_g_star location = prev init_effect = { set_name = random set_surveyed = { surveyed = yes surveyor = from.owner } } } }
						pc_k_star = { spawn_planet = { class = pc_k_star location = prev init_effect = { set_name = random set_surveyed = { surveyed = yes surveyor = from.owner } } } }

						pc_m_star = { spawn_planet = { class = pc_m_star location = prev init_effect = { set_name = random set_surveyed = { surveyed = yes surveyor = from.owner } } } }
						pc_t_star = { spawn_planet = { class = pc_t_star location = prev init_effect = { set_name = random set_surveyed = { surveyed = yes surveyor = from.owner } } } }
						pc_pulsar = { spawn_planet = { class = pc_pulsar location = prev init_effect = { set_name = random set_surveyed = { surveyed = yes surveyor = from.owner } } } }
						pc_m_giant_star = { spawn_planet = { class = pc_m_giant_star location = prev init_effect = { set_name = random set_surveyed = { surveyed = yes surveyor = from.owner } } } }
						pc_neutron_star = { spawn_planet = { class = pc_neutron_star location = prev init_effect = { set_name = random set_surveyed = { surveyed = yes surveyor = from.owner } } } }
					}
					else = {
						spawn_planet = { class = pc_pulsar location = prev init_effect = { set_planet_size = 25 set_name = random set_surveyed = { surveyed = yes surveyor = from.owner } } }
					}
				}
				spawn_planet = {
					class = pc_barren_cold
					size = 25
					orbit_distance = 0.01 #intentional tiny offset for this star-replacement presentation; retain until runtime visual regression proves 0 safe here
					has_ring = no
					init_effect = {
						set_name = event_target:swaped_target_world # Cygnus 4.5.0: event-target naming is current and preserves the target world name
						clear_planet_modifiers = yes
						save_event_target_as = already_swaped_world
						random_list = {
							1 = {
								add_deposit = d_minerals_3
								add_deposit = d_minerals_3
								add_deposit = d_minerals_3
								add_deposit = d_minerals_3
								add_deposit = d_minerals_3
							}
							1 = {
								add_deposit = d_minerals_3
								add_deposit = d_minerals_3
								add_deposit = d_minerals_3
								add_deposit = d_minerals_3
							}
							1 = {
								add_deposit = d_minerals_3
								add_deposit = d_minerals_3
								add_deposit = d_minerals_3
							}
							1 = {
								add_deposit = d_minerals_3
								add_deposit = d_minerals_3
							}
						}
						set_surveyed = { surveyed = yes surveyor = from.owner }
					}
				}
				star = {
					clear_deposits = yes
					set_planet_size = 1 #intentional hidden-star carrier size; keep nonzero until this transformation is runtime-certified
				}
				set_star_flag = already_swaped_system #shouldn't swap star with a planet more than once
				prev = {
					remove_planet = yes
					from = {
						set_location = {
							target = event_target:already_swaped_world
							distance = 0
						}
					}
				}
			}
			if = {
				limit = { OR = { has_star_flag = already_swaped_system star = { is_planet_class = pc_black_hole } } }
				prev = {
					kill_all_pop = yes
					remove_all_buildings = yes
					change_pc = pc_barren_cold
				}
			}
		}
	}
	else_if = {
		limit = { is_planet_class = pc_habitat }
		clear_consecration_effect = yes
		# Cygnus helper owns the terminal remove_planet operation.
		spawn_habitat_cracker_effect = yes
	}
	else_if = {
		limit = { is_planet_class = pc_ringworld_habitable }
		spawn_ringworld_cracker_effect = yes
		change_pc = pc_ringworld_habitable_damaged
	}
}

# Cygnus 4.5.0 relic-owned Geo-Centralizer path.
# Current scope is the activating empire's capital planet; actor identity is captured from planet.owner
# before the destructive transform. This deliberately does not reuse the Colossus fleet-owned FROM contract.
aspmod_geo_centralizer_relic_effect = {
	save_event_target_as = aspmod_geocentric_relic_target_world
	owner = { save_event_target_as = aspmod_geocentric_relic_actor }
	if = {
		limit = {
			NOR = {
				is_planet_class = pc_habitat
				is_planet_class = pc_ringworld_habitable
			}
		}
		solar_system = {
			if = {
				limit = { NOR = { has_star_flag = already_swaped_system star = { is_planet_class = pc_black_hole } } }
				if = {
					limit = {
						OR = {
							has_planet_class = pc_b_star
							has_planet_class = pc_a_star
							has_planet_class = pc_f_star
							has_planet_class = pc_g_star
							has_planet_class = pc_k_star
							has_planet_class = pc_m_star
							has_planet_class = pc_t_star
							has_planet_class = pc_pulsar
							has_planet_class = pc_m_giant_star
							has_planet_class = pc_neutron_star
						}
					}
					switch = {
						trigger = has_planet_class
						pc_b_star = { spawn_planet = { class = pc_b_star location = prev init_effect = { set_name = random set_surveyed = { surveyed = yes surveyor = event_target:aspmod_geocentric_relic_actor } } } }
						pc_a_star = { spawn_planet = { class = pc_a_star location = prev init_effect = { set_name = random set_surveyed = { surveyed = yes surveyor = event_target:aspmod_geocentric_relic_actor } } } }
						pc_f_star = { spawn_planet = { class = pc_f_star location = prev init_effect = { set_name = random set_surveyed = { surveyed = yes surveyor = event_target:aspmod_geocentric_relic_actor } } } }
						pc_g_star = { spawn_planet = { class = pc_g_star location = prev init_effect = { set_name = random set_surveyed = { surveyed = yes surveyor = event_target:aspmod_geocentric_relic_actor } } } }
						pc_k_star = { spawn_planet = { class = pc_k_star location = prev init_effect = { set_name = random set_surveyed = { surveyed = yes surveyor = event_target:aspmod_geocentric_relic_actor } } } }

						pc_m_star = { spawn_planet = { class = pc_m_star location = prev init_effect = { set_name = random set_surveyed = { surveyed = yes surveyor = event_target:aspmod_geocentric_relic_actor } } } }
						pc_t_star = { spawn_planet = { class = pc_t_star location = prev init_effect = { set_name = random set_surveyed = { surveyed = yes surveyor = event_target:aspmod_geocentric_relic_actor } } } }
						pc_pulsar = { spawn_planet = { class = pc_pulsar location = prev init_effect = { set_name = random set_surveyed = { surveyed = yes surveyor = event_target:aspmod_geocentric_relic_actor } } } }
						pc_m_giant_star = { spawn_planet = { class = pc_m_giant_star location = prev init_effect = { set_name = random set_surveyed = { surveyed = yes surveyor = event_target:aspmod_geocentric_relic_actor } } } }
						pc_neutron_star = { spawn_planet = { class = pc_neutron_star location = prev init_effect = { set_name = random set_surveyed = { surveyed = yes surveyor = event_target:aspmod_geocentric_relic_actor } } } }
					}
					else = {
						spawn_planet = { class = pc_pulsar location = prev init_effect = { set_planet_size = 25 set_name = random set_surveyed = { surveyed = yes surveyor = event_target:aspmod_geocentric_relic_actor } } }
					}
				}
				spawn_planet = {
					class = pc_barren_cold
					size = 25
					orbit_distance = 0.01 #intentional tiny offset for this star-replacement presentation; retain until runtime visual regression proves 0 safe here
					has_ring = no
					init_effect = {
						set_name = event_target:aspmod_geocentric_relic_target_world # Cygnus 4.5.0: event-target naming is current and preserves the target world name
						clear_planet_modifiers = yes
						save_event_target_as = already_swaped_world
						random_list = {
							1 = {
								add_deposit = d_minerals_3
								add_deposit = d_minerals_3
								add_deposit = d_minerals_3
								add_deposit = d_minerals_3
								add_deposit = d_minerals_3
							}
							1 = {
								add_deposit = d_minerals_3
								add_deposit = d_minerals_3
								add_deposit = d_minerals_3
								add_deposit = d_minerals_3
							}
							1 = {
								add_deposit = d_minerals_3
								add_deposit = d_minerals_3
								add_deposit = d_minerals_3
							}
							1 = {
								add_deposit = d_minerals_3
								add_deposit = d_minerals_3
							}
						}
						set_surveyed = { surveyed = yes surveyor = event_target:aspmod_geocentric_relic_actor }
					}
				}
				star = {
					clear_deposits = yes
					set_planet_size = 1 #intentional hidden-star carrier size; keep nonzero until this transformation is runtime-certified
				}
				set_star_flag = already_swaped_system #shouldn't swap star with a planet more than once
				prev = {
					# Relic activation has no Colossus/fleet FROM scope to reposition.
					remove_planet = yes
				}
			}
			if = {
				limit = { OR = { has_star_flag = already_swaped_system star = { is_planet_class = pc_black_hole } } }
				prev = {
					kill_all_pop = yes
					remove_all_buildings = yes
					change_pc = pc_barren_cold
				}
			}
		}
	}
	else_if = {
		limit = { is_planet_class = pc_habitat }
		clear_consecration_effect = yes
		# Cygnus helper owns the terminal remove_planet operation.
		spawn_habitat_cracker_effect = yes
	}
	else_if = {
		limit = { is_planet_class = pc_ringworld_habitable }
		spawn_ringworld_cracker_effect = yes
		change_pc = pc_ringworld_habitable_damaged
	}
}

# Legacy alias for any external/scripted call that still expects the original effect name.
# Valid only when called from planet-killer context where from = fleet.
aspmod_geo_centralizer_effect = {
	aspmod_geo_centralizer_colossus_effect = yes
}

# RC5: exact Cygnus 4.5.0 non-terminal World Cracker transaction surface.
# Scope contract is the successful Planet Killer hook: ROOT/THIS = target carrier, FROM = firing fleet.
# Narrative/physical cracker endpoint is intentionally excluded; Geocentric owns its own terminal transform.
aspmod_geocentric_colossus_common_consequences = {
	#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
				}
			}
		}
	}
}

