aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorShadowNinja <noreply@gmail.com>2013-10-30 15:38:13 -0400
committerShadowNinja <noreply@gmail.com>2013-11-01 13:16:07 -0400
commitfba2650e392ad4b3c1ec0d7839f24e1cbb53a1cb (patch)
treed41026f35ce158a8db39838622fea48605c53848
parent1cbba877ffaa1b080d08e7571665e086f4656805 (diff)
downloadminetest-fba2650e392ad4b3c1ec0d7839f24e1cbb53a1cb.tar.gz
minetest-fba2650e392ad4b3c1ec0d7839f24e1cbb53a1cb.tar.bz2
minetest-fba2650e392ad4b3c1ec0d7839f24e1cbb53a1cb.zip
Add more checks to vector functions
-rw-r--r--builtin/vector.lua39
1 files changed, 25 insertions, 14 deletions
diff --git a/builtin/vector.lua b/builtin/vector.lua
index 698309c4a..6b787366d 100644
--- a/builtin/vector.lua
+++ b/builtin/vector.lua
@@ -1,41 +1,46 @@
vector = {}
+local function assert_vector(v)
+ assert(type(v) == "table" and v.x and v.y and v.z, "Invalid vector")
+end
+
function vector.new(a, b, c)
- assert(a)
if type(a) == "table" then
+ assert(a.x and a.y and a.z, "Invalid vector passed to vector.new()")
return {x=a.x, y=a.y, z=a.z}
- else
- assert(b and c)
+ elseif a then
+ assert(b and c, "Invalid arguments for vector.new()")
return {x=a, y=b, z=c}
end
return {x=0, y=0, z=0}
end
function vector.equals(a, b)
- assert(a and b)
+ assert_vector(a)
+ assert_vector(b)
return a.x == b.x and
a.y == b.y and
a.z == b.z
end
function vector.length(v)
- assert(v)
+ assert_vector(v)
return math.hypot(v.x, math.hypot(v.y, v.z))
end
function vector.normalize(v)
- assert(v)
+ assert_vector(v)
local len = vector.length(v)
if len == 0 then
- return vector.new()
+ return {x=0, y=0, z=0}
else
return vector.divide(v, len)
end
end
function vector.round(v)
- assert(v)
+ assert_vector(v)
return {
x = math.floor(v.x + 0.5),
y = math.floor(v.y + 0.5),
@@ -44,7 +49,8 @@ function vector.round(v)
end
function vector.distance(a, b)
- assert(a and b)
+ assert_vector(a)
+ assert_vector(b)
local x = a.x - b.x
local y = a.y - b.y
local z = a.z - b.z
@@ -52,7 +58,8 @@ function vector.distance(a, b)
end
function vector.direction(pos1, pos2)
- assert(pos1 and pos2)
+ assert_vector(pos1)
+ assert_vector(pos2)
local x_raw = pos2.x - pos1.x
local y_raw = pos2.y - pos1.y
local z_raw = pos2.z - pos1.z
@@ -82,7 +89,8 @@ end
function vector.add(a, b)
- assert(a and b)
+ assert_vector(a)
+ assert_vector(b)
if type(b) == "table" then
return {x = a.x + b.x,
y = a.y + b.y,
@@ -95,7 +103,8 @@ function vector.add(a, b)
end
function vector.subtract(a, b)
- assert(a and b)
+ assert_vector(a)
+ assert_vector(b)
if type(b) == "table" then
return {x = a.x - b.x,
y = a.y - b.y,
@@ -108,7 +117,8 @@ function vector.subtract(a, b)
end
function vector.multiply(a, b)
- assert(a and b)
+ assert_vector(a)
+ assert_vector(b)
if type(b) == "table" then
return {x = a.x * b.x,
y = a.y * b.y,
@@ -121,7 +131,8 @@ function vector.multiply(a, b)
end
function vector.divide(a, b)
- assert(a and b)
+ assert_vector(a)
+ assert_vector(b)
if type(b) == "table" then
return {x = a.x / b.x,
y = a.y / b.y,