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
	show_in_outliner = no
	build_time = 360
	resources = {
		category = megastructures_hyper_relay
		cost = {
			trigger = {
				NOT = { has_country_flag = flash_forge_hyper_relay_activated }
			}
			rare_crystals = 100
			influence = 25
		}
		cost = {
			trigger = {
				country_uses_bio_ships = no
			}
			alloys = 500
		}
		cost = {
			trigger = {
				country_uses_bio_ships = yes
			}
			alloys = 500
			mult = 0.5
		}
		cost = {
			trigger = {
				country_uses_bio_ships = yes
			}
			food = 500
			mult = @halved_alloy_to_food_cost_ratio
		}

		upkeep = {
			energy = 2
		}
	}

	construction_blocks_and_blocked_by = self_type
	build_megastructure_no_cost_localization_key = "FLASH_FORGE_MEGASTRUCTURE"
	custom_tooltip_requirements = "MEGASTRUCTURE_TOOLTIP_REQUIREMENTS_OUTSIDE_GRAVITY_WELL"

	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
	}

	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 = {
		factor = 5

		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_planet = {
				count > 0
			}
		}

		# non-gestalt empires will prefer building along trade routes, since those connect colonies to the capital
		# this may occasionally catch other empire's trade routes that clip our territory, but pirates will eventually appear there so it's fine
		modifier = {
			factor = 0.2
			from = {
				is_gestalt = no
			}
			is_capital_system = no
			#has_trade_route = no
		}
		modifier = {
			factor = 0 
			NOT = { has_global_flag = f82_button_2_1_off }
			OR = { 
				not = { has_star_flag = f93_crossing_capital_route_flag }
				f93_system_has_ancient_relay = yes
			}
		}
		# modifier = {
		# 	factor = 50
		# 	has_star_flag = f93_crossing_capital_route_flag
		# }
	}

	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
hyper_relay_ruined = {
	entity = "hyper_relay_01_destroyed_entity"
	portrait = "GFX_megastructure_construction_background"
	place_entity_on_planet_plane = no
	build_type = outside_gravity_well
	# 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
hyper_relay_restored = {
	entity = "hyper_relay_entity"
	construction_entity = "hyper_relay_entity"
	portrait = "GFX_megastructure_construction_background"
	place_entity_on_planet_plane = no
	# build_type = outside_gravity_well
	show_galactic_map_icon = no
	show_in_outliner = no
	bypass_type = relay_bypass

	potential = {
		has_technology = tech_hyper_relays
	}

	possible = {
		custom_tooltip = {
			fail_text = "requires_tech_hyper_drive_2"
			from = { has_technology = tech_hyper_drive_2 }
		}
	}
	upgrade_from = {
		hyper_relay_ruined
	}
	build_time = 180
	resources = {
		category = megastructures
		cost = {
			rare_crystals = 100
			energy = 500
		}
		cost = {
			trigger = {
				country_uses_bio_ships = no
			}
			alloys = 500
		}
		cost = {
			trigger = {
				country_uses_bio_ships = yes
			}
			alloys = 500
			mult = 0.5
		}
		cost = {
			trigger = {
				country_uses_bio_ships = yes
			}
			food = 500
			mult = @halved_alloy_to_food_cost_ratio
		}

		upkeep = {
			energy = 2
		}
	}

	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
			}
		}
	}

	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 }
			}
		}
	}
}
