diff --git a/polymer/eduke32/source/lunatic/defs_common.lua b/polymer/eduke32/source/lunatic/defs_common.lua index 79cae718e..f2dd5b90c 100644 --- a/polymer/eduke32/source/lunatic/defs_common.lua +++ b/polymer/eduke32/source/lunatic/defs_common.lua @@ -312,7 +312,7 @@ end function getbunch(sectnum, cf) if (sectnum < 0 or sectnum >= ffiC.numsectors) then - error('passed out-of-bounds sector number'..sectnum, 2) + error('passed out-of-bounds sector number '..sectnum, 2) end if (cf ~= 0 and cf ~= 1) then error("passed invalid 'cf' to getbunch, must be 0 or 1", 2) @@ -324,12 +324,14 @@ end ---=== Engine functions, wrapped for Lua convenience ===--- -- returns a hitdata_ct -function hitscan(x,y,z, sectnum, vx,vy,vz, cliptype) +-- TODO: make v[xyz] be passed as one aggregate, too? +-- Additionally, permit different coordinates? (ang&horiz, ...) +function hitscan(pos, sectnum, vx,vy,vz, cliptype) if (sectnum < 0 or sectnum >= ffiC.numsectors) then - error('passed out-of-bounds sector number'..sectnum, 2) + error('passed out-of-bounds sector number '..sectnum, 2) end - local vec = vec3_ct(x,y,z) + local vec = vec3_ct(pos.x, pos.y, pos.z) local hitdata = hitdata_ct() ffiC.hitscan(vec, sectnum, vx,vy,vz, hitdata, cliptype) diff --git a/polymer/eduke32/source/lunatic/geom.lua b/polymer/eduke32/source/lunatic/geom.lua index f750af363..abe545f08 100644 --- a/polymer/eduke32/source/lunatic/geom.lua +++ b/polymer/eduke32/source/lunatic/geom.lua @@ -12,13 +12,14 @@ module(...) ffi.cdef[[ typedef struct { double x, y; } dvec2_t; +typedef struct { double x, y, z; } dvec3_t; ]] local vec2_ -local mt = { +local vec2_mt = { __add = function(a, b) return vec2_(a.x+b.x, a.y+b.y) end, __sub = function(a, b) return vec2_(a.x-b.x, a.y-b.y) end, - __unm = function(a) return vec2_(-a.x, -b.x) end, + __unm = function(a) return vec2_(-a.x, -a.x) end, __mul = function(a,b) if (type(a)=="number") then @@ -50,17 +51,60 @@ local mt = { }, } +local vec3_ +local vec3_mt = { + __add = function(a, b) return vec3_(a.x+b.x, a.y+b.y, a.z+b.z) end, + __sub = function(a, b) return vec3_(a.x-b.x, a.y-b.y, a.z-b.z) end, + __unm = function(a) return vec3_(-a.x, -a.x, -a.z) end, + + __mul = function(a,b) + if (type(a)=="number") then + return vec3_(a*b.x, a*b.y, a*b.z) + end + + assert(type(b)=="number") + return vec2_(a.x*b, a.y*b, a.z*b) + end, + + __div = function(a,b) + assert(type(b)=="number") + return vec2_(a.x/b, a.y/b, a.z/b) + end, +--[[ + __eq = function(a,b) + return (a.x==b.x and a.y==b.y and a.z==b.z) + end, +--]] + __len = function(a) return math.sqrt(a.x*a.x + a.y*a.y + a.z*a.z) end, + + __tostring = function(a) return "vec3("..a.x..", "..a.y..", "..a.z..")" end, + + __index = { + lensq = function(a) return a.x*a.x + a.y*a.y + a.z*a.z end, + }, +} + -- VEC2 user data constructor. --- * vec2(