Source:NetHack 3.4.3/src/artifact.c

From NetHackWiki
Jump to: navigation, search

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

Top of file[edit]

  1. /*	SCCS Id: @(#)artifact.c 3.4	2003/08/11	*/
  2. /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
  3. /* NetHack may be freely redistributed.  See license for details. */

The NetHack General Public License applies to screenshots, source code and other content from NetHack.

This content was modified from the original NetHack source code distribution (by splitting up NetHack content between wiki pages, and possibly further editing). See the page history for a list of who changed it, and on what dates.

  1. #include "hack.h"
  2. #include "artifact.h"
  3. #ifdef OVLB
  4. #include "artilist.h"
  5. #else
  6. STATIC_DCL struct artifact artilist[];
  7. #endif
  8. /*
  9. * Note:  both artilist[] and artiexist[] have a dummy element #0,
  10. *	  so loops over them should normally start at #1.  The primary
  11. *	  exception is the save & restore code, which doesn't care about
  12. *	  the contents, just the total size.
  13. */
  14.  
  15. extern boolean notonhead;	/* for long worms */
  16.  
  17. #define get_artifact(o) \
  18. 		(((o)&&(o)->oartifact) ? &artilist[(int) (o)->oartifact] : 0)
  19.  
  20. STATIC_DCL int FDECL(spec_applies, (const struct artifact *,struct monst *));
  21. STATIC_DCL int FDECL(arti_invoke, (struct obj*));
  22. STATIC_DCL boolean FDECL(Mb_hit, (struct monst *magr,struct monst *mdef,
  23. 				  struct obj *,int *,int,BOOLEAN_P,char *));
  24.  
  25. /* The amount added to the victim's total hit points to insure that the
  26. victim will be killed even after damage bonus/penalty adjustments.
  27. Most such penalties are small, and 200 is plenty; the exception is
  28. half physical damage.  3.3.1 and previous versions tried to use a very
  29. large number to account for this case; now, we just compute the fatal
  30. damage by adding it to 2 times the total hit points instead of 1 time.
  31. Note: this will still break if they have more than about half the number
  32. of hit points that will fit in a 15 bit integer. */
  33. #define FATAL_DAMAGE_MODIFIER 200
  34.  
  35. #ifndef OVLB
  36. STATIC_DCL int spec_dbon_applies;
  37. STATIC_DCL xchar artidisco[NROFARTIFACTS];
  38. #else	/* OVLB */
  39. /* coordinate effects from spec_dbon() with messages in artifact_hit() */
  40. STATIC_OVL int spec_dbon_applies = 0;
  41.  
  42. /* flags including which artifacts have already been created */
  43. static boolean artiexist[1+NROFARTIFACTS+1];
  44. /* and a discovery list for them (no dummy first entry here) */
  45. STATIC_OVL xchar artidisco[NROFARTIFACTS];
  46.  
  47. STATIC_DCL void NDECL(hack_artifacts);
  48. STATIC_DCL boolean FDECL(attacks, (int,struct obj *));

hack_artifacts[edit]

  1. /* handle some special cases; must be called after u_init() */
  2. STATIC_OVL void
  3. hack_artifacts()
  4. {
  5. 	struct artifact *art;
  6. 	int alignmnt = aligns[flags.initalign].value;
  7.  
  8. 	/* Fix up the alignments of "gift" artifacts */
  9. 	for (art = artilist+1; art->otyp; art++)
  10. 	    if (art->role == Role_switch && art->alignment != A_NONE)
  11. 		art->alignment = alignmnt;
  12.  
  13. 	/* Excalibur can be used by any lawful character, not just knights */
  14. 	if (!Role_if(PM_KNIGHT))
  15. 	    artilist[ART_EXCALIBUR].role = NON_PM;
  16.  
  17. 	/* Fix up the quest artifact */
  18. 	if (urole.questarti) {
  19. 	    artilist[urole.questarti].alignment = alignmnt;
  20. 	    artilist[urole.questarti].role = Role_switch;
  21. 	}
  22. 	return;
  23. }

init_artifacts[edit]

  1. /* zero out the artifact existence list */
  2. void
  3. init_artifacts()
  4. {
  5. 	(void) memset((genericptr_t) artiexist, 0, sizeof artiexist);
  6. 	(void) memset((genericptr_t) artidisco, 0, sizeof artidisco);
  7. 	hack_artifacts();
  8. }

save_artifacts[edit]

  1. void
  2. save_artifacts(fd)
  3. int fd;
  4. {
  5. 	bwrite(fd, (genericptr_t) artiexist, sizeof artiexist);
  6. 	bwrite(fd, (genericptr_t) artidisco, sizeof artidisco);
  7. }

restore_artifacts[edit]

  1. void
  2. restore_artifacts(fd)
  3. int fd;
  4. {
  5. 	mread(fd, (genericptr_t) artiexist, sizeof artiexist);
  6. 	mread(fd, (genericptr_t) artidisco, sizeof artidisco);
  7. 	hack_artifacts();	/* redo non-saved special cases */
  8. }

artiname[edit]

  1. const char *
  2. artiname(artinum)
  3. int artinum;
  4. {
  5. 	if (artinum <= 0 || artinum > NROFARTIFACTS) return("");
  6. 	return(artilist[artinum].name);
  7. }

mk_artifact[edit]

  1. /*
  2. Make an artifact.  If a specific alignment is specified, then an object of
  3. the appropriate alignment is created from scratch, or 0 is returned if
  4. none is available.  (If at least one aligned artifact has already been
  5. given, then unaligned ones also become eligible for this.)
  6. If no alignment is given, then 'otmp' is converted
  7. into an artifact of matching type, or returned as-is if that's not possible.
  8. For the 2nd case, caller should use ``obj = mk_artifact(obj, A_NONE);''
  9. for the 1st, ``obj = mk_artifact((struct obj *)0, some_alignment);''.
  10. */
  11. struct obj *
  12. mk_artifact(otmp, alignment)
  13. struct obj *otmp;	/* existing object; ignored if alignment specified */
  14. aligntyp alignment;	/* target alignment, or A_NONE */
  15. {
  16. 	const struct artifact *a;
  17. 	int n, m;
  18. 	boolean by_align = (alignment != A_NONE);
  19. 	short o_typ = (by_align || !otmp) ? 0 : otmp->otyp;
  20. 	boolean unique = !by_align && otmp && objects[o_typ].oc_unique;
  21. 	short eligible[NROFARTIFACTS];
  22.  
  23. 	/* gather eligible artifacts */
  24. 	for (n = 0, a = artilist+1, m = 1; a->otyp; a++, m++)
  25. 	    if ((!by_align ? a->otyp == o_typ :
  26. 		    (a->alignment == alignment ||
  27. 			(a->alignment == A_NONE && u.ugifts > 0))) &&
  28. 		(!(a->spfx & SPFX_NOGEN) || unique) && !artiexist[m]) {
  29. 		if (by_align && a->race != NON_PM && race_hostile(&mons[a->race]))
  30. 		    continue;	/* skip enemies' equipment */
  31. 		else if (by_align && Role_if(a->role))
  32. 		    goto make_artif;	/* 'a' points to the desired one */
  33. 		else
  34. 		    eligible[n++] = m;
  35. 	    }
  36.  
  37. 	if (n) {		/* found at least one candidate */
  38. 	    m = eligible[rn2(n)];	/* [0..n-1] */
  39. 	    a = &artilist[m];
  40.  
  41. 	    /* make an appropriate object if necessary, then christen it */
  42. make_artif: if (by_align) otmp = mksobj((int)a->otyp, TRUE, FALSE);
  43. 	    otmp = oname(otmp, a->name);
  44. 	    otmp->oartifact = m;
  45. 	    artiexist[m] = TRUE;
  46. 	} else {
  47. 	    /* nothing appropriate could be found; return the original object */
  48. 	    if (by_align) otmp = 0;	/* (there was no original object) */
  49. 	}
  50. 	return otmp;
  51. }

artifact_name[edit]

  1. /*
  2. * Returns the full name (with articles and correct capitalization) of an
  3. * artifact named "name" if one exists, or NULL, it not.
  4. * The given name must be rather close to the real name for it to match.
  5. * The object type of the artifact is returned in otyp if the return value
  6. * is non-NULL.
  7. */
  8. const char*
  9. artifact_name(name, otyp)
  10. const char *name;
  11. short *otyp;
  12. {
  13. register const struct artifact *a;
  14. register const char *aname;
  15.  
  16. if(!strncmpi(name, "the ", 4)) name += 4;
  17.  
  18. for (a = artilist+1; a->otyp; a++) {
  19. 	aname = a->name;
  20. 	if(!strncmpi(aname, "the ", 4)) aname += 4;
  21. 	if(!strcmpi(name, aname)) {
  22. 	    *otyp = a->otyp;
  23. 	    return a->name;
  24. 	}
  25. }
  26.  
  27. return (char *)0;
  28. }

exist_artifact[edit]

