Difference between revisions of "Source:NetHack 3.4.3/include/obj.h"

From NetHackWiki
Jump to navigation Jump to search
(add some info on the object flags)
m (moved Source:Obj.h to Source:NetHack 3.4.3/include/obj.h: Moving src to subdirs)
 
(16 intermediate revisions by 8 users not shown)
Line 1: Line 1:
The '''obj.h''' file contains C-language declarations for [[object]]s. It is a header file and part of the [[source code]] for NetHack 3.4.3.
+
__MIXEDSYNTAXHIGHLIGHT__
 +
Below is the full text to include/obj.h from NetHack 3.4.3. To link to a particular line, write [[obj.h#line123|<nowiki>[[obj.h#line123]]</nowiki>]], for example.
  
For the purposes of this annotation, we declare a pointer to an example object: <tt>obj *thing;</tt>
+
This particular file contains C-language declarations for [[object]]s. In particular, it declares <tt>struct obj</tt>, the data structure representing an [[object]] in the dungeon. This data structure contains many fields about the object, such as the type of the object, the [[BUC]] status, the number of charges, the erosion, the quantity of this object in a stack, the pointer to the [[monster]] presently carrying it, the flag indicating whether the player has [[identify|identified]] the object, and many other fields.
 +
 
 +
For the purposes of this annotation, we declare a pointer to an example object: <tt>struct obj *thing;</tt>
  
 
=== Top of file ===
 
=== Top of file ===
<pre>
+
<span id="line1">1.    /* SCCS Id: @(#)obj.h 3.4 2002/01/07 */</span>
/* SCCS Id: @(#)obj.h 3.4 2002/01/07 */
+
<span id="line2">2.    /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */</span>
/* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
+
<span id="line3">3.    /* NetHack may be freely redistributed.  See license for details. */</span>
/* NetHack may be freely redistributed.  See license for details. */
+
<span id="line4">4.    </span>
</pre>
+
<span id="line5">5.    #ifndef OBJ_H</span>
 
+
<span id="line6">6.    #define OBJ_H</span>
{{NGPL}}
+
<span id="line7">7.    </span>
 
+
<span id="line8">8.    /* #define obj obj_nh */ /* uncomment for SCO UNIX, which has a conflicting</span>
<pre>
+
<span id="line9">9.      * typedef for "obj" in <sys/types.h> */</span>
#ifndef OBJ_H
+
<span id="line10">10.  </span>
#define OBJ_H
 
 
 
/* #define obj obj_nh */ /* uncomment for SCO UNIX, which has a conflicting
 
  * typedef for "obj" in <sys/types.h> */
 
</pre>
 
  
 
=== union vptrs ===
 
=== union vptrs ===
<pre>
+
<span id="line11">11.  union vptrs {</span>
union vptrs {
+
<span id="line12">12.      struct obj *v_nexthere; /* floor location lists */</span>
    struct obj *v_nexthere; /* floor location lists */
+
<span id="line13">13.      struct obj *v_ocontainer; /* point back to container */</span>
    struct obj *v_ocontainer; /* point back to container */
+
<span id="line14">14.      struct monst *v_ocarry; /* point back to carrying monst */</span>
    struct monst *v_ocarry; /* point back to carrying monst */
+
<span id="line15">15.  };</span>
};
+
<span id="line16">16.  </span>
</pre>
 
  
Each <tt>struct obj</tt> (defined in the next section) contains a pointer called v, for example <tt>thing->v</tt>. Depening on where (<tt>thing->where</tt>) the object is, this pointer can have on of three meanings:
+
Each <tt>struct obj</tt> (defined in the next section) contains a pointer called v, for example <tt>thing->v</tt>. Depending on where (<tt>thing->where</tt>) the object is, this pointer can have one of three meanings:
* <tt>thing->v.v_nexthere</tt>, for an object at the dungeon [[floor]], is the next object below this one in the pile.
+
* <tt>thing->v.v_nexthere</tt>, for an object on the dungeon [[floor]], is the next object below this one in the pile.
 
* <tt>thing->v.v_ocontainer</tt>, for an object in a [[container]] such as a [[chest]] or [[bag]], points to that containing object.
 
* <tt>thing->v.v_ocontainer</tt>, for an object in a [[container]] such as a [[chest]] or [[bag]], points to that containing object.
 
* <tt>thing->v.v_ocarry</tt>, for an object in the [[inventory]] of a [[monster]], points to that monster.
 
* <tt>thing->v.v_ocarry</tt>, for an object in the [[inventory]] of a [[monster]], points to that monster.
Line 42: Line 39:
  
 
=== struct obj ===
 
=== struct obj ===
<pre>
+
<span id="line17">17.  struct obj {</span>
struct obj {
+
<span id="line18">18.  struct obj *nobj;</span>
struct obj *nobj;
 
</pre>
 
  
Each [[object]] contains a pointer like <tt>thing->nobj</tt> which allows the object to be part of a linked list. We are not of the purpose for this list in [[NetHack]].
+
Each [[object]] contains a pointer like <tt>thing->nobj</tt> which allows the object to be part of a linked list. (What does NetHack use this list for?)
  
<pre>
+
<span id="line19">19.  union vptrs v;</span>
union vptrs v;
+
<span id="line20">20.  #define nexthere v.v_nexthere</span>
#define nexthere v.v_nexthere
+
<span id="line21">21.  #define ocontainer v.v_ocontainer</span>
#define ocontainer v.v_ocontainer
+
<span id="line22">22.  #define ocarry v.v_ocarry</span>
#define ocarry v.v_ocarry
+
<span id="line23">23.  </span>
</pre>
 
  
 
This declares the pointer v and the three macros explained in the [[#union vptrs]] section above.
 
This declares the pointer v and the three macros explained in the [[#union vptrs]] section above.
  
<pre>
+
<span id="line24">24.  struct obj *cobj; /* contents list for containers */</span>
struct obj *cobj; /* contents list for containers */
+
<span id="line25">25.  unsigned o_id;</span>
unsigned o_id;
+
<span id="line26">26.  xchar ox,oy;</span>
xchar ox,oy;
+
<span id="line27">27.  short otyp; /* object class number */</span>
short otyp; /* object class number */
+
<span id="line28">28.  unsigned owt;</span>
unsigned owt;
+
<span id="line29">29.  long quan; /* number of items */</span>
long quan; /* number of items */
+
<span id="line30">30.  </span>
</pre>
 
  
* <tt>thing->cobj</tt> is a linked list of objects contained by <tt>thing</tt>, for example bag and statue contents.
+
* <tt>thing->cobj</tt> is a linked list of objects contained by <tt>thing</tt>, for example if the thing is a bag or statue.
* <tt>thing->o_id</tt> is a unique identifier for this object. It's set when the object is generated, and does not
+
* <tt>thing->o_id</tt> is a unique identifier for this object. It's set when the object is generated, and does not change. (Uniqueness across _all_ objects in the game is not guaranteed)
change. (This uniqueness is not guaranteed)
 
 
* <tt>thing->ox</tt> and <tt>thing->oy</tt> are object coordinates on the map.
 
* <tt>thing->ox</tt> and <tt>thing->oy</tt> are object coordinates on the map.
* <tt>thing->otyp</tt> is the object class from onames.h. Those definitions are generated when compiling, each object definition in objects.c gets one.
+
* <tt>thing->otyp</tt> is the object type from onames.h. Those definitions are generated when compiling, each object definition in objects.c gets one.
* <tt>thing->owt</tt> is the total weight of this object. use <tt>weight()</tt> in mkobj.c to calculate this.
+
* <tt>thing->owt</tt> is the total weight of this object. use <tt>weight()</tt> in [[mkobj.c]] to calculate this.
 
 
  
 
Some items [[stack]]. That is when multiple items take only one spot in your [[inventory]] or other lists, for example, "2 [[dagger]]s" or "7 [[candle]]s". If <tt>thing->quan</tt> is 2 or 7, then 2 or 7 of those things are stacked.
 
Some items [[stack]]. That is when multiple items take only one spot in your [[inventory]] or other lists, for example, "2 [[dagger]]s" or "7 [[candle]]s". If <tt>thing->quan</tt> is 2 or 7, then 2 or 7 of those things are stacked.
  
<pre>
+
<span id="line31">31.  schar spe; /* quality of weapon, armor or ring (+ or -)</span>
schar spe; /* quality of weapon, armor or ring (+ or -)
+
<span id="line32">32.    number of charges for wand ( >= -1 )</span>
  number of charges for wand ( >= -1 )
+
<span id="line33">33.    marks your eggs, spinach tins</span>
  marks your eggs, spinach tins
+
<span id="line34">34.    royal coffers for a court ( == 2)</span>
  royal coffers for a court ( == 2)
+
<span id="line35">35.    tells which fruit a fruit is</span>
  tells which fruit a fruit is
+
<span id="line36">36.    special for uball and amulet</span>
  special for uball and amulet
+
<span id="line37">37.    historic and gender for statues */</span>
  historic and gender for statues */
+
<span id="line38">38.  #define STATUE_HISTORIC 0x01</span>
#define STATUE_HISTORIC 0x01
+
<span id="line39">39.  #define STATUE_MALE    0x02</span>
#define STATUE_MALE    0x02
+
<span id="line40">40.  #define STATUE_FEMALE  0x04</span>
#define STATUE_FEMALE  0x04
 
</pre>
 
  
 
This gives a ''spe''cial statistic for certain types of items.
 
This gives a ''spe''cial statistic for certain types of items.
Line 96: Line 85:
 
* For [[statue]]s, this field is a [[bit mask]]. If <tt>thing->spe & STATUE_HISTORIC</tt>, then the statue is [[historic]]. Likewise, a statue can be [[male]] or [[female]]. It can be simultaneously historic, male, and female, though the last two might not combine well.
 
* For [[statue]]s, this field is a [[bit mask]]. If <tt>thing->spe & STATUE_HISTORIC</tt>, then the statue is [[historic]]. Likewise, a statue can be [[male]] or [[female]]. It can be simultaneously historic, male, and female, though the last two might not combine well.
  
<pre>
+
<span id="line41">41.  char oclass; /* object class */</span>
char oclass; /* object class */
+
<span id="line42">42.  char invlet; /* designation in inventory */</span>
char invlet; /* designation in inventory */
+
<span id="line43">43.  char oartifact; /* artifact array index */</span>
char oartifact; /* artifact array index */
+
<span id="line44">44.  </span>
</pre>
 
  
* [[We]] need to check this, but <tt>thing->oclass</tt> should be the character that represents the object at the screen, such as '*' for gems or '!' for potions.
+
* <tt>thing->oclass</tt> is one of the <tt>_CLASS</tt> definitions from [[objclass.h]], eg. WEAPON_CLASS or POTION_CLASS, and is the class that represents the object on the screen.
 
* An object in your [[inventory]] will have a letter [a-zA-Z], stored for example at <tt>thing->invlet</tt>. Even after an object is dropped, the object will try to remember its old inventory letter. Suppose [[you]] drop a [[spellbook]] which was inventory letter "B". If [[you]] retake the spellbook, the [[fixinv]] option is on, and the letter "B" is available, then the spellbook will again be letter "B" in your inventory.
 
* An object in your [[inventory]] will have a letter [a-zA-Z], stored for example at <tt>thing->invlet</tt>. Even after an object is dropped, the object will try to remember its old inventory letter. Suppose [[you]] drop a [[spellbook]] which was inventory letter "B". If [[you]] retake the spellbook, the [[fixinv]] option is on, and the letter "B" is available, then the spellbook will again be letter "B" in your inventory.
* It seems that NetHack has an array for [[artifact]]s.
+
* NetHack has a separate array for [[artifact]]s, defined in artilist.h
  
 
The hero should be memorising the inventory letter, not the object! But internal to the game, NetHack stores the letter in the object, not the adventurer. This works because this is a single-player game.
 
The hero should be memorising the inventory letter, not the object! But internal to the game, NetHack stores the letter in the object, not the adventurer. This works because this is a single-player game.
  
<pre>
+
<span id="line45">45.  xchar where; /* where the object thinks it is */</span>
xchar where; /* where the object thinks it is */
+
<span id="line46">46.  #define OBJ_FREE 0 /* object not attached to anything */</span>
#define OBJ_FREE 0 /* object not attached to anything */
+
<span id="line47">47.  #define OBJ_FLOOR 1 /* object on floor */</span>
#define OBJ_FLOOR 1 /* object on floor */
+
<span id="line48">48.  #define OBJ_CONTAINED 2 /* object in a container */</span>
#define OBJ_CONTAINED 2 /* object in a container */
+
<span id="line49">49.  #define OBJ_INVENT 3 /* object in the hero's inventory */</span>
#define OBJ_INVENT 3 /* object in the hero's inventory */
+
<span id="line50">50.  #define OBJ_MINVENT 4 /* object in a monster inventory */</span>
#define OBJ_MINVENT 4 /* object in a monster inventory */
+
<span id="line51">51.  #define OBJ_MIGRATING 5 /* object sent off to another level */</span>
#define OBJ_MIGRATING 5 /* object sent off to another level */
+
<span id="line52">52.  #define OBJ_BURIED 6 /* object buried */</span>
#define OBJ_BURIED 6 /* object buried */
+
<span id="line53">53.  #define OBJ_ONBILL 7 /* object on shk bill */</span>
#define OBJ_ONBILL 7 /* object on shk bill */
+
<span id="line54">54.  #define NOBJ_STATES 8</span>
#define NOBJ_STATES 8
 
</pre>
 
  
 
So <tt>thing->where</tt> indicates where this object is. A "shk" is a [[shopkeeper]]. If <tt>thing</tt> is in your inventory, then <tt>thing->where</tt> is either OBJ_INVENT or OBJ_ONBILL. A for loop that enumerated these locations would be like <tt>for(int i = 0; i < NOBJ_STATES; i++)</tt>.
 
So <tt>thing->where</tt> indicates where this object is. A "shk" is a [[shopkeeper]]. If <tt>thing</tt> is in your inventory, then <tt>thing->where</tt> is either OBJ_INVENT or OBJ_ONBILL. A for loop that enumerated these locations would be like <tt>for(int i = 0; i < NOBJ_STATES; i++)</tt>.
  
<pre>
+
<span id="line55">55.  xchar timed; /* # of fuses (timers) attached to this obj */</span>
xchar timed; /* # of fuses (timers) attached to this obj */
+
<span id="line56">56.  </span>
</pre>
 
  
Timers? It might be that these are for rotting eggs and corpses, for example.
+
Timers are for rotting eggs and corpses, for example. Do not change this by hand, rather use the code in [[timeout.c]], see <tt>start_timer()</tt>
  
<pre>
+
<span id="line57">57.  Bitfield(cursed,1);</span>
Bitfield(cursed,1);
+
<span id="line58">58.  Bitfield(blessed,1);</span>
Bitfield(blessed,1);
+
<span id="line59">59.  Bitfield(unpaid,1); /* on some bill */</span>
Bitfield(unpaid,1); /* on some bill */
+
<span id="line60">60.  Bitfield(no_charge,1); /* if shk shouldn't charge for this */</span>
Bitfield(no_charge,1); /* if shk shouldn't charge for this */
+
<span id="line61">61.  Bitfield(known,1); /* exact nature known */</span>
Bitfield(known,1); /* exact nature known */
+
<span id="line62">62.  Bitfield(dknown,1); /* color or text known */</span>
Bitfield(dknown,1); /* color or text known */
+
<span id="line63">63.  Bitfield(bknown,1); /* blessing or curse known */</span>
Bitfield(bknown,1); /* blessing or curse known */
+
<span id="line64">64.  Bitfield(rknown,1); /* rustproof or not known */</span>
Bitfield(rknown,1); /* rustproof or not known */
+
<span id="line65">65.  </span>
</pre>
 
  
 
[[Bitfield]]s use less than one byte in a struct. Most of these are obvious, like [[cursed]] or [[blessed]]. Four of these indicate what the adventurer knows about this object. In a game with more than one adventurer, these fields could not be stored in the object. Programming multiplayer [[NetHack]] would require many changes.
 
[[Bitfield]]s use less than one byte in a struct. Most of these are obvious, like [[cursed]] or [[blessed]]. Four of these indicate what the adventurer knows about this object. In a game with more than one adventurer, these fields could not be stored in the object. Programming multiplayer [[NetHack]] would require many changes.
  
<pre>
+
<span id="line66">66.  Bitfield(oeroded,2); /* rusted/burnt weapon/armor */</span>
Bitfield(oeroded,2); /* rusted/burnt weapon/armor */
+
<span id="line67">67.  Bitfield(oeroded2,2); /* corroded/rotted weapon/armor */</span>
Bitfield(oeroded2,2); /* corroded/rotted weapon/armor */
+
<span id="line68">68.  #define greatest_erosion(otmp) (int)((otmp)->oeroded > (otmp)->oeroded2 ? (otmp)->oeroded : (otmp)->oeroded2)</span>
#define greatest_erosion(otmp) (int)((otmp)->oeroded > \
+
<span id="line69">69.  #define MAX_ERODE 3</span>
  (otmp)->oeroded2 ? (otmp)->oeroded : (otmp)->oeroded2)
+
<span id="line70">70.  #define orotten oeroded /* rotten food */</span>
#define MAX_ERODE 3
+
<span id="line71">71.  #define odiluted oeroded /* diluted potions */</span>
#define orotten oeroded /* rotten food */
+
<span id="line72">72.  #define norevive oeroded2</span>
#define odiluted oeroded /* diluted potions */
 
#define norevive oeroded2
 
</pre>
 
 
 
'''Note:''' The <tt>greatest_erosion</tt> macro is defined on one line of the source code. [[We]] added the backslash-newline-space-space to this wiki because the line was too wide.
 
  
 
[[Object]]s can erode in two different ways.
 
[[Object]]s can erode in two different ways.
* <tt>thing->oeroded</tt> (or orotten or odiluted) indicates [[rusty]] [[metal]] or [[burnt]] [[wood]]. For [[food]] such as [[corpse]]s, this indicates how [[rotten]] it is. Also, NetHack uses this to mark a [[potion]] as [[diluted]].
+
* <tt>thing->oeroded</tt> (or orotten or odiluted) indicates [[rusty]] [[metal]] or [[erosion|burnt]] organic material. For [[food]] such as [[corpse]]s, this indicates how [[rotten]] it is. Also, NetHack uses this to mark a [[potion]] as [[diluted]].
* <tt>thing->oeroded2</tt> (or norevive) indicated [[corroded]] or [[rotted]] status. Sometimes [[acid]] causes this damage. The <tt>thing->norevive</tt> is probably a flag for NetHack to check whether [[undead turning]] would revive something, but we would need to check.
+
* <tt>thing->oeroded2</tt> (or norevive) indicated [[corroded]] metal or [[rotted]] organic material. Sometimes [[acid]] causes this damage. The <tt>thing->norevive</tt> is probably a flag for NetHack to check whether [[undead turning]] would revive something, but we would need to check.
  
Because <tt>thing</tt> might erode two different ways, a macro call to <tt>greatest_erosion</tt> returns the greater of the two erosions. There is a scale from 0 (no erosion) to 1 (eroded) to 2 (very eroded) to 3 (MAX_ERODE, which completely destroys an object).
+
Because <tt>thing</tt> might erode two different ways, a macro call to <tt>greatest_erosion</tt> returns the greater of the two erosions. There is a scale from 0 (no erosion) to 1 (eroded) to 2 (very eroded) to 3 (thoroughly eroded).
  
<pre>
+
<span id="line73">73.  Bitfield(oerodeproof,1); /* erodeproof weapon/armor */</span>
Bitfield(oerodeproof,1); /* erodeproof weapon/armor */
+
<span id="line74">74.  Bitfield(olocked,1); /* object is locked */</span>
Bitfield(olocked,1); /* object is locked */
+
<span id="line75">75.  Bitfield(obroken,1); /* lock has been broken */</span>
Bitfield(obroken,1); /* lock has been broken */
+
<span id="line76">76.  Bitfield(otrapped,1); /* container is trapped */</span>
Bitfield(otrapped,1); /* container is trapped */
+
<span id="line77">77.  /* or accidental tripped rolling boulder trap */</span>
/* or accidental tripped rolling boulder trap */
+
<span id="line78">78.  #define opoisoned otrapped /* object (weapon) is coated with poison */</span>
#define opoisoned otrapped /* object (weapon) is coated with poison */
+
<span id="line79">79.  </span>
</pre>
 
  
 
Here are some more boolean bits.
 
Here are some more boolean bits.
* Even though an object might erode two ways, there is exactly one "erodeproof" flag - setting it prevents both types of erosion.
+
* Even though an object might erode two ways, there is exactly one "erodeproof" flag - setting it prevents both types of erosion. This is why using the [[scroll of enchant weapon]] while [[confused]] will erodeproof any weapon. If <tt>obj->rknown</tt> is true than the code in [[objnam.c]] will refer to an erodeproof object as "fixed" if it is a [[crysknife]], else rustproof if the metal would normally rust, else corrodeproof if the metal would normally corrode, else fireproof if the object would be flammable. For example, an erodeproof [[dragon scale mail]] would never be marked as such in your [[inventory]], because dragon scales are neither crysknives nor rustprone nor corrodeable nor flammable.
 
* <tt>thing->olocked</tt> is set if an object such a [[chest]] or [[box]] is [[locked]]; <tt>thing->obroken</tt> if the lock is broken (though [[you]] could fix it with [[wizard lock]]).
 
* <tt>thing->olocked</tt> is set if an object such a [[chest]] or [[box]] is [[locked]]; <tt>thing->obroken</tt> if the lock is broken (though [[you]] could fix it with [[wizard lock]]).
* <tt>thing->otrapped</tt> (or synonym <tt>thing->opoisoned</tt>) is set to show that a [[chest]] or [[box]] contains a trap, such as a flame or poison needle, that will activate if you open it without [[untrap]]ping it first. It also marks if an object, such as a [[dagger]] or [[arrow]], has [[poison]].
+
* <tt>thing->otrapped</tt> (or synonym <tt>thing->opoisoned</tt>) indicates that a weapon is [[poison]]ed or that the [[chest]] or [[box]] contains a trap (which might be a flame or poison needle when you open it).
  
<pre>
+
<span id="line80">80.  Bitfield(recharged,3); /* number of times it's been recharged */</span>
Bitfield(recharged,3); /* number of times it's been recharged */
+
<span id="line81">81.  Bitfield(lamplit,1); /* a light-source -- can be lit */</span>
Bitfield(lamplit,1); /* a light-source -- can be lit */
+
<span id="line82">82.  #ifdef INVISIBLE_OBJECTS</span>
#ifdef INVISIBLE_OBJECTS
+
<span id="line83">83.  Bitfield(oinvis,1); /* invisible */</span>
Bitfield(oinvis,1); /* invisible */
+
<span id="line84">84.  #endif</span>
#endif
+
<span id="line85">85.  Bitfield(greased,1); /* covered with grease */</span>
Bitfield(greased,1); /* covered with grease */
+
<span id="line86">86.  Bitfield(oattached,2); /* obj struct has special attachment */</span>
Bitfield(oattached,2); /* obj struct has special attachment */
+
<span id="line87">87.  #define OATTACHED_NOTHING 0</span>
#define OATTACHED_NOTHING 0
+
<span id="line88">88.  #define OATTACHED_MONST  1 /* monst struct in oextra */</span>
#define OATTACHED_MONST  1 /* monst struct in oextra */
+
<span id="line89">89.  #define OATTACHED_M_ID    2 /* monst id in oextra */</span>
#define OATTACHED_M_ID    2 /* monst id in oextra */
+
<span id="line90">90.  #define OATTACHED_UNUSED3 3</span>
#define OATTACHED_UNUSED3 3
+
<span id="line91">91.  </span>
</pre>
 
  
* <tt>thing->recharged</tt> is the number of times that something was recharged, for example if <tt>thing</tt> is a [[wand]]. This can range from 0 to 7, though most wands [[explosion|explode]] much sooner than that.
+
* <tt>thing->recharged</tt> is the number of times that something was recharged, for example if <tt>thing</tt> is a [[wand]] or the [[Bell of Opening]]. This can range from 0 to 7, though most wands [[explosion|explode]] much sooner than that. This field complements <tt>thing->spe</tt> which is the number of charges in the wand.
 
* <tt>thing->lamplit</tt> is set if <tt>thing</tt> is a lamp, and you switch it on. You can switch on a lamp by [[apply]]ing it.
 
* <tt>thing->lamplit</tt> is set if <tt>thing</tt> is a lamp, and you switch it on. You can switch on a lamp by [[apply]]ing it.
* <tt>thing->oinvis</tt> can be set for an [[invisible]] object. NetHack 3.4.3 does not implement invisible objects, even though it has some code for it, so [[you]] will not actually find this set in a game.
+
* <tt>thing->oinvis</tt> can be set for an [[invisible]] object. NetHack 3.4.3 does not implement invisible objects, even though it has some code for it, so [[you]] will not actually find this field set in a game.
 
* <tt>thing->greased</tt> if it is [[greased]].
 
* <tt>thing->greased</tt> if it is [[greased]].
 
* <tt>thing->oattached</tt> is a number from 0 to 3 (one of the defined constants OATTACHED_NOTHING, ...) to indicate the meaning of <tt>thing->oextra</tt>, which is defined at the end of this struct.
 
* <tt>thing->oattached</tt> is a number from 0 to 3 (one of the defined constants OATTACHED_NOTHING, ...) to indicate the meaning of <tt>thing->oextra</tt>, which is defined at the end of this struct.
  
<pre>
+
<span id="line92">92.  Bitfield(in_use,1); /* for magic items before useup items */</span>
Bitfield(in_use,1); /* for magic items before useup items */
+
<span id="line93">93.  Bitfield(bypass,1); /* mark this as an object to be skipped by bhito() */</span>
Bitfield(bypass,1); /* mark this as an object to be skipped by bhito() */
+
<span id="line94">94.  /* 6 free bits */</span>
/* 6 free bits */
+
<span id="line95">95.  </span>
</pre>
 
  
 
...
 
...
  
<pre>
+
<span id="line96">96.  int corpsenm; /* type of corpse is mons[corpsenm] */</span>
int corpsenm; /* type of corpse is mons[corpsenm] */
+
<span id="line97">97.  #define leashmon  corpsenm /* gets m_id of attached pet */</span>
#define leashmon  corpsenm /* gets m_id of attached pet */
+
<span id="line98">98.  #define spestudied corpsenm /* # of times a spellbook has been studied */</span>
#define spestudied corpsenm /* # of times a spellbook has been studied */
+
<span id="line99">99.  #define fromsink  corpsenm /* a potion from a sink */</span>
#define fromsink  corpsenm /* a potion from a sink */
+
<span id="line100">100.  unsigned oeaten; /* nutrition left in food, if partly eaten */</span>
unsigned oeaten; /* nutrition left in food, if partly eaten */
+
<span id="line101">101.  long age; /* creation date */</span>
long age; /* creation date */
+
<span id="line102">102.  </span>
</pre>
 
  
The "corpsenm", "leashmon", "spestudied", and "fromsink" all refer to the same field of the object. This works because an object can only be one of a [[corpse]], [[leash]], [[spellbook]], or [[potion]].
+
The "corpsenm", "leashmon", "spestudied", and "fromsink" all refer to the same field of the object. This works because an object can only be one of a [[corpse]], [[leash]], [[spellbook]], or [[potion]]. (It might be possible to abolish this field and just widen <tt>thing->spe</tt> from schar to int, unless someone can find an object that needs to use both fields.)
  
<pre>
+
<span id="line103">103.  uchar onamelth; /* length of name (following oxlth) */</span>
uchar onamelth; /* length of name (following oxlth) */
+
<span id="line104">104.  short oxlth; /* length of following data */</span>
short oxlth; /* length of following data */
+
<span id="line105">105.  /* in order to prevent alignment problems oextra should</span>
/* in order to prevent alignment problems oextra should
+
<span id="line106">106.    be (or follow) a long int */</span>
  be (or follow) a long int */
+
<span id="line107">107.  long owornmask;</span>
long owornmask;
+
<span id="line108">108.  long oextra[1]; /* used for name of ordinary objects - length</span>
long oextra[1]; /* used for name of ordinary objects - length
+
<span id="line109">109.    is flexible; amount for tmp gold objects */</span>
  is flexible; amount for tmp gold objects */
+
<span id="line110">110.  };</span>
};
+
<span id="line111">111.  </span>
</pre>
 
  
 
As C is not an object-oriented language with inheritance, the programmers decided to use this hack to allow objects to have extra information. A <tt>long</tt> is 32 bits on some computers but possibly 64 bits on others.
 
As C is not an object-oriented language with inheritance, the programmers decided to use this hack to allow objects to have extra information. A <tt>long</tt> is 32 bits on some computers but possibly 64 bits on others.
 +
* <tt>thing->oextra</tt> is usually a pointer to a monst struct, but this depends what <tt>thing->oattached</tt> (above) is set to.
 +
* <tt>thing->oxlth</tt> is the length of the data pointed to by <tt>thing->oextra</tt>
  
 
=== useful macros ===
 
=== useful macros ===
<pre>
+
<span id="line112">112.  #define newobj(xl) (struct obj *)alloc((unsigned)(xl) + sizeof(struct obj))</span>
#define newobj(xl) (struct obj *)alloc((unsigned)(xl) + sizeof(struct obj))
+
<span id="line113">113.  #define ONAME(otmp) (((char *)(otmp)->oextra) + (otmp)->oxlth)</span>
#define ONAME(otmp) (((char *)(otmp)->oextra) + (otmp)->oxlth)
+
<span id="line114">114.  </span>
</pre>
 
  
 
* <tt>newobj</tt> uses the <tt>alloc</tt> function of NetHack to dynamically allocate memory in an appropriate way for the [[platform]]. For example, <tt>thing = newobj(10)</tt> allocates an object with 10 extra bytes, and stores a pointer to that object in <tt>thing</tt>.
 
* <tt>newobj</tt> uses the <tt>alloc</tt> function of NetHack to dynamically allocate memory in an appropriate way for the [[platform]]. For example, <tt>thing = newobj(10)</tt> allocates an object with 10 extra bytes, and stores a pointer to that object in <tt>thing</tt>.
Line 237: Line 212:
  
 
==== weapons ====
 
==== weapons ====
<pre>
+
<span id="line115">115.  /* Weapons and weapon-tools */</span>
/* Weapons and weapon-tools */
+
<span id="line116">116.  /* KMH -- now based on skill categories.  Formerly:</span>
/* KMH -- now based on skill categories.  Formerly:
+
  <span id="line117">117.  * #define is_sword(otmp) (otmp->oclass == WEAPON_CLASS && \</span>
  * #define is_sword(otmp) (otmp->oclass == WEAPON_CLASS && \
+
  <span id="line118">118.  * objects[otmp->otyp].oc_wepcat == WEP_SWORD)</span>
  * objects[otmp->otyp].oc_wepcat == WEP_SWORD)
+
  <span id="line119">119.  * #define is_blade(otmp) (otmp->oclass == WEAPON_CLASS && \</span>
  * #define is_blade(otmp) (otmp->oclass == WEAPON_CLASS && \
+
  <span id="line120">120.  * (objects[otmp->otyp].oc_wepcat == WEP_BLADE || \</span>
  * (objects[otmp->otyp].oc_wepcat == WEP_BLADE || \
+
  <span id="line121">121.  *   objects[otmp->otyp].oc_wepcat == WEP_SWORD))</span>
  *   objects[otmp->otyp].oc_wepcat == WEP_SWORD))
+
  <span id="line122">122.  * #define is_weptool(o) ((o)->oclass == TOOL_CLASS && \</span>
  * #define is_weptool(o) ((o)->oclass == TOOL_CLASS && \
+
  <span id="line123">123.  * objects[(o)->otyp].oc_weptool)</span>
  * objects[(o)->otyp].oc_weptool)
+
  <span id="line124">124.  * #define is_multigen(otyp) (otyp <= SHURIKEN)</span>
  * #define is_multigen(otyp) (otyp <= SHURIKEN)
+
  <span id="line125">125.  * #define is_poisonable(otyp) (otyp <= BEC_DE_CORBIN)</span>
  * #define is_poisonable(otyp) (otyp <= BEC_DE_CORBIN)
+
  <span id="line126">126.  */</span>
  */
+
<span id="line127">127.  #define is_blade(otmp) (otmp->oclass == WEAPON_CLASS && \</span>
#define is_blade(otmp) (otmp->oclass == WEAPON_CLASS && \
+
<span id="line128">128.  objects[otmp->otyp].oc_skill >= P_DAGGER && \</span>
objects[otmp->otyp].oc_skill >= P_DAGGER && \
+
<span id="line129">129.  objects[otmp->otyp].oc_skill <= P_SABER)</span>
objects[otmp->otyp].oc_skill <= P_SABER)
+
<span id="line130">130.  #define is_axe(otmp) ((otmp->oclass == WEAPON_CLASS || \</span>
#define is_axe(otmp) ((otmp->oclass == WEAPON_CLASS || \
+
<span id="line131">131.  otmp->oclass == TOOL_CLASS) && \</span>
otmp->oclass == TOOL_CLASS) && \
+
<span id="line132">132.  objects[otmp->otyp].oc_skill == P_AXE)</span>
objects[otmp->otyp].oc_skill == P_AXE)
+
<span id="line133">133.  #define is_pick(otmp) ((otmp->oclass == WEAPON_CLASS || \</span>
#define is_pick(otmp) ((otmp->oclass == WEAPON_CLASS || \
+
<span id="line134">134.  otmp->oclass == TOOL_CLASS) && \</span>
otmp->oclass == TOOL_CLASS) && \
+
<span id="line135">135.  objects[otmp->otyp].oc_skill == P_PICK_AXE)</span>
objects[otmp->otyp].oc_skill == P_PICK_AXE)
+
<span id="line136">136.  #define is_sword(otmp) (otmp->oclass == WEAPON_CLASS && \</span>
#define is_sword(otmp) (otmp->oclass == WEAPON_CLASS && \
+
<span id="line137">137.  objects[otmp->otyp].oc_skill >= P_SHORT_SWORD && \</span>
objects[otmp->otyp].oc_skill >= P_SHORT_SWORD && \
+
<span id="line138">138.  objects[otmp->otyp].oc_skill <= P_SABER)</span>
objects[otmp->otyp].oc_skill <= P_SABER)
+
<span id="line139">139.  #define is_pole(otmp) ((otmp->oclass == WEAPON_CLASS || \</span>
#define is_pole(otmp) ((otmp->oclass == WEAPON_CLASS || \
+
<span id="line140">140.  otmp->oclass == TOOL_CLASS) && \</span>
otmp->oclass == TOOL_CLASS) && \
+
<span id="line141">141.  (objects[otmp->otyp].oc_skill == P_POLEARMS || \</span>
(objects[otmp->otyp].oc_skill == P_POLEARMS || \
+
<span id="line142">142.  objects[otmp->otyp].oc_skill == P_LANCE))</span>
objects[otmp->otyp].oc_skill == P_LANCE))
+
<span id="line143">143.  #define is_spear(otmp) (otmp->oclass == WEAPON_CLASS && \</span>
#define is_spear(otmp) (otmp->oclass == WEAPON_CLASS && \
+
<span id="line144">144.  objects[otmp->otyp].oc_skill >= P_SPEAR && \</span>
objects[otmp->otyp].oc_skill >= P_SPEAR && \
+
<span id="line145">145.  objects[otmp->otyp].oc_skill <= P_JAVELIN)</span>
objects[otmp->otyp].oc_skill <= P_JAVELIN)
+
<span id="line146">146.  #define is_launcher(otmp) (otmp->oclass == WEAPON_CLASS && \</span>
#define is_launcher(otmp) (otmp->oclass == WEAPON_CLASS && \
+
<span id="line147">147.  objects[otmp->otyp].oc_skill >= P_BOW && \</span>
objects[otmp->otyp].oc_skill >= P_BOW && \
+
<span id="line148">148.  objects[otmp->otyp].oc_skill <= P_CROSSBOW)</span>
objects[otmp->otyp].oc_skill <= P_CROSSBOW)
+
<span id="line149">149.  #define is_ammo(otmp) ((otmp->oclass == WEAPON_CLASS || \</span>
#define is_ammo(otmp) ((otmp->oclass == WEAPON_CLASS || \
+
<span id="line150">150.  otmp->oclass == GEM_CLASS) && \</span>
otmp->oclass == GEM_CLASS) && \
+
<span id="line151">151.  objects[otmp->otyp].oc_skill >= -P_CROSSBOW && \</span>
objects[otmp->otyp].oc_skill >= -P_CROSSBOW && \
+
<span id="line152">152.  objects[otmp->otyp].oc_skill <= -P_BOW)</span>
objects[otmp->otyp].oc_skill <= -P_BOW)
+
<span id="line153">153.  #define ammo_and_launcher(otmp,ltmp) \</span>
#define ammo_and_launcher(otmp,ltmp) \
+
<span id="line154">154.  (is_ammo(otmp) && (ltmp) && \</span>
(is_ammo(otmp) && (ltmp) && \
+
<span id="line155">155.  objects[(otmp)->otyp].oc_skill == -objects[(ltmp)->otyp].oc_skill)</span>
objects[(otmp)->otyp].oc_skill == -objects[(ltmp)->otyp].oc_skill)
+
<span id="line156">156.  #define is_missile(otmp) ((otmp->oclass == WEAPON_CLASS || \</span>
#define is_missile(otmp) ((otmp->oclass == WEAPON_CLASS || \
+
<span id="line157">157.  otmp->oclass == TOOL_CLASS) && \</span>
otmp->oclass == TOOL_CLASS) && \
+
<span id="line158">158.  objects[otmp->otyp].oc_skill >= -P_BOOMERANG && \</span>
objects[otmp->otyp].oc_skill >= -P_BOOMERANG && \
+
<span id="line159">159.  objects[otmp->otyp].oc_skill <= -P_DART)</span>
objects[otmp->otyp].oc_skill <= -P_DART)
+
<span id="line160">160.  #define is_weptool(o) ((o)->oclass == TOOL_CLASS && \</span>
#define is_weptool(o) ((o)->oclass == TOOL_CLASS && \
+
<span id="line161">161.  objects[(o)->otyp].oc_skill != P_NONE)</span>
objects[(o)->otyp].oc_skill != P_NONE)
+
<span id="line162">162.  #define bimanual(otmp) ((otmp->oclass == WEAPON_CLASS || \</span>
#define bimanual(otmp) ((otmp->oclass == WEAPON_CLASS || \
+
<span id="line163">163.  otmp->oclass == TOOL_CLASS) && \</span>
otmp->oclass == TOOL_CLASS) && \
+
<span id="line164">164.  objects[otmp->otyp].oc_bimanual)</span>
objects[otmp->otyp].oc_bimanual)
+
<span id="line165">165.  #define is_multigen(otmp) (otmp->oclass == WEAPON_CLASS && \</span>
#define is_multigen(otmp) (otmp->oclass == WEAPON_CLASS && \
+
<span id="line166">166.  objects[otmp->otyp].oc_skill >= -P_SHURIKEN && \</span>
objects[otmp->otyp].oc_skill >= -P_SHURIKEN && \
+
<span id="line167">167.  objects[otmp->otyp].oc_skill <= -P_BOW)</span>
objects[otmp->otyp].oc_skill <= -P_BOW)
+
<span id="line168">168.  #define is_poisonable(otmp) (otmp->oclass == WEAPON_CLASS && \</span>
#define is_poisonable(otmp) (otmp->oclass == WEAPON_CLASS && \
+
<span id="line169">169.  objects[otmp->otyp].oc_skill >= -P_SHURIKEN && \</span>
objects[otmp->otyp].oc_skill >= -P_SHURIKEN && \
+
<span id="line170">170.  objects[otmp->otyp].oc_skill <= -P_BOW)</span>
objects[otmp->otyp].oc_skill <= -P_BOW)
+
<span id="line171">171.  #define uslinging() (uwep && objects[uwep->otyp].oc_skill == P_SLING)</span>
#define uslinging() (uwep && objects[uwep->otyp].oc_skill == P_SLING)
+
<span id="line172">172.  </span>
</pre>
 
  
These macros act as functions to test various properties of weapons. Some of these work by quering the class of the object, others look in the <tt>objects</tt> global array for more information about objects of that type.
+
These macros act as functions to test various properties of weapons. Some of these work by querying the class of the object, others look in the <tt>objects</tt> global array for more information about objects of that type.
  
