# =============================================================================
# Giga + Age of Wonder Compatibility Patch — scripted_triggers bridge
# Loads after both mods (see descriptor.mod dependencies), so these definitions
# win the merge for every shared vanilla-override trigger below.
#
# PROBLEM: Both mods overwrite the same vanilla scripted_triggers, but each only
# "knows" its own megastructures:
#   - Giga's versions are huge hard-coded enumerations of Giga megastructure IDs
#     (zzz_overwrites.txt) and never list AOW structures.
#   - AOW's versions (zz_AOW_trgr_Vanilla.txt) only account for AOW structures.
# Whichever loads last wins, so cross-mod placement checks misfire (double-builds,
# broken gating).
#
# FIX: Re-author each trigger using mod-AGNOSTIC generic iterators
# (has_any_megastructure / any_owned_megastructure / any_system_megastructure),
# enumerating only the small, stable set of "gate-type" megastructures as the
# exception. That way both mods' rosters are recognized automatically.
# =============================================================================

# Helper (new name, no collision): true when the scoped megastructure is a
# transit "gate" that should NOT block building other megastructures.
# Covers vanilla gateways/relays/L-gate plus the gateway variants both mods add.
compat_is_gate_megastructure = {
	OR = {
		is_megastructure_type = gateway_0
		is_megastructure_type = gateway_final
		is_megastructure_type = gateway_restored
		is_megastructure_type = gateway_ruined
		is_megastructure_type = gateway_derelict
		is_megastructure_type = gateway_heaven
		is_megastructure_type = gateway_titan
		is_megastructure_type = gateway_ness_restored
		is_megastructure_type = gateway_ness_ruined
		is_megastructure_type = hyper_relay
		is_megastructure_type = hyper_relay_restored
		is_megastructure_type = hyper_relay_ruined
		is_megastructure_type = lgate_base
	}
}

# "No non-gate megastructure in this system" — used to enforce one big
# megastructure per system. Preserves Giga's outer structure (incl. ring_world
# flag), but the inner test now means "a megastructure that is NOT a gate exists"
# for ANY mod's structure, instead of Giga's fixed blocking list.
# Conservative by design: enforces one-mega-per-system across BOTH mods, which is
# exactly what prevents the cross-mod double-build bug.
has_no_non_gate_megastructure = {
	OR = {
		has_any_megastructure = no
		NOT = {
			has_star_flag = ring_world_built # ringworlds count as a megastructure
			any_system_megastructure = {     # exists a non-gate megastructure?
				NOT = { compat_is_gate_megastructure = yes }
			}
		}
	}
}

# "Empire owns any (real) megastructure" — generic, catches both rosters.
# Gate-type structures are excluded to match Giga's original intent (a mere
# gateway/relay should not count as "owning a megastructure").
has_any_megastructure_in_empire = {
	OR = {
		any_owned_megastructure = {
			NOT = { compat_is_gate_megastructure = yes }
		}
		any_system_within_border = {
			any_system_megastructure = {
				NOT = { compat_is_gate_megastructure = yes }
			}
		}
	}
}

# Ringworld output boost — keep Giga's logic (incl. its exclusion list) and ALSO
# grant the boost to AOW's pc_starcage_world (AOW's ringworld-equivalent).
has_ringworld_output_boost = {
	OR = {
		AND = {
			is_ringworld = yes
			giga_has_ringworld_output_boost_exclusions = no
		}
		giga_has_ringworld_output_boost = yes
		is_planet_class = pc_starcage_world # AOW
	}
}

# Dismantle check (parameterized $TECH$, called by both mods). Keep Giga's
# custom_tooltips AND AOW's "dismantle at war" global-flag flexibility.
can_dismantle_megastructure = {
	custom_tooltip = {
		fail_text = "requires_not_upgrading"
		is_upgrading = no
	}
	custom_tooltip = {
		fail_text = "requires_owned_system"
		solar_system = {
			is_owned_by = from
		}
	}
	if = {
		limit = { has_global_flag = can_dismantle_megastructure_at_war } # AOW feature
		from = { has_technology = $TECH$ }
	}
	else = {
		from = {
			has_technology = $TECH$
			is_at_war = no
		}
	}
}

# =============================================================================
# AOW_trigger_mega_potential — patch override (megastructure build-menu tabs)
# Every AOW megastructure routes its `potential` through this one shared trigger,
# so adding a single condition here gates the ENTIRE AOW roster behind the
# Giga-only build-menu tab. Body is AOW's original verbatim; the only addition is
# the `NOT = { has_country_flag = compat_mega_hide_aow }` line in the player branch.
# AI evaluates the is_ai=yes branch (no tab check) and is never affected, since the
# hide flag only ever exists on a human player's country (set by the tab buttons).
# Scope here is the viewing country (megastructure potential scope = country).
# Keeps AOW's $KEY$ parameter intact.
# =============================================================================
AOW_trigger_mega_potential = {
	if = { #如果是玩家，则对应巨构没有被禁用
		limit = { is_ai = no }
		NOT = { has_country_flag = compat_mega_hide_aow } # compat: Giga-only tab hides AOW structures
		OR = {
			NOT = { has_global_flag = AOW_flag_GLBL_no_player_$KEY$ }
			event_target:global_event_country = {
				check_variable = { which = AOW_var_player_mega_cap_$KEY$ value != 0 }
			}
		}
	}
	else_if = { #如果是AI，则对应巨构没有被禁用
		limit = { is_ai = yes }
		OR = {
			NOT = { has_global_flag = AOW_flag_GLBL_no_ai_$KEY$ }
			event_target:global_event_country = {
				check_variable = { which = AOW_var_ai_mega_cap_$KEY$ value != 0 }
			}
		}
	}
}
