-- Helper: Get a nice display name for a surface (uses Space Platform name if available)
local function get_surface_display_name(surface)
if surface.platform and surface.platform.valid then
return surface.platform.name
end
-- Planets keep their normal name (nauvis, vulcanus, ...)
return surface.name
end
-- Open menu when left-clicking the teleporter
script.on_event(defines.events.on_gui_opened, function(event)
local entity = event.entity
if not (entity and entity.valid and entity.name == "interstellar-teleporter") then
return
end
local player = game.players[event.player_index]
player.opened = nil -- Close the system container window
open_teleport_gui(player)
end)
-- Create UI with buttons for available planets / platforms
function open_teleport_gui(player)
if player.gui.screen["teleport_gui"] then
player.gui.screen["teleport_gui"].destroy()
end
local current_surface = player.surface.name
local target_surfaces = {}
-- Find surfaces that have AT LEAST one teleporter
for _, surface in pairs(game.surfaces) do
if surface.name ~= current_surface then
local teleporters = surface.find_entities_filtered{name = "interstellar-teleporter"}
if #teleporters > 0 then
table.insert(target_surfaces, surface)
end
end
end
if #target_surfaces == 0 then
player.create_local_flying_text{
text = {"interstellar-teleport.no-receivers"},
create_at = "cursor",
color = {r = 1, g = 0.3, b = 0.3}
}
return
end
local frame = player.gui.screen.add{
type = "frame",
name = "teleport_gui",
caption = {"interstellar-teleport.gui-title"},
direction = "vertical"
}
frame.auto_center = true
local scroll = frame.add{
type = "scroll-pane",
name = "scroll_pane",
vertical_scroll_policy = "always"
}
scroll.style.maximal_height = 300
for _, surface in ipairs(target_surfaces) do
local display_name = get_surface_display_name(surface)
scroll.add{
type = "button",
name = "teleport_to_" .. surface.name, -- still use internal name for the button ID
caption = {"interstellar-teleport.go-to", display_name},
style = "confirm_button"
}
end
frame.add{
type = "button",
name = "teleport_close",
caption = {"interstellar-teleport.cancel"},
style = "back_button"
}
player.opened = frame
end
-- Handle clicks in the GUI
script.on_event(defines.events.on_gui_click, function(event)
local element = event.element
if not (element and element.valid) then return end
local player = game.players[event.player_index]
if element.name == "teleport_close" then
if player.gui.screen["teleport_gui"] then
player.gui.screen["teleport_gui"].destroy()
end
return
end
if element.name:sub(1, 12) == "teleport_to_" then
local target_surface_name = element.name:sub(13)
local target_surface = game.surfaces[target_surface_name]
if target_surface and target_surface.valid then
local target_teleporters = target_surface.find_entities_filtered{name = "interstellar-teleporter"}
if #target_teleporters > 0 then
local dest_teleporter = target_teleporters[1]
local dest_pos = dest_teleporter.position
local safe_pos = target_surface.find_non_colliding_position("character", dest_pos, 5, 0.5) or dest_pos
player.teleport(safe_pos, target_surface)
local display_name = get_surface_display_name(target_surface)
player.print({"interstellar-teleport.success", display_name})
else
player.print({"interstellar-teleport.error-destroyed"})
end
end
if player.gui.screen["teleport_gui"] then
player.gui.screen["teleport_gui"].destroy()
end
end
end)
-- Close GUI with Esc / E
script.on_event(defines.events.on_gui_closed, function(event)
local player = game.players[event.player_index]
if player.gui.screen["teleport_gui"] and event.element == player.gui.screen["teleport_gui"] then
player.gui.screen["teleport_gui"].destroy()
end
end)
-- Restriction: 1 teleporter per planet / platform (Singleton)
local function check_teleporter_limit(event)
local entity = event.entity or event.created_entity
if not (entity and entity.valid and entity.name == "interstellar-teleporter") then
return
end
local surface = entity.surface
local teleporters = surface.find_entities_filtered{name = "interstellar-teleporter"}
if #teleporters > 1 then
local player = event.player_index and game.players[event.player_index]
local pos = entity.position
entity.destroy()
if player then
player.insert{name = "interstellar-teleporter", count = 1}
player.create_local_flying_text{
text = {"interstellar-teleport.limit-exceeded"},
position = pos,
color = {r = 1, g = 0.2, b = 0.2}
}
end
end
end
script.on_event(defines.events.on_built_entity, check_teleporter_limit)
script.on_event(defines.events.on_robot_built_entity, check_teleporter_limit)
script.on_event(defines.events.on_space_platform_built_entity, check_teleporter_limit)
script.on_event(defines.events.script_raised_built, check_teleporter_limit)