For example, a "launcher" (<tt>is_launcher(thing)</tt>) is any object that is a weapon, that needs [[you]]r "bow" or "crossbow" [[skill]]. That is, if you can use your bow or crossbow skill with the object, then the object qualifies as a bow or crossbow for the purposes of the game. So [[wield]] it, quiver those [[arrow]]s or [[crossbow bolt]]s, and [[fire]]!
+
For example, a "launcher" (<tt>is_launcher(thing)</tt>) is any weapon that needs [[you]]r "bow" or "crossbow" [[skill]]. That is, if you can use your bow or crossbow skill with the object, then the object qualifies as a bow or crossbow for the purposes of the game. So [[wield]] it, quiver those [[arrow]]s or [[crossbow bolt]]s, and [[fire]]!
  
 
==== armor ====
 
==== armor ====
<pre>
+
<span id="line173">173.  /* Armor */</span>
/* Armor */
+
<span id="line174">174.  #define is_shield(otmp) (otmp->oclass == ARMOR_CLASS && \</span>
#define is_shield(otmp) (otmp->oclass == ARMOR_CLASS && \
+
<span id="line175">175.  objects[otmp->otyp].oc_armcat == ARM_SHIELD)</span>
objects[otmp->otyp].oc_armcat == ARM_SHIELD)
+
<span id="line176">176.  #define is_helmet(otmp) (otmp->oclass == ARMOR_CLASS && \</span>
#define is_helmet(otmp) (otmp->oclass == ARMOR_CLASS && \
+
<span id="line177">177.  objects[otmp->otyp].oc_armcat == ARM_HELM)</span>
objects[otmp->otyp].oc_armcat == ARM_HELM)
+
<span id="line178">178.  #define is_boots(otmp) (otmp->oclass == ARMOR_CLASS && \</span>
#define is_boots(otmp) (otmp->oclass == ARMOR_CLASS && \
+
<span id="line179">179.  objects[otmp->otyp].oc_armcat == ARM_BOOTS)</span>
objects[otmp->otyp].oc_armcat == ARM_BOOTS)
+
<span id="line180">180.  #define is_gloves(otmp) (otmp->oclass == ARMOR_CLASS && \</span>
#define is_gloves(otmp) (otmp->oclass == ARMOR_CLASS && \
+
<span id="line181">181.  objects[otmp->otyp].oc_armcat == ARM_GLOVES)</span>
objects[otmp->otyp].oc_armcat == ARM_GLOVES)
+
<span id="line182">182.  #define is_cloak(otmp) (otmp->oclass == ARMOR_CLASS && \</span>
#define is_cloak(otmp) (otmp->oclass == ARMOR_CLASS && \
+
<span id="line183">183.  objects[otmp->otyp].oc_armcat == ARM_CLOAK)</span>
objects[otmp->otyp].oc_armcat == ARM_CLOAK)
+
<span id="line184">184.  #define is_shirt(otmp) (otmp->oclass == ARMOR_CLASS && \</span>
#define is_shirt(otmp) (otmp->oclass == ARMOR_CLASS && \
+
<span id="line185">185.  objects[otmp->otyp].oc_armcat == ARM_SHIRT)</span>
objects[otmp->otyp].oc_armcat == ARM_SHIRT)
+
<span id="line186">186.  #define is_suit(otmp) (otmp->oclass == ARMOR_CLASS && \</span>
#define is_suit(otmp) (otmp->oclass == ARMOR_CLASS && \
+
<span id="line187">187.  objects[otmp->otyp].oc_armcat == ARM_SUIT)</span>
objects[otmp->otyp].oc_armcat == ARM_SUIT)
+
<span id="line188">188.  #define is_elven_armor(otmp) ((otmp)->otyp == ELVEN_LEATHER_HELM\</span>
#define is_elven_armor(otmp) ((otmp)->otyp == ELVEN_LEATHER_HELM\
+
<span id="line189">189.  || (otmp)->otyp == ELVEN_MITHRIL_COAT\</span>
|| (otmp)->otyp == ELVEN_MITHRIL_COAT\
+
<span id="line190">190.  || (otmp)->otyp == ELVEN_CLOAK\</span>
|| (otmp)->otyp == ELVEN_CLOAK\
+
<span id="line191">191.  || (otmp)->otyp == ELVEN_SHIELD\</span>
|| (otmp)->otyp == ELVEN_SHIELD\
+
<span id="line192">192.  || (otmp)->otyp == ELVEN_BOOTS)</span>
|| (otmp)->otyp == ELVEN_BOOTS)
+
<span id="line193">193.  #define is_orcish_armor(otmp) ((otmp)->otyp == ORCISH_HELM\</span>
#define is_orcish_armor(otmp) ((otmp)->otyp == ORCISH_HELM\
+
<span id="line194">194.  || (otmp)->otyp == ORCISH_CHAIN_MAIL\</span>
|| (otmp)->otyp == ORCISH_CHAIN_MAIL\
+
<span id="line195">195.  || (otmp)->otyp == ORCISH_RING_MAIL\</span>
|| (otmp)->otyp == ORCISH_RING_MAIL\
+
<span id="line196">196.  || (otmp)->otyp == ORCISH_CLOAK\</span>
|| (otmp)->otyp == ORCISH_CLOAK\
+
<span id="line197">197.  || (otmp)->otyp == URUK_HAI_SHIELD\</span>
|| (otmp)->otyp == URUK_HAI_SHIELD\
+
<span id="line198">198.  || (otmp)->otyp == ORCISH_SHIELD)</span>
|| (otmp)->otyp == ORCISH_SHIELD)
+
<span id="line199">199.  #define is_dwarvish_armor(otmp) ((otmp)->otyp == DWARVISH_IRON_HELM\</span>
#define is_dwarvish_armor(otmp) ((otmp)->otyp == DWARVISH_IRON_HELM\
+
<span id="line200">200.  || (otmp)->otyp == DWARVISH_MITHRIL_COAT\</span>
|| (otmp)->otyp == DWARVISH_MITHRIL_COAT\
+
<span id="line201">201.  || (otmp)->otyp == DWARVISH_CLOAK\</span>
|| (otmp)->otyp == DWARVISH_CLOAK\
+
<span id="line202">202.  || (otmp)->otyp == DWARVISH_ROUNDSHIELD)</span>
|| (otmp)->otyp == DWARVISH_ROUNDSHIELD)
+
<span id="line203">203.  #define is_gnomish_armor(otmp) (FALSE)</span>
#define is_gnomish_armor(otmp) (FALSE)
+
<span id="line204">204.  </span>
</pre>
+
<span id="line205">205.  </span>
 
