kbd module, snapshots works

This commit is contained in:
p.kosyh 2010-07-06 08:14:42 +00:00
parent 066ede062e
commit b20da4d481
7 changed files with 110 additions and 48 deletions

View file

@ -22,6 +22,7 @@ install:
$(INSTALL) para.lua $(STEADPATH)/para.lua
$(INSTALL) quotes.lua $(STEADPATH)/quotes.lua
$(INSTALL) timer.lua $(STEADPATH)/timer.lua
$(INSTALL) kbd.lua $(STEADPATH)/kbd.lua
uninstall:

View file

@ -19,4 +19,5 @@ install:
copy quotes.lua ..\bin\stead
copy dash.lua ..\bin\stead
copy timer.lua ..\bin\stead
copy kbd.lua ..\bin\stead

29
stead/kbd.lua Normal file
View file

@ -0,0 +1,29 @@
game.action = stead.hook(game.action, function(f, s, cmd, ...)
if cmd == 'user_kbd' then
local r,v = call(game, 'kbd',
input.key_event.down, input.key_event.key);
if r == nil and v == nil then
return nil, true-- nothing to do
end
return r,v
end
return f(cmd, unpack(arg));
end)
input.key = stead.hook(input.key, function(f, s, down, key, ...)
local k,v
for k,v in ipairs(input.key_hooks) do
if v == key then
input.key_event = { key = key, down = down };
return 'user_kbd'
end
end
return f(s, down, key, unpack(arg))
end)
stead.module_init(function()
input.key_hooks = {}
end)
-- vim:ts=4

View file

@ -188,4 +188,4 @@ function path(w, wh) -- search in way, disabled too
end
game.lifes = list(game.lifes)
stead:init(); -- reinit
stead:init(); -- reinit ob

View file

@ -3,6 +3,14 @@ game._snapshots = {}
function make_snapshot(nr)
if not tonumber(nr) then nr = 0 end
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.write = function(s, ...)
local i
@ -13,6 +21,7 @@ function make_snapshot(nr)
local old = game._snapshots; game._snapshots = nil
for_each_object(save_object, h);
save_object('game', game, h);
for_everything(save_var, h);
clearvar(_G);
game._snapshots = old
game._snapshots[nr] = h.txt;
@ -25,9 +34,10 @@ function restore_snapshot(nr)
stead:init();
game.lifes:zap();
dofile('main.lua');
if type(init) == 'function' then -- no hooks here!!!
init();
end
game:ini()
-- if type(init) == 'function' then -- no hooks here!!!
-- init();
-- end
local f, err = loadstring(ss[nr]);
if not f then return end
local i,r = f();
@ -35,8 +45,12 @@ function restore_snapshot(nr)
if r then
return nil, false
end
i = do_ini(game);
i = do_ini(game, true);
RAW_TEXT = true
delete_snapshot(nr);
if cctx() then
pr(i)
end
return i;
end
@ -44,3 +58,5 @@ function delete_snapshot(nr)
if not tonumber(nr) then nr = 0 end
game._snapshots[nr] = nil
end
-- vim:ts=4

View file

@ -1800,7 +1800,6 @@ iface = {
if stead.state and r == nil and v == true then -- we do nothing
return nil;
end
if RAW_TEXT then
v = false
end
@ -1939,46 +1938,6 @@ function allocator_save(s, name, h, need)
savemembers(h, s, name, false);
end
allocator = obj {
nam = 'allocator',
get = function(s, n, c)
local v = ref(c);
if not v then
error ("Null object in allocator: "..tostring(c));
end
v.key_name = n;
v.save = allocator_save;
v.constructor = c;
return v
end,
delete = function(s, w)
w = ref(w);
if type(w.key_name) ~= 'string' then
return
end
local f = loadstring(w.key_name..'= nil;');
if f then
f();
end
end,
new = function(s, n)
local v = ref(n);
if type(v) ~= 'table' or type(n) ~= 'string' then
error ("Error in new.", 2);
end
v.save = allocator_save;
v.constructor = n;
stead.table.insert(s.objects, v);
v.key_name = 'allocator["objects"]['..stead.table.maxn(s.objects)..']';
return v
end,
objects = {
save = function(self, name, h, need)
savemembers(h, self, name, true);
end,
},
};
function new(str)
if type(str) ~= 'string' then
error("Non string constructor in new.", 2);
@ -2470,7 +2429,47 @@ function code(v)
end
stead.code = code
--- here the game begins
stead.init = function(s)
stead.objects = function(s)
allocator = obj {
nam = 'allocator',
get = function(s, n, c)
local v = ref(c);
if not v then
error ("Null object in allocator: "..tostring(c));
end
v.key_name = n;
v.save = allocator_save;
v.constructor = c;
return v
end,
delete = function(s, w)
w = ref(w);
if type(w.key_name) ~= 'string' then
return
end
local f = loadstring(w.key_name..'= nil;');
if f then
f();
end
end,
new = function(s, n)
local v = ref(n);
if type(v) ~= 'table' or type(n) ~= 'string' then
error ("Error in new.", 2);
end
v.save = allocator_save;
v.constructor = n;
stead.table.insert(s.objects, v);
v.key_name = 'allocator["objects"]['..stead.table.maxn(s.objects)..']';
return v
end,
objects = {
save = function(self, name, h, need)
savemembers(h, self, name, true);
end,
},
};
pl = player {
nam = "Incognito",
where = 'main',
@ -2480,7 +2479,11 @@ stead.init = function(s)
main = room {
nam = 'main',
dsc = 'No main room defined.',
}
};
end
stead.init = function(s)
stead:objects();
s.functions = {} -- code blocks
local k,v
for k,v in ipairs(s.modules_ini) do
@ -2488,3 +2491,4 @@ stead.init = function(s)
end
end
stead:init();
-- vim:ts=4

View file

@ -83,6 +83,17 @@ function(f, v, ...)
end)
stead.module_init(function()
local k,v
if type(variables) == 'nil' then
variables = {}
return
end
if type(variables) ~= 'table' then
return
end
for k,v in ipairs(variables) do
_G[v] = nil
end
variables = {}
end)