﻿namespace = aspetc

event = {
	id = aspetc.1
	hide_window = yes
	is_triggered_only = yes

	immediate = {
		set_global_flag = xvcv_aspmod_active
		# Fresh games have no pre-RC3 repair state to discover. Old saves do not refire on_game_start.
		set_global_flag = aspmod_rc3d_repair_migration_scanned
	}
}
event = {
	id = aspetc.2
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		NOR = {
			any_system = {
				OR = {
					has_star_flag = sol
					has_star_flag = sol_system
				}
			}
			#if the player empire is human
			any_country = {
				is_ai = no
				is_human_species = yes
			}
		}
	}

	immediate = {
		set_global_flag = asp_beyond_earth_purity_story_human_unlocked
	}
}
event = {
	id = aspetc.3
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		NOR = {
			any_system = { has_star_flag = deneb_system }
			any_country = {
				is_ai = no
				is_human_species = yes
			}
		}
	}

	immediate = {
		set_global_flag = asp_kessler_syndrome_story_human_unlocked
	}
}

namespace = asp_on_action

country_event = {
	id = asp_on_action.1
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		exists = from
		exists = fromfrom
		exists = event_target:ASP_site_beyond_earth_purity_planet_2
		OR = {
			has_special_project = ASP_BEYOND_EARTH_PURITY_PROJECT
			has_special_project = ASP_BEYOND_EARTH_PURITY_PROJECT_2
		}
		from = { any_system_planet = { is_same_value = event_target:ASP_site_beyond_earth_purity_planet_2 } }
	}

	immediate = {
		set_country_flag = aspmod_beyond_earth_project_transfer
		if = {
			limit = { has_special_project = ASP_BEYOND_EARTH_PURITY_PROJECT }
			abort_special_project = { type = ASP_BEYOND_EARTH_PURITY_PROJECT }
			enable_special_project = {
				name = ASP_BEYOND_EARTH_PURITY_PROJECT
				location = event_target:ASP_site_beyond_earth_purity_planet_2
				owner = fromfrom
			}
		}
		if = {
			limit = { has_special_project = ASP_BEYOND_EARTH_PURITY_PROJECT_2 }
			abort_special_project = { type = ASP_BEYOND_EARTH_PURITY_PROJECT_2 }
			enable_special_project = {
				name = ASP_BEYOND_EARTH_PURITY_PROJECT_2
				location = event_target:ASP_site_beyond_earth_purity_planet_2
				owner = fromfrom
			}
		}
		remove_country_flag = aspmod_beyond_earth_project_transfer
	}
}

# Infinite Observatory reward: immediate legal ownership change.
country_event = {
	id = asp_on_action.2
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		exists = from
		from = { any_system_planet = { has_modifier = aspmod_infinite_observatory_planet } }
	}

	immediate = {
		every_country = {
			limit = { has_modifier = aspmod_infinite_observatory_country }
			remove_modifier = aspmod_infinite_observatory_country
		}
		if = {
			limit = { exists = fromfrom }
			fromfrom = { add_modifier = { modifier = aspmod_infinite_observatory_country days = -1 } }
		}
	}
}

# Infinite Observatory reward: gained-system fallback, including old on_action ordering.
country_event = {
	id = asp_on_action.3
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		exists = from
		from = { any_system_planet = { has_modifier = aspmod_infinite_observatory_planet } }
	}

	immediate = {
		every_country = {
			limit = { has_modifier = aspmod_infinite_observatory_country }
			remove_modifier = aspmod_infinite_observatory_country
		}
		add_modifier = { modifier = aspmod_infinite_observatory_country days = -1 }
	}
}

# Infinite Observatory reward: temporary system occupation/controller change.
system_event = {
	id = asp_on_action.4
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		any_system_planet = { has_modifier = aspmod_infinite_observatory_planet }
	}

	immediate = {
		every_country = {
			limit = { has_modifier = aspmod_infinite_observatory_country }
			remove_modifier = aspmod_infinite_observatory_country
		}
		if = {
			limit = { exists = from }
			from = { add_modifier = { modifier = aspmod_infinite_observatory_country days = -1 } }
		}
	}
}

# Annual idempotent repair for active legacy-sensitive rewards.
# Galaxy scans are gated behind durable global state; each repair family retires
# after its last source planet disappears. Old saves are discovered once by asp_game_start.2.
event = {
	id = asp_on_action.6
	hide_window = yes
	is_triggered_only = yes

	trigger = {
		OR = {
			has_global_flag = aspmod_observatory_repair_active
			has_global_flag = aspmod_ftl_switcher_repair_active
		}
	}

	immediate = {
		if = {
			limit = { has_global_flag = aspmod_observatory_repair_active }

			# Rebuild the unique country reward from the surviving source planet and current controller.
			every_country = {
				limit = { has_modifier = aspmod_infinite_observatory_country }
				remove_modifier = aspmod_infinite_observatory_country
			}
			if = {
				limit = {
					any_system = {
						any_system_planet = { has_modifier = aspmod_infinite_observatory_planet }
					}
				}
				every_system = {
					limit = {
						any_system_planet = { has_modifier = aspmod_infinite_observatory_planet }
						exists = controller
					}
					controller = { add_modifier = { modifier = aspmod_infinite_observatory_country days = -1 } }
				}
			}
			else = {
				remove_global_flag = aspmod_observatory_repair_active
			}
		}

		if = {
			limit = { has_global_flag = aspmod_ftl_switcher_repair_active }

			# A completed FTL-lane starbase building must not survive destruction/removal of its source planet.
			every_country = {
				every_owned_starbase = {
					limit = {
						has_starbase_building = sm_aspmod_axis_of_planet_building
						solar_system = {
							NOT = { any_system_planet = { has_modifier = aspmod_ftl_lane_switcher } }
						}
					}
					remove_starbase_building = { building = sm_aspmod_axis_of_planet_building }
				}
				every_owned_nonprimary_starbase = {
					limit = {
						has_starbase_building = sm_aspmod_axis_of_planet_building
						solar_system = {
							NOT = { any_system_planet = { has_modifier = aspmod_ftl_lane_switcher } }
						}
					}
					remove_starbase_building = { building = sm_aspmod_axis_of_planet_building }
				}
			}

			if = {
				limit = {
					NOT = {
						any_system = {
							any_system_planet = { has_modifier = aspmod_ftl_lane_switcher }
						}
					}
				}
				remove_global_flag = aspmod_ftl_switcher_repair_active
			}
		}
	}
}
