Difference between revisions of "Manual:DIL Manual/findunit()"

From DikuMUD Wiki
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:
 +
= 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
  
−
<span id="bffindu"></span>
+
Looks for a unit called <code>s</code> in the places <code>flags</code> names, as seen by <code>u</code>: 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 [[Manual:DIL Manual/findsymbolic()|findsymbolic()]] instead; a name can belong to anyone, players included.
−
unitptr findunit ( u : unitptr , s : string , i : integer , l : unitptr )
 
−
      u : The unit the local environment is relative to.
 
−
      s : A string with name of the unit to find.
 
−
      i : The environment, where to look for it, see FIND_UNIT_*
 
−
          in values.h and/or vme.h.
 
−
      l : An optional list of units to search.
 
−
  return: If the unit is found, the function returns that unit.
 
−
          Returns null if nothing found.
 
  
−
  The first argument is typically the char that's looking for something,
+
== Parameters ==
−
  i.e. if Mary needs a spoon to stir the pot, she'll be the first argument.
+
{| class="wikitable"
 +
! 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 <code>2.sword</code> means the second sword.
 +
|-
 +
| flags || integer || Where to look, <code>FIND_UNIT_*</code> from <code>vme/include/vme.h</code>, joined with <code>|</code>: <code>FIND_UNIT_EQUIP</code> (what <code>u</code> wears), <code>FIND_UNIT_INVEN</code> (what it carries), <code>FIND_UNIT_SURRO</code> (its surroundings: the room it is in and the units there), <code>FIND_UNIT_ZONE</code> (anywhere in the zone <code>u</code> is in), <code>FIND_UNIT_WORLD</code> (anywhere). <code>FIND_UNIT_IN_ME</code> is equip and inventory, <code>FIND_UNIT_HERE</code> adds the surroundings, <code>FIND_UNIT_GLOBAL</code> is all of them. 0 searches only <code>list</code>.
 +
|-
 +
| 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 <code>inside</code>.
 +
|-
 +
| type || integer || Optional: the kinds of unit wanted, <code>UNIT_ST_PC</code>, <code>UNIT_ST_NPC</code>, <code>UNIT_ST_OBJ</code>, <code>UNIT_ST_ROOM</code>, joined with <code>|</code>. Without it, any kind.
 +
|}
  
−
  The second argument is what you're looking for, represented by a string.
+
== Returns ==
−
  In the above mentioned example, that'd be "spoon".
+
The unit found, or null. When <code>u</code> is null or <code>s</code> is empty, the call fails and the variable assigned keeps its old value: clear it first if you test it for null afterwards.
  
−
  For the second or third argument, you have a choice, as you'll only need to
+
== Notes ==
−
  use one of them, and let the other be 0 or null. For instance, if you have
+
* '''It changes <code>s</code>.''' When <code>s</code> is a string variable (or <code>argument</code>), the words it matched, and the filler words before them, are taken off its start. Copy the string first if you need it again.
−
  a pointer to Mary's kitchen utensil pot, you can use the line:
+
* When names of different lengths fit the start of <code>s</code>, 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.
 +
* <code>"self"</code> with <code>FIND_UNIT_SURRO</code> finds <code>u</code> itself.
 +
* When nothing matches the whole word and <code>u</code> is a player, a word of up to three letters also matches the start of a name: "bra" finds Brawla.
 +
* When <code>u</code> is a character it must be able to see the unit: not blind, light in the room, the unit not invisible (unless <code>u</code> detects invisible), not buried, and not wizard-invisible above <code>u</code>'s level. Gods from <code>CREATOR_LEVEL</code> up see in the dark and see invisible units.
 +
* When <code>u</code> is not a character (an object or a room running the program), <code>flags</code> are ignored and only <code>list</code> is searched.
 +
* <code>FIND_UNIT_PAY</code> and <code>FIND_UNIT_NOPAY</code> are ignored here.
 +
* <code>FIND_UNIT_ZONE</code> and <code>FIND_UNIT_WORLD</code> go through every unit in the game; to find a player by exact name, walk the player list from <code>pc_head()</code> instead.
  
−
       findunit(mary, "spoon", 0, pot);
+
== Example ==
 +
<pre>
 +
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
 +
</pre>
 +
A player command that finds a character in the room by the name typed.
  
−
  Or you can just let her look around for it with:
+
== See also ==
−
 
+
* [[Manual:DIL Manual/findsymbolic()|findsymbolic()]]
−
      findunit(mary, "spoon", FIND_UNIT_INVEN or FIND_UNIT_SURRO, null);
+
* [[Manual:DIL Manual/findrndunit()|findrndunit()]]
−
 
+
* [[Manual:DIL Manual/findroom()|findroom()]]
−
  You can use all the FIND_UNIT_ values defined in values.h and/or vme.h, if you
+
* [[Manual:DIL Manual/getword()|getword()]]
−
  want to look for something for example within the zone (FIND_UNIT_ZONE), the
+
* [[Manual:DIL Manual/pc head()|pc head()]]
−
  entire world (FIND_UNIT_WORLD) (you rarely need to do that, it's mainly for
+
* [[Manual:DIL Manual/visible()|visible()]]
−
  tell etc.), or just the inventory or equipment of the char in question
 
−
  (FIND_UNIT_INVEN and FIND_UNIT_EQUIP).  Finally, as in the example above,
 
−
  you can look in the room of the char (FIND_UNIT_SURRO).
 
−
 
 
−
  Using FIND_UNIT_PAY or FIND_UNIT_NOPAY in this function will be ignored.
 
−
 
 
−
The flags for findunit, intuitively:
 
−
 
 
−
 
 
−
FIND_UNIT_EQUIP:
 
−
  The objects you will see with `equipment'
 
−
  Assumes first argument to findunit is a char.
 
−
 
 
−
 
 
−
FIND_UNIT_INVEN:
 
−
  The objects you will see with `inventory' or `look in bag'
 
−
 
 
−
 
 
−
FIND_UNIT_SURRO:
 
−
  The objects you can see, and get with `get',
 
−
  or the characters you can `poke' :-)
 
−
 
 
−
 
 
−
FIND_UNIT_ZONE:
 
−
  As FIND_UNIT_WORLD, only more local.
 
−
 
 
−
 
 
−
FIND_UNIT_WORLD:
 
−
  Any object in the entire world.  Does NOT start looking close to the
 
−
  object of findunit's first argument, but rather somewhat randomly.
 
−
 
 
−
 
 
−
On top of these, the following (new) values are defined:
 
−
 
 
−
 
 
−
FIND_UNIT_IN_ME:
 
−
  Anything inside of the object of the first argument.
 
−
 
 
−
 
 
−
FIND_UNIT_HERE:
 
−
  Anything `here', i.e. in object or around it (same as IN_ME + SURRO)
 
−
 
 
−
 
 
−
FIND_UNIT_GLOBAL:
 
−
  ANYTHING, starting close to object and working out.
 
−
 
 
−
 
 
−
 
 
−
 
 
−
 
 
−
---~---~---~---~---~---~---~---~---
 

Latest revision as of 23:18, 14 November 2026

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