# =============================================================================
# Giga + Age of Wonder Compatibility Patch — hyper_relay family  [TIER 1]
# Loads after both mods, so these definitions win the merge.
#
# MERGE METHOD (Tier 1 template):
#   * Base = Giga's definition — it uses the modern 4.x dismantle API
#     (dismantle_cost/dismantle_possible/...), the correct hyper-relay portrait,
#     the richer `possible` block, Giga's AI weighting, and already lists
#     `giga_hyper_relay_ai_site` in upgrade_from (so Giga's AI relay placement
#     keeps working).
#   * Graft from AOW = its per-structure enable/disable toggle
#     `AOW_trigger_mega_potential = { KEY = hyper_relay }`, added to `potential`
#     so AOW's settings menu can still disable the relay. (Defaults to enabled.)
#   * upgrade_from = union of both — Giga's is already the superset here.
# Net: nothing from either mod is lost regardless of load order.
# =============================================================================

hyper_relay = {
	entity = "hyper_relay_entity"
	construction_entity = "hyper_relay_entity"
	portrait = "GFX_megastructure_hyper_relay_background"
	place_entity_on_planet_plane = no
	build_outside_gravity_well = yes
	show_galactic_map_icon = no
	build_time = 360
	show_in_outliner = no

	resources = {
		category = megastructures_hyper_relay
		cost = {
			trigger = {
				NOT = { has_country_flag = flash_forge_hyper_relay_activated }
			}
			rare_crystals = 100
			influence = 25
		}
		inline_script = {
			script = megastructures/generic_parts/giga_mega_alloy_cost
			condition = "NOT = { has_country_flag = flash_forge_hyper_relay_activated }"
			alloys = 500
		}

		upkeep = {
			energy = 2
		}
	}
	construction_blocks_and_blocked_by = self_type
	build_megastructure_no_cost_localization_key = "FLASH_FORGE_MEGASTRUCTURE"

	dismantle_cost = {
		category = megastructures_hyper_relay
		cost = {
			energy = 500
		}
	}

	dismantle_time = 360

	dismantle_potential = {
		always = yes
	}

	dismantle_possible = {
		can_dismantle_megastructure = {
			TECH = tech_hyper_relays
		}
	}

	on_dismantle_complete = {
		from = {
			add_resource = {
				alloys = 500
				rare_crystals = 100
				mult = modifier:megastructure_dismantle_refund_mult
			}
		}
	}

	potential = {
		has_technology = tech_hyper_relays
		AOW_trigger_mega_potential = { KEY = hyper_relay } # AOW toggle (merged in)
	}

	possible = {
		hidden_trigger = {
			exists = starbase
		}
		custom_tooltip = {
			fail_text = "requires_inside_border_or_subject"
			OR = {
				is_inside_border = from
				AND = {
					exists = owner
					owner = {
						is_subject = yes
						overlord = { is_same_value = from }
					}
				}
			}
		}
		custom_tooltip = {
			fail_text = "requires_surveyed_system" # or is in my subject's territory
			OR = {
				NOT = {
					any_system_planet = {
						is_surveyed = {
							who = prev.from
							status = no
						}
					}
				}
				AND = {
					exists = owner
					owner = {
						is_subject = yes
						overlord = { is_same_value = from }
					}
				}
			}
		}
		custom_tooltip = {
			fail_text = "requires_no_existing_hyper_relay"
			NOR = {
				has_megastructure = hyper_relay
				has_megastructure = hyper_relay_ruined
				has_megastructure = hyper_relay_restored
			}
		}
		custom_tooltip = {
			fail_text = "requires_technology_hyper_relays"
			from = { has_technology = tech_hyper_relays }
		}
		custom_tooltip = {
			fail_text = "requires_hyperlane"
			count_neighbor_system = {
				count > 0
			}
		}
	}

	placement_rules = {			# those would be ignored since the hyper relay is being "free-placed" between the inner and outer radius of the system
	}

	# root = system
	# from = country
	ai_weight = {
		weight = value:giga_ai_base_kilo

		modifier = {
			factor = 3
			is_capital_system = yes
		}

		modifier = {
			factor = 1.5
			any_neighbor_system = {
				OR = {
					has_megastructure = hyper_relay
					has_megastructure = hyper_relay_restored
				}
			}
		}

		modifier = {
			factor = 1.5
			count_system_colony = {
				count > 0
			}
		}

		modifier = {
			factor = 0.2
			from = {
				is_gestalt = no
			}
			is_capital_system = no
		}
	}

	bypass_type = relay_bypass
	on_build_queued = {
		fromfrom = {
			fleet_event = { id = astral_planes.1010 }
		}
	}
	on_build_unqueued = {
		fromfrom = {
			fleet_event = { id = astral_planes.1015 }
		}
	}
	on_build_complete = {
		fromfromfrom = {
			fleet_event = { id = astral_planes.1020 }
		}
		from = {
			country_event = { id = tutorial.2131 }
		}
	}
}

# Ruined Hyper Relay
## Tags: @ruined
hyper_relay_ruined = {
	entity = "hyper_relay_01_destroyed_entity"
	portrait = "GFX_megastructure_construction_background"
	place_entity_on_planet_plane = no
	build_outside_gravity_well = yes
	# not a valid bypass. bypass_type = relay_bypass
	show_galactic_map_icon = no
	show_in_outliner = no
	construction_blocks_and_blocked_by = self_type
	potential = {
		always = no
	}
}

# Restored Hyper Relay
## Tags: @restored
hyper_relay_restored = {
	entity = "hyper_relay_entity"
	construction_entity = "hyper_relay_entity"
	portrait = "GFX_megastructure_construction_background"
	place_entity_on_planet_plane = no
	# build_outside_gravity_well = yes
	show_galactic_map_icon = no
	bypass_type = relay_bypass
	show_in_outliner = no

	potential = {
		has_technology = tech_hyper_relays
	}

	dismantle_cost = {
		category = megastructures_hyper_relay
		cost = {
			energy = 500
		}
	}

	dismantle_time = 360

	dismantle_potential = {
		always = yes
	}

	dismantle_possible = {
		can_dismantle_megastructure = {
			TECH = tech_hyper_relays
		}
	}

	on_dismantle_complete = {
		from = {
			add_resource = {
				alloys = 500
				rare_crystals = 100
				mult = modifier:megastructure_dismantle_refund_mult
			}
		}
	}

	possible = {
		custom_tooltip = {
			fail_text = "requires_tech_hyper_drive_2"
			from = { has_technology = tech_hyper_drive_2 }
		}
	}
	upgrade_from = {
		hyper_relay_ruined
		giga_hyper_relay_ai_site
	}
	build_time = 180
	resources = {
		category = megastructures_hyper_relay
		cost = {
			alloys = 500
			rare_crystals = 100
			energy = 500
		}

		upkeep = {
			energy = 2
		}
	}
	construction_blocks_and_blocked_by = self_type
	on_build_start = {
		fromfrom = {
			set_graphical_culture = root.from
		}
	}
	on_build_complete = {
		if = {
			limit = { exists = from } #does not always exist when upgraded from script via "finish_upgrade = yes"
			from = {
				country_event = { id = tutorial.2131 }
			}
		}
	}
}
