From 21779988f0b90f0c24727729c708897d06210b5f Mon Sep 17 00:00:00 2001 From: PHIDIAS Date: Fri, 26 Jun 2026 19:54:05 +0900 Subject: [PATCH] lawnmower changed to selection (#434) * Update lawnmower.lua * Update quick_actions.lua * Update lawnmower.lua * update locale * Update lawnmower.lua * Fix game.get_player --------- Co-authored-by: Cooldude2606 <25043174+Cooldude2606@users.noreply.github.com> --- exp_scenario/module/commands/lawnmower.lua | 73 +++++++++++++++------- exp_scenario/module/gui/quick_actions.lua | 2 + exp_scenario/module/locale/en.cfg | 4 ++ 3 files changed, 56 insertions(+), 23 deletions(-) diff --git a/exp_scenario/module/commands/lawnmower.lua b/exp_scenario/module/commands/lawnmower.lua index e002f21b..f75f2fdc 100644 --- a/exp_scenario/module/commands/lawnmower.lua +++ b/exp_scenario/module/commands/lawnmower.lua @@ -2,32 +2,58 @@ Adds a command that clean up biter corpse and nuclear hole ]] +local AABB = require("modules/exp_util/aabb") local Commands = require("modules/exp_commands") -local config = require("modules.exp_legacy.config.lawnmower") --- @dep config.lawnmower +local Selection = require("modules/exp_util/selection") +local SelectArea = Selection.connect("ExpCommand_Lawnmower") +local config = require("modules.exp_legacy.config.lawnmower") -Commands.new("lawnmower", { "exp-commands_lawnmower.description" }) - :argument("range", { "exp-commands_lawnmower.arg-range" }, Commands.types.integer_range(1, 200)) - :register(function(player, range) - --- @cast range number - local surface = player.surface - - -- Intentionally left as player.position to allow use in remote view - local entities = surface.find_entities_filtered{ position = player.position, radius = range, type = "corpse" } - for _, entity in pairs(entities) do - if (entity.name ~= "transport-caution-corpse" and entity.name ~= "invisible-transport-caution-corpse") then - entity.destroy() - end - end - - local replace_tiles = {} - local tiles = surface.find_tiles_filtered{ position = player.position, radius = range, name = { "nuclear-ground" } } - for i, tile in pairs(tiles) do - replace_tiles[i] = { name = "grass-1", position = tile.position } +--- @class ExpCommand_Lawnmower.commands +local commands = {} + +--- Toggle player selection mode for lawnmower +--- @class ExpCommands_Lawnmower.commands.lawnmower: ExpCommand +--- @overload fun(player: LuaPlayer) +commands.lawnmower = Commands.new("lawnmower", { "exp-commands_lawnmower.description" }) + :register(function(player) + if SelectArea:stop(player) then + return Commands.status.success{ "exp-commands_lawnmower.exit" } end - surface.set_tiles(replace_tiles) - surface.destroy_decoratives{ position = player.position, radius = range } - end) + SelectArea:start(player) + return Commands.status.success{ "exp-commands_lawnmower.enter" } + end) --[[ @as any ]] + +--- When an area is selected to be handled +SelectArea:on_selection(function(event) + local player = assert(game.get_player(event.player_index)) + local area = AABB.expand(event.area) + local surface = event.surface + local area_size = (area.right_bottom.x - area.left_top.x) * (area.right_bottom.y - area.left_top.y) + + if area_size > 1000 then + player.print({ "exp-commands_lawnmower.area-too-large", 1000, area_size }, Commands.print_settings.error) + return + end + + local entities = surface.find_entities_filtered{ area = area, type = "corpse" } + for _, entity in pairs(entities) do + if (entity.name ~= "transport-caution-corpse" and entity.name ~= "invisible-transport-caution-corpse") then + entity.destroy() + end + end + + local replace_tiles = {} + local tiles = surface.find_tiles_filtered{ area = area, name = { "nuclear-ground" } } + for i, tile in pairs(tiles) do + replace_tiles[i] = { name = "grass-1", position = tile.position } + end + + surface.set_tiles(replace_tiles) + surface.destroy_decoratives{ area = area } + + player.print({ "exp-commands_lawnmower.complete", #replace_tiles }, Commands.print_settings.default) +end) --- @param event EventData.on_built_entity | EventData.on_robot_built_entity | EventData.script_raised_built | EventData.script_raised_revive local function destroy_decoratives(event) @@ -48,5 +74,6 @@ if config.destroy_decoratives then end return { - events = events + events = events, + commands = commands, } diff --git a/exp_scenario/module/gui/quick_actions.lua b/exp_scenario/module/gui/quick_actions.lua index b8f7e605..17847a8f 100644 --- a/exp_scenario/module/gui/quick_actions.lua +++ b/exp_scenario/module/gui/quick_actions.lua @@ -11,6 +11,7 @@ local addon_research = require("modules/exp_scenario/commands/research") local addon_trains = require("modules/exp_scenario/commands/trains") local addon_teleport = require("modules/exp_scenario/commands/teleport") local addon_waterfill = require("modules/exp_scenario/commands/waterfill") +local addon_lawnmower = require("modules/exp_scenario/commands/lawnmower") local addon_home = require("modules/exp_scenario/commands/home") local addon_vlayer = require("modules/exp_scenario/commands/vlayer") local addon_repair = require("modules/exp_scenario/commands/repair") @@ -54,6 +55,7 @@ new_quick_action("spawn", addon_teleport.commands.spawn, function(def, player, e end) new_quick_action("waterfill", addon_waterfill.commands.waterfill) +new_quick_action("lawnmower", addon_lawnmower.commands.lawnmower) new_quick_action("home", addon_home.commands.home) new_quick_action("return", addon_home.commands._return) new_quick_action("set-home", addon_home.commands.set_home) diff --git a/exp_scenario/module/locale/en.cfg b/exp_scenario/module/locale/en.cfg index 731d80af..70572e03 100644 --- a/exp_scenario/module/locale/en.cfg +++ b/exp_scenario/module/locale/en.cfg @@ -103,6 +103,10 @@ lower-role=You must have a higher role than the target. [exp-commands_lawnmower] description=Clean up biter corpse, decoratives and nuclear hole. arg-range=Range to clean up. +enter=Entered selection mode, select the area to fill with water. +exit=Exited selection mode. +area-too-large=Selected area is too large, must be less than __1__ tiles, selected __2__. +complete=__1__ tiles were handled. [exp-commands_locate] description=Opens remote view at the location of the player's last location.