From d35a55ea9e30acee4ba074418bbac1b4f2c70d61 Mon Sep 17 00:00:00 2001 From: Starbeamrainbowlabs Date: Sun, 3 May 2020 00:37:18 +0100 Subject: [PATCH] commands: refactor into separate files --- .../commands/ellipsoid.lua | 92 ++++ .../commands/floodfill.lua | 64 +++ worldeditadditions_commands/commands/maze.lua | 121 +++++ .../commands/overlay.lua | 41 ++ .../commands/torus.lua | 92 ++++ worldeditadditions_commands/init.lua | 415 +----------------- 6 files changed, 421 insertions(+), 404 deletions(-) create mode 100644 worldeditadditions_commands/commands/ellipsoid.lua create mode 100644 worldeditadditions_commands/commands/floodfill.lua create mode 100644 worldeditadditions_commands/commands/maze.lua create mode 100644 worldeditadditions_commands/commands/overlay.lua create mode 100644 worldeditadditions_commands/commands/torus.lua diff --git a/worldeditadditions_commands/commands/ellipsoid.lua b/worldeditadditions_commands/commands/ellipsoid.lua new file mode 100644 index 0000000..a090b25 --- /dev/null +++ b/worldeditadditions_commands/commands/ellipsoid.lua @@ -0,0 +1,92 @@ +local we_c = worldeditadditions_commands + +-- ███████ ██ ██ ██ ██████ ███████ ██████ ██ ██████ +-- ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ +-- █████ ██ ██ ██ ██████ ███████ ██ ██ ██ ██ ██ +-- ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ +-- ███████ ███████ ███████ ██ ██ ███████ ██████ ██ ██████ + +local function parse_params_ellipsoid(params_text) + local found, _, radius_x, radius_y, radius_z, replace_node = params_text:find("([0-9]+)%s+([0-9]+)%s+([0-9]+)%s+([a-z:_\\-]+)") + + if found == nil then + return nil, nil + end + + local radius = { + x = tonumber(radius_x), + y = tonumber(radius_y), + z = tonumber(radius_z) + } + + replace_node = worldedit.normalize_nodename(replace_node) + + return replace_node, radius +end + +minetest.register_chatcommand("/ellipsoid", { + params = " ", + description = "Creates a 3D ellipsoid with a radius of (rx, ry, rz) at pos1, filled with .", + privs = { worldedit = true }, + func = we_c.safe_region(function(name, params_text) + local target_node, radius = parse_params_ellipsoid(params_text) + + if not target_node then + worldedit.player_notify(name, "Error: Invalid node name.") + return false + end + if not radius then + worldedit.player_notify(name, "Error: Invalid radius(es).") + return false + end + + local start_time = os.clock() + local replaced = worldedit.ellipsoid(worldedit.pos1[name], radius, target_node, false) + local time_taken = os.clock() - start_time + + worldedit.player_notify(name, replaced .. " nodes replaced in " .. time_taken .. "s") + minetest.log("action", name .. " used //ellipsoid at " .. worldeditadditions.vector.tostring(worldedit.pos1[name]) .. ", replacing " .. replaced .. " nodes in " .. time_taken .. "s") + end, function(name, params_text) + local target_node, radius = parse_params_ellipsoid(params_text) + if not target_node or not radius then + worldedit.player_notify(name, "Error: Invalid input '" .. params_text .. "'. Try '/help /ellipsoid' to learn how to use this command.") + return 0 + end + + return math.ceil(4/3 * math.pi * radius.x * radius.y * radius.z) + end) +}) + +-- TODO: This duplicates a lot of code. Perhaps we can trim it down a bit? +minetest.register_chatcommand("/hollowellipsoid", { + params = " ", + description = "Creates a 3D hollow ellipsoid with a radius of (rx, ry, rz) at pos1, made out of .", + privs = { worldedit = true }, + func = we_c.safe_region(function(name, params_text) + local target_node, radius = parse_params_ellipsoid(params_text) + + if not target_node then + worldedit.player_notify(name, "Error: Invalid node name.") + return false + end + if not radius then + worldedit.player_notify(name, "Error: Invalid radius(es).") + return false + end + + local start_time = os.clock() + local replaced = worldedit.ellipsoid(worldedit.pos1[name], radius, target_node, true) + local time_taken = os.clock() - start_time + + worldedit.player_notify(name, replaced .. " nodes replaced in " .. time_taken .. "s") + minetest.log("action", name .. " used //hollowellipsoid at " .. worldeditadditions.vector.tostring(worldedit.pos1[name]) .. ", replacing " .. replaced .. " nodes in " .. time_taken .. "s") + end, function(name, params_text) + local target_node, radius = parse_params_ellipsoid(params_text) + if not target_node or not radius then + worldedit.player_notify(name, "Error: Invalid input '" .. params_text .. "'. Try '/help /hollowellipsoid' to learn how to use this command.") + return 0 + end + + return math.ceil(4/3 * math.pi * radius.x * radius.y * radius.z) + end) +}) diff --git a/worldeditadditions_commands/commands/floodfill.lua b/worldeditadditions_commands/commands/floodfill.lua new file mode 100644 index 0000000..237f9b0 --- /dev/null +++ b/worldeditadditions_commands/commands/floodfill.lua @@ -0,0 +1,64 @@ +local we_c = worldeditadditions_commands + +-- ███████ ██ ██████ ██████ ██████ ███████ ██ ██ ██ +-- ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ +-- █████ ██ ██ ██ ██ ██ ██ ██ █████ ██ ██ ██ +-- ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ +-- ██ ███████ ██████ ██████ ██████ ██ ██ ███████ ███████ + +local function parse_params_floodfill(params_text) + if not params_text then params_text = "" end + local found, _, replace_node, radius = params_text:find("([a-z:_\\-]+)%s+([0-9]+)") + + if found == nil then + found, _, replace_node = params_text:find("([a-z:_\\-]+)") + radius = 20 + end + if found == nil then + replace_node = "default:water_source" + end + radius = tonumber(radius) + + replace_node = worldedit.normalize_nodename(replace_node) + + return replace_node, radius +end + +minetest.register_chatcommand("/floodfill", { + params = "[ []]", + description = "Floods all connected nodes of the same type starting at pos1 with (which defaults to `water_source`), in a sphere with a radius of (which defaults to 20).", + privs = { worldedit = true }, + func = we_c.safe_region(function(name, params_text) + local replace_node, radius = parse_params_floodfill(params_text) + + if not replace_node then + worldedit.player_notify(name, "Error: Invalid node name.") + return false + end + + if not worldedit.pos1[name] then + worldedit.player_notify(name, "Error: No pos1 defined.") + return false + end + + local start_time = os.clock() + local nodes_replaced = worldedit.floodfill(worldedit.pos1[name], radius, replace_node) + local time_taken = os.clock() - start_time + + if nodes_replaced == false then + worldedit.player_notify(name, "Error: The search node is the same as the replace node.") + return false + end + + worldedit.player_notify(name, nodes_replaced .. " nodes replaced in " .. time_taken .. "s") + minetest.log("action", name .. " used //floodfill at " .. worldeditadditions.vector.tostring(worldedit.pos1[name]) .. ", replacing " .. nodes_replaced .. " nodes in " .. time_taken .. "s") + end, function(name, params_text) + local replace_node, radius = parse_params_floodfill(params_text) + + if not worldedit.pos1[name] then + return 0 -- The actual command will send the error message to the client + end + -- Volume of a hemisphere + return math.ceil(((4 * math.pi * (tonumber(radius) ^ 3)) / 3) / 2) + end) +}) diff --git a/worldeditadditions_commands/commands/maze.lua b/worldeditadditions_commands/commands/maze.lua new file mode 100644 index 0000000..d55a5b8 --- /dev/null +++ b/worldeditadditions_commands/commands/maze.lua @@ -0,0 +1,121 @@ +local we_c = worldeditadditions_commands + +-- ███ ███ █████ ███████ ███████ +-- ████ ████ ██ ██ ███ ██ +-- ██ ████ ██ ███████ ███ █████ +-- ██ ██ ██ ██ ██ ███ ██ +-- ██ ██ ██ ██ ███████ ███████ + +local function parse_params_maze(params_text) + if not params_text then + return nil, nil, nil + end + local found, _, replace_node, seed_text = params_text:find("([a-z:_\\-]+)%s+([0-9]+)") + + local has_seed = true + + if found == nil then + has_seed = false + replace_node = params_text + end + + local seed = tonumber(seed_text) + + replace_node = worldedit.normalize_nodename(replace_node) + + return replace_node, seed, has_seed +end + +minetest.register_chatcommand("/maze", { + params = " []", + description = "Generates a maze covering the currently selected area (must be at least 3x3 on the x,z axes) with replace_node as the walls. Optionally takes a (integer) seed.", + privs = { worldedit = true }, + func = we_c.safe_region(function(name, params_text) + local replace_node, seed, has_seed = parse_params_maze(params_text) + + if not replace_node then + worldedit.player_notify(name, "Error: Invalid node name.") + return false + end + if not seed and has_seed then + worldedit.player_notify(name, "Error: Invalid seed.") + return false + end + if not seed then seed = os.time() end + + local start_time = os.clock() + local replaced = worldedit.maze(worldedit.pos1[name], worldedit.pos2[name], replace_node, seed) + local time_taken = os.clock() - start_time + + worldedit.player_notify(name, replaced .. " nodes replaced in " .. time_taken .. "s") + minetest.log("action", name .. " used //maze at " .. worldeditadditions.vector.tostring(worldedit.pos1[name]) .. ", replacing " .. replaced .. " nodes in " .. time_taken .. "s") + end, function(name, params_text) + local replace_node, seed, has_seed = parse_params_maze(params_text) + if not params_text then params_text = "" end + + if not replace_node then + worldedit.player_notify(name, "Error: Invalid input '" .. params_text .. "' (specifically the replace node). Try '/help /maze' to learn how to use this command.") + return 0 + end + if not seed and has_seed then + worldedit.player_notify(name, "Error: Invalid input '" .. params_text .. "' (specifically the seed). Try '/help /maze' to learn how to use this command.") + return 0 + end + + local pos1 = worldedit.pos1[name] + local pos2 = worldedit.pos2[name] + + return (pos2.x - pos1.x) * (pos2.y - pos1.y) * (pos1.z - pos2.z) + end) +}) + + + +-- ███ ███ █████ ███████ ███████ ██████ ██████ +-- ████ ████ ██ ██ ███ ██ ██ ██ ██ +-- ██ ████ ██ ███████ ███ █████ █████ ██ ██ +-- ██ ██ ██ ██ ██ ███ ██ ██ ██ ██ +-- ██ ██ ██ ██ ███████ ███████ ██████ ██████ + +minetest.register_chatcommand("/maze3d", { + params = " []", + description = "Generates a 3d maze covering the currently selected area (must be at least 3x3x3) with replace_node as the walls. Optionally takes a (integer) seed.", + privs = { worldedit = true }, + func = we_c.safe_region(function(name, params_text) + local replace_node, seed, has_seed = parse_params_maze(params_text) + + if not replace_node then + worldedit.player_notify(name, "Error: Invalid node name.") + return false + end + if not seed and has_seed then + worldedit.player_notify(name, "Error: Invalid seed.") + return false + end + if not seed then seed = os.time() end + + local start_time = os.clock() + local replaced = worldedit.maze3d(worldedit.pos1[name], worldedit.pos2[name], replace_node, seed) + local time_taken = os.clock() - start_time + + worldedit.player_notify(name, replaced .. " nodes replaced in " .. time_taken .. "s") + minetest.log("action", name .. " used //maze at " .. worldeditadditions.vector.tostring(worldedit.pos1[name]) .. ", replacing " .. replaced .. " nodes in " .. time_taken .. "s") + end, function(name, params_text) + local replace_node, seed, has_seed = parse_params_maze(params_text) + if not params_text then params_text = "" end + + if not replace_node then + worldedit.player_notify(name, "Error: Invalid input '" .. params_text .. "' (specifically the replace node). Try '/help /maze3d' to learn how to use this command.") + return 0 + end + if not seed and has_seed then + worldedit.player_notify(name, "Error: Invalid input '" .. params_text .. "' (specifically the seed). Try '/help /maze3d' to learn how to use this command.") + return 0 + end + + local pos1 = worldedit.pos1[name] + local pos2 = worldedit.pos2[name] + + return (pos2.x - pos1.x) * (pos2.y - pos1.y) * (pos1.z - pos2.z) + end) +}) diff --git a/worldeditadditions_commands/commands/overlay.lua b/worldeditadditions_commands/commands/overlay.lua new file mode 100644 index 0000000..3d9e2d1 --- /dev/null +++ b/worldeditadditions_commands/commands/overlay.lua @@ -0,0 +1,41 @@ +local we_c = worldeditadditions_commands + +-- ██████ ██ ██ ███████ ██████ ██ █████ ██ ██ +-- ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ +-- ██ ██ ██ ██ █████ ██████ ██ ███████ ████ +-- ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ +-- ██████ ████ ███████ ██ ██ ███████ ██ ██ ██ + +minetest.register_chatcommand("/overlay", { + params = "", + description = "Places in the last contiguous air space encountered above the first non-air node. In other words, overlays all top-most nodes in the specified area with .", + privs = { worldedit = true }, + func = we_c.safe_region(function(name, params_text) + local target_node = worldedit.normalize_nodename(params_text) + + if not target_node then + worldedit.player_notify(name, "Error: Invalid node name.") + return false + end + + local start_time = os.clock() + local changes = worldedit.overlay(worldedit.pos1[name], worldedit.pos2[name], target_node) + local time_taken = os.clock() - start_time + + worldedit.player_notify(name, changes.updated .. " nodes replaced and " .. changes.skipped_columns .. " columns skipped in " .. time_taken .. "s") + minetest.log("action", name .. " used //overlay at " .. worldeditadditions.vector.tostring(worldedit.pos1[name]) .. ", replacing " .. changes.updated .. " nodes and skipping " .. changes.skipped_columns .. " columns in " .. time_taken .. "s") + end, function(name, params_text) + if not worldedit.normalize_nodename(params_text) then + worldedit.player_notify(name, "Error: Invalid node name '" .. params_text .. "'.") + return 0 + end + + local pos1 = worldedit.pos1[name] + local pos2 = worldedit.pos2[name] + pos1, pos2 = worldedit.sort_pos(pos1, pos2) + + local vol = vector.subtract(pos2, pos1) + + return vol.x*vol.z + end) +}) diff --git a/worldeditadditions_commands/commands/torus.lua b/worldeditadditions_commands/commands/torus.lua new file mode 100644 index 0000000..4303a85 --- /dev/null +++ b/worldeditadditions_commands/commands/torus.lua @@ -0,0 +1,92 @@ +local we_c = worldeditadditions_commands + +-- ████████ ██████ ██████ ██ ██ ███████ +-- ██ ██ ██ ██ ██ ██ ██ ██ +-- ██ ██ ██ ██████ ██ ██ ███████ +-- ██ ██ ██ ██ ██ ██ ██ ██ +-- ██ ██████ ██ ██ ██████ ███████ +local function parse_params_torus(params_text) + local found, _, major_radius, minor_radius, replace_node = params_text:find("([0-9]+)%s+([0-9]+)%s+([a-z:_\\-]+)") + + if found == nil then + return nil, nil + end + + major_radius = tonumber(major_radius) + minor_radius = tonumber(minor_radius) + + replace_node = worldedit.normalize_nodename(replace_node) + + return replace_node, major_radius, minor_radius +end + +minetest.register_chatcommand("/torus", { + params = " ", + description = "Creates a 3D torus with a major radius of and a minor radius of at pos1, filled with .", + privs = { worldedit = true }, + func = we_c.safe_region(function(name, params_text) + local target_node, major_radius, minor_radius = parse_params_torus(params_text) + + if not target_node then + worldedit.player_notify(name, "Error: Invalid node name.") + return false + end + if not major_radius or not minor_radius then + worldedit.player_notify(name, "Error: Invalid radius(es).") + return false + end + + if not worldedit.pos1[name] then + worldedit.player_notify(name, "Error: No pos1 specified (try //1 or left click with the wand tool)") + end + + local start_time = os.clock() + local replaced = worldedit.torus(worldedit.pos1[name], major_radius, minor_radius, target_node, false) + local time_taken = os.clock() - start_time + + worldedit.player_notify(name, replaced .. " nodes replaced in " .. time_taken .. "s") + minetest.log("action", name .. " used //torus at " .. worldeditadditions.vector.tostring(worldedit.pos1[name]) .. ", replacing " .. replaced .. " nodes in " .. time_taken .. "s") + end, function(name, params_text) + local target_node, major_radius, minor_radius = parse_params_torus(params_text) + if not target_node or not major_radius or not minor_radius then + worldedit.player_notify(name, "Error: Invalid input '" .. params_text .. "'. Try '/help /torus' to learn how to use this command.") + return 0 + end + + return math.ceil(2 * math.pi*math.pi * major_radius * minor_radius*minor_radius) + end) +}) + +-- TODO: This duplicates a lot of code. Perhaps we can trim it down a bit? +minetest.register_chatcommand("/hollowtorus", { + params = " ", + description = "Creates a 3D hollow torus with a major radius of and a minor radius of at pos1, made out of .", + privs = { worldedit = true }, + func = we_c.safe_region(function(name, params_text) + local target_node, major_radius, minor_radius = parse_params_torus(params_text) + + if not target_node then + worldedit.player_notify(name, "Error: Invalid node name.") + return false + end + if not major_radius or not minor_radius then + worldedit.player_notify(name, "Error: Invalid radius(es).") + return false + end + + local start_time = os.clock() + local replaced = worldedit.torus(worldedit.pos1[name], major_radius, minor_radius, target_node, true) + local time_taken = os.clock() - start_time + + worldedit.player_notify(name, replaced .. " nodes replaced in " .. time_taken .. "s") + minetest.log("action", name .. " used //hollowtorus at " .. worldeditadditions.vector.tostring(worldedit.pos1[name]) .. ", replacing " .. replaced .. " nodes in " .. time_taken .. "s") + end, function(name, params_text) + local target_node, major_radius, minor_radius = parse_params_torus(params_text) + if not target_node or not major_radius or not minor_radius then + worldedit.player_notify(name, "Error: Invalid input '" .. params_text .. "'. Try '/help /hollowtorus' to learn how to use this command.") + return 0 + end + + return math.ceil(2 * math.pi*math.pi * major_radius * minor_radius*minor_radius) + end) +}) diff --git a/worldeditadditions_commands/init.lua b/worldeditadditions_commands/init.lua index 0c0fdac..ab0473f 100644 --- a/worldeditadditions_commands/init.lua +++ b/worldeditadditions_commands/init.lua @@ -5,411 +5,18 @@ -- @license Mozilla Public License, 2.0 -- @author Starbeamrainbowlabs -dofile(minetest.get_modpath("worldeditadditions_commands").."/multi.lua") +worldeditadditions_commands = {} +local we_c = worldeditadditions_commands -local safe_region, check_region, reset_pending = dofile(minetest.get_modpath("worldeditadditions_commands") .. "/safe.lua") +we_c.modpath = minetest.get_modpath("worldeditadditions_commands") +dofile(we_c.modpath.."/multi.lua") --- ███████ ██ ██████ ██████ ██████ ███████ ██ ██ ██ --- ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ --- █████ ██ ██ ██ ██ ██ ██ ██ █████ ██ ██ ██ --- ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ --- ██ ███████ ██████ ██████ ██████ ██ ██ ███████ ███████ +we_c.safe_region, we_c.check_region, we_c.reset_pending + = dofile(we_c.modpath.."/safe.lua") -local function parse_params_floodfill(params_text) - if not params_text then params_text = "" end - local found, _, replace_node, radius = params_text:find("([a-z:_\\-]+)%s+([0-9]+)") - - if found == nil then - found, _, replace_node = params_text:find("([a-z:_\\-]+)") - radius = 20 - end - if found == nil then - replace_node = "default:water_source" - end - radius = tonumber(radius) - - replace_node = worldedit.normalize_nodename(replace_node) - - return replace_node, radius -end - -minetest.register_chatcommand("/floodfill", { - params = "[ []]", - description = "Floods all connected nodes of the same type starting at pos1 with (which defaults to `water_source`), in a sphere with a radius of (which defaults to 20).", - privs = { worldedit = true }, - func = safe_region(function(name, params_text) - local replace_node, radius = parse_params_floodfill(params_text) - - if not replace_node then - worldedit.player_notify(name, "Error: Invalid node name.") - return false - end - - if not worldedit.pos1[name] then - worldedit.player_notify(name, "Error: No pos1 defined.") - return false - end - - local start_time = os.clock() - local nodes_replaced = worldedit.floodfill(worldedit.pos1[name], radius, replace_node) - local time_taken = os.clock() - start_time - - if nodes_replaced == false then - worldedit.player_notify(name, "Error: The search node is the same as the replace node.") - return false - end - - worldedit.player_notify(name, nodes_replaced .. " nodes replaced in " .. time_taken .. "s") - minetest.log("action", name .. " used //floodfill at " .. worldeditadditions.vector.tostring(worldedit.pos1[name]) .. ", replacing " .. nodes_replaced .. " nodes in " .. time_taken .. "s") - end, function(name, params_text) - local replace_node, radius = parse_params_floodfill(params_text) - - if not worldedit.pos1[name] then - return 0 -- The actual command will send the error message to the client - end - -- Volume of a hemisphere - return math.ceil(((4 * math.pi * (tonumber(radius) ^ 3)) / 3) / 2) - end) -}) - - --- ██████ ██ ██ ███████ ██████ ██ █████ ██ ██ --- ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ --- ██ ██ ██ ██ █████ ██████ ██ ███████ ████ --- ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ --- ██████ ████ ███████ ██ ██ ███████ ██ ██ ██ - -minetest.register_chatcommand("/overlay", { - params = "", - description = "Places in the last contiguous air space encountered above the first non-air node. In other words, overlays all top-most nodes in the specified area with .", - privs = { worldedit = true }, - func = safe_region(function(name, params_text) - local target_node = worldedit.normalize_nodename(params_text) - - if not target_node then - worldedit.player_notify(name, "Error: Invalid node name.") - return false - end - - local start_time = os.clock() - local changes = worldedit.overlay(worldedit.pos1[name], worldedit.pos2[name], target_node) - local time_taken = os.clock() - start_time - - worldedit.player_notify(name, changes.updated .. " nodes replaced and " .. changes.skipped_columns .. " columns skipped in " .. time_taken .. "s") - minetest.log("action", name .. " used //overlay at " .. worldeditadditions.vector.tostring(worldedit.pos1[name]) .. ", replacing " .. changes.updated .. " nodes and skipping " .. changes.skipped_columns .. " columns in " .. time_taken .. "s") - end, function(name, params_text) - if not worldedit.normalize_nodename(params_text) then - worldedit.player_notify(name, "Error: Invalid node name '" .. params_text .. "'.") - return 0 - end - - local pos1 = worldedit.pos1[name] - local pos2 = worldedit.pos2[name] - pos1, pos2 = worldedit.sort_pos(pos1, pos2) - - local vol = vector.subtract(pos2, pos1) - - return vol.x*vol.z - end) -}) - - --- ███████ ██ ██ ██ ██████ ███████ ██████ ██ ██████ --- ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ --- █████ ██ ██ ██ ██████ ███████ ██ ██ ██ ██ ██ --- ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ --- ███████ ███████ ███████ ██ ██ ███████ ██████ ██ ██████ - -local function parse_params_ellipsoid(params_text) - local found, _, radius_x, radius_y, radius_z, replace_node = params_text:find("([0-9]+)%s+([0-9]+)%s+([0-9]+)%s+([a-z:_\\-]+)") - - if found == nil then - return nil, nil - end - - local radius = { - x = tonumber(radius_x), - y = tonumber(radius_y), - z = tonumber(radius_z) - } - - replace_node = worldedit.normalize_nodename(replace_node) - - return replace_node, radius -end - -minetest.register_chatcommand("/ellipsoid", { - params = " ", - description = "Creates a 3D ellipsoid with a radius of (rx, ry, rz) at pos1, filled with .", - privs = { worldedit = true }, - func = safe_region(function(name, params_text) - local target_node, radius = parse_params_ellipsoid(params_text) - - if not target_node then - worldedit.player_notify(name, "Error: Invalid node name.") - return false - end - if not radius then - worldedit.player_notify(name, "Error: Invalid radius(es).") - return false - end - - local start_time = os.clock() - local replaced = worldedit.ellipsoid(worldedit.pos1[name], radius, target_node, false) - local time_taken = os.clock() - start_time - - worldedit.player_notify(name, replaced .. " nodes replaced in " .. time_taken .. "s") - minetest.log("action", name .. " used //ellipsoid at " .. worldeditadditions.vector.tostring(worldedit.pos1[name]) .. ", replacing " .. replaced .. " nodes in " .. time_taken .. "s") - end, function(name, params_text) - local target_node, radius = parse_params_ellipsoid(params_text) - if not target_node or not radius then - worldedit.player_notify(name, "Error: Invalid input '" .. params_text .. "'. Try '/help /ellipsoid' to learn how to use this command.") - return 0 - end - - return math.ceil(4/3 * math.pi * radius.x * radius.y * radius.z) - end) -}) - --- TODO: This duplicates a lot of code. Perhaps we can trim it down a bit? -minetest.register_chatcommand("/hollowellipsoid", { - params = " ", - description = "Creates a 3D hollow ellipsoid with a radius of (rx, ry, rz) at pos1, made out of .", - privs = { worldedit = true }, - func = safe_region(function(name, params_text) - local target_node, radius = parse_params_ellipsoid(params_text) - - if not target_node then - worldedit.player_notify(name, "Error: Invalid node name.") - return false - end - if not radius then - worldedit.player_notify(name, "Error: Invalid radius(es).") - return false - end - - local start_time = os.clock() - local replaced = worldedit.ellipsoid(worldedit.pos1[name], radius, target_node, true) - local time_taken = os.clock() - start_time - - worldedit.player_notify(name, replaced .. " nodes replaced in " .. time_taken .. "s") - minetest.log("action", name .. " used //hollowellipsoid at " .. worldeditadditions.vector.tostring(worldedit.pos1[name]) .. ", replacing " .. replaced .. " nodes in " .. time_taken .. "s") - end, function(name, params_text) - local target_node, radius = parse_params_ellipsoid(params_text) - if not target_node or not radius then - worldedit.player_notify(name, "Error: Invalid input '" .. params_text .. "'. Try '/help /hollowellipsoid' to learn how to use this command.") - return 0 - end - - return math.ceil(4/3 * math.pi * radius.x * radius.y * radius.z) - end) -}) - --- ████████ ██████ ██████ ██ ██ ███████ --- ██ ██ ██ ██ ██ ██ ██ ██ --- ██ ██ ██ ██████ ██ ██ ███████ --- ██ ██ ██ ██ ██ ██ ██ ██ --- ██ ██████ ██ ██ ██████ ███████ -local function parse_params_torus(params_text) - local found, _, major_radius, minor_radius, replace_node = params_text:find("([0-9]+)%s+([0-9]+)%s+([a-z:_\\-]+)") - - if found == nil then - return nil, nil - end - - major_radius = tonumber(major_radius) - minor_radius = tonumber(minor_radius) - - replace_node = worldedit.normalize_nodename(replace_node) - - return replace_node, major_radius, minor_radius -end - -minetest.register_chatcommand("/torus", { - params = " ", - description = "Creates a 3D torus with a major radius of and a minor radius of at pos1, filled with .", - privs = { worldedit = true }, - func = safe_region(function(name, params_text) - local target_node, major_radius, minor_radius = parse_params_torus(params_text) - - if not target_node then - worldedit.player_notify(name, "Error: Invalid node name.") - return false - end - if not major_radius or not minor_radius then - worldedit.player_notify(name, "Error: Invalid radius(es).") - return false - end - - local start_time = os.clock() - local replaced = worldedit.torus(worldedit.pos1[name], major_radius, minor_radius, target_node, false) - local time_taken = os.clock() - start_time - - worldedit.player_notify(name, replaced .. " nodes replaced in " .. time_taken .. "s") - minetest.log("action", name .. " used //torus at " .. worldeditadditions.vector.tostring(worldedit.pos1[name]) .. ", replacing " .. replaced .. " nodes in " .. time_taken .. "s") - end, function(name, params_text) - local target_node, major_radius, minor_radius = parse_params_torus(params_text) - if not target_node or not major_radius or not minor_radius then - worldedit.player_notify(name, "Error: Invalid input '" .. params_text .. "'. Try '/help /torus' to learn how to use this command.") - return 0 - end - - return math.ceil(2 * math.pi*math.pi * major_radius * minor_radius*minor_radius) - end) -}) - --- TODO: This duplicates a lot of code. Perhaps we can trim it down a bit? -minetest.register_chatcommand("/hollowtorus", { - params = " ", - description = "Creates a 3D hollow torus with a major radius of and a minor radius of at pos1, made out of .", - privs = { worldedit = true }, - func = safe_region(function(name, params_text) - local target_node, major_radius, minor_radius = parse_params_torus(params_text) - - if not target_node then - worldedit.player_notify(name, "Error: Invalid node name.") - return false - end - if not major_radius or not minor_radius then - worldedit.player_notify(name, "Error: Invalid radius(es).") - return false - end - - local start_time = os.clock() - local replaced = worldedit.torus(worldedit.pos1[name], major_radius, minor_radius, target_node, true) - local time_taken = os.clock() - start_time - - worldedit.player_notify(name, replaced .. " nodes replaced in " .. time_taken .. "s") - minetest.log("action", name .. " used //hollowtorus at " .. worldeditadditions.vector.tostring(worldedit.pos1[name]) .. ", replacing " .. replaced .. " nodes in " .. time_taken .. "s") - end, function(name, params_text) - local target_node, major_radius, minor_radius = parse_params_torus(params_text) - if not target_node or not major_radius or not minor_radius then - worldedit.player_notify(name, "Error: Invalid input '" .. params_text .. "'. Try '/help /hollowtorus' to learn how to use this command.") - return 0 - end - - return math.ceil(2 * math.pi*math.pi * major_radius * minor_radius*minor_radius) - end) -}) - - --- ███ ███ █████ ███████ ███████ --- ████ ████ ██ ██ ███ ██ --- ██ ████ ██ ███████ ███ █████ --- ██ ██ ██ ██ ██ ███ ██ --- ██ ██ ██ ██ ███████ ███████ - -local function parse_params_maze(params_text) - if not params_text then - return nil, nil, nil - end - local found, _, replace_node, seed_text = params_text:find("([a-z:_\\-]+)%s+([0-9]+)") - - local has_seed = true - - if found == nil then - has_seed = false - replace_node = params_text - end - - local seed = tonumber(seed_text) - - replace_node = worldedit.normalize_nodename(replace_node) - - return replace_node, seed, has_seed -end - -minetest.register_chatcommand("/maze", { - params = " []", - description = "Generates a maze covering the currently selected area (must be at least 3x3 on the x,z axes) with replace_node as the walls. Optionally takes a (integer) seed.", - privs = { worldedit = true }, - func = safe_region(function(name, params_text) - local replace_node, seed, has_seed = parse_params_maze(params_text) - - if not replace_node then - worldedit.player_notify(name, "Error: Invalid node name.") - return false - end - if not seed and has_seed then - worldedit.player_notify(name, "Error: Invalid seed.") - return false - end - if not seed then seed = os.time() end - - local start_time = os.clock() - local replaced = worldedit.maze(worldedit.pos1[name], worldedit.pos2[name], replace_node, seed) - local time_taken = os.clock() - start_time - - worldedit.player_notify(name, replaced .. " nodes replaced in " .. time_taken .. "s") - minetest.log("action", name .. " used //maze at " .. worldeditadditions.vector.tostring(worldedit.pos1[name]) .. ", replacing " .. replaced .. " nodes in " .. time_taken .. "s") - end, function(name, params_text) - local replace_node, seed, has_seed = parse_params_maze(params_text) - if not params_text then params_text = "" end - - if not replace_node then - worldedit.player_notify(name, "Error: Invalid input '" .. params_text .. "' (specifically the replace node). Try '/help /maze' to learn how to use this command.") - return 0 - end - if not seed and has_seed then - worldedit.player_notify(name, "Error: Invalid input '" .. params_text .. "' (specifically the seed). Try '/help /maze' to learn how to use this command.") - return 0 - end - - local pos1 = worldedit.pos1[name] - local pos2 = worldedit.pos2[name] - - return (pos2.x - pos1.x) * (pos2.y - pos1.y) * (pos1.z - pos2.z) - end) -}) - - - --- ███ ███ █████ ███████ ███████ ██████ ██████ --- ████ ████ ██ ██ ███ ██ ██ ██ ██ --- ██ ████ ██ ███████ ███ █████ █████ ██ ██ --- ██ ██ ██ ██ ██ ███ ██ ██ ██ ██ --- ██ ██ ██ ██ ███████ ███████ ██████ ██████ - -minetest.register_chatcommand("/maze3d", { - params = " []", - description = "Generates a 3d maze covering the currently selected area (must be at least 3x3x3) with replace_node as the walls. Optionally takes a (integer) seed.", - privs = { worldedit = true }, - func = safe_region(function(name, params_text) - local replace_node, seed, has_seed = parse_params_maze(params_text) - - if not replace_node then - worldedit.player_notify(name, "Error: Invalid node name.") - return false - end - if not seed and has_seed then - worldedit.player_notify(name, "Error: Invalid seed.") - return false - end - if not seed then seed = os.time() end - - local start_time = os.clock() - local replaced = worldedit.maze3d(worldedit.pos1[name], worldedit.pos2[name], replace_node, seed) - local time_taken = os.clock() - start_time - - worldedit.player_notify(name, replaced .. " nodes replaced in " .. time_taken .. "s") - minetest.log("action", name .. " used //maze at " .. worldeditadditions.vector.tostring(worldedit.pos1[name]) .. ", replacing " .. replaced .. " nodes in " .. time_taken .. "s") - end, function(name, params_text) - local replace_node, seed, has_seed = parse_params_maze(params_text) - if not params_text then params_text = "" end - - if not replace_node then - worldedit.player_notify(name, "Error: Invalid input '" .. params_text .. "' (specifically the replace node). Try '/help /maze3d' to learn how to use this command.") - return 0 - end - if not seed and has_seed then - worldedit.player_notify(name, "Error: Invalid input '" .. params_text .. "' (specifically the seed). Try '/help /maze3d' to learn how to use this command.") - return 0 - end - - local pos1 = worldedit.pos1[name] - local pos2 = worldedit.pos2[name] - - return (pos2.x - pos1.x) * (pos2.y - pos1.y) * (pos1.z - pos2.z) - end) -}) +dofile(we_c.modpath.."/commands/floodfill.lua") +dofile(we_c.modpath.."/commands/overlay.lua") +dofile(we_c.modpath.."/commands/ellipsoid.lua") +dofile(we_c.modpath.."/commands/torus.lua") +dofile(we_c.modpath.."/commands/maze.lua")