1
0
mirror of https://github.com/Oarcinae/FactorioScenarioMultiplayerSpawn.git synced 2024-12-04 09:43:00 +02:00

Saving work on custom events and example scenario.

This commit is contained in:
Oarcinae 2024-11-28 13:30:11 -05:00
parent 47ddc9fd08
commit 8a077190ec
7 changed files with 198 additions and 5 deletions

View File

@ -20,6 +20,9 @@ script.on_init(function(event)
if not script.active_mods["oarc-mod"] then
error("OARC mod not found! This scenario is intended to be run with the OARC mod!")
end
storage.ocfg_copy = remote.call("oarc_mod", "get_mod_settings")
log("server info test: ")
end)
@ -47,4 +50,164 @@ local oarc_scenario_interface =
end
}
remote.add_interface("oarc_scenario", oarc_scenario_interface)
remote.add_interface("oarc_scenario", oarc_scenario_interface)
----------------------------------------------------------------------------------------------------------------
-- Everything below here is an example of how to use the custom events provided by the OARC mod.
----------------------------------------------------------------------------------------------------------------
local util = require("util")
-- You can require files to use them if you want like this:
require("__oarc-mod__/lib/oarc_gui_utils")
-- This will keep a local copy of the mod's config so you can use it in your custom events.
script.on_event("oarc-mod-on-config-changed", function(event)
storage.ocfg_copy = remote.call("oarc_mod", "get_mod_settings")
end)
-- This is how you can customize the spawn options menu by inserting whatever you want into it!
script.on_event("oarc-mod-on-spawn-choices-gui-displayed", function(event)
--This is just here so I don't get lua warnings
---@type OarcModOnSpawnChoicesGuiDisplayedEvent
local custom_event = event
local player = game.players[custom_event.player_index]
-- The 4 main sub sections are called: spawn_settings_frame, solo_spawn_frame, shared_spawn_frame, and buddy_spawn_frame
local gui = custom_event.gui_element
if (gui.spawn_settings_frame ~= nil) then
CreateTilesSelectDropdown(gui.spawn_settings_frame)
end
if storage.custom_spawn_choices == nil then
storage.custom_spawn_choices = {}
end
if storage.custom_spawn_choices[player.name] == nil then
storage.custom_spawn_choices[player.name] = { tile_select_name = "refined-concrete" }
end
end)
-- This is how you can customize the spawn area chunk generation process.
script.on_event("oarc-mod-on-chunk-generated-near-spawn", function(event)
--This is just here so I don't get lua warnings
---@type OarcModOnChunkGeneratedNearSpawnEvent
local custom_event = event
-- Get the spawn config from our local copy.
local general_spawn_config = storage.ocfg_copy.spawn_general
local surface_spawn_config = storage.ocfg_copy.surfaces_config[custom_event.surface.name].spawn_config
local radius = general_spawn_config.spawn_radius_tiles * surface_spawn_config.radius_modifier
-- Use spawn data to look up custom spawn choices if you want to
local host_name = custom_event.spawn_data.host_name
local custom_spawn_choices = storage.custom_spawn_choices[host_name]
if custom_spawn_choices == nil then
error("Custom spawn choices entry not found for host: " .. host_name)
end
-- As an example, we are placing a specific tile over the entire spawn area.
local chunk_area = custom_event.chunk_area
local tiles = {}
-- I leave it as an exercise to the reader to implement the other shapes:
if (general_spawn_config.shape == "circle") then
for x = chunk_area.left_top.x, chunk_area.right_bottom.x, 1 do
for y = chunk_area.left_top.y, chunk_area.right_bottom.y, 1 do
if (util.distance(custom_event.spawn_data.position, {x=x,y=y}) < radius) then
table.insert(tiles, {name=custom_spawn_choices.tile_select_name, position={x,y}})
end
end
end
end
custom_event.surface.set_tiles(tiles)
end)
---A helper function to create a dropdown for selecting tiles
---@param parent_flow LuaGuiElement
---@return nil
function CreateTilesSelectDropdown(parent_flow)
local flow = parent_flow.add {
name = "tile_horizontal_flow",
type = "flow",
direction = "horizontal"
}
local tiles =
{
"concrete" ,
"refined-concrete" ,
"red-refined-concrete" ,
"green-refined-concrete" ,
"blue-refined-concrete" ,
"orange-refined-concrete" ,
"yellow-refined-concrete" ,
"pink-refined-concrete" ,
"purple-refined-concrete" ,
"black-refined-concrete" ,
"brown-refined-concrete" ,
"cyan-refined-concrete" ,
"acid-refined-concrete" ,
}
local tilesLocalised = {}
for _, name in ipairs(tiles) do
table.insert(tilesLocalised, {"", "[tile="..name.. "]", " ", prototypes.tile[name].localised_name} )
end
AddLabel(flow, nil, { "oarc-tile-select-cap"}, my_label_style)
flow.add {
type = "drop-down",
name = "tile_select_dropdown",
tags = { action = "custom_spawn_options", setting = "tile_select" },
selected_index = 1,
items = tilesLocalised
}
end
---Handle dropdown selection changes
---@param event EventData.on_gui_selection_state_changed
---@return nil
function CustomSpawnOptsSelectionChanged(event)
if not event.element.valid then return end
local player = game.players[event.player_index]
local tags = event.element.tags
if (tags.action ~= "custom_spawn_options") then
return
end
if (tags.setting == "tile_select") then
local tiles =
{
"concrete" ,
"refined-concrete" ,
"red-refined-concrete" ,
"green-refined-concrete" ,
"blue-refined-concrete" ,
"orange-refined-concrete" ,
"yellow-refined-concrete" ,
"pink-refined-concrete" ,
"purple-refined-concrete" ,
"black-refined-concrete" ,
"brown-refined-concrete" ,
"cyan-refined-concrete" ,
"acid-refined-concrete" ,
}
local index = event.element.selected_index
storage.custom_spawn_choices[player.name].tile_select_name = tiles[index]
end
end
--- For dropdowns and listboxes.
script.on_event(defines.events.on_gui_selection_state_changed, function(event)
if not event.element.valid then return end
CustomSpawnOptsSelectionChanged(event)
end)