A boolean test to see if an artifact with the same name and type already exists in-game. This is used to prevent the player from making multiple Stings, dipping for multiple Excaliburs, or getting another Stormbringer or Vorpal Blade when crowned.

  1. boolean
  2. exist_artifact(otyp, name)
  3. register int otyp;
  4. register const char *name;
  5. {
  6. 	register const struct artifact *a;
  7. 	register boolean *arex;
  8.  
  9. 	if (otyp && *name)
  10. 	    for (a = artilist+1,arex = artiexist+1; a->otyp; a++,arex++)
  11. 		if ((int) a->otyp == otyp && !strcmp(a->name, name))
  12. 		    return *arex;
  13. 	return FALSE;
  14. }

artifact_exists[edit]

  1. void
  2. artifact_exists(otmp, name, mod)
  3. register struct obj *otmp;
  4. register const char *name;
  5. register boolean mod;
  6. {
  7. 	register const struct artifact *a;
  8.  
  9. 	if (otmp && *name)
  10. 	    for (a = artilist+1; a->otyp; a++)
  11. 		if (a->otyp == otmp->otyp && !strcmp(a->name, name)) {
  12. 		    register int m = a - artilist;
  13. 		    otmp->oartifact = (char)(mod ? m : 0);
  14. 		    otmp->age = 0;
  15. 		    if(otmp->otyp == RIN_INCREASE_DAMAGE)
  16. 			otmp->spe = 0;
  17. 		    artiexist[m] = mod;
  18. 		    break;
  19. 		}
  20. 	return;
  21. }

nartifact_exist[edit]

  1. int
  2. nartifact_exist()
  3. {
  4. int a = 0;
  5. int n = SIZE(artiexist);
  6.  
  7. while(n > 1)
  8. 	if(artiexist[--n]) a++;
  9.  
  10. return a;
  11. }
  12. #endif /* OVLB */
  13. #ifdef OVL0

spec_ability[edit]

  1. boolean
  2. spec_ability(otmp, abil)
  3. struct obj *otmp;
  4. unsigned long abil;
  5. {
  6. 	const struct artifact *arti = get_artifact(otmp);
  7.  
  8. 	return((boolean)(arti && (arti->spfx & abil)));
  9. }

confers_luck[edit]

  1. /* used so that callers don't need to known about SPFX_ codes */
  2. boolean
  3. confers_luck(obj)
  4. struct obj *obj;
  5. {
  6. /* might as well check for this too */
  7. if (obj->otyp == LUCKSTONE) return TRUE;
  8.  
  9. return (obj->oartifact && spec_ability(obj, SPFX_LUCK));
  10. }

arti_reflects[edit]

  1. /* used to check whether a monster is getting reflection from an artifact */
  2. boolean
  3. arti_reflects(obj)
  4. struct obj *obj;
  5. {
  6. const struct artifact *arti = get_artifact(obj);
  7.  
  8. if (arti) {      
  9. 	/* while being worn */
  10. 	if ((obj->owornmask & ~W_ART) && (arti->spfx & SPFX_REFLECT))
  11. 	    return TRUE;
  12. 	/* just being carried */
  13. 	if (arti->cspfx & SPFX_REFLECT) return TRUE;
  14. }
  15. return FALSE;
  16. }
  17.  
  18. #endif /* OVL0 */

restrict_name[edit]

restrict_name returns TRUE if the given name and object-type correspond to an artifact. For example, a long sword and "Fire Brand" or "Demonbane" will return TRUE, while the same sword and "Stormbringer" will return FALSE.

This is one of the two functions behind the artifact naming bug. The other is do_oname.

  1. #ifdef OVLB
  2.  
  3. boolean
  4. restrict_name(otmp, name)  /* returns 1 if name is restricted for otmp->otyp */
  5. register struct obj *otmp;
  6. register const char *name;
  7. {
  8. 	register const struct artifact *a;
  9. 	register const char *aname;
  10.  
  11. 	if (!*name) return FALSE;
  12. 	if (!strncmpi(name, "the ", 4)) name += 4;
  13.  
  14. 		/* Since almost every artifact is SPFX_RESTR, it doesn't cost
  15. 		   us much to do the string comparison before the spfx check.
  16. 		   Bug fix:  don't name multiple elven daggers "Sting".
  17. 		 */
  18. 	for (a = artilist+1; a->otyp; a++) {
  19. 	    if (a->otyp != otmp->otyp) continue;
  20. 	    aname = a->name;
  21. 	    if (!strncmpi(aname, "the ", 4)) aname += 4;
  22. 	    if (!strcmp(aname, name))
  23. 		return ((boolean)((a->spfx & (SPFX_NOGEN|SPFX_RESTR)) != 0 ||
  24. 			otmp->quan > 1L));
  25. 	}
  26.  
  27. 	return FALSE;
  28. }

attacks[edit]

  1. STATIC_OVL boolean
  2. attacks(adtyp, otmp)
  3. register int adtyp;
  4. register struct obj *otmp;
  5. {
  6. 	register const struct artifact *weap;
  7.  
  8. 	if ((weap = get_artifact(otmp)) != 0)
  9. 		return((boolean)(weap->attk.adtyp == adtyp));
  10. 	return FALSE;
  11. }

defends[edit]

  1. boolean
  2. defends(adtyp, otmp)
  3. register int adtyp;
  4. register struct obj *otmp;
  5. {
  6. 	register const struct artifact *weap;
  7.  
  8. 	if ((weap = get_artifact(otmp)) != 0)
  9. 		return((boolean)(weap->defn.adtyp == adtyp));
  10. 	return FALSE;
  11. }

protects[edit]

  1. /* used for monsters */
  2. boolean
  3. protects(adtyp, otmp)
  4. int adtyp;
  5. struct obj *otmp;
  6. {
  7. 	register const struct artifact *weap;
  8.  
  9. 	if ((weap = get_artifact(otmp)) != 0)
  10. 		return (boolean)(weap->cary.adtyp == adtyp);
  11. 	return FALSE;
  12. }

set_artifact_intrinsic[edit]

  1. /*
  2. * a potential artifact has just been worn/wielded/picked-up or
  3. * unworn/unwielded/dropped.  Pickup/drop only set/reset the W_ART mask.
  4. */
  5. void
  6. set_artifact_intrinsic(otmp,on,wp_mask)
  7. register struct obj *otmp;
  8. boolean on;
  9. long wp_mask;
  10. {
  11. 	long *mask = 0;
  12. 	register const struct artifact *oart = get_artifact(otmp);
  13. 	uchar dtyp;
  14. 	long spfx;
  15.  
  16. 	if (!oart) return;
  17.  
  18. 	/* effects from the defn field */
  19. 	dtyp = (wp_mask != W_ART) ? oart->defn.adtyp : oart->cary.adtyp;
  20.  
  21. 	if (dtyp == AD_FIRE)
  22. 	    mask = &EFire_resistance;
  23. 	else if (dtyp == AD_COLD)
  24. 	    mask = &ECold_resistance;
  25. 	else if (dtyp == AD_ELEC)
  26. 	    mask = &EShock_resistance;
  27. 	else if (dtyp == AD_MAGM)
  28. 	    mask = &EAntimagic;
  29. 	else if (dtyp == AD_DISN)
  30. 	    mask = &EDisint_resistance;
  31. 	else if (dtyp == AD_DRST)
  32. 	    mask = &EPoison_resistance;
  33.  
  34. 	if (mask && wp_mask == W_ART && !on) {
  35. 	    /* find out if some other artifact also confers this intrinsic */
  36. 	    /* if so, leave the mask alone */
  37. 	    register struct obj* obj;
  38. 	    for(obj = invent; obj; obj = obj->nobj)
  39. 		if(obj != otmp && obj->oartifact) {
  40. 		    register const struct artifact *art = get_artifact(obj);
  41. 		    if(art->cary.adtyp == dtyp) {
  42. 			mask = (long *) 0;
  43. 			break;
  44. 		    }
  45. 		}
  46. 	}
  47. 	if (mask) {
  48. 	    if (on) *mask |= wp_mask;
  49. 	    else *mask &= ~wp_mask;
  50. 	}
  51.  
  52. 	/* intrinsics from the spfx field; there could be more than one */
  53. 	spfx = (wp_mask != W_ART) ? oart->spfx : oart->cspfx;
  54. 	if(spfx && wp_mask == W_ART && !on) {
  55. 	    /* don't change any spfx also conferred by other artifacts */
  56. 	    register struct obj* obj;
  57. 	    for(obj = invent; obj; obj = obj->nobj)
  58. 		if(obj != otmp && obj->oartifact) {
  59. 		    register const struct artifact *art = get_artifact(obj);
  60. 		    spfx &= ~art->cspfx;
  61. 		}
  62. 	}
  63.  
  64. 	if (spfx & SPFX_SEARCH) {
  65. 	    if(on) ESearching |= wp_mask;
  66. 	    else ESearching &= ~wp_mask;
  67. 	}
  68. 	if (spfx & SPFX_HALRES) {
  69. 	    /* make_hallucinated must (re)set the mask itself to get
  70. 	     * the display right */
  71. 	    /* restoring needed because this is the only artifact intrinsic
  72. 	     * that can print a message--need to guard against being printed
  73. 	     * when restoring a game
  74. 	     */
  75. 	    (void) make_hallucinated((long)!on, restoring ? FALSE : TRUE, wp_mask);
  76. 	}
  77. 	if (spfx & SPFX_ESP) {
  78. 	    if(on) ETelepat |= wp_mask;
  79. 	    else ETelepat &= ~wp_mask;
  80. 	    see_monsters();
  81. 	}
  82. 	if (spfx & SPFX_STLTH) {
  83. 	    if (on) EStealth |= wp_mask;
  84. 	    else EStealth &= ~wp_mask;
  85. 	}
  86. 	if (spfx & SPFX_REGEN) {
  87. 	    if (on) ERegeneration |= wp_mask;
  88. 	    else ERegeneration &= ~wp_mask;
  89. 	}
  90. 	if (spfx & SPFX_TCTRL) {
  91. 	    if (on) ETeleport_control |= wp_mask;
  92. 	    else ETeleport_control &= ~wp_mask;
  93. 	}
  94. 	if (spfx & SPFX_WARN) {
  95. 	    if (spec_m2(otmp)) {
  96. 	    	if (on) {
  97. 			EWarn_of_mon |= wp_mask;
  98. 			flags.warntype |= spec_m2(otmp);
  99. 	    	} else {
  100. 			EWarn_of_mon &= ~wp_mask;
  101. 	    		flags.warntype &= ~spec_m2(otmp);
  102. 		}
  103. 		see_monsters();
  104. 	    } else {
  105. 		if (on) EWarning |= wp_mask;
  106. 	    	else EWarning &= ~wp_mask;
  107. 	    }
  108. 	}
  109. 	if (spfx & SPFX_EREGEN) {
  110. 	    if (on) EEnergy_regeneration |= wp_mask;
  111. 	    else EEnergy_regeneration &= ~wp_mask;
  112. 	}
  113. 	if (spfx & SPFX_HSPDAM) {
  114. 	    if (on) EHalf_spell_damage |= wp_mask;
  115. 	    else EHalf_spell_damage &= ~wp_mask;
  116. 	}
  117. 	if (spfx & SPFX_HPHDAM) {
  118. 	    if (on) EHalf_physical_damage |= wp_mask;
  119. 	    else EHalf_physical_damage &= ~wp_mask;
  120. 	}
  121. 	if (spfx & SPFX_XRAY) {
  122. 	    /* this assumes that no one else is using xray_range */
  123. 	    if (on) u.xray_range = 3;
  124. 	    else u.xray_range = -1;
  125. 	    vision_full_recalc = 1;
  126. 	}
  127. 	if ((spfx & SPFX_REFLECT) && (wp_mask & W_WEP)) {
  128. 	    if (on) EReflecting |= wp_mask;
  129. 	    else EReflecting &= ~wp_mask;
  130. 	}
  131.  
  132. 	if(wp_mask == W_ART && !on && oart->inv_prop) {
  133. 	    /* might have to turn off invoked power too */
  134. 	    if (oart->inv_prop <= LAST_PROP &&
  135. 		(u.uprops[oart->inv_prop].extrinsic & W_ARTI))
  136. 		(void) arti_invoke(otmp);
  137. 	}
  138. }

