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
This commit is contained in:
helixhorned 2013-06-22 11:31:09 +00:00
parent 5db04f585e
commit 1febaae767
6 changed files with 42 additions and 21 deletions

View file

@ -1599,7 +1599,9 @@ local function our_require(modname, ...)
error("Module name must not contain directory separators", ERRLEV-1) error("Module name must not contain directory separators", ERRLEV-1)
end end
-- Instead, dots are translated to directory separators. For EDuke32's -- 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("%.", "/") modname = modname:gsub("%.", "/")
local omod = package_loaded[modname] local omod = package_loaded[modname]
@ -1633,7 +1635,7 @@ local function our_require(modname, ...)
-- Run the module code in a separate Lua thread! -- Run the module code in a separate Lua thread!
local modthread = coroutine.create(modfunc) local modthread = coroutine.create(modfunc)
local ok, retval = coroutine.resume(modthread, modname, ...) local ok, retval = coroutine.resume(modthread, omodname, ...)
if (not ok) then if (not ok) then
errorf(ERRLEV-1, "Failed running \"%s\": %s", modname, retval) errorf(ERRLEV-1, "Failed running \"%s\": %s", modname, retval)

View file

@ -88,7 +88,7 @@ local vec3_mt = {
-- '^' is the "translate upwards" operator, returns same-typed vector. -- '^' is the "translate upwards" operator, returns same-typed vector.
__pow = function(v, zofs) __pow = function(v, zofs)
return v(v.x, v.y, v.z-zofs) return v:_ctor(v.x, v.y, v.z-zofs)
end, end,
-- XXX: Rewrite using _serialize internal API instead. -- 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, 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:_getctor(v.x, v.y, arshift(v.z, 4)) and v:_ctor(v.x, v.y, arshift(v.z, 4))
or v:_getctor(v.x, v.y, v.z/4) or v:_ctor(v.x, v.y, v.z/16)
end, 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 <v> integer vec3? INTERNAL. -- Is <v> integer vec3? INTERNAL.
_isi = function(v) _isi = function(v)
return ffi.istype(ivec3_t, v) return ffi.istype(ivec3_t, v)
@ -135,6 +136,7 @@ local vec3_mt = {
ffi.metatype(dvec2_t, vec2_mt) ffi.metatype(dvec2_t, vec2_mt)
ffi.metatype(dvec3_t, vec3_mt) ffi.metatype(dvec3_t, vec3_mt)
ffi.metatype(ivec3_t, vec3_mt)
-- VEC2 user data constructor. -- VEC2 user data constructor.
-- * vec2([x [, y]]), assuming that x and y are numbers. Vacant positions are -- * vec2([x [, y]]), assuming that x and y are numbers. Vacant positions are
@ -160,8 +162,15 @@ function vec3(...)
end end
end end
-- TODO: make a constructor like VEC3? -- IVEC3 user data constructor.
ivec3 = ffi.metatype(ivec3_t, vec3_mt) 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. -- Two-element vector cross product.

View file

@ -426,7 +426,7 @@ function on.actor_end(pos, usertype, tsamm, codetab)
str = str .. movflags.."," str = str .. movflags..","
end 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()) addcode(get_cache_sap_code())
add_code_and_end(codetab, "end}") add_code_and_end(codetab, "end}")
@ -476,7 +476,7 @@ function on.state_begin_Cmt(_subj, _pos, statename)
end end
function on.state_end(pos, funcname, codetab) 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()) addcode(get_cache_sap_code())
add_code_and_end(codetab, "end") add_code_and_end(codetab, "end")
end end
@ -484,7 +484,7 @@ end
function on.event_end(pos, eventidx, codetab) function on.event_end(pos, eventidx, codetab)
assert(type(codetab)=="table") assert(type(codetab)=="table")
-- 0x20000000: actor.FLAGS.chain_beg -- 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(get_cache_sap_code())
addcode(codetab) addcode(codetab)
addcode("end}") addcode("end}")
@ -494,7 +494,7 @@ end
function on.eventloadactor_end(pos, tilenum, codetab) function on.eventloadactor_end(pos, tilenum, codetab)
-- Translate eventloadactor into a chained EVENT_LOADACTOR block -- 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()) addcode(get_cache_sap_code())
addcodef("if (%s==%d) then", SPS".picnum", tilenum) addcodef("if (%s==%d) then", SPS".picnum", tilenum)
addcode(codetab) addcode(codetab)
@ -2127,7 +2127,7 @@ local Cinner = {
lockplayer = cmd(R) lockplayer = cmd(R)
/ PLS".transporter_hold=%1", / PLS".transporter_hold=%1",
quake = cmd(R) quake = cmd(R)
/ "_gv.doQuake(%1,81)", -- EARTHQUAKE / "_gv.doQuake(%1,81)", -- TODO: dynsound (EARTHQUAKE)
jump = cmd(R) jump = cmd(R)
/ handle.NYI, -- will never be / handle.NYI, -- will never be
cmenu = cmd(R) cmenu = cmd(R)

View file

@ -356,7 +356,9 @@ gameevent
mpos = mpos + wall[i] mpos = mpos + wall[i]
end end
mpos = mpos/gv.numwalls 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 end
} }
@ -597,7 +599,7 @@ printf("EVENT_INIT = %d", gv.EVENT_INIT) -- tests default defines
local bittest = require "test.test_bitar" local bittest = require "test.test_bitar"
bittest.sieve() bittest.sieve()
require("test.test_geom") require("test.test_geom", 123123)
require("test.test_rotspr") require("test.test_rotspr")
do do

View file

@ -83,6 +83,6 @@ gameevent
"DISPLAYREST", "DISPLAYREST",
function() function()
con.minitext(160, 10, string.format("jumped %d times", ournumjumps)) con.minitext(240, 10, string.format("jumped %d times", ournumjumps))
end end
} }

View file

@ -12,12 +12,17 @@ local V,W = {}, {}
local randvec local randvec
local args = { ... }
local ourname = args[1]
if (os.exit) then if (os.exit) then
local math = require("math") local math = require("math")
randvec = function() randvec = function()
return geom.vec2(math.random(), math.random()) return geom.vec2(math.random(), math.random())
end end
print("Running stand-alone. ourname: "..tostring(ourname))
else else
local randgen = require("randgen") local randgen = require("randgen")
local s = randgen.new(true) local s = randgen.new(true)
@ -27,6 +32,9 @@ else
randvec = function() randvec = function()
return geom.vec2(s:getdbl(), s:getdbl()) return geom.vec2(s:getdbl(), s:getdbl())
end end
-- Test optional arguments from our_require().
printf("Running %s stand-alone with opt arg %s", ourname, tostring(args[2]))
end end
local t1 = os.clock() local t1 = os.clock()