From e80ceaa528665cc6ab846c768717767af0d5a97f Mon Sep 17 00:00:00 2001 From: Oarcinae Date: Tue, 26 Nov 2024 22:04:08 -0500 Subject: [PATCH] Modding interface updates. --- changelog.txt | 4 ++ control.lua | 25 +++++++++++- data.lua | 12 +++--- lib/gui_tabs/spawn_controls.lua | 2 +- lib/separate_spawns.lua | 67 ++++++--------------------------- lib/separate_spawns_guis.lua | 11 +----- 6 files changed, 50 insertions(+), 71 deletions(-) diff --git a/changelog.txt b/changelog.txt index ed7390b..a2bc286 100644 --- a/changelog.txt +++ b/changelog.txt @@ -5,6 +5,10 @@ Date: ???? - Fix crash when player spawns due to missing surface configuration due to a missing migration. Changes: - Images losslessly compressed to reduce mod size. (Thanks plexpt!) + Modding: + - Adding first_spawn and is_host flags to oarc-mod-on-player-spawned event. + - Add additional remote interfaces: get_unique_spawns, get_player_home_spawn, get_player_primary_spawn. + - Adding new custom event: oarc-mod-on-spawn-choices-gui-displayed. Intended to let other modders customize the spawn choices GUI. --------------------------------------------------------------------------------------------------- Version: 2.1.14 Date: 2024-11-21 diff --git a/control.lua b/control.lua index b9d91a1..f375052 100644 --- a/control.lua +++ b/control.lua @@ -162,6 +162,15 @@ end) ---@field name integer --For custom events, this is the event ID ---@field tick integer +---@class OarcModOnSpawnChoicesGuiDisplayedEvent: OarcCustomEventBase +---@field player_index integer +---@field gui_element LuaGuiElement +script.on_event("oarc-mod-on-spawn-choices-gui-displayed", function(event) + log("EVENT - oarc-mod-on-spawn-choices-gui-displayed:" .. serpent.block(event --[[@as OarcModOnSpawnChoicesGuiDisplayedEvent]])) + -- The 4 main sub sections are called: spawn_settings_frame, solo_spawn_frame, shared_spawn_frame, and buddy_spawn_frame +end) + + ---@class OarcModOnSpawnCreatedEvent: OarcCustomEventBase ---@field spawn_data OarcUniqueSpawn script.on_event("oarc-mod-on-spawn-created", function(event) @@ -182,6 +191,8 @@ end) ---@class OarcModOnPlayerSpawnedEvent: OarcCustomEventBase ---@field player_index integer +---@field first_spawn boolean +---@field is_host boolean script.on_event("oarc-mod-on-player-spawned", function(event) log("EVENT - oarc-mod-on-player-spawned:" .. serpent.block(event --[[@as OarcModOnPlayerSpawnedEvent]])) end) @@ -452,7 +463,19 @@ local oarc_mod_interface = { get_mod_settings = function() return storage.ocfg - end + end, + + get_unique_spawns = function() + return storage.unique_spawns + end, + + get_player_home_spawn = function(player_name) + return FindPlayerHomeSpawn(player_name) + end, + + get_player_primary_spawn = function(player_name) + return FindPrimaryUniqueSpawn(player_name) + end, } remote.add_interface("oarc_mod", oarc_mod_interface) diff --git a/data.lua b/data.lua index aab1cd0..3606eb6 100644 --- a/data.lua +++ b/data.lua @@ -24,19 +24,24 @@ data:extend({ oarc_linked_chest, oarc_linked_power, }) +-- See control.lua for the event type defs to see what each event provides you. data:extend({ + -- A player was presented with the spawn options + { + type = "custom-event", + name = "oarc-mod-on-spawn-choices-gui-displayed", + }, + -- A spawn area was created (and is finished generating) { type = "custom-event", name = "oarc-mod-on-spawn-created", - -- Provides data table called spawn_data }, -- A spawn area was REQUESTED to be removed. (Not that it has been removed already.) { type = "custom-event", name = "oarc-mod-on-spawn-remove-request", - -- Provides data table called spawn_data }, -- A player was reset (also called when a player is removed) @@ -44,21 +49,18 @@ data:extend({ { type = "custom-event", name = "oarc-mod-on-player-reset", - -- Provides player_index }, -- A player was spawned (sent to a new spawn OR joined a shared spawn) { type = "custom-event", name = "oarc-mod-on-player-spawned", - -- Provides player_index }, -- A player moved from surface to space platform { type = "custom-event", name = "oarc-mod-character-surface-changed", - -- Provides player_index, old_surface_name, new_surface_name }, }) diff --git a/lib/gui_tabs/spawn_controls.lua b/lib/gui_tabs/spawn_controls.lua index 7732d1b..751178c 100644 --- a/lib/gui_tabs/spawn_controls.lua +++ b/lib/gui_tabs/spawn_controls.lua @@ -543,7 +543,7 @@ function SpawnCtrlTabGuiClick(event) end -- Send player to the host's primary spawn. - SendPlayerToNewSpawn(joining_player_name, primary_spawn.surface_name, true) + SendPlayerToNewSpawn(joining_player_name, primary_spawn.surface_name, true, false) -- Render some welcoming text... DisplayWelcomeGroundTextAtSpawn(primary_spawn.surface_name, primary_spawn.position) diff --git a/lib/separate_spawns.lua b/lib/separate_spawns.lua index e18509b..71655ac 100644 --- a/lib/separate_spawns.lua +++ b/lib/separate_spawns.lua @@ -671,15 +671,16 @@ end ---@param player_name string ---@param surface_name string ---@param first_spawn boolean +---@param is_host boolean ---@return nil -function SendPlayerToNewSpawn(player_name, surface_name, first_spawn) +function SendPlayerToNewSpawn(player_name, surface_name, first_spawn, is_host) local player = game.players[player_name] -- Check if player character is nil if (player.character == nil) then log("Player character is nil, can't send to spawn point just yet: " .. player_name) - QueueNilCharacterForNewSpawnTeleport(player_name, surface_name, first_spawn) + QueueNilCharacterForNewSpawnTeleport(player_name, surface_name, first_spawn, is_host) return end @@ -694,10 +695,10 @@ function SendPlayerToNewSpawn(player_name, surface_name, first_spawn) -- Only first time spawns get starter items. if first_spawn then GivePlayerStarterItems(player) - - -- Trigger the event that player was spawned too. - script.raise_event("oarc-mod-on-player-spawned", {player_index = player.index}) end + + -- Trigger the event that player was spawned too. + script.raise_event("oarc-mod-on-player-spawned", {player_index = player.index, first_spawn = first_spawn, is_host = is_host}) end ---Displays some welcoming text at the spawn point on the ground. Fades out over time. @@ -865,51 +866,6 @@ end --]] --- ---Resets the player and destroys their force if they are not on the main one. --- ---@param player LuaPlayer --- ---@return nil --- function ResetPlayerAndDestroyForce(player) --- local player_old_force = player.force - --- player.force = storage.ocfg.gameplay.main_force_name - --- if ((#player_old_force.players == 0) and (player_old_force.name ~= storage.ocfg.gameplay.main_force_name)) then --- SendBroadcastMsg("Team " .. --- player_old_force.name .. " has been destroyed! All buildings will slowly be destroyed now.") --: localize --- log("DestroyForce - FORCE DESTROYED: " .. player_old_force.name) --- game.merge_forces(player_old_force, DESTROYED_FORCE_NAME) --- end - --- RemoveOrResetPlayer(player, false, false, true, true) --- SeparateSpawnsInitPlayer(player.index) --- end - --- ---Resets the player and merges their force into the abandoned_force. --- ---@param player LuaPlayer --- ---@return nil --- function ResetPlayerAndAbandonForce(player) --- local player_old_force = player.force - --- player.force = storage.ocfg.gameplay.main_force_name - --- if ((#player_old_force.players == 0) and (player_old_force.name ~= storage.ocfg.gameplay.main_force_name)) then --- SendBroadcastMsg("Team " .. player_old_force.name .. " has been abandoned!") --: localize --- log("AbandonForce - FORCE ABANDONED: " .. player_old_force.name) --- game.merge_forces(player_old_force, ABANDONED_FORCE_NAME) --- end - --- RemoveOrResetPlayer(player, false, false, false, false) --- SeparateSpawnsInitPlayer(player.index) --- end - --- ---Reset player and merge their force to neutral --- ---@param player LuaPlayer --- ---@return nil --- function ResetPlayerAndMergeForceToNeutral(player) --- RemoveOrResetPlayer(player, false, true, true, true) --- SeparateSpawnsInitPlayer(player.index) --- end - ---Call this if a player leaves the game early (or a player wants an early game reset) ---@param player LuaPlayer ---@param remove_player boolean Deletes player from the game assuming they are offline. @@ -1210,10 +1166,11 @@ end ---@param player_name string ---@param surface_name string ---@param first_spawn boolean +---@param is_host boolean ---@return nil -function QueueNilCharacterForNewSpawnTeleport(player_name, surface_name, first_spawn) +function QueueNilCharacterForNewSpawnTeleport(player_name, surface_name, first_spawn, is_host) if (storage.nil_character_teleport_queue[player_name] == nil) then - storage.nil_character_teleport_queue[player_name] = { surface_name = surface_name, first_spawn = first_spawn } + storage.nil_character_teleport_queue[player_name] = { surface_name = surface_name, first_spawn = first_spawn, is_host = is_host } end end @@ -1231,7 +1188,7 @@ function OnTickNilCharacterTeleportQueue() -- And hope to high heaven this doesn't recurse infinitely. elseif (player.character ~= nil) then storage.nil_character_teleport_queue[player_name] = nil - SendPlayerToNewSpawn(player_name, data.surface_name, data.first_spawn) + SendPlayerToNewSpawn(player_name, data.surface_name, data.first_spawn, data.is_host) end end end @@ -1691,7 +1648,7 @@ function DelayedSpawnOnTick() for _,player_name in pairs(delayed_spawn.waiting_players) do local player = game.players[player_name] if (player ~= nil) then - SendPlayerToNewSpawn(player_name, delayed_spawn.surface_name, delayed_spawn.primary) + SendPlayerToNewSpawn(player_name, delayed_spawn.surface_name, delayed_spawn.primary, delayed_spawn.host_name == player_name) end end @@ -1878,4 +1835,4 @@ SPAWN_TEAM_CHOICE = { ---@alias OarcSurfaceSpawnSetting { primary: boolean, secondary: boolean} ---Entry for a nil_character_teleport_queue ----@alias OarcNilCharacterTeleportQueueEntry { surface_name: string, first_spawn: boolean } +---@alias OarcNilCharacterTeleportQueueEntry { surface_name: string, first_spawn: boolean, is_host: boolean } diff --git a/lib/separate_spawns_guis.lua b/lib/separate_spawns_guis.lua index abd9be9..583e512 100644 --- a/lib/separate_spawns_guis.lua +++ b/lib/separate_spawns_guis.lua @@ -127,15 +127,6 @@ function CreateSpawnMenuGuiFrame(player) } subhead.style.height = 46 subhead.style.horizontally_stretchable = true - -- changelog_subheader_frame = - -- { - -- type = "frame_style", - -- parent = "subheader_frame", - -- left_padding = 12, - -- right_padding = 12, - -- top_padding = 4, - -- horizontally_stretchable = "on" - -- } AddLabel(subhead, "warning_lbl1", { "oarc-click-info-btn-help" }, my_note_style) return inside_frame @@ -623,6 +614,8 @@ function DisplaySpawnOptions(player) CreateSoloSpawnFrame(sGui, gameplay.enable_shared_spawns, gameplay.number_of_players_per_shared_spawn) -- The primary method of spawning CreateSharedSpawnFrame(sGui, gameplay.enable_shared_spawns) -- Spawn options to join another player's base. CreateBuddySpawnFrame(sGui, player, gameplay.enable_buddy_spawn, gameplay.enable_separate_teams) -- Awesome buddy spawning system + + script.raise_event("oarc-mod-on-spawn-choices-gui-displayed", { player_index = player.index, gui_element = sGui }) end