Manual:DIL Manual/findunit()

From DikuMUD Wiki
< Manual:DIL Manual
Revision as of 23:18, 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

findunit()

Function (unitptr). Finds a unit by name, the way commands find what a player names.

findunit(u : unitptr, s : string, flags : integer, list : unitptr) : unitptr
findunit(u : unitptr, s : string, flags : integer, list : unitptr, type : integer) : unitptr

Looks for a unit called s in the places flags names, as seen by u: what it wears, carries, what is around it, and so on. It reads names the way the game's commands do, so it is the right tool for the words a player typed: "sword", "the 2.sword", "long sword". To find one particular NPC or object of a zone, use findsymbolic() instead; a name can belong to anyone, players included.

Parameters

Name Type Meaning
u unitptr The unit that looks. The places searched are relative to it, and when it is a character it only finds what it can see.
s string The name to look for. Leading filler words (a, an, at, from, in, into, of, on, the, to, with) are skipped, and 2.sword means the second sword.
flags integer : FIND_UNIT_EQUIP (what u wears), FIND_UNIT_INVEN (what it carries), FIND_UNIT_SURRO (its surroundings: the room it is in and the units there), FIND_UNIT_ZONE (anywhere in the zone u is in), FIND_UNIT_WORLD (anywhere). FIND_UNIT_IN_ME is equip and inventory, FIND_UNIT_HERE adds the surroundings, FIND_UNIT_GLOBAL is all of them. 0 searches only list.
list unitptr null, or a unit to search together with the units after it in the same place. To search what a container holds, pass its inside.
type integer . Without it, any kind.

Returns

The unit found, or null. When u is null or s is empty, the call fails and the variable assigned keeps its old value: clear it first if you test it for null afterwards.

Notes

  • It changes s. When s is a string variable (or argument), the words it matched, and the filler words before them, are taken off its start. Copy the string first if you need it again.
  • When names of different lengths fit the start of s, the longest wins: for "long sword", a unit named "long sword" beats one named "long". Among equally long matches the order is equipment, inventory, surroundings, zone, world, list.
  • "self" with FIND_UNIT_SURRO finds u itself.
  • When nothing matches the whole word and u is a player, a word of up to three letters also matches the start of a name: "bra" finds Brawla.
  • When u is a character it must be able to see the unit: not blind, light in the room, the unit not invisible (unless u detects invisible), not buried, and not wizard-invisible above u's level. Gods from CREATOR_LEVEL up see in the dark and see invisible units.
  • When u is not a character (an object or a room running the program), flags are ignored and only list is searched.
  • FIND_UNIT_PAY and FIND_UNIT_NOPAY are ignored here.
  • FIND_UNIT_ZONE and FIND_UNIT_WORLD go through every unit in the game; to find a player by exact name, walk the player list from pc_head() instead.

Example

dilbegin do_poke(arg : string);
var
   u : unitptr;
code
{
   u := null;
   if (arg != "")
      u := findunit(self, arg, FIND_UNIT_SURRO, null, UNIT_ST_PC | UNIT_ST_NPC);
   if (u == null)
   {
      sendtext("Poke whom?<br/>", self);
      quit;
   }
   act("You poke $3n.", A_ALWAYS, self, null, u, TO_CHAR);
   act("$1n pokes you.", A_SOMEONE, self, null, u, TO_VICT);
   act("$1n pokes $3n.", A_SOMEONE, self, null, u, TO_NOTVICT);
   quit;
}
dilend

A player command that finds a character in the room by the name typed.

See also