+
 
Each type of [[object]] has information about it in the global <tt>objects</tt> array. This information includes the [[armor]] category: [[shield]], [[helm]], [[boots]], [[gloves]], [[cloak]], [[shirt]]. The game prevents [[you]] from [[wear]]ing two helms simultaneously, which is why it needs to know about these classes. The <tt>is_helmet(thing)</tt> macro tests if <tt>thing</tt> qualifies as a helm. If it does, and you are already wearing a helment, then you cannot wear <tt>thing</tt>.
 
Each type of [[object]] has information about it in the global <tt>objects</tt> array. This information includes the [[armor]] category: [[shield]], [[helm]], [[boots]], [[gloves]], [[cloak]], [[shirt]]. The game prevents [[you]] from [[wear]]ing two helms simultaneously, which is why it needs to know about these classes. The <tt>is_helmet(thing)</tt> macro tests if <tt>thing</tt> qualifies as a helm. If it does, and you are already wearing a helment, then you cannot wear <tt>thing</tt>.
  
 
==== Eggs and other food ====
 
==== Eggs and other food ====
<pre>
+
<span id="line206">206.  /* Eggs and other food */</span>
/* Eggs and other food */
+
<span id="line207">207.  #define MAX_EGG_HATCH_TIME 200 /* longest an egg can remain unhatched */</span>
#define MAX_EGG_HATCH_TIME 200 /* longest an egg can remain unhatched */
+
<span id="line208">208.  #define stale_egg(egg) ((monstermoves - (egg)->age) > (2*MAX_EGG_HATCH_TIME))</span>
#define stale_egg(egg) ((monstermoves - (egg)->age) > (2*MAX_EGG_HATCH_TIME))
+
<span id="line209">209.  #define ofood(o) ((o)->otyp == CORPSE || (o)->otyp == EGG || (o)->otyp == TIN)</span>
#define ofood(o) ((o)->otyp == CORPSE || (o)->otyp == EGG || (o)->otyp == TIN)
+
<span id="line210">210.  #define polyfodder(obj) (ofood(obj) && \</span>
#define polyfodder(obj) (ofood(obj) && \
+
<span id="line211">211.  pm_to_cham((obj)->corpsenm) != CHAM_ORDINARY)</span>
pm_to_cham((obj)->corpsenm) != CHAM_ORDINARY)
+
<span id="line212">212.  #define mlevelgain(obj) (ofood(obj) && (obj)->corpsenm == PM_WRAITH)</span>
#define mlevelgain(obj) (ofood(obj) && (obj)->corpsenm == PM_WRAITH)
+
<span id="line213">213.  #define mhealup(obj) (ofood(obj) && (obj)->corpsenm == PM_NURSE)</span>
#define mhealup(obj) (ofood(obj) && (obj)->corpsenm == PM_NURSE)
+
<span id="line214">214.  </span>
</pre>
 
  
 