touch_artifact[edit]

  1. /*
  2. * creature (usually player) tries to touch (pick up or wield) an artifact obj.
  3. * Returns 0 if the object refuses to be touched.
  4. * This routine does not change any object chains.
  5. * Ignores such things as gauntlets, assuming the artifact is not
  6. * fooled by such trappings.
  7. */
  8. int
  9. touch_artifact(obj,mon)
  10. struct obj *obj;
  11. struct monst *mon;
  12. {
  13. register const struct artifact *oart = get_artifact(obj);
  14. boolean badclass, badalign, self_willed, yours;
  15.  
  16. if(!oart) return 1;
  17.  
  18. yours = (mon == &youmonst);
  19. /* all quest artifacts are self-willed; it this ever changes, `badclass'
  20. will have to be extended to explicitly include quest artifacts */
  21. self_willed = ((oart->spfx & SPFX_INTEL) != 0);
  22. if (yours) {
  23. 	badclass = self_willed &&
  24. 		   ((oart->role != NON_PM && !Role_if(oart->role)) ||
  25. 		    (oart->race != NON_PM && !Race_if(oart->race)));
  26. 	badalign = (oart->spfx & SPFX_RESTR) && oart->alignment != A_NONE &&
  27. 		   (oart->alignment != u.ualign.type || u.ualign.record < 0);
  28. } else if (!is_covetous(mon->data) && !is_mplayer(mon->data)) {
  29. 	badclass = self_willed &&
  30. 		   oart->role != NON_PM && oart != &artilist[ART_EXCALIBUR];
  31. 	badalign = (oart->spfx & SPFX_RESTR) && oart->alignment != A_NONE &&
  32. 		   (oart->alignment != sgn(mon->data->maligntyp));
  33. } else {    /* an M3_WANTSxxx monster or a fake player */
  34. 	/* special monsters trying to take the Amulet, invocation tools or
  35. 	   quest item can touch anything except for `spec_applies' artifacts */
  36. 	badclass = badalign = FALSE;
  37. }
  38. /* weapons which attack specific categories of monsters are
  39. bad for them even if their alignments happen to match */
  40. if (!badalign && (oart->spfx & SPFX_DBONUS) != 0) {
  41. 	struct artifact tmp;
  42.  
  43. 	tmp = *oart;
  44. 	tmp.spfx &= SPFX_DBONUS;
  45. 	badalign = !!spec_applies(&tmp, mon);
  46. }
  47.  
  48. if (((badclass || badalign) && self_willed) ||
  49. (badalign && (!yours || !rn2(4))))  {
  50. 	int dmg;
  51. 	char buf[BUFSZ];
  52.  
  53. 	if (!yours) return 0;
  54. 	You("are blasted by %s power!", s_suffix(the(xname(obj))));
  55. 	dmg = d((Antimagic ? 2 : 4), (self_willed ? 10 : 4));
  56. 	Sprintf(buf, "touching %s", oart->name);
  57. 	losehp(dmg, buf, KILLED_BY);
  58. 	exercise(A_WIS, FALSE);
  59. }
  60.  
  61. /* can pick it up unless you're totally non-synch'd with the artifact */
  62. if (badclass && badalign && self_willed) {
  63. 	if (yours) pline("%s your grasp!", Tobjnam(obj, "evade"));
  64. 	return 0;
  65. }
  66.  
  67. return 1;
  68. }
  69.  
  70. #endif /* OVLB */

spec_applies[edit]

This function is used to determine whether the artifact's to-hit and damage bonuses apply to the given target monster (which might be you).

It is used by #spec_abon, #spec_dbon to determine whether to grant their respective bonuses.

It is also called by #touch_artifact, to see if the artifact hates your race. In this case, the artifact's spfx is ANDed with SPFX_DBONUS before calling, so that other factors (like resistances) are not erroneously considered.

First, if the artifact doesn't hate any type of monster, and its attack bonus is physical (rather than elemental), the bonus always applies.

Next, the function checks if the artifact hates some particular monster, monster class, monster flag, or target alignment. If it does, the bonus is only applied if the target is appropriate.

Finally, if none of the above are true, (and the weapon has SPFX_ATTK, which is true for all artifacts with elemental damage bonuses), the function checks if the target resists the element type of the attack. If the target does not have the appropriate resistance, the bonus applies.

  1. #ifdef OVL1
  2.  
  3. /* decide whether an artifact's special attacks apply against mtmp */
  4. STATIC_OVL int
  5. spec_applies(weap, mtmp)
  6. register const struct artifact *weap;
  7. struct monst *mtmp;
  8. {
  9. 	struct permonst *ptr;
  10. 	boolean yours;
  11.  
  12. 	if(!(weap->spfx & (SPFX_DBONUS | SPFX_ATTK)))
  13. 	    return(weap->attk.adtyp == AD_PHYS);
  14.  
  15. 	yours = (mtmp == &youmonst);
  16. 	ptr = mtmp->data;
  17.  
  18. 	if (weap->spfx & SPFX_DMONS) {
  19. 	    return (ptr == &mons[(int)weap->mtype]);
  20. 	} else if (weap->spfx & SPFX_DCLAS) {
  21. 	    return (weap->mtype == (unsigned long)ptr->mlet);
  22. 	} else if (weap->spfx & SPFX_DFLAG1) {
  23. 	    return ((ptr->mflags1 & weap->mtype) != 0L);
  24. 	} else if (weap->spfx & SPFX_DFLAG2) {
  25. 	    return ((ptr->mflags2 & weap->mtype) || (yours &&
  26. 			((!Upolyd && (urace.selfmask & weap->mtype)) ||
  27. 			 ((weap->mtype & M2_WERE) && u.ulycn >= LOW_PM))));
  28. 	} else if (weap->spfx & SPFX_DALIGN) {
  29. 	    return yours ? (u.ualign.type != weap->alignment) :
  30. 			   (ptr->maligntyp == A_NONE ||
  31. 				sgn(ptr->maligntyp) != weap->alignment);
  32. 	} else if (weap->spfx & SPFX_ATTK) {
  33. 	    struct obj *defending_weapon = (yours ? uwep : MON_WEP(mtmp));
  34.  
  35. 	    if (defending_weapon && defending_weapon->oartifact &&
  36. 		    defends((int)weap->attk.adtyp, defending_weapon))
  37. 		return FALSE;
  38. 	    switch(weap->attk.adtyp) {
  39. 		case AD_FIRE:
  40. 			return !(yours ? Fire_resistance : resists_fire(mtmp));
  41. 		case AD_COLD:
  42. 			return !(yours ? Cold_resistance : resists_cold(mtmp));
  43. 		case AD_ELEC:
  44. 			return !(yours ? Shock_resistance : resists_elec(mtmp));
  45. 		case AD_MAGM:
  46. 		case AD_STUN:
  47. 			return !(yours ? Antimagic : (rn2(100) < ptr->mr));
  48. 		case AD_DRST:
  49. 			return !(yours ? Poison_resistance : resists_poison(mtmp));
  50. 		case AD_DRLI:
  51. 			return !(yours ? Drain_resistance : resists_drli(mtmp));
  52. 		case AD_STON:
  53. 			return !(yours ? Stone_resistance : resists_ston(mtmp));
  54. 		default:	impossible("Weird weapon special attack.");
  55. 	    }
  56. 	}
  57. 	return(0);
  58. }

