Source:NetHack 3.6.0/src/artifact.c

From NetHackWiki
Jump to: navigation, search

Below is the full text to artifact.c from the source code of NetHack 3.6.0. To link to a particular line, write [[Source:NetHack 3.6.0/src/artifact.c#line123]], for example.

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.

Top of file

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

hack_artifacts

  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.  }
  24.  

init_artifacts

  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.  }
  9.  

save_artifacts

  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.  }
  8.  

restore_artifacts

  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.  }
  9.  

artiname

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

mk_artifact

  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
  8.     possible.
  9.     For the 2nd case, caller should use ``obj = mk_artifact(obj, A_NONE);''
  10.     for the 1st, ``obj = mk_artifact((struct obj *)0, some_alignment);''.
  11.   */
  12.  struct obj *
  13.  mk_artifact(otmp, alignment)
  14.  struct obj *otmp;   /* existing object; ignored if alignment specified */
  15.  aligntyp alignment; /* target alignment, or A_NONE */
  16.  {
  17.      const struct artifact *a;
  18.      int m, n, altn;
  19.      boolean by_align = (alignment != A_NONE);
  20.      short o_typ = (by_align || !otmp) ? 0 : otmp->otyp;
  21.      boolean unique = !by_align && otmp && objects[o_typ].oc_unique;
  22.      short eligible[NROFARTIFACTS];
  23.  
  24.      n = altn = 0;    /* no candidates found yet */
  25.      eligible[0] = 0; /* lint suppression */
  26.      /* gather eligible artifacts */
  27.      for (m = 1, a = &artilist[m]; a->otyp; a++, m++) {
  28.          if (artiexist[m])
  29.              continue;
  30.          if ((a->spfx & SPFX_NOGEN) || unique)
  31.              continue;
  32.  
  33.          if (!by_align) {
  34.              /* looking for a particular type of item; not producing a
  35.                 divine gift so we don't care about role's first choice */
  36.              if (a->otyp == o_typ)
  37.                  eligible[n++] = m;
  38.              continue; /* move on to next possibility */
  39.          }
  40.  
  41.          /* we're looking for an alignment-specific item
  42.             suitable for hero's role+race */
  43.          if ((a->alignment == alignment || a->alignment == A_NONE)
  44.              /* avoid enemies' equipment */
  45.              && (a->race == NON_PM || !race_hostile(&mons[a->race]))) {
  46.              /* when a role-specific first choice is available, use it */
  47.              if (Role_if(a->role)) {
  48.                  /* make this be the only possibility in the list */
  49.                  eligible[0] = m;
  50.                  n = 1;
  51.                  break; /* skip all other candidates */
  52.              }
  53.              /* found something to consider for random selection */
  54.              if (a->alignment != A_NONE || u.ugifts > 0) {
  55.                  /* right alignment, or non-aligned with at least 1
  56.                     previous gift bestowed, makes this one viable */
  57.                  eligible[n++] = m;
  58.              } else {
  59.                  /* non-aligned with no previous gifts;
  60.                     if no candidates have been found yet, record
  61.                     this one as a[nother] fallback possibility in
  62.                     case all aligned candidates have been used up
  63.                     (via wishing, naming, bones, random generation) */
  64.                  if (!n)
  65.                      eligible[altn++] = m;
  66.                  /* [once a regular candidate is found, the list
  67.                     is overwritten and `altn' becomes irrelevant] */
  68.              }
  69.          }
  70.      }
  71.  
  72.      /* resort to fallback list if main list was empty */
  73.      if (!n)
  74.          n = altn;
  75.  
  76.      if (n) {
  77.          /* found at least one candidate; pick one at random */
  78.          m = eligible[rn2(n)]; /* [0..n-1] */
  79.          a = &artilist[m];
  80.  
  81.          /* make an appropriate object if necessary, then christen it */
  82.          if (by_align)
  83.              otmp = mksobj((int) a->otyp, TRUE, FALSE);
  84.  
  85.          if (otmp) {
  86.              otmp = oname(otmp, a->name);
  87.              otmp->oartifact = m;
  88.              artiexist[m] = TRUE;
  89.          }
  90.      } else {
  91.          /* nothing appropriate could be found; return original object */
  92.          if (by_align)
  93.              otmp = 0; /* (there was no original object) */
  94.      }
  95.      return otmp;
  96.  }
  97.  

artifact_name

  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))
  17.          name += 4;
  18.  
  19.      for (a = artilist + 1; a->otyp; a++) {
  20.          aname = a->name;
  21.          if (!strncmpi(aname, "the ", 4))
  22.              aname += 4;
  23.          if (!strcmpi(name, aname)) {
  24.              *otyp = a->otyp;
  25.              return a->name;
  26.          }
  27.      }
  28.  
  29.      return (char *) 0;
  30.  }
  31.  

exist_artifact

  1.  boolean
  2.  exist_artifact(otyp, name)
  3.  int otyp;
  4.  const char *name;
  5.  {
  6.      register const struct artifact *a;
  7.      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.  }
  15.  

artifact_exists

  1.  void
  2.  artifact_exists(otmp, name, mod)
  3.  struct obj *otmp;
  4.  const char *name;
  5.  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 = (int) (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.  }
  22.  

nartifact_exist

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

spec_ability

  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) != 0L);
  9.  }
  10.  

confers_luck

  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)
  8.          return TRUE;
  9.  
  10.      return (boolean) (obj->oartifact && spec_ability(obj, SPFX_LUCK));
  11.  }
  12.  

arti_reflects

  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)
  14.              return TRUE;
  15.      }
  16.      return FALSE;
  17.  }
  18.  

shade_glare

  1.  /* decide whether this obj is effective when attacking against shades;
  2.     does not consider the bonus for blessed objects versus undead */
  3.  boolean
  4.  shade_glare(obj)
  5.  struct obj *obj;
  6.  {
  7.      const struct artifact *arti;
  8.  
  9.      /* any silver object is effective */
  10.      if (objects[obj->otyp].oc_material == SILVER)
  11.          return TRUE;
  12.      /* non-silver artifacts with bonus against undead also are effective */
  13.      arti = get_artifact(obj);
  14.      if (arti && (arti->spfx & SPFX_DFLAG2) && arti->mtype == M2_UNDEAD)
  15.          return TRUE;
  16.      /* [if there was anything with special bonus against noncorporeals,
  17.         it would be effective too] */
  18.      /* otherwise, harmless to shades */
  19.      return FALSE;
  20.  }
  21.  

restrict_name

  1.  /* returns 1 if name is restricted for otmp->otyp */
  2.  boolean
  3.  restrict_name(otmp, name)
  4.  struct obj *otmp;
  5.  const char *name;
  6.  {
  7.      register const struct artifact *a;
  8.      const char *aname, *odesc, *other;
  9.      boolean sametype[NUM_OBJECTS];
  10.      int i, lo, hi, otyp = otmp->otyp, ocls = objects[otyp].oc_class;
  11.  
  12.      if (!*name)
  13.          return FALSE;
  14.      if (!strncmpi(name, "the ", 4))
  15.          name += 4;
  16.  
  17.      /* decide what types of objects are the same as otyp;
  18.         if it's been discovered, then only itself matches;
  19.         otherwise, include all other undiscovered objects
  20.         of the same class which have the same description
  21.         or share the same pool of shuffled descriptions */
  22.      (void) memset((genericptr_t) sametype, 0, sizeof sametype); /* FALSE */
  23.      sametype[otyp] = TRUE;
  24.      if (!objects[otyp].oc_name_known
  25.          && (odesc = OBJ_DESCR(objects[otyp])) != 0) {
  26.          obj_shuffle_range(otyp, &lo, &hi);
  27.          for (i = bases[ocls]; i < NUM_OBJECTS; i++) {
  28.              if (objects[i].oc_class != ocls)
  29.                  break;
  30.              if (!objects[i].oc_name_known
  31.                  && (other = OBJ_DESCR(objects[i])) != 0
  32.                  && (!strcmp(odesc, other) || (i >= lo && i <= hi)))
  33.                  sametype[i] = TRUE;
  34.          }
  35.      }
  36.  
  37.      /* Since almost every artifact is SPFX_RESTR, it doesn't cost
  38.         us much to do the string comparison before the spfx check.
  39.         Bug fix:  don't name multiple elven daggers "Sting".
  40.       */
  41.      for (a = artilist + 1; a->otyp; a++) {
  42.          if (!sametype[a->otyp])
  43.              continue;
  44.          aname = a->name;
  45.          if (!strncmpi(aname, "the ", 4))
  46.              aname += 4;
  47.          if (!strcmp(aname, name))
  48.              return (boolean) ((a->spfx & (SPFX_NOGEN | SPFX_RESTR)) != 0
  49.                                || otmp->quan > 1L);
  50.      }
  51.  
  52.      return FALSE;
  53.  }
  54.  