These are some macros related to [[egg]]s (which might hatch!) and [[food]]. It is apparent from this code which [[corpse]] will level you up or heal you when you eat it. No other corpse but that of a [[wraith]] levels you up; no other corpse but that of a [[nurse]] heals you.
 
These are some macros related to [[egg]]s (which might hatch!) and [[food]]. It is apparent from this code which [[corpse]] will level you up or heal you when you eat it. No other corpse but that of a [[wraith]] levels you up; no other corpse but that of a [[nurse]] heals you.
  
 
==== Containers ====
 
==== Containers ====
<pre>
+
<span id="line215">215.  /* Containers */</span>
/* Containers */
+
<span id="line216">216.  #define carried(o) ((o)->where == OBJ_INVENT)</span>
#define carried(o) ((o)->where == OBJ_INVENT)
+
<span id="line217">217.  #define mcarried(o) ((o)->where == OBJ_MINVENT)</span>
#define mcarried(o) ((o)->where == OBJ_MINVENT)
+
<span id="line218">218.  #define Has_contents(o) (/* (Is_container(o) || (o)->otyp == STATUE) && */ \</span>
#define Has_contents(o) (/* (Is_container(o) || (o)->otyp == STATUE) && */ \
+
<span id="line219">219.  (o)->cobj != (struct obj *)0)</span>
(o)->cobj != (struct obj *)0)
+
<span id="line220">220.  #define Is_container(o) ((o)->otyp >= LARGE_BOX && (o)->otyp <= BAG_OF_TRICKS)</span>
#define Is_container(o) ((o)->otyp >= LARGE_BOX && (o)->otyp <= BAG_OF_TRICKS)
+
<span id="line221">221.  #define Is_box(otmp) (otmp->otyp == LARGE_BOX || otmp->otyp == CHEST)</span>
#define Is_box(otmp) (otmp->otyp == LARGE_BOX || otmp->otyp == CHEST)
+
<span id="line222">222.  #define Is_mbag(otmp) (otmp->otyp == BAG_OF_HOLDING || \</span>
#define Is_mbag(otmp) (otmp->otyp == BAG_OF_HOLDING || \
+
<span id="line223">223.  otmp->otyp == BAG_OF_TRICKS)</span>
otmp->otyp == BAG_OF_TRICKS)
+
<span id="line224">224.  </span>
</pre>
 
  
 
