gamefile hack
This commit is contained in:
parent
6a91f15706
commit
422b5da0be
|
@ -3,14 +3,6 @@ game._snapshots = {}
|
||||||
function make_snapshot(nr)
|
function make_snapshot(nr)
|
||||||
if not tonumber(nr) then nr = 0 end
|
if not tonumber(nr) then nr = 0 end
|
||||||
local h = { };
|
local h = { };
|
||||||
local function save_object(key, value, h)
|
|
||||||
savevar(h, value, key, false);
|
|
||||||
return true;
|
|
||||||
end
|
|
||||||
local function save_var(key, value, h)
|
|
||||||
savevar(h, value, key, isForSave(key, value, _G))
|
|
||||||
end
|
|
||||||
|
|
||||||
h.txt = ''
|
h.txt = ''
|
||||||
h.write = function(s, ...)
|
h.write = function(s, ...)
|
||||||
local i
|
local i
|
||||||
|
@ -19,10 +11,7 @@ function make_snapshot(nr)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
local old = game._snapshots; game._snapshots = nil
|
local old = game._snapshots; game._snapshots = nil
|
||||||
for_each_object(save_object, h);
|
do_savegame(game, h);
|
||||||
save_object('game', game, h);
|
|
||||||
for_everything(save_var, h);
|
|
||||||
clearvar(_G);
|
|
||||||
game._snapshots = old
|
game._snapshots = old
|
||||||
game._snapshots[nr] = h.txt;
|
game._snapshots[nr] = h.txt;
|
||||||
end
|
end
|
||||||
|
@ -36,10 +25,7 @@ function restore_snapshot(nr)
|
||||||
if not tonumber(nr) then nr = 0 end
|
if not tonumber(nr) then nr = 0 end
|
||||||
local ss = game._snapshots
|
local ss = game._snapshots
|
||||||
if not ss[nr] then return nil, true end -- nothing todo
|
if not ss[nr] then return nil, true end -- nothing todo
|
||||||
stead:init();
|
gamefile("main.lua"); stead.pclr();
|
||||||
game.lifes:zap();
|
|
||||||
dofile('main.lua');
|
|
||||||
game:ini()
|
|
||||||
-- if type(init) == 'function' then -- no hooks here!!!
|
-- if type(init) == 'function' then -- no hooks here!!!
|
||||||
-- init();
|
-- init();
|
||||||
-- end
|
-- end
|
||||||
|
|
|
@ -1634,18 +1634,40 @@ function savevar (h, v, n, need)
|
||||||
h:write("\n")
|
h:write("\n")
|
||||||
end
|
end
|
||||||
|
|
||||||
|
function gamefile(file)
|
||||||
|
stead:init();
|
||||||
|
game.lifes:zap();
|
||||||
|
dofile(file);
|
||||||
|
game:ini();
|
||||||
|
if file == 'main.lua' then -- legacy???
|
||||||
|
file = nil
|
||||||
|
end
|
||||||
|
game._script = file;
|
||||||
|
return goto(here())
|
||||||
|
end
|
||||||
|
|
||||||
|
function do_savegame(s, h)
|
||||||
function game_save(self, name, file)
|
|
||||||
local h;
|
|
||||||
local function save_object(key, value, h)
|
local function save_object(key, value, h)
|
||||||
savevar(h, value, key, false);
|
savevar(h, value, key, false);
|
||||||
return true;
|
return true;
|
||||||
end
|
end
|
||||||
|
|
||||||
local function save_var(key, value, h)
|
local function save_var(key, value, h)
|
||||||
savevar(h, value, key, isForSave(key, value, _G))
|
savevar(h, value, key, isForSave(key, value, _G))
|
||||||
end
|
end
|
||||||
|
if s._script then
|
||||||
|
h:write(stead.string.format("gamefile(%q)\n",
|
||||||
|
s._script))
|
||||||
|
end
|
||||||
|
save_object('allocator', allocator, h); -- always first!
|
||||||
|
for_each_object(save_object, h);
|
||||||
|
save_object('game', self, h);
|
||||||
|
for_everything(save_var, h);
|
||||||
|
-- save_object('_G', _G, h);
|
||||||
|
clearvar(_G);
|
||||||
|
end
|
||||||
|
|
||||||
|
function game_save(self, name, file)
|
||||||
|
local h;
|
||||||
|
|
||||||
if file ~= nil then
|
if file ~= nil then
|
||||||
file:write(name..".pl = '"..deref(self.pl).."'\n");
|
file:write(name..".pl = '"..deref(self.pl).."'\n");
|
||||||
|
@ -1668,12 +1690,7 @@ function game_save(self, name, file)
|
||||||
if type(n) == 'string' and n ~= "" then
|
if type(n) == 'string' and n ~= "" then
|
||||||
h:write("-- $Name: "..n:gsub("\n","\\n").."$\n");
|
h:write("-- $Name: "..n:gsub("\n","\\n").."$\n");
|
||||||
end
|
end
|
||||||
save_object('allocator', allocator, h); -- always first!
|
do_savegame(self, h);
|
||||||
for_each_object(save_object, h);
|
|
||||||
save_object('game', self, h);
|
|
||||||
for_everything(save_var, h);
|
|
||||||
-- save_object('_G', _G, h);
|
|
||||||
clearvar(_G);
|
|
||||||
h:flush();
|
h:flush();
|
||||||
h:close();
|
h:close();
|
||||||
game.autosave = false; -- we have only one try for autosave
|
game.autosave = false; -- we have only one try for autosave
|
||||||
|
|
Reference in a new issue