From a671f742e9b38e47f57b38fe7c7d32f5bb9e685e Mon Sep 17 00:00:00 2001 From: VorTechnix <45538536+VorTechnix@users.noreply.github.com> Date: Tue, 9 Mar 2021 11:05:03 -0800 Subject: [PATCH] scol and srect refactor --- worldeditadditions/utils/numbers.lua | 21 +++------ worldeditadditions/utils/selector_helps.lua | 18 +++++-- .../commands/selectors/scol.lua | 47 +++++++++++++++++++ .../commands/selectors/srect.lua | 46 +++++++----------- worldeditadditions_commands/init.lua | 2 +- 5 files changed, 86 insertions(+), 48 deletions(-) create mode 100644 worldeditadditions_commands/commands/selectors/scol.lua diff --git a/worldeditadditions/utils/numbers.lua b/worldeditadditions/utils/numbers.lua index f287199..84d34f2 100644 --- a/worldeditadditions/utils/numbers.lua +++ b/worldeditadditions/utils/numbers.lua @@ -42,21 +42,12 @@ function worldeditadditions.eta(existing_times, done_count, total_count) end --- Returns the sign (+ or -) at the beginning of a string if present. --- @param str string Input string. --- @param type string The type of value to return. Valid values: "string" (default), "int" --- @return string|int Returns the sign string or signed multiplier (1|-1). -function worldeditadditions.getsign(str, type) - if not type then type = "string" end - if not (type == "string" or type == "int") then - return false, "Error: Unknown type '"..type.."'." - end - if str:sub(1, 1) == "-" then - if type == "int" then return true, -1 - else return true, "-" end - else - if type == "int" then return true, 1 - else return true, "+" end - end +-- @param src string|int Input string. +-- @return string|int Returns the signed multiplier (1|-1). +function worldeditadditions.getsign(src) + if type(src) == "number" then return src < 0 and -1 or 1 + elseif type(src) ~= "string" then return 1 + else return src:match('-') and -1 or 1 end end -- For Testing: diff --git a/worldeditadditions/utils/selector_helps.lua b/worldeditadditions/utils/selector_helps.lua index 60d31ba..28e96fc 100644 --- a/worldeditadditions/utils/selector_helps.lua +++ b/worldeditadditions/utils/selector_helps.lua @@ -1,11 +1,21 @@ -- Returns the player's facing direction on the horizontal axes only. -- @param name string The name of the player to return facing direction of. --- @return table Returns axis name and sign multiplyer. +-- @return Returns axis name and sign multiplier. function worldeditadditions.player_axis2d(name) -- minetest.get_player_by_name("singleplayer"): - local dir = math.floor(minetest.get_player_by_name(name):get_look_horizontal() / math.pi * 2 + 0.5) % 4 - local crdnl = { {1,"z"},{-1,"x"},{-1,"z"},{1,"x"} } - return crdnl[dir+1] + local dir = minetest.get_player_by_name(name):get_look_dir() + local x, z= math.abs(dir.x), math.abs(dir.z) + if x > z then return "x", dir.x > 0 and 1 or -1 + else return "z", dir.z > 0 and 1 or -1 end +end +-- Returns the axis and sign of the axis to the left of the input axis. +-- @param axis string x or z. +-- @param sign int Sign multiplier. +-- @return Returns axis name and sign multiplier. +function worldeditadditions.axis_left(axis,sign) + if not axis:match("[xz]") then return false, "Error: Not a horizontal axis!" + elseif axis == "x" then return true, "z", sign + else return true, "x", -sign end end -- Tests diff --git a/worldeditadditions_commands/commands/selectors/scol.lua b/worldeditadditions_commands/commands/selectors/scol.lua new file mode 100644 index 0000000..c03ba69 --- /dev/null +++ b/worldeditadditions_commands/commands/selectors/scol.lua @@ -0,0 +1,47 @@ +-- ███████ ██████ ██████ ██ +-- ██ ██ ██ ██ ██ +-- ███████ ██ ██ ██ ██ +-- ██ ██ ██ ██ ██ +-- ███████ ██████ ██████ ███████ +worldedit.register_command("scol", { + params = "[] ", + description = "Set WorldEdit region position 2 at a set distance along 1 axis.", + privs = {worldedit=true}, + require_pos = 1, + parse = function(params_text) + local wea = worldeditadditions + local find = wea.split(params_text, "%s", false) + local ax1, len = find[1], find[table.maxn(find)] + + -- If ax1 is bad set to player facing dir + if ax1 == len or not ax1:match('[xyz]') then ax1 = "get" + else ax1 = { wea.getsign(ax1), ax1:gsub('[^xyz]', ''):sub(1, 1) } end + + len = tonumber(len) + -- If len == nill cancel the operation + if len == nil then + return false, "No length specified." + end + + return true, ax1, len + end, + func = function(name, axis1, len) + if axis1 == "get" then + ax1, dir = worldedit.player_axis(name) + axis1 = {dir,ax1} + end + + local p2 = vector.new(worldedit.pos1[name]) + p2[axis1[2]] = p2[axis1[2]] + tonumber(len) * axis1[1] + + worldedit.pos2[name] = p2 + worldedit.mark_pos2(name) + return true, "position 2 set to " .. minetest.pos_to_string(p2) + end, +}) + +-- Tests +-- /multi //fp set1 -63 19 -20 //scol 5 + +-- lua print(worldedit.player_axis(myname)) +-- tonumber(('y1'):gsub('[xyz]',''):sub(1,2)) diff --git a/worldeditadditions_commands/commands/selectors/srect.lua b/worldeditadditions_commands/commands/selectors/srect.lua index 1ddc602..e477d40 100644 --- a/worldeditadditions_commands/commands/selectors/srect.lua +++ b/worldeditadditions_commands/commands/selectors/srect.lua @@ -9,39 +9,28 @@ worldedit.register_command("srect", { privs = { worldedit = true }, require_pos = 1, parse = function(params_text) - local wea = worldeditadditions + local wea, vec, tmp = worldeditadditions, vector.new(0, 0, 0), {} local find = wea.split(params_text, "%s", false) - local ax1, ax2, len = find[1], find[2], find[table.maxn(find)] + local ax1, ax2 = (tostring(find[1]):match('[xyz]') or "g"):sub(1,1), (tostring(find[2]):match('[xyz]') or "y"):sub(1,1) + local sn1, sn2, len = wea.getsign(find[1]), wea.getsign(find[2]), find[table.maxn(find)] - -- If ax1 is bad set to player facing dir - if ax1 == len or not ax1:match('[xyz]') then ax1 = "get" - else - local success, value = wea.getsign(ax1, "int") - if not success then return success, value - else ax1 = { value, ax1:gsub('[^xyz]', ''):sub(1, 1) } end - end - -- If ax2 is bad set to +y - if not ax2 or ax2 == len or not ax2:match('[xyz]') then ax2 = "y" end - local success, value = wea.getsign(ax2, "int") - if not success then return success, value end - ax2 = { value, ax2:gsub('[^xyz]', ''):sub(1, 1) } - - len = tonumber(len) + tmp.len = tonumber(len) -- If len == nill cancel the operation - if len == nil then - return false, "No length specified." + if tmp.len == nil then return false, "No length specified." end + -- If ax1 is bad send "get" order + if ax1 == "g" then tmp.get = true + else vec[ax1] = sn1 * tmp.len end + vec[ax2] = sn2 * tmp.len + + return true, vec, tmp + end, + func = function(name, vec, tmp) + if tmp.get then + local ax, dir = worldeditadditions.player_axis2d(name) + vec[ax] = tmp.len * dir end - return true, ax1, ax2, len - end, - func = function(name, axis1, axis2, len) - if axis1 == "get" then axis1 = worldeditadditions.player_axis2d(name) end - - local p2 = vector.new(worldedit.pos1[name]) - - p2[axis1[2]] = p2[axis1[2]] + tonumber(len) * axis1[1] - p2[axis2[2]] = p2[axis2[2]] + tonumber(len) * axis2[1] - + p2 = vector.add(vec,worldedit.pos1[name]) worldedit.pos2[name] = p2 worldedit.mark_pos2(name) return true, "position 2 set to " .. minetest.pos_to_string(p2) @@ -56,3 +45,4 @@ worldedit.register_command("srect", { -- /multi //fp set1 -63 19 -20 //srect -z 5 -- /multi //fp set1 -63 19 -20 //srect a -x 5 -- /multi //fp set1 -63 19 -20 //srect -x -a 5 +-- lua vec = vector.new(15,-12,17); vec["len"] = 5; vec.get = true; vec2 = vector.add(vector.new(1,1,1),vec) print(vec2.x,vec2.y,vec2.z,vec2.len) diff --git a/worldeditadditions_commands/init.lua b/worldeditadditions_commands/init.lua index 75b85cf..ff1b526 100644 --- a/worldeditadditions_commands/init.lua +++ b/worldeditadditions_commands/init.lua @@ -41,7 +41,7 @@ dofile(we_c.modpath.."/commands/meta/many.lua") dofile(we_c.modpath.."/commands/meta/subdivide.lua") dofile(we_c.modpath.."/commands/meta/ellipsoidapply.lua") --- dofile(we_c.modpath.."/commands/selectors/scol.lua") +dofile(we_c.modpath.."/commands/selectors/scol.lua") dofile(we_c.modpath.."/commands/selectors/srect.lua") -- dofile(we_c.modpath.."/commands/selectors/scube.lua") dofile(we_c.modpath.."/commands/selectors/sstack.lua")