spec_m2[edit]

  1. /* return the M2 flags of monster that an artifact's special attacks apply against */
  2. long
  3. spec_m2(otmp)
  4. struct obj *otmp;
  5. {
  6. 	register const struct artifact *artifact = get_artifact(otmp);
  7. 	if (artifact)
  8. 		return artifact->mtype;
  9. 	return 0L;
  10. }

spec_abon[edit]

  1. /* special attack bonus */
  2. int
  3. spec_abon(otmp, mon)
  4. struct obj *otmp;
  5. struct monst *mon;
  6. {
  7. 	register const struct artifact *weap = get_artifact(otmp);
  8.  
  9. 	/* no need for an extra check for `NO_ATTK' because this will
  10. 	   always return 0 for any artifact which has that attribute */
  11.  
  12. 	if (weap && weap->attk.damn && spec_applies(weap, mon))
  13. 	    return rnd((int)weap->attk.damn);
  14. 	return 0;
  15. }

spec_dbon[edit]

Calculates the additional damage caused by an artifact.

First, if the weapon has no damage bonus, then no additional damage is applied.

Next, the function checks if the artifact's special damage applies to the target monster. If it does not, there is no damage bonus.

Finally, the actual bonus is computed. If the artifact's attk contains a non-zero damage value, then a random amount between 1 and that value is applied. Otherwise (i.e. the damage value is 0), tmp is applied. Since tmp should be the result of the weapon's base damage roll, this has the effect of dealing double damage. This is how artifacts such as Grayswandir, Frost Brand, and the Banes get their double-damage property.

  1. /* special damage bonus */
  2. int
  3. spec_dbon(otmp, mon, tmp)
  4. struct obj *otmp;
  5. struct monst *mon;
  6. int tmp;
  7. {
  8. 	register const struct artifact *weap = get_artifact(otmp);
  9.  
  10. 	if (!weap || (weap->attk.adtyp == AD_PHYS && /* check for `NO_ATTK' */
  11. 			weap->attk.damn == 0 && weap->attk.damd == 0))
  12. 	    spec_dbon_applies = FALSE;
  13. 	else
  14. 	    spec_dbon_applies = spec_applies(weap, mon);
  15.  
  16. 	if (spec_dbon_applies)
  17. 	    return weap->attk.damd ? rnd((int)weap->attk.damd) : max(tmp,1);
  18. 	return 0;
  19. }

discover_artifact[edit]

  1. /* add identified artifact to discoveries list */
  2. void
  3. discover_artifact(m)
  4. xchar m;
  5. {
  6. int i;
  7.  
  8. /* look for this artifact in the discoveries list;
  9. if we hit an empty slot then it's not present, so add it */
  10. for (i = 0; i < NROFARTIFACTS; i++)
  11. 	if (artidisco[i] == 0 || artidisco[i] == m) {
  12. 	    artidisco[i] = m;
  13. 	    return;
  14. 	}
  15. /* there is one slot per artifact, so we should never reach the
  16. end without either finding the artifact or an empty slot... */
  17. impossible("couldn't discover artifact (%d)", (int)m);
  18. }

undiscovered_artifact[edit]

  1. /* used to decide whether an artifact has been fully identified */
  2. boolean
  3. undiscovered_artifact(m)
  4. xchar m;
  5. {
  6. int i;
  7.  
  8. /* look for this artifact in the discoveries list;
  9. if we hit an empty slot then it's undiscovered */
  10. for (i = 0; i < NROFARTIFACTS; i++)
  11. 	if (artidisco[i] == m)
  12. 	    return FALSE;
  13. 	else if (artidisco[i] == 0)
  14. 	    break;
  15. return TRUE;
  16. }

disp_artifact_discoveries[edit]

  1. /* display a list of discovered artifacts; return their count */
  2. int
  3. disp_artifact_discoveries(tmpwin)
  4. winid tmpwin;		/* supplied by dodiscover() */
  5. {
  6. int i, m, otyp;
  7. char buf[BUFSZ];
  8.  
  9. for (i = 0; i < NROFARTIFACTS; i++) {
  10. 	if (artidisco[i] == 0) break;	/* empty slot implies end of list */
  11. 	if (i == 0) putstr(tmpwin, iflags.menu_headings, "Artifacts");
  12. 	m = artidisco[i];
  13. 	otyp = artilist[m].otyp;
  14. 	Sprintf(buf, "  %s [%s %s]", artiname(m),
  15. 		align_str(artilist[m].alignment), simple_typename(otyp));
  16. 	putstr(tmpwin, 0, buf);
  17. }
  18. return i;
  19. }
  20.  
  21. #endif /* OVL1 */

Mb_hit[edit]

The Mb_hit function controls the special effects of Magicbane.

  1. #ifdef OVLB
  2.  
  3.  
  4. 	/*
  5. 	 * Magicbane's intrinsic magic is incompatible with normal
  6. 	 * enchantment magic.  Thus, its effects have a negative
  7. 	 * dependence on spe.  Against low mr victims, it typically
  8. 	 * does "double athame" damage, 2d4.  Occasionally, it will
  9. 	 * cast unbalancing magic which effectively averages out to
  10. 	 * 4d4 damage (3d4 against high mr victims), for spe = 0.
  11. 	 *
  12. 	 * Prior to 3.4.1, the cancel (aka purge) effect always
  13. 	 * included the scare effect too; now it's one or the other.
  14. 	 * Likewise, the stun effect won't be combined with either
  15. 	 * of those two; it will be chosen separately or possibly
  16. 	 * used as a fallback when scare or cancel fails.
  17. 	 *
  18. 	 * [Historical note: a change to artifact_hit() for 3.4.0
  19. 	 * unintentionally made all of Magicbane's special effects
  20. 	 * be blocked if the defender successfully saved against a
  21. 	 * stun attack.  As of 3.4.1, those effects can occur but
  22. 	 * will be slightly less likely than they were in 3.3.x.]
  23. 	 */
  24. #define MB_MAX_DIEROLL		8	/* rolls above this aren't magical */
  25. static const char * const mb_verb[2][4] = {
  26. 	{ "probe", "stun", "scare", "cancel" },
  27. 	{ "prod", "amaze", "tickle", "purge" },
  28. };
  29. #define MB_INDEX_PROBE		0
  30. #define MB_INDEX_STUN		1
  31. #define MB_INDEX_SCARE		2
  32. #define MB_INDEX_CANCEL		3

