From 77574e30b933929deefd14c49f70482d09116a54 Mon Sep 17 00:00:00 2001 From: helixhorned Date: Fri, 8 Nov 2013 18:08:37 +0000 Subject: [PATCH] LunaCON: implement NORESET flag for gamevars. git-svn-id: https://svn.eduke32.com/eduke32@4140 1a8010ca-5511-0410-912e-c29ae57300e0 --- polymer/eduke32/source/lunatic/defs.ilua | 5 +++- polymer/eduke32/source/lunatic/lunacon.lua | 32 +++++++++++++++------ polymer/eduke32/source/lunatic/savegame.lua | 24 +++++++++++++--- 3 files changed, 47 insertions(+), 14 deletions(-) diff --git a/polymer/eduke32/source/lunatic/defs.ilua b/polymer/eduke32/source/lunatic/defs.ilua index 7ce712ef2..52fcaf0d8 100644 --- a/polymer/eduke32/source/lunatic/defs.ilua +++ b/polymer/eduke32/source/lunatic/defs.ilua @@ -2261,10 +2261,13 @@ do -- Handle global gamevars first. for i=1,#modvars do local varname = modvars[i] + local excludedVars = modname==CON_MODULE_NAME and varname=="_V" and + package_loaded[CON_MODULE_NAME]._V._IS_NORESET_GAMEVAR or nil + -- Serialize gamevar named 'varname' from module named 'modname'. -- XXX: May error. This will terminate EDuke32 since this callback -- is run unprotected. - if (sb:add("M."..varname, package_loaded[modname][varname])) then + if (sb:add("M."..varname, package_loaded[modname][varname], excludedVars)) then -- We couldn't serialize that gamevar. slenptr[0] = -1 -- Signal which gamevar that was. diff --git a/polymer/eduke32/source/lunatic/lunacon.lua b/polymer/eduke32/source/lunatic/lunacon.lua index b8243f877..f1dd2976f 100644 --- a/polymer/eduke32/source/lunatic/lunacon.lua +++ b/polymer/eduke32/source/lunatic/lunacon.lua @@ -827,6 +827,10 @@ local function check_sysvar_def_attempt(identifier) errprintf("cannot define reserved symbol `actorvar'") return true end + if (identifier=="_IS_NORESET_GAMEVAR") then + errprintf("cannot define reserved symbol `_IS_NORESET_GAMEVAR'") + return true + end end local Define = {} @@ -1391,11 +1395,6 @@ function Cmd.gamevar(identifier, initval, flags) return end - if (bit.band(flags, GVFLAG.NORESET) ~= 0) then - warnprintf("gamevar \"%s\" flag NORESET (131072): not yet implemented", - identifier) - end - local perPlayer = (bit.band(flags, GVFLAG.PERPLAYER) ~= 0) local perActor = (bit.band(flags, GVFLAG.PERACTOR) ~= 0) @@ -1406,12 +1405,14 @@ function Cmd.gamevar(identifier, initval, flags) local ogv = g_gamevar[identifier] local isSessionVar = (bit.band(flags, GVFLAG.NODEFAULT) ~= 0) + local storeWithSavegames = (bit.band(flags, GVFLAG.NORESET) == 0) if (isSessionVar and (perPlayer or perActor)) then if (ogv == nil) then -- warn only once per gamevar - warnprintf("per-%s for session gamevars: not yet implemented (made into %s)", + warnprintf("per-%s session gamevar `%s': NYI, made %s", perPlayer and "player" or "actor", - perPlayer and "global" or "plain") + identifier, + perPlayer and "global" or "non-session") end if (perActor) then @@ -1490,7 +1491,9 @@ function Cmd.gamevar(identifier, initval, flags) local gv = { name=mangle_name(identifier, "V"), flags=flags } g_gamevar[identifier] = gv - addcode("if _S then") + if (storeWithSavegames) then + addcode("if _S then") + end if (perActor) then addcodef("%s=_con.actorvar(%d)", gv.name, initval) @@ -1501,7 +1504,9 @@ function Cmd.gamevar(identifier, initval, flags) addcodef("%s=%d", gv.name, initval) end - addcode("end") + if (storeWithSavegames) then + addcode("end") + end end function Cmd.dynamicremap() @@ -3549,6 +3554,15 @@ end -- : Get line info? local function get_code_string(codetab, lineinfop) + -- Create meta-info gamevar: which gamevars have bit NORESET set? + codetab[#codetab+1] = "_V._IS_NORESET_GAMEVAR={" + for identifier, gv in pairs(g_gamevar) do + if (bit.band(gv.flags, GVFLAG.NORESET) ~= 0) then + codetab[#codetab+1] = format("[%q]=true,", identifier) + end + end + codetab[#codetab+1] = "}" + -- Return defined labels in a table... codetab[#codetab+1] = "return {" for label, val in pairs(g_labeldef) do diff --git a/polymer/eduke32/source/lunatic/savegame.lua b/polymer/eduke32/source/lunatic/savegame.lua index b1ce23583..c8b63e4c5 100644 --- a/polymer/eduke32/source/lunatic/savegame.lua +++ b/polymer/eduke32/source/lunatic/savegame.lua @@ -66,6 +66,7 @@ local savebuffer_mt = { --== vvv INTERNAL vvv ===--- resetRefs = function(self) self.numrefs = 0 + self.recursionDepth = 0 self.val2ref = {} self.havereq = {} end, @@ -92,8 +93,10 @@ local savebuffer_mt = { -- Add an entry of Lua object that can be referenced by -- on the left hand side of an assignment. + -- : if non-nil, should be table + -- [] = true. See EXCLUDE_KEYS below. -- Returns 'true' if cannot be serialized. - add = function(self, lhscode, value) + add = function(self, lhscode, value, excludedKeys) local valcode = basicSerialize(value) if (valcode == nil) then @@ -129,13 +132,25 @@ local savebuffer_mt = { return true end + -- EXCLUDE_KEYS + local isNORESET = (self.recursionDepth == 0 and + excludedKeys and excludedKeys[k]) + -- Generate the name under which the table element -- is referenced. local refcode2 = format("%s[%s]", refcode, keystr) - -- Recurse! - if (self:add(refcode2, v)) then - return true + if (isNORESET) then + self:emitAssign(refcode2, format("M._V[%s]", keystr)) + else + -- Recurse! + self.recursionDepth = self.recursionDepth+1 + local ret = self:add(refcode2, v) + self.recursionDepth = self.recursionDepth-1 + + if (ret) then + return true + end end end @@ -187,6 +202,7 @@ function savebuffer() -- .strbuf: array of Lua code pieces local sb = { numrefs=0, val2ref={}, havereq={}, + recursionDepth = 0, strbuf=sb_get_initial_strbuf() }