From 8f89c89d860fad3909a75d157eb5f8193e5c859d Mon Sep 17 00:00:00 2001 From: helixhorned Date: Sun, 7 Oct 2012 15:26:24 +0000 Subject: [PATCH] Lunatic: various tweaks and API changes/additions. git-svn-id: https://svn.eduke32.com/eduke32@3063 1a8010ca-5511-0410-912e-c29ae57300e0 --- .../eduke32/source/lunatic/defs_common.lua | 10 ++-- polymer/eduke32/source/lunatic/geom.lua | 60 ++++++++++++++++--- polymer/eduke32/source/lunatic/stat.lua | 6 +- polymer/eduke32/source/lunatic/test.elua | 3 +- 4 files changed, 62 insertions(+), 17 deletions(-) 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(), the arg should be indexable with "x" and "y" +-- * vec2(
),
should be indexable with "x" and "y" -- * vec2(x, y), assuming that x and y are numbers -vec2_ = ffi.metatype("dvec2_t", mt) +vec2_ = ffi.metatype("dvec2_t", vec2_mt) vec2 = vec2_ -- Returns a vec2 from anything indexable with "x" and "y" --- (vec2(t) works if t is such a table, but not if it's a vec2) -function tovec2(t) - return vec2(t.x, t.y) -end +-- (vec2(t) works if t is such a table, but not if it's a vec2 or other cdata) +function tovec2(t) return vec2(t.x, t.y) end + +-- Same for vec3 +vec3_ = ffi.metatype("dvec3_t", vec3_mt) +vec3 = vec3_ +function tovec3(t) return vec3(t.x, t.y, t.z) end + +-- This has no metamethods, but can be useful for calculations expecting +-- integer values, e.g. geom.ivec3(x, y, z) is a reasonable way to round +-- a vec3. It can be also used as the RHS to the vec2/vec3 arithmetic +-- methods. +ivec3 = ffi.typeof("vec3_t") + -- Two-element vector cross product. -- Anti-commutative, distributive. diff --git a/polymer/eduke32/source/lunatic/stat.lua b/polymer/eduke32/source/lunatic/stat.lua index 3b5001111..ee0254570 100644 --- a/polymer/eduke32/source/lunatic/stat.lua +++ b/polymer/eduke32/source/lunatic/stat.lua @@ -76,8 +76,8 @@ local mt = { end, reset = function(s) - s.n, s.m = 0, 0 - s.s, s.min, s.max = NaN, NaN, NaN + s.n = 0 + s.m, s.s, s.min, s.max = NaN, NaN, NaN, NaN end, }, } @@ -86,7 +86,7 @@ local rstat = ffi.metatype("runningstat_t", mt) function new(n,m,s, min,max) if (n == nil) then -- initialization containing no elements - return rstat(0, 0, NaN, NaN, NaN) + return rstat(0, NaN, NaN, NaN, NaN) elseif (m == nil) then -- same as initialization with N==0 above (one element) return rstat(1, n, 0, n, n) diff --git a/polymer/eduke32/source/lunatic/test.elua b/polymer/eduke32/source/lunatic/test.elua index 9e34dc518..67e028cbc 100644 --- a/polymer/eduke32/source/lunatic/test.elua +++ b/polymer/eduke32/source/lunatic/test.elua @@ -245,9 +245,8 @@ gameactor(1680, -- LIZTROOP local spr = sprite[i] - local x,y,z = spr.x, spr.y, spr.z local t = gv.gethitickms() - local hit = hitscan(x,y,z, spr.sectnum, 10, 10, 0, gv.CLIPMASK0) + local hit = hitscan(spr, spr.sectnum, 10, 10, 0, gv.CLIPMASK0) hs:add(1000*(gv.gethitickms()-t))