attacks

  1.  STATIC_OVL boolean
  2.  attacks(adtyp, otmp)
  3.  int adtyp;
  4.  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.  }
  12.  

defends

  1.  boolean
  2.  defends(adtyp, otmp)
  3.  int adtyp;
  4.  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.  }
  12.  

defends_when_carried

  1.  /* used for monsters */
  2.  boolean
  3.  defends_when_carried(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.  }
  13.  

protects

  1.  /* determine whether an item confers Protection */
  2.  boolean
  3.  protects(otmp, being_worn)
  4.  struct obj *otmp;
  5.  boolean being_worn;
  6.  {
  7.      const struct artifact *arti;
  8.  
  9.      if (being_worn && objects[otmp->otyp].oc_oprop == PROTECTION)
  10.          return TRUE;
  11.      arti = get_artifact(otmp);
  12.      if (!arti)
  13.          return FALSE;
  14.      return (boolean) ((arti->cspfx & SPFX_PROTECT) != 0
  15.                        || (being_worn && (arti->spfx & SPFX_PROTECT) != 0));
  16.  }
  17.  

set_artifact_intrinsic

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

touch_artifacts

  1.  /* touch_artifact()'s return value isn't sufficient to tell whether it
  2.     dished out damage, and tracking changes to u.uhp, u.mh, Lifesaved
  3.     when trying to avoid second wounding is too cumbersome */
  4.  STATIC_VAR boolean touch_blasted; /* for retouch_object() */
  5.  
  6.  /*
  7.   * creature (usually hero) tries to touch (pick up or wield) an artifact obj.
  8.   * Returns 0 if the object refuses to be touched.
  9.   * This routine does not change any object chains.
  10.   * Ignores such things as gauntlets, assuming the artifact is not
  11.   * fooled by such trappings.
  12.   */
  13.  int
  14.  touch_artifact(obj, mon)
  15.  struct obj *obj;
  16.  struct monst *mon;
  17.  {
  18.      register const struct artifact *oart = get_artifact(obj);
  19.      boolean badclass, badalign, self_willed, yours;
  20.  
  21.      touch_blasted = FALSE;
  22.      if (!oart)
  23.          return 1;
  24.  
  25.      yours = (mon == &youmonst);
  26.      /* all quest artifacts are self-willed; it this ever changes, `badclass'
  27.         will have to be extended to explicitly include quest artifacts */
  28.      self_willed = ((oart->spfx & SPFX_INTEL) != 0);
  29.      if (yours) {
  30.          badclass = self_willed
  31.                     && ((oart->role != NON_PM && !Role_if(oart->role))
  32.                         || (oart->race != NON_PM && !Race_if(oart->race)));
  33.          badalign =
  34.              (oart->spfx & SPFX_RESTR) && oart->alignment != A_NONE
  35.              && (oart->alignment != u.ualign.type || u.ualign.record < 0);
  36.      } else if (!is_covetous(mon->data) && !is_mplayer(mon->data)) {
  37.          badclass = self_willed && oart->role != NON_PM
  38.                     && oart != &artilist[ART_EXCALIBUR];
  39.          badalign = (oart->spfx & SPFX_RESTR) && oart->alignment != A_NONE
  40.                     && (oart->alignment != mon_aligntyp(mon));
  41.      } else { /* an M3_WANTSxxx monster or a fake player */
  42.          /* special monsters trying to take the Amulet, invocation tools or
  43.             quest item can touch anything except `spec_applies' artifacts */
  44.          badclass = badalign = FALSE;
  45.      }
  46.      /* weapons which attack specific categories of monsters are
  47.         bad for them even if their alignments happen to match */
  48.      if (!badalign)
  49.          badalign = bane_applies(oart, mon);
  50.  
  51.      if (((badclass || badalign) && self_willed)
  52.          || (badalign && (!yours || !rn2(4)))) {
  53.          int dmg, tmp;
  54.          char buf[BUFSZ];
  55.  
  56.          if (!yours)
  57.              return 0;
  58.          You("are blasted by %s power!", s_suffix(the(xname(obj))));
  59.          touch_blasted = TRUE;
  60.          dmg = d((Antimagic ? 2 : 4), (self_willed ? 10 : 4));
  61.          /* add half (maybe quarter) of the usual silver damage bonus */
  62.          if (objects[obj->otyp].oc_material == SILVER && Hate_silver)
  63.              tmp = rnd(10), dmg += Maybe_Half_Phys(tmp);
  64.          Sprintf(buf, "touching %s", oart->name);
  65.          losehp(dmg, buf, KILLED_BY); /* magic damage, not physical */
  66.          exercise(A_WIS, FALSE);
  67.      }
  68.  
  69.      /* can pick it up unless you're totally non-synch'd with the artifact */
  70.      if (badclass && badalign && self_willed) {
  71.          if (yours) {
  72.              if (!carried(obj))
  73.                  pline("%s your grasp!", Tobjnam(obj, "evade"));
  74.              else
  75.                  pline("%s beyond your control!", Tobjnam(obj, "are"));
  76.          }
  77.          return 0;
  78.      }
  79.  
  80.      return 1;
  81.  }
  82.  

arti_immune

  1.  /* decide whether an artifact itself is vulnerable to a particular type
  2.     of erosion damage, independent of the properties of its bearer */
  3.  boolean
  4.  arti_immune(obj, dtyp)
  5.  struct obj *obj;
  6.  int dtyp;
  7.  {
  8.      register const struct artifact *weap = get_artifact(obj);
  9.  
  10.      if (!weap)
  11.          return FALSE;
  12.      if (dtyp == AD_PHYS)
  13.          return FALSE; /* nothing is immune to phys dmg */
  14.      return (boolean) (weap->attk.adtyp == dtyp
  15.                        || weap->defn.adtyp == dtyp
  16.                        || weap->cary.adtyp == dtyp);
  17.  }
  18.  

bane_applies

  1.  STATIC_OVL boolean
  2.  bane_applies(oart, mon)
  3.  const struct artifact *oart;
  4.  struct monst *mon;
  5.  {
  6.      struct artifact atmp;
  7.  
  8.      if (oart && (oart->spfx & SPFX_DBONUS) != 0) {
  9.          atmp = *oart;
  10.          atmp.spfx &= SPFX_DBONUS; /* clear other spfx fields */
  11.          if (spec_applies(&atmp, mon))
  12.              return TRUE;
  13.      }
  14.      return FALSE;
  15.  }
  16.  

spec_applies

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

spec_m2

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

spec_abon

  1.  /* special attack bonus */
  2.  int
  3.  spec_abon(otmp, mon)
  4.  struct obj *otmp;
  5.  struct monst *mon;
  6.  {
  7.      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.  }
  16.  

