Difference between revisions of "Manual:DIL Manual/unitptr"

From DikuMUD Wiki
Jump to navigation Jump to search
(XML import)
(checked against the compiler and the interpreter; example compiles)
 
Line 1: Line 1:
−
<span id="unit"></span>
+
= unitptr =
−
'''The unitptr has the following fields:'''
+
'''Type.''' A pointer to a unit: a room, an object, an NPC or a player.
  
−
    unitptr:
+
  var
−
      'names'        :stringlist (RW)
+
     u : unitptr;
−
          A list of names that matches the unit.
 
−
      'name'        :string (RW)
 
−
          The first name in the namelist in 'names'
 
−
      'outside_descr' :string (RW)
 
−
          The description of the unit from the 'outside'. f.inst.
 
−
          the description of a boat, seen from the outside.
 
−
      'inside_descr' :string (RW)
 
−
          The description of the unit from the 'inside'. f.inst.
 
−
          the description of a boat, seen from the inside.
 
−
      'next'        :unitptr (RO)
 
−
          The next unit in the local list of units. For instance, the
 
−
          units in the inventory of a PC, is linked with this field.
 
−
      'title'        :string (RW)
 
−
          The title of the unit.
 
−
      'extra'        :extraptr (RW)
 
−
          Extra descriptions of the unit (identify, look at, read etc.).
 
−
      'outside'      :unitptr (RO)
 
−
          The outside of the unit hierarchy (carried by, contained by).
 
−
          For instance, the contents of a bag have their 'outside'
 
−
          field pointing to the bag.
 
−
      'inside'      :unitptr (RO)
 
−
          The inside of the unit hierarchy (carry, contains).
 
−
          For instance the first unit in a PC's inventory is referenced
 
−
          by the 'inside' field.
 
−
      'key'      :string (RO)
 
−
          The key that will open this unitptr
 
−
          For instance "big_key@blackzon"
 
−
          by the 'inside' field.
 
−
      'gnext'        :unitptr (RO)
 
−
          Next unit in the global list of units.
 
−
      'gprevious'    :unitptr (RO)
 
−
          Previous unit in the global list of units.
 
−
      'hasfunc'      :integer (RO)
 
−
          Returns TRUE if unit has special functions attached to it.
 
−
      'max_hp'      :integer (RO/RW)
 
−
          The maximum hitpoints of unit, RO for players, RW for mobiles.
 
−
      'max_mana'      :integer (RO)
 
−
          The maximum mana of the character (player or mobile).
 
−
      'max_endurance' :integer (RO)
 
−
          The maximum endurance of the character (player or mobile).
 
−
      'lifespan'      :integer (RW)
 
−
          The lifespan of a character, write permission only on root access.
 
−
      'hp'          :integer (RW)
 
−
          The current hitpoints of unit. For objects, this can render
 
−
          it 'broken' by being set low. If an unit has -1 hitpoints
 
−
          its considered unbreakable.
 
−
      'manipulate'  :integer (RW)
 
−
          Bits that specify how unit can be handled, see MANIPULATE_*
 
−
          flags in values.h and/or vme.h
 
−
      'flags'        :integer (RW)
 
−
          Bits that specify different properties of a unit, see
 
−
          UNIT_FL_* in values.h and/or vme.h
 
−
      'baseweight'  :integer (RO)
 
−
          The empty weight of the unit. This can be set with the
 
−
          procedure 'setweight()'
 
−
      'weight'      :integer (RO)
 
−
          The current weight of the unit (and its contents).
 
−
      'capacity'     :integer (RW)
 
−
          The amount of weight the unit can contain.
 
−
      'height'      :integer (RW)
 
−
          The height of a PC/NPC, the length of a rope, the size of weapons,
 
−
          armours, and shields.
 
−
      'alignment'    :integer (RW)
 
−
          The alignment of the unit. [1000..-1000],
 
−
          1000 is good, 0 is neutral, -1000 is evil.
 
−
      'openflags'    :integer (RW)
 
−
          Bits thats specify how a unit may be opened, locked, etc,
 
−
          see EX_* in values.
 
−
      'light'        :integer (RO)
 
−
          How much light is inside the unit.
 
