This repository has been archived on 2019-04-06. You can view files and clone it, but cannot push or open issues or pull requests.
adventin/stead/xobj.lua

106 lines
2.2 KiB
Lua
Raw Normal View History

2010-06-25 12:31:54 +03:00
function isXobject(s)
return type(s) == 'table' and s.xobject_type;
end
__hook_act = function(v, n)
local k,o
if type(v.var) == 'table' then
for k,o in ipairs(v.var) do -- var add
if o == n then
stead.table.insert(v.var, 'x'..n);
break
2010-06-25 12:31:54 +03:00
end
end
end
return function(s, ...)
local r, v = call(s, 'x'..n, unpack(arg));
if type(r) == 'string' then
r = do_xact(s, r);
2010-06-25 12:31:54 +03:00
end
return r,v
2010-06-07 16:06:00 +03:00
end
end
xobj = stead.inherit(obj, function(v) -- object without name and dsc, but with xact attribute
2010-06-25 12:31:54 +03:00
v.xdsc = v.dsc
v.dsc = __hook_act(v, 'dsc');
2010-06-07 16:06:00 +03:00
v.xact = v.act
v.act = __hook_act(v, 'act');
2010-06-25 12:31:54 +03:00
v.xinv = v.inv
v.inv = __hook_act(v, 'inv');
2010-06-25 12:31:54 +03:00
v.xtak = v.tak
v.tak = __hook_act(v, 'tak');
2010-06-25 12:31:54 +03:00
v.xuse = v.use
v.use = __hook_act(v, 'use');
2010-06-25 12:31:54 +03:00
v.xused = v.used
v.used = __hook_act(v, 'used');
2010-06-25 12:31:54 +03:00
v.xobject_type = true
return v
end)
2010-06-07 16:06:00 +03:00
2010-06-25 12:31:54 +03:00
xact = function(n, f) -- just simple action!
2010-06-07 16:06:00 +03:00
local v = {};
2010-06-25 12:31:54 +03:00
if type(n) ~= 'string' or (type(f) ~= 'string' and not isCode(f)) then
error ("Wrong parameter to xact.", 2)
end
v.nam = n
v.act = f;
2010-06-07 16:06:00 +03:00
v = xobj(v);
2010-06-25 12:31:54 +03:00
v.save = function(self, name, h, need)
if need then
local f = self.xact;
if isCode(f) then
f = stead.string.format("code %q", stead.functions[f].code);
else
f = stead.string.format("%q", f);
end
h:write(stead.string.format("%s = xact(%q, %s);\n", name, self.nam, f))
end
savemembers(h, self, name, false);
end
2010-06-07 16:06:00 +03:00
return v
end
2010-06-25 12:31:54 +03:00
do_xact = function(self, str)
local aarg = {}
local function parg(v)
stead.table.insert(aarg, v);
return ''
end
2010-06-07 16:06:00 +03:00
local xrefrep = function(str)
local s = stead.string.gsub(str,'[{}]','');
2010-06-25 12:31:54 +03:00
local o,d, a;
local i = s:find(":", 1, true);
aarg = {}
if i then
o = s:sub(1, i - 1);
d = s:sub(i + 1);
i = o:find("(", 1, true);
if i then
a = o:sub(i);
o = o:sub(1, i - 1);
a:gsub('[^,()]+', parg);
end
if o == '' then o = self end
else
o = self
d = s;
end
local oo = objs():srch(o)
2010-06-25 12:31:54 +03:00
if not oo then
oo = ref(o)
2010-06-25 12:31:54 +03:00
end
return xref(d, ref(oo), unpack(aarg));
2010-06-07 16:06:00 +03:00
end
if type(str) ~= 'string' then return end
2010-06-26 22:27:15 +03:00
me():tag()
2010-06-07 16:06:00 +03:00
local s = stead.string.gsub(str,'{[^}]+}', xrefrep);
return s;
end
2010-06-25 12:31:54 +03:00
xroom = stead.inherit(room, function(v)
v.xdsc = v.dsc
v.dsc = __hook_act(v, 'dsc');
2010-06-07 16:06:00 +03:00
return v;
2010-06-25 12:31:54 +03:00
end)