spec_dbon

  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 if (otmp->oartifact == ART_GRIMTOOTH)
  14.          /* Grimtooth has SPFX settings to warn against elves but we want its
  15.             damage bonus to apply to all targets, so bypass spec_applies() */
  16.          spec_dbon_applies = TRUE;
  17.      else
  18.          spec_dbon_applies = spec_applies(weap, mon);
  19.  
  20.      if (spec_dbon_applies)
  21.          return weap->attk.damd ? rnd((int) weap->attk.damd) : max(tmp, 1);
  22.      return 0;
  23.  }
  24.  

discover_artifact

  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.  }
  19.  

undiscovered_artifact

  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.  }
  17.  

disp_artifact_discoveries

  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)
  11.              break; /* empty slot implies end of list */
  12.          if (tmpwin == WIN_ERR)
  13.              continue; /* for WIN_ERR, we just count */
  14.  
  15.          if (i == 0)
  16.              putstr(tmpwin, iflags.menu_headings, "Artifacts");
  17.          m = artidisco[i];
  18.          otyp = artilist[m].otyp;
  19.          Sprintf(buf, "  %s [%s %s]", artiname(m),
  20.                  align_str(artilist[m].alignment), simple_typename(otyp));
  21.          putstr(tmpwin, 0, buf);
  22.      }
  23.      return i;
  24.  }
  25.  

Mb_hit

  1.  /*
  2.   * Magicbane's intrinsic magic is incompatible with normal
  3.   * enchantment magic.  Thus, its effects have a negative
  4.   * dependence on spe.  Against low mr victims, it typically
  5.   * does "double athame" damage, 2d4.  Occasionally, it will
  6.   * cast unbalancing magic which effectively averages out to
  7.   * 4d4 damage (3d4 against high mr victims), for spe = 0.
  8.   *
  9.   * Prior to 3.4.1, the cancel (aka purge) effect always
  10.   * included the scare effect too; now it's one or the other.
  11.   * Likewise, the stun effect won't be combined with either
  12.   * of those two; it will be chosen separately or possibly
  13.   * used as a fallback when scare or cancel fails.
  14.   *
  15.   * [Historical note: a change to artifact_hit() for 3.4.0
  16.   * unintentionally made all of Magicbane's special effects
  17.   * be blocked if the defender successfully saved against a
  18.   * stun attack.  As of 3.4.1, those effects can occur but
  19.   * will be slightly less likely than they were in 3.3.x.]
  20.   */
  21.  #define MB_MAX_DIEROLL 8 /* rolls above this aren't magical */
  22.  static const char *const mb_verb[2][4] = {
  23.      { "probe", "stun", "scare", "cancel" },
  24.      { "prod", "amaze", "tickle", "purge" },
  25.  };
  26.  #define MB_INDEX_PROBE 0
  27.  #define MB_INDEX_STUN 1
  28.  #define MB_INDEX_SCARE 2
  29.  #define MB_INDEX_CANCEL 3
  30.  
  31.  /* called when someone is being hit by Magicbane */
  32.  STATIC_OVL boolean
  33.  Mb_hit(magr, mdef, mb, dmgptr, dieroll, vis, hittee)
  34.  struct monst *magr, *mdef; /* attacker and defender */
  35.  struct obj *mb;            /* Magicbane */
  36.  int *dmgptr;               /* extra damage target will suffer */
  37.  int dieroll;               /* d20 that has already scored a hit */
  38.  boolean vis;               /* whether the action can be seen */
  39.  char *hittee;              /* target's name: "you" or mon_nam(mdef) */
  40.  {
  41.      struct permonst *old_uasmon;
  42.      const char *verb, *fakename;
  43.      boolean youattack = (magr == &youmonst), youdefend = (mdef == &youmonst),
  44.              resisted = FALSE, do_stun, do_confuse, result;
  45.      int attack_indx, scare_dieroll = MB_MAX_DIEROLL / 2;
  46.  
  47.      result = FALSE; /* no message given yet */
  48.      /* the most severe effects are less likely at higher enchantment */
  49.      if (mb->spe >= 3)
  50.          scare_dieroll /= (1 << (mb->spe / 3));
  51.      /* if target successfully resisted the artifact damage bonus,
  52.         reduce overall likelihood of the assorted special effects */
  53.      if (!spec_dbon_applies)
  54.          dieroll += 1;
  55.  
  56.      /* might stun even when attempting a more severe effect, but
  57.         in that case it will only happen if the other effect fails;
  58.         extra damage will apply regardless; 3.4.1: sometimes might
  59.         just probe even when it hasn't been enchanted */
  60.      do_stun = (max(mb->spe, 0) < rn2(spec_dbon_applies ? 11 : 7));
  61.  
  62.      /* the special effects also boost physical damage; increments are
  63.         generally cumulative, but since the stun effect is based on a
  64.         different criterium its damage might not be included; the base
  65.         damage is either 1d4 (athame) or 2d4 (athame+spec_dbon) depending
  66.         on target's resistance check against AD_STUN (handled by caller)
  67.         [note that a successful save against AD_STUN doesn't actually
  68.         prevent the target from ending up stunned] */
  69.      attack_indx = MB_INDEX_PROBE;
  70.      *dmgptr += rnd(4); /* (2..3)d4 */
  71.      if (do_stun) {
  72.          attack_indx = MB_INDEX_STUN;
  73.          *dmgptr += rnd(4); /* (3..4)d4 */
  74.      }
  75.      if (dieroll <= scare_dieroll) {
  76.          attack_indx = MB_INDEX_SCARE;
  77.          *dmgptr += rnd(4); /* (3..5)d4 */
  78.      }
  79.      if (dieroll <= (scare_dieroll / 2)) {
  80.          attack_indx = MB_INDEX_CANCEL;
  81.          *dmgptr += rnd(4); /* (4..6)d4 */
  82.      }
  83.  
  84.      /* give the hit message prior to inflicting the effects */
  85.      verb = mb_verb[!!Hallucination][attack_indx];
  86.      if (youattack || youdefend || vis) {
  87.          result = TRUE;
  88.          pline_The("magic-absorbing blade %s %s!",
  89.                    vtense((const char *) 0, verb), hittee);
  90.          /* assume probing has some sort of noticeable feedback
  91.             even if it is being done by one monster to another */
  92.          if (attack_indx == MB_INDEX_PROBE && !canspotmon(mdef))
  93.              map_invisible(mdef->mx, mdef->my);
  94.      }
  95.  
  96.      /* now perform special effects */
  97.      switch (attack_indx) {
  98.      case MB_INDEX_CANCEL:
  99.          old_uasmon = youmonst.data;
  100.          /* No mdef->mcan check: even a cancelled monster can be polymorphed
  101.           * into a golem, and the "cancel" effect acts as if some magical
  102.           * energy remains in spellcasting defenders to be absorbed later.
  103.           */
  104.          if (!cancel_monst(mdef, mb, youattack, FALSE, FALSE)) {
  105.              resisted = TRUE;
  106.          } else {
  107.              do_stun = FALSE;
  108.              if (youdefend) {
  109.                  if (youmonst.data != old_uasmon)
  110.                      *dmgptr = 0; /* rehumanized, so no more damage */
  111.                  if (u.uenmax > 0) {
  112.                      You("lose magical energy!");
  113.                      u.uenmax--;
  114.                      if (u.uen > 0)
  115.                          u.uen--;
  116.                      context.botl = 1;
  117.                  }
  118.              } else {
  119.                  if (mdef->data == &mons[PM_CLAY_GOLEM])
  120.                      mdef->mhp = 1; /* cancelled clay golems will die */
  121.                  if (youattack && attacktype(mdef->data, AT_MAGC)) {
  122.                      You("absorb magical energy!");
  123.                      u.uenmax++;
  124.                      u.uen++;
  125.                      context.botl = 1;
  126.                  }
  127.              }
  128.          }
  129.          break;
  130.  
  131.      case MB_INDEX_SCARE:
  132.          if (youdefend) {
  133.              if (Antimagic) {
  134.                  resisted = TRUE;
  135.              } else {
  136.                  nomul(-3);
  137.                  multi_reason = "being scared stiff";
  138.                  nomovemsg = "";
  139.                  if (magr && magr == u.ustuck && sticks(youmonst.data)) {
  140.                      u.ustuck = (struct monst *) 0;
  141.                      You("release %s!", mon_nam(magr));
  142.                  }
  143.              }
  144.          } else {
  145.              if (rn2(2) && resist(mdef, WEAPON_CLASS, 0, NOTELL))
  146.                  resisted = TRUE;
  147.              else
  148.                  monflee(mdef, 3, FALSE, (mdef->mhp > *dmgptr));
  149.          }
  150.          if (!resisted)
  151.              do_stun = FALSE;
  152.          break;
  153.  
  154.      case MB_INDEX_STUN:
  155.          do_stun = TRUE; /* (this is redundant...) */
  156.          break;
  157.  
  158.      case MB_INDEX_PROBE:
  159.          if (youattack && (mb->spe == 0 || !rn2(3 * abs(mb->spe)))) {
  160.              pline_The("%s is insightful.", verb);
  161.              /* pre-damage status */
  162.              probe_monster(mdef);
  163.          }
  164.          break;
  165.      }
  166.      /* stun if that was selected and a worse effect didn't occur */
  167.      if (do_stun) {
  168.          if (youdefend)
  169.              make_stunned(((HStun & TIMEOUT) + 3L), FALSE);
  170.          else
  171.              mdef->mstun = 1;
  172.          /* avoid extra stun message below if we used mb_verb["stun"] above */
  173.          if (attack_indx == MB_INDEX_STUN)
  174.              do_stun = FALSE;
  175.      }
  176.      /* lastly, all this magic can be confusing... */
  177.      do_confuse = !rn2(12);
  178.      if (do_confuse) {
  179.          if (youdefend)
  180.              make_confused((HConfusion & TIMEOUT) + 4L, FALSE);
  181.          else
  182.              mdef->mconf = 1;
  183.      }
  184.  
  185.      /* now give message(s) describing side-effects;
  186.         don't let vtense() be fooled by assigned name ending in 's' */
  187.      fakename = youdefend ? "you" : "mon";
  188.      if (youattack || youdefend || vis) {
  189.          (void) upstart(hittee); /* capitalize */
  190.          if (resisted) {
  191.              pline("%s %s!", hittee, vtense(fakename, "resist"));
  192.              shieldeff(youdefend ? u.ux : mdef->mx,
  193.                        youdefend ? u.uy : mdef->my);
  194.          }
  195.          if ((do_stun || do_confuse) && flags.verbose) {
  196.              char buf[BUFSZ];
  197.  
  198.              buf[0] = '\0';
  199.              if (do_stun)
  200.                  Strcat(buf, "stunned");
  201.              if (do_stun && do_confuse)
  202.                  Strcat(buf, " and ");
  203.              if (do_confuse)
  204.                  Strcat(buf, "confused");
  205.              pline("%s %s %s%c", hittee, vtense(fakename, "are"), buf,
  206.                    (do_stun && do_confuse) ? '!' : '.');
  207.          }
  208.      }
  209.  
  210.      return result;
  211.  }
  212.  

