From 00b9a1f517b24dea2891cb9c66ce54d8109efc72 Mon Sep 17 00:00:00 2001 From: VorTechnix <45538536+VorTechnix@users.noreply.github.com> Date: Wed, 17 Mar 2021 11:36:12 -0700 Subject: [PATCH] srel stable? --- worldeditadditions/utils/selector_helps.lua | 16 ++++++ worldeditadditions/utils/tables.lua | 19 +++++++ .../commands/selectors/srel.lua | 54 +++++++++++++++++++ worldeditadditions_commands/init.lua | 2 +- 4 files changed, 90 insertions(+), 1 deletion(-) create mode 100644 worldeditadditions_commands/commands/selectors/srel.lua diff --git a/worldeditadditions/utils/selector_helps.lua b/worldeditadditions/utils/selector_helps.lua index 28e96fc..fb4c82d 100644 --- a/worldeditadditions/utils/selector_helps.lua +++ b/worldeditadditions/utils/selector_helps.lua @@ -18,5 +18,21 @@ function worldeditadditions.axis_left(axis,sign) else return true, "x", -sign end end +--- Dehumanize Direction: translates up, down, left, right, front, into xyz based on player orientation. +-- @param name string The name of the player to return facing direction of. +-- @param dir string Relative direction to translate. +-- @return Returns axis name and sign multiplier. +function worldeditadditions.dh_dir(name, dir) + local axfac, drfac = worldeditadditions.player_axis2d(name) + local _, axlft, drlft = worldeditadditions.axis_left(axfac,drfac) + if dir:match("front") or dir:match("back") then + return axfac, dir:match("front") and drfac or -drfac + elseif dir:match("left") or dir:match("right") then + return axlft, dir:match("left") and drlft or -drlft + elseif dir:match("up") or dir:match("down") then + return "y", dir == "down" and -1 or 1 + else return false, "\"" .. dir .. "\" not a recognized direction! Try: (up | down | left | right | front | back)" end +end + -- Tests -- /lua print(unpack(worldeditadditions.player_axis2d(myname))) diff --git a/worldeditadditions/utils/tables.lua b/worldeditadditions/utils/tables.lua index c961a06..e20bd63 100644 --- a/worldeditadditions/utils/tables.lua +++ b/worldeditadditions/utils/tables.lua @@ -46,3 +46,22 @@ function worldeditadditions.table_get_last(tbl, count) math.max(0, (#tbl) - (count - 1)) )} end + +--- Returns the key value pairs in a table as a single string +-- @param tbl table input table +-- @param sep string key value seperator +-- @param new_line string key value pair delimiter +-- @return string concatenated table pairs +function worldeditadditions.table_tostring(tbl, sep, new_line) + if type(sep) ~= "string" then sep = ": " end + if type(new_line) ~= "string" then new_line = ", " end + local ret = {} + if type(tbl) ~= "table" then return "Error: input not table!" end + for key,value in pairs(tbl) do + ret:append(key) + ret:append(sep) + ret:append(value) + ret:append(new_line) + end + return ret:concat("") +end diff --git a/worldeditadditions_commands/commands/selectors/srel.lua b/worldeditadditions_commands/commands/selectors/srel.lua new file mode 100644 index 0000000..22150c5 --- /dev/null +++ b/worldeditadditions_commands/commands/selectors/srel.lua @@ -0,0 +1,54 @@ +-- ███████ ██████ ███████ ██ +-- ██ ██ ██ ██ ██ +-- ███████ ██████ █████ ██ +-- ██ ██ ██ ██ ██ +-- ███████ ██ ██ ███████ ███████ +local wea = worldeditadditions +local function parse_with_name(name,args) + local vec, tmp = vector.new(0, 0, 0), {} + local find, _, i = {}, 0, 0 + repeat + _, i, tmp.proc = args:find("([%l%s+-]+%d+)%s*", i) + if tmp.proc:match("[xyz]") then + tmp.ax = tmp.proc:match("[xyz]") + tmp.dir = tonumber(tmp.proc:match("[+-]?%d+")) * (tmp.proc:match("-%l+") and -1 or 1) + else + tmp.ax, _ = wea.dh_dir(name, tmp.proc:match("%l+")) + if not tmp.ax then return false, _ end + tmp.dir = tonumber(tmp.proc:match("[+-]?%d+")) * (tmp.proc:match("-%l+") and -1 or 1) * _ + end + vec[tmp.ax] = tmp.dir + until not args:find("([%l%s+-]+%d+)%s*", i) + return true, vec +end +worldedit.register_command("srel", { + params = " [ [ ]]", + description = "Set WorldEdit region position 2 at set distances along 3 axes.", + privs = { worldedit = true }, + require_pos = 0, + parse = function(params_text) + if params_text:match("([%l%s+-]+%d+)") then return true, params_text + else return false, "No acceptable params found" end + end, + func = function(name, params_text) + local ret = "" + local _, vec = parse_with_name(name,params_text) + if not _ then return false, vec end + + if not worldedit.pos1[name] then + local pos = vector.add(minetest.get_player_by_name(name):get_pos(), vector.new(0.5,-0.5,0.5)) + wea.vector.floor(pos) + worldedit.pos1[name] = pos + worldedit.mark_pos1(name) + ret = "position 1 set to " .. minetest.pos_to_string(pos) .. ", " + end + + local p2 = vector.add(vec,worldedit.pos1[name]) + worldedit.pos2[name] = p2 + worldedit.mark_pos2(name) + return true, ret .. "position 2 set to " .. minetest.pos_to_string(p2) + end, +}) + +-- Tests +-- //srel front 5 left 3 y 2 diff --git a/worldeditadditions_commands/init.lua b/worldeditadditions_commands/init.lua index 09bf0fd..f880a9e 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/srel.lua") +dofile(we_c.modpath.."/commands/selectors/srel.lua") dofile(we_c.modpath.."/commands/selectors/scentre.lua") dofile(we_c.modpath.."/commands/selectors/scloud.lua") dofile(we_c.modpath.."/commands/selectors/scol.lua")