steed/doc/examples/menu-demo/menu.lua

214 lines
3.9 KiB
Lua
Raw Normal View History

2009-09-20 13:37:00 +03:00
use_proxy = function(o)
local v = {};
v.proxy_type = true;
2010-09-22 10:39:42 +03:00
v.nam = '   '..call(ref(o), 'nam');
2009-09-27 16:12:29 +03:00
if inv():srch(ref(o)) then
v.nam = txtem(v.nam);
end
2009-09-20 13:37:00 +03:00
v.pobj = deref(o);
v.save = function(self, name, h, need)
if need then
2010-09-22 10:39:42 +03:00
h:write(stead.string.format(name.." = use_proxy(%q);\n", tostring(self.pobj)));
2009-09-20 13:37:00 +03:00
end
savemembers(h, self, name,false);
end
if ref(o).use ~= nil then
v.use = function(s, w)
if ref(w).proxy_type then
2010-07-05 19:19:29 +03:00
local v,r = call(ref(s.pobj), 'use', ref(w.pobj));
2009-09-20 13:37:00 +03:00
-- where(s):gen();
return v,r;
end
end
v.inv = function(s)
local v,r = call(ref(s.pobj), 'use', nil);
-- where(s):gen();
return v,r;
end
end
if ref(o).used ~= nil then
v.used = function(s, w)
if ref(w).proxy_type then
2010-07-05 19:19:29 +03:00
local v,r = call(ref(s.pobj), 'used', ref(w.pobj));
2009-09-20 13:37:00 +03:00
-- where(s):gen();
return v,r;
end
end
end
return obj(v)
end
act_proxy = function(o, act)
local v = {};
v.proxy_type = true;
2010-09-22 10:39:42 +03:00
v.nam = '   '..call(ref(o), 'nam');
2009-09-20 13:37:00 +03:00
v.pobj = deref(o);
v.pact = act;
v.save = function(self, name, h, need)
if need then
2010-09-22 10:39:42 +03:00
h:write(stead.string.format(name.." = act_proxy(%q, %q);\n", self.pobj, self.pact));
2009-09-20 13:37:00 +03:00
end
savemembers(h, self, name,false);
end
if ref(o)[act] ~= nil then
v.inv = function(s)
local v, r;
v,r = call(ref(s.pobj), act);
-- where(s):gen();
return v,r;
end
end
return menu(v)
end
fill_objs = function(s, w, act)
local ii,i, o
for i,o,ii in opairs(objs(w)) do
o = ref(o);
if isObject(o) and not isDisabled(o) and o ~= s and not isPhrase(o) then
2009-09-26 18:51:48 +03:00
local n = deref(o)
if type(n) ~= 'string' then
n = deref(w)..".obj["..tonumber(ii).."]";
end
2009-09-20 13:37:00 +03:00
if act == "use" then
2009-09-26 18:51:48 +03:00
put(use_proxy(n), s);
2009-09-20 13:37:00 +03:00
else
2009-09-26 18:51:48 +03:00
put(act_proxy(n, act), s);
2009-09-20 13:37:00 +03:00
end
fill_objs(s, o, act);
end
end
end
fill_inv = function(s, w, act)
local i, o
2009-09-20 16:43:53 +03:00
local rc = false
2009-09-20 13:37:00 +03:00
for i,o in opairs(w) do
o = ref(o);
if isObject(o) and not isDisabled(o)
and not o.proxy_type
and not isStatus(o)
and s ~= o and not o.action_type then
2009-09-26 18:51:48 +03:00
local n = deref(o)
if type(n) ~= 'string' then
n = deref(w)..".obj["..tonumber(ii).."]";
end
2009-09-20 13:37:00 +03:00
if act == "use" then
2009-09-26 18:51:48 +03:00
put(use_proxy(n), s);
2009-09-20 13:37:00 +03:00
else
2009-09-26 18:51:48 +03:00
put(act_proxy(n, act), s);
2009-09-20 13:37:00 +03:00
end
fill_inv(s, o.obj, act);
2009-09-20 16:43:53 +03:00
rc = true
2009-09-20 13:37:00 +03:00
end
end
2009-09-20 16:43:53 +03:00
return rc
2009-09-20 13:37:00 +03:00
end
select_only = function(s)
local k, o, i
for k,o in opairs(me().obj) do
o = ref(o)
2010-07-05 19:19:29 +03:00
if o.action_type and o._state and o ~= s then
2009-09-20 13:37:00 +03:00
o:inv();
end
end
2009-09-25 11:21:58 +03:00
obj_tag(me(), MENU_TAG_ID);
2009-09-20 13:37:00 +03:00
end
2009-09-20 16:43:53 +03:00
actmenu = function(nam, act, _scene, _inv, _ifinvonly)
2009-09-20 13:37:00 +03:00
local v = { };
v.action_type = true;
2010-07-05 19:19:29 +03:00
v._state = false;
2009-09-20 13:37:00 +03:00
v._nam = nam;
v.nam = function(s)
2010-07-05 19:19:29 +03:00
if s._state then
2009-09-20 13:37:00 +03:00
return txtu(s._nam);
end
return s._nam;
end
v._scene = _scene;
v._inv = _inv;
v.gen = function(s)
local k,o,i
2009-09-20 16:43:53 +03:00
local rc = false
2009-09-20 13:37:00 +03:00
s.obj:zap();
if s._inv then
2009-09-20 16:43:53 +03:00
rc =fill_inv(s, inv(), act);
2009-09-20 13:37:00 +03:00
end
2009-09-20 16:43:53 +03:00
if not _ifinvonly or rc then
if s._scene then
fill_objs(s, here(), act);
end
2009-09-20 13:37:00 +03:00
end
select_only(s);
end
v.inv = function(s)
local i,o
local k,v
2010-07-05 19:19:29 +03:00
if not s._state then
2009-09-20 13:37:00 +03:00
s:gen();
2010-07-05 19:19:29 +03:00
s._state = true;
2009-09-20 13:37:00 +03:00
else
2010-07-05 19:19:29 +03:00
s._state = false;
2009-09-20 13:37:00 +03:00
s.obj:zap();
end
2009-09-25 11:21:58 +03:00
return nil, true -- to say instead, do not redraw scene, only inv ;)
2009-09-20 13:37:00 +03:00
end
return menu(v);
end
function gen_actions(s)
local k, o
for k,o in opairs(me().obj) do
o = ref(o)
2010-07-05 19:19:29 +03:00
if o.action_type and o._state then
2009-09-20 13:37:00 +03:00
o:gen();
end
end
end
pocket = function(nam)
local v = {}
v.action_type = true;
2010-07-05 19:19:29 +03:00
v._state = false;
2009-09-20 13:37:00 +03:00
v._nam = nam;
v.nam = function(s)
2010-07-05 19:19:29 +03:00
if s._state then
2009-09-20 13:37:00 +03:00
return txtu(s._nam);
end
return s._nam;
end;
v.gen = function(s)
s.obj:zap();
fill_inv(s, inv(), 'act');
-- put(act_proxy(o, 'inv'), s);
-- s.obj:cat(s.robj);
select_only(s);
end;
v.inv = function(s)
2010-07-05 19:19:29 +03:00
if not s._state then
2009-09-20 13:37:00 +03:00
s:gen();
2010-07-05 19:19:29 +03:00
s._state = true;
2009-09-20 13:37:00 +03:00
else
s.obj:zap();
2010-07-05 19:19:29 +03:00
s._state = false;
2009-09-20 13:37:00 +03:00
end
return nil,true
end;
v.robj = list {};
return menu(v);
end
menu_init = function(s)
pl.inv = function(s)
gen_actions(s);
return player_inv(s);
end
end