From 104fe97be3a5a82b9c17ca78a0ebbd286f5fe8ed Mon Sep 17 00:00:00 2001 From: VorTechnix <45538536+VorTechnix@users.noreply.github.com> Date: Tue, 16 Nov 2021 11:34:58 -0800 Subject: [PATCH 01/13] Update selection.lua --- .../lib/selection/selection.lua | 34 +++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/worldeditadditions/lib/selection/selection.lua b/worldeditadditions/lib/selection/selection.lua index e32fc08..18b3701 100644 --- a/worldeditadditions/lib/selection/selection.lua +++ b/worldeditadditions/lib/selection/selection.lua @@ -3,7 +3,7 @@ -- ███████ █████ ██ █████ ██ ██ ██ ██ ██ ██ ██ ██ -- ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ -- ███████ ███████ ███████ ███████ ██████ ██ ██ ██████ ██ ████ - +local v3 = worldeditadditions.Vector3 ---Selection helpers and modifiers local selection = {} @@ -13,6 +13,7 @@ local selection = {} -- @param pos vector The position to include. function selection.add_point(name, pos) if pos ~= nil then + local is_new = not worldedit.pos1[name] and not worldedit.pos2[name] -- print("[set_pos1]", name, "("..pos.x..", "..pos.y..", "..pos.z..")") if not worldedit.pos1[name] then worldedit.pos1[name] = vector.new(pos) end if not worldedit.pos2[name] then worldedit.pos2[name] = vector.new(pos) end @@ -28,7 +29,16 @@ function selection.add_point(name, pos) local volume_difference = volume_after - volume_before worldedit.marker_update(name) - worldedit.player_notify(name, "Expanded region by "..volume_difference.." nodes") + -- print("DEBUG volume_before", volume_before, "volume_after", volume_after) + if is_new then + local msg = "Created new region of "..volume_after.." node" + if volume_after ~= 1 then msg = msg.."s" end + worldedit.player_notify(name, msg) + else + local msg = "Expanded region by "..volume_difference.." node" + if volume_difference ~= 1 then msg = msg.."s" end + worldedit.player_notify(name, msg) + end else worldedit.player_notify(name, "Error: Too far away (try raising your maxdist with //farwand maxdist )") -- print("[set_pos1]", name, "nil") @@ -65,4 +75,24 @@ function selection.check_dir(str) return (str == "front" or str == "back" or str == "left" or str == "right" or str == "up" or str == "down") end +--- Makes two vectors from the given positions and expands or contracts them +-- (based on mode) by a third provided vector. +-- @param mode string "grow" | "shrink". +-- @param pos1 vector worldedit pos1. +-- @param pos2 vector worldedit pos2. +-- @param vec vector The modifying vector. +-- @return Vector3,Vector3 New vectors for worldedit positions. +function selection.resize(mode, pos1, pos2, vec) + local pos1, pos2 = v3.sort(pos1, pos2) + local vmin = v3.min(vec,v3.new()):abs() + local vmax = v3.max(vec,v3.new()) + if mode == "grow" then + return pos1 - vmin, pos2 + vMax + elseif mode == "shrink" then + return pos1 + vmin, pos2 - vMax + else + error("Resize Error: invalid mode \""..tostring(mode).."\".") + end +end + return selection From a6b4d047b8b207148b8e90a59a7b1941e08f9a44 Mon Sep 17 00:00:00 2001 From: VorTechnix <45538536+VorTechnix@users.noreply.github.com> Date: Fri, 20 May 2022 19:27:47 -0700 Subject: [PATCH 02/13] axes_parser implemented (not added to init) --- .../utils/parse/axes_parser.lua | 204 ++++++++++++++++++ .../utils/parse/key_instance.lua | 69 ++++++ 2 files changed, 273 insertions(+) create mode 100644 worldeditadditions/utils/parse/axes_parser.lua create mode 100644 worldeditadditions/utils/parse/key_instance.lua diff --git a/worldeditadditions/utils/parse/axes_parser.lua b/worldeditadditions/utils/parse/axes_parser.lua new file mode 100644 index 0000000..409e2cd --- /dev/null +++ b/worldeditadditions/utils/parse/axes_parser.lua @@ -0,0 +1,204 @@ +-- ██████ █████ ██████ ███████ ███████ █████ ██ ██ ███████ ███████ +-- ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ +-- ██████ ███████ ██████ ███████ █████ ███████ ███ █████ ███████ +-- ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ +-- ██ ██ ██ ██ ██ ███████ ███████ ██ ██ ██ ██ ███████ ███████ + +-- Error codes: https://kinsta.com/blog/http-status-codes/ + +--- FOR TESTING --- +local function unpack(tbl) + if table.unpack then + return table.unpack(tbl) + else return unpack(tbl) end +end +--------------- + +local Vector3 +if worldeditadditions then + local wea = worldeditadditions + Vector3 = dofile(wea.modpath.."/utils/vector3.lua") +else + Vector3 = require("worldeditadditions.utils.vector3") +end + +local key_instance +if worldeditadditions then + local wea = worldeditadditions + key_instance = dofile(wea.modpath.."/utils/parse/key_instance.lua") +else + key_instance = require("worldeditadditions.key_instance") +end + +--- Unified Axis Keywords banks +local keywords = {} +-- Direction keywords +keywords.dir = { + ["?"] = "front", f = "front", + facing = "front", front = "front", + b = "back", back = "back", + behind = "back", rear = "back", + l = "left", left = "left", + r = "right", right = "right", + u = "up", up = "up", + d = "down", down = "down", +} +-- Mirroring keywords +keywords.mirroring = { + sym = true, symmetrical = true, + mirror = true, mir = true, + rev = true, reverse = true, + ["true"] = true +} + +--- Initialize parser function container +local parse = {} + +--- Processes an axis declaration into ordered xyz format. (Supports axis clumping) +-- For example, "zzy" would become "yz" +-- @param: str: String: Axis declaration to parse +-- @returns: Table|Bool: axis | axes | false +function parse.axes(str) + local axes, ret = {"x","y","z"}, {} + for i,v in ipairs(axes) do + if str:match(v) then table.insert(ret,v) end + end + if #ret > 0 and str:match("^[xyz]+$") then + return ret + elseif str == "h" then + return {"x", "z"} + elseif str == "v" then + return {"y"} + else return false end +end + +--- Processes an number from a string. +-- @param: str: String: string to parse +-- @returns: Number|Bool: processed number | false +function parse.num(str) + str = tostring(str) -- To prevent meltdown if str isn't a string + local num = str:match("^-?%d+%.?%d*$") + if num then + return tonumber(num) + else return false end +end + +--- Checks if a string is a valid Unified Axis Keyword. (Supports axis clumping) +-- @param: str: String: Keyword instance to parse +-- @returns: Key Instance: returns keyword type, processed keyword content and signed number (or nil) +function parse.keyword(str) + if type(str) ~= "string" then + return key_instance.new("err", "Error: \""..tostring(str).."\" is not a string.", 404) + end + local sign = 1 + if str:sub(1,1) == "-" then + sign = -1 + str = str:sub(2) + end + + local axes = parse.axes(str) + if axes then + return key_instance.new("axis", axes, sign) + elseif keywords.dir[str] then + return key_instance.new("dir", keywords.dir[str], sign) + elseif keywords.mirroring[str] then + return key_instance.new("rev", "mirroring") + else return key_instance.new("err", "Error: \""..str.."\" is not a valid axis, direction or keyword.", 422) + end +end + +--- Creates a vector with a length of (@param: value * @param: sign) +-- on each axis in @param: axes. +-- @param: axes: Table: List of axes to set +-- @param: value: Number: length of to set axes +-- @param: sign: Number: sign multiplier for axes +-- @returns: Vector3: processed vector +function parse.vectorize(axes,value,sign) + local ret = Vector3.new() + for i,v in ipairs(axes) do + ret[v] = value * sign + end + return ret +end + +--- Converts Unified Axis Keyword table into Vector3 instances. +-- @param: tbl: Table: Keyword table to parse +-- @param: facing: Table: Output from worldeditadditions.player_dir(name) +-- @param: sum: Bool: Return a single vector by summing the 2 output vectors together +-- @returns: Vector3, [Vector3]: returns min, max Vector3s or sum Vector3 (if @param: sum ~= nil) +-- if error: @returns: false, String: error message +function parse.keytable(tbl, facing, sum) + local min, max = Vector3.new(), Vector3.new() + local expected, tmp = 1, {axes = {}, num = 0, sign = 1, mirror = false} + function tmp:reset() self.axis, self.sign = "", 1 end + + for i,v in ipairs(tbl) do + if v:sub(1,1) == "+" then v = v:sub(2) end + tmp.num = parse.num(v) + if expected == 1 then -- Mode 1 of state machine + -- State machine expects string + if tmp.num then + -- If this is a number treat as all axes and add to appropriate vector + if tmp.num * tmp.sign >= 0 then + max = max:add(parse.vectorize({"x","y","z"}, tmp.num, tmp.sign)) + else + min = min:add(parse.vectorize({"x","y","z"}, tmp.num, tmp.sign)) + end + -- We are still looking for axes so the state machine should remain + -- in Mode 1 for the next iteration + else + -- Else parse.keyword + local key_inst = parse.keyword(v) + -- Stop if error and return message + if key_inst:is_error() then return false, key_inst.entry end + -- Check key type and process further + if key_inst.type == "axis" then + tmp.axes = key_inst.entry + elseif key_inst.type == "dir" then + tmp.axes = {facing[key_inst.entry].axis} + tmp.sign = facing[key_inst.entry].sign + elseif key_inst.type == "rev" then + tmp.mirror = true + else + -- If key type is error or unknown throw error and stop + if key_inst.type == "err" then + return false, key_inst.entry + else + return false, "Error: Unknown Key Instance type \"".. + tostring(key_inst.type).."\". Contact the devs!" + end + end + expected = 2 -- Toggle state machine to expect number (Mode 2) + end + + else -- Mode 2 of state machine + -- State machine expects number + if tmp.num then + -- If this is a number process num and add to appropriate vector + if tmp.num * tmp.sign >= 0 then + max = max:add(parse.vectorize(tmp.axes, tmp.num, tmp.sign)) + else + min = min:add(parse.vectorize(tmp.axes, tmp.num, tmp.sign)) + end + expected = 1 -- Toggle state machine to expect string (Mode 1) + else + -- Else throw an error and stop everything + return false, "Error: Expected number after \""..tostring(tbl[i-1]).. + "\", got \""..tostring(v).."\"." + end + end -- End of state machine + + end -- End of main for loop + + -- Handle Mirroring + if tmp.mirror then + max = max:max(min:abs()) + min = max:multiply(-1) + end + + if sum then return min:add(max) + else return min, max end + +end + +return parse diff --git a/worldeditadditions/utils/parse/key_instance.lua b/worldeditadditions/utils/parse/key_instance.lua new file mode 100644 index 0000000..fc78a86 --- /dev/null +++ b/worldeditadditions/utils/parse/key_instance.lua @@ -0,0 +1,69 @@ +--- A container for transmitting axis, sign pairs within parsing functions. +-- @class +local key_instance = {} +key_instance.__index = key_instance +key_instance.__name = "Key Instance" + +--- Creates a new Key Instance. +-- @param: type: String: Key type (axis, direction, mirroring or error). +-- @param: entry: String: The main content of the key. +-- @param: sign: Int: The signed multiplier of the key (if any). +-- @return: Key Instance: The new Key Instance. +function key_instance.new(type,entry,sign) + if type == "error" then type = "err" end + local tbl = {type = type, entry = entry} + if sign and sign ~= 0 then + if type == "err" then tbl.code = sign + else tbl.sign = sign end + end + return setmetatable(tbl, key_instance) +end + +--- Checks if Key Instance "entry" field is a table. +-- @param: tbl: Key Instance: The Key Instance to check. +-- @return: Bool: Returns true if Key Instance has a non 0 sign value. +function key_instance.entry_table(a) + if type(a.entry) == "table" then + return true + else return false end +end + +--- Checks if Key Instance has a signed multiplier. +-- @param: tbl: Key Instance: The Key Instance to check. +-- @return: Bool: Returns true if Key Instance has a non 0 sign value. +function key_instance.has_sign(a) + if not a.sign or a.sign == 0 then + return false + else return true end +end + +--- Checks if Key Instance is an error. +-- @param: tbl: Key Instance: The Key Instance to check. +-- @return: Bool: Returns true if Key Instance is an error. +function key_instance.is_error(a) + if a.type == "err" then return true + else return false end +end + +function key_instance.__tostring(a) + local function enquote(take) + if type(take) == "string" then + return '"'..take..'"' + else return tostring(take) end + end + local ret = "{type = "..enquote(a.type)..", entry = " + if type(a.entry) == "table" and #a.entry <= 3 then + ret = ret.."{" + for _i,v in ipairs(a.entry) do + ret = ret..enquote(v)..", " + end + ret = ret:sub(1,-3).."}" + else ret = ret..enquote(a.entry) end + + if a:is_error() and a.code then ret = ret..", code = "..a.code.."}" + elseif a:has_sign() then ret = ret..", sign = "..a.sign.."}" + else ret = ret.."}" end + return ret +end + +return key_instance From a0982c70f06cad5b3918185464d34f57d3229425 Mon Sep 17 00:00:00 2001 From: VorTechnix <45538536+VorTechnix@users.noreply.github.com> Date: Fri, 20 May 2022 19:29:01 -0700 Subject: [PATCH 03/13] check_dir update (unification) --- worldeditadditions/lib/selection/selection.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/worldeditadditions/lib/selection/selection.lua b/worldeditadditions/lib/selection/selection.lua index a1e5ed0..c4fdc0c 100644 --- a/worldeditadditions/lib/selection/selection.lua +++ b/worldeditadditions/lib/selection/selection.lua @@ -71,7 +71,7 @@ end -- @param str string String to check (be sure to remove any + or -). -- @return bool If string is a valid dir then true. function selection.check_dir(str) - return (str == "front" or str == "back" or str == "left" or str == "right" or str == "up" or str == "down") + return (str == "facing" or str == "front" or str == "back" or str == "left" or str == "right" or str == "up" or str == "down") end return selection From 2cddccdca4ebfee5748f2d677e8eee6c6c7bd70c Mon Sep 17 00:00:00 2001 From: VorTechnix <45538536+VorTechnix@users.noreply.github.com> Date: Mon, 23 May 2022 14:58:27 -0700 Subject: [PATCH 04/13] working (tested) parser implementation --- worldeditadditions/utils/parse/axes_parser.lua | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/worldeditadditions/utils/parse/axes_parser.lua b/worldeditadditions/utils/parse/axes_parser.lua index 409e2cd..1e68c2a 100644 --- a/worldeditadditions/utils/parse/axes_parser.lua +++ b/worldeditadditions/utils/parse/axes_parser.lua @@ -58,7 +58,7 @@ local parse = {} -- For example, "zzy" would become "yz" -- @param: str: String: Axis declaration to parse -- @returns: Table|Bool: axis | axes | false -function parse.axes(str) +function parse.axis(str) local axes, ret = {"x","y","z"}, {} for i,v in ipairs(axes) do if str:match(v) then table.insert(ret,v) end @@ -96,7 +96,7 @@ function parse.keyword(str) str = str:sub(2) end - local axes = parse.axes(str) + local axes = parse.axis(str) if axes then return key_instance.new("axis", axes, sign) elseif keywords.dir[str] then @@ -191,7 +191,7 @@ function parse.keytable(tbl, facing, sum) end -- End of main for loop -- Handle Mirroring - if tmp.mirror then + if tmp.mirror and not sum then max = max:max(min:abs()) min = max:multiply(-1) end From 23140467d20e58c1a0ce7e7d38b851e6ff87f990 Mon Sep 17 00:00:00 2001 From: VorTechnix <45538536+VorTechnix@users.noreply.github.com> Date: Mon, 23 May 2022 14:58:45 -0700 Subject: [PATCH 05/13] new parser connected --- worldeditadditions/utils/parse/init.lua | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/worldeditadditions/utils/parse/init.lua b/worldeditadditions/utils/parse/init.lua index 2308fe8..fede457 100644 --- a/worldeditadditions/utils/parse/init.lua +++ b/worldeditadditions/utils/parse/init.lua @@ -5,11 +5,16 @@ -- ██ ██ ██ ██ ██ ███████ ███████ local axes = dofile(worldeditadditions.modpath.."/utils/parse/axes.lua") +local parse = dofile(worldeditadditions.modpath.."/utils/parse/axes.lua") +local key_instance = dofile(worldeditadditions.modpath.."/utils/parse/key_instance.lua") -worldeditadditions.parse = { - axes = axes.parse_axes, - axis_name = axes.parse_axis_name -} +worldeditadditions.key_instance = key_instance +worldeditadditions.parse = parse + +-- Old parse functions (depricated). +-- Use parse.keytable or parse.keyword instead +worldeditadditions.parse.axes = axes.parse_axes +worldeditadditions.parse.axis_name = axes.parse_axis_name dofile(worldeditadditions.modpath.."/utils/parse/chance.lua") dofile(worldeditadditions.modpath.."/utils/parse/map.lua") From e17b2cce4e1b565debe815c7d48a0e9cab1bd40d Mon Sep 17 00:00:00 2001 From: VorTechnix <45538536+VorTechnix@users.noreply.github.com> Date: Mon, 23 May 2022 15:02:59 -0700 Subject: [PATCH 06/13] small correction --- worldeditadditions/utils/parse/init.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/worldeditadditions/utils/parse/init.lua b/worldeditadditions/utils/parse/init.lua index fede457..356ccc4 100644 --- a/worldeditadditions/utils/parse/init.lua +++ b/worldeditadditions/utils/parse/init.lua @@ -11,7 +11,7 @@ local key_instance = dofile(worldeditadditions.modpath.."/utils/parse/key_instan worldeditadditions.key_instance = key_instance worldeditadditions.parse = parse --- Old parse functions (depricated). +-- Old parse functions (marked for deprecation). -- Use parse.keytable or parse.keyword instead worldeditadditions.parse.axes = axes.parse_axes worldeditadditions.parse.axis_name = axes.parse_axis_name From 1cef968731d8400c4975c5ad0bd3b7013560e24b Mon Sep 17 00:00:00 2001 From: VorTechnix <45538536+VorTechnix@users.noreply.github.com> Date: Mon, 23 May 2022 17:06:23 -0700 Subject: [PATCH 07/13] Key Instance type checking --- .../utils/parse/key_instance.lua | 37 +++++++++++++++---- 1 file changed, 29 insertions(+), 8 deletions(-) diff --git a/worldeditadditions/utils/parse/key_instance.lua b/worldeditadditions/utils/parse/key_instance.lua index fc78a86..1cec51d 100644 --- a/worldeditadditions/utils/parse/key_instance.lua +++ b/worldeditadditions/utils/parse/key_instance.lua @@ -1,16 +1,42 @@ ---- A container for transmitting axis, sign pairs within parsing functions. +--- A container for transmitting (axis table, sign) or (dir, sign) pairs +-- and other data within parsing functions. -- @class local key_instance = {} key_instance.__index = key_instance key_instance.__name = "Key Instance" +-- Allowed values for "type" field +local types = { + err = true, rev = true, + axis = true, dir = true, + replace = { + error = "err", + axes = "axis", + }, +} + +-- Simple function for putting stuff in quotes +local function enquote(take) + if type(take) == "string" then + return '"'..take..'"' + else return tostring(take) end +end + --- Creates a new Key Instance. --- @param: type: String: Key type (axis, direction, mirroring or error). +-- This is a table with a "type" string, an entry string or table +-- and an optional signed integer (or code number in the case of errors) +-- @param: type: String: Key type (axis, dir(ection), rev (mirroring) or error). -- @param: entry: String: The main content of the key. -- @param: sign: Int: The signed multiplier of the key (if any). -- @return: Key Instance: The new Key Instance. function key_instance.new(type,entry,sign) - if type == "error" then type = "err" end + if types.replace[type] then + type = types.replace[type] + elseif not types[type] then + return key_instance.new("err", + "Key Instance internal error: Invalid type "..enquote(type)..".", + 500) + end local tbl = {type = type, entry = entry} if sign and sign ~= 0 then if type == "err" then tbl.code = sign @@ -46,11 +72,6 @@ function key_instance.is_error(a) end function key_instance.__tostring(a) - local function enquote(take) - if type(take) == "string" then - return '"'..take..'"' - else return tostring(take) end - end local ret = "{type = "..enquote(a.type)..", entry = " if type(a.entry) == "table" and #a.entry <= 3 then ret = ret.."{" From 3196a590f5d1b9af64069b184b5f4c9f689d0900 Mon Sep 17 00:00:00 2001 From: VorTechnix <45538536+VorTechnix@users.noreply.github.com> Date: Mon, 23 May 2022 17:08:42 -0700 Subject: [PATCH 08/13] changed: functions returned (and unexposed key_instance class) --- .../utils/parse/axes_parser.lua | 44 +++++++++++-------- worldeditadditions/utils/parse/init.lua | 21 +++++---- 2 files changed, 37 insertions(+), 28 deletions(-) diff --git a/worldeditadditions/utils/parse/axes_parser.lua b/worldeditadditions/utils/parse/axes_parser.lua index 1e68c2a..1a3295e 100644 --- a/worldeditadditions/utils/parse/axes_parser.lua +++ b/worldeditadditions/utils/parse/axes_parser.lua @@ -31,24 +31,26 @@ else end --- Unified Axis Keywords banks -local keywords = {} --- Direction keywords -keywords.dir = { - ["?"] = "front", f = "front", - facing = "front", front = "front", - b = "back", back = "back", - behind = "back", rear = "back", - l = "left", left = "left", - r = "right", right = "right", - u = "up", up = "up", - d = "down", down = "down", -} --- Mirroring keywords -keywords.mirroring = { - sym = true, symmetrical = true, - mirror = true, mir = true, - rev = true, reverse = true, - ["true"] = true +local keywords = { + -- Direction keywords + keywords.dir = { + ["?"] = "front", f = "front", + facing = "front", front = "front", + b = "back", back = "back", + behind = "back", rear = "back", + l = "left", left = "left", + r = "right", right = "right", + u = "up", up = "up", + d = "down", down = "down", + }, + + -- Mirroring keywords + keywords.mirroring = { + sym = true, symmetrical = true, + mirror = true, mir = true, + rev = true, reverse = true, + ["true"] = true + }, } --- Initialize parser function container @@ -201,4 +203,8 @@ function parse.keytable(tbl, facing, sum) end -return parse +return { + keyword = parse.keyword, + keytable = parse.keytable, + number = parse.num, +} diff --git a/worldeditadditions/utils/parse/init.lua b/worldeditadditions/utils/parse/init.lua index 356ccc4..ff0a0c7 100644 --- a/worldeditadditions/utils/parse/init.lua +++ b/worldeditadditions/utils/parse/init.lua @@ -4,17 +4,20 @@ -- ██ ██ ██ ██ ██ ██ ██ -- ██ ██ ██ ██ ██ ███████ ███████ +-- Unified Axes Keyword Parser +local uak_parse = dofile(worldeditadditions.modpath.."/utils/parse/axes_parser.lua") +-- Old axis parsing functions local axes = dofile(worldeditadditions.modpath.."/utils/parse/axes.lua") -local parse = dofile(worldeditadditions.modpath.."/utils/parse/axes.lua") -local key_instance = dofile(worldeditadditions.modpath.."/utils/parse/key_instance.lua") -worldeditadditions.key_instance = key_instance -worldeditadditions.parse = parse - --- Old parse functions (marked for deprecation). --- Use parse.keytable or parse.keyword instead -worldeditadditions.parse.axes = axes.parse_axes -worldeditadditions.parse.axis_name = axes.parse_axis_name +worldeditadditions.parse = { + keyword = uak_parse.keyword, + keytable = uak_parse.keytable, + number = uak_parse.num, + -- Old parse functions (marked for deprecation). + -- Use parse.keytable or parse.keyword instead + axes = axes.parse_axes, + axis_name = axes.parse_axis_name, +} dofile(worldeditadditions.modpath.."/utils/parse/chance.lua") dofile(worldeditadditions.modpath.."/utils/parse/map.lua") From a8655373a2b20d188c3e67df6152ea80b5198d31 Mon Sep 17 00:00:00 2001 From: VorTechnix <45538536+VorTechnix@users.noreply.github.com> Date: Mon, 23 May 2022 17:11:26 -0700 Subject: [PATCH 09/13] fixed typo --- worldeditadditions/utils/parse/axes_parser.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/worldeditadditions/utils/parse/axes_parser.lua b/worldeditadditions/utils/parse/axes_parser.lua index 1a3295e..e1dde17 100644 --- a/worldeditadditions/utils/parse/axes_parser.lua +++ b/worldeditadditions/utils/parse/axes_parser.lua @@ -33,7 +33,7 @@ end --- Unified Axis Keywords banks local keywords = { -- Direction keywords - keywords.dir = { + dir = { ["?"] = "front", f = "front", facing = "front", front = "front", b = "back", back = "back", @@ -45,7 +45,7 @@ local keywords = { }, -- Mirroring keywords - keywords.mirroring = { + mirroring = { sym = true, symmetrical = true, mirror = true, mir = true, rev = true, reverse = true, From 6ed65074b8a4d4bf7453edd37afac4135f89c784 Mon Sep 17 00:00:00 2001 From: VorTechnix <45538536+VorTechnix@users.noreply.github.com> Date: Mon, 23 May 2022 18:04:34 -0700 Subject: [PATCH 10/13] unexposed number parser --- worldeditadditions/utils/parse/axes_parser.lua | 1 - worldeditadditions/utils/parse/init.lua | 1 - 2 files changed, 2 deletions(-) diff --git a/worldeditadditions/utils/parse/axes_parser.lua b/worldeditadditions/utils/parse/axes_parser.lua index e1dde17..9df45ce 100644 --- a/worldeditadditions/utils/parse/axes_parser.lua +++ b/worldeditadditions/utils/parse/axes_parser.lua @@ -206,5 +206,4 @@ end return { keyword = parse.keyword, keytable = parse.keytable, - number = parse.num, } diff --git a/worldeditadditions/utils/parse/init.lua b/worldeditadditions/utils/parse/init.lua index ff0a0c7..fae8bfd 100644 --- a/worldeditadditions/utils/parse/init.lua +++ b/worldeditadditions/utils/parse/init.lua @@ -12,7 +12,6 @@ local axes = dofile(worldeditadditions.modpath.."/utils/parse/axes.lua") worldeditadditions.parse = { keyword = uak_parse.keyword, keytable = uak_parse.keytable, - number = uak_parse.num, -- Old parse functions (marked for deprecation). -- Use parse.keytable or parse.keyword instead axes = axes.parse_axes, From 021fdac284d383ebdb8b914bd083198c29c0b492 Mon Sep 17 00:00:00 2001 From: VorTechnix <45538536+VorTechnix@users.noreply.github.com> Date: Mon, 23 May 2022 18:49:24 -0700 Subject: [PATCH 11/13] variable refactor --- worldeditadditions/utils/parse/init.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/worldeditadditions/utils/parse/init.lua b/worldeditadditions/utils/parse/init.lua index fae8bfd..49c8f44 100644 --- a/worldeditadditions/utils/parse/init.lua +++ b/worldeditadditions/utils/parse/init.lua @@ -10,8 +10,8 @@ local uak_parse = dofile(worldeditadditions.modpath.."/utils/parse/axes_parser.l local axes = dofile(worldeditadditions.modpath.."/utils/parse/axes.lua") worldeditadditions.parse = { - keyword = uak_parse.keyword, - keytable = uak_parse.keytable, + direction_keyword = uak_parse.keyword, + directions = uak_parse.keytable, -- Old parse functions (marked for deprecation). -- Use parse.keytable or parse.keyword instead axes = axes.parse_axes, From 53600789d870c875e17b05ee4607a41bc9fb0dec Mon Sep 17 00:00:00 2001 From: VorTechnix <45538536+VorTechnix@users.noreply.github.com> Date: Sun, 29 May 2022 16:23:59 -0700 Subject: [PATCH 12/13] added compass directions, fixed tmp.sign bug --- .../utils/parse/axes_parser.lua | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/worldeditadditions/utils/parse/axes_parser.lua b/worldeditadditions/utils/parse/axes_parser.lua index 9df45ce..a96c13a 100644 --- a/worldeditadditions/utils/parse/axes_parser.lua +++ b/worldeditadditions/utils/parse/axes_parser.lua @@ -27,11 +27,23 @@ if worldeditadditions then local wea = worldeditadditions key_instance = dofile(wea.modpath.."/utils/parse/key_instance.lua") else - key_instance = require("worldeditadditions.key_instance") + key_instance = require("worldeditadditions.utils.parse.key_instance") end --- Unified Axis Keywords banks local keywords = { + -- Compass keywords + compass = { + n = "z", north = "z", + ["-n"] = "-z", ["-north"] = "-z", + s = "-z", south = "-z", + ["-s"] = "z", ["-south"] = "z", + e = "x", east = "x", + ["-e"] = "-x", ["-east"] = "-x", + w = "-x", west = "-x", + ["-w"] = "x", ["-west"] = "x", + }, + -- Direction keywords dir = { ["?"] = "front", f = "front", @@ -91,6 +103,8 @@ end function parse.keyword(str) if type(str) ~= "string" then return key_instance.new("err", "Error: \""..tostring(str).."\" is not a string.", 404) + elseif keywords.compass[str] then + str = keywords.compass[str] end local sign = 1 if str:sub(1,1) == "-" then @@ -156,9 +170,10 @@ function parse.keytable(tbl, facing, sum) -- Check key type and process further if key_inst.type == "axis" then tmp.axes = key_inst.entry + tmp.sign = key_inst.sign elseif key_inst.type == "dir" then tmp.axes = {facing[key_inst.entry].axis} - tmp.sign = facing[key_inst.entry].sign + tmp.sign = facing[key_inst.entry].sign * key_inst.sign elseif key_inst.type == "rev" then tmp.mirror = true else From 74ca84fcee3e6beba77bed3e5b64bdd588538505 Mon Sep 17 00:00:00 2001 From: VorTechnix <45538536+VorTechnix@users.noreply.github.com> Date: Sun, 29 May 2022 17:49:01 -0700 Subject: [PATCH 13/13] Create axes_parser.test.lua --- .tests/parse/axes/axes_parser.test.lua | 158 +++++++++++++++++++++++++ 1 file changed, 158 insertions(+) create mode 100644 .tests/parse/axes/axes_parser.test.lua diff --git a/.tests/parse/axes/axes_parser.test.lua b/.tests/parse/axes/axes_parser.test.lua new file mode 100644 index 0000000..527000b --- /dev/null +++ b/.tests/parse/axes/axes_parser.test.lua @@ -0,0 +1,158 @@ +local Vector3 = require("worldeditadditions.utils.vector3") + +local facing_dirs = dofile("./.tests/parse/axes/include_facing_dirs.lua") + +local parse = require("worldeditadditions.utils.parse.axes_parser") +local parse_axes = parse.keytable + + +describe("parse_axes", function() + + -- Basic tests + it("should work on single horizontal axes", function() + local minv, maxv = parse_axes({ + "x", "3", + "-z", "10", + }, facing_dirs.x_pos) + assert.is.truthy(minv) + assert.are.same(Vector3.new(0, 0, -10), minv) + assert.are.same(Vector3.new(3, 0, 0), maxv) + end) + + it("should handle axis clumps and orphan (universal) values", function() + local minv, maxv = parse_axes({ + "xz", "-3", + "10", + }, facing_dirs.x_pos) + assert.is.truthy(minv) + assert.are.same(Vector3.new(-3, 0, -3), minv) + assert.are.same(Vector3.new(10, 10, 10), maxv) + end) + + it("should work on directions and their abriviations", function() + local minv, maxv = parse_axes({ + "l", "3", -- +z + "-r", "-3", -- +z + "b", "-10", -- -x + }, facing_dirs.x_pos) + assert.is.truthy(minv) + assert.are.same(Vector3.new(0, 0, -3), minv) + assert.are.same(Vector3.new(10, 0, 3), maxv) + end) + + it("should work with compass directions and their abriviations", function() + local minv, maxv = parse_axes({ + "n", "3", -- +z + "south", "3", -- -z + "-west", "10", -- +x + }, facing_dirs.x_pos) + assert.is.truthy(minv) + assert.are.same(Vector3.new(0, 0, -3), minv) + assert.are.same(Vector3.new(10, 0, 3), maxv) + end) + + it("should work with ?", function() + local minv, maxv = parse_axes({ + "?", "3", -- -z + }, facing_dirs.z_neg) + assert.is.truthy(minv) + assert.are.same(Vector3.new(0, 0, -3), minv) + assert.are.same(Vector3.new(0, 0, 0), maxv) + end) + + it("should work with complex relative / absolute combinations", function() + local minv, maxv = parse_axes({ + "f", "3", -- +x + "left", "10", -- +z + "y", "77", + "x", "30", + "back", "99", + }, facing_dirs.x_pos) + assert.is.truthy(minv) + assert.are.same(Vector3.new(-99, 0, 0), minv) + assert.are.same(Vector3.new(33, 77, 10), maxv) + end) + + it("should work with complex relative / absolute combinations with negative facing_dirs", function() + local minv, maxv = parse_axes({ + "f", "3", -- -z + "l", "10", -- +x + "y", "77", + "x", "30", + "b", "99", -- +z + }, facing_dirs.z_neg) + assert.is.truthy(minv) + assert.are.same(Vector3.new(0, 0, -3), minv) + assert.are.same(Vector3.new(40, 77, 99), maxv) + end) + + it("should return 2 0,0,0 vectors if no input", function() + local minv, maxv = parse_axes({ + -- No input + }, facing_dirs.z_neg) + assert.is.truthy(minv) + assert.are.same(Vector3.new(0, 0, 0), minv) + assert.are.same(Vector3.new(0, 0, 0), maxv) + end) + + -- Options tests + it("should mirror the max values of the two vectors if mirroring keyword is present", function() + local minv, maxv = parse_axes({ + "x", "3", + "f", "-5", -- +x + "z", "-10", + "mir", + }, facing_dirs.x_pos) + assert.is.truthy(minv) + assert.are.same(Vector3.new(-5, 0, -10), minv) + assert.are.same(Vector3.new(5, 0, 10), maxv) + end) + + it("should return a single vector if 'sum' input is truthy", function() + local minv, maxv = parse_axes({ + "x", "3", + "z", "-10", + }, facing_dirs.x_pos,"sum") + assert.is.truthy(minv) + assert.are.same(Vector3.new(3, 0, -10), minv) + assert.are.same(nil, maxv) + end) + + it("should dissable mirroring if 'sum' input is truthy", function() + local minv, maxv = parse_axes({ + "x", "3", + "f", "-5", -- +x + "z", "-10", + "sym", + }, facing_dirs.x_pos,"sum") + assert.is.truthy(minv) + assert.are.same(Vector3.new(-2, 0, -10), minv) + assert.are.same(nil, maxv) + end) + + -- Error tests + it("should return error if bad axis/dir", function() + local minv, maxv = parse_axes({ + "f", "3", -- +x + "lift", "10", -- Invalid axis + "y", "77", + "x", "30", + "back", "99", -- -x + }, facing_dirs.x_pos) + assert.are.same(false, minv) + assert.are.same("string", type(maxv)) + end) + + it("should return error if bad value", function() + local minv, maxv = parse_axes({ + "f", "3", -- +x + "left", "10", -- +z + "y", "!Q", -- Invalid value + "x", "30", + "back", "99", + }, facing_dirs.x_pos) + assert.are.same(false, minv) + assert.are.same("string", type(maxv)) + end) + +end)