There is are macros to check if an [[object]] is in your inventory or that of a [[monster]]. It is slightly easier to say <tt>carried(thing)</tt> than <tt>thing->where == OBJ_INVENT</tt>. There are also several macros for containers.
 
There is are macros to check if an [[object]] is in your inventory or that of a [[monster]]. It is slightly easier to say <tt>carried(thing)</tt> than <tt>thing->where == OBJ_INVENT</tt>. There are also several macros for containers.
Line 370: Line 342:
  
 
==== Items from dragons, dwarves, elves, gnomes ====
 
==== Items from dragons, dwarves, elves, gnomes ====
<pre>
+
<span id="line225">225.  /* dragon gear */</span>
/* dragon gear */
+
<span id="line226">226.  #define Is_dragon_scales(obj) ((obj)->otyp >= GRAY_DRAGON_SCALES && \</span>
#define Is_dragon_scales(obj) ((obj)->otyp >= GRAY_DRAGON_SCALES && \
+
<span id="line227">227.  (obj)->otyp <= YELLOW_DRAGON_SCALES)</span>
(obj)->otyp <= YELLOW_DRAGON_SCALES)
+
<span id="line228">228.  #define Is_dragon_mail(obj) ((obj)->otyp >= GRAY_DRAGON_SCALE_MAIL && \</span>
#define Is_dragon_mail(obj) ((obj)->otyp >= GRAY_DRAGON_SCALE_MAIL && \
+
<span id="line229">229.  (obj)->otyp <= YELLOW_DRAGON_SCALE_MAIL)</span>
(obj)->otyp <= YELLOW_DRAGON_SCALE_MAIL)
+
<span id="line230">230.  #define Is_dragon_armor(obj) (Is_dragon_scales(obj) || Is_dragon_mail(obj))</span>
#define Is_dragon_armor(obj) (Is_dragon_scales(obj) || Is_dragon_mail(obj))
+
<span id="line231">231.  #define Dragon_scales_to_pm(obj) &mons[PM_GRAY_DRAGON + (obj)->otyp \</span>
#define Dragon_scales_to_pm(obj) &mons[PM_GRAY_DRAGON + (obj)->otyp \
+
<span id="line232">232.        - GRAY_DRAGON_SCALES]</span>
      - GRAY_DRAGON_SCALES]
+
<span id="line233">233.  #define Dragon_mail_to_pm(obj) &mons[PM_GRAY_DRAGON + (obj)->otyp \</span>
#define Dragon_mail_to_pm(obj) &mons[PM_GRAY_DRAGON + (obj)->otyp \
+
<span id="line234">234.        - GRAY_DRAGON_SCALE_MAIL]</span>
      - GRAY_DRAGON_SCALE_MAIL]
 
#define Dragon_to_scales(pm) (GRAY_DRAGON_SCALES + (pm - mons))
 
  
/* Elven gear */
+
These macros facilitate code that needs to know about [[dragon scales]], [[dragon scale mail]] and their matching [[dragon]]s. It assumes that the range of dragon colors starts with gray and ends with yellow. The <tt>obj</tt> parameter is a pointer to an object, but these macros only examine the object type.
#define is_elven_weapon(otmp) ((otmp)->otyp == ELVEN_ARROW\
 
|| (otmp)->otyp == ELVEN_SPEAR\
 
|| (otmp)->otyp == ELVEN_DAGGER\
 
|| (otmp)->otyp == ELVEN_SHORT_SWORD\
 
|| (otmp)->otyp == ELVEN_BROADSWORD\
 
|| (otmp)->otyp == ELVEN_BOW)
 
#define is_elven_obj(otmp) (is_elven_armor(otmp) || is_elven_weapon(otmp))
 
  
/* Orcish gear */
+
If a monster does [[polymorph]] while wearing dragon scales or dragon scale mail, the code uses <tt>Dragon_scales_to_pm</tt> or <tt>Dragon_mail_to_pm</tt> to polymorph you into the matching dragon: [[mon.c#line2351]], [[muse.c#line1684]]. Then the game disables the message that the dragon would break out of its armor: [[worn.c#line605]]. (The code in [[polyself.c]] uses a separate function called [[polyself.c#armor_to_dragon |<tt>armor_to_dragon</tt>]].)
#define is_orcish_obj(otmp) (is_orcish_armor(otmp)\
 
|| (otmp)->otyp == ORCISH_ARROW\
 
|| (otmp)->otyp == ORCISH_SPEAR\
 
|| (otmp)->otyp == ORCISH_DAGGER\
 
|| (otmp)->otyp == ORCISH_SHORT_SWORD\
 
|| (otmp)->otyp == ORCISH_BOW)
 
  
/* Dwarvish gear */
+
<span id="line235">235.  #define Dragon_to_scales(pm) (GRAY_DRAGON_SCALES + (pm - mons))</span>
#define is_dwarvish_obj(otmp) (is_dwarvish_armor(otmp)\
+
<span id="line236">236.  </span>
|| (otmp)->otyp == DWARVISH_SPEAR\
 
|| (otmp)->otyp == DWARVISH_SHORT_SWORD\
 
|| (otmp)->otyp == DWARVISH_MATTOCK)
 
  
/* Gnomish gear */
+
Do not use the <tt>Dragon_to_scales</tt> macro! The [[DevTeam]] left this broken and incorrect definition of a macro, but never called this macro from the NetHack source code. If <tt>pm</tt> is a monster type (an index into the <tt>mons</tt> array), then the correct version of the macro would expand to <tt>(GRAY_DRAGON_SCALES + (pm - PM_GRAY_DRAGON))</tt>. NetHack uses the correct formula at [[mon.c#line194]] to pick the correct scales for the [[death drop]] of a dragon.
#define is_gnomish_obj(otmp) (is_gnomish_armor(otmp))
 
</pre>
 
  
==== Candles, lamps ====
+
<span id="line237">237.  /* Elven gear */</span>
<pre>
+
<span id="line238">238.  #define is_elven_weapon(otmp) ((otmp)->otyp == ELVEN_ARROW\</span>
/* Light sources */
+
<span id="line239">239.  || (otmp)->otyp == ELVEN_SPEAR\</span>
#define Is_candle(otmp) (otmp->otyp == TALLOW_CANDLE || \
+
<span id="line240">240.  || (otmp)->otyp == ELVEN_DAGGER\</span>
otmp->otyp == WAX_CANDLE)
+
<span id="line241">241.  || (otmp)->otyp == ELVEN_SHORT_SWORD\</span>
#define MAX_OIL_IN_FLASK 400 /* maximum amount of oil in a potion of oil */
+
<span id="line242">242.  || (otmp)->otyp == ELVEN_BROADSWORD\</span>
 +
<span id="line243">243.  || (otmp)->otyp == ELVEN_BOW)</span>
 +
<span id="line244">244.  #define is_elven_obj(otmp) (is_elven_armor(otmp) || is_elven_weapon(otmp))</span>
 +
<span id="line245">245.  </span>
 +
<span id="line246">246.  /* Orcish gear */</span>
 +
<span id="line247">247.  #define is_orcish_obj(otmp) (is_orcish_armor(otmp)\</span>
 +
<span id="line248">248.  || (otmp)->otyp == ORCISH_ARROW\</span>
 +
<span id="line249">249.  || (otmp)->otyp == ORCISH_SPEAR\</span>
 +
<span id="line250">250.  || (otmp)->otyp == ORCISH_DAGGER\</span>
 +
<span id="line251">251.  || (otmp)->otyp == ORCISH_SHORT_SWORD\</span>
 +