artifact_hit

  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
  48.                            ? "hits"
  49.                            : (mdef->data == &mons[PM_WATER_ELEMENTAL])
  50.                                  ? "vaporizes part of"
  51.                                  : "burns",
  52.                        hittee, !spec_dbon_applies ? '.' : '!');
  53.          if (!rn2(4))
  54.              (void) destroy_mitem(mdef, POTION_CLASS, AD_FIRE);
  55.          if (!rn2(4))
  56.              (void) destroy_mitem(mdef, SCROLL_CLASS, AD_FIRE);
  57.          if (!rn2(7))
  58.              (void) destroy_mitem(mdef, SPBOOK_CLASS, AD_FIRE);
  59.          if (youdefend && Slimed)
  60.              burn_away_slime();
  61.          return realizes_damage;
  62.      }
  63.      if (attacks(AD_COLD, otmp)) {
  64.          if (realizes_damage)
  65.              pline_The("ice-cold blade %s %s%c",
  66.                        !spec_dbon_applies ? "hits" : "freezes", hittee,
  67.                        !spec_dbon_applies ? '.' : '!');
  68.          if (!rn2(4))
  69.              (void) destroy_mitem(mdef, POTION_CLASS, AD_COLD);
  70.          return realizes_damage;
  71.      }
  72.      if (attacks(AD_ELEC, otmp)) {
  73.          if (realizes_damage)
  74.              pline_The("massive hammer hits%s %s%c",
  75.                        !spec_dbon_applies ? "" : "!  Lightning strikes",
  76.                        hittee, !spec_dbon_applies ? '.' : '!');
  77.          if (!rn2(5))
  78.              (void) destroy_mitem(mdef, RING_CLASS, AD_ELEC);
  79.          if (!rn2(5))
  80.              (void) destroy_mitem(mdef, WAND_CLASS, AD_ELEC);
  81.          return realizes_damage;
  82.      }
  83.      if (attacks(AD_MAGM, otmp)) {
  84.          if (realizes_damage)
  85.              pline_The("imaginary widget hits%s %s%c",
  86.                        !spec_dbon_applies
  87.                            ? ""
  88.                            : "!  A hail of magic missiles strikes",
  89.                        hittee, !spec_dbon_applies ? '.' : '!');
  90.          return realizes_damage;
  91.      }
  92.  
  93.      if (attacks(AD_STUN, otmp) && dieroll <= MB_MAX_DIEROLL) {
  94.          /* Magicbane's special attacks (possibly modifies hittee[]) */
  95.          return Mb_hit(magr, mdef, otmp, dmgptr, dieroll, vis, hittee);
  96.      }
  97.  
  98.      if (!spec_dbon_applies) {
  99.          /* since damage bonus didn't apply, nothing more to do;
  100.             no further attacks have side-effects on inventory */
  101.          return FALSE;
  102.      }
  103.  
  104.      /* We really want "on a natural 20" but Nethack does it in */
  105.      /* reverse from AD&D. */
  106.      if (spec_ability(otmp, SPFX_BEHEAD)) {
  107.          if (otmp->oartifact == ART_TSURUGI_OF_MURAMASA && dieroll == 1) {
  108.              wepdesc = "The razor-sharp blade";
  109.              /* not really beheading, but so close, why add another SPFX */
  110.              if (youattack && u.uswallow && mdef == u.ustuck) {
  111.                  You("slice %s wide open!", mon_nam(mdef));
  112.                  *dmgptr = 2 * mdef->mhp + FATAL_DAMAGE_MODIFIER;
  113.                  return TRUE;
  114.              }
  115.              if (!youdefend) {
  116.                  /* allow normal cutworm() call to add extra damage */
  117.                  if (notonhead)
  118.                      return FALSE;
  119.  
  120.                  if (bigmonst(mdef->data)) {
  121.                      if (youattack)
  122.                          You("slice deeply into %s!", mon_nam(mdef));
  123.                      else if (vis)
  124.                          pline("%s cuts deeply into %s!", Monnam(magr),
  125.                                hittee);
  126.                      *dmgptr *= 2;
  127.                      return TRUE;
  128.                  }
  129.                  *dmgptr = 2 * mdef->mhp + FATAL_DAMAGE_MODIFIER;
  130.                  pline("%s cuts %s in half!", wepdesc, mon_nam(mdef));
  131.                  otmp->dknown = TRUE;
  132.                  return TRUE;
  133.              } else {
  134.                  if (bigmonst(youmonst.data)) {
  135.                      pline("%s cuts deeply into you!",
  136.                            magr ? Monnam(magr) : wepdesc);
  137.                      *dmgptr *= 2;
  138.                      return TRUE;
  139.                  }
  140.  
  141.                  /* Players with negative AC's take less damage instead
  142.                   * of just not getting hit.  We must add a large enough
  143.                   * value to the damage so that this reduction in
  144.                   * damage does not prevent death.
  145.                   */
  146.                  *dmgptr = 2 * (Upolyd ? u.mh : u.uhp) + FATAL_DAMAGE_MODIFIER;
  147.                  pline("%s cuts you in half!", wepdesc);
  148.                  otmp->dknown = TRUE;
  149.                  return TRUE;
  150.              }
  151.          } else if (otmp->oartifact == ART_VORPAL_BLADE
  152.                     && (dieroll == 1 || mdef->data == &mons[PM_JABBERWOCK])) {
  153.              static const char *const behead_msg[2] = { "%s beheads %s!",
  154.                                                         "%s decapitates %s!" };
  155.  
  156.              if (youattack && u.uswallow && mdef == u.ustuck)
  157.                  return FALSE;
  158.              wepdesc = artilist[ART_VORPAL_BLADE].name;
  159.              if (!youdefend) {
  160.                  if (!has_head(mdef->data) || notonhead || u.uswallow) {
  161.                      if (youattack)
  162.                          pline("Somehow, you miss %s wildly.", mon_nam(mdef));
  163.                      else if (vis)
  164.                          pline("Somehow, %s misses wildly.", mon_nam(magr));
  165.                      *dmgptr = 0;
  166.                      return (boolean) (youattack || vis);
  167.                  }
  168.                  if (noncorporeal(mdef->data) || amorphous(mdef->data)) {
  169.                      pline("%s slices through %s %s.", wepdesc,
  170.                            s_suffix(mon_nam(mdef)), mbodypart(mdef, NECK));
  171.                      return TRUE;
  172.                  }
  173.                  *dmgptr = 2 * mdef->mhp + FATAL_DAMAGE_MODIFIER;
  174.                  pline(behead_msg[rn2(SIZE(behead_msg))], wepdesc,
  175.                        mon_nam(mdef));
  176.                  if (Hallucination && !flags.female)
  177.                      pline("Good job Henry, but that wasn't Anne.");
  178.                  otmp->dknown = TRUE;
  179.                  return TRUE;
  180.              } else {
  181.                  if (!has_head(youmonst.data)) {
  182.                      pline("Somehow, %s misses you wildly.",
  183.                            magr ? mon_nam(magr) : wepdesc);
  184.                      *dmgptr = 0;
  185.                      return TRUE;
  186.                  }
  187.                  if (noncorporeal(youmonst.data) || amorphous(youmonst.data)) {
  188.                      pline("%s slices through your %s.", wepdesc,
  189.                            body_part(NECK));
  190.                      return TRUE;
  191.                  }
  192.                  *dmgptr = 2 * (Upolyd ? u.mh : u.uhp) + FATAL_DAMAGE_MODIFIER;
  193.                  pline(behead_msg[rn2(SIZE(behead_msg))], wepdesc, "you");
  194.                  otmp->dknown = TRUE;
  195.                  /* Should amulets fall off? */
  196.                  return TRUE;
  197.              }
  198.          }
  199.      }
  200.      if (spec_ability(otmp, SPFX_DRLI)) {
  201.          /* some non-living creatures (golems, vortices) are
  202.             vulnerable to life drain effects */
  203.          const char *life = nonliving(mdef->data) ? "animating force" : "life";
  204.  
  205.          if (!youdefend) {
  206.              if (vis) {
  207.                  if (otmp->oartifact == ART_STORMBRINGER)
  208.                      pline_The("%s blade draws the %s from %s!",
  209.                                hcolor(NH_BLACK), life, mon_nam(mdef));
  210.                  else
  211.                      pline("%s draws the %s from %s!",
  212.                            The(distant_name(otmp, xname)), life,
  213.                            mon_nam(mdef));
  214.              }
  215.              if (mdef->m_lev == 0) {
  216.                  *dmgptr = 2 * mdef->mhp + FATAL_DAMAGE_MODIFIER;
  217.              } else {
  218.                  int drain = monhp_per_lvl(mdef);
  219.  
  220.                  *dmgptr += drain;
  221.                  mdef->mhpmax -= drain;
  222.                  mdef->m_lev--;
  223.                  drain /= 2;
  224.                  if (drain)
  225.                      healup(drain, 0, FALSE, FALSE);
  226.              }
  227.              return vis;
  228.          } else { /* youdefend */
  229.              int oldhpmax = u.uhpmax;
  230.  
  231.              if (Blind)
  232.                  You_feel("an %s drain your %s!",
  233.                           (otmp->oartifact == ART_STORMBRINGER)
  234.                              ? "unholy blade"
  235.                              : "object",
  236.                           life);
  237.              else if (otmp->oartifact == ART_STORMBRINGER)
  238.                  pline_The("%s blade drains your %s!", hcolor(NH_BLACK), life);
  239.              else
  240.                  pline("%s drains your %s!", The(distant_name(otmp, xname)),
  241.                        life);
  242.              losexp("life drainage");
  243.              if (magr && magr->mhp < magr->mhpmax) {
  244.                  magr->mhp += (oldhpmax - u.uhpmax) / 2;
  245.                  if (magr->mhp > magr->mhpmax)
  246.                      magr->mhp = magr->mhpmax;
  247.              }
  248.              return TRUE;
  249.          }
  250.      }
  251.      return FALSE;
  252.  }
  253.  

