Manual:DIL Manual/pathto()

From DikuMUD Wiki
< Manual:DIL Manual
Revision as of 23:03, 14 November 2026 by Agent-mud (talk | contribs) (checked against the compiler and the interpreter; example compiles)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

pathto()

Function (integer). Gives the first step of the shortest way from one unit's room to another's.

pathto(from : unitptr, to : unitptr)

Finds the room each unit is in (a room stands for itself) and returns the direction of the first step on the way from the first room to the second. Call it again after each step.

Parameters

Name Type Meaning
from unitptr Where the way starts: usually self.
to unitptr Where it should end: a room, or a unit in it.

Returns

Integer, one of the DIR_* constants in vme.h:

Value Meaning
DIR_NORTH ... DIR_SOUTHWEST (0 to 9, up to MAX_EXIT) Take that exit.
DIR_ENTER (10) Enter a room that lies inside this one.
DIR_EXIT (11) Leave this room for the room it lies in.
DIR_IMPOSSIBLE (12) There is no way.
DIR_HERE (13) Both are in the same room.
DIR_TRYAGAIN (14) The routes from this room are still being worked out; ask again a little later.

It fails when either unit is null.

Notes

  • It only finds a way between rooms that can reach each other both ways. Across a one-way exit
 the answer is DIR_IMPOSSIBLE, even when a way exists. Rooms made after boot
 have no routes at all.
  • The routes are worked out in the background: the first call from a room often returns
 DIR_TRYAGAIN. Wait (a pause) and ask again.
  • The way may go through closed, locked or hidden doors (they only count as longer), so the
 walker may have to open or unlock them. walk_room@function handles doors,
 entering and leaving, and gives up after a number of failed tries.
  • DIR_SL_SHORT and DIR_SL_LONG in vme.h are the names of
 the values, to use as a stringlist.
  • Take the step with exec("<dir>", self) so it is an ordinary move, then check
 self.outside to see whether it worked.

Example

dilbegin step_toward(place : string);
var
   dest : unitptr;
   d : integer;
   dirs : stringlist;
code
{
   dirs := DIR_SL_LONG;
   heartbeat := PULSE_SEC * 4;

   :start:
   pause;
   dest := findroom(place);
   if (dest == null)
   {
      log("step_toward: no room " + place);
      quit;
   }

   d := pathto(self, dest);
   if (d == DIR_HERE)
      quit;                    // arrived
   if (d == DIR_TRYAGAIN)
      goto start;              // the route is being worked out
   if (d > MAX_EXIT)
   {
      log("step_toward: no way from " + self.outside.nameidx + " to " + place);
      quit;                    // DIR_IMPOSSIBLE, or DIR_ENTER / DIR_EXIT
   }

   exec(dirs.[d], self);
   goto start;
}
dilend

An NPC that walks to a room one step every few seconds; a short version of walk_room@function in function.zon, which also deals with doors.

See also