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 = {
				NOT = {
					has_country_flag = flash_forge_hyper_relay_activated
				}
				country_uses_bio_ships = no
			}
			alloys = 500
		}
		cost = {
			trigger = {
				NOT = {
					has_country_flag = flash_forge_hyper_relay_activated
				}
				country_uses_bio_ships = yes
			}
			alloys = 500
			mult = 0.5
		}
		cost = {
			trigger = {
				NOT = {
					has_country_flag = flash_forge_hyper_relay_activated
				}
				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 = {
		if = {
			limit = {
				from = {
					is_nomadic = no
				}
			}
			hidden_trigger = {
				exists = starbase
			}
			custom_tooltip = {
				fail_text = "requires_inside_border_or_subject"
				OR = {
					is_inside_border = from
					AND = {
						owner? = {
							is_subject = yes
							overlord = {
								is_same_value = from
							}
						}
					}
					AND = {
						from = {
							has_menace_perk = menp_defender_galcom_2
						}
						solar_system.owner? = {
							OR = {
								has_defensive_pact = from
								is_in_federation_with = from
								from = {
									has_subject = prev
								}
							}
						}
					}
				}
			}
		}
		else = {
			custom_tooltip = {
				fail_text = requires_tier_2_waystation_on_wayline_or_subject
				OR = {
					any_starbase_in_system = {
						is_waystation_starbase = yes
						owner? = {
							is_same_value = from
						}
						any_starbase_in_network = {
							has_starbase_size >= starbase_waystation_2
							owner? = {
								is_same_value = from
							}
						}
					}
					AND = {
						exists = starbase
						owner? = {
							is_subject = yes
							overlord = {
								is_same_value = from
							}
						}
					}
					AND = {
						from = {
							has_menace_perk = menp_defender_galcom_2
						}
						solar_system.owner? = {
							OR = {
								has_defensive_pact = from
								is_in_federation_with = from
								from = {
									has_subject = prev
								}
							}
						}
					}
				}
			}
		}
		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
						}
					}
				}
				any_starbase_in_system = {
					is_waystation_starbase = yes
					owner? = {
						is_same_value = from
					}
				}
				AND = {
					exists = starbase
					owner? = {
						is_subject = yes
						overlord = {
							is_same_value = from
						}
					}
				}
				AND = {
					exists = starbase
					from = {
						has_menace_perk = menp_defender_galcom_2
					}
					solar_system.owner? = {
						OR = {
							has_defensive_pact = from
							is_in_federation_with = from
							from = {
								has_subject = prev
							}
						}
					}
				}
			}
		}
		custom_tooltip = {
			fail_text = "requires_no_existing_hyper_relay"
			has_any_hyper_relay = no
		}
		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
			}
		}
	}

	context_menu_potential = {
		has_any_hyper_relay = no
	}

	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 = 0
			from = {
				is_nomadic = yes
			}
			NOT = {
				any_fleet_in_system = {
					OR = {
						is_ship_size = starbase_waystation_2
						is_ship_size = starbase_waystation_3
					}
					owner? = {
						is_same_value = from
					}
				}
			}
		}

		modifier = {
			factor = 3
			is_capital_system = yes
		}

		modifier = {
			factor = 1.5
			any_neighbor_system = {
				has_hyper_relay = yes
			}
		}

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

	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
	nomad_reactivatable = yes
	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
				}
			}
		}
	}
}
