From b20da4d481c0a5ead8c6dfcbd2a6837a2d2b729c Mon Sep 17 00:00:00 2001 From: "p.kosyh" Date: Tue, 6 Jul 2010 08:14:42 +0000 Subject: [PATCH] kbd module, snapshots works --- stead/Makefile | 1 + stead/Makefile.windows | 1 + stead/kbd.lua | 29 ++++++++++++++ stead/object.lua | 2 +- stead/snapshot.lua | 24 +++++++++-- stead/stead.lua | 90 ++++++++++++++++++++++-------------------- stead/vars.lua | 11 ++++++ 7 files changed, 110 insertions(+), 48 deletions(-) create mode 100644 stead/kbd.lua diff --git a/stead/Makefile b/stead/Makefile index 3db7130..f6fb01f 100644 --- a/stead/Makefile +++ b/stead/Makefile @@ -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: diff --git a/stead/Makefile.windows b/stead/Makefile.windows index c77d125..3f01365 100644 --- a/stead/Makefile.windows +++ b/stead/Makefile.windows @@ -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 diff --git a/stead/kbd.lua b/stead/kbd.lua new file mode 100644 index 0000000..2211dd2 --- /dev/null +++ b/stead/kbd.lua @@ -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 + diff --git a/stead/object.lua b/stead/object.lua index a948876..34bc7dd 100644 --- a/stead/object.lua +++ b/stead/object.lua @@ -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 diff --git a/stead/snapshot.lua b/stead/snapshot.lua index 89fa104..e6573b7 100644 --- a/stead/snapshot.lua +++ b/stead/snapshot.lua @@ -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 + diff --git a/stead/stead.lua b/stead/stead.lua index d8c9c80..ce0326d 100644 --- a/stead/stead.lua +++ b/stead/stead.lua @@ -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 diff --git a/stead/vars.lua b/stead/vars.lua index 6c9b2fc..504c344 100644 --- a/stead/vars.lua +++ b/stead/vars.lua @@ -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)