doinvoke

  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 */
  4.  
  5.  /* the #invoke command */
  6.  int
  7.  doinvoke()
  8.  {
  9.      struct obj *obj;
  10.  
  11.      obj = getobj(invoke_types, "invoke");
  12.      if (!obj)
  13.          return 0;
  14.      if (!retouch_object(&obj, FALSE))
  15.          return 1;
  16.      return arti_invoke(obj);
  17.  }
  18.  

arti_invoke

  1.  STATIC_OVL int
  2.  arti_invoke(obj)
  3.  struct obj *obj;
  4.  {
  5.      register const struct artifact *oart = get_artifact(obj);
  6.      if (!obj) {
  7.          impossible("arti_invoke without obj");
  8.          return 0;
  9.      }
  10.      if (!oart || !oart->inv_prop) {
  11.          if (obj->otyp == CRYSTAL_BALL)
  12.              use_crystal_ball(&obj);
  13.          else
  14.              pline1(nothing_happens);
  15.          return 1;
  16.      }
  17.  
  18.      if (oart->inv_prop > LAST_PROP) {
  19.          /* It's a special power, not "just" a property */
  20.          if (obj->age > monstermoves) {
  21.              /* the artifact is tired :-) */
  22.              You_feel("that %s %s ignoring you.", the(xname(obj)),
  23.                       otense(obj, "are"));
  24.              /* and just got more so; patience is essential... */
  25.              obj->age += (long) d(3, 10);
  26.              return 1;
  27.          }
  28.          obj->age = monstermoves + rnz(100);
  29.  
  30.          switch (oart->inv_prop) {
  31.          case TAMING: {
  32.              struct obj pseudo;
  33.  
  34.              pseudo =
  35.                  zeroobj; /* neither cursed nor blessed, zero oextra too */
  36.              pseudo.otyp = SCR_TAMING;
  37.              (void) seffects(&pseudo);
  38.              break;
  39.          }
  40.          case HEALING: {
  41.              int healamt = (u.uhpmax + 1 - u.uhp) / 2;
  42.              long creamed = (long) u.ucreamed;
  43.  
  44.              if (Upolyd)
  45.                  healamt = (u.mhmax + 1 - u.mh) / 2;
  46.              if (healamt || Sick || Slimed || Blinded > creamed)
  47.                  You_feel("better.");
  48.              else
  49.                  goto nothing_special;
  50.              if (healamt > 0) {
  51.                  if (Upolyd)
  52.                      u.mh += healamt;
  53.                  else
  54.                      u.uhp += healamt;
  55.              }
  56.              if (Sick)
  57.                  make_sick(0L, (char *) 0, FALSE, SICK_ALL);
  58.              if (Slimed)
  59.                  make_slimed(0L, (char *) 0);
  60.              if (Blinded > creamed)
  61.                  make_blinded(creamed, FALSE);
  62.              context.botl = 1;
  63.              break;
  64.          }
  65.          case ENERGY_BOOST: {
  66.              int epboost = (u.uenmax + 1 - u.uen) / 2;
  67.              if (epboost > 120)
  68.                  epboost = 120; /* arbitrary */
  69.              else if (epboost < 12)
  70.                  epboost = u.uenmax - u.uen;
  71.              if (epboost) {
  72.                  You_feel("re-energized.");
  73.                  u.uen += epboost;
  74.                  context.botl = 1;
  75.              } else
  76.                  goto nothing_special;
  77.              break;
  78.          }
  79.          case UNTRAP: {
  80.              if (!untrap(TRUE)) {
  81.                  obj->age = 0; /* don't charge for changing their mind */
  82.                  return 0;
  83.              }
  84.              break;
  85.          }
  86.          case CHARGE_OBJ: {
  87.              struct obj *otmp = getobj(recharge_type, "charge");
  88.              boolean b_effect;
  89.  
  90.              if (!otmp) {
  91.                  obj->age = 0;
  92.                  return 0;
  93.              }
  94.              b_effect =
  95.                  obj->blessed && (Role_switch == oart->role || !oart->role);
  96.              recharge(otmp, b_effect ? 1 : obj->cursed ? -1 : 0);
  97.              update_inventory();
  98.              break;
  99.          }
  100.          case LEV_TELE:
  101.              level_tele();
  102.              break;
  103.          case CREATE_PORTAL: {
  104.              int i, num_ok_dungeons, last_ok_dungeon = 0;
  105.              d_level newlev;
  106.              extern int n_dgns; /* from dungeon.c */
  107.              winid tmpwin = create_nhwindow(NHW_MENU);
  108.              anything any;
  109.  
  110.              any = zeroany; /* set all bits to zero */
  111.              start_menu(tmpwin);
  112.              /* use index+1 (cant use 0) as identifier */
  113.              for (i = num_ok_dungeons = 0; i < n_dgns; i++) {
  114.                  if (!dungeons[i].dunlev_ureached)
  115.                      continue;
  116.                  any.a_int = i + 1;
  117.                  add_menu(tmpwin, NO_GLYPH, &any, 0, 0, ATR_NONE,
  118.                           dungeons[i].dname, MENU_UNSELECTED);
  119.                  num_ok_dungeons++;
  120.                  last_ok_dungeon = i;
  121.              }
  122.              end_menu(tmpwin, "Open a portal to which dungeon?");
  123.              if (num_ok_dungeons > 1) {
  124.                  /* more than one entry; display menu for choices */
  125.                  menu_item *selected;
  126.                  int n;
  127.  
  128.                  n = select_menu(tmpwin, PICK_ONE, &selected);
  129.                  if (n <= 0) {
  130.                      destroy_nhwindow(tmpwin);
  131.                      goto nothing_special;
  132.                  }
  133.                  i = selected[0].item.a_int - 1;
  134.                  free((genericptr_t) selected);
  135.              } else
  136.                  i = last_ok_dungeon; /* also first & only OK dungeon */
  137.              destroy_nhwindow(tmpwin);
  138.  
  139.              /*
  140.               * i is now index into dungeon structure for the new dungeon.
  141.               * Find the closest level in the given dungeon, open
  142.               * a use-once portal to that dungeon and go there.
  143.               * The closest level is either the entry or dunlev_ureached.
  144.               */
  145.              newlev.dnum = i;
  146.              if (dungeons[i].depth_start >= depth(&u.uz))
  147.                  newlev.dlevel = dungeons[i].entry_lev;
  148.              else
  149.                  newlev.dlevel = dungeons[i].dunlev_ureached;
  150.  
  151.              if (u.uhave.amulet || In_endgame(&u.uz) || In_endgame(&newlev)
  152.                  || newlev.dnum == u.uz.dnum || !next_to_u()) {
  153.                  You_feel("very disoriented for a moment.");
  154.              } else {
  155.                  if (!Blind)
  156.                      You("are surrounded by a shimmering sphere!");
  157.                  else
  158.                      You_feel("weightless for a moment.");
  159.                  goto_level(&newlev, FALSE, FALSE, FALSE);
  160.              }
  161.              break;
  162.          }
  163.          case ENLIGHTENING:
  164.              enlightenment(MAGICENLIGHTENMENT, ENL_GAMEINPROGRESS);
  165.              break;
  166.          case CREATE_AMMO: {
  167.              struct obj *otmp = mksobj(ARROW, TRUE, FALSE);
  168.  
  169.              if (!otmp)
  170.                  goto nothing_special;
  171.              otmp->blessed = obj->blessed;
  172.              otmp->cursed = obj->cursed;
  173.              otmp->bknown = obj->bknown;
  174.              if (obj->blessed) {
  175.                  if (otmp->spe < 0)
  176.                      otmp->spe = 0;
  177.                  otmp->quan += rnd(10);
  178.              } else if (obj->cursed) {
  179.                  if (otmp->spe > 0)
  180.                      otmp->spe = 0;
  181.              } else
  182.                  otmp->quan += rnd(5);
  183.              otmp->owt = weight(otmp);
  184.              otmp =
  185.                  hold_another_object(otmp, "Suddenly %s out.",
  186.                                      aobjnam(otmp, "fall"), (const char *) 0);
  187.              break;
  188.          }
  189.          }
  190.      } else {
  191.          long eprop = (u.uprops[oart->inv_prop].extrinsic ^= W_ARTI),
  192.               iprop = u.uprops[oart->inv_prop].intrinsic;
  193.          boolean on = (eprop & W_ARTI) != 0; /* true if prop just set */
  194.  
  195.          if (on && obj->age > monstermoves) {
  196.              /* the artifact is tired :-) */
  197.              u.uprops[oart->inv_prop].extrinsic ^= W_ARTI;
  198.              You_feel("that %s %s ignoring you.", the(xname(obj)),
  199.                       otense(obj, "are"));
  200.              /* can't just keep repeatedly trying */
  201.              obj->age += (long) d(3, 10);
  202.              return 1;
  203.          } else if (!on) {
  204.              /* when turning off property, determine downtime */
  205.              /* arbitrary for now until we can tune this -dlc */
  206.              obj->age = monstermoves + rnz(100);
  207.          }
  208.  
  209.          if ((eprop & ~W_ARTI) || iprop) {
  210.          nothing_special:
  211.              /* you had the property from some other source too */
  212.              if (carried(obj))
  213.                  You_feel("a surge of power, but nothing seems to happen.");
  214.              return 1;
  215.          }
  216.          switch (oart->inv_prop) {
  217.          case CONFLICT:
  218.              if (on)
  219.                  You_feel("like a rabble-rouser.");
  220.              else
  221.                  You_feel("the tension decrease around you.");
  222.              break;
  223.          case LEVITATION:
  224.              if (on) {
  225.                  float_up();
  226.                  spoteffects(FALSE);
  227.              } else
  228.                  (void) float_down(I_SPECIAL | TIMEOUT, W_ARTI);
  229.              break;
  230.          case INVIS:
  231.              if (BInvis || Blind)
  232.                  goto nothing_special;
  233.              newsym(u.ux, u.uy);
  234.              if (on)
  235.                  Your("body takes on a %s transparency...",
  236.                       Hallucination ? "normal" : "strange");
  237.              else
  238.                  Your("body seems to unfade...");
  239.              break;
  240.          }
  241.      }
  242.  
  243.      return 1;
  244.  }
  245.  

