# =============================================================================
# Giga + Age of Wonder Compatibility Patch — scripted_effects bridge
# Loads after both mods, so this wins the merge for spawn_habitat_cracker_effect.
#
# Both mods overwrite this vanilla effect (fired when a habitat planet is
# cracked): each cleans up only ITS OWN habitat-station fleets —
#   AOW: limit = { is_ship_class = shipclass_habitat_station }
#   Giga: limit = { giga_is_habitat_orbital = yes }
# so under the other mod the orphaned station fleet / has_megastructure flag is
# left behind. Merged version below uses Giga's (safer) spawn ordering and sets
# the build-site flag, but cleans up BOTH mods' habitat fleets.
# =============================================================================

spawn_habitat_cracker_effect = {
	# this: the pc_habitat being cracked
	create_ambient_object = {
		location = this
		type = habitat_cracker_object
		play_animation_once = yes
		duration = 5

		use_3d_location = yes
		base_angle_towards = star
		entity_face_object = star

		entity_offset = { min = 0 max = 0 }

		entity_scale_to_size = yes
		scale = 0.10
	}
	save_event_target_as = target_habitat
	solar_system = {
		spawn_megastructure = {
			type = habitat_central_complex_ruined
			owner = this.owner
			coords_from = PREV
		}
		random_system_planet = {
			limit = {
				OR = {
					has_planet_flag = habitat@event_target:target_habitat
					has_planet_flag = habitat_build_site
				}
			}
			remove_planet_flag = habitat@event_target:target_habitat
			set_planet_flag = habitat_build_site
		}
		every_fleet_in_system = {
			limit = {
				OR = {
					is_ship_class = shipclass_habitat_station # AOW habitats
					giga_is_habitat_orbital = yes             # Giga habitats
				}
			}
			orbit = {
				if = {
					limit = { NOT = { has_planet_flag = habitat_build_site } }
					remove_planet_flag = has_megastructure
				}
			}
			delete_fleet = this
		}
	}
	remove_planet = yes
}

# =============================================================================
# Giga + Age of Wonder Compatibility Patch — Dyson Sphere gigastructure flag
#
# The AOW Dyson Sphere replaces the Giga Dyson Sphere, but the AOW sphere_5
# completion only sets has_built_or_repaired_megastructure — it does NOT set
# has_built_or_repaired_gigastructure. That flag is required by the Giga
# "Gigastructural Constructs" ascension perk to unlock the rest of the Giga
# tech tree. The Giga Dyson Sphere stage 5 normally sets both flags.
#
# AOW_eft_SSTM_dyson_sphere_change_planets is called from every AOW sphere_5
# on_build_complete (all 10 star-class variants + the vanilla override), so
# injecting the flag here fixes all paths with a single override.
#
# Scope: megastructure (system). from = the country that built the sphere.
# =============================================================================

AOW_eft_SSTM_dyson_sphere_change_planets = {
	from = {
		set_country_flag = has_built_or_repaired_gigastructure
	}

	every_system_planet = {
		limit = {
			OR = {
				is_planet_class = pc_molten
				is_planet_class = pc_toxic
				habitable_planet = yes
			}
		}
		if = {
			limit = {
				OR = {
					has_colony_progress > 0
					is_colony = yes
				}
			}
			destroy_colony = yes
		}
		change_pc = pc_frozen
	}
	every_system_planet = {
		limit = {
			is_planet_class = pc_barren
		}
		change_pc = pc_barren_cold
	}
	remove_system_terraforming_candidates = yes
}
