mirror of
https://github.com/sbrl/Minetest-WorldEditAdditions.git
synced 2024-11-22 23:42:59 +00:00
axes parser re-refactor tests passed
This commit is contained in:
parent
d6c452a221
commit
61bfab240a
2 changed files with 49 additions and 40 deletions
|
@ -96,6 +96,15 @@ describe("parse_axes", function()
|
||||||
assert.are.same(Vector3.new(40, 77, 99), maxv)
|
assert.are.same(Vector3.new(40, 77, 99), maxv)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
it("should infer that directions before a value are connected to that value", function()
|
||||||
|
local minv, maxv = parse_axes({
|
||||||
|
"xy", "-x", "5"
|
||||||
|
}, facing_dirs.z_neg)
|
||||||
|
assert.is.truthy(minv)
|
||||||
|
assert.are.same(Vector3.new(-5, 0, 0), minv)
|
||||||
|
assert.are.same(Vector3.new(5, 5, 0), maxv)
|
||||||
|
end)
|
||||||
|
|
||||||
it("should return 2 0,0,0 vectors if no input", function()
|
it("should return 2 0,0,0 vectors if no input", function()
|
||||||
local minv, maxv = parse_axes({
|
local minv, maxv = parse_axes({
|
||||||
-- No input
|
-- No input
|
||||||
|
|
|
@ -23,7 +23,7 @@ else
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
--- Unified Axis Keywords banks
|
--- Unified Axis Syntax banks
|
||||||
local keywords = {
|
local keywords = {
|
||||||
-- Compass keywords
|
-- Compass keywords
|
||||||
compass = {
|
compass = {
|
||||||
|
@ -36,7 +36,9 @@ local keywords = {
|
||||||
["w"] = "-x", ["west"] = "-x",
|
["w"] = "-x", ["west"] = "-x",
|
||||||
["-w"] = "x", ["-west"] = "x",
|
["-w"] = "x", ["-west"] = "x",
|
||||||
["u"] = "y", ["up"] = "y",
|
["u"] = "y", ["up"] = "y",
|
||||||
|
["-u"] = "-y", ["-up"] = "-y",
|
||||||
["d"] = "-y", ["down"] = "-y",
|
["d"] = "-y", ["down"] = "-y",
|
||||||
|
["-d"] = "y", ["-down"] = "y",
|
||||||
},
|
},
|
||||||
|
|
||||||
-- Direction keywords
|
-- Direction keywords
|
||||||
|
@ -90,9 +92,9 @@ function parse.num(str)
|
||||||
else return false end
|
else return false end
|
||||||
end
|
end
|
||||||
|
|
||||||
--- Checks if a string is a valid Unified Axis Keyword. (Supports axis clumping)
|
--- Checks if a string is a valid Unified Axis Syntax. (Supports axis clumping)
|
||||||
-- @param: str: String: Keyword instance to parse
|
-- @param: str: String: Keyword to parse
|
||||||
-- @returns: Key Instance: returns keyword type, processed keyword content and signed number (or nil)
|
-- @returns: keyword type, processed keyword content and signed number (or nil)
|
||||||
function parse.keyword(str)
|
function parse.keyword(str)
|
||||||
if type(str) ~= "string" then
|
if type(str) ~= "string" then
|
||||||
return "err", "Error: \""..tostring(str).."\" is not a string.", 404
|
return "err", "Error: \""..tostring(str).."\" is not a string.", 404
|
||||||
|
@ -135,68 +137,65 @@ end
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
--- Converts Unified Axis Keyword table into Vector3 instances.
|
--- Converts Unified Axis Syntax table into Vector3 instances.
|
||||||
-- @param: tbl: Table: Keyword table to parse
|
-- @param: tbl: Table: Keyword table to parse
|
||||||
-- @param: facing: Table: Output from worldeditadditions_core.player_dir(name)
|
-- @param: facing: Table: Output from worldeditadditions_core.player_dir(name)
|
||||||
-- @param: sum: Bool | String | nil: Return a single vector by summing the 2 output vectors together
|
-- @param: sum: Bool | String | nil: Return a single vector by summing the 2 output vectors together
|
||||||
-- @returns: Vector3, [Vector3]: returns min, max Vector3 or sum Vector3 (if @param: sum ~= nil)
|
-- @returns: Vector3, [Vector3]: returns min, max Vector3 or sum Vector3 (if @param: sum ~= nil)
|
||||||
-- if error: @returns: false, String: error message
|
-- if error: @returns: false, String: error message
|
||||||
function parse.keytable(tbl, facing, sum)
|
function parse.keytable(tbl, facing, sum)
|
||||||
local min, max = Vector3.new(), Vector3.new()
|
local min, max, mir= Vector3.new(), Vector3.new(), false
|
||||||
local expected = 1
|
local neg, pos, v0 = Vector3.new(), Vector3.new(), Vector3.new()
|
||||||
local tmp = {axes = {}, num = 0, sign = 1, mirror = false}
|
|
||||||
|
|
||||||
--- Processes a number and adds it to the min and max vectors.
|
local function update(num) -- Update "min" and "max"
|
||||||
-- @param num The number to process.
|
if num < 0 then
|
||||||
-- @param axes The axes to apply the number to.
|
min, max = min:add(pos:mul(num)), max:add(neg:mul(num))
|
||||||
-- @param sign The sign of the number.
|
|
||||||
local function parseNumber(num, axes, sign)
|
|
||||||
if axes.rev then parseNumber(num, axes.rev, -sign) end
|
|
||||||
if num * sign >= 0 then
|
|
||||||
max = max:add(parse.vectorize(axes, num, sign))
|
|
||||||
else
|
else
|
||||||
min = min:add(parse.vectorize(axes, num, sign))
|
min, max = min:add(neg:mul(num)), max:add(pos:mul(num))
|
||||||
|
end
|
||||||
|
neg, pos = v0:clone(), v0:clone()
|
||||||
|
end
|
||||||
|
|
||||||
|
local function udir(axes, sign) -- Update "neg" and "pos"
|
||||||
|
if axes.rev then udir(axes.rev, -sign) end
|
||||||
|
for _, v in ipairs(axes) do
|
||||||
|
if sign < 0 then neg[v] = -1
|
||||||
|
else pos[v] = 1 end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
for i, v in ipairs(tbl) do
|
for i,v in ipairs(tbl) do
|
||||||
if v:sub(1, 1) == "+" then
|
if v:sub(1, 1) == "+" then
|
||||||
v = v:sub(2)
|
v = v:sub(2)
|
||||||
end
|
end
|
||||||
|
|
||||||
tmp.num = parse.num(v)
|
local num = parse.num(v)
|
||||||
|
|
||||||
if expected == 1 then
|
-- If we have a dimension add it to output
|
||||||
if tmp.num then
|
-- Else gather direction statements
|
||||||
parseNumber(tmp.num, {"x", "y", "z", rev={"x", "y", "z"}}, tmp.sign)
|
if num then
|
||||||
else
|
-- Check for direction vectors
|
||||||
local key_type, key_entry, key_sign = parse.keyword(v)
|
if neg == v0 and pos == v0 then
|
||||||
|
neg, pos = v0:add(-1), v0:add(1)
|
||||||
|
end
|
||||||
|
update(num)
|
||||||
|
else
|
||||||
|
local key_type, key_entry, key_sign = parse.keyword(v)
|
||||||
|
|
||||||
if key_type == "axis" then
|
if key_type == "axis" then
|
||||||
tmp.axes = key_entry
|
udir(key_entry, key_sign)
|
||||||
tmp.sign = key_sign
|
|
||||||
elseif key_type == "dir" then
|
elseif key_type == "dir" then
|
||||||
tmp.axes = {facing[key_entry].axis}
|
udir({facing[key_entry].axis},
|
||||||
tmp.sign = facing[key_entry].sign * key_sign
|
facing[key_entry].sign * key_sign)
|
||||||
elseif key_type == "rev" then
|
elseif key_type == "rev" then
|
||||||
tmp.mirror = true
|
mir = true
|
||||||
else
|
else
|
||||||
return false, key_entry
|
return false, key_entry
|
||||||
end
|
end
|
||||||
|
|
||||||
expected = 2
|
|
||||||
end
|
|
||||||
else
|
|
||||||
if tmp.num then
|
|
||||||
parseNumber(tmp.num, tmp.axes, tmp.sign)
|
|
||||||
expected = 1
|
|
||||||
else
|
|
||||||
return false, "Error: Expected number after \""..tostring(tbl[i-1]).."\". Got \""..tostring(v).."\"."
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
if tmp.mirror and not sum then
|
if mir and not sum then
|
||||||
max = max:max(min:abs())
|
max = max:max(min:abs())
|
||||||
min = max:multiply(-1)
|
min = max:multiply(-1)
|
||||||
end
|
end
|
||||||
|
@ -209,6 +208,7 @@ function parse.keytable(tbl, facing, sum)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
keyword = parse.keyword,
|
keyword = parse.keyword,
|
||||||
keytable = parse.keytable,
|
keytable = parse.keytable,
|
||||||
|
|
Loading…
Reference in a new issue