From fe7a552c4042c6fcea328edf76456c7e7b763a47 Mon Sep 17 00:00:00 2001 From: sfan5 Date: Wed, 24 Sep 2025 21:54:54 +0200 Subject: [PATCH] Move and refactor cuboid functions --- worldedit/init.lua | 1 - .../cuboid_funcs.lua | 237 ++++++++---------- worldedit_commands/init.lua | 4 +- 3 files changed, 101 insertions(+), 141 deletions(-) rename worldedit/cuboid.lua => worldedit_commands/cuboid_funcs.lua (51%) diff --git a/worldedit/init.lua b/worldedit/init.lua index bbd3c7a..0a54832 100644 --- a/worldedit/init.lua +++ b/worldedit/init.lua @@ -32,7 +32,6 @@ load_module(path .. "/visualization.lua") load_module(path .. "/serialization.lua") load_module(path .. "/code.lua") load_module(path .. "/compatibility.lua") -load_module(path .. "/cuboid.lua") if minetest.settings:get_bool("log_mods") then diff --git a/worldedit/cuboid.lua b/worldedit_commands/cuboid_funcs.lua similarity index 51% rename from worldedit/cuboid.lua rename to worldedit_commands/cuboid_funcs.lua index a467046..5cf6f81 100644 --- a/worldedit/cuboid.lua +++ b/worldedit_commands/cuboid_funcs.lua @@ -1,114 +1,21 @@ --- FIXME: worldedit.pos1/2 is a concept of worldedit_commands only, so these functions here --- are actually misplaced. - --- Expands or contracts the cuboid in all axes by amount (positive or negative) -worldedit.cuboid_volumetric_expand = function(name, amount) - local pos1 = worldedit.pos1[name] - local pos2 = worldedit.pos2[name] - - if pos1 == nil or pos2 == nil then - return false, "Undefined cuboid" - end - - local delta1 = vector.new() - local delta2 = vector.new() - local delta_dir1 - local delta_dir2 - - delta1 = vector.add(delta1, amount) - delta2 = vector.add(delta2, amount) - delta_dir1, delta_dir2 = worldedit.get_expansion_directions(pos1, pos2) - delta1 = vector.multiply(delta1, delta_dir1) - delta2 = vector.multiply(delta2, delta_dir2) - worldedit.pos1[name] = vector.add(pos1, delta1) - worldedit.pos2[name] = vector.add(pos2, delta2) - - return true -end - - --- Expands or contracts the cuboid in a single axis by amount (positive or negative) -worldedit.cuboid_linear_expand = function(name, axis, direction, amount) - local pos1 = worldedit.pos1[name] - local pos2 = worldedit.pos2[name] - - if pos1 == nil or pos2 == nil then - return false, "undefined cuboid" - end - - if direction ~= 1 and direction ~= -1 then - return false, "invalid marker" - end - - local marker = worldedit.marker_get_closest_to_axis(name, axis, direction) - local deltavect = vector.new() - - if axis == 'x' then - deltavect.x = amount * direction - elseif axis == 'y' then - deltavect.y = amount * direction - elseif axis == 'z' then - deltavect.z = amount * direction - else - return false, "invalid axis" - end - - worldedit.marker_move(name, marker, deltavect) - return true -end - - --- Shifts the cuboid by '+-amount' in axis 'axis' -worldedit.cuboid_shift = function(name, axis, amount) - local pos1 = worldedit.pos1[name] - local pos2 = worldedit.pos2[name] - - if pos1 == nil or pos2 == nil then - return false, "undefined cuboid" - end - - assert(not rawequal(pos1, pos2)) -- vectors must not alias - - if axis == 'x' then - worldedit.pos1[name].x = pos1.x + amount - worldedit.pos2[name].x = pos2.x + amount - elseif axis == 'y' then - worldedit.pos1[name].y = pos1.y + amount - worldedit.pos2[name].y = pos2.y + amount - elseif axis == 'z' then - worldedit.pos1[name].z = pos1.z + amount - worldedit.pos2[name].z = pos2.z + amount - else - return false, "invalid axis" - end - - return true -end - - -- Moves the location of a single marker by adding deltavector -worldedit.marker_move = function(name, marker, deltavector) - if marker ~= 1 and marker ~= 2 then - return false - end - +local function marker_move(name, marker, deltavector) if marker == 1 then local pos = worldedit.pos1[name] worldedit.pos1[name] = vector.add(deltavector, pos) - else + elseif marker == 2 then local pos = worldedit.pos2[name] worldedit.pos2[name] = vector.add(deltavector, pos) + else + assert(false) end - return true end -- Returns two vectors with the directions for volumetric expansion -worldedit.get_expansion_directions = function(mark1, mark2) - if mark1 == nil or mark2 == nil then - return - end +local function get_expansion_directions(mark1, mark2) + assert(mark1 and mark2) local dir1 = vector.new() local dir2 = vector.new() @@ -137,55 +44,109 @@ worldedit.get_expansion_directions = function(mark1, mark2) end --- Return the marker that is closest to the player -worldedit.marker_get_closest_to_player = function(name) - local player = assert(minetest.get_player_by_name(name)) - local playerpos = player:get_pos() - local dist1 = vector.distance(playerpos, worldedit.pos1[name]) - local dist2 = vector.distance(playerpos, worldedit.pos2[name]) +-- Returns the closest marker to the specified axis and direction +local function marker_get_closest_to_axis(name, axis, direction) + assert(direction == 1 or direction == -1) + local pos1 = worldedit.pos1[name] + local pos2 = worldedit.pos2[name] - if dist1 < dist2 then - return 1 + if axis == "x" then + if pos1.x * direction > pos2.x * direction then + return 1 + else + return 2 + end + elseif axis == "y" then + if pos1.y * direction > pos2.y * direction then + return 1 + else + return 2 + end + elseif axis == "z" then + if pos1.z * direction > pos2.z * direction then + return 1 + else + return 2 + end else - return 2 + assert(false) end end --- Returns the closest marker to the specified axis and direction -worldedit.marker_get_closest_to_axis = function(name, axis, direction) - local pos1 = vector.new() - local pos2 = vector.new() +-- Expands or contracts the cuboid in all axes by amount (positive or negative) +worldedit.cuboid_volumetric_expand = function(name, amount) + local pos1 = worldedit.pos1[name] + local pos2 = worldedit.pos2[name] + + if pos1 == nil or pos2 == nil then + return false, "Undefined cuboid" + end + + local delta1 = vector.new(amount, amount, amount) + local delta2 = vector.new(amount, amount, amount) + local delta_dir1, delta_dir2 = get_expansion_directions(pos1, pos2) + delta1 = vector.multiply(delta1, delta_dir1) + delta2 = vector.multiply(delta2, delta_dir2) + worldedit.pos1[name] = vector.add(pos1, delta1) + worldedit.pos2[name] = vector.add(pos2, delta2) + + return true +end + + +-- Expands or contracts the cuboid in a single axis by amount (positive or negative) +worldedit.cuboid_linear_expand = function(name, axis, direction, amount) + local pos1 = worldedit.pos1[name] + local pos2 = worldedit.pos2[name] + + if pos1 == nil or pos2 == nil then + return false, "undefined cuboid" + end if direction ~= 1 and direction ~= -1 then - return nil + return false, "invalid marker" end - if axis == 'x' then - pos1.x = worldedit.pos1[name].x * direction - pos2.x = worldedit.pos2[name].x * direction - if pos1.x > pos2.x then - return 1 - else - return 2 - end - elseif axis == 'y' then - pos1.y = worldedit.pos1[name].y * direction - pos2.y = worldedit.pos2[name].y * direction - if pos1.y > pos2.y then - return 1 - else - return 2 - end - elseif axis == 'z' then - pos1.z = worldedit.pos1[name].z * direction - pos2.z = worldedit.pos2[name].z * direction - if pos1.z > pos2.z then - return 1 - else - return 2 - end + local marker = marker_get_closest_to_axis(name, axis, direction) + local deltavect = vector.new() + + if axis == "x" then + deltavect.x = amount * direction + elseif axis == "y" then + deltavect.y = amount * direction + elseif axis == "z" then + deltavect.z = amount * direction else - return nil + return false, "invalid axis" end + + marker_move(name, marker, deltavect) + return true +end + + +-- Shifts the cuboid by '+-amount' in axis 'axis' +worldedit.cuboid_shift = function(name, axis, amount) + local pos1 = worldedit.pos1[name] + local pos2 = worldedit.pos2[name] + + if pos1 == nil or pos2 == nil then + return false, "undefined cuboid" + end + + local delta = vector.new() + if axis == "x" then + delta.x = amount + elseif axis == "y" then + delta.y = amount + elseif axis == "z" then + delta.z = amount + else + return false, "invalid axis" + end + + worldedit.pos1[name] = vector.add(pos1, delta) + worldedit.pos2[name] = vector.add(pos2, delta) + return true end diff --git a/worldedit_commands/init.lua b/worldedit_commands/init.lua index 94fa84c..30145b5 100644 --- a/worldedit_commands/init.lua +++ b/worldedit_commands/init.lua @@ -395,8 +395,8 @@ worldedit.register_command("reset", { do local modpath = minetest.get_modpath("worldedit_commands") for _, name in ipairs({ - "code", "cuboid", "manipulations", "marker", "nodename", "primitives", - "region", "schematics", "transform", "wand" + "code", "cuboid_funcs", "cuboid", "manipulations", "marker", "nodename", + "primitives", "region", "schematics", "transform", "wand" }) do dofile(modpath .. "/" .. name .. ".lua") end