This repository has been archived on 2019-04-06. You can view files and clone it, but cannot push or open issues or pull requests.
adventin/stead/snapshots.lua

78 lines
1.5 KiB
Lua
Raw Normal View History

2010-06-19 13:14:25 +03:00
game._snapshots = {}
2010-07-14 20:59:49 +03:00
stead.make_snapshot = function(nr)
2010-06-19 13:14:25 +03:00
if not tonumber(nr) then nr = 0 end
local h = { };
h.txt = ''
h.write = function(s, ...)
local i
2011-02-23 12:11:27 +02:00
local a = {...};
for i = 1, stead.table.maxn(a) do
s.txt = s.txt .. tostring(a[i]);
2010-06-19 13:14:25 +03:00
end
end
local old = game._snapshots; game._snapshots = nil
2010-07-14 11:31:44 +03:00
do_savegame(game, h);
2010-06-19 13:14:25 +03:00
game._snapshots = old
game._snapshots[nr] = h.txt;
end
2010-07-06 12:18:25 +03:00
function isSnapshot(nr)
if not tonumber(nr) then nr = 0 end
return (game._snapshots[nr] ~= nil)
end
2010-07-14 20:59:49 +03:00
stead.restore_snapshot = function (nr)
2010-06-19 13:14:25 +03:00
if not tonumber(nr) then nr = 0 end
local ss = game._snapshots
if not ss[nr] then return nil, true end -- nothing todo
2010-07-14 17:22:32 +03:00
local i,v
2010-07-15 09:59:57 +03:00
stead.gamefile("main.lua", true);
2010-07-14 21:53:42 +03:00
2010-09-15 13:09:45 +03:00
local f, err = loadstring(ss[nr]..' ');
2010-06-19 13:14:25 +03:00
if not f then return end
local i,r = f();
game._snapshots = ss
if r then
return nil, false
end
2010-07-06 11:14:42 +03:00
i = do_ini(game, true);
2010-06-19 13:14:25 +03:00
RAW_TEXT = true
2010-07-15 14:26:50 +03:00
-- delete_snapshot(nr);
2010-07-06 11:14:42 +03:00
if cctx() then
pr(i)
end
2010-06-19 13:14:25 +03:00
return i;
end
2010-07-14 20:59:49 +03:00
stead.delete_snapshot = function(nr)
2010-06-19 13:14:25 +03:00
if not tonumber(nr) then nr = 0 end
game._snapshots[nr] = nil
end
2010-07-14 20:59:49 +03:00
function make_snapshot(nr)
if type(nr) ~= 'number' then
nr = 0
end
MAKE_SNAPSHOT = nr
end
function restore_snapshot(nr)
return stead.restore_snapshot(nr)
end
function delete_snapshot(nr)
return stead.delete_snapshot(nr);
end
iface.cmd = stead.hook(iface.cmd, function(f, ...)
2011-02-23 12:11:27 +02:00
local r,v = f(...);
2010-07-14 20:59:49 +03:00
if MAKE_SNAPSHOT ~= nil then
stead.make_snapshot(MAKE_SNAPSHOT);
MAKE_SNAPSHOT = nil
end
if v == nil then return r end
return r,v
end)
2010-07-06 11:14:42 +03:00
-- vim:ts=4