View File

@ -1,3 +1,6 @@
scenario-name=OARC-TEMPLATE
description=This is an EMPTY TEMPLATE scenario! It is provided as an example of how to override the default freeplay scenario if you want to. Additionally, experienced server hosts can configure all settings from a file instead of through the mod settings. Please read the control.lua file inside the scenario for notes. [color=red]To edit this scenario, you must make a copy of it and place it in your own scenarios folder first. Do not edit this scenario directly.[/color]
# Custom locale settings example
oarc-tile-select-cap=Decorative tiles:

View File

@ -212,11 +212,21 @@ script.on_event("oarc-mod-character-surface-changed", function(event)
SeparateSpawnsPlayerChangedSurface(player, custom_event.old_surface_name, custom_event.new_surface_name)
end)
---@class OarcModOnChunkGeneratedNearSpawnEvent: OarcCustomEventBase
---@field surface LuaSurface
---@field chunk_area BoundingBox
---@field spawn_data OarcUniqueSpawn
script.on_event("oarc-mod-on-chunk-generated-near-spawn", function(event)
log("EVENT - oarc-mod-on-chunk-generated-near-spawn:" .. serpent.block(event --[[@as OarcModOnChunkGeneratedNearSpawnEvent]]))
end)
---@class OarcModOnConfigChangedEvent: OarcCustomEventBase
script.on_event("oarc-mod-on-config-changed", function(event)
log("EVENT - oarc-mod-on-config-changed:" .. serpent.block(event --[[@as OarcModOnConfigChangedEvent]]))
end)
-- I raise this event whenever teleporting the player!
script.on_event(defines.events.script_raised_teleported, function(event)
log("script_raised_teleported")
log(serpent.block(event))
local entity = event.entity
if entity.type == "character" and entity.player then
SeparateSpawnsUpdatePlayerSurface(entity.player, entity.surface.name)

View File

@ -62,6 +62,18 @@ data:extend({
type = "custom-event",
name = "oarc-mod-character-surface-changed",
},
-- A chunk was generated near a unique spawn
{
type = "custom-event",
name = "oarc-mod-on-chunk-generated-near-spawn",
},
-- An entry in storage.ocfg was changed
{
type = "custom-event",
name = "oarc-mod-on-config-changed",
},
})

View File

@ -345,7 +345,7 @@ function RuntimeModSettingChanged(event)
end
end
---A probably quit stupid function to let me lookup and set the storage.ocfg entries using a key table.
---A probably quite stupid function to let me lookup and set the storage.ocfg entries using a key table.
---@param key_table table<integer, string>
---@param value any
function SetGlobalOarcConfigUsingKeyTable(key_table, value)
@ -360,6 +360,9 @@ function SetGlobalOarcConfigUsingKeyTable(key_table, value)
else
error("Invalid key_table length: " .. number_of_keys .. "\n" .. serpent.block(key_table))
end
-- Hopefully I'm not missing any other entry points where storage.ocfg might be changed.
script.raise_event("oarc-mod-on-config-changed", {})
end
---An equally stupid function to let me lookup the storage.ocfg entries using a key table.

View File

@ -595,6 +595,7 @@ function SettingsSurfaceControlsTabGuiClick(event)
storage.ocfg = table.deepcopy(copy)
ValidateSettings() -- Some basic validation, not 100% foolproof
SyncModSettingsToOCFG() -- Sync the mod settings.
script.raise_event("oarc-mod-on-config-changed", {})
log("Imported settings!")
CompatSend(player, "Imported settings!")
OarcGuiRefreshContent(player)

View File

@ -800,6 +800,7 @@ function SetupAndClearSpawnAreas(surface, chunkArea)
CreateCropSquare(surface, spawn, chunkArea)
end
script.raise_event("oarc-mod-on-chunk-generated-near-spawn", {surface = surface, chunk_area = chunkArea, spawn_data = spawn})
:: CONTINUE ::
end
end