<span id="line252">252.  || (otmp)->otyp == ORCISH_BOW)</span>
 +
<span id="line253">253.  </span>
 +
<span id="line254">254.  /* Dwarvish gear */</span>
 +
<span id="line255">255.  #define is_dwarvish_obj(otmp) (is_dwarvish_armor(otmp)\</span>
 +
<span id="line256">256.  || (otmp)->otyp == DWARVISH_SPEAR\</span>
 +
<span id="line257">257.  || (otmp)->otyp == DWARVISH_SHORT_SWORD\</span>
 +
<span id="line258">258.  || (otmp)->otyp == DWARVISH_MATTOCK)</span>
 +
<span id="line259">259.  </span>
 +
<span id="line260">260.  /* Gnomish gear */</span>
 +
<span id="line261">261.  #define is_gnomish_obj(otmp) (is_gnomish_armor(otmp))</span>
 +
<span id="line262">262.  </span>
  
/* MAGIC_LAMP intentionally excluded below */
+
These macros tests if an object is elven, orcish, dwarvish or gnomish. (How is this important, and where are the definitions of the is_''foo''_armor macros?) These macros are boolean expressions that check if the object is of one type or another; the macro for is_elven_weapon literally checks the object against the macro's list of six known elven weapons. If you added a new elven weapon to [[objects.c]], then you would also have to add it to this macro.
/* age field of this is relative age rather than absolute */
 
#define age_is_relative(otmp) ((otmp)->otyp == BRASS_LANTERN\
 
|| (otmp)->otyp == OIL_LAMP\
 
|| (otmp)->otyp == CANDELABRUM_OF_INVOCATION\
 
|| (otmp)->otyp == TALLOW_CANDLE\
 
|| (otmp)->otyp == WAX_CANDLE\
 
|| (otmp)->otyp == POT_OIL)
 
/* object can be ignited */
 
#define ignitable(otmp) ((otmp)->otyp == BRASS_LANTERN\
 
|| (otmp)->otyp == OIL_LAMP\
 
|| (otmp)->otyp == CANDELABRUM_OF_INVOCATION\
 
|| (otmp)->otyp == TALLOW_CANDLE\
 
|| (otmp)->otyp == WAX_CANDLE\
 
|| (otmp)->otyp == POT_OIL)
 
</pre>
 
  
==== Stones and other miscellaneous ====
+
One may consider refactoring this, so that <tt>struct objclass</tt> in [[objclass.h]] contains new bitfields oc_elven, oc_orcish, oc_dwarvish, oc_gnomish. Then one would have macros like so,
<pre>
 
/* special stones */
 
#define is_graystone(obj) ((obj)->otyp == LUCKSTONE || \
 
(obj)->otyp == LOADSTONE || \
 
(obj)->otyp == FLINT    || \
 
(obj)->otyp == TOUCHSTONE)
 
  
/* misc */
+
/* not part of NetHack */
#ifdef KOPS
+
#define is_elven_weapon(otmp) (is_elven_object(otmp) && objects[otmp->otyp].oc_class == WEAPON_CLASS)
#define is_flimsy(otmp) (objects[(otmp)->otyp].oc_material <= LEATHER || \
+
#define is_elven_object(otmp) (objects[otmp->otyp].oc_elven)
(otmp)->otyp == RUBBER_HOSE)
 
#else
 
#define is_flimsy(otmp) (objects[(otmp)->otyp].oc_material <= LEATHER)
 
#endif
 
  
/* helpers, simple enough to be macros */
+
Then we could adjust the macros in [[object.c]] to clear the oc_orcish on most objects, but to set the flag on orcish objects. The advantage is that it slightly easier for a [[variant]] to add a new type of orcish object to the game. The disadvantage is that the vast majority of object types are neither elven nor orcish nor dwarvish nor gnomish, so we would be allocating four extra zero bits (negligible with yesterday's computers) for each object type.
#define is_plural(o) ((o)->quan > 1 || \
 
(o)->oartifact == ART_EYES_OF_THE_OVERWORLD)
 
  
/* Flags for get_obj_location(). */
+
==== Candles, lamps ====
#define CONTAINED_TOO 0x1
+
<span id="line263">263.  /* Light sources */</span>
#define BURIED_TOO 0x2
+
<span id="line264">264.  #define Is_candle(otmp) (otmp->otyp == TALLOW_CANDLE || \</span>
 +
<span id="line265">265. otmp->otyp == WAX_CANDLE)</span>
 +
<span id="line266">266.  #define MAX_OIL_IN_FLASK 400 /* maximum amount of oil in a potion of oil */</span>
 +
<span id="line267">267.  </span>
 +
<span id="line268">268.  /* MAGIC_LAMP intentionally excluded below */</span>
 +
<span id="line269">269.  /* age field of this is relative age rather than absolute */</span>
 +
<span id="line270">270.  #define age_is_relative(otmp) ((otmp)->otyp == BRASS_LANTERN\</span>
 +
<span id="line271">271.  || (otmp)->otyp == OIL_LAMP\</span>
 +
<span id="line272">272.  || (otmp)->otyp == CANDELABRUM_OF_INVOCATION\</span>
 +
<span id="line273">273.  || (otmp)->otyp == TALLOW_CANDLE\</span>
 +
<span id="line274">274.  || (otmp)->otyp == WAX_CANDLE\</span>
 +
<span id="line275">275.  || (otmp)->otyp == POT_OIL)</span>
 +
<span id="line276">276.  /* object can be ignited */</span>
 +
<span id="line277">277.  #define ignitable(otmp) ((otmp)->otyp == BRASS_LANTERN\</span>
 +
<span id="line278">278.  || (otmp)->otyp == OIL_LAMP\</span>
 +
<span id="line279">279.  || (otmp)->otyp == CANDELABRUM_OF_INVOCATION\</span>
 +
<span id="line280">280.  || (otmp)->otyp == TALLOW_CANDLE\</span>
 +
<span id="line281">281.  || (otmp)->otyp == WAX_CANDLE\</span>
 +
<span id="line282">282.  || (otmp)->otyp == POT_OIL)</span>
 +
<span id="line283">283.  </span>
  
#endif /* OBJ_H */
+
==== Stones and other miscellaneous ====
</pre>
+
<span id="line284">284.  /* special stones */</span>
 +
<span id="line285">285.  #define is_graystone(obj) ((obj)->otyp == LUCKSTONE || \</span>
 +
<span id="line286">286.  (obj)->otyp == LOADSTONE || \</span>
 +
<span id="line287">287.  (obj)->otyp == FLINT    || \</span>
 +
<span id="line288">288.  (obj)->otyp == TOUCHSTONE)</span>
 +
<span id="line289">289.  </span>
 +
<span id="line290">290.  /* misc */</span>
 +
<span id="line291">291.  #ifdef KOPS</span>
 +
<span id="line292">292.  #define is_flimsy(otmp) (objects[(otmp)->otyp].oc_material <= LEATHER || \</span>
 +
<span id="line293">293.  (otmp)->otyp == RUBBER_HOSE)</span>
 +
<span id="line294">294.  #else</span>
 +
<span id="line295">295.  #define is_flimsy(otmp) (objects[(otmp)->otyp].oc_material <= LEATHER)</span>
 +
<span id="line296">296.  #endif</span>
 +
<span id="line297">297.  </span>
 +
<span id="line298">298.  /* helpers, simple enough to be macros */</span>
 +
<span id="line299">299.  #define is_plural(o) ((o)->quan > 1 || \</span>
 +
<span id="line300">300.  (o)->oartifact == ART_EYES_OF_THE_OVERWORLD)</span>
 +
<span id="line301">301.  </span>
 +
<span id="line302">302.  /* Flags for get_obj_location(). */</span>
 +
<span id="line303">303.  #define CONTAINED_TOO 0x1</span>
 +
<span id="line304">304.  #define BURIED_TOO 0x2</span>
 +
<span id="line305">305.  </span>
 +
<span id="line306">306.  #endif /* OBJ_H */</span>
  
 
[[Category:Source code]]
 
[[Category:Source code]]

Latest revision as of 19:19, 31 January 2011

