Manual:DIL Manual/findrndunit()

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

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