1local MAKE_SNAPSHOT
2game._snapshots = {}
3stead.make_snapshot = function(nr)
4	if not stead.tonum(nr) then nr = 0 end
5	local h = { };
6	h.txt = ''
7	h.write = function(s, ...)
8		local a = {...};
9		for i = 1, stead.table.maxn(a) do
10			s.txt = s.txt .. stead.tostr(a[i]);
11		end
12	end
13	local old = game._snapshots; game._snapshots = nil
14	stead.do_savegame(game, h);
15	game._snapshots = old
16	game._snapshots[nr] = h.txt;
17end
18
19function isSnapshot(nr)
20	if not stead.tonum(nr) then nr = 0 end
21	return (game._snapshots[nr] ~= nil)
22end
23
24stead.restore_snapshot = function (nr)
25	if not stead.tonum(nr) then nr = 0 end
26	local ss = game._snapshots
27	if not ss[nr] then return nil, true end -- nothing todo
28	local i,v
29
30	if stead.api_atleast(1, 7, 1) then
31		stead.gamereset("main.lua", true);
32	else
33		stead.gamefile("main.lua", true);
34	end
35
36	local f, err = stead.eval(ss[nr]..' ');
37	if not f then return end
38	local i,r = f();
39	game._snapshots = ss
40	if r then
41		return nil, false
42	end
43
44	i = stead.do_ini(game, true);
45
46	if stead.api_atleast(1, 7, 1) then
47		i = game:start()
48		stead.rawset(_G, 'PLAYER_MOVED',  true) -- force fading
49	else -- legacy
50		if not game.showlast then
51			stead.last_disp(false)
52		end
53		i = stead.cat('', stead.last_disp())
54	end
55	stead.rawset(_G, 'RAW_TEXT', true)
56--	delete_snapshot(nr);
57	if stead.cctx() then
58		stead.pr(i)
59	end
60	return i;
61end
62
63stead.delete_snapshot = function(nr)
64	if not stead.tonum(nr) then nr = 0 end
65	game._snapshots[nr] = nil
66end
67
68function make_snapshot(nr)
69	if stead.type(nr) ~= 'number' then
70		nr = 0
71	end
72	MAKE_SNAPSHOT = nr
73end
74
75function restore_snapshot(nr)
76	return stead.restore_snapshot(nr)
77end
78
79function delete_snapshot(nr)
80	return stead.delete_snapshot(nr);
81end
82
83iface.cmd = stead.hook(iface.cmd, function(f, ...)
84	local r,v = f(...);
85	if MAKE_SNAPSHOT ~= nil then
86		stead.make_snapshot(MAKE_SNAPSHOT);
87		MAKE_SNAPSHOT = nil
88	end
89	return r,v
90end)
91-- vim:ts=4
92