Difference between revisions of "Manual:DIL Manual/pathto()"
Jump to navigation
Jump to search
(XML import) |
(checked against the compiler and the interpreter; example compiles) |
||
| (One intermediate revision by one other user not shown) | |||
| Line 1: | Line 1: | ||
| + | = 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 == | |
| + | {| class="wikitable" | ||
| + | ! Name !! Type !! Meaning | ||
| + | |- | ||
| + | | from || unitptr || Where the way starts: usually <code>self</code>. | ||
| + | |- | ||
| + | | to || unitptr || Where it should end: a room, or a unit in it. | ||
| + | |} | ||
| + | |||
| + | == Returns == | ||
| + | Integer, one of the <code>DIR_*</code> constants in <code>vme.h</code>: | ||
| + | {| class="wikitable" | ||
| + | ! Value !! Meaning | ||
| + | |- | ||
| + | | <code>DIR_NORTH</code> ... <code>DIR_SOUTHWEST</code> (0 to 9, up to <code>MAX_EXIT</code>) || Take that exit. | ||
| + | |- | ||
| + | | <code>DIR_ENTER</code> (10) || Enter a room that lies inside this one. | ||
| + | |- | ||
| + | | <code>DIR_EXIT</code> (11) || Leave this room for the room it lies in. | ||
| + | |- | ||
| + | | <code>DIR_IMPOSSIBLE</code> (12) || There is no way. | ||
| + | |- | ||
| + | | <code>DIR_HERE</code> (13) || Both are in the same room. | ||
| + | |- | ||
| + | | <code>DIR_TRYAGAIN</code> (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 <code>DIR_IMPOSSIBLE</code>, 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 | ||
| + | <code>DIR_TRYAGAIN</code>. Wait (a <code>pause</code>) 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. <code>walk_room@function</code> handles doors, | ||
| + | entering and leaving, and gives up after a number of failed tries. | ||
| + | * <code>DIR_SL_SHORT</code> and <code>DIR_SL_LONG</code> in <code>vme.h</code> are the names of | ||
| + | the values, to use as a stringlist. | ||
| + | * Take the step with <code>exec("<dir>", self)</code> so it is an ordinary move, then check | ||
| + | <code>self.outside</code> to see whether it worked. | ||
| + | |||
| + | == Example == | ||
| + | <pre> | ||
| + | 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 | ||
| + | </pre> | ||
| + | An NPC that walks to a room one step every few seconds; a short version of | ||
| + | <code>walk_room@function</code> in function.zon, which also deals with doors. | ||
| + | |||
| + | == See also == | ||
| + | * [[Manual:DIL Manual/findroom()|findroom()]] | ||
| + | * [[Manual:DIL Manual/exec()|exec()]] | ||
| + | * [[Manual:DIL Manual/exit to|exit to]] | ||
| + | * [[Manual:DIL Manual/strdir()|strdir()]] | ||
Latest revision as of 23:03, 14 November 2026
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 (apause) 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_SHORTandDIR_SL_LONGinvme.hare 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.