steed/stead/goto.lua

240 lines
5.1 KiB
Lua
Raw Normal View History

2011-07-29 10:17:07 +03:00
stead.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
2011-07-27 16:11:29 +03:00
if not isRoom(stead.ref(where)) then
2010-06-07 13:18:44 +03:00
error ("Trying to go nowhere: "..where);
end
2011-07-27 16:11:29 +03:00
if not isRoom(stead.ref(self.where)) then
2010-06-07 13:18:44 +03:00
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
2011-07-27 16:11:29 +03:00
if not isVroom(stead.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
2011-07-27 16:11:29 +03:00
v,r = stead.call(stead.ref(self.where), 'exit', stead.ref(where));
2010-06-07 13:18:44 +03:00
stead.in_exit_call = nil
2010-10-30 15:54:41 +03:00
if r == false or (stead.api_version >= "1.3.0" and v == false and r == nil) then
2010-06-07 13:18:44 +03:00
return v, ret(r)
end
2010-06-23 20:25:23 +03:00
if self.where ~= was then
2011-07-27 16:11:29 +03:00
where = stead.deref(self.where) -- jump
2010-06-23 20:25:23 +03:00
jump = true
end
2010-06-07 13:18:44 +03:00
end
local res = v;
v = nil;
2011-07-27 16:11:29 +03:00
if not isVroom(stead.ref(where)) then
self.where = stead.deref(where);
2010-06-07 13:18:44 +03:00
end
2010-07-12 10:50:13 +03:00
if not jump and not noenter then
2011-07-27 16:11:29 +03:00
v, r = stead.call(stead.ref(where), 'enter', stead.ref(was));
2010-10-30 15:54:41 +03:00
if r == false or (stead.api_version >= "1.3.0" and v == false and r == nil) then
2010-06-16 13:25:29 +03:00
self.where = was;
2011-07-27 20:15:18 +03:00
return par(stead.scene_delim, res, v), ret(r)
2010-06-16 13:25:29 +03:00
end
2010-07-12 10:50:13 +03:00
end
need_scene = true;
2011-07-27 16:11:29 +03:00
if stead.ref(where) ~= stead.ref(self.where) then -- jump !!!
2010-07-12 10:50:13 +03:00
need_scene = false;
2010-06-07 13:18:44 +03:00
end
2010-06-16 13:09:41 +03:00
2011-07-27 20:15:18 +03:00
res = par(stead.scene_delim, res, v);
2010-06-07 13:18:44 +03:00
if not back then
2011-07-27 16:11:29 +03:00
stead.ref(where).__from__ = stead.deref(was);
2010-06-07 13:18:44 +03:00
end
ret()
2010-07-15 17:21:18 +03:00
PLAYER_MOVED = true
if need_scene and not nodsc then
NEED_SCENE = true
end
2010-06-07 13:18:44 +03:00
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
2011-07-27 16:11:29 +03:00
v = stead.call(stead.ref(was), 'left', stead.ref(to));
2010-07-12 10:50:13 +03:00
stead.in_onexit_call = false
2011-07-27 20:15:18 +03:00
res = par(stead.scene_delim, res, v);
2010-07-12 10:50:13 +03:00
end
2010-06-07 13:18:44 +03:00
2011-07-27 16:11:29 +03:00
self.where = stead.deref(to)
2010-06-07 13:18:44 +03:00
2010-07-12 10:50:13 +03:00
if not noenter then
stead.in_entered_call = true
2011-07-27 16:11:29 +03:00
v = stead.call(stead.ref(to), 'entered', stead.ref(was));
2010-07-12 10:50:13 +03:00
stead.in_entered_call = false
2011-07-27 20:15:18 +03:00
res = par(stead.scene_delim, res, v);
2010-07-12 10:50:13 +03:00
end
2011-07-27 16:11:29 +03:00
if tonumber(stead.ref(to).__visited) then
stead.ref(to).__visited = stead.ref(to).__visited + 1;
2010-07-02 07:34:12 +03:00
elseif here().__visited == nil then
2011-07-27 16:11:29 +03:00
stead.ref(to).__visited = 1
2010-07-02 07:34:12 +03:00
end
2010-07-12 10:50:13 +03:00
2011-07-27 16:11:29 +03:00
if isDialog(stead.ref(to)) then
dialog_rescan(stead.ref(to));
2010-07-07 20:44:44 +03:00
end
2010-06-07 13:18:44 +03:00
end
return res;
end
2011-07-29 10:17:07 +03:00
go = stead.go
2010-06-22 12:00:29 +03:00
2010-07-11 20:59:45 +03:00
function player_go(self, where) -- cmd iface
2011-07-27 16:11:29 +03:00
local w = stead.ref(self.where).way:srch(where);
2010-07-11 20:59:45 +03:00
if not w then
return nil,false
end
return self:goto(w);
end
function player_goto(self, where, ...) -- real work
2011-02-23 12:11:27 +02:00
local v, r = stead.go(self, where, ...);
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
2011-07-29 10:17:07 +03:00
stead.back = function()
2010-07-12 10:50:13 +03:00
if isDialog(here()) and not isDialog(from()) then
2010-07-19 11:18:14 +03:00
return stead.goout();
2010-07-12 10:50:13 +03:00
end
2010-07-17 16:47:03 +03:00
return stead.goback();
2010-07-11 20:59:45 +03:00
end
2011-07-29 10:17:07 +03:00
back = stead.back
2010-07-11 20:59:45 +03:00
2011-07-29 10:17:07 +03:00
stead.goback = function()
2010-07-12 10:50:13 +03:00
return me():goto(from(), true);
end
2011-07-29 10:17:07 +03:00
goback = stead.goback
2010-07-12 10:50:13 +03:00
2011-07-29 10:17:07 +03:00
stead.goto = function(what, back, noenter, noexit, nodsc, ...)
2011-02-23 12:11:27 +02:00
return me():goto(what, back, noenter, noexit, nodsc, ...);
2010-07-11 20:59:45 +03:00
end
2011-07-29 10:17:07 +03:00
goto = stead.goto
2010-07-11 20:59:45 +03:00
2011-07-29 10:17:07 +03:00
stead.goin = function(what)
2010-07-17 16:47:03 +03:00
return me():goto(what, false, false, true);
end
2011-07-29 10:17:07 +03:00
goin = stead.goin
2010-07-17 16:47:03 +03:00
2011-07-29 10:17:07 +03:00
stead.goout = function(what)
2011-07-27 16:11:29 +03:00
if isRoom(stead.ref(what)) then
2010-07-17 17:11:34 +03:00
return me():goto(what, true, true, false, true);
end
2010-07-17 16:47:03 +03:00
return me():goto(from(), true, true, false, true);
end
2011-07-29 10:17:07 +03:00
goout = stead.goout
2010-07-17 16:47:03 +03:00
2010-07-02 07:34:12 +03:00
function visited(w)
if not w then w = here() end
2011-07-27 16:11:29 +03:00
w = stead.ref(w)
2010-07-27 14:13:49 +03:00
if w == nil then
2010-07-27 14:14:20 +03:00
return nil;
2010-07-27 14:13:49 +03:00
end
2010-07-02 07:34:12 +03:00
if not isRoom(w) then
error ("Wrong parameter to visited.", 2);
end
return w.__visited
end
2011-04-30 08:20:51 +03:00
function visits(w)
local n = visited(w)
if not n then n = 0 end
return n
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
2010-11-06 08:11:59 +02:00
vv = stead.fmt(stead.cat(stead.par(stead.scene_delim, r, av, l, objs, pv), '^'));
2010-06-23 13:35:53 +03:00
else
2010-11-06 08:11:59 +02:00
vv = stead.fmt(stead.cat(stead.par(stead.scene_delim, l, r, av, objs, pv), '^'));
2010-06-23 13:35:53 +03:00
end
return vv
end
2010-07-11 21:48:47 +03:00
stead.go = stead.hook(stead.go, function(f, ...)
2011-02-23 12:11:27 +02:00
local r,v = f(...)
2011-07-17 14:13:28 +03:00
if type(r) == 'string' and stead.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
2011-07-17 14:13:28 +03:00
if stead.cctx() then
stead.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
2011-02-23 12:11:27 +02:00
return f(...)
2010-06-23 13:35:53 +03:00
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)
2011-04-14 19:13:52 +03:00
if not stead.started then
game:start()
2011-04-14 20:38:39 +03:00
stead.started = true
2011-04-14 19:13:52 +03:00
end
if game._time == 0 then
return stead.goto(here(), false, false, true);
end
2010-06-23 13:35:53 +03:00
NEED_SCENE = true
2011-04-23 10:57:13 +03:00
if stead.api_version >= "1.3.5" then
return true -- force action
end
2010-06-23 13:35:53 +03:00
end
return v
end)
pl = player(pl) -- reinit
2010-07-06 13:15:27 +03:00
-- vim:ts=4