Below is the full text to include/obj.h from NetHack 3.4.3. To link to a particular line, write [[obj.h#line123]], for example.

This particular file contains C-language declarations for objects. In particular, it declares struct obj, the data structure representing an object in the dungeon. This data structure contains many fields about the object, such as the type of the object, the BUC status, the number of charges, the erosion, the quantity of this object in a stack, the pointer to the monster presently carrying it, the flag indicating whether the player has identified the object, and many other fields.

For the purposes of this annotation, we declare a pointer to an example object: struct obj *thing;

Top of file

/*	SCCS Id: @(#)obj.h	3.4	2002/01/07	*/
/* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
/* NetHack may be freely redistributed.  See license for details. */

#ifndef OBJ_H
#define OBJ_H

/* #define obj obj_nh */ /* uncomment for SCO UNIX, which has a conflicting
			  * typedef for "obj" in <sys/types.h> */

union vptrs

union vptrs {
	    struct obj *v_nexthere;	/* floor location lists */
	    struct obj *v_ocontainer;	/* point back to container */
	    struct monst *v_ocarry;	/* point back to carrying monst */
};

Each struct obj (defined in the next section) contains a pointer called v, for example thing->v. Depending on where (thing->where) the object is, this pointer can have one of three meanings:

  • thing->v.v_nexthere, for an object on the dungeon floor, is the next object below this one in the pile.
  • thing->v.v_ocontainer, for an object in a container such as a chest or bag, points to that containing object.
  • thing->v.v_ocarry, for an object in the inventory of a monster, points to that monster.

The header file will soon define macros that allow you to say:

  • thing->nexthere for the next object in the pile
  • thing->ocontainer for the containing object
  • thing->ocarry for the carrying monster

Remember, it is a C union, so all three of these pointers share the same memory; you can use only one pointer at a time. For example, an object cannot simultaneously be on the floor and be carried by a monster.

struct obj

struct obj {
	struct obj *nobj;

Each object contains a pointer like thing->nobj which allows the object to be part of a linked list. (What does NetHack use this list for?)

	union vptrs v;
#define nexthere	v.v_nexthere
#define ocontainer	v.v_ocontainer
#define ocarry		v.v_ocarry

This declares the pointer v and the three macros explained in the #union vptrs section above.

	struct obj *cobj;	/* contents list for containers */
	unsigned o_id;
	xchar ox,oy;
	short otyp;		/* object class number */
	unsigned owt;
	long quan;		/* number of items */
  • thing->cobj is a linked list of objects contained by thing, for example if the thing is a bag or statue.
  • thing->o_id is a unique identifier for this object. It's set when the object is generated, and does not change. (Uniqueness across _all_ objects in the game is not guaranteed)
  • thing->ox and thing->oy are object coordinates on the map.
  • thing->otyp is the object type from onames.h. Those definitions are generated when compiling, each object definition in objects.c gets one.
  • thing->owt is the total weight of this object. use weight() in mkobj.c to calculate this.

Some items stack. That is when multiple items take only one spot in your inventory or other lists, for example, "2 daggers" or "7 candles". If thing->quan is 2 or 7, then 2 or 7 of those things are stacked.

	schar spe;		/* quality of weapon, armor or ring (+ or -)
				   number of charges for wand ( >= -1 )
				   marks your eggs, spinach tins
				   royal coffers for a court ( == 2)
				   tells which fruit a fruit is
				   special for uball and amulet
				   historic and gender for statues */
#define STATUE_HISTORIC 0x01
#define STATUE_MALE     0x02
#define STATUE_FEMALE   0x04

This gives a special statistic for certain types of items.

  • If thing was a +1 elvish dagger, then thing->spe is 1.
  • If it is a wand of wishing (0:3), a wand with three charges, then thing->spe is 3.
  • This statistic also provides certain information about eggs, spinach, fruit, iron balls (?), and amulets.
  • For statues, this field is a bit mask. If thing->spe & STATUE_HISTORIC, then the statue is historic. Likewise, a statue can be male or female. It can be simultaneously historic, male, and female, though the last two might not combine well.
	char	oclass;		/* object class */
	char	invlet;		/* designation in inventory */
	char	oartifact;	/* artifact array index */
  • thing->oclass is one of the _CLASS definitions from objclass.h, eg. WEAPON_CLASS or POTION_CLASS, and is the class that represents the object on the screen.
  • An object in your inventory will have a letter [a-zA-Z], stored for example at thing->invlet. Even after an object is dropped, the object will try to remember its old inventory letter. Suppose you drop a spellbook which was inventory letter "B". If you retake the spellbook, the fixinv option is on, and the letter "B" is available, then the spellbook will again be letter "B" in your inventory.
  • NetHack has a separate array for artifacts, defined in artilist.h

The hero should be memorising the inventory letter, not the object! But internal to the game, NetHack stores the letter in the object, not the adventurer. This works because this is a single-player game.

	xchar where;		/* where the object thinks it is */
#define OBJ_FREE	0		/* object not attached to anything */
#define OBJ_FLOOR	1		/* object on floor */
#define OBJ_CONTAINED	2		/* object in a container */
#define OBJ_INVENT	3		/* object in the hero's inventory */
#define OBJ_MINVENT	4		/* object in a monster inventory */
#define OBJ_MIGRATING	5		/* object sent off to another level */
#define OBJ_BURIED	6		/* object buried */
#define OBJ_ONBILL	7		/* object on shk bill */
#define NOBJ_STATES	8

So thing->where indicates where this object is. A "shk" is a shopkeeper. If thing is in your inventory, then thing->where is either OBJ_INVENT or OBJ_ONBILL. A for loop that enumerated these locations would be like for(int i = 0; i < NOBJ_STATES; i++).

	xchar timed;		/* # of fuses (timers) attached to this obj */

Timers are for rotting eggs and corpses, for example. Do not change this by hand, rather use the code in timeout.c, see start_timer()

	Bitfield(cursed,1);
	Bitfield(blessed,1);
	Bitfield(unpaid,1);	/* on some bill */
	Bitfield(no_charge,1);	/* if shk shouldn't charge for this */
	Bitfield(known,1);	/* exact nature known */
	Bitfield(dknown,1);	/* color or text known */
	Bitfield(bknown,1);	/* blessing or curse known */
	Bitfield(rknown,1);	/* rustproof or not known */

Bitfields use less than one byte in a struct. Most of these are obvious, like cursed or blessed. Four of these indicate what the adventurer knows about this object. In a game with more than one adventurer, these fields could not be stored in the object. Programming multiplayer NetHack would require many changes.

	Bitfield(oeroded,2);	/* rusted/burnt weapon/armor */
	Bitfield(oeroded2,2);	/* corroded/rotted weapon/armor */
#define greatest_erosion(otmp) (int)((otmp)->oeroded > (otmp)->oeroded2 ? (otmp)->oeroded : (otmp)->oeroded2)
#define MAX_ERODE 3
#define orotten oeroded		/* rotten food */
#define odiluted oeroded	/* diluted potions */
#define norevive oeroded2

Objects can erode in two different ways.

  • thing->oeroded (or orotten or odiluted) indicates rusty metal or burnt organic material. For food such as corpses, this indicates how rotten it is. Also, NetHack uses this to mark a potion as diluted.
  • thing->oeroded2 (or norevive) indicated corroded metal or rotted organic material. Sometimes acid causes this damage. The thing->norevive is probably a flag for NetHack to check whether undead turning would revive something, but we would need to check.

Because thing might erode two different ways, a macro call to greatest_erosion returns the greater of the two erosions. There is a scale from 0 (no erosion) to 1 (eroded) to 2 (very eroded) to 3 (thoroughly eroded).

	Bitfield(oerodeproof,1); /* erodeproof weapon/armor */
	Bitfield(olocked,1);	/* object is locked */
	Bitfield(obroken,1);	/* lock has been broken */
	Bitfield(otrapped,1);	/* container is trapped */
				/* or accidental tripped rolling boulder trap */
#define opoisoned otrapped	/* object (weapon) is coated with poison */

Here are some more boolean bits.

  • Even though an object might erode two ways, there is exactly one "erodeproof" flag - setting it prevents both types of erosion. This is why using the scroll of enchant weapon while confused will erodeproof any weapon. If obj->rknown is true than the code in objnam.c will refer to an erodeproof object as "fixed" if it is a crysknife, else rustproof if the metal would normally rust, else corrodeproof if the metal would normally corrode, else fireproof if the object would be flammable. For example, an erodeproof dragon scale mail would never be marked as such in your inventory, because dragon scales are neither crysknives nor rustprone nor corrodeable nor flammable.
  • thing->olocked is set if an object such a chest or box is locked; thing->obroken if the lock is broken (though you could fix it with wizard lock).
  • thing->otrapped (or synonym thing->opoisoned) indicates that a weapon is poisoned or that the chest or box contains a trap (which might be a flame or poison needle when you open it).
	Bitfield(recharged,3);	/* number of times it's been recharged */
	Bitfield(lamplit,1);	/* a light-source -- can be lit */
#ifdef INVISIBLE_OBJECTS
	Bitfield(oinvis,1);	/* invisible */
#endif
	Bitfield(greased,1);	/* covered with grease */
	Bitfield(oattached,2);	/* obj struct has special attachment */
#define OATTACHED_NOTHING 0
#define OATTACHED_MONST   1	/* monst struct in oextra */
#define OATTACHED_M_ID    2	/* monst id in oextra */
#define OATTACHED_UNUSED3 3
  • thing->recharged is the number of times that something was recharged, for example if thing is a wand or the Bell of Opening. This can range from 0 to 7, though most wands explode much sooner than that. This field complements thing->spe which is the number of charges in the wand.
  • thing->lamplit is set if thing is a lamp, and you switch it on. You can switch on a lamp by applying it.
  • thing->oinvis can be set for an invisible object. NetHack 3.4.3 does not implement invisible objects, even though it has some code for it, so you will not actually find this field set in a game.
  • thing->greased if it is greased.
  • thing->oattached is a number from 0 to 3 (one of the defined constants OATTACHED_NOTHING, ...) to indicate the meaning of thing->oextra, which is defined at the end of this struct.
	Bitfield(in_use,1);	/* for magic items before useup items */
	Bitfield(bypass,1);	/* mark this as an object to be skipped by bhito() */
	/* 6 free bits */

...

	int	corpsenm;	/* type of corpse is mons[corpsenm] */
#define leashmon  corpsenm	/* gets m_id of attached pet */
#define spestudied corpsenm	/* # of times a spellbook has been studied */
#define fromsink  corpsenm	/* a potion from a sink */
	unsigned oeaten;	/* nutrition left in food, if partly eaten */
	long age;		/* creation date */

The "corpsenm", "leashmon", "spestudied", and "fromsink" all refer to the same field of the object. This works because an object can only be one of a corpse, leash, spellbook, or potion. (It might be possible to abolish this field and just widen thing->spe from schar to int, unless someone can find an object that needs to use both fields.)

	uchar onamelth;		/* length of name (following oxlth) */
	short oxlth;		/* length of following data */
	/* in order to prevent alignment problems oextra should
	   be (or follow) a long int */
	long owornmask;
	long oextra[1];		/* used for name of ordinary objects - length
				   is flexible; amount for tmp gold objects */
};

As C is not an object-oriented language with inheritance, the programmers decided to use this hack to allow objects to have extra information. A long is 32 bits on some computers but possibly 64 bits on others.

  • thing->oextra is usually a pointer to a monst struct, but this depends what thing->oattached (above) is set to.
  • thing->oxlth is the length of the data pointed to by thing->oextra

useful macros

#define newobj(xl)	(struct obj *)alloc((unsigned)(xl) + sizeof(struct obj))
#define ONAME(otmp)	(((char *)(otmp)->oextra) + (otmp)->oxlth)
  • newobj uses the alloc function of NetHack to dynamically allocate memory in an appropriate way for the platform. For example, thing = newobj(10) allocates an object with 10 extra bytes, and stores a pointer to that object in thing.
  • ONAME(thing) returns a character pointer to text that NetHack can store in the extra memory after an object. Recall that thing->oextra[0] is the "long int" defined at the end of the struct, thus thing->oextra is a pointer into the end of the struct. Either thing->oxlth is zero, or there is extra information before the text that we need to skip.

weapons

/* Weapons and weapon-tools */
/* KMH -- now based on skill categories.  Formerly:
*	#define is_sword(otmp)	(otmp->oclass == WEAPON_CLASS && \
*			 objects[otmp->otyp].oc_wepcat == WEP_SWORD)
*	#define is_blade(otmp)	(otmp->oclass == WEAPON_CLASS && \
*			 (objects[otmp->otyp].oc_wepcat == WEP_BLADE || \
*			  objects[otmp->otyp].oc_wepcat == WEP_SWORD))
*	#define is_weptool(o)	((o)->oclass == TOOL_CLASS && \
*			 objects[(o)->otyp].oc_weptool)
*	#define is_multigen(otyp) (otyp <= SHURIKEN)
*	#define is_poisonable(otyp) (otyp <= BEC_DE_CORBIN)
*/
#define is_blade(otmp)	(otmp->oclass == WEAPON_CLASS && \
			 objects[otmp->otyp].oc_skill >= P_DAGGER && \
			 objects[otmp->otyp].oc_skill <= P_SABER)
#define is_axe(otmp)	((otmp->oclass == WEAPON_CLASS || \
			 otmp->oclass == TOOL_CLASS) && \
			 objects[otmp->otyp].oc_skill == P_AXE)
#define is_pick(otmp)	((otmp->oclass == WEAPON_CLASS || \
			 otmp->oclass == TOOL_CLASS) && \
			 objects[otmp->otyp].oc_skill == P_PICK_AXE)
#define is_sword(otmp)	(otmp->oclass == WEAPON_CLASS && \
			 objects[otmp->otyp].oc_skill >= P_SHORT_SWORD && \
			 objects[otmp->otyp].oc_skill <= P_SABER)
#define is_pole(otmp)	((otmp->oclass == WEAPON_CLASS || \
			otmp->oclass == TOOL_CLASS) && \
			 (objects[otmp->otyp].oc_skill == P_POLEARMS || \
			 objects[otmp->otyp].oc_skill == P_LANCE))
#define is_spear(otmp)	(otmp->oclass == WEAPON_CLASS && \
			 objects[otmp->otyp].oc_skill >= P_SPEAR && \
			 objects[otmp->otyp].oc_skill <= P_JAVELIN)
#define is_launcher(otmp)	(otmp->oclass == WEAPON_CLASS && \
			 objects[otmp->otyp].oc_skill >= P_BOW && \
			 objects[otmp->otyp].oc_skill <= P_CROSSBOW)
#define is_ammo(otmp)	((otmp->oclass == WEAPON_CLASS || \
			 otmp->oclass == GEM_CLASS) && \
			 objects[otmp->otyp].oc_skill >= -P_CROSSBOW && \
			 objects[otmp->otyp].oc_skill <= -P_BOW)
#define ammo_and_launcher(otmp,ltmp) \
			 (is_ammo(otmp) && (ltmp) && \
			 objects[(otmp)->otyp].oc_skill == -objects[(ltmp)->otyp].oc_skill)
#define is_missile(otmp)	((otmp->oclass == WEAPON_CLASS || \
			 otmp->oclass == TOOL_CLASS) && \
			 objects[otmp->otyp].oc_skill >= -P_BOOMERANG && \
			 objects[otmp->otyp].oc_skill <= -P_DART)
#define is_weptool(o)	((o)->oclass == TOOL_CLASS && \
			 objects[(o)->otyp].oc_skill != P_NONE)
#define bimanual(otmp)	((otmp->oclass == WEAPON_CLASS || \
			 otmp->oclass == TOOL_CLASS) && \
			 objects[otmp->otyp].oc_bimanual)
#define is_multigen(otmp)	(otmp->oclass == WEAPON_CLASS && \
			 objects[otmp->otyp].oc_skill >= -P_SHURIKEN && \
			 objects[otmp->otyp].oc_skill <= -P_BOW)
