aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDS <vorunbekannt75@web.de>2021-09-10 23:16:16 +0200
committerGitHub <noreply@github.com>2021-09-10 23:16:16 +0200
commit2cefe51d3b9bc4f3ae18854e171a06ea83e9cb25 (patch)
treec80c7a13df16eb1a8db02dad5c39ddffb71c206a
parentbbfae0cc673d3abdc21224c53e09b209ee4688a2 (diff)
downloadminetest-2cefe51d3b9bc4f3ae18854e171a06ea83e9cb25.tar.gz
minetest-2cefe51d3b9bc4f3ae18854e171a06ea83e9cb25.tar.bz2
minetest-2cefe51d3b9bc4f3ae18854e171a06ea83e9cb25.zip
Split vector.new into 3 constructors
-rw-r--r--builtin/common/tests/vector_spec.lua51
-rw-r--r--builtin/common/vector.lua22
-rw-r--r--doc/lua_api.txt11
3 files changed, 57 insertions, 27 deletions
diff --git a/builtin/common/tests/vector_spec.lua b/builtin/common/tests/vector_spec.lua
index 9ebe69056..2a50e2889 100644
--- a/builtin/common/tests/vector_spec.lua
+++ b/builtin/common/tests/vector_spec.lua
@@ -31,6 +31,21 @@ describe("vector", function()
end)
end)
+ it("zero()", function()
+ assert.same({x = 0, y = 0, z = 0}, vector.zero())
+ assert.same(vector.new(), vector.zero())
+ assert.equal(vector.new(), vector.zero())
+ assert.is_true(vector.check(vector.zero()))
+ end)
+
+ it("copy()", function()
+ local v = vector.new(1, 2, 3)
+ assert.same(v, vector.copy(v))
+ assert.same(vector.new(v), vector.copy(v))
+ assert.equal(vector.new(v), vector.copy(v))
+ assert.is_true(vector.check(vector.copy(v)))
+ end)
+
it("indexes", function()
local some_vector = vector.new(24, 42, 13)
assert.equal(24, some_vector[1])
@@ -114,25 +129,25 @@ describe("vector", function()
end)
it("equals()", function()
- local function assertE(a, b)
- assert.is_true(vector.equals(a, b))
- end
- local function assertNE(a, b)
- assert.is_false(vector.equals(a, b))
- end
+ local function assertE(a, b)
+ assert.is_true(vector.equals(a, b))
+ end
+ local function assertNE(a, b)
+ assert.is_false(vector.equals(a, b))
+ end
- assertE({x = 0, y = 0, z = 0}, {x = 0, y = 0, z = 0})
- assertE({x = -1, y = 0, z = 1}, {x = -1, y = 0, z = 1})
- assertE({x = -1, y = 0, z = 1}, vector.new(-1, 0, 1))
- local a = {x = 2, y = 4, z = -10}
- assertE(a, a)
- assertNE({x = -1, y = 0, z = 1}, a)
-
- assert.equal(vector.new(1, 2, 3), vector.new(1, 2, 3))
- assert.is_true(vector.new(1, 2, 3):equals(vector.new(1, 2, 3)))
- assert.not_equal(vector.new(1, 2, 3), vector.new(1, 2, 4))
- assert.is_true(vector.new(1, 2, 3) == vector.new(1, 2, 3))
- assert.is_false(vector.new(1, 2, 3) == vector.new(1, 3, 3))
+ assertE({x = 0, y = 0, z = 0}, {x = 0, y = 0, z = 0})
+ assertE({x = -1, y = 0, z = 1}, {x = -1, y = 0, z = 1})
+ assertE({x = -1, y = 0, z = 1}, vector.new(-1, 0, 1))
+ local a = {x = 2, y = 4, z = -10}
+ assertE(a, a)
+ assertNE({x = -1, y = 0, z = 1}, a)
+
+ assert.equal(vector.new(1, 2, 3), vector.new(1, 2, 3))
+ assert.is_true(vector.new(1, 2, 3):equals(vector.new(1, 2, 3)))
+ assert.not_equal(vector.new(1, 2, 3), vector.new(1, 2, 4))
+ assert.is_true(vector.new(1, 2, 3) == vector.new(1, 2, 3))
+ assert.is_false(vector.new(1, 2, 3) == vector.new(1, 3, 3))
end)
it("metatable is same", function()
diff --git a/builtin/common/vector.lua b/builtin/common/vector.lua
index 752167a63..02fc1bdee 100644
--- a/builtin/common/vector.lua
+++ b/builtin/common/vector.lua
@@ -30,16 +30,28 @@ local function fast_new(x, y, z)
end
function vector.new(a, b, c)
- if type(a) == "table" then
- assert(a.x and a.y and a.z, "Invalid vector passed to vector.new()")
- return fast_new(a.x, a.y, a.z)
- elseif a then
- assert(b and c, "Invalid arguments for vector.new()")
+ if a and b and c then
return fast_new(a, b, c)
end
+
+ -- deprecated, use vector.copy and vector.zero directly
+ if type(a) == "table" then
+ return vector.copy(a)
+ else
+ assert(not a, "Invalid arguments for vector.new()")
+ return vector.zero()
+ end
+end
+
+function vector.zero()
return fast_new(0, 0, 0)
end
+function vector.copy(v)
+ assert(v.x and v.y and v.z, "Invalid vector passed to vector.copy()")
+ return fast_new(v.x, v.y, v.z)
+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)
diff --git a/doc/lua_api.txt b/doc/lua_api.txt
index 3a1a3f02f..73c5b43a5 100644
--- a/doc/lua_api.txt
+++ b/doc/lua_api.txt
@@ -3271,10 +3271,13 @@ For the following functions, `v`, `v1`, `v2` are vectors,
vectors are written like this: `(x, y, z)`:
* `vector.new([a[, b, c]])`:
- * Returns a vector.
- * A copy of `a` if `a` is a vector.
- * `(a, b, c)`, if all of `a`, `b`, `c` are defined numbers.
- * `(0, 0, 0)`, if no arguments are given.
+ * Returns a new vector `(a, b, c)`.
+ * Deprecated: `vector.new()` does the same as `vector.zero()` and
+ `vector.new(v)` does the same as `vector.copy(v)`
+* `vector.zero()`:
+ * Returns a new vector `(0, 0, 0)`.
+* `vector.copy(v)`:
+ * Returns a copy of the vector `v`.
* `vector.from_string(s[, init])`:
* Returns `v, np`, where `v` is a vector read from the given string `s` and
`np` is the next position in the string after the vector.