mirror of
https://github.com/PHIDIAS0303/ExpCluster.git
synced 2026-11-17 08:50:42 +09:00
Features can now be enabled, disabled, and configured from the web UI while the game is running, without editing Lua files. The first five features covered are AFK Kick, Chat Auto Reply, Death Markers, Mine Depletion, and Station Auto Name. Lua side: module/features.lua holds each feature's defaults, keeps the overrides from the controller in storage, and patches them onto the config table in place. Features.guard skips a feature's handlers while it is disabled. The five config files move from exp_legacy into exp_scenario/module/config and their control files now read config at runtime instead of deciding at load which handlers to register. AFK Kick times are now in minutes. Controller side: features.ts lists the editable settings and their defaults. Overrides that differ from the default are stored in exp_scenario/features.json and guarded by the existing exp_scenario.config.view/edit permissions. The instance plugin sends them over rcon on start, on every change, and again after the controller restarts. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
108 lines
3.8 KiB
Lua
108 lines
3.8 KiB
Lua
--[[-- Control - Station Auto Name
|
|
Automatically name stations when they are placed based on closest resource and direction from spawn
|
|
]]
|
|
|
|
local Features = require("modules/exp_scenario/features")
|
|
local config = require("modules/exp_scenario/config/station_auto_name")
|
|
|
|
local get_direction do
|
|
local directions = {
|
|
["W"] = -0.875,
|
|
["NW"] = -0.625,
|
|
["N"] = -0.375,
|
|
["NE"] = -0.125,
|
|
["E"] = 0.125,
|
|
["SE"] = 0.375,
|
|
["S"] = 0.625,
|
|
["SW"] = 0.875,
|
|
}
|
|
|
|
--- Get the direction of a position from the centre of the surface
|
|
--- @param position MapPosition
|
|
--- @return string
|
|
function get_direction(position)
|
|
local angle = math.atan2(position.y, position.x) / math.pi
|
|
for direction, required_angle in pairs(directions) do
|
|
if angle < required_angle then
|
|
return direction
|
|
end
|
|
end
|
|
|
|
return "W"
|
|
end
|
|
end
|
|
|
|
-- Custom strings are used to detect backer names from ghosts
|
|
local custom_string = " *"
|
|
local custom_string_len = #custom_string
|
|
|
|
--- Change the name of a station when it is placed
|
|
--- @param event EventData.on_built_entity | EventData.on_robot_built_entity
|
|
local function rename_station(event)
|
|
local entity = event.entity
|
|
local name = entity.name
|
|
if name == "entity-ghost" and entity.ghost_name == "train-stop" then
|
|
local backer_name = entity.backer_name
|
|
if backer_name ~= "" then
|
|
entity.backer_name = backer_name .. custom_string
|
|
end
|
|
|
|
elseif name == "train-stop" then
|
|
-- Restore the backer name
|
|
local backer_name = entity.backer_name or ""
|
|
if backer_name:sub(-custom_string_len) == custom_string then
|
|
entity.backer_name = backer_name:sub(1, -custom_string_len - 1)
|
|
return
|
|
end
|
|
|
|
-- Find the closest resource
|
|
local icon = ""
|
|
local item_name = ""
|
|
local bounding_box = entity.bounding_box
|
|
local resources = entity.surface.find_entities_filtered{ position = entity.position, radius = 250, type = "resource" }
|
|
if #resources > 0 then
|
|
local closest_recourse --- @type LuaEntity?
|
|
local closest_distance = 250 * 250 -- search radius + 1
|
|
local px, py = bounding_box.left_top.x, bounding_box.left_top.y
|
|
|
|
-- Check which recourse is closest
|
|
for _, resource in ipairs(resources) do
|
|
local resource_box = resource.bounding_box
|
|
local dx = px - resource_box.left_top.x
|
|
local dy = py - resource_box.left_top.y
|
|
local distance = (dx * dx) + (dy * dy)
|
|
if distance < closest_distance then
|
|
closest_distance = distance
|
|
closest_recourse = resource
|
|
end
|
|
end
|
|
|
|
-- Set the item name and icon
|
|
if closest_recourse then
|
|
item_name = closest_recourse.name:gsub("^%l", string.upper):gsub("-", " ") -- remove dashes and making first letter capital
|
|
local products = assert(closest_recourse.prototype.mineable_properties.products)
|
|
local product = assert(products[1])
|
|
icon = string.format("[img=%s.%s]", product.type, product.name)
|
|
end
|
|
end
|
|
|
|
-- Rename the station
|
|
entity.backer_name = config.station_name
|
|
:gsub("__icon__", icon)
|
|
:gsub("__item_name__", item_name)
|
|
:gsub("__backer_name__", entity.backer_name)
|
|
:gsub("__direction__", get_direction(entity.position))
|
|
:gsub("__x__", math.floor(entity.position.x))
|
|
:gsub("__y__", math.floor(entity.position.y))
|
|
end
|
|
end
|
|
|
|
local e = defines.events
|
|
|
|
return Features.guard(config, {
|
|
events = {
|
|
[e.on_built_entity] = rename_station,
|
|
[e.on_robot_built_entity] = rename_station,
|
|
},
|
|
})
|