Lunatic: clean up 'geom' module, make walls readable with pseudo-member 'z'.

git-svn-id: https://svn.eduke32.com/eduke32@3875 1a8010ca-5511-0410-912e-c29ae57300e0
This commit is contained in:
helixhorned 2013-06-12 17:49:56 +00:00
parent 9911e96fe8
commit 9c1eeae751
3 changed files with 30 additions and 22 deletions

View file

@ -579,6 +579,10 @@ local walltype_mt = {
ishittable = function(w) ishittable = function(w)
return (band(w.cstat, 64)~=0) return (band(w.cstat, 64)~=0)
end, end,
-- Indexing a wall with 'z' gets 0, so that you can e.g. use a wall as
-- RHS to vec3_t addition.
z = 0,
} }
} }
ffi.metatype("walltype", walltype_mt) ffi.metatype("walltype", walltype_mt)

View file

@ -4,6 +4,9 @@ local require = require
local ffi = require("ffi") local ffi = require("ffi")
local math = require("math") local math = require("math")
local abs = math.abs
local sqrt = math.sqrt
local type = type local type = type
local error = error local error = error
@ -23,7 +26,6 @@ local ivec3_t = ffi.typeof("vec3_t")
local dvec2_t = ffi.typeof("struct { double x, y; }") local dvec2_t = ffi.typeof("struct { double x, y; }")
local dvec3_t = ffi.typeof("struct { double x, y, z; }") local dvec3_t = ffi.typeof("struct { double x, y, z; }")
local vec2_mt = { local vec2_mt = {
__add = function(a, b) return dvec2_t(a.x+b.x, a.y+b.y) end, __add = function(a, b) return dvec2_t(a.x+b.x, a.y+b.y) end,
__sub = function(a, b) return dvec2_t(a.x-b.x, a.y-b.y) end, __sub = function(a, b) return dvec2_t(a.x-b.x, a.y-b.y) end,
@ -47,14 +49,12 @@ local vec2_mt = {
return dvec2_t(a.x/b, a.y/b) return dvec2_t(a.x/b, a.y/b)
end, end,
__len = function(a) return math.sqrt(a.x*a.x + a.y*a.y) end,
__tostring = function(a) return "vec2("..a.x..", "..a.y..")" end, __tostring = function(a) return "vec2("..a.x..", "..a.y..")" end,
__index = { __index = {
lensq = function(a) return a.x*a.x + a.y*a.y end, lensq = function(a) return a.x*a.x + a.y*a.y end,
mhlen = function(a) return math.abs(a.x)+math.abs(a.y) end, mhlen = function(a) return abs(a.x)+abs(a.y) end,
}, },
} }
@ -91,44 +91,40 @@ local vec3_mt = {
return v(v.x, v.y, v.z-zofs) return v(v.x, v.y, v.z-zofs)
end, end,
-- The # operator returns the Euclidean length. -- XXX: Rewrite using _serialize internal API instead.
-- TODO: REMOVE.
__len = function(a) return math.sqrt(a.x*a.x + a.y*a.y + a.z*a.z) end,
-- INTERNAL: Calling a vector calls the constructor of its type.
__call = function(v, ...)
return v:_isi() and ivec3_t(...) or dvec3_t(...)
end,
-- INTERNAL
__tostring = function(a) __tostring = function(a)
return (a:_isi() and "i" or "").."vec3("..a.x..", "..a.y..", "..a.z..")" return (a:_isi() and "i" or "").."vec3("..a.x..", "..a.y..", "..a.z..")"
end, end,
__index = { __index = {
-- Euclidean 3D length. -- Euclidean 3D length.
len = function(a) return math.sqrt(a.x*a.x + a.y*a.y + a.z*a.z) end, len = function(a) return sqrt(a.x*a.x + a.y*a.y + a.z*a.z) end,
-- Euclidean 3D squared length. -- Euclidean 3D squared length.
lensq = function(a) return a.x*a.x + a.y*a.y + a.z*a.z end, lensq = function(a) return a.x*a.x + a.y*a.y + a.z*a.z end,
-- Euclidean 2D length. -- Euclidean 2D length.
len2 = function(a) return math.sqrt(a.x*a.x + a.y*a.y) end, len2 = function(a) return sqrt(a.x*a.x + a.y*a.y) end,
-- Euclidean 2D squared length. -- Euclidean 2D squared length.
len2sq = function(a) return a.x*a.x + a.y*a.y end, len2sq = function(a) return a.x*a.x + a.y*a.y end,
-- Manhattan-distance 3D length: -- Manhattan-distance 3D length:
mhlen = function(a) return math.abs(a.x)+math.abs(a.y)+math.abs(a.z) end, mhlen = function(a) return abs(a.x)+abs(a.y)+abs(a.z) end,
toivec3 = function(v) return ivec3_t(v.x, v.y, v.z) end, toivec3 = function(v) return ivec3_t(v.x, v.y, v.z) end,
-- Get the type constructor for this vector.
_getctor = function(v)
return v:_isi() and ivec3_t or dvec3_t
end,
-- BUILD-coordinate (z scaled by 16) <-> uniform conversions. -- BUILD-coordinate (z scaled by 16) <-> uniform conversions.
touniform = function(v) touniform = function(v)
return v:_isi() return v:_isi()
and v(v.x, v.y, arshift(v.z, 4)) and v:_getctor(v.x, v.y, arshift(v.z, 4))
or v(v.x, v.y, v.z/4) or v:_getctor(v.x, v.y, v.z/4)
end, end,
tobuild = function(v) return v(v.x, v.y, 16*v.z) end, tobuild = function(v) return v:_getctor(v.x, v.y, 16*v.z) end,
-- Is <v> integer vec3? INTERNAL. -- Is <v> integer vec3? INTERNAL.
_isi = function(v) _isi = function(v)

View file

@ -247,6 +247,8 @@ gameevent
end end
} }
local geom = require "geom"
gameevent gameevent
{ {
gv.EVENT_ENTERLEVEL, gv.EVENT_ENTERLEVEL,
@ -337,11 +339,17 @@ gameevent
checkfail("gameevent('GAME', function() print('qwe') end)", checkfail("gameevent('GAME', function() print('qwe') end)",
"must be called from top level") "must be called from top level")
-- Test vec3 + wall. Pseudo wall member 'z' will be accessed.
local mpos = geom.vec3()
for i=0,gv.numwalls-1 do
mpos = mpos + wall[i]
end
mpos = mpos/gv.numwalls
printf("Map center point: (%d,%d)", mpos.x, mpos.y)
end end
} }
local geom = require "geom"
gameevent{"LOADACTOR", function(i) gameevent{"LOADACTOR", function(i)
local spr = sprite[i] local spr = sprite[i]
if (i==614 and spr.picnum==930) then if (i==614 and spr.picnum==930) then