Difference between revisions of "Manual:DIL Manual/findrndunit()"
Jump to navigation
Jump to search
(XML import) |
(checked against the compiler and the interpreter; example compiles) |
||
| 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 | |
| − | + | 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. | |
| − | |||
| − | |||
| − | |||
| − | |||
| − | + | == 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>. | |
| − | + | |} | |
| − | |||
| − | |||
| − | + | == Returns == | |
| + | 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. | ||
| − | + | == Notes == | |
| − | + | * <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. | |
| − | + | * <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. | |
| − | + | * <code>FIND_UNIT_ZONE</code> and <code>FIND_UNIT_WORLD</code> go through every unit in the game. Don't call them often. | |
| − | + | * 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. | |
| + | * 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 aroundu, as forfindunit().FIND_UNIT_INVEN: whatuholds and doesn't wear.FIND_UNIT_EQUIP: whatuwears; only whentypesincludesUNIT_ST_OBJ, and the later items are more likely to be picked than the first ones.FIND_UNIT_ZONEmeans the zoneuwas 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_ZONEandFIND_UNIT_WORLDgo through every unit in the game. Don't call them often.- With
FIND_UNIT_PAYonly players who pay and rooms of pay zones count; withFIND_UNIT_NOPAYthe others. Objects and NPCs never count then. - It doesn't check whether
ucan 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.