finesse_ahriman

  1.  /* will freeing this object from inventory cause levitation to end? */
  2.  boolean
  3.  finesse_ahriman(obj)
  4.  struct obj *obj;
  5.  {
  6.      const struct artifact *oart;
  7.      struct prop save_Lev;
  8.      boolean result;
  9.  
  10.      /* if we aren't levitating or this isn't an artifact which confers
  11.         levitation via #invoke then freeinv() won't toggle levitation */
  12.      if (!Levitation || (oart = get_artifact(obj)) == 0
  13.          || oart->inv_prop != LEVITATION || !(ELevitation & W_ARTI))
  14.          return FALSE;
  15.  
  16.      /* arti_invoke(off) -> float_down() clears I_SPECIAL|TIMEOUT & W_ARTI;
  17.         probe ahead to see whether that actually results in floating down;
  18.         (this assumes that there aren't two simultaneously invoked artifacts
  19.         both conferring levitation--safe, since if there were two of them,
  20.         invoking the 2nd would negate the 1st rather than stack with it) */
  21.      save_Lev = u.uprops[LEVITATION];
  22.      HLevitation &= ~(I_SPECIAL | TIMEOUT);
  23.      ELevitation &= ~W_ARTI;
  24.      result = (boolean) !Levitation;
  25.      u.uprops[LEVITATION] = save_Lev;
  26.      return result;
  27.  }
  28.  

