From 1febaae767b8a44e31d68e04a3a36e80f97e8e9c Mon Sep 17 00:00:00 2001 From: helixhorned Date: Sat, 22 Jun 2013 11:31:09 +0000 Subject: [PATCH] Lunatic: various minor tweaks and fixes. - Pass original module name (dot=dirsep) to module via our require() - geom.lua: fix some operations using the vector type constructor - geom.lua: provide constructor for ivec3, useable like vec3 git-svn-id: https://svn.eduke32.com/eduke32@3894 1a8010ca-5511-0410-912e-c29ae57300e0 --- polymer/eduke32/source/lunatic/defs.ilua | 6 ++-- polymer/eduke32/source/lunatic/geom.lua | 31 ++++++++++++------- polymer/eduke32/source/lunatic/lunacon.lua | 10 +++--- polymer/eduke32/source/lunatic/test.elua | 6 ++-- .../source/lunatic/test/delmusicsfx.lua | 2 +- .../eduke32/source/lunatic/test/test_geom.lua | 8 +++++ 6 files changed, 42 insertions(+), 21 deletions(-) diff --git a/polymer/eduke32/source/lunatic/defs.ilua b/polymer/eduke32/source/lunatic/defs.ilua index 50338136d..19b27660a 100644 --- a/polymer/eduke32/source/lunatic/defs.ilua +++ b/polymer/eduke32/source/lunatic/defs.ilua @@ -1599,7 +1599,9 @@ local function our_require(modname, ...) error("Module name must not contain directory separators", ERRLEV-1) end -- Instead, dots are translated to directory separators. For EDuke32's - -- virtual file system, this is always a forward slash. + -- virtual file system, this is always a forward slash. Keep the original + -- module name for passing to the module function. + local omodname = modname modname = modname:gsub("%.", "/") local omod = package_loaded[modname] @@ -1633,7 +1635,7 @@ local function our_require(modname, ...) -- Run the module code in a separate Lua thread! local modthread = coroutine.create(modfunc) - local ok, retval = coroutine.resume(modthread, modname, ...) + local ok, retval = coroutine.resume(modthread, omodname, ...) if (not ok) then errorf(ERRLEV-1, "Failed running \"%s\": %s", modname, retval) diff --git a/polymer/eduke32/source/lunatic/geom.lua b/polymer/eduke32/source/lunatic/geom.lua index fb95ce153..de4d9ba71 100644 --- a/polymer/eduke32/source/lunatic/geom.lua +++ b/polymer/eduke32/source/lunatic/geom.lua @@ -88,7 +88,7 @@ local vec3_mt = { -- '^' is the "translate upwards" operator, returns same-typed vector. __pow = function(v, zofs) - return v(v.x, v.y, v.z-zofs) + return v:_ctor(v.x, v.y, v.z-zofs) end, -- XXX: Rewrite using _serialize internal API instead. @@ -112,20 +112,21 @@ local vec3_mt = { 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. touniform = function(v) return v:_isi() - and v:_getctor(v.x, v.y, arshift(v.z, 4)) - or v:_getctor(v.x, v.y, v.z/4) + and v:_ctor(v.x, v.y, arshift(v.z, 4)) + or v:_ctor(v.x, v.y, v.z/16) end, - tobuild = function(v) return v:_getctor(v.x, v.y, 16*v.z) end, + tobuild = function(v) return v:_ctor(v.x, v.y, 16*v.z) end, + -- PRIVATE methods -- + + -- Get the type constructor for this vector. + _ctor = function(v, ...) + return v:_isi() and ivec3_t(...) or dvec3_t(...) + end, -- Is integer vec3? INTERNAL. _isi = function(v) return ffi.istype(ivec3_t, v) @@ -135,6 +136,7 @@ local vec3_mt = { ffi.metatype(dvec2_t, vec2_mt) ffi.metatype(dvec3_t, vec3_mt) +ffi.metatype(ivec3_t, vec3_mt) -- VEC2 user data constructor. -- * vec2([x [, y]]), assuming that x and y are numbers. Vacant positions are @@ -160,8 +162,15 @@ function vec3(...) end end --- TODO: make a constructor like VEC3? -ivec3 = ffi.metatype(ivec3_t, vec3_mt) +-- IVEC3 user data constructor. +function ivec3(...) + local x, y, z = ... + if (type(x)=="number" or x==nil) then + return ivec3_t(...) + else + return ivec3_t(x.x, x.y, x.z) + end +end -- Two-element vector cross product. diff --git a/polymer/eduke32/source/lunatic/lunacon.lua b/polymer/eduke32/source/lunatic/lunacon.lua index e6d775f48..8784c3061 100644 --- a/polymer/eduke32/source/lunatic/lunacon.lua +++ b/polymer/eduke32/source/lunatic/lunacon.lua @@ -426,7 +426,7 @@ function on.actor_end(pos, usertype, tsamm, codetab) str = str .. movflags.."," end - paddcodef(pos, "gameactor{%d,%sfunction(_aci, _pli, _dist)", tilenum, str) + paddcodef(pos, "gameactor{%d,%sfunction(_aci,_pli,_dist)", tilenum, str) addcode(get_cache_sap_code()) add_code_and_end(codetab, "end}") @@ -476,7 +476,7 @@ function on.state_begin_Cmt(_subj, _pos, statename) end function on.state_end(pos, funcname, codetab) - paddcodef(pos, "%s=function(_aci, _pli, _dist)", funcname) + paddcodef(pos, "%s=function(_aci,_pli,_dist)", funcname) addcode(get_cache_sap_code()) add_code_and_end(codetab, "end") end @@ -484,7 +484,7 @@ end function on.event_end(pos, eventidx, codetab) assert(type(codetab)=="table") -- 0x20000000: actor.FLAGS.chain_beg - paddcodef(pos, "gameevent{%d,0x20000000,function (_aci, _pli, _dist)", eventidx) + paddcodef(pos, "gameevent{%d,0x20000000,function (_aci,_pli,_dist)", eventidx) addcode(get_cache_sap_code()) addcode(codetab) addcode("end}") @@ -494,7 +494,7 @@ end function on.eventloadactor_end(pos, tilenum, codetab) -- Translate eventloadactor into a chained EVENT_LOADACTOR block - paddcodef(pos, "gameevent{'LOADACTOR', function (_aci, _pli, _dist)") + paddcodef(pos, "gameevent{'LOADACTOR', function (_aci,_pli,_dist)") addcode(get_cache_sap_code()) addcodef("if (%s==%d) then", SPS".picnum", tilenum) addcode(codetab) @@ -2127,7 +2127,7 @@ local Cinner = { lockplayer = cmd(R) / PLS".transporter_hold=%1", quake = cmd(R) - / "_gv.doQuake(%1,81)", -- EARTHQUAKE + / "_gv.doQuake(%1,81)", -- TODO: dynsound (EARTHQUAKE) jump = cmd(R) / handle.NYI, -- will never be cmenu = cmd(R) diff --git a/polymer/eduke32/source/lunatic/test.elua b/polymer/eduke32/source/lunatic/test.elua index 163cae886..851d620cd 100644 --- a/polymer/eduke32/source/lunatic/test.elua +++ b/polymer/eduke32/source/lunatic/test.elua @@ -356,7 +356,9 @@ gameevent mpos = mpos + wall[i] end mpos = mpos/gv.numwalls - printf("Map center point: (%d,%d)", mpos.x, mpos.y) + local impos = geom.ivec3(mpos)^20 -- test ivec3 with dvec3 arg, test '^' op + assert(impos.z == -20) + printf("Map center point: (%d,%f)", mpos.x, impos.y) end } @@ -597,7 +599,7 @@ printf("EVENT_INIT = %d", gv.EVENT_INIT) -- tests default defines local bittest = require "test.test_bitar" bittest.sieve() -require("test.test_geom") +require("test.test_geom", 123123) require("test.test_rotspr") do diff --git a/polymer/eduke32/source/lunatic/test/delmusicsfx.lua b/polymer/eduke32/source/lunatic/test/delmusicsfx.lua index fef64db6c..63ccd352b 100644 --- a/polymer/eduke32/source/lunatic/test/delmusicsfx.lua +++ b/polymer/eduke32/source/lunatic/test/delmusicsfx.lua @@ -83,6 +83,6 @@ gameevent "DISPLAYREST", function() - con.minitext(160, 10, string.format("jumped %d times", ournumjumps)) + con.minitext(240, 10, string.format("jumped %d times", ournumjumps)) end } diff --git a/polymer/eduke32/source/lunatic/test/test_geom.lua b/polymer/eduke32/source/lunatic/test/test_geom.lua index 867509350..f4d861ddd 100755 --- a/polymer/eduke32/source/lunatic/test/test_geom.lua +++ b/polymer/eduke32/source/lunatic/test/test_geom.lua @@ -12,12 +12,17 @@ local V,W = {}, {} local randvec +local args = { ... } +local ourname = args[1] + if (os.exit) then local math = require("math") randvec = function() return geom.vec2(math.random(), math.random()) end + + print("Running stand-alone. ourname: "..tostring(ourname)) else local randgen = require("randgen") local s = randgen.new(true) @@ -27,6 +32,9 @@ else randvec = function() return geom.vec2(s:getdbl(), s:getdbl()) end + + -- Test optional arguments from our_require(). + printf("Running %s stand-alone with opt arg %s", ourname, tostring(args[2])) end local t1 = os.clock()