Source:NetHack 3.4.3/include/obj.h

From NetHackWiki
Jump to: navigation, search

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[edit]

  1. /*	SCCS Id: @(#)obj.h	3.4	2002/01/07	*/
  2. /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
  3. /* NetHack may be freely redistributed.  See license for details. */
  4.  
  5. #ifndef OBJ_H
  6. #define OBJ_H
  7.  
  8. /* #define obj obj_nh */ /* uncomment for SCO UNIX, which has a conflicting
  9. 			  * typedef for "obj" in <sys/types.h> */

union vptrs[edit]

  1. union vptrs {
  2. 	    struct obj *v_nexthere;	/* floor location lists */
  3. 	    struct obj *v_ocontainer;	/* point back to container */
  4. 	    struct monst *v_ocarry;	/* point back to carrying monst */
  5. };

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[edit]

  1. struct obj {
  2. 	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?)

  1. 	union vptrs v;
  2. #define nexthere	v.v_nexthere
  3. #define ocontainer	v.v_ocontainer
  4. #define ocarry		v.v_ocarry

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

  1. 	struct obj *cobj;	/* contents list for containers */
  2. 	unsigned o_id;
  3. 	xchar ox,oy;
  4. 	short otyp;		/* object class number */
  5. 	unsigned owt;
  6. 	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.

  1. 	schar spe;		/* quality of weapon, armor or ring (+ or -)
  2. 				   number of charges for wand ( >= -1 )
  3. 				   marks your eggs, spinach tins
  4. 				   royal coffers for a court ( == 2)
  5. 				   tells which fruit a fruit is
  6. 				   special for uball and amulet
  7. 				   historic and gender for statues */
  8. #define STATUE_HISTORIC 0x01
  9. #define STATUE_MALE     0x02
  10. #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.
  1. 	char	oclass;		/* object class */
  2. 	char	invlet;		/* designation in inventory */
  3. 	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.

  1. 	xchar where;		/* where the object thinks it is */
  2. #define OBJ_FREE	0		/* object not attached to anything */
  3. #define OBJ_FLOOR	1		/* object on floor */
  4. #define OBJ_CONTAINED	2		/* object in a container */
  5. #define OBJ_INVENT	3		/* object in the hero's inventory */
  6. #define OBJ_MINVENT	4		/* object in a monster inventory */
  7. #define OBJ_MIGRATING	5		/* object sent off to another level */
  8. #define OBJ_BURIED	6		/* object buried */
  9. #define OBJ_ONBILL	7		/* object on shk bill */
  10. #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++).

  1. 	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()

  1. 	Bitfield(cursed,1);
  2. 	Bitfield(blessed,1);
  3. 	Bitfield(unpaid,1);	/* on some bill */
  4. 	Bitfield(no_charge,1);	/* if shk shouldn't charge for this */
  5. 	Bitfield(known,1);	/* exact nature known */
  6. 	Bitfield(dknown,1);	/* color or text known */
  7. 	Bitfield(bknown,1);	/* blessing or curse known */
  8. 	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.

  1. 	Bitfield(oeroded,2);	/* rusted/burnt weapon/armor */
  2. 	Bitfield(oeroded2,2);	/* corroded/rotted weapon/armor */
  3. #define greatest_erosion(otmp) (int)((otmp)->oeroded > (otmp)->oeroded2 ? (otmp)->oeroded : (otmp)->oeroded2)
  4. #define MAX_ERODE 3
  5. #define orotten oeroded		/* rotten food */
  6. #define odiluted oeroded	/* diluted potions */
  7. #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).

  1. 	Bitfield(oerodeproof,1); /* erodeproof weapon/armor */
  2. 	Bitfield(olocked,1);	/* object is locked */
  3. 	Bitfield(obroken,1);	/* lock has been broken */
  4. 	Bitfield(otrapped,1);	/* container is trapped */
  5. 				/* or accidental tripped rolling boulder trap */
  6. #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).
  1. 	Bitfield(recharged,3);	/* number of times it's been recharged */
  2. 	Bitfield(lamplit,1);	/* a light-source -- can be lit */
  3. #ifdef INVISIBLE_OBJECTS
  4. 	Bitfield(oinvis,1);	/* invisible */
  5. #endif
  6. 	Bitfield(greased,1);	/* covered with grease */
  7. 	Bitfield(oattached,2);	/* obj struct has special attachment */
  8. #define OATTACHED_NOTHING 0
  9. #define OATTACHED_MONST   1	/* monst struct in oextra */
  10. #define OATTACHED_M_ID    2	/* monst id in oextra */
  11. #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.
  1. 	Bitfield(in_use,1);	/* for magic items before useup items */
  2. 	Bitfield(bypass,1);	/* mark this as an object to be skipped by bhito() */
  3. 	/* 6 free bits */

