steed/stead/goto.lua

218 lines
4.4 KiB
Lua
Raw Normal View History

2010-07-12 11:01:11 +03:00
go = function (self, where, back, noenter, noexit, nodsc)
2010-06-07 13:18:44 +03:00
local was = self.where;
local need_scene = false;
local ret
if not stead.in_goto_call then
ret = function(rc) stead.in_goto_call = false return nil end
else
ret = function(rc) return rc end
end
stead.in_goto_call = true
if where == nil then
return nil, ret(false)
end
if not isRoom(ref(where)) then
error ("Trying to go nowhere: "..where);
end
if not isRoom(ref(self.where)) then
error ("Trying to go from nowhere: "..self.where);
end
if stead.in_entered_call or stead.in_onexit_call then
error ("Do not use goto from left/entered action! Use exit/enter action instead:" .. self.where);
end
2010-06-23 20:25:23 +03:00
local v, r, jump;
2010-06-07 13:18:44 +03:00
2010-07-12 10:50:13 +03:00
if not isVroom(ref(where)) and not stead.in_exit_call and not noexit then
2010-06-07 13:18:44 +03:00
stead.in_exit_call = true -- to break recurse
2010-06-25 12:31:54 +03:00
v,r = call(ref(self.where), 'exit', ref(where));
2010-06-07 13:18:44 +03:00
stead.in_exit_call = nil
if r == false then
return v, ret(r)
end
2010-06-23 20:25:23 +03:00
if self.where ~= was then
where = deref(self.where) -- jump
jump = true
end
2010-06-07 13:18:44 +03:00
end
local res = v;
v = nil;
if not isVroom(ref(where)) then
self.where = deref(where);
end
2010-07-12 10:50:13 +03:00
if not jump and not noenter then
2010-06-25 12:31:54 +03:00
v, r = call(ref(where), 'enter', ref(was));
2010-06-16 13:25:29 +03:00
if r == false then
self.where = was;
return par('^^', res, v), ret(r)
end
2010-07-12 10:50:13 +03:00
end
need_scene = true;
if ref(where) ~= ref(self.where) then -- jump !!!
need_scene = false;
2010-06-07 13:18:44 +03:00
end
2010-06-16 13:09:41 +03:00
2010-06-07 13:18:44 +03:00
res = par('^^',res,v);
if not back then
ref(where).__from__ = deref(was);
end
ret()
if not stead.in_goto_call then
local to = self.where
2010-07-12 10:50:13 +03:00
if not noexit then
self.where = was
stead.in_onexit_call = true
v = call(ref(was), 'left', ref(to));
stead.in_onexit_call = false
res = par('^^',res,v);
end
2010-06-07 13:18:44 +03:00
self.where = deref(to)
2010-07-12 10:50:13 +03:00
if not noenter then
stead.in_entered_call = true
v = call(ref(to), 'entered', ref(was));
stead.in_entered_call = false
res = par('^^',res,v);
end
2010-07-02 07:34:12 +03:00
if tonumber(ref(to).__visited) then
ref(to).__visited = ref(to).__visited + 1;
elseif here().__visited == nil then
ref(to).__visited = 1
end
2010-07-12 10:50:13 +03:00
2010-07-07 20:44:44 +03:00
if isDialog(ref(to)) then
dialog_rescan(ref(to));
end
2010-06-07 13:18:44 +03:00
end
2010-06-23 13:35:53 +03:00
PLAYER_MOVED = true
2010-07-12 11:01:11 +03:00
if need_scene and not nodsc then
2010-06-23 13:35:53 +03:00
NEED_SCENE = true
2010-06-07 13:18:44 +03:00
end
return res;
end
2010-07-11 21:48:47 +03:00
stead.go = go
2010-06-22 12:00:29 +03:00
2010-07-11 20:59:45 +03:00
function player_go(self, where) -- cmd iface
local w = ref(self.where).way:srch(where);
if not w then
return nil,false
end
return self:goto(w);
end
function player_goto(self, where, ...) -- real work
2010-07-11 21:48:47 +03:00
local v, r = stead.go(self, where, unpack(arg));
2010-07-11 20:59:45 +03:00
return v, r;
end
function player_back(self) -- deprecated
error ("Do not use me():back(). It's deprecated.", 2)
end
function back()
2010-07-12 10:50:13 +03:00
if isDialog(here()) and not isDialog(from()) then
2010-07-12 11:01:11 +03:00
local r, v = me():goto(from(), true, true, false, true);
2010-07-12 10:50:13 +03:00
return r,v;
end
2010-07-11 21:48:47 +03:00
return me():goto(from(), true);
2010-07-11 20:59:45 +03:00
end
2010-07-12 10:50:13 +03:00
stead.back = back
2010-07-11 20:59:45 +03:00
function goback()
2010-07-12 10:50:13 +03:00
return me():goto(from(), true);
end
stead.goback = goback
2010-07-12 11:01:11 +03:00
function goto(what, back, noenter, noexit, nodsc, ...)
return me():goto(what, back, noenter, noexit, nodsc, unpack(arg));
2010-07-11 20:59:45 +03:00
end
2010-07-12 10:50:13 +03:00
stead.goto = goto
2010-07-11 20:59:45 +03:00
game.ini = stead.hook(game.ini,function(f, ...)
if isRoom(here()) then
here().__visited = 1
end
return f(unpack(arg))
end)
2010-07-02 07:34:12 +03:00
function visited(w)
if not w then w = here() end
w = ref(w)
if not isRoom(w) then
error ("Wrong parameter to visited.", 2);
end
return w.__visited
end
2010-06-23 13:35:53 +03:00
iface.fmt = function(self, cmd, st, moved, r, av, objs, pv) -- st -- changed state (main win), move -- loc changed
local l
if st then
av = txtem(av);
pv = txtem(pv);
2010-07-15 16:42:26 +03:00
-- if not PLAYER_MOVED then
2010-07-12 10:28:09 +03:00
r = txtem(r)
2010-07-15 16:42:26 +03:00
-- end
2010-06-23 13:35:53 +03:00
if isForcedsc(here()) or NEED_SCENE then
l = here():scene();
end
end
if moved then
vv = stead.fmt(stead.cat(stead.par("^^", r, av, l, objs, pv), '^'));
else
vv = stead.fmt(stead.cat(stead.par("^^", l, r, av, objs, pv), '^'));
end
return vv
end
2010-07-11 21:48:47 +03:00
stead.go = stead.hook(stead.go, function(f, ...)
2010-06-22 12:00:29 +03:00
local r,v = f(unpack(arg))
2010-06-23 09:05:25 +03:00
if type(r) == 'string' and cctx() then
2010-06-22 12:00:29 +03:00
pr (r)
end
if stead.in_life_call then
ACTION_TEXT = nil
end
2010-07-09 19:28:18 +03:00
if r == nil and v == nil then
2010-07-04 17:54:55 +03:00
if cctx() then
2010-07-04 18:43:39 +03:00
cctx().action = true
2010-07-04 17:54:55 +03:00
else
r = true
end
end
2010-06-22 12:00:29 +03:00
return r,v
end)
2010-06-23 13:35:53 +03:00
2010-06-23 15:09:33 +03:00
iface.cmd = stead.hook(iface.cmd, function(f, ...)
2010-06-23 13:35:53 +03:00
NEED_SCENE = nil
return f(unpack(arg))
end)
2010-06-23 15:09:33 +03:00
player = stead.inherit(player, function(v)
2010-06-23 13:35:53 +03:00
v.look = function(s)
if game._time == 0 then
return stead.goto(here(), false, false, true);
end
2010-06-23 13:35:53 +03:00
NEED_SCENE = true
end
return v
end)
pl = player(pl) -- reinit
2010-07-06 13:15:27 +03:00
-- vim:ts=4