−
      'bright'      :integer (RO)
 
−
          How much the unit lights up. This can be set with the
 
−
          procedure 'setbright()'
 
−
      'illum'        :integer (RO)
 
−
          How much light units inside a transparent unit create
 
−
      'minv'        :integer (RW)
 
−
          The 'wizard invisibility' level.
 
−
      'spells'[]    :integer (RO)
 
−
          The defence/skill of the different spell spheres/spells
 
−
          The index should be one of the SPL_* in values.h and/or vme.h
 
−
      'zoneidx'      :string (RO)
 
−
          The unique database name (in the zone) for this unit.
 
−
      'nameidx'      :string (RO)
 
−
          The unique database zone name for this unit.
 
−
      'idx'      :integer (RO)
 
−
          The unique database index (in the zone) for this unit.
 
−
          This provides a smart alternative to check for zoneidx
 
−
          and nameidx. Thus if (u1.idx == u2.idx) then
 
−
          (u1.nameidx == u2.nameidx) and (u1.zoneidx == u2.zoneidx).
 
−
          Be warned, this value change between each reboot, so do not
 
−
          count on it for purposes of saving.
 
−
      'zone'        :string (RO)
 
−
          The name of the zone the unit is in.
 
−
      'type'        :integer (RO)
 
−
          The type of the unit, see UNIT_ST_* in values.h and/or vme.h
 
−
      'loadcount'    :integer (RO)
 
−
          Returns the number of units loaded in the game
 
−
          of this definition (zoneidx,nameidx / idx)
 
−
<span id="uptr"></span>
 
−
'''Unitptr:'''
 
  
−
  Unit pointers are used to keep track of units: rooms, players, non-player or
+
Through a unitptr a program reads and changes a unit's [[Manual:DIL Manual/Fields|fields]] (<code>u.name</code>, <code>u.hp</code>, <code>u.outside</code>...), and hands the unit to functions and procedures. <code>u.type</code> says which kind of unit it is: <code>UNIT_ST_ROOM</code>, <code>UNIT_ST_OBJ</code>, <code>UNIT_ST_NPC</code> or <code>UNIT_ST_PC</code>. Which fields a unit has depends on that type.
−
objects. Through a variable of this type you can access most of the information
 
−
in a unit. Unitptr variables are 'volatile', and thus cleared whenever there is
 
−
a possibility that they are rendered unusable.
 
−
<span id="unit"></span>
 
−
'''The unitptr has the following fields:'''
 
  
−
    unitptr:
+
A unitptr is <code>null</code> or points to a unit. Built-in variables such as <code>self</code>, <code>activator</code>, <code>medium</code> and <code>target</code> are unitptrs, and so are the results of <code>findunit()</code>, <code>findroom()</code>, <code>findsymbolic()</code> and <code>load()</code>.
−
      'names'        :stringlist (RW)
+
 
−
          A list of names that matches the unit.
+
== Notes ==
−
      'name'         :string (RW)
+
* '''A unitptr variable is cleared at every wait.''' When the program wakes from <code>wait</code>, <code>pause</code> or a heartbeat, every unitptr variable is null again, unless that unit is <code>secure</code>d. Find the unit again after waiting, or secure it. <code>self</code> is always the program's own unit.
−
          The first name in the namelist in 'names'
+
* A field of a null unit fails, and a failed value is false in every comparison: <code>u.level &gt; 5</code> and <code>u.level &lt;= 5</code> are both false when <code>u</code> is null. Test <code>u == null</code> (or <code>not u</code>) first, on its own: <code>and</code> doesn't stop early.
−
      'outside_descr' :string (RW)
+
* A field on a call's result is a syntax error: <code>findroom(s).title</code> doesn't compile. Put the unit in a variable first.
−
          The description of the unit from the 'outside'. f.inst.
+
* <code>==</code> and <code>!=</code> tell whether two unitptrs point to the same unit.
−
          the description of a boat, seen from the outside.
+
* Units in a room (or carried, or in a container) form a list: <code>u.inside</code> is the first one, <code>.next</code> the next. When the loop moves or destroys units, use <code>foreach</code> instead.
−
      'inside_descr' :string (RW)