...

  1. 	int	corpsenm;	/* type of corpse is mons[corpsenm] */
  2. #define leashmon  corpsenm	/* gets m_id of attached pet */
  3. #define spestudied corpsenm	/* # of times a spellbook has been studied */
  4. #define fromsink  corpsenm	/* a potion from a sink */
  5. 	unsigned oeaten;	/* nutrition left in food, if partly eaten */
  6. 	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.)

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

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[edit]

  1. #define newobj(xl)	(struct obj *)alloc((unsigned)(xl) + sizeof(struct obj))
  2. #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[edit]

  1. /* Weapons and weapon-tools */
  2. /* KMH -- now based on skill categories.  Formerly:
  3. *	#define is_sword(otmp)	(otmp->oclass == WEAPON_CLASS && \
  4. *			 objects[otmp->otyp].oc_wepcat == WEP_SWORD)
  5. *	#define is_blade(otmp)	(otmp->oclass == WEAPON_CLASS && \
  6. *			 (objects[otmp->otyp].oc_wepcat == WEP_BLADE || \
  7. *			  objects[otmp->otyp].oc_wepcat == WEP_SWORD))
  8. *	#define is_weptool(o)	((o)->oclass == TOOL_CLASS && \
  9. *			 objects[(o)->otyp].oc_weptool)
  10. *	#define is_multigen(otyp) (otyp <= SHURIKEN)
  11. *	#define is_poisonable(otyp) (otyp <= BEC_DE_CORBIN)
  12. */
  13. #define is_blade(otmp)	(otmp->oclass == WEAPON_CLASS && \
  14. 			 objects[otmp->otyp].oc_skill >= P_DAGGER && \
  15. 			 objects[otmp->otyp].oc_skill <= P_SABER)
  16. #define is_axe(otmp)	((otmp->oclass == WEAPON_CLASS || \
  17. 			 otmp->oclass == TOOL_CLASS) && \
  18. 			 objects[otmp->otyp].oc_skill == P_AXE)
  19. #define is_pick(otmp)	((otmp->oclass == WEAPON_CLASS || \
  20. 			 otmp->oclass == TOOL_CLASS) && \
  21. 			 objects[otmp->otyp].oc_skill == P_PICK_AXE)
  22. #define is_sword(otmp)	(otmp->oclass == WEAPON_CLASS && \
  23. 			 objects[otmp->otyp].oc_skill >= P_SHORT_SWORD && \
  24. 			 objects[otmp->otyp].oc_skill <= P_SABER)
  25. #define is_pole(otmp)	((otmp->oclass == WEAPON_CLASS || \
  26. 			otmp->oclass == TOOL_CLASS) && \
  27. 			 (objects[otmp->otyp].oc_skill == P_POLEARMS || \
  28. 			 objects[otmp->otyp].oc_skill == P_LANCE))
  29. #define is_spear(otmp)	(otmp->oclass == WEAPON_CLASS && \
  30. 			 objects[otmp->otyp].oc_skill >= P_SPEAR && \
  31. 			 objects[otmp->otyp].oc_skill <= P_JAVELIN)
  32. #define is_launcher(otmp)	(otmp->oclass == WEAPON_CLASS && \
  33. 			 objects[otmp->otyp].oc_skill >= P_BOW && \
  34. 			 objects[otmp->otyp].oc_skill <= P_CROSSBOW)
  35. #define is_ammo(otmp)	((otmp->oclass == WEAPON_CLASS || \
  36. 			 otmp->oclass == GEM_CLASS) && \
  37. 			 objects[otmp->otyp].oc_skill >= -P_CROSSBOW && \
  38. 			 objects[otmp->otyp].oc_skill <= -P_BOW)
  39. #define ammo_and_launcher(otmp,ltmp) \
  40. 			 (is_ammo(otmp) && (ltmp) && \
  41. 			 objects[(otmp)->otyp].oc_skill == -objects[(ltmp)->otyp].oc_skill)
  42. #define is_missile(otmp)	((otmp->oclass == WEAPON_CLASS || \
  43. 			 otmp->oclass == TOOL_CLASS) && \
  44. 			 objects[otmp->otyp].oc_skill >= -P_BOOMERANG && \
  45. 			 objects[otmp->otyp].oc_skill <= -P_DART)
  46. #define is_weptool(o)	((o)->oclass == TOOL_CLASS && \
  47. 			 objects[(o)->otyp].oc_skill != P_NONE)
  48. #define bimanual(otmp)	((otmp->oclass == WEAPON_CLASS || \
  49. 			 otmp->oclass == TOOL_CLASS) && \
  50. 			 objects[otmp->otyp].oc_bimanual)
  51. #define is_multigen(otmp)	(otmp->oclass == WEAPON_CLASS && \
  52. 			 objects[otmp->otyp].oc_skill >= -P_SHURIKEN && \
  53. 			 objects[otmp->otyp].oc_skill <= -P_BOW)
  54. #define is_poisonable(otmp)	(otmp->oclass == WEAPON_CLASS && \
  55. 			 objects[otmp->otyp].oc_skill >= -P_SHURIKEN && \
  56. 			 objects[otmp->otyp].oc_skill <= -P_BOW)
  57. #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[edit]

  1. /* Armor */
  2. #define is_shield(otmp) (otmp->oclass == ARMOR_CLASS && \
  3. 			 objects[otmp->otyp].oc_armcat == ARM_SHIELD)
  4. #define is_helmet(otmp) (otmp->oclass == ARMOR_CLASS && \
  5. 			 objects[otmp->otyp].oc_armcat == ARM_HELM)
  6. #define is_boots(otmp)	(otmp->oclass == ARMOR_CLASS && \
  7. 			 objects[otmp->otyp].oc_armcat == ARM_BOOTS)
  8. #define is_gloves(otmp) (otmp->oclass == ARMOR_CLASS && \
  9. 			 objects[otmp->otyp].oc_armcat == ARM_GLOVES)
  10. #define is_cloak(otmp)	(otmp->oclass == ARMOR_CLASS && \
  11. 			 objects[otmp->otyp].oc_armcat == ARM_CLOAK)
  12. #define is_shirt(otmp)	(otmp->oclass == ARMOR_CLASS && \
  13. 			 objects[otmp->otyp].oc_armcat == ARM_SHIRT)
  14. #define is_suit(otmp)	(otmp->oclass == ARMOR_CLASS && \
  15. 			 objects[otmp->otyp].oc_armcat == ARM_SUIT)
  16. #define is_elven_armor(otmp)	((otmp)->otyp == ELVEN_LEATHER_HELM\
  17. 				|| (otmp)->otyp == ELVEN_MITHRIL_COAT\
  18. 				|| (otmp)->otyp == ELVEN_CLOAK\
  19. 				|| (otmp)->otyp == ELVEN_SHIELD\
  20. 				|| (otmp)->otyp == ELVEN_BOOTS)
  21. #define is_orcish_armor(otmp)	((otmp)->otyp == ORCISH_HELM\
  22. 				|| (otmp)->otyp == ORCISH_CHAIN_MAIL\
  23. 				|| (otmp)->otyp == ORCISH_RING_MAIL\
  24. 				|| (otmp)->otyp == ORCISH_CLOAK\
  25. 				|| (otmp)->otyp == URUK_HAI_SHIELD\
  26. 				|| (otmp)->otyp == ORCISH_SHIELD)
  27. #define is_dwarvish_armor(otmp)	((otmp)->otyp == DWARVISH_IRON_HELM\
  28. 				|| (otmp)->otyp == DWARVISH_MITHRIL_COAT\
  29. 				|| (otmp)->otyp == DWARVISH_CLOAK\
  30. 				|| (otmp)->otyp == DWARVISH_ROUNDSHIELD)
  31. #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[edit]

  1. /* Eggs and other food */
  2. #define MAX_EGG_HATCH_TIME 200	/* longest an egg can remain unhatched */
  3. #define stale_egg(egg)	((monstermoves - (egg)->age) > (2*MAX_EGG_HATCH_TIME))
  4. #define ofood(o) ((o)->otyp == CORPSE || (o)->otyp == EGG || (o)->otyp == TIN)
  5. #define polyfodder(obj) (ofood(obj) && \
  6. 			 pm_to_cham((obj)->corpsenm) != CHAM_ORDINARY)
  7. #define mlevelgain(obj) (ofood(obj) && (obj)->corpsenm == PM_WRAITH)
  8. #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[edit]

  1. /* Containers */
  2. #define carried(o)	((o)->where == OBJ_INVENT)
  3. #define mcarried(o)	((o)->where == OBJ_MINVENT)
  4. #define Has_contents(o) (/* (Is_container(o) || (o)->otyp == STATUE) && */ \
  5. 			 (o)->cobj != (struct obj *)0)
  6. #define Is_container(o) ((o)->otyp >= LARGE_BOX && (o)->otyp <= BAG_OF_TRICKS)
  7. #define Is_box(otmp)	(otmp->otyp == LARGE_BOX || otmp->otyp == CHEST)
  8. #define Is_mbag(otmp)	(otmp->otyp == BAG_OF_HOLDING || \
  9. 			 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[edit]

  1. /* dragon gear */
  2. #define Is_dragon_scales(obj)	((obj)->otyp >= GRAY_DRAGON_SCALES && \
  3. 				 (obj)->otyp <= YELLOW_DRAGON_SCALES)
  4. #define Is_dragon_mail(obj)	((obj)->otyp >= GRAY_DRAGON_SCALE_MAIL && \
  5. 				 (obj)->otyp <= YELLOW_DRAGON_SCALE_MAIL)
  6. #define Is_dragon_armor(obj)	(Is_dragon_scales(obj) || Is_dragon_mail(obj))
  7. #define Dragon_scales_to_pm(obj) &mons[PM_GRAY_DRAGON + (obj)->otyp \
  8. 				       - GRAY_DRAGON_SCALES]
  9. #define Dragon_mail_to_pm(obj)	&mons[PM_GRAY_DRAGON + (obj)->otyp \
  10. 				      - 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.)

  1. #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.

  1. /* Elven gear */
  2. #define is_elven_weapon(otmp)	((otmp)->otyp == ELVEN_ARROW\
  3. 				|| (otmp)->otyp == ELVEN_SPEAR\
  4. 				|| (otmp)->otyp == ELVEN_DAGGER\
  5. 				|| (otmp)->otyp == ELVEN_SHORT_SWORD\
  6. 				|| (otmp)->otyp == ELVEN_BROADSWORD\
  7. 				|| (otmp)->otyp == ELVEN_BOW)
  8. #define is_elven_obj(otmp)	(is_elven_armor(otmp) || is_elven_weapon(otmp))
  9.  
  10. /* Orcish gear */
  11. #define is_orcish_obj(otmp)	(is_orcish_armor(otmp)\
  12. 				|| (otmp)->otyp == ORCISH_ARROW\
  13. 				|| (otmp)->otyp == ORCISH_SPEAR\
  14. 				|| (otmp)->otyp == ORCISH_DAGGER\
  15. 				|| (otmp)->otyp == ORCISH_SHORT_SWORD\
  16. 				|| (otmp)->otyp == ORCISH_BOW)
  17.  
  18. /* Dwarvish gear */
  19. #define is_dwarvish_obj(otmp)	(is_dwarvish_armor(otmp)\
  20. 				|| (otmp)->otyp == DWARVISH_SPEAR\
  21. 				|| (otmp)->otyp == DWARVISH_SHORT_SWORD\
  22. 				|| (otmp)->otyp == DWARVISH_MATTOCK)
  23.  
  24. /* Gnomish gear */
  25. #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[edit]

  1. /* Light sources */
  2. #define Is_candle(otmp) (otmp->otyp == TALLOW_CANDLE || \
  3. 			 otmp->otyp == WAX_CANDLE)
  4. #define MAX_OIL_IN_FLASK 400	/* maximum amount of oil in a potion of oil */
  5.  
  6. /* MAGIC_LAMP intentionally excluded below */
  7. /* age field of this is relative age rather than absolute */
  8. #define age_is_relative(otmp)	((otmp)->otyp == BRASS_LANTERN\
  9. 				|| (otmp)->otyp == OIL_LAMP\
  10. 				|| (otmp)->otyp == CANDELABRUM_OF_INVOCATION\
  11. 				|| (otmp)->otyp == TALLOW_CANDLE\
  12. 				|| (otmp)->otyp == WAX_CANDLE\
  13. 				|| (otmp)->otyp == POT_OIL)
  14. /* object can be ignited */
  15. #define ignitable(otmp)	((otmp)->otyp == BRASS_LANTERN\
  16. 				|| (otmp)->otyp == OIL_LAMP\
  17. 				|| (otmp)->otyp == CANDELABRUM_OF_INVOCATION\
  18. 				|| (otmp)->otyp == TALLOW_CANDLE\
  19. 				|| (otmp)->otyp == WAX_CANDLE\
  20. 				|| (otmp)->otyp == POT_OIL)

Stones and other miscellaneous[edit]

  1. /* special stones */
  2. #define is_graystone(obj)	((obj)->otyp == LUCKSTONE || \
  3. 				 (obj)->otyp == LOADSTONE || \
  4. 				 (obj)->otyp == FLINT     || \
  5. 				 (obj)->otyp == TOUCHSTONE)
  6.  
  7. /* misc */
  8. #ifdef KOPS
  9. #define is_flimsy(otmp)		(objects[(otmp)->otyp].oc_material <= LEATHER || \
  10. 				 (otmp)->otyp == RUBBER_HOSE)
  11. #else
  12. #define is_flimsy(otmp)		(objects[(otmp)->otyp].oc_material <= LEATHER)
  13. #endif
  14.  
  15. /* helpers, simple enough to be macros */
  16. #define is_plural(o)	((o)->quan > 1 || \
  17. 			 (o)->oartifact == ART_EYES_OF_THE_OVERWORLD)
  18.  
  19. /* Flags for get_obj_location(). */
  20. #define CONTAINED_TOO	0x1
  21. #define BURIED_TOO	0x2
  22.  
  23. #endif /* OBJ_H */