Several constant definitions precede the Mb_hit function code.

  1. /* called when someone is being hit by Magicbane */
  2. STATIC_OVL boolean
  3. Mb_hit(magr, mdef, mb, dmgptr, dieroll, vis, hittee)
  4. struct monst *magr, *mdef;	/* attacker and defender */
  5. struct obj *mb;			/* Magicbane */
  6. int *dmgptr;			/* extra damage target will suffer */
  7. int dieroll;			/* d20 that has already scored a hit */
  8. boolean vis;			/* whether the action can be seen */
  9. char *hittee;			/* target's name: "you" or mon_nam(mdef) */
  10. {
  11. struct permonst *old_uasmon;
  12. const char *verb;
  13. boolean youattack = (magr == &youmonst),
  14. 	    youdefend = (mdef == &youmonst),
  15. 	    resisted = FALSE, do_stun, do_confuse, result;
  16. int attack_indx, scare_dieroll = MB_MAX_DIEROLL / 2;
  17.  
  18. result = FALSE;		/* no message given yet */
  19. /* the most severe effects are less likely at higher enchantment */
  20. if (mb->spe >= 3)
  21. 	scare_dieroll /= (1 << (mb->spe / 3));
  22. /* if target successfully resisted the artifact damage bonus,
  23. reduce overall likelihood of the assorted special effects */
  24. if (!spec_dbon_applies) dieroll += 1;
  25.  
  26. /* might stun even when attempting a more severe effect, but
  27. in that case it will only happen if the other effect fails;
  28. extra damage will apply regardless; 3.4.1: sometimes might
  29. just probe even when it hasn't been enchanted */
  30. do_stun = (max(mb->spe,0) < rn2(spec_dbon_applies ? 11 : 7));
  31.  
  32. /* the special effects also boost physical damage; increments are
  33. generally cumulative, but since the stun effect is based on a
  34. different criterium its damage might not be included; the base
  35. damage is either 1d4 (athame) or 2d4 (athame+spec_dbon) depending
  36. on target's resistance check against AD_STUN (handled by caller)
  37. [note that a successful save against AD_STUN doesn't actually
  38. prevent the target from ending up stunned] */
  39. attack_indx = MB_INDEX_PROBE;
  40. *dmgptr += rnd(4);			/* (2..3)d4 */
  41. if (do_stun) {
  42. 	attack_indx = MB_INDEX_STUN;
  43. 	*dmgptr += rnd(4);		/* (3..4)d4 */
  44. }
  45. if (dieroll <= scare_dieroll) {
  46. 	attack_indx = MB_INDEX_SCARE;
  47. 	*dmgptr += rnd(4);		/* (3..5)d4 */
  48. }
  49. if (dieroll <= (scare_dieroll / 2)) {
  50. 	attack_indx = MB_INDEX_CANCEL;
  51. 	*dmgptr += rnd(4);		/* (4..6)d4 */
  52. }
  53.  
  54. /* give the hit message prior to inflicting the effects */
  55. verb = mb_verb[!!Hallucination][attack_indx];
  56. if (youattack || youdefend || vis) {
  57. 	result = TRUE;
  58. 	pline_The("magic-absorbing blade %s %s!",
  59. 		  vtense((const char *)0, verb), hittee);
  60. 	/* assume probing has some sort of noticeable feedback
  61. 	   even if it is being done by one monster to another */
  62. 	if (attack_indx == MB_INDEX_PROBE && !canspotmon(mdef))
  63. 	    map_invisible(mdef->mx, mdef->my);
  64. }
  65.  
  66. /* now perform special effects */
  67. switch (attack_indx) {
  68. case MB_INDEX_CANCEL:
  69. 	old_uasmon = youmonst.data;
  70. 	/* No mdef->mcan check: even a cancelled monster can be polymorphed
  71. 	 * into a golem, and the "cancel" effect acts as if some magical
  72. 	 * energy remains in spellcasting defenders to be absorbed later.
  73. 	 */
  74. 	if (!cancel_monst(mdef, mb, youattack, FALSE, FALSE)) {
  75. 	    resisted = TRUE;
  76. 	} else {
  77. 	    do_stun = FALSE;
  78. 	    if (youdefend) {
  79. 		if (youmonst.data != old_uasmon)
  80. 		    *dmgptr = 0;    /* rehumanized, so no more damage */
  81. 		if (u.uenmax > 0) {
  82. 		    You("lose magical energy!");
  83. 		    u.uenmax--;
  84. 		    if (u.uen > 0) u.uen--;
  85. 		    flags.botl = 1;
  86. 		}
  87. 	    } else {
  88. 		if (mdef->data == &mons[PM_CLAY_GOLEM])
  89. 		    mdef->mhp = 1;	/* cancelled clay golems will die */
  90. 		if (youattack && attacktype(mdef->data, AT_MAGC)) {
  91. 		    You("absorb magical energy!");
  92. 		    u.uenmax++;
  93. 		    u.uen++;
  94. 		    flags.botl = 1;
  95. 		}
  96. 	    }
  97. 	}
  98. 	break;
  99.  
  100. case MB_INDEX_SCARE:
  101. 	if (youdefend) {
  102. 	    if (Antimagic) {
  103. 		resisted = TRUE;
  104. 	    } else {
  105. 		nomul(-3);
  106. 		nomovemsg = "";
  107. 		if (magr && magr == u.ustuck && sticks(youmonst.data)) {
  108. 		    u.ustuck = (struct monst *)0;
  109. 		    You("release %s!", mon_nam(magr));
  110. 		}
  111. 	    }
  112. 	} else {
  113. 	    if (rn2(2) && resist(mdef, WEAPON_CLASS, 0, NOTELL))
  114. 		resisted = TRUE;
  115. 	    else
  116. 		monflee(mdef, 3, FALSE, (mdef->mhp > *dmgptr));
  117. 	}
  118. 	if (!resisted) do_stun = FALSE;
  119. 	break;
  120.  
  121. case MB_INDEX_STUN:
  122. 	do_stun = TRUE;		/* (this is redundant...) */
  123. 	break;
  124.  
  125. case MB_INDEX_PROBE:
  126. 	if (youattack && (mb->spe == 0 || !rn2(3 * abs(mb->spe)))) {
  127. 	    pline_The("%s is insightful.", verb);
  128. 	    /* pre-damage status */
  129. 	    probe_monster(mdef);
  130. 	}
  131. 	break;
  132. }
  133. /* stun if that was selected and a worse effect didn't occur */
  134. if (do_stun) {
  135. 	if (youdefend)
  136. 	    make_stunned((HStun + 3), FALSE);
  137. 	else
  138. 	    mdef->mstun = 1;
  139. 	/* avoid extra stun message below if we used mb_verb["stun"] above */
  140. 	if (attack_indx == MB_INDEX_STUN) do_stun = FALSE;
  141. }
  142. /* lastly, all this magic can be confusing... */
  143. do_confuse = !rn2(12);
  144. if (do_confuse) {
  145. 	if (youdefend)
  146. 	    make_confused(HConfusion + 4, FALSE);
  147. 	else
  148. 	    mdef->mconf = 1;
  149. }
  150.  
  151. if (youattack || youdefend || vis) {
  152. 	(void) upstart(hittee);	/* capitalize */
  153. 	if (resisted) {
  154. 	    pline("%s %s!", hittee, vtense(hittee, "resist"));
  155. 	    shieldeff(youdefend ? u.ux : mdef->mx,
  156. 		      youdefend ? u.uy : mdef->my);
  157. 	}
  158. 	if ((do_stun || do_confuse) && flags.verbose) {
  159. 	    char buf[BUFSZ];
  160.  
  161. 	    buf[0] = '\0';
  162. 	    if (do_stun) Strcat(buf, "stunned");
  163. 	    if (do_stun && do_confuse) Strcat(buf, " and ");
  164. 	    if (do_confuse) Strcat(buf, "confused");
  165. 	    pline("%s %s %s%c", hittee, vtense(hittee, "are"),
  166. 		  buf, (do_stun && do_confuse) ? '!' : '.');
  167. 	}
  168. }
  169.  
  170. return result;
  171. }

