steed/stead/dbg.lua

229 lines
5.3 KiB
Lua
Raw Normal View History

2010-06-14 14:41:55 +03:00
-- add this: reuire "dbg"
-- in your project
-- for debug tools
2010-06-14 15:55:29 +03:00
require "input"
2010-06-14 16:19:37 +03:00
2010-06-16 08:05:23 +03:00
function disp_obj()
local v = obj{
nam = 'disp',
dsc = function(s)
local r = s._txt
s._txt = nil;
return r
end
}
return v;
end
2010-06-15 21:58:52 +03:00
dump_obj = function(w)
w = ref(w)
if type(w) ~= 'table' then
2010-06-16 08:05:23 +03:00
seen('disp')._txt = '^^No such object.';
return true
2010-06-15 21:58:52 +03:00
end
local i,o
local rc=''
for i,o in pairs(w) do
if rc ~='' then rc = rc..'^' end
rc = cat(rc, par(' ', 'Key:'..tostring(i),
2010-06-15 22:51:46 +03:00
'Val:'..tostring(deref(o))));
2010-06-15 21:58:52 +03:00
end
2010-06-16 08:05:23 +03:00
seen('disp')._txt = cat('^^', rc)
return true;
2010-06-15 21:58:52 +03:00
end
2010-06-14 16:19:37 +03:00
list_objects = function()
local i,o
2010-06-15 21:58:52 +03:00
local rc = par(' ', 'Room:'..tostring(deref(from())),
'Nam:'..tostring(call(from(),'nam')));
2010-06-14 16:19:37 +03:00
for i,o in opairs(objs(from())) do
2010-06-15 21:58:52 +03:00
rc = rc..'^';
o = ref(o)
rc = cat(rc, par(' ', 'Id:'..tostring(o.id),
'Obj:'..tostring(deref(o)),
'Nam:'..tostring(call(o, 'nam')),
'Disabled:'..tostring(isDisabled(o))));
end
2010-06-16 08:05:23 +03:00
seen('disp')._txt = rc
2010-06-15 21:58:52 +03:00
return rc
end
list_inv = function()
local i,o
local rc=''
for i,o in opairs(inv()) do
if rc ~='' then rc = rc..'^' end
2010-06-14 16:19:37 +03:00
o = ref(o)
2010-06-15 21:58:52 +03:00
rc = cat(rc, par(' ', 'Id:'..tostring(o.id), 'Obj:'..tostring(deref(o)),
'Nam:'..tostring(call(o, 'nam')),
'Disabled:'..tostring(isDisabled(o)),
'Taken:'..tostring(taken(o))));
2010-06-14 16:19:37 +03:00
end
2010-06-15 21:58:52 +03:00
if rc == '' then return end
2010-06-16 08:05:23 +03:00
seen('disp')._txt = rc
2010-06-14 16:19:37 +03:00
return rc
end
2010-06-14 15:55:29 +03:00
execute_cmd = room {
nam = "Execute Lua code",
debug = true,
forcedsc = true,
dsc = "Enter Lua code here to exec.",
inp_enter = function(s)
if type(s.obj[1]._txt) == 'string' then
local f = loadstring(s.obj[1]._txt);
if f then
2010-06-16 08:05:23 +03:00
seen('disp')._txt = cat('^^', f());
return true
2010-06-14 15:55:29 +03:00
end
2010-06-16 08:05:23 +03:00
seen('disp')._txt = "^^Error in exec.";
return true
2010-06-14 15:55:29 +03:00
end
return back();
end,
2010-06-14 16:01:19 +03:00
act = function(s, w)
return back();
end,
2010-06-15 21:58:52 +03:00
obj = { inp('{Enter cmd}: ', 'return "Hello World!"'),
2010-06-16 08:05:23 +03:00
vobj(1, 'Back', '^{Back}'),
new [[ disp_obj() ]],
}
2010-06-15 21:58:52 +03:00
}
dump_object = room {
nam = "Dump object",
debug = true,
forcedsc = true,
dsc = "Enter object name here to dump.",
inp_enter = function(s)
if type(s.obj[1]._txt) == 'string' then
return dump_obj(s.obj[1]._txt);
end
return back();
end,
act = function(s, w)
2010-06-15 22:51:46 +03:00
if w == 1 then
return back();
elseif w == 2 then
return dump_obj(from(from()));
elseif w == 3 then
return dump_obj(me());
elseif w == 4 then
return dump_obj(game.lifes);
elseif w == 5 then
return dump_obj(ways(from(from())));
end
2010-06-15 21:58:52 +03:00
end,
obj = { inp('{Enter object}: ', 'main'),
2010-06-15 22:51:46 +03:00
vobj(2, 'Here', '^{Dump here}'),
vobj(3, 'Player', '^{Dump player}'),
vobj(4, 'Lifes', '^{Dump lifes}'),
vobj(5, 'Ways', '^{Dump ways}'),
2010-06-16 08:05:23 +03:00
vobj(1, 'Back', '^{Back}'),
new[[ disp_obj() ]]}
2010-06-14 15:55:29 +03:00
}
2010-06-14 14:41:55 +03:00
choose_location = dlg {
debug = true,
2010-06-14 14:54:21 +03:00
forcedsc = true,
2010-06-14 14:41:55 +03:00
nam = 'Go to',
dsc = 'Select location.',
gen = function(s)
local k,v
objs(s):zap();
for k,v in pairs(_G) do
if isRoom(v) and not v.debug then
local n = call(v, 'nam');
2010-06-14 14:54:21 +03:00
put(phr(n, true, [[return goto("]]..k..[[")]]), s);
2010-06-14 14:41:55 +03:00
end
end
2010-06-14 15:55:29 +03:00
put (phr('Back',true, 'return back()'), s)
2010-06-14 14:41:55 +03:00
end
}
choose_object = dlg {
debug = true,
2010-06-14 14:54:21 +03:00
forcedsc = true,
2010-06-14 14:41:55 +03:00
nam = 'Get object',
dsc = 'Select object to get.',
gen = function(s)
local k,v
objs(s):zap();
for k,v in pairs(_G) do
if isObject(v) and not isRoom(v) and not isPlayer(v) and not v.debug and not have(v) and not isStatus(v) then
local n = call(v, 'nam');
2010-06-14 14:54:21 +03:00
put(phr(n, true, k..':enable(); return take("'..k..'")'), s);
2010-06-14 14:41:55 +03:00
end
end
2010-06-14 15:55:29 +03:00
put (phr('Back',true, 'return back()'), s)
2010-06-14 14:41:55 +03:00
end
}
drop_object = dlg {
debug = true,
2010-06-14 14:54:21 +03:00
forcedsc = true,
2010-06-14 14:41:55 +03:00
nam = 'Drop object',
dsc = 'Select object to drop.',
gen = function(s)
local k,v
objs(s):zap();
for k,v in pairs(_G) do
if isObject(v) and not isRoom(v) and not isPlayer(v) and not v.debug and have(v) then
local n = call(v, 'nam');
2010-06-14 14:54:21 +03:00
put (phr(k, true, 'drop("'..k..'","'..deref(from())..'")'), s)
2010-06-14 14:41:55 +03:00
end
end
2010-06-14 15:55:29 +03:00
put (phr('Back', true, 'return back()'), s)
2010-06-14 14:41:55 +03:00
end
}
2010-06-15 21:58:52 +03:00
function dbg_exit()
local r = call(from(), 'dsc');
return par ('^^', back(), r);
end
2010-06-14 14:41:55 +03:00
debug_dlg = dlg {
2010-06-14 14:54:21 +03:00
debug = true,
forcedsc = true,
2010-06-14 14:41:55 +03:00
nam = 'Debug Tool',
dsc = 'Select tool.',
obj = {
2010-06-14 15:55:29 +03:00
phr('Go to location...', true, [[pon(); choose_location:gen(); return goto('choose_location')]]),
phr('Get object...', true, [[pon(); choose_object:gen(); return goto('choose_object')]]),
phr('Put object...', true, [[pon(); drop_object:gen(); return goto('drop_object')]]),
2010-06-14 16:19:37 +03:00
phr('Current scene...', true, [[pon(); return list_objects();]]),
2010-06-15 21:58:52 +03:00
phr('Inventory...', true, [[pon(); return list_inv();]]),
2010-06-15 22:51:46 +03:00
phr('Dump object...', true, [[pon(); return goto(dump_object);]]),
phr('Exec Lua string...', true, [[pon(); return goto('execute_cmd')]]),
2010-06-15 21:58:52 +03:00
phr('Exit',true , [[pon(); return dbg_exit()]]),
2010-06-16 08:05:23 +03:00
new [[ disp_obj ]]
2010-06-14 14:41:55 +03:00
},
};
debug_tool = menu {
debug = true,
2010-06-14 14:54:21 +03:00
forcedsc = true,
2010-06-14 14:41:55 +03:00
nam = txtb('debug'),
inv = function(s)
debug_dlg.__from__ = deref(here());
me().where = 'debug_dlg'; -- force to go
2010-06-14 16:26:28 +03:00
local r = par('^^', call(debug_dlg, 'enter'), call(debug_dlg, 'dsc'));
2010-06-14 14:41:55 +03:00
return r;
2010-06-15 21:58:52 +03:00
end,
2010-06-14 14:41:55 +03:00
};
2010-06-15 21:58:52 +03:00
game.action = hook(game.action,
function (f, s, cmd, ...)
if cmd == 'use_debug' then
return debug_tool:inv()
end
2010-06-16 08:05:23 +03:00
return f(s, cmd, unpack(arg))
2010-06-15 21:58:52 +03:00
end)
input.key = hook(input.key,
function(f, s, down, key, ...)
if not here().debug and down and key == 'f7' then return 'use_debug' end
2010-06-16 08:05:23 +03:00
return f(s, down, key, unpack(arg))
2010-06-15 21:58:52 +03:00
end)
2010-06-14 14:41:55 +03:00
putf('debug_tool', me());