artifact_light

  1.  /* WAC return TRUE if artifact is always lit */
  2.  boolean
  3.  artifact_light(obj)
  4.  struct obj *obj;
  5.  {
  6.      return (boolean) (get_artifact(obj) && obj->oartifact == ART_SUNSWORD);
  7.  }
  8.  

arti_speak

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

artifact_has_invprop

  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.  }
  10.  

arti_cost

  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.  

abil_to_adtyp

  1.  STATIC_OVL uchar
  2.  abil_to_adtyp(abil)
  3.  long *abil;
  4.  {
  5.      struct abil2adtyp_tag {
  6.          long *abil;
  7.          uchar adtyp;
  8.      } abil2adtyp[] = {
  9.          { &EFire_resistance, AD_FIRE },
  10.          { &ECold_resistance, AD_COLD },
  11.          { &EShock_resistance, AD_ELEC },
  12.          { &EAntimagic, AD_MAGM },
  13.          { &EDisint_resistance, AD_DISN },
  14.          { &EPoison_resistance, AD_DRST },
  15.      };
  16.      int k;
  17.  
  18.      for (k = 0; k < SIZE(abil2adtyp); k++) {
  19.          if (abil2adtyp[k].abil == abil)
  20.              return abil2adtyp[k].adtyp;
  21.      }
  22.      return 0;
  23.  }
  24.  

abil_to_spfx

  1.  STATIC_OVL unsigned long
  2.  abil_to_spfx(abil)
  3.  long *abil;
  4.  {
  5.      static const struct abil2spfx_tag {
  6.          long *abil;
  7.          unsigned long spfx;
  8.      } abil2spfx[] = {
  9.          { &ESearching, SPFX_SEARCH },
  10.          { &EHalluc_resistance, SPFX_HALRES },
  11.          { &ETelepat, SPFX_ESP },
  12.          { &EStealth, SPFX_STLTH },
  13.          { &ERegeneration, SPFX_REGEN },
  14.          { &ETeleport_control, SPFX_TCTRL },
  15.          { &EWarn_of_mon, SPFX_WARN },
  16.          { &EWarning, SPFX_WARN },
  17.          { &EEnergy_regeneration, SPFX_EREGEN },
  18.          { &EHalf_spell_damage, SPFX_HSPDAM },
  19.          { &EHalf_physical_damage, SPFX_HPHDAM },
  20.          { &EReflecting, SPFX_REFLECT },
  21.      };
  22.      int k;
  23.  
  24.      for (k = 0; k < SIZE(abil2spfx); k++) {
  25.          if (abil2spfx[k].abil == abil)
  26.              return abil2spfx[k].spfx;
  27.      }
  28.      return 0L;
  29.  }
  30.  

what_gives

  1.  /*
  2.   * Return the first item that is conveying a particular intrinsic.
  3.   */
  4.  struct obj *
  5.  what_gives(abil)
  6.  long *abil;
  7.  {
  8.      struct obj *obj;
  9.      uchar dtyp;
  10.      unsigned long spfx;
  11.      long wornbits;
  12.      long wornmask = (W_ARM | W_ARMC | W_ARMH | W_ARMS
  13.                       | W_ARMG | W_ARMF | W_ARMU
  14.                       | W_AMUL | W_RINGL | W_RINGR | W_TOOL
  15.                       /* [do W_ART and W_ARTI actually belong here?] */
  16.                       | W_ART | W_ARTI);
  17.  
  18.      if (u.twoweap)
  19.          wornmask |= W_SWAPWEP;
  20.      dtyp = abil_to_adtyp(abil);
  21.      spfx = abil_to_spfx(abil);
  22.      wornbits = (wornmask & *abil);
  23.  
  24.      for (obj = invent; obj; obj = obj->nobj) {
  25.          if (obj->oartifact
  26.              && (abil != &EWarn_of_mon || context.warntype.obj)) {
  27.              const struct artifact *art = get_artifact(obj);
  28.  
  29.              if (art) {
  30.                  if (dtyp) {
  31.                      if (art->cary.adtyp == dtyp || art->defn.adtyp == dtyp)
  32.                          return obj;
  33.                  }
  34.                  if (spfx) {
  35.                      /* property conferred when carried */
  36.                      if ((art->cspfx & spfx) == spfx)
  37.                          return obj;
  38.                      /* property conferred when wielded or worn */
  39.                      if ((art->spfx & spfx) == spfx && obj->owornmask)
  40.                          return obj;
  41.                  }
  42.              }
  43.          } else {
  44.              if (wornbits && wornbits == (wornmask & obj->owornmask))
  45.                  return obj;
  46.          }
  47.      }
  48.      return (struct obj *) 0;
  49.  }
  50.  

glow_color

  1.  const char *
  2.  glow_color(arti_indx)
  3.  int arti_indx;
  4.  {
  5.      int colornum = artilist[arti_indx].acolor;
  6.      const char *colorstr = clr2colorname(colornum);
  7.  
  8.      return hcolor(colorstr);
  9.  }
  10.  

Sting_effects

  1.  /* use for warning "glow" for Sting, Orcrist, and Grimtooth */
  2.  void
  3.  Sting_effects(orc_count)
  4.  int orc_count; /* new count (warn_obj_cnt is old count); -1 is a flag value */
  5.  {
  6.      if (uwep
  7.          && (uwep->oartifact == ART_STING
  8.              || uwep->oartifact == ART_ORCRIST
  9.              || uwep->oartifact == ART_GRIMTOOTH)) {
  10.          if (orc_count == -1 && warn_obj_cnt > 0) {
  11.              /* -1 means that blindness has just been toggled; give a
  12.                 'continue' message that eventual 'stop' message will match */
  13.              pline("%s is %s.", bare_artifactname(uwep),
  14.                    !Blind ? "glowing" : "quivering");
  15.          } else if (orc_count > 0 && warn_obj_cnt == 0) {
  16.              /* 'start' message */
  17.              if (!Blind)
  18.                  pline("%s %s %s!", bare_artifactname(uwep),
  19.                        otense(uwep, "glow"), glow_color(uwep->oartifact));
  20.              else
  21.                  pline("%s quivers slightly.", bare_artifactname(uwep));
  22.          } else if (orc_count == 0 && warn_obj_cnt > 0) {
  23.              /* 'stop' message */
  24.              pline("%s stops %s.", bare_artifactname(uwep),
  25.                    !Blind ? "glowing" : "quivering");
  26.          }
  27.      }
  28.  }
  29.  