artifact_hit[edit]

  1. /* Function used when someone attacks someone else with an artifact
  2. * weapon.  Only adds the special (artifact) damage, and returns a 1 if it
  3. * did something special (in which case the caller won't print the normal
  4. * hit message).  This should be called once upon every artifact attack;
  5. * dmgval() no longer takes artifact bonuses into account.  Possible
  6. * extension: change the killer so that when an orc kills you with
  7. * Stormbringer it's "killed by Stormbringer" instead of "killed by an orc".
  8. */
  9. boolean
  10. artifact_hit(magr, mdef, otmp, dmgptr, dieroll)
  11. struct monst *magr, *mdef;
  12. struct obj *otmp;
  13. int *dmgptr;
  14. int dieroll; /* needed for Magicbane and vorpal blades */
  15. {
  16. 	boolean youattack = (magr == &youmonst);
  17. 	boolean youdefend = (mdef == &youmonst);
  18. 	boolean vis = (!youattack && magr && cansee(magr->mx, magr->my))
  19. 	    || (!youdefend && cansee(mdef->mx, mdef->my))
  20. 	    || (youattack && u.uswallow && mdef == u.ustuck && !Blind);
  21. 	boolean realizes_damage;
  22. 	const char *wepdesc;
  23. 	static const char you[] = "you";
  24. 	char hittee[BUFSZ];
  25.  
  26. 	Strcpy(hittee, youdefend ? you : mon_nam(mdef));
  27.  
  28. 	/* The following takes care of most of the damage, but not all--
  29. 	 * the exception being for level draining, which is specially
  30. 	 * handled.  Messages are done in this function, however.
  31. 	 */
  32. 	*dmgptr += spec_dbon(otmp, mdef, *dmgptr);
  33.  
  34. 	if (youattack && youdefend) {
  35. 	    impossible("attacking yourself with weapon?");
  36. 	    return FALSE;
  37. 	}
  38.  
  39. 	realizes_damage = (youdefend || vis || 
  40. 			   /* feel the effect even if not seen */
  41. 			   (youattack && mdef == u.ustuck));
  42.  
  43. 	/* the four basic attacks: fire, cold, shock and missiles */
  44. 	if (attacks(AD_FIRE, otmp)) {
  45. 	    if (realizes_damage)
  46. 		pline_The("fiery blade %s %s%c",
  47. 			!spec_dbon_applies ? "hits" :
  48. 			(mdef->data == &mons[PM_WATER_ELEMENTAL]) ?
  49. 			"vaporizes part of" : "burns",
  50. 			hittee, !spec_dbon_applies ? '.' : '!');
  51. 	    if (!rn2(4)) (void) destroy_mitem(mdef, POTION_CLASS, AD_FIRE);
  52. 	    if (!rn2(4)) (void) destroy_mitem(mdef, SCROLL_CLASS, AD_FIRE);
  53. 	    if (!rn2(7)) (void) destroy_mitem(mdef, SPBOOK_CLASS, AD_FIRE);
  54. 	    if (youdefend && Slimed) burn_away_slime();
  55. 	    return realizes_damage;
  56. 	}
  57. 	if (attacks(AD_COLD, otmp)) {
  58. 	    if (realizes_damage)
  59. 		pline_The("ice-cold blade %s %s%c",
  60. 			!spec_dbon_applies ? "hits" : "freezes",
  61. 			hittee, !spec_dbon_applies ? '.' : '!');
  62. 	    if (!rn2(4)) (void) destroy_mitem(mdef, POTION_CLASS, AD_COLD);
  63. 	    return realizes_damage;
  64. 	}
  65. 	if (attacks(AD_ELEC, otmp)) {
  66. 	    if (realizes_damage)
  67. 		pline_The("massive hammer hits%s %s%c",
  68. 			  !spec_dbon_applies ? "" : "!  Lightning strikes",
  69. 			  hittee, !spec_dbon_applies ? '.' : '!');
  70. 	    if (!rn2(5)) (void) destroy_mitem(mdef, RING_CLASS, AD_ELEC);
  71. 	    if (!rn2(5)) (void) destroy_mitem(mdef, WAND_CLASS, AD_ELEC);
  72. 	    return realizes_damage;
  73. 	}
  74. 	if (attacks(AD_MAGM, otmp)) {
  75. 	    if (realizes_damage)
  76. 		pline_The("imaginary widget hits%s %s%c",
  77. 			  !spec_dbon_applies ? "" :
  78. 				"!  A hail of magic missiles strikes",
  79. 			  hittee, !spec_dbon_applies ? '.' : '!');
  80. 	    return realizes_damage;
  81. 	}
  82.  
  83. 	if (attacks(AD_STUN, otmp) && dieroll <= MB_MAX_DIEROLL) {
  84. 	    /* Magicbane's special attacks (possibly modifies hittee[]) */
  85. 	    return Mb_hit(magr, mdef, otmp, dmgptr, dieroll, vis, hittee);
  86. 	}
  87.  
  88. 	if (!spec_dbon_applies) {
  89. 	    /* since damage bonus didn't apply, nothing more to do;  
  90. 	       no further attacks have side-effects on inventory */
  91. 	    return FALSE;
  92. 	}
  93.  
  94. 	/* We really want "on a natural 20" but Nethack does it in */
  95. 	/* reverse from AD&D. */
  96. 	if (spec_ability(otmp, SPFX_BEHEAD)) {
  97. 	    if (otmp->oartifact == ART_TSURUGI_OF_MURAMASA && dieroll == 1) {
  98. 		wepdesc = "The razor-sharp blade";
  99. 		/* not really beheading, but so close, why add another SPFX */
  100. 		if (youattack && u.uswallow && mdef == u.ustuck) {
  101. 		    You("slice %s wide open!", mon_nam(mdef));
  102. 		    *dmgptr = 2 * mdef->mhp + FATAL_DAMAGE_MODIFIER;
  103. 		    return TRUE;
  104. 		}
  105. 		if (!youdefend) {
  106. 			/* allow normal cutworm() call to add extra damage */
  107. 			if(notonhead)
  108. 			    return FALSE;
  109.  
  110. 			if (bigmonst(mdef->data)) {
  111. 				if (youattack)
  112. 					You("slice deeply into %s!",
  113. 						mon_nam(mdef));
  114. 				else if (vis)
  115. 					pline("%s cuts deeply into %s!",
  116. 					      Monnam(magr), hittee);
  117. 				*dmgptr *= 2;
  118. 				return TRUE;
  119. 			}
  120. 			*dmgptr = 2 * mdef->mhp + FATAL_DAMAGE_MODIFIER;
  121. 			pline("%s cuts %s in half!", wepdesc, mon_nam(mdef));
  122. 			otmp->dknown = TRUE;
  123. 			return TRUE;
  124. 		} else {
  125. 			if (bigmonst(youmonst.data)) {
  126. 				pline("%s cuts deeply into you!",
  127. 				      magr ? Monnam(magr) : wepdesc);
  128. 				*dmgptr *= 2;
  129. 				return TRUE;
  130. 			}
  131.  
  132. 			/* Players with negative AC's take less damage instead
  133. 			 * of just not getting hit.  We must add a large enough
  134. 			 * value to the damage so that this reduction in
  135. 			 * damage does not prevent death.
  136. 			 */
  137. 			*dmgptr = 2 * (Upolyd ? u.mh : u.uhp) + FATAL_DAMAGE_MODIFIER;
  138. 			pline("%s cuts you in half!", wepdesc);
  139. 			otmp->dknown = TRUE;
  140. 			return TRUE;
  141. 		}
  142. 	    } else if (otmp->oartifact == ART_VORPAL_BLADE &&
  143. 			(dieroll == 1 || mdef->data == &mons[PM_JABBERWOCK])) {
  144. 		static const char * const behead_msg[2] = {
  145. 		     "%s beheads %s!",
  146. 		     "%s decapitates %s!"
  147. 		};
  148.  
  149. 		if (youattack && u.uswallow && mdef == u.ustuck)
  150. 			return FALSE;
  151. 		wepdesc = artilist[ART_VORPAL_BLADE].name;
  152. 		if (!youdefend) {
  153. 			if (!has_head(mdef->data) || notonhead || u.uswallow) {
  154. 				if (youattack)
  155. 					pline("Somehow, you miss %s wildly.",
  156. 						mon_nam(mdef));
  157. 				else if (vis)
  158. 					pline("Somehow, %s misses wildly.",
  159. 						mon_nam(magr));
  160. 				*dmgptr = 0;
  161. 				return ((boolean)(youattack || vis));
  162. 			}
  163. 			if (noncorporeal(mdef->data) || amorphous(mdef->data)) {
  164. 				pline("%s slices through %s %s.", wepdesc,
  165. 				      s_suffix(mon_nam(mdef)),
  166. 				      mbodypart(mdef,NECK));
  167. 				return TRUE;
  168. 			}
  169. 			*dmgptr = 2 * mdef->mhp + FATAL_DAMAGE_MODIFIER;
  170. 			pline(behead_msg[rn2(SIZE(behead_msg))],
  171. 			      wepdesc, mon_nam(mdef));
  172. 			otmp->dknown = TRUE;
  173. 			return TRUE;
  174. 		} else {
  175. 			if (!has_head(youmonst.data)) {
  176. 				pline("Somehow, %s misses you wildly.",
  177. 				      magr ? mon_nam(magr) : wepdesc);
  178. 				*dmgptr = 0;
  179. 				return TRUE;
  180. 			}
  181. 			if (noncorporeal(youmonst.data) || amorphous(youmonst.data)) {
  182. 				pline("%s slices through your %s.",
  183. 				      wepdesc, body_part(NECK));
  184. 				return TRUE;
  185. 			}
  186. 			*dmgptr = 2 * (Upolyd ? u.mh : u.uhp)
  187. 				  + FATAL_DAMAGE_MODIFIER;
  188. 			pline(behead_msg[rn2(SIZE(behead_msg))],
  189. 			      wepdesc, "you");
  190. 			otmp->dknown = TRUE;
  191. 			/* Should amulets fall off? */
  192. 			return TRUE;
  193. 		}
  194. 	    }
  195. 	}
  196. 	if (spec_ability(otmp, SPFX_DRLI)) {
  197. 		if (!youdefend) {
  198. 			if (vis) {
  199. 			    if(otmp->oartifact == ART_STORMBRINGER)
  200. 				pline_The("%s blade draws the life from %s!",
  201. 				      hcolor(NH_BLACK),
  202. 				      mon_nam(mdef));
  203. 			    else
  204. 				pline("%s draws the life from %s!",
  205. 				      The(distant_name(otmp, xname)),
  206. 				      mon_nam(mdef));
  207. 			}
  208. 			if (mdef->m_lev == 0) {
  209. 			    *dmgptr = 2 * mdef->mhp + FATAL_DAMAGE_MODIFIER;
  210. 			} else {
  211. 			    int drain = rnd(8);
  212. 			    *dmgptr += drain;
  213. 			    mdef->mhpmax -= drain;
  214. 			    mdef->m_lev--;
  215. 			    drain /= 2;
  216. 			    if (drain) healup(drain, 0, FALSE, FALSE);
  217. 			}
  218. 			return vis;
  219. 		} else { /* youdefend */
  220. 			int oldhpmax = u.uhpmax;
  221.  
  222. 			if (Blind)
  223. 				You_feel("an %s drain your life!",
  224. 				    otmp->oartifact == ART_STORMBRINGER ?
  225. 				    "unholy blade" : "object");
  226. 			else if (otmp->oartifact == ART_STORMBRINGER)
  227. 				pline_The("%s blade drains your life!",
  228. 				      hcolor(NH_BLACK));
  229. 			else
  230. 				pline("%s drains your life!",
  231. 				      The(distant_name(otmp, xname)));
  232. 			losexp("life drainage");
  233. 			if (magr && magr->mhp < magr->mhpmax) {
  234. 			    magr->mhp += (oldhpmax - u.uhpmax)/2;
  235. 			    if (magr->mhp > magr->mhpmax) magr->mhp = magr->mhpmax;
  236. 			}
  237. 			return TRUE;
  238. 		}
  239. 	}
  240. 	return FALSE;
  241. }

