Manual:DIL Manual/npc head()

From DikuMUD Wiki
Jump to navigation Jump to search

npc_head()

Function (unitptr). The first NPC in the game's global unit list.

npc_head()

The global list holds every unit in the game, kept in this order: all players (the one who entered the game last first), then all NPCs, then all objects, then all rooms. Each unit's gnext is the next one in the list, and null after the last room.

npc_head() returns the first NPC, the one loaded last, or null when there are no NPCs. Follow gnext while the unit is an NPC to visit them all.

Returns

Unitptr: the first NPC, or null if there is none.

Notes

  • Stop at the first unit that isn't an NPC: while (u.type == UNIT_ST_NPC). After the last NPC come the objects.
  • A program runs at most 52,000 instructions between two waits (WAITCMD_MAXINST in vme/src/dil.h); past that it is stopped with "had endless loop" in vme.log. The game holds many thousands of objects, NPCs and rooms, so a loop of a few instructions per unit over a whole section can run into that limit. You can't pause half-way either: unit variables are cleared at every wait unless secured.
  • To find a particular NPC, findsymbolic("name@zone") is much cheaper than a walk.

Example

dilbegin unitptr npc_named(nm : string);
var
   u : unitptr;
code
{
   u := npc_head();
   while ((u.type == UNIT_ST_NPC) and (u.nameidx != nm))
      u := u.gnext;
   if (u.type != UNIT_ST_NPC)
      u := null;
   return (u);
}
dilend

Finds the first NPC whose symbolic name (nameidx, without the zone) is nm, or returns null.

See also