diff options
Diffstat (limited to 'builtin/common')
-rw-r--r-- | builtin/common/misc_helpers.lua | 18 | ||||
-rw-r--r-- | builtin/common/tests/vector_spec.lua | 19 | ||||
-rw-r--r-- | builtin/common/vector.lua | 22 |
3 files changed, 56 insertions, 3 deletions
diff --git a/builtin/common/misc_helpers.lua b/builtin/common/misc_helpers.lua index b86d68f5f..324d83b07 100644 --- a/builtin/common/misc_helpers.lua +++ b/builtin/common/misc_helpers.lua @@ -244,6 +244,15 @@ function math.factorial(x) return v end + +function math.round(x) + if x >= 0 then + return math.floor(x + 0.5) + end + return math.ceil(x - 0.5) +end + + function core.formspec_escape(text) if text ~= nil then text = string.gsub(text,"\\","\\\\") @@ -785,3 +794,12 @@ end function core.is_nan(number) return number ~= number end + +function core.inventorycube(img1, img2, img3) + img2 = img2 or img1 + img3 = img3 or img1 + return "[inventorycube" + .. "{" .. img1:gsub("%^", "&") + .. "{" .. img2:gsub("%^", "&") + .. "{" .. img3:gsub("%^", "&") +end diff --git a/builtin/common/tests/vector_spec.lua b/builtin/common/tests/vector_spec.lua index 0f287363a..104c656e9 100644 --- a/builtin/common/tests/vector_spec.lua +++ b/builtin/common/tests/vector_spec.lua @@ -48,6 +48,25 @@ describe("vector", function() assert.same({ x = 41, y = 52, z = 63 }, vector.offset(vector.new(1, 2, 3), 40, 50, 60)) end) + it("to_string()", function() + local v = vector.new(1, 2, 3.14) + assert.same("(1, 2, 3.14)", vector.to_string(v)) + end) + + it("from_string()", function() + local v = vector.new(1, 2, 3.14) + assert.same({v, 13}, {vector.from_string("(1, 2, 3.14)")}) + assert.same({v, 12}, {vector.from_string("(1,2 ,3.14)")}) + assert.same({v, 12}, {vector.from_string("(1,2,3.14,)")}) + assert.same({v, 11}, {vector.from_string("(1 2 3.14)")}) + assert.same({v, 15}, {vector.from_string("( 1, 2, 3.14 )")}) + assert.same({v, 15}, {vector.from_string(" ( 1, 2, 3.14) ")}) + assert.same({vector.new(), 8}, {vector.from_string("(0,0,0) ( 1, 2, 3.14) ")}) + assert.same({v, 22}, {vector.from_string("(0,0,0) ( 1, 2, 3.14) ", 8)}) + assert.same({v, 22}, {vector.from_string("(0,0,0) ( 1, 2, 3.14) ", 9)}) + assert.same(nil, vector.from_string("nothing")) + end) + -- This function is needed because of floating point imprecision. local function almost_equal(a, b) if type(a) == "number" then diff --git a/builtin/common/vector.lua b/builtin/common/vector.lua index d6437deda..2ef8fc617 100644 --- a/builtin/common/vector.lua +++ b/builtin/common/vector.lua @@ -12,6 +12,22 @@ function vector.new(a, b, c) return {x=0, y=0, z=0} end +function vector.from_string(s, init) + local x, y, z, np = string.match(s, "^%s*%(%s*([^%s,]+)%s*[,%s]%s*([^%s,]+)%s*[,%s]" .. + "%s*([^%s,]+)%s*[,%s]?%s*%)()", init) + x = tonumber(x) + y = tonumber(y) + z = tonumber(z) + if not (x and y and z) then + return nil + end + return {x = x, y = y, z = z}, np +end + +function vector.to_string(v) + return string.format("(%g, %g, %g)", v.x, v.y, v.z) +end + function vector.equals(a, b) return a.x == b.x and a.y == b.y and @@ -41,9 +57,9 @@ end function vector.round(v) return { - x = math.floor(v.x + 0.5), - y = math.floor(v.y + 0.5), - z = math.floor(v.z + 0.5) + x = math.round(v.x), + y = math.round(v.y), + z = math.round(v.z) } end |