doinvoke[edit]

The doinvoke function in artifact.c handles the #invoke extended command (cmd.c#line1485) and the M-i keypress (cmd.c#line1402). This function puts up a menu to ask which object to invoke.

  1. static NEARDATA const char recharge_type[] = { ALLOW_COUNT, ALL_CLASSES, 0 };
  2. static NEARDATA const char invoke_types[] = { ALL_CLASSES, 0 };
  3. 		/* #invoke: an "ugly check" filters out most objects */

The invoke_types constant declared on line 1170 specifies that all classes of objects will appear in the menu. Thus in the game, you can attempt to #invoke any object. If you do not know that invoke is meant for certain artifacts, then you can waste much effort in the game in unsuccessful attempts to invoke something. (The recharge_type declaration on line 1169 is used later in the arti_invoke function to implement the Platinum Yendorian Express Card, which recharges objects when you #invoke it.)

  1. int
  2. doinvoke()
  3. {
  4. register struct obj *obj;
  5.  
  6. obj = getobj(invoke_types, "invoke");

The doinvoke function itself declares *obj to be the object, and calls getobj to ask the user which object to invoke.

  1. if (!obj) return 0;
  2. if (obj->oartifact && !touch_artifact(obj, &youmonst)) return 1;

Line 1179 checks if the user escaped from the menu without actually selecting an object. Line 1180 enforces that a player must touch the artifact to #invoke it. If the object is actually an artifact, line 1180 causes the player to touch it. If the artifact "evades your grasp" (#line550) then the function returns.

  1. return arti_invoke(obj);
  2. }

If we pass both checks, then we call arti_invoke to actually attempt to invoke the object (which at this point could be any object from the inventory).

arti_invoke[edit]