retouch_object

  1.  /* called when hero is wielding/applying/invoking a carried item, or
  2.     after undergoing a transformation (alignment change, lycanthropy,
  3.     polymorph) which might affect item access */
  4.  int
  5.  retouch_object(objp, loseit)
  6.  struct obj **objp; /* might be destroyed or unintentionally dropped */
  7.  boolean loseit;    /* whether to drop it if hero can longer touch it */
  8.  {
  9.      struct obj *obj = *objp;
  10.  
  11.      if (touch_artifact(obj, &youmonst)) {
  12.          char buf[BUFSZ];
  13.          int dmg = 0, tmp;
  14.          boolean ag =
  15.                      (objects[obj->otyp].oc_material == SILVER && Hate_silver),
  16.                  bane = bane_applies(get_artifact(obj), &youmonst);
  17.  
  18.          /* nothing else to do if hero can successfully handle this object */
  19.          if (!ag && !bane)
  20.              return 1;
  21.  
  22.          /* hero can't handle this object, but didn't get touch_artifact()'s
  23.             "<obj> evades your grasp|control" message; give an alternate one */
  24.          You_cant("handle %s%s!", yname(obj),
  25.                   obj->owornmask ? " anymore" : "");
  26.          /* also inflict damage unless touch_artifact() already did so */
  27.          if (!touch_blasted) {
  28.              /* damage is somewhat arbitrary; half the usual 1d20 physical
  29.                 for silver, 1d10 magical for <foo>bane, potentially both */
  30.              if (ag)
  31.                  tmp = rnd(10), dmg += Maybe_Half_Phys(tmp);
  32.              if (bane)
  33.                  dmg += rnd(10);
  34.              Sprintf(buf, "handling %s", killer_xname(obj));
  35.              losehp(dmg, buf, KILLED_BY);
  36.              exercise(A_CON, FALSE);
  37.          }
  38.      }
  39.  
  40.      /* removing a worn item might result in loss of levitation,
  41.         dropping the hero onto a polymorph trap or into water or
  42.         lava and potentially dropping or destroying the item */
  43.      if (obj->owornmask) {
  44.          struct obj *otmp;
  45.  
  46.          remove_worn_item(obj, FALSE);
  47.          for (otmp = invent; otmp; otmp = otmp->nobj)
  48.              if (otmp == obj)
  49.                  break;
  50.          if (!otmp)
  51.              *objp = obj = 0;
  52.      }
  53.  
  54.      /* if we still have it and caller wants us to drop it, do so now */
  55.      if (loseit && obj) {
  56.          if (Levitation) {
  57.              freeinv(obj);
  58.              hitfloor(obj);
  59.          } else {
  60.              /* dropx gives a message iff item lands on an altar */
  61.              if (!IS_ALTAR(levl[u.ux][u.uy].typ))
  62.                  pline("%s to the %s.", Tobjnam(obj, "fall"),
  63.                        surface(u.ux, u.uy));
  64.              dropx(obj);
  65.          }
  66.          *objp = obj = 0; /* no longer in inventory */
  67.      }
  68.      return 0;
  69.  }
  70.  

untouchable

  1.  /* an item which is worn/wielded or an artifact which conveys
  2.     something via being carried or which has an #invoke effect
  3.     currently in operation undergoes a touch test; if it fails,
  4.     it will be unworn/unwielded and revoked but not dropped */
  5.  STATIC_OVL boolean
  6.  untouchable(obj, drop_untouchable)
  7.  struct obj *obj;
  8.  boolean drop_untouchable;
  9.  {
  10.      struct artifact *art;
  11.      boolean beingworn, carryeffect, invoked;
  12.      long wearmask = ~(W_QUIVER | (u.twoweap ? 0L : W_SWAPWEP) | W_BALL);
  13.  
  14.      beingworn = ((obj->owornmask & wearmask) != 0L
  15.                   /* some items in use don't have any wornmask setting */
  16.                   || (obj->oclass == TOOL_CLASS
  17.                       && (obj->lamplit || (obj->otyp == LEASH && obj->leashmon)
  18.                           || (Is_container(obj) && Has_contents(obj)))));
  19.  
  20.      if ((art = get_artifact(obj)) != 0) {
  21.          carryeffect = (art->cary.adtyp || art->cspfx);
  22.          invoked = (art->inv_prop > 0 && art->inv_prop <= LAST_PROP
  23.                     && (u.uprops[art->inv_prop].extrinsic & W_ARTI) != 0L);
  24.      } else {
  25.          carryeffect = invoked = FALSE;
  26.      }
  27.  
  28.      if (beingworn || carryeffect || invoked) {
  29.          if (!retouch_object(&obj, drop_untouchable)) {
  30.              /* "<artifact> is beyond your control" or "you can't handle
  31.                 <object>" has been given and it is now unworn/unwielded
  32.                 and possibly dropped (depending upon caller); if dropped,
  33.                 carried effect was turned off, else we leave that alone;
  34.                 we turn off invocation property here if still carried */
  35.              if (invoked && obj)
  36.                  arti_invoke(obj); /* reverse #invoke */
  37.              return TRUE;
  38.          }
  39.      }
  40.      return FALSE;
  41.  }
  42.  
  43.  /* check all items currently in use (mostly worn) for touchability */

retouch_equipment

  1.  void
  2.  retouch_equipment(dropflag)
  3.  int dropflag; /* 0==don't drop, 1==drop all, 2==drop weapon */
  4.  {
  5.      static int nesting = 0; /* recursion control */
  6.      struct obj *obj;
  7.      boolean dropit, had_gloves = (uarmg != 0);
  8.      int had_rings = (!!uleft + !!uright);
  9.  
  10.      /*
  11.       * We can potentially be called recursively if losing/unwearing
  12.       * an item causes worn helm of opposite alignment to come off or
  13.       * be destroyed.
  14.       *
  15.       * BUG: if the initial call was due to putting on a helm of
  16.       * opposite alignment and it does come off to trigger recursion,
  17.       * after the inner call executes, the outer call will finish
  18.       * using the non-helm alignment rather than the helm alignment
  19.       * which triggered this in the first place.
  20.       */
  21.      if (!nesting++)
  22.          clear_bypasses(); /* init upon initial entry */
  23.  
  24.      dropit = (dropflag > 0); /* drop all or drop weapon */
  25.      /* check secondary weapon first, before possibly unwielding primary */
  26.      if (u.twoweap)
  27.          (void) untouchable(uswapwep, dropit);
  28.      /* check primary weapon next so that they're handled together */
  29.      if (uwep)
  30.          (void) untouchable(uwep, dropit);
  31.  
  32.      /* in case someone is daft enough to add artifact or silver saddle */
  33.      if (u.usteed && (obj = which_armor(u.usteed, W_SADDLE)) != 0) {
  34.          /* untouchable() calls retouch_object() which expects an object in
  35.             hero's inventory, but remove_worn_item() will be harmless for
  36.             saddle and we're suppressing drop, so this works as intended */
  37.          if (untouchable(obj, FALSE))
  38.              dismount_steed(DISMOUNT_THROWN);
  39.      }
  40.      /*
  41.       * TODO?  Force off gloves if either or both rings are going to
  42.       * become unworn; force off cloak [suit] before suit [shirt].
  43.       * The torso handling is hypothetical; the case for gloves is
  44.       * not, due to the possibility of unwearing silver rings.
  45.       */
  46.  
  47.      dropit = (dropflag == 1); /* all untouchable items */
  48.      /* loss of levitation (silver ring, or Heart of Ahriman invocation)
  49.         might cause hero to lose inventory items (by dropping into lava,
  50.         for instance), so inventory traversal needs to rescan the whole
  51.         invent chain each time it moves on to another object; we use bypass
  52.         handling to keep track of which items have already been processed */
  53.      while ((obj = nxt_unbypassed_obj(invent)) != 0)
  54.          (void) untouchable(obj, dropit);
  55.  
  56.      if (had_rings != (!!uleft + !!uright) && uarmg && uarmg->cursed)
  57.          uncurse(uarmg); /* temporary? hack for ring removal plausibility */
  58.      if (had_gloves && !uarmg)
  59.          selftouch("After losing your gloves, you");
  60.  
  61.      if (!--nesting)
  62.          clear_bypasses(); /* reset upon final exit */
  63.  }
  64.  
  65.  /*artifact.c*/