2024-08-21 20:46:29 -04:00
|
|
|
-- Spawn control tab in the Oarc GUI, for things like sharing your base with others.
|
|
|
|
|
|
|
|
---Check if shared spawn is active (enabled and open)
|
|
|
|
---@param player LuaPlayer
|
|
|
|
---@return boolean
|
|
|
|
local function IsSharedSpawnActive(player)
|
|
|
|
if ((global.ocore.sharedSpawns[player.name] == nil) or
|
|
|
|
(global.ocore.sharedSpawns[player.name].openAccess == false)) then
|
|
|
|
return false
|
|
|
|
else
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
---Provides the content of the spawn control tab in the Oarc GUI.
|
|
|
|
---@param tab_container LuaGuiElement
|
|
|
|
---@param player LuaPlayer
|
|
|
|
---@return nil
|
|
|
|
function CreateSpawnControlsTab(tab_container, player)
|
|
|
|
local spwnCtrls = tab_container.add {
|
|
|
|
type = "scroll-pane",
|
|
|
|
name = "spwn_ctrl_panel",
|
2024-09-05 21:15:56 -04:00
|
|
|
caption = ""
|
|
|
|
}
|
2024-08-21 20:46:29 -04:00
|
|
|
ApplyStyle(spwnCtrls, my_fixed_width_style)
|
|
|
|
spwnCtrls.style.maximal_height = 1000
|
|
|
|
spwnCtrls.horizontal_scroll_policy = "never"
|
|
|
|
|
2024-09-10 11:40:19 -04:00
|
|
|
CreateSetRespawnLocationButton(player, spwnCtrls)
|
|
|
|
CreateSharedSpawnControls(player, spwnCtrls)
|
|
|
|
CreateJoinQueueControls(player, spwnCtrls)
|
|
|
|
end
|
|
|
|
|
|
|
|
---Display the shared spawn controls
|
|
|
|
---@param player LuaPlayer
|
|
|
|
---@param container LuaGuiElement
|
|
|
|
---@return nil
|
|
|
|
function CreateSharedSpawnControls(player, container)
|
2024-08-21 20:46:29 -04:00
|
|
|
if global.ocfg.gameplay.enable_shared_spawns then
|
|
|
|
if (global.ocore.uniqueSpawns[player.name] ~= nil) then
|
2024-09-10 11:40:19 -04:00
|
|
|
|
|
|
|
AddLabel(container, nil, { "oarc-shared-spawn-controls" }, "caption_label")
|
|
|
|
|
2024-09-05 21:15:56 -04:00
|
|
|
-- This checkbox allows people to join your base when they first start the game.
|
2024-09-10 11:40:19 -04:00
|
|
|
local toggle = container.add {
|
2024-09-05 21:15:56 -04:00
|
|
|
type = "checkbox",
|
|
|
|
name = "accessToggle",
|
|
|
|
tags = { action = "oarc_spawn_ctrl_tab", setting = "shared_access_toggle" },
|
2024-09-10 11:40:19 -04:00
|
|
|
caption = { "oarc-shared-spawn-allow-joiners" },
|
2024-09-05 21:15:56 -04:00
|
|
|
state = IsSharedSpawnActive(player)
|
|
|
|
}
|
|
|
|
ApplyStyle(toggle, my_fixed_width_style)
|
2024-08-21 20:46:29 -04:00
|
|
|
end
|
|
|
|
end
|
2024-09-10 11:40:19 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
---Display the set respawn location button in the spawn control tab.
|
|
|
|
---@param player LuaPlayer
|
|
|
|
---@param container LuaGuiElement
|
|
|
|
---@return nil
|
|
|
|
function CreateSetRespawnLocationButton(player, container)
|
|
|
|
AddLabel(container, nil, { "oarc-set-respawn-loc-header" }, "caption_label")
|
2024-08-21 20:46:29 -04:00
|
|
|
|
|
|
|
-- Sets the player's custom spawn point to their current location
|
|
|
|
if ((game.tick - global.ocore.playerCooldowns[player.name].setRespawn) >
|
|
|
|
(global.ocfg.gameplay.respawn_cooldown_min * TICKS_PER_MINUTE)) then
|
2024-09-10 11:40:19 -04:00
|
|
|
local change_respawn_button = container.add {
|
2024-09-05 21:15:56 -04:00
|
|
|
type = "button",
|
|
|
|
tags = { action = "oarc_spawn_ctrl_tab", setting = "set_respawn_location" },
|
|
|
|
name = "setRespawnLocation",
|
2024-09-10 11:40:19 -04:00
|
|
|
caption = { "oarc-set-respawn-loc" },
|
|
|
|
tooltip = { "oarc-set-respawn-loc-tooltip" },
|
|
|
|
style = "red_button"
|
2024-09-05 21:15:56 -04:00
|
|
|
}
|
|
|
|
change_respawn_button.style.font = "default-small-semibold"
|
2024-08-21 20:46:29 -04:00
|
|
|
else
|
2024-09-10 11:40:19 -04:00
|
|
|
AddLabel(container, nil,
|
2024-08-21 20:46:29 -04:00
|
|
|
{ "oarc-set-respawn-loc-cooldown", FormatTime((global.ocfg.gameplay.respawn_cooldown_min * TICKS_PER_MINUTE) -
|
|
|
|
(game.tick - global.ocore.playerCooldowns[player.name].setRespawn)) }, my_note_style)
|
|
|
|
end
|
2024-09-10 11:40:19 -04:00
|
|
|
AddLabel(container, nil, { "oarc-set-respawn-note" }, my_note_style)
|
|
|
|
AddSpacerLine(container)
|
|
|
|
end
|
2024-08-21 20:46:29 -04:00
|
|
|
|
2024-09-10 11:40:19 -04:00
|
|
|
---Display a list of people in the join queue for a shared spawn.
|
|
|
|
---@param player LuaPlayer
|
|
|
|
---@param container LuaGuiElement
|
|
|
|
---@return nil
|
|
|
|
function CreateJoinQueueControls(player, container)
|
2024-08-21 20:46:29 -04:00
|
|
|
if (global.ocfg.gameplay.enable_shared_spawns and IsSharedSpawnActive(player)) then
|
|
|
|
if (TableLength(global.ocore.sharedSpawns[player.name].joinQueue) > 0) then
|
2024-09-10 11:40:19 -04:00
|
|
|
AddLabel(container, nil, { "oarc-join-queue-header" }, "caption_label")
|
|
|
|
AddLabel(container, "drop_down_msg_lbl1", { "oarc-select-player-join-queue" }, my_label_style)
|
2024-09-05 21:15:56 -04:00
|
|
|
|
2024-09-10 11:40:19 -04:00
|
|
|
local horizontal_flow = container.add { type = "flow", direction = "horizontal" }
|
2024-09-05 21:15:56 -04:00
|
|
|
horizontal_flow.style.horizontally_stretchable = true
|
|
|
|
|
|
|
|
horizontal_flow.add {
|
|
|
|
name = "join_queue_dropdown",
|
2024-08-21 20:46:29 -04:00
|
|
|
type = "drop-down",
|
2024-09-05 21:15:56 -04:00
|
|
|
items = global.ocore.sharedSpawns[player.name].joinQueue
|
|
|
|
}
|
|
|
|
|
|
|
|
local dragger = horizontal_flow.add {
|
|
|
|
type = "empty-widget",
|
|
|
|
style = "draggable_space_header"
|
|
|
|
}
|
|
|
|
dragger.style.horizontally_stretchable = true
|
|
|
|
|
|
|
|
horizontal_flow.add {
|
|
|
|
name = "accept_player_request",
|
|
|
|
tags = { action = "oarc_spawn_ctrl_tab", setting = "accept_player_request" },
|
2024-08-21 20:46:29 -04:00
|
|
|
type = "button",
|
2024-09-05 21:15:56 -04:00
|
|
|
style = "green_button",
|
|
|
|
caption = { "oarc-accept" }
|
|
|
|
}
|
|
|
|
horizontal_flow.add {
|
|
|
|
name = "reject_player_request",
|
|
|
|
tags = { action = "oarc_spawn_ctrl_tab", setting = "reject_player_request" },
|
2024-08-21 20:46:29 -04:00
|
|
|
type = "button",
|
2024-09-05 21:15:56 -04:00
|
|
|
style = "red_button",
|
|
|
|
caption = { "oarc-reject" }
|
|
|
|
}
|
2024-08-21 20:46:29 -04:00
|
|
|
else
|
2024-09-10 11:40:19 -04:00
|
|
|
AddLabel(container, "empty_join_queue_note1", { "oarc-no-player-join-reqs" }, my_note_style)
|
2024-08-21 20:46:29 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2024-09-10 11:40:19 -04:00
|
|
|
|
2024-08-21 20:46:29 -04:00
|
|
|
---Handle the gui checkboxes & radio buttons of the spawn control tab in the Oarc GUI.
|
|
|
|
---@param event EventData.on_gui_checked_state_changed
|
|
|
|
---@return nil
|
|
|
|
function SpawnCtrlGuiOptionsSelect(event)
|
2024-09-05 21:15:56 -04:00
|
|
|
if not event.element.valid then return end
|
2024-08-21 20:46:29 -04:00
|
|
|
local player = game.players[event.player_index]
|
2024-09-05 21:15:56 -04:00
|
|
|
local tags = event.element.tags
|
2024-08-21 20:46:29 -04:00
|
|
|
|
2024-09-05 21:15:56 -04:00
|
|
|
if (tags.action ~= "oarc_spawn_ctrl_tab") then
|
2024-08-21 20:46:29 -04:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
---@type OarcSharedSpawn
|
|
|
|
local sharedSpawn = global.ocore.sharedSpawns[player.name]
|
|
|
|
|
|
|
|
-- Handle changes to spawn sharing.
|
2024-09-05 21:15:56 -04:00
|
|
|
if (tags.setting == "shared_access_toggle") then
|
2024-08-21 20:46:29 -04:00
|
|
|
if event.element.state then
|
|
|
|
if DoesPlayerHaveCustomSpawn(player) then
|
|
|
|
if (sharedSpawn == nil) then
|
|
|
|
CreateNewSharedSpawn(player)
|
|
|
|
else
|
2024-09-05 21:15:56 -04:00
|
|
|
global.ocore.sharedSpawns[player.name].openAccess = true
|
2024-08-21 20:46:29 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
SendBroadcastMsg({ "oarc-start-shared-base", player.name })
|
|
|
|
end
|
|
|
|
else
|
|
|
|
if (sharedSpawn ~= nil) then
|
2024-09-05 21:15:56 -04:00
|
|
|
global.ocore.sharedSpawns[player.name].openAccess = false
|
2024-08-21 20:46:29 -04:00
|
|
|
SendBroadcastMsg({ "oarc-stop-shared-base", player.name })
|
|
|
|
end
|
|
|
|
end
|
2024-09-04 22:21:43 -04:00
|
|
|
OarcGuiRefreshContent(player)
|
2024-08-28 01:36:02 -04:00
|
|
|
|
|
|
|
-- Refresh the shared spawn spawn gui for all players
|
|
|
|
for _,p in pairs(game.connected_players) do
|
|
|
|
RefreshSharedSpawnFrameIfExist(p)
|
|
|
|
end
|
2024-08-21 20:46:29 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
---Handle the gui click of the spawn control tab in the Oarc GUI.
|
|
|
|
---@param event EventData.on_gui_click
|
|
|
|
---@return nil
|
|
|
|
function SpawnCtrlGuiClick(event)
|
2024-09-05 21:15:56 -04:00
|
|
|
if not event.element.valid then return end
|
2024-08-21 20:46:29 -04:00
|
|
|
local player = game.players[event.player_index]
|
2024-09-05 21:15:56 -04:00
|
|
|
local tags = event.element.tags
|
2024-08-21 20:46:29 -04:00
|
|
|
|
2024-09-05 21:15:56 -04:00
|
|
|
if (tags.action ~= "oarc_spawn_ctrl_tab") then
|
2024-08-21 20:46:29 -04:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
-- Sets a new respawn point and resets the cooldown.
|
2024-09-05 21:15:56 -04:00
|
|
|
if (tags.setting == "set_respawn_location") then
|
2024-08-21 20:46:29 -04:00
|
|
|
if DoesPlayerHaveCustomSpawn(player) then
|
|
|
|
ChangePlayerSpawn(player, player.surface.name, player.position)
|
2024-09-04 22:21:43 -04:00
|
|
|
OarcGuiRefreshContent(player)
|
2024-08-21 20:46:29 -04:00
|
|
|
player.print({ "oarc-spawn-point-updated" })
|
|
|
|
end
|
2024-09-05 21:15:56 -04:00
|
|
|
|
2024-08-21 20:46:29 -04:00
|
|
|
|
|
|
|
-- Accept or reject pending player join requests to a shared base
|
2024-09-05 21:15:56 -04:00
|
|
|
elseif ((tags.setting == "accept_player_request") or (tags.setting == "reject_player_request")) then
|
|
|
|
|
2024-08-21 20:46:29 -04:00
|
|
|
if ((event.element.parent.join_queue_dropdown == nil) or
|
|
|
|
(event.element.parent.join_queue_dropdown.selected_index == 0)) then
|
2024-09-05 21:15:56 -04:00
|
|
|
player.print({ "oarc-selected-player-not-valid" })
|
2024-09-04 22:21:43 -04:00
|
|
|
OarcGuiRefreshContent(player)
|
2024-08-21 20:46:29 -04:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
local joinQueueIndex = event.element.parent.join_queue_dropdown.selected_index
|
|
|
|
local joinQueuePlayerChoice = event.element.parent.join_queue_dropdown.get_item(joinQueueIndex) --[[@as string]]
|
|
|
|
|
2024-09-05 21:15:56 -04:00
|
|
|
-- Shouldn't be able to hit this since we force a GUI refresh when they leave?
|
2024-08-21 20:46:29 -04:00
|
|
|
if ((game.players[joinQueuePlayerChoice] == nil) or
|
|
|
|
(not game.players[joinQueuePlayerChoice].connected)) then
|
|
|
|
player.print({ "oarc-selected-player-not-wait" })
|
2024-09-04 22:21:43 -04:00
|
|
|
OarcGuiRefreshContent(player)
|
2024-08-21 20:46:29 -04:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
|
|
|
---@type OarcSharedSpawn
|
|
|
|
local sharedSpawn = global.ocore.sharedSpawns[player.name]
|
|
|
|
|
2024-09-05 21:15:56 -04:00
|
|
|
if (tags.setting == "reject_player_request") then
|
|
|
|
|
|
|
|
RemovePlayerFromJoinQueue(joinQueuePlayerChoice) -- This also refreshes the host gui
|
2024-09-04 22:21:43 -04:00
|
|
|
|
|
|
|
-- Inform the host that the player was rejected
|
2024-08-21 20:46:29 -04:00
|
|
|
player.print({ "oarc-reject-joiner", joinQueuePlayerChoice })
|
2024-09-04 22:21:43 -04:00
|
|
|
-- Inform the player that their request was rejected
|
2024-08-21 20:46:29 -04:00
|
|
|
SendMsg(joinQueuePlayerChoice, { "oarc-your-request-rejected" })
|
|
|
|
|
|
|
|
-- Close the waiting players menu
|
|
|
|
if (game.players[joinQueuePlayerChoice].gui.screen.join_shared_spawn_wait_menu) then
|
|
|
|
game.players[joinQueuePlayerChoice].gui.screen.join_shared_spawn_wait_menu.destroy()
|
|
|
|
DisplaySpawnOptions(game.players[joinQueuePlayerChoice])
|
|
|
|
end
|
|
|
|
|
2024-09-05 21:15:56 -04:00
|
|
|
elseif (tags.setting == "accept_player_request") then
|
|
|
|
|
2024-09-11 21:20:00 -04:00
|
|
|
-- Check if there is space first
|
2024-09-11 12:49:23 -04:00
|
|
|
if (TableLength(sharedSpawn.players) >= global.ocfg.gameplay.number_of_players_per_shared_spawn - 1) then
|
|
|
|
player.print({ "oarc-shared-spawn-full" })
|
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2024-09-05 21:15:56 -04:00
|
|
|
RemovePlayerFromJoinQueue(joinQueuePlayerChoice) -- This also refreshes the host gui
|
2024-09-04 22:21:43 -04:00
|
|
|
|
2024-09-05 21:15:56 -04:00
|
|
|
-- Send an announcement
|
|
|
|
SendBroadcastMsg({ "oarc-player-joining-base", joinQueuePlayerChoice, player.name })
|
2024-09-04 22:21:43 -04:00
|
|
|
|
2024-09-05 21:15:56 -04:00
|
|
|
-- Close the waiting players menu
|
|
|
|
if (game.players[joinQueuePlayerChoice].gui.screen.join_shared_spawn_wait_menu) then
|
|
|
|
game.players[joinQueuePlayerChoice].gui.screen.join_shared_spawn_wait_menu.destroy()
|
2024-08-21 20:46:29 -04:00
|
|
|
end
|
|
|
|
|
2024-09-05 21:15:56 -04:00
|
|
|
-- Spawn the player
|
|
|
|
local joiningPlayer = game.players[joinQueuePlayerChoice]
|
|
|
|
ChangePlayerSpawn(joiningPlayer, sharedSpawn.surface, sharedSpawn.position)
|
|
|
|
SendPlayerToSpawn(joiningPlayer)
|
|
|
|
GivePlayerStarterItems(joiningPlayer)
|
|
|
|
table.insert(sharedSpawn.players, joiningPlayer.name)
|
|
|
|
joiningPlayer.force = game.players[player.name].force
|
2024-08-21 20:46:29 -04:00
|
|
|
|
2024-09-05 21:15:56 -04:00
|
|
|
-- Render some welcoming text...
|
|
|
|
DisplayWelcomeGroundTextAtSpawn(joiningPlayer, sharedSpawn.surface, sharedSpawn.position)
|
2024-09-04 22:21:43 -04:00
|
|
|
|
2024-09-05 21:15:56 -04:00
|
|
|
-- Unlock spawn control gui tab
|
|
|
|
SetOarcGuiTabEnabled(joiningPlayer, OARC_SPAWN_CTRL_TAB_NAME, true)
|
2024-08-21 20:46:29 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|