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

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:
 +
= findrndunit() =
 +
'''Function''' (unitptr). Picks a random unit of the wanted kinds in one place.
  
 +
findrndunit(u : unitptr, where : integer, types : integer) : unitptr
  
−
<span id="bffindru"></span>
+
Returns a unit chosen at random among those of the kinds <code>types</code> in the place <code>where</code>, relative to <code>u</code>: a random player in the room, a random room in the world, a random NPC of the zone. <code>u</code> itself is never picked.
−
unitptr findrndunit( u : unitptr, sv : integer, uf : integer)
 
−
    Returns: A pointer to a random unit, or null
 
−
    u  : The unit pointer which the search is relative to.
 
−
    sv : The search-location, a value (not bit vector) of FIND_UNIT_XXX
 
−
    uf : Bit vector. The unit flags which can match of UNIT_ST_XXX
 
  
−
    Example: u := findrndunit(self, FIND_UNIT_ZONE, UNIT_ST_PC|UNIT_ST_NPC);
+
== Parameters ==
 +
{| class="wikitable"
 +
! Name !! Type !! Meaning
 +
|-
 +
| u || unitptr || The unit the search is relative to, usually <code>self</code>.
 +
|-
 +
| where || integer || '''One''' of <code>FIND_UNIT_SURRO</code>, <code>FIND_UNIT_INVEN</code>, <code>FIND_UNIT_EQUIP</code>, <code>FIND_UNIT_ZONE</code>, <code>FIND_UNIT_WORLD</code> (<code>vme/include/vme.h</code>), optionally with <code>FIND_UNIT_PAY</code> or <code>FIND_UNIT_NOPAY</code> added. Not a combination of places: to search two, call it twice.
 +
|-
 +
| types || integer || The kinds 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>.
 +
|}
  
−
    This routine returns a random unit. Notice how the 'uf' lets you
+
== Returns ==
−
    specify exactly what unit types to look for. The 'sv' is not a
+
A random unit, or null when there is none (or <code>where</code> is a combination of places). When <code>u</code> is null the call fails, and the variable assigned keeps its old value.
−
    bit vector, although FIND_UNIT_XXX is usually used as such. If
 
−
    you need to search multiple environments, then call the routine
 
−
    once for each.
 
  
−
    Using FIND_UNIT_PAY or FIND_UNIT_NOPAY in this function will
+
== Notes ==
−
    pick a random player which in addition to being in the search
+
* <code>FIND_UNIT_SURRO</code>: what is around <code>u</code>, as for <code>findunit()</code>. <code>FIND_UNIT_INVEN</code>: what <code>u</code> holds and doesn't wear. <code>FIND_UNIT_EQUIP</code>: what <code>u</code> wears; only when <code>types</code> includes <code>UNIT_ST_OBJ</code>, and the later items are more likely to be picked than the first ones.
−
    environment also is registered as valid payer (or not). Asking
+
* <code>FIND_UNIT_ZONE</code> means the zone <code>u</code> was defined in, not the one it stands in, and counts the units defined in that zone wherever they are now, plus the players standing in it. Called on a player, it counts every player in the game.
−
    for a room would yield a random room registered to be accessible
+
* <code>FIND_UNIT_ZONE</code> and <code>FIND_UNIT_WORLD</code> go through every unit in the game. Don't call them often.
−
    for paying players only (or not). Asking for objects would
+
* With <code>FIND_UNIT_PAY</code> only players who pay and rooms of pay zones count; with <code>FIND_UNIT_NOPAY</code> the others. Objects and NPCs never count then.
−
    return no unit (null).
+
* It doesn't check whether <code>u</code> can see the unit.
 +
 
 +
== Example ==
 +
<pre>
 +
dilbegin pickpocket();
 +
var
 +
  u : unitptr;
 +
code
 +
{
 +
  heartbeat := PULSE_SEC * 30;
 +
  :loop:
 +
  wait(SFB_TICK, TRUE);
 +
  u := findrndunit(self, FIND_UNIT_SURRO, UNIT_ST_PC);
 +
  if (u != null)
 +
      act("$1n brushes past $3n.", A_SOMEONE, self, null, u, TO_NOTVICT);
 +
  goto loop;
 +
}
 +
dilend
 +
</pre>
 +
An NPC that picks a random player in its room now and then, as basis.zon does.
 +
 
 +
== See also ==
 +
* [[Manual:DIL Manual/findunit()|findunit()]]
 +
* [[Manual:DIL Manual/rnd()|rnd()]]
 +
* [[Manual:DIL Manual/foreach()|foreach()]]

Latest revision as of 23:18, 14 November 2026

findrndunit()

Function (unitptr). Picks a random unit of the wanted kinds in one place.

findrndunit(u : unitptr, where : integer, types : integer) : unitptr

Returns a unit chosen at random among those of the kinds types in the place where, relative to u: a random player in the room, a random room in the world, a random NPC of the zone. u itself is never picked.

Parameters

Name Type Meaning
u unitptr The unit the search is relative to, usually self.
where integer One of FIND_UNIT_SURRO, FIND_UNIT_INVEN, FIND_UNIT_EQUIP, FIND_UNIT_ZONE, FIND_UNIT_WORLD (vme/include/vme.h), optionally with FIND_UNIT_PAY or FIND_UNIT_NOPAY added. Not a combination of places: to search two, call it twice.
types integer .

Returns

A random unit, or null when there is none (or where is a combination of places). When u is null the call fails, and the variable assigned keeps its old value.

Notes

  • FIND_UNIT_SURRO: what is around u, as for findunit(). FIND_UNIT_INVEN: what u holds and doesn't wear. FIND_UNIT_EQUIP: what u wears; only when types includes UNIT_ST_OBJ, and the later items are more likely to be picked than the first ones.
  • FIND_UNIT_ZONE means the zone u was defined in, not the one it stands in, and counts the units defined in that zone wherever they are now, plus the players standing in it. Called on a player, it counts every player in the game.
  • FIND_UNIT_ZONE and FIND_UNIT_WORLD go through every unit in the game. Don't call them often.
  • With FIND_UNIT_PAY only players who pay and rooms of pay zones count; with FIND_UNIT_NOPAY the others. Objects and NPCs never count then.
  • It doesn't check whether u can see the unit.

Example

dilbegin pickpocket();
var
   u : unitptr;
code
{
   heartbeat := PULSE_SEC * 30;
   :loop:
   wait(SFB_TICK, TRUE);
   u := findrndunit(self, FIND_UNIT_SURRO, UNIT_ST_PC);
   if (u != null)
      act("$1n brushes past $3n.", A_SOMEONE, self, null, u, TO_NOTVICT);
   goto loop;
}
dilend

An NPC that picks a random player in its room now and then, as basis.zon does.

See also