#define is_poisonable(otmp)	(otmp->oclass == WEAPON_CLASS && \
			 objects[otmp->otyp].oc_skill >= -P_SHURIKEN && \
			 objects[otmp->otyp].oc_skill <= -P_BOW)
#define uslinging()	(uwep && objects[uwep->otyp].oc_skill == P_SLING)

These macros act as functions to test various properties of weapons. Some of these work by querying the class of the object, others look in the objects global array for more information about objects of that type.

For example, a "launcher" (is_launcher(thing)) is any weapon that needs your "bow" or "crossbow" skill. That is, if you can use your bow or crossbow skill with the object, then the object qualifies as a bow or crossbow for the purposes of the game. So wield it, quiver those arrows or crossbow bolts, and fire!

armor

/* Armor */
#define is_shield(otmp) (otmp->oclass == ARMOR_CLASS && \
			 objects[otmp->otyp].oc_armcat == ARM_SHIELD)
#define is_helmet(otmp) (otmp->oclass == ARMOR_CLASS && \
			 objects[otmp->otyp].oc_armcat == ARM_HELM)
#define is_boots(otmp)	(otmp->oclass == ARMOR_CLASS && \
			 objects[otmp->otyp].oc_armcat == ARM_BOOTS)
#define is_gloves(otmp) (otmp->oclass == ARMOR_CLASS && \
			 objects[otmp->otyp].oc_armcat == ARM_GLOVES)
#define is_cloak(otmp)	(otmp->oclass == ARMOR_CLASS && \
			 objects[otmp->otyp].oc_armcat == ARM_CLOAK)
#define is_shirt(otmp)	(otmp->oclass == ARMOR_CLASS && \
			 objects[otmp->otyp].oc_armcat == ARM_SHIRT)
#define is_suit(otmp)	(otmp->oclass == ARMOR_CLASS && \
			 objects[otmp->otyp].oc_armcat == ARM_SUIT)
#define is_elven_armor(otmp)	((otmp)->otyp == ELVEN_LEATHER_HELM\
				|| (otmp)->otyp == ELVEN_MITHRIL_COAT\
				|| (otmp)->otyp == ELVEN_CLOAK\
				|| (otmp)->otyp == ELVEN_SHIELD\
				|| (otmp)->otyp == ELVEN_BOOTS)
#define is_orcish_armor(otmp)	((otmp)->otyp == ORCISH_HELM\
				|| (otmp)->otyp == ORCISH_CHAIN_MAIL\
				|| (otmp)->otyp == ORCISH_RING_MAIL\
				|| (otmp)->otyp == ORCISH_CLOAK\
				|| (otmp)->otyp == URUK_HAI_SHIELD\
				|| (otmp)->otyp == ORCISH_SHIELD)
#define is_dwarvish_armor(otmp)	((otmp)->otyp == DWARVISH_IRON_HELM\
				|| (otmp)->otyp == DWARVISH_MITHRIL_COAT\
				|| (otmp)->otyp == DWARVISH_CLOAK\
				|| (otmp)->otyp == DWARVISH_ROUNDSHIELD)
#define is_gnomish_armor(otmp)	(FALSE)

Each type of object has information about it in the global objects array. This information includes the armor category: shield, helm, boots, gloves, cloak, shirt. The game prevents you from wearing two helms simultaneously, which is why it needs to know about these classes. The is_helmet(thing) macro tests if thing qualifies as a helm. If it does, and you are already wearing a helment, then you cannot wear thing.

Eggs and other food

/* Eggs and other food */
#define MAX_EGG_HATCH_TIME 200	/* longest an egg can remain unhatched */
#define stale_egg(egg)	((monstermoves - (egg)->age) > (2*MAX_EGG_HATCH_TIME))
#define ofood(o) ((o)->otyp == CORPSE || (o)->otyp == EGG || (o)->otyp == TIN)
#define polyfodder(obj) (ofood(obj) && \
			 pm_to_cham((obj)->corpsenm) != CHAM_ORDINARY)
#define mlevelgain(obj) (ofood(obj) && (obj)->corpsenm == PM_WRAITH)
#define mhealup(obj)	(ofood(obj) && (obj)->corpsenm == PM_NURSE)

These are some macros related to eggs (which might hatch!) and food. It is apparent from this code which corpse will level you up or heal you when you eat it. No other corpse but that of a wraith levels you up; no other corpse but that of a nurse heals you.

Containers

/* Containers */
#define carried(o)	((o)->where == OBJ_INVENT)
#define mcarried(o)	((o)->where == OBJ_MINVENT)
#define Has_contents(o) (/* (Is_container(o) || (o)->otyp == STATUE) && */ \
			 (o)->cobj != (struct obj *)0)
#define Is_container(o) ((o)->otyp >= LARGE_BOX && (o)->otyp <= BAG_OF_TRICKS)
#define Is_box(otmp)	(otmp->otyp == LARGE_BOX || otmp->otyp == CHEST)
#define Is_mbag(otmp)	(otmp->otyp == BAG_OF_HOLDING || \
			 otmp->otyp == BAG_OF_TRICKS)

There is are macros to check if an object is in your inventory or that of a monster. It is slightly easier to say carried(thing) than thing->where == OBJ_INVENT. There are also several macros for containers.

An "mbag" (magic bag) is either a bag of holding or bag of tricks; the trait is that both bags magically adjust the weight of their contents. Normally, these bags feel lighter than the actual weight of the items or monsters inside, but a curse could cause a problem.

Items from dragons, dwarves, elves, gnomes

/* dragon gear */
#define Is_dragon_scales(obj)	((obj)->otyp >= GRAY_DRAGON_SCALES && \
				 (obj)->otyp <= YELLOW_DRAGON_SCALES)
#define Is_dragon_mail(obj)	((obj)->otyp >= GRAY_DRAGON_SCALE_MAIL && \
				 (obj)->otyp <= YELLOW_DRAGON_SCALE_MAIL)
#define Is_dragon_armor(obj)	(Is_dragon_scales(obj) || Is_dragon_mail(obj))
#define Dragon_scales_to_pm(obj) &mons[PM_GRAY_DRAGON + (obj)->otyp \
				       - GRAY_DRAGON_SCALES]
#define Dragon_mail_to_pm(obj)	&mons[PM_GRAY_DRAGON + (obj)->otyp \
				      - GRAY_DRAGON_SCALE_MAIL]

These macros facilitate code that needs to know about dragon scales, dragon scale mail and their matching dragons. It assumes that the range of dragon colors starts with gray and ends with yellow. The obj parameter is a pointer to an object, but these macros only examine the object type.

If a monster does polymorph while wearing dragon scales or dragon scale mail, the code uses Dragon_scales_to_pm or Dragon_mail_to_pm to polymorph you into the matching dragon: mon.c#line2351, muse.c#line1684. Then the game disables the message that the dragon would break out of its armor: worn.c#line605. (The code in polyself.c uses a separate function called armor_to_dragon.)

#define Dragon_to_scales(pm)	(GRAY_DRAGON_SCALES + (pm - mons))

Do not use the Dragon_to_scales macro! The DevTeam left this broken and incorrect definition of a macro, but never called this macro from the NetHack source code. If pm is a monster type (an index into the mons array), then the correct version of the macro would expand to (GRAY_DRAGON_SCALES + (pm - PM_GRAY_DRAGON)). NetHack uses the correct formula at mon.c#line194 to pick the correct scales for the death drop of a dragon.

/* Elven gear */
#define is_elven_weapon(otmp)	((otmp)->otyp == ELVEN_ARROW\
				|| (otmp)->otyp == ELVEN_SPEAR\
				|| (otmp)->otyp == ELVEN_DAGGER\
				|| (otmp)->otyp == ELVEN_SHORT_SWORD\
				|| (otmp)->otyp == ELVEN_BROADSWORD\
				|| (otmp)->otyp == ELVEN_BOW)
#define is_elven_obj(otmp)	(is_elven_armor(otmp) || is_elven_weapon(otmp))

/* Orcish gear */
#define is_orcish_obj(otmp)	(is_orcish_armor(otmp)\
				|| (otmp)->otyp == ORCISH_ARROW\
				|| (otmp)->otyp == ORCISH_SPEAR\
				|| (otmp)->otyp == ORCISH_DAGGER\
				|| (otmp)->otyp == ORCISH_SHORT_SWORD\
				|| (otmp)->otyp == ORCISH_BOW)

/* Dwarvish gear */
#define is_dwarvish_obj(otmp)	(is_dwarvish_armor(otmp)\
				|| (otmp)->otyp == DWARVISH_SPEAR\
				|| (otmp)->otyp == DWARVISH_SHORT_SWORD\
				|| (otmp)->otyp == DWARVISH_MATTOCK)

/* Gnomish gear */
#define is_gnomish_obj(otmp)	(is_gnomish_armor(otmp))

These macros tests if an object is elven, orcish, dwarvish or gnomish. (How is this important, and where are the definitions of the is_foo_armor macros?) These macros are boolean expressions that check if the object is of one type or another; the macro for is_elven_weapon literally checks the object against the macro's list of six known elven weapons. If you added a new elven weapon to objects.c, then you would also have to add it to this macro.

One may consider refactoring this, so that struct objclass in objclass.h contains new bitfields oc_elven, oc_orcish, oc_dwarvish, oc_gnomish. Then one would have macros like so,

/* not part of NetHack */
#define is_elven_weapon(otmp) (is_elven_object(otmp) && objects[otmp->otyp].oc_class == WEAPON_CLASS)
#define is_elven_object(otmp) (objects[otmp->otyp].oc_elven)

Then we could adjust the macros in object.c to clear the oc_orcish on most objects, but to set the flag on orcish objects. The advantage is that it slightly easier for a variant to add a new type of orcish object to the game. The disadvantage is that the vast majority of object types are neither elven nor orcish nor dwarvish nor gnomish, so we would be allocating four extra zero bits (negligible with yesterday's computers) for each object type.

Candles, lamps

/* Light sources */
#define Is_candle(otmp) (otmp->otyp == TALLOW_CANDLE || \
			 otmp->otyp == WAX_CANDLE)
#define MAX_OIL_IN_FLASK 400	/* maximum amount of oil in a potion of oil */

/* MAGIC_LAMP intentionally excluded below */
/* age field of this is relative age rather than absolute */
#define age_is_relative(otmp)	((otmp)->otyp == BRASS_LANTERN\
				|| (otmp)->otyp == OIL_LAMP\
				|| (otmp)->otyp == CANDELABRUM_OF_INVOCATION\
				|| (otmp)->otyp == TALLOW_CANDLE\
				|| (otmp)->otyp == WAX_CANDLE\
				|| (otmp)->otyp == POT_OIL)
/* object can be ignited */
#define ignitable(otmp)	((otmp)->otyp == BRASS_LANTERN\
				|| (otmp)->otyp == OIL_LAMP\
				|| (otmp)->otyp == CANDELABRUM_OF_INVOCATION\
				|| (otmp)->otyp == TALLOW_CANDLE\
				|| (otmp)->otyp == WAX_CANDLE\
				|| (otmp)->otyp == POT_OIL)

Stones and other miscellaneous

/* special stones */
#define is_graystone(obj)	((obj)->otyp == LUCKSTONE || \
				 (obj)->otyp == LOADSTONE || \
				 (obj)->otyp == FLINT     || \
				 (obj)->otyp == TOUCHSTONE)

/* misc */
#ifdef KOPS
#define is_flimsy(otmp)		(objects[(otmp)->otyp].oc_material <= LEATHER || \
				 (otmp)->otyp == RUBBER_HOSE)
#else
#define is_flimsy(otmp)		(objects[(otmp)->otyp].oc_material <= LEATHER)
#endif

/* helpers, simple enough to be macros */
#define is_plural(o)	((o)->quan > 1 || \
			 (o)->oartifact == ART_EYES_OF_THE_OVERWORLD)

/* Flags for get_obj_location(). */
#define CONTAINED_TOO	0x1
#define BURIED_TOO	0x2

#endif /* OBJ_H */