+
* unitptr variables are not saved with a unit: after loading they are null.
−
          The description of the unit from the 'inside'. f.inst.
+
 
−
          the description of a boat, seen from the inside.
+
== Example ==
−
      'next'        :unitptr (RO)
+
<pre>
−
          The next unit in the local list of units. For instance, the
+
dilbegin do_count(arg : string);
−
          units in the inventory of a PC, is linked with this field.
+
var
−
      'title'        :string (RW)
+
   u : unitptr;
−
          The title of the unit.
+
  pcs, others, things : integer;
−
      'extra'        :extraptr (RW)
+
code
−
          Extra descriptions of the unit (identify, look at, read etc.).
+
{
−
      'outside'      :unitptr (RO)
+
  u := self.outside.inside;
−
          The outside of the unit hierarchy (carried by, contained by).
+
  while (u != null)
−
          For instance, the contents of a bag have their 'outside'
+
  {
−
          field pointing to the bag.
+
       if (u.type == UNIT_ST_PC)
−
      'inside'      :unitptr (RO)
+
        pcs := pcs + 1;
−
          The inside of the unit hierarchy (carry, contains).
+
      else if (u.type == UNIT_ST_NPC)
−
          For instance the first unit in a PC's inventory is referenced
+
        others := others + 1;
−
          by the 'inside' field.
+
      else if (u.type == UNIT_ST_OBJ)
−
      'key'      :string (RO)
+
        things := things + 1;
−
          The key that will open this unitptr
+
      u := u.next;
−
          For instance "big_key@blackzon"
+
  }
−
          by the 'inside' field.
+
  sendtext("Here: " + itoa(pcs) + " players, " + itoa(others) + " others and "
−
      'gnext'        :unitptr (RO)
+
            + itoa(things) + " things.<br/>", self);
−
          Next unit in the global list of units.
+
   quit;
−
      'gprevious'   :unitptr (RO)
+
}
−
          Previous unit in the global list of units.
+
dilend
−
      'hasfunc'      :integer (RO)
+
</pre>
−
          Returns TRUE if unit has special functions attached to it.
+
A command that counts the players, NPCs and objects in the room (<code>npcs</code> and <code>objs</code> are reserved words, so the variables have other names).
−
      'max_hp'      :integer (RO/RW)
+
 
−
          The maximum hitpoints of unit, RO for players, RW for mobiles.
+
== See also ==
−
      'max_mana'       :integer (RO)
+
* [[Manual:DIL Manual/Fields|Fields]]
−
          The maximum mana of the character (player or mobile).
+
* [[Manual:DIL Manual/null|null]]
−
      'max_endurance'  :integer (RO)
+
* [[Manual:DIL Manual/self|self]]
−
          The maximum endurance of the character (player or mobile).
+
* [[Manual:DIL Manual/secure()|secure()]]
−
      'lifespan'      :integer (RW)
+
* [[Manual:DIL Manual/foreach()|foreach()]]
−
          The lifespan of a character, write permission only on root access.
+
* [[Manual:DIL Manual/findunit()|findunit()]]
−
      'hp'          :integer (RW)
+
* [[Manual:DIL Manual/type|type]]
−
          The current hitpoints of unit. For objects, this can render
+
* [[Manual:DIL Manual/inside|inside]]
−
          it 'broken' by being set low. If an unit has -1 hitpoints
+
* [[Manual:DIL Manual/next|next]]
−
          its considered unbreakable.
 
−
      'manipulate'  :integer (RW)
 
−
          Bits that specify how unit can be handled, see MANIPULATE_*
 
−
          flags in values.h and/or vme.h
 
−
      'flags'        :integer (RW)
 
−
          Bits that specify different properties of a unit, see
 
−
          UNIT_FL_* in values.h and/or vme.h
 
−
      'baseweight'  :integer (RO)
 
−
          The empty weight of the unit. This can be set with the
 
−
          procedure 'setweight()'
 
−
      'weight'      :integer (RO)
 
−
          The current weight of the unit (and its contents).
 
−
      'capacity'    :integer (RW)
 
−
          The amount of weight the unit can contain.
 
−
      'height'      :integer (RW)
 
