Refactor Selection Util (#409)

* Add Selection to ExpUtil

* Convert modules to use new lib

* Bug Fixes
This commit is contained in:
Cooldude2606
2025-09-04 10:54:55 +01:00
committed by GitHub
parent 85c7cce740
commit 27a2feaae9
9 changed files with 316 additions and 263 deletions

View File

@@ -4,8 +4,9 @@ Adds a command that helps shoot artillery
local AABB = require("modules/exp_util/aabb")
local Commands = require("modules/exp_commands")
local Selection = require("modules.exp_legacy.modules.control.selection") --- @dep modules.control.selection
local SelectionName = "ExpCommand_Artillery"
local Selection = require("modules/exp_util/selection")
local SelectArea = Selection.connect("ExpCommand_Artillery")
local floor = math.floor
local abs = math.abs
@@ -37,18 +38,15 @@ end
--- @overload fun(player: LuaPlayer)
commands.artillery = Commands.new("artillery", { "exp-commands_artillery.description" })
:register(function(player)
if Selection.is_selecting(player, SelectionName) then
Selection.stop(player)
if SelectArea:stop(player) then
return Commands.status.success{ "exp-commands_artillery.exit" }
else
Selection.start(player, SelectionName)
return Commands.status.success{ "exp-commands_artillery.enter" }
end
SelectArea:start(player)
return Commands.status.success{ "exp-commands_artillery.enter" }
end) --[[ @as any ]]
--- when an area is selected to add protection to the area
Selection.on_selection(SelectionName, function(event)
--- @cast event EventData.on_player_selected_area
SelectArea:on_selection(function(event)
local area = AABB.expand(event.area)
local player = game.players[event.player_index]
local surface = event.surface

View File

@@ -12,14 +12,14 @@ local Commands = require("modules/exp_commands")
local format_player_name = Commands.format_player_name_locale
local Roles = require("modules.exp_legacy.expcore.roles") --- @dep expcore.roles
local Selection = require("modules.exp_legacy.modules.control.selection") --- @dep modules.control.selection
local EntityProtection = require("modules.exp_legacy.modules.control.protection") --- @dep modules.control.protection
local format_string = string.format
local floor = math.floor
local SelectionNameEntity = "ExpCommand_ProtectEntity"
local SelectionNameArea = "ExpCommand_ProtectArea"
local Selection = require("modules/exp_util/selection")
local SelectEntities = Selection.connect("ExpCommand_ProtectEntity")
local SelectArea = Selection.connect("ExpCommand_ProtectArea")
local renders = {} --- @type table<number, table<string, LuaRenderObject>> Stores all renders for a player
Storage.register({
@@ -95,31 +95,26 @@ end
Commands.new("protect-entity", { "exp-commands_entity-protection.description-entity" })
:add_aliases{ "pe" }
:register(function(player)
if Selection.is_selecting(player, SelectionNameEntity) then
Selection.stop(player)
if SelectEntities:stop(player) then
return Commands.status.success{ "exp-commands_entity-protection.exit-entity" }
else
Selection.start(player, SelectionNameEntity)
return Commands.status.success{ "exp-commands_entity-protection.enter-entity" }
end
SelectEntities:start(player)
return Commands.status.success{ "exp-commands_entity-protection.enter-entity" }
end)
--- Toggles area protection selection
Commands.new("protect-area", { "exp-commands_entity-protection.description-area" })
:add_aliases{ "pa" }
:register(function(player)
if Selection.is_selecting(player, SelectionNameEntity) then
Selection.stop(player)
if SelectArea:stop(player) then
return Commands.status.success{ "exp-commands_entity-protection.exit-area" }
else
Selection.start(player, SelectionNameEntity)
return Commands.status.success{ "exp-commands_entity-protection.enter-area" }
end
SelectArea:start(player)
return Commands.status.success{ "exp-commands_entity-protection.enter-area" }
end)
--- When an area is selected to add protection to entities
Selection.on_selection(SelectionNameEntity, function(event)
--- @cast event EventData.on_player_selected_area
SelectEntities:on_selection(function(event)
local player = game.players[event.player_index]
for _, entity in ipairs(event.entities) do
EntityProtection.add_entity(entity)
@@ -130,8 +125,7 @@ Selection.on_selection(SelectionNameEntity, function(event)
end)
--- When an area is selected to remove protection from entities
Selection.on_alt_selection(SelectionNameEntity, function(event)
--- @cast event EventData.on_player_alt_selected_area
SelectEntities:on_alt_selection(function(event)
local player = game.players[event.player_index]
for _, entity in ipairs(event.entities) do
EntityProtection.remove_entity(entity)
@@ -142,8 +136,7 @@ Selection.on_alt_selection(SelectionNameEntity, function(event)
end)
--- When an area is selected to add protection to the area
Selection.on_selection(SelectionNameArea, function(event)
--- @cast event EventData.on_player_selected_area
SelectArea:on_selection(function(event)
local surface = event.surface
local area = expand_area(event.area)
local areas = EntityProtection.get_areas(event.surface)
@@ -160,8 +153,7 @@ Selection.on_selection(SelectionNameArea, function(event)
end)
--- When an area is selected to remove protection from the area
Selection.on_alt_selection(SelectionNameArea, function(event)
--- @cast event EventData.on_player_alt_selected_area
SelectArea:on_alt_selection(function(event)
local surface = event.surface
local area = expand_area(event.area)
local areas = EntityProtection.get_areas(surface)
@@ -177,7 +169,6 @@ end)
--- When selection starts show all protected entities and protected areas
local function on_player_selection_start(event)
if event.selection ~= SelectionNameEntity and event.selection ~= SelectionNameArea then return end
local player = game.players[event.player_index]
local surface = player.surface -- Allow remote view
renders[player.index] = {}
@@ -210,8 +201,7 @@ local function on_player_selection_start(event)
end
--- When selection ends hide protected entities and protected areas
local function on_player_selection_end(event)
if event.selection ~= SelectionNameEntity and event.selection ~= SelectionNameArea then return end
local function on_player_selection_stop(event)
for _, render in pairs(renders[event.player_index]) do
if render.valid then render.destroy() end
end
@@ -219,6 +209,11 @@ local function on_player_selection_end(event)
renders[event.player_index] = nil
end
SelectArea:on_start(on_player_selection_start)
SelectEntities:on_start(on_player_selection_start)
SelectArea:on_stop(on_player_selection_stop)
SelectEntities:on_stop(on_player_selection_stop)
--- When there is a repeat offence print it in chat
local function on_repeat_violation(event)
Roles.print_to_roles_higher("Regular", {
@@ -232,8 +227,6 @@ end
return {
events = {
[Selection.events.on_player_selection_start] = on_player_selection_start,
[Selection.events.on_player_selection_end] = on_player_selection_end,
[EntityProtection.events.on_repeat_violation] = on_repeat_violation,
}
}

View File

@@ -4,8 +4,9 @@ Adds a command that places shallow water
local AABB = require("modules/exp_util/aabb")
local Commands = require("modules/exp_commands")
local Selection = require("modules.exp_legacy.modules.control.selection") --- @dep modules.control.selection
local SelectionName = "ExpCommand_Waterfill"
local Selection = require("modules/exp_util/selection")
local SelectArea = Selection.connect("ExpCommand_Waterfill")
local planet = {
["nauvis"] = "water-mud",
@@ -23,25 +24,22 @@ local commands = {}
--- @overload fun(player: LuaPlayer)
commands.waterfill = Commands.new("waterfill", { "exp-commands_waterfill.description" })
:register(function(player)
if Selection.is_selecting(player, SelectionName) then
Selection.stop(player)
if SelectArea:stop(player) then
return Commands.status.success{ "exp-commands_waterfill.exit" }
end
local item_count_cliff = player.get_item_count("cliff-explosives")
local item_count_craft = math.min(math.floor(player.get_item_count("explosives") / 10), player.get_item_count("barrel"), player.get_item_count("grenade"))
local item_count_total = item_count_cliff + item_count_craft
if item_count_total == 0 then
return Commands.status.error{ "exp-commands_waterfill.requires-explosives" }
else
local item_count_cliff = player.get_item_count("cliff-explosives")
local item_count_craft = math.min(math.floor(player.get_item_count("explosives") / 10), player.get_item_count("barrel"), player.get_item_count("grenade"))
local item_count_total = item_count_cliff + item_count_craft
if item_count_total == 0 then
return Commands.status.error{ "exp-commands_waterfill.requires-explosives" }
else
Selection.start(player, SelectionName)
return Commands.status.success{ "exp-commands_waterfill.enter" }
end
SelectArea:start(player)
return Commands.status.success{ "exp-commands_waterfill.enter" }
end
end) --[[ @as any ]]
--- When an area is selected to be converted to water
Selection.on_selection(SelectionName, function(event)
--- @cast event EventData.on_player_selected_area
SelectArea:on_selection(function(event)
local area = AABB.expand(event.area)
local player = game.players[event.player_index]
local surface = event.surface

View File

@@ -5,8 +5,9 @@ Adds a Gui which creates an selection planner to insert modules into buildings
local Gui = require("modules/exp_gui")
local AABB = require("modules/exp_util/aabb")
local Roles = require("modules/exp_legacy/expcore/roles")
local Selection = require("modules/exp_legacy/modules/control/selection")
local SelectionModuleArea = "ModuleArea"
local Selection = require("modules/exp_util/selection")
local SelectArea = Selection.connect("ModuleArea")
local config = require("modules/exp_legacy/config/module")
@@ -76,7 +77,7 @@ Elements.create_selection_planner = Gui.define("module_inserter/create_selection
)
:on_click(function(def, player, element)
--- @cast def ExpGui_ModuleInserter.elements.create_selection_planner
Selection.start(player, SelectionModuleArea, false, def.data[element])
SelectArea:start(player, def.data[element])
end) --[[ @as any ]]
--- Used to select the machine to apply modules to
@@ -311,7 +312,7 @@ end
--- When an area is selected to have module changes applied to it
--- @param event EventData.on_player_selected_area
--- @param module_table LuaGuiElement
Selection.on_selection(SelectionModuleArea, function(event, module_table)
SelectArea:on_selection(function(event, module_table)
local player = Gui.get_player(event)
local area = AABB.expand(event.area)

View File

@@ -301,8 +301,9 @@ end
--- @param force LuaForce
--- @return number
function Elements.container.calculate_starting_research_index(force)
local force_data = Elements.container.data[force]
local force_data = Elements.container.data[force] or {}
local research_index = research_targets.length
Elements.container.data[force] = force_data -- needed because of @clusterio/research_sync
-- # does not work here because it returned the array alloc size
for i = 1, research_targets.length do