The doinvoke function calls the arti_invoke function, passing the object to invoke. At this point, we do not know if obj is an artifact, but if it is an artifact than the player has already touched it and (whether or not it blasted the player) it has not evaded the player's grasp.

  1. STATIC_OVL int
  2. arti_invoke(obj)
  3. register struct obj *obj;
  4. {
  5. register const struct artifact *oart = get_artifact(obj);

Because this is the top of a new function, we can declare a new variable: struct artifact *oart to describe what this artifact does. In particular, oart->inv_prop is the invocation property that tells us what to do. You can find invocation properties by looking in artilist.h.

  1. if(!oart || !oart->inv_prop) {
  2. 	if(obj->otyp == CRYSTAL_BALL)
  3. 	    use_crystal_ball(obj);
  4. 	else
  5. 	    pline(nothing_happens);
  6. 	return 1;
  7. }

If the object is not an artifact, or if it is an artifact that does nott have an invocation property, then "nothing happens". The only exception is the crystal ball. For a crystall ball, we call use_crystal_ball at detect.c#line789, exactly as if the player had applied the thing instead of invoking it.

Invoking special powers[edit]

As it is, there are two types of invocation property. The crucial difference is whether or not the property is a special power. In the header files, most properties appear in prop.h#line8. They include such things as fire resistance, levitation, water walking, and life saving. Artifacts can provide properties when you carry them, when you wield them, or as the invocation property, in which case you can #invoke to enable or disable the property. Special powers appear in artifact.h#line54. You can only use them by #invoking the correct artifact.

The giant if statement starting at line 1198 handles special powers. If the property is not a special power, then we skip to #line1353.

  1. if(oart->inv_prop > LAST_PROP) {
  2. 	/* It's a special power, not "just" a property */
  3. 	if(obj->age > monstermoves) {
  4. 	    /* the artifact is tired :-) */
  5. 	    You_feel("that %s %s ignoring you.",
  6. 		     the(xname(obj)), otense(obj, "are"));
  7. 	    /* and just got more so; patience is essential... */
  8. 	    obj->age += (long) d(3,10);
  9. 	    return 1;
  10. 	}
  11. 	obj->age = monstermoves + rnz(100);

"You feel that" the artifact "is ignoring you", and the function returns, if the player invokes a tired artifact. Otherwise, we tire the artifact for an average of 100 turns and pass control to the next statement, which switches on the specific special property.

  1. 	switch(oart->inv_prop) {
  2. 	case TAMING: {
  3. 	    struct obj pseudo;
  4.  
  5. 	    pseudo = zeroobj;	/* neither cursed nor blessed */
  6. 	    pseudo.otyp = SCR_TAMING;
  7. 	    (void) seffects(&pseudo);
  8. 	    break;
  9. 	  }

TAMING is the special power of the Palantir of Westernesse. As it happens, the Palantir no longer exists in the game. Thus though this is an example of how to implement a special power, this code will never actually run.

TAMING behaves exactly like an uncursed scroll of taming. The hack here is that we declare a pseudo object to act as the scroll, then pass control to seffects in read.c#line646.

  1. 	case HEALING: {
  2. 	    int healamt = (u.uhpmax + 1 - u.uhp) / 2;
  3. 	    long creamed = (long)u.ucreamed;
  4.  
  5. 	    if (Upolyd) healamt = (u.mhmax + 1 - u.mh) / 2;
  6. 	    if (healamt || Sick || Slimed || Blinded > creamed)
  7. 		You_feel("better.");
  8. 	    else
  9. 		goto nothing_special;
  10. 	    if (healamt > 0) {
  11. 		if (Upolyd) u.mh += healamt;
  12. 		else u.uhp += healamt;
  13. 	    }
  14. 	    if(Sick) make_sick(0L,(char *)0,FALSE,SICK_ALL);
  15. 	    if(Slimed) Slimed = 0L;
  16. 	    if (Blinded > creamed) make_blinded(creamed, FALSE);
  17. 	    flags.botl = 1;
  18. 	    break;
  19. 	  }

HEALING is the special power of the the Staff of Aesculapius. Line 1220 (or 1223 if you are polymorphed) gives the formula for computing the amount of to heal. The code also cures sickness and slime, but it trying to unblind you it is careful to not #wipe any cream pie.

The goto on line 1227 jumps to #line1373. The nothing_special label handles when the invocation seems to do nothing; in this case nothing happens if you are already healthy.

  1. 	case ENERGY_BOOST: {
  2. 	    int epboost = (u.uenmax + 1 - u.uen) / 2;
  3. 	    if (epboost > 120) epboost = 120;		/* arbitrary */
  4. 	    else if (epboost < 12) epboost = u.uenmax - u.uen;
  5. 	    if(epboost) {
  6. 		You_feel("re-energized.");
  7. 		u.uen += epboost;
  8. 		flags.botl = 1;
  9. 	    } else
  10. 		goto nothing_special;
  11. 	    break;
  12. 	  }

ENERGY_BOOST is the special power of the Mitre of Holiness. It restores power instead of healing you. The formula is on line 1239.

  1. 	case UNTRAP: {
  2. 	    if(!untrap(TRUE)) {
  3. 		obj->age = 0; /* don't charge for changing their mind */
  4. 		return 0;
  5. 	    }
  6. 	    break;
  7. 	  }

UNTRAP is the special power of the Master Key of Thievery. Control passes to the untrap function trap.c#line3395. This is the same function used by the #untrap command, the difference being that #untrap calls untrap(FALSE). Thus the Master Key of Thievery can sometimes force the untrapping to succeed in cases where the #untrap command might fail.

  1. 	case CHARGE_OBJ: {
  2. 	    struct obj *otmp = getobj(recharge_type, "charge");
  3. 	    boolean b_effect;
  4.  
  5. 	    if (!otmp) {
  6. 		obj->age = 0;
  7. 		return 0;
  8. 	    }
  9. 	    b_effect = obj->blessed &&
  10. 		(Role_switch == oart->role || !oart->role);
  11. 	    recharge(otmp, b_effect ? 1 : obj->cursed ? -1 : 0);
  12. 	    update_inventory();
  13. 	    break;
  14. 	  }

CHARGE_OBJ is the special power of the Platinum Yendorian Express Card. Now we put up a menu asking for any object to charge. Nothing happens (and the PYEC does not become tired) if the player escapes without choosing an object. Otherwise, the selected object (otmp) is charged by calling recharge(), exactly like when reading a scroll of charging.

The type of charging done depends not only on the BUC status of the artifact being invoked , but also on the role of the player, which must match the designated role of the artifact (Tourist for the PYEC) for blessed charging to be possible. Interestingly, the code also explicitly allows blessed charging using artifacts that have no designated role, even though no such artifacts currently exist in the game.

  1. 	case LEV_TELE:
  2. 	    level_tele();
  3. 	    break;

LEV_TELE or level teleportation is the special power of the Orb of Fate. We call level_tele at teleport.c#line563.

  1. 	case CREATE_PORTAL: {
  2. 	    int i, num_ok_dungeons, last_ok_dungeon = 0;
  3. 	    d_level newlev;
  4. 	    extern int n_dgns; /* from dungeon.c */
  5. 	    winid tmpwin = create_nhwindow(NHW_MENU);
  6. 	    anything any;
  7.  
  8. 	    any.a_void = 0;	/* set all bits to zero */
  9. 	    start_menu(tmpwin);
  10. 	    /* use index+1 (cant use 0) as identifier */
  11. 	    for (i = num_ok_dungeons = 0; i < n_dgns; i++) {
  12. 		if (!dungeons[i].dunlev_ureached) continue;
  13. 		any.a_int = i+1;
  14. 		add_menu(tmpwin, NO_GLYPH, &any, 0, 0, ATR_NONE,
  15. 			 dungeons[i].dname, MENU_UNSELECTED);
  16. 		num_ok_dungeons++;
  17. 		last_ok_dungeon = i;
  18. 	    }
  19. 	    end_menu(tmpwin, "Open a portal to which dungeon?");
  20. 	    if (num_ok_dungeons > 1) {
  21. 		/* more than one entry; display menu for choices */
  22. 		menu_item *selected;
  23. 		int n;
  24.  
  25. 		n = select_menu(tmpwin, PICK_ONE, &selected);
  26. 		if (n <= 0) {
  27. 		    destroy_nhwindow(tmpwin);
  28. 		    goto nothing_special;
  29. 		}
  30. 		i = selected[0].item.a_int - 1;
  31. 		free((genericptr_t)selected);
  32. 	    } else
  33. 		i = last_ok_dungeon;	/* also first & only OK dungeon */
  34. 	    destroy_nhwindow(tmpwin);
  35.  
  36. 	    /*
  37. 	     * i is now index into dungeon structure for the new dungeon.
  38. 	     * Find the closest level in the given dungeon, open
  39. 	     * a use-once portal to that dungeon and go there.
  40. 	     * The closest level is either the entry or dunlev_ureached.
  41. 	     */
  42. 	    newlev.dnum = i;
  43. 	    if(dungeons[i].depth_start >= depth(&u.uz))
  44. 		newlev.dlevel = dungeons[i].entry_lev;
  45. 	    else
  46. 		newlev.dlevel = dungeons[i].dunlev_ureached;
  47. 	    if(u.uhave.amulet || In_endgame(&u.uz) || In_endgame(&newlev) ||
  48. 	       newlev.dnum == u.uz.dnum) {
  49. 		You_feel("very disoriented for a moment.");
  50. 	    } else {
  51. 		if(!Blind) You("are surrounded by a shimmering sphere!");
  52. 		else You_feel("weightless for a moment.");
  53. 		goto_level(&newlev, FALSE, FALSE, FALSE);
  54. 	    }
  55. 	    break;
  56. 	  }

CREATE_PORTAL is the special power of the Eye of the Aethiopica. The code all appears here because there is no other way to branchport in NetHack.

  1. 	case ENLIGHTENING:
  2. 	    enlightenment(0);
  3. 	    break;

ENLIGHTENING is the special power of the Eyes of the Overworld. Control passes to the enlightenment function of cmd.c#line829. The 0 simply means that the game is not over yet.

  1. 	case CREATE_AMMO: {
  2. 	    struct obj *otmp = mksobj(ARROW, TRUE, FALSE);
  3.  
  4. 	    if (!otmp) goto nothing_special;
  5. 	    otmp->blessed = obj->blessed;
  6. 	    otmp->cursed = obj->cursed;
  7. 	    otmp->bknown = obj->bknown;
  8. 	    if (obj->blessed) {
  9. 		if (otmp->spe < 0) otmp->spe = 0;
  10. 		otmp->quan += rnd(10);
  11. 	    } else if (obj->cursed) {
  12. 		if (otmp->spe > 0) otmp->spe = 0;
  13. 	    } else
  14. 		otmp->quan += rnd(5);
  15. 	    otmp->owt = weight(otmp);
  16. 	    otmp = hold_another_object(otmp, "Suddenly %s out.",
  17. 				       aobjnam(otmp, "fall"), (const char *)0);
  18. 	    break;
  19. 	  }
  20. 	}

CREATE_AMMO is the special power of the Longbow of Diana. It calls mksobj() to create a stack of random arrows. The arrows are given the same BUC status as the artifact that created them, and their enchantment is adjusted to ensure that no blessed negatively enchanted or cursed positively enchanted arrows are produced. The size of the stack is also randomly increased by up to 10 (if blessed) or 5 (if uncursed) arrows. If possible, the arrows are silently put in your inventory, but they will "suddenly fall out" if your inventory is full.

Invoking properties that toggle[edit]

  1. } else {
  2. 	long eprop = (u.uprops[oart->inv_prop].extrinsic ^= W_ARTI),
  3. 	     iprop = u.uprops[oart->inv_prop].intrinsic;
  4. 	boolean on = (eprop & W_ARTI) != 0; /* true if invoked prop just set */
  5.  
  6. 	if(on && obj->age > monstermoves) {
  7. 	    /* the artifact is tired :-) */
  8. 	    u.uprops[oart->inv_prop].extrinsic ^= W_ARTI;
  9. 	    You_feel("that %s %s ignoring you.",
  10. 		     the(xname(obj)), otense(obj, "are"));
  11. 	    /* can't just keep repeatedly trying */
  12. 	    obj->age += (long) d(3,10);
  13. 	    return 1;
  14. 	} else if(!on) {
  15. 	    /* when turning off property, determine downtime */
  16. 	    /* arbitrary for now until we can tune this -dlc */
  17. 	    obj->age = monstermoves + rnz(100);
  18. 	}
  19.  
  20. 	if ((eprop & ~W_ARTI) || iprop) {
  21. nothing_special:
  22. 	    /* you had the property from some other source too */
  23. 	    if (carried(obj))
  24. 		You_feel("a surge of power, but nothing seems to happen.");
  25. 	    return 1;
  26. 	}
  27. 	switch(oart->inv_prop) {
  28. 	case CONFLICT:
  29. 	    if(on) You_feel("like a rabble-rouser.");
  30. 	    else You_feel("the tension decrease around you.");
  31. 	    break;
  32. 	case LEVITATION:
  33. 	    if(on) {
  34. 		float_up();
  35. 		spoteffects(FALSE);
  36. 	    } else (void) float_down(I_SPECIAL|TIMEOUT, W_ARTI);
  37. 	    break;
  38. 	case INVIS:
  39. 	    if (BInvis || Blind) goto nothing_special;
  40. 	    newsym(u.ux, u.uy);
  41. 	    if (on)
  42. 		Your("body takes on a %s transparency...",
  43. 		     Hallucination ? "normal" : "strange");
  44. 	    else
  45. 		Your("body seems to unfade...");
  46. 	    break;
  47. 	}
  48. }
  49.  
  50. return 1;
  51. }

artifact_light[edit]

  1. /* WAC return TRUE if artifact is always lit */
  2. boolean
  3. artifact_light(obj)
  4. struct obj *obj;
  5. {
  6. return (get_artifact(obj) && obj->oartifact == ART_SUNSWORD);
  7. }
  8.  
  9. /* KMH -- Talking artifacts are finally implemented */
  10. void
  11. arti_speak(obj)
  12. struct obj *obj;
  13. {
  14. 	register const struct artifact *oart = get_artifact(obj);
  15. 	const char *line;
  16. 	char buf[BUFSZ];
  17.  
  18.  
  19. 	/* Is this a speaking artifact? */
  20. 	if (!oart || !(oart->spfx & SPFX_SPEAK))
  21. 		return;
  22.  
  23. 	line = getrumor(bcsign(obj), buf, TRUE);
  24. 	if (!*line)
  25. 		line = "NetHack rumors file closed for renovation.";
  26. 	pline("%s:", Tobjnam(obj, "whisper"));
  27. 	verbalize("%s", line);
  28. 	return;
  29. }

artifact_has_invprop[edit]

  1. boolean
  2. artifact_has_invprop(otmp, inv_prop)
  3. struct obj *otmp;
  4. uchar inv_prop;
  5. {
  6. 	const struct artifact *arti = get_artifact(otmp);
  7.  
  8. 	return((boolean)(arti && (arti->inv_prop == inv_prop)));
  9. }

arti_cost[edit]

  1. /* Return the price sold to the hero of a given artifact or unique item */
  2. long
  3. arti_cost(otmp)
  4. struct obj *otmp;
  5. {
  6. 	if (!otmp->oartifact)
  7. 	    return ((long)objects[otmp->otyp].oc_cost);
  8. 	else if (artilist[(int) otmp->oartifact].cost)
  9. 	    return (artilist[(int) otmp->oartifact].cost);
  10. 	else
  11. 	    return (100L * (long)objects[otmp->otyp].oc_cost);
  12. }
  13.  
  14. #endif /* OVLB */
  15.  
  16. /*artifact.c*/