−
          The height of a PC/NPC, the length of a rope, the size of weapons,
 
−
          armours, and shields.
 
−
      'alignment'   :integer (RW)
 
−
          The alignment of the unit. [1000..-1000],
 
−
          1000 is good, 0 is neutral, -1000 is evil.
 
−
      'openflags'    :integer (RW)
 
−
          Bits thats specify how a unit may be opened, locked, etc,
 
−
          see EX_* in values.
 
−
      'light'        :integer (RO)
 
−
          How much light is inside the unit.
 
−
      'bright'      :integer (RO)
 
−
          How much the unit lights up. This can be set with the
 
−
          procedure 'setbright()'
 
−
      'illum'        :integer (RO)
 
−
          How much light units inside a transparent unit create
 
−
      'minv'        :integer (RW)
 
−
          The 'wizard invisibility' level.
 
−
      'spells'[]    :integer (RO)
 
−
          The defence/skill of the different spell spheres/spells
 
−
          The index should be one of the SPL_* in values.h and/or vme.h
 
−
      'zoneidx'      :string (RO)
 
−
          The unique database name (in the zone) for this unit.
 
−
      'nameidx'      :string (RO)
 
−
          The unique database zone name for this unit.
 
−
      'idx'      :integer (RO)
 
−
          The unique database index (in the zone) for this unit.
 
−
          This provides a smart alternative to check for zoneidx
 
−
          and nameidx. Thus if (u1.idx == u2.idx) then
 
−
          (u1.nameidx == u2.nameidx) and (u1.zoneidx == u2.zoneidx).
 
−
          Be warned, this value change between each reboot, so do not
 
−
          count on it for purposes of saving.
 
−
      'zone'        :string (RO)
 
−
          The name of the zone the unit is in.
 
−
      'type'        :integer (RO)
 
−
          The type of the unit, see UNIT_ST_* in values.h and/or vme.h
 
−
      'loadcount'    :integer (RO)
 
−
          Returns the number of units loaded in the game
 
−
          of this definition (zoneidx,nameidx / idx)
 

Latest revision as of 23:27, 14 November 2026

unitptr

Type. A pointer to a unit: a room, an object, an NPC or a player.

var
   u : unitptr;

Through a unitptr a program reads and changes a unit's fields (u.name, u.hp, u.outside...), and hands the unit to functions and procedures. u.type says which kind of unit it is: UNIT_ST_ROOM, UNIT_ST_OBJ, UNIT_ST_NPC or UNIT_ST_PC. Which fields a unit has depends on that type.

A unitptr is null or points to a unit. Built-in variables such as self, activator, medium and target are unitptrs, and so are the results of findunit(), findroom(), findsymbolic() and load().

Notes

  • A unitptr variable is cleared at every wait. When the program wakes from wait, pause or a heartbeat, every unitptr variable is null again, unless that unit is secured. Find the unit again after waiting, or secure it. self is always the program's own unit.
  • A field of a null unit fails, and a failed value is false in every comparison: u.level > 5 and u.level <= 5 are both false when u is null. Test u == null (or not u) first, on its own: and doesn't stop early.
  • A field on a call's result is a syntax error: findroom(s).title doesn't compile. Put the unit in a variable first.
  • == and != tell whether two unitptrs point to the same unit.
  • Units in a room (or carried, or in a container) form a list: u.inside is the first one, .next the next. When the loop moves or destroys units, use foreach instead.
  • unitptr variables are not saved with a unit: after loading they are null.

Example

dilbegin do_count(arg : string);
var
   u : unitptr;
   pcs, others, things : integer;
code
{
   u := self.outside.inside;
   while (u != null)
   {
      if (u.type == UNIT_ST_PC)
         pcs := pcs + 1;
      else if (u.type == UNIT_ST_NPC)
         others := others + 1;
      else if (u.type == UNIT_ST_OBJ)
         things := things + 1;
      u := u.next;
   }
   sendtext("Here: " + itoa(pcs) + " players, " + itoa(others) + " others and "
            + itoa(things) + " things.<br/>", self);
   quit;
}
dilend

A command that counts the players, NPCs and objects in the room (npcs and objs are reserved words, so the variables have other names).

See also