From 528366da466afa0ce4293ce4a5a8b7b8a8b47905 Mon Sep 17 00:00:00 2001 From: helixhorned Date: Fri, 7 Jun 2013 10:18:20 +0000 Subject: [PATCH] Lunatic: add preliminary helper code for local gamevar saving. git-svn-id: https://svn.eduke32.com/eduke32@3858 1a8010ca-5511-0410-912e-c29ae57300e0 --- polymer/eduke32/source/lunatic/control.lua | 10 ++++---- polymer/eduke32/source/lunatic/defs.ilua | 28 ++++++++++++++++++++++ 2 files changed, 33 insertions(+), 5 deletions(-) diff --git a/polymer/eduke32/source/lunatic/control.lua b/polymer/eduke32/source/lunatic/control.lua index 588fd70ac..b2981d9e2 100644 --- a/polymer/eduke32/source/lunatic/control.lua +++ b/polymer/eduke32/source/lunatic/control.lua @@ -443,7 +443,7 @@ local function A_FP_ManhattanDist(ps, spr) end -- Returns: player index, distance --- TODO: MP case +-- TODO_MP function _findplayer(pli, spritenum) return 0, A_FP_ManhattanDist(player[pli], sprite[spritenum]) end @@ -981,7 +981,7 @@ function _pstomp(ps, i) end function _pkick(ps, spr) - -- TODO: multiplayer branch + -- TODO_MP if (spr.picnum~=D.APLAYER and ps.quick_kick==0) then ps.quick_kick = 14 end @@ -1514,7 +1514,7 @@ function _ifp(flags, pli, aci) elseif (band(l,32768)~=0 and sprite[ps.i].extra <= 0) then return true elseif (band(l,65536)~=0) then - -- TODO: multiplayer branch + -- TODO_MP if (_angdiffabs(ps.ang, ffiC.getangle(sprite[aci].x-ps.pos.x, sprite[aci].y-ps.pos.y)) < 128) then return true end @@ -2119,8 +2119,8 @@ local actorvar_methods = { _serialize = function(acv) local strtab = { OUR_NAME..".actorvar(", tostring(acv._defval), ",{" } - -- TODO: Must clean up sometime if not saving, too. (That is, what is - -- A_ResetVars() in the C-CON build.) + -- NOTE: We also clean up when spawning a sprite, too. (See + -- A_ResetVars() and related functions above.) acv:_cleanup() return serialize_array(acv, strtab, ffiC.MAXSPRITES) end, diff --git a/polymer/eduke32/source/lunatic/defs.ilua b/polymer/eduke32/source/lunatic/defs.ilua index a47f2bf7f..6ad3b56ed 100644 --- a/polymer/eduke32/source/lunatic/defs.ilua +++ b/polymer/eduke32/source/lunatic/defs.ilua @@ -1352,6 +1352,7 @@ end local package_loaded = {} -- [] = false/true/table local modname_stack = {} -- []=string local module_gamevars = {} -- [] = { , , ... } +local module_gvlocali = {} -- [] = { , } local function getcurmodname(thisfuncname) if (#modname_stack == 0) then @@ -1379,6 +1380,24 @@ local function readintostr_mod(fn) end +local debug = require("debug") + +-- Get the number of active locals in the function that calls the function +-- calling this one. +local function getnumlocals(l) + -- 200 is the max. number of locals at one level + for i=1,200 do + -- level: + -- 0 is getlocal() itself. + -- 1 is this function. + -- 2 is the function calling getnumlocals() + -- 3 is the function calling that one. + if (debug.getlocal(3, i) == nil) then + return i-1 + end + end +end + local required_module_mt = { __newindex = function() -- TODO: allow gamevars to be nil? @@ -1420,6 +1439,13 @@ local function our_require(modname, ...) end module_gamevars[thismodname] = gvnames + local gvmodi = module_gvlocali[thismodname] + gvmodi[2] = getnumlocals() + + if (ffiC._DEBUG_LUNATIC ~= 0) then + printf("Module '%s' has %d locals, index %d to %d", thismodname, + gvmodi[2]-gvmodi[1]+1, gvmodi[1], gvmodi[2]) + end return end @@ -1500,6 +1526,8 @@ local function our_module() package_loaded[modname] = M -- change the environment of the function which called us: setfenv(2, M) + + module_gvlocali[modname] = { getnumlocals()+1 } end -- overridden 'error' that always passes a string to the base 'error'