Source:NetHack 3.6.0/src/worn.c

From NetHackWiki
(Redirected from Source:NetHack 3.6.0/worn.c)
Jump to: navigation, search

Below is the full text to worn.c from the source code of NetHack 3.6.0. To link to a particular line, write [[Source:NetHack 3.6.0/src/worn.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	worn.c	$NHDT-Date: 1446887541 2015/11/07 09:12:21 $  $NHDT-Branch: master $:$NHDT-Revision: 1.47 $ */
  2.  /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
  3.  /* NetHack may be freely redistributed.  See license for details. */
  4.  
  5.  #include "hack.h"
  6.  
  7.  STATIC_DCL void FDECL(m_lose_armor, (struct monst *, struct obj *));
  8.  STATIC_DCL void FDECL(m_dowear_type,
  9.                        (struct monst *, long, BOOLEAN_P, BOOLEAN_P));
  10.  STATIC_DCL int FDECL(extra_pref, (struct monst *, struct obj *));
  11.  
  12.  const struct worn {
  13.      long w_mask;
  14.      struct obj **w_obj;
  15.  } worn[] = { { W_ARM, &uarm },
  16.               { W_ARMC, &uarmc },
  17.               { W_ARMH, &uarmh },
  18.               { W_ARMS, &uarms },
  19.               { W_ARMG, &uarmg },
  20.               { W_ARMF, &uarmf },
  21.               { W_ARMU, &uarmu },
  22.               { W_RINGL, &uleft },
  23.               { W_RINGR, &uright },
  24.               { W_WEP, &uwep },
  25.               { W_SWAPWEP, &uswapwep },
  26.               { W_QUIVER, &uquiver },
  27.               { W_AMUL, &uamul },
  28.               { W_TOOL, &ublindf },
  29.               { W_BALL, &uball },
  30.               { W_CHAIN, &uchain },
  31.               { 0, 0 } };
  32.  
  33.  /* This only allows for one blocking item per property */
  34.  #define w_blocks(o, m)                                                     \
  35.      ((o->otyp == MUMMY_WRAPPING && ((m) &W_ARMC))                          \
  36.           ? INVIS                                                           \
  37.           : (o->otyp == CORNUTHAUM && ((m) &W_ARMH) && !Role_if(PM_WIZARD)) \
  38.                 ? CLAIRVOYANT                                               \
  39.                 : 0)
  40.  /* note: monsters don't have clairvoyance, so your role
  41.     has no significant effect on their use of w_blocks() */
  42.  

setworn

  1.  /* Updated to use the extrinsic and blocked fields. */
  2.  void
  3.  setworn(obj, mask)
  4.  register struct obj *obj;
  5.  long mask;
  6.  {
  7.      register const struct worn *wp;
  8.      register struct obj *oobj;
  9.      register int p;
  10.  
  11.      if ((mask & (W_ARM | I_SPECIAL)) == (W_ARM | I_SPECIAL)) {
  12.          /* restoring saved game; no properties are conferred via skin */
  13.          uskin = obj;
  14.          /* assert( !uarm ); */
  15.      } else {
  16.          if ((mask & W_ARMOR))
  17.              u.uroleplay.nudist = FALSE;
  18.          for (wp = worn; wp->w_mask; wp++)
  19.              if (wp->w_mask & mask) {
  20.                  oobj = *(wp->w_obj);
  21.                  if (oobj && !(oobj->owornmask & wp->w_mask))
  22.                      impossible("Setworn: mask = %ld.", wp->w_mask);
  23.                  if (oobj) {
  24.                      if (u.twoweap && (oobj->owornmask & (W_WEP | W_SWAPWEP)))
  25.                          u.twoweap = 0;
  26.                      oobj->owornmask &= ~wp->w_mask;
  27.                      if (wp->w_mask & ~(W_SWAPWEP | W_QUIVER)) {
  28.                          /* leave as "x = x <op> y", here and below, for broken
  29.                           * compilers */
  30.                          p = objects[oobj->otyp].oc_oprop;
  31.                          u.uprops[p].extrinsic =
  32.                              u.uprops[p].extrinsic & ~wp->w_mask;
  33.                          if ((p = w_blocks(oobj, mask)) != 0)
  34.                              u.uprops[p].blocked &= ~wp->w_mask;
  35.                          if (oobj->oartifact)
  36.                              set_artifact_intrinsic(oobj, 0, mask);
  37.                      }
  38.                  }
  39.                  *(wp->w_obj) = obj;
  40.                  if (obj) {
  41.                      obj->owornmask |= wp->w_mask;
  42.                      /* Prevent getting/blocking intrinsics from wielding
  43.                       * potions, through the quiver, etc.
  44.                       * Allow weapon-tools, too.
  45.                       * wp_mask should be same as mask at this point.
  46.                       */
  47.                      if (wp->w_mask & ~(W_SWAPWEP | W_QUIVER)) {
  48.                          if (obj->oclass == WEAPON_CLASS || is_weptool(obj)
  49.                              || mask != W_WEP) {
  50.                              p = objects[obj->otyp].oc_oprop;
  51.                              u.uprops[p].extrinsic =
  52.                                  u.uprops[p].extrinsic | wp->w_mask;
  53.                              if ((p = w_blocks(obj, mask)) != 0)
  54.                                  u.uprops[p].blocked |= wp->w_mask;
  55.                          }
  56.                          if (obj->oartifact)
  57.                              set_artifact_intrinsic(obj, 1, mask);
  58.                      }
  59.                  }
  60.              }
  61.      }
  62.      update_inventory();
  63.  }
  64.  

setnotworn

  1.  /* called e.g. when obj is destroyed */
  2.  /* Updated to use the extrinsic and blocked fields. */
  3.  void
  4.  setnotworn(obj)
  5.  register struct obj *obj;
  6.  {
  7.      register const struct worn *wp;
  8.      register int p;
  9.  
  10.      if (!obj)
  11.          return;
  12.      if (obj == uwep || obj == uswapwep)
  13.          u.twoweap = 0;
  14.      for (wp = worn; wp->w_mask; wp++)
  15.          if (obj == *(wp->w_obj)) {
  16.              *(wp->w_obj) = 0;
  17.              p = objects[obj->otyp].oc_oprop;
  18.              u.uprops[p].extrinsic = u.uprops[p].extrinsic & ~wp->w_mask;
  19.              obj->owornmask &= ~wp->w_mask;
  20.              if (obj->oartifact)
  21.                  set_artifact_intrinsic(obj, 0, wp->w_mask);
  22.              if ((p = w_blocks(obj, wp->w_mask)) != 0)
  23.                  u.uprops[p].blocked &= ~wp->w_mask;
  24.          }
  25.      update_inventory();
  26.  }
  27.  

wearslot

  1.  /* return a bitmask of the equipment slot(s) a given item might be worn in */
  2.  long
  3.  wearslot(obj)
  4.  struct obj *obj;
  5.  {
  6.      int otyp = obj->otyp;
  7.      /* practically any item can be wielded or quivered; it's up to
  8.         our caller to handle such things--we assume "normal" usage */
  9.      long res = 0L; /* default: can't be worn anywhere */
  10.  
  11.      switch (obj->oclass) {
  12.      case AMULET_CLASS:
  13.          res = W_AMUL; /* WORN_AMUL */
  14.          break;
  15.      case RING_CLASS:
  16.          res = W_RINGL | W_RINGR; /* W_RING, BOTH_SIDES */
  17.          break;
  18.      case ARMOR_CLASS:
  19.          switch (objects[otyp].oc_armcat) {
  20.          case ARM_SUIT:
  21.              res = W_ARM;
  22.              break; /* WORN_ARMOR */
  23.          case ARM_SHIELD:
  24.              res = W_ARMS;
  25.              break; /* WORN_SHIELD */
  26.          case ARM_HELM:
  27.              res = W_ARMH;
  28.              break; /* WORN_HELMET */
  29.          case ARM_GLOVES:
  30.              res = W_ARMG;
  31.              break; /* WORN_GLOVES */
  32.          case ARM_BOOTS:
  33.              res = W_ARMF;
  34.              break; /* WORN_BOOTS */
  35.          case ARM_CLOAK:
  36.              res = W_ARMC;
  37.              break; /* WORN_CLOAK */
  38.          case ARM_SHIRT:
  39.              res = W_ARMU;
  40.              break; /* WORN_SHIRT */
  41.          }
  42.          break;
  43.      case WEAPON_CLASS:
  44.          res = W_WEP | W_SWAPWEP;
  45.          if (objects[otyp].oc_merge)
  46.              res |= W_QUIVER;
  47.          break;
  48.      case TOOL_CLASS:
  49.          if (otyp == BLINDFOLD || otyp == TOWEL || otyp == LENSES)
  50.              res = W_TOOL; /* WORN_BLINDF */
  51.          else if (is_weptool(obj) || otyp == TIN_OPENER)
  52.              res = W_WEP | W_SWAPWEP;
  53.          else if (otyp == SADDLE)
  54.              res = W_SADDLE;
  55.          break;
  56.      case FOOD_CLASS:
  57.          if (obj->otyp == MEAT_RING)
  58.              res = W_RINGL | W_RINGR;
  59.          break;
  60.      case GEM_CLASS:
  61.          res = W_QUIVER;
  62.          break;
  63.      case BALL_CLASS:
  64.          res = W_BALL;
  65.          break;
  66.      case CHAIN_CLASS:
  67.          res = W_CHAIN;
  68.          break;
  69.      default:
  70.          break;
  71.      }
  72.      return res;
  73.  }
  74.  

mon_set_minvis

  1.  void
  2.  mon_set_minvis(mon)
  3.  struct monst *mon;
  4.  {
  5.      mon->perminvis = 1;
  6.      if (!mon->invis_blkd) {
  7.          mon->minvis = 1;
  8.          newsym(mon->mx, mon->my); /* make it disappear */
  9.          if (mon->wormno)
  10.              see_wsegs(mon); /* and any tail too */
  11.      }
  12.  }
  13.  

mon_adjust_speed

  1.  void
  2.  mon_adjust_speed(mon, adjust, obj)
  3.  struct monst *mon;
  4.  int adjust;      /* positive => increase speed, negative => decrease */
  5.  struct obj *obj; /* item to make known if effect can be seen */
  6.  {
  7.      struct obj *otmp;
  8.      boolean give_msg = !in_mklev, petrify = FALSE;
  9.      unsigned int oldspeed = mon->mspeed;
  10.  
  11.      switch (adjust) {
  12.      case 2:
  13.          mon->permspeed = MFAST;
  14.          give_msg = FALSE; /* special case monster creation */
  15.          break;
  16.      case 1:
  17.          if (mon->permspeed == MSLOW)
  18.              mon->permspeed = 0;
  19.          else
  20.              mon->permspeed = MFAST;
  21.          break;
  22.      case 0: /* just check for worn speed boots */
  23.          break;
  24.      case -1:
  25.          if (mon->permspeed == MFAST)
  26.              mon->permspeed = 0;
  27.          else
  28.              mon->permspeed = MSLOW;
  29.          break;
  30.      case -2:
  31.          mon->permspeed = MSLOW;
  32.          give_msg = FALSE; /* (not currently used) */
  33.          break;
  34.      case -3: /* petrification */
  35.          /* take away intrinsic speed but don't reduce normal speed */
  36.          if (mon->permspeed == MFAST)
  37.              mon->permspeed = 0;
  38.          petrify = TRUE;
  39.          break;
  40.      case -4: /* green slime */
  41.          if (mon->permspeed == MFAST)
  42.              mon->permspeed = 0;
  43.          give_msg = FALSE;
  44.          break;
  45.      }
  46.  
  47.      for (otmp = mon->minvent; otmp; otmp = otmp->nobj)
  48.          if (otmp->owornmask && objects[otmp->otyp].oc_oprop == FAST)
  49.              break;
  50.      if (otmp) /* speed boots */
  51.          mon->mspeed = MFAST;
  52.      else
  53.          mon->mspeed = mon->permspeed;
  54.  
  55.      /* no message if monster is immobile (temp or perm) or unseen */
  56.      if (give_msg && (mon->mspeed != oldspeed || petrify) && mon->data->mmove
  57.          && !(mon->mfrozen || mon->msleeping) && canseemon(mon)) {
  58.          /* fast to slow (skipping intermediate state) or vice versa */
  59.          const char *howmuch =
  60.              (mon->mspeed + oldspeed == MFAST + MSLOW) ? "much " : "";
  61.  
  62.          if (petrify) {
  63.              /* mimic the player's petrification countdown; "slowing down"
  64.                 even if fast movement rate retained via worn speed boots */
  65.              if (flags.verbose)
  66.                  pline("%s is slowing down.", Monnam(mon));
  67.          } else if (adjust > 0 || mon->mspeed == MFAST)
  68.              pline("%s is suddenly moving %sfaster.", Monnam(mon), howmuch);
  69.          else
  70.              pline("%s seems to be moving %sslower.", Monnam(mon), howmuch);
  71.  
  72.          /* might discover an object if we see the speed change happen */
  73.          if (obj != 0)
  74.              learnwand(obj);
  75.      }
  76.  }
  77.  

update_mon_intrinsics

  1.  /* armor put on or taken off; might be magical variety */
  2.  void
  3.  update_mon_intrinsics(mon, obj, on, silently)
  4.  struct monst *mon;
  5.  struct obj *obj;
  6.  boolean on, silently;
  7.  {
  8.      int unseen;
  9.      uchar mask;
  10.      struct obj *otmp;
  11.      int which = (int) objects[obj->otyp].oc_oprop;
  12.  
  13.      unseen = !canseemon(mon);
  14.      if (!which)
  15.          goto maybe_blocks;
  16.  
  17.      if (on) {
  18.          switch (which) {
  19.          case INVIS:
  20.              mon->minvis = !mon->invis_blkd;
  21.              break;
  22.          case FAST: {
  23.              boolean save_in_mklev = in_mklev;
  24.              if (silently)
  25.                  in_mklev = TRUE;
  26.              mon_adjust_speed(mon, 0, obj);
  27.              in_mklev = save_in_mklev;
  28.              break;
  29.          }
  30.          /* properties handled elsewhere */
  31.          case ANTIMAGIC:
  32.          case REFLECTING:
  33.              break;
  34.          /* properties which have no effect for monsters */
  35.          case CLAIRVOYANT:
  36.          case STEALTH:
  37.          case TELEPAT:
  38.              break;
  39.          /* properties which should have an effect but aren't implemented */
  40.          case LEVITATION:
  41.          case WWALKING:
  42.              break;
  43.          /* properties which maybe should have an effect but don't */
  44.          case DISPLACED:
  45.          case FUMBLING:
  46.          case JUMPING:
  47.          case PROTECTION:
  48.              break;
  49.          default:
  50.              if (which <= 8) { /* 1 thru 8 correspond to MR_xxx mask values */
  51.                  /* FIRE,COLD,SLEEP,DISINT,SHOCK,POISON,ACID,STONE */
  52.                  mask = (uchar) (1 << (which - 1));
  53.                  mon->mintrinsics |= (unsigned short) mask;
  54.              }
  55.              break;
  56.          }
  57.      } else { /* off */
  58.          switch (which) {
  59.          case INVIS:
  60.              mon->minvis = mon->perminvis;
  61.              break;
  62.          case FAST: {
  63.              boolean save_in_mklev = in_mklev;
  64.              if (silently)
  65.                  in_mklev = TRUE;
  66.              mon_adjust_speed(mon, 0, obj);
  67.              in_mklev = save_in_mklev;
  68.              break;
  69.          }
  70.          case FIRE_RES:
  71.          case COLD_RES:
  72.          case SLEEP_RES:
  73.          case DISINT_RES:
  74.          case SHOCK_RES:
  75.          case POISON_RES:
  76.          case ACID_RES:
  77.          case STONE_RES:
  78.              mask = (uchar) (1 << (which - 1));
  79.              /* If the monster doesn't have this resistance intrinsically,
  80.                 check whether any other worn item confers it.  Note that
  81.                 we don't currently check for anything conferred via simply
  82.                 carrying an object. */
  83.              if (!(mon->data->mresists & mask)) {
  84.                  for (otmp = mon->minvent; otmp; otmp = otmp->nobj)
  85.                      if (otmp->owornmask
  86.                          && (int) objects[otmp->otyp].oc_oprop == which)
  87.                          break;
  88.                  if (!otmp)
  89.                      mon->mintrinsics &= ~((unsigned short) mask);
  90.              }
  91.              break;
  92.          default:
  93.              break;
  94.          }
  95.      }
  96.  
  97.  maybe_blocks:
  98.      /* obj->owornmask has been cleared by this point, so we can't use it.
  99.         However, since monsters don't wield armor, we don't have to guard
  100.         against that and can get away with a blanket worn-mask value. */
  101.      switch (w_blocks(obj, ~0L)) {
  102.      case INVIS:
  103.          mon->invis_blkd = on ? 1 : 0;
  104.          mon->minvis = on ? 0 : mon->perminvis;
  105.          break;
  106.      default:
  107.          break;
  108.      }
  109.  
  110.      if (!on && mon == u.usteed && obj->otyp == SADDLE)
  111.          dismount_steed(DISMOUNT_FELL);
  112.  
  113.      /* if couldn't see it but now can, or vice versa, update display */
  114.      if (!silently && (unseen ^ !canseemon(mon)))
  115.          newsym(mon->mx, mon->my);
  116.  }
  117.  

find_mac

  1.  int
  2.  find_mac(mon)
  3.  register struct monst *mon;
  4.  {
  5.      register struct obj *obj;
  6.      int base = mon->data->ac;
  7.      long mwflags = mon->misc_worn_check;
  8.  
  9.      for (obj = mon->minvent; obj; obj = obj->nobj) {
  10.          if (obj->owornmask & mwflags)
  11.              base -= ARM_BONUS(obj);
  12.          /* since ARM_BONUS is positive, subtracting it increases AC */
  13.      }
  14.      return base;
  15.  }
  16.  

m_dowear

  1.  /*
  2.   * weapons are handled separately;
  3.   * rings and eyewear aren't used by monsters
  4.   */
  5.  
  6.  /* Wear the best object of each type that the monster has.  During creation,
  7.   * the monster can put everything on at once; otherwise, wearing takes time.
  8.   * This doesn't affect monster searching for objects--a monster may very well
  9.   * search for objects it would not want to wear, because we don't want to
  10.   * check which_armor() each round.
  11.   *
  12.   * We'll let monsters put on shirts and/or suits under worn cloaks, but
  13.   * not shirts under worn suits.  This is somewhat arbitrary, but it's
  14.   * too tedious to have them remove and later replace outer garments,
  15.   * and preventing suits under cloaks makes it a little bit too easy for
  16.   * players to influence what gets worn.  Putting on a shirt underneath
  17.   * already worn body armor is too obviously buggy...
  18.   */
  19.  void
  20.  m_dowear(mon, creation)
  21.  register struct monst *mon;
  22.  boolean creation;
  23.  {
  24.  #define RACE_EXCEPTION TRUE
  25.      /* Note the restrictions here are the same as in dowear in do_wear.c
  26.       * except for the additional restriction on intelligence.  (Players
  27.       * are always intelligent, even if polymorphed).
  28.       */
  29.      if (verysmall(mon->data) || nohands(mon->data) || is_animal(mon->data))
  30.          return;
  31.      /* give mummies a chance to wear their wrappings
  32.       * and let skeletons wear their initial armor */
  33.      if (mindless(mon->data)
  34.          && (!creation || (mon->data->mlet != S_MUMMY
  35.                            && mon->data != &mons[PM_SKELETON])))
  36.          return;
  37.  
  38.      m_dowear_type(mon, W_AMUL, creation, FALSE);
  39.      /* can't put on shirt if already wearing suit */
  40.      if (!cantweararm(mon->data) && !(mon->misc_worn_check & W_ARM))
  41.          m_dowear_type(mon, W_ARMU, creation, FALSE);
  42.      /* treating small as a special case allows
  43.         hobbits, gnomes, and kobolds to wear cloaks */
  44.      if (!cantweararm(mon->data) || mon->data->msize == MZ_SMALL)
  45.          m_dowear_type(mon, W_ARMC, creation, FALSE);
  46.      m_dowear_type(mon, W_ARMH, creation, FALSE);
  47.      if (!MON_WEP(mon) || !bimanual(MON_WEP(mon)))
  48.          m_dowear_type(mon, W_ARMS, creation, FALSE);
  49.      m_dowear_type(mon, W_ARMG, creation, FALSE);
  50.      if (!slithy(mon->data) && mon->data->mlet != S_CENTAUR)
  51.          m_dowear_type(mon, W_ARMF, creation, FALSE);
  52.      if (!cantweararm(mon->data))
  53.          m_dowear_type(mon, W_ARM, creation, FALSE);
  54.      else
  55.          m_dowear_type(mon, W_ARM, creation, RACE_EXCEPTION);
  56.  }
  57.  

m_dowear_type

  1.  STATIC_OVL void
  2.  m_dowear_type(mon, flag, creation, racialexception)
  3.  struct monst *mon;
  4.  long flag;
  5.  boolean creation;
  6.  boolean racialexception;
  7.  {
  8.      struct obj *old, *best, *obj;
  9.      int m_delay = 0;
  10.      int unseen = !canseemon(mon);
  11.      boolean autocurse;
  12.      char nambuf[BUFSZ];
  13.  
  14.      if (mon->mfrozen)
  15.          return; /* probably putting previous item on */
  16.  
  17.      /* Get a copy of monster's name before altering its visibility */
  18.      Strcpy(nambuf, See_invisible ? Monnam(mon) : mon_nam(mon));
  19.  
  20.      old = which_armor(mon, flag);
  21.      if (old && old->cursed)
  22.          return;
  23.      if (old && flag == W_AMUL)
  24.          return; /* no such thing as better amulets */
  25.      best = old;
  26.  
  27.      for (obj = mon->minvent; obj; obj = obj->nobj) {
  28.          switch (flag) {
  29.          case W_AMUL:
  30.              if (obj->oclass != AMULET_CLASS
  31.                  || (obj->otyp != AMULET_OF_LIFE_SAVING
  32.                      && obj->otyp != AMULET_OF_REFLECTION))
  33.                  continue;
  34.              best = obj;
  35.              goto outer_break; /* no such thing as better amulets */
  36.          case W_ARMU:
  37.              if (!is_shirt(obj))
  38.                  continue;
  39.              break;
  40.          case W_ARMC:
  41.              if (!is_cloak(obj))
  42.                  continue;
  43.              break;
  44.          case W_ARMH:
  45.              if (!is_helmet(obj))
  46.                  continue;
  47.              /* changing alignment is not implemented for monsters;
  48.                 priests and minions could change alignment but wouldn't
  49.                 want to, so they reject helms of opposite alignment */
  50.              if (obj->otyp == HELM_OF_OPPOSITE_ALIGNMENT
  51.                  && (mon->ispriest || mon->isminion))
  52.                  continue;
  53.              /* (flimsy exception matches polyself handling) */
  54.              if (has_horns(mon->data) && !is_flimsy(obj))
  55.                  continue;
  56.              break;
  57.          case W_ARMS:
  58.              if (!is_shield(obj))
  59.                  continue;
  60.              break;
  61.          case W_ARMG:
  62.              if (!is_gloves(obj))
  63.                  continue;
  64.              break;
  65.          case W_ARMF:
  66.              if (!is_boots(obj))
  67.                  continue;
  68.              break;
  69.          case W_ARM:
  70.              if (!is_suit(obj))
  71.                  continue;
  72.              if (racialexception && (racial_exception(mon, obj) < 1))
  73.                  continue;
  74.              break;
  75.          }
  76.          if (obj->owornmask)
  77.              continue;
  78.          /* I'd like to define a VISIBLE_ARM_BONUS which doesn't assume the
  79.           * monster knows obj->spe, but if I did that, a monster would keep
  80.           * switching forever between two -2 caps since when it took off one
  81.           * it would forget spe and once again think the object is better
  82.           * than what it already has.
  83.           */
  84.          if (best && (ARM_BONUS(best) + extra_pref(mon, best)
  85.                       >= ARM_BONUS(obj) + extra_pref(mon, obj)))
  86.              continue;
  87.          best = obj;
  88.      }
  89.  outer_break:
  90.      if (!best || best == old)
  91.          return;
  92.  
  93.      /* same auto-cursing behavior as for hero */
  94.      autocurse = ((best->otyp == HELM_OF_OPPOSITE_ALIGNMENT
  95.                    || best->otyp == DUNCE_CAP) && !best->cursed);
  96.      /* if wearing a cloak, account for the time spent removing
  97.         and re-wearing it when putting on a suit or shirt */
  98.      if ((flag == W_ARM || flag == W_ARMU) && (mon->misc_worn_check & W_ARMC))
  99.          m_delay += 2;
  100.      /* when upgrading a piece of armor, account for time spent
  101.         taking off current one */
  102.      if (old)
  103.          m_delay += objects[old->otyp].oc_delay;
  104.  
  105.      if (old) /* do this first to avoid "(being worn)" */
  106.          old->owornmask = 0L;
  107.      if (!creation) {
  108.          if (canseemon(mon)) {
  109.              char buf[BUFSZ];
  110.  
  111.              if (old)
  112.                  Sprintf(buf, " removes %s and", distant_name(old, doname));
  113.              else
  114.                  buf[0] = '\0';
  115.              pline("%s%s puts on %s.", Monnam(mon), buf,
  116.                    distant_name(best, doname));
  117.              if (autocurse)
  118.                  pline("%s %s %s %s for a moment.", s_suffix(Monnam(mon)),
  119.                        simpleonames(best), otense(best, "glow"),
  120.                        hcolor(NH_BLACK));
  121.          } /* can see it */
  122.          m_delay += objects[best->otyp].oc_delay;
  123.          mon->mfrozen = m_delay;
  124.          if (mon->mfrozen)
  125.              mon->mcanmove = 0;
  126.      }
  127.      if (old)
  128.          update_mon_intrinsics(mon, old, FALSE, creation);
  129.      mon->misc_worn_check |= flag;
  130.      best->owornmask |= flag;
  131.      if (autocurse)
  132.          curse(best);
  133.      update_mon_intrinsics(mon, best, TRUE, creation);
  134.      /* if couldn't see it but now can, or vice versa, */
  135.      if (!creation && (unseen ^ !canseemon(mon))) {
  136.          if (mon->minvis && !See_invisible) {
  137.              pline("Suddenly you cannot see %s.", nambuf);
  138.              makeknown(best->otyp);
  139.          } /* else if (!mon->minvis) pline("%s suddenly appears!",
  140.               Amonnam(mon)); */
  141.      }
  142.  }
  143.  #undef RACE_EXCEPTION
  144.  

which_armor

  1.  struct obj *
  2.  which_armor(mon, flag)
  3.  struct monst *mon;
  4.  long flag;
  5.  {
  6.      if (mon == &youmonst) {
  7.          switch (flag) {
  8.          case W_ARM:
  9.              return uarm;
  10.          case W_ARMC:
  11.              return uarmc;
  12.          case W_ARMH:
  13.              return uarmh;
  14.          case W_ARMS:
  15.              return uarms;
  16.          case W_ARMG:
  17.              return uarmg;
  18.          case W_ARMF:
  19.              return uarmf;
  20.          case W_ARMU:
  21.              return uarmu;
  22.          default:
  23.              impossible("bad flag in which_armor");
  24.              return 0;
  25.          }
  26.      } else {
  27.          register struct obj *obj;
  28.  
  29.          for (obj = mon->minvent; obj; obj = obj->nobj)
  30.              if (obj->owornmask & flag)
  31.                  return obj;
  32.          return (struct obj *) 0;
  33.      }
  34.  }
  35.  

m_lose_armor

  1.  /* remove an item of armor and then drop it */
  2.  STATIC_OVL void
  3.  m_lose_armor(mon, obj)
  4.  struct monst *mon;
  5.  struct obj *obj;
  6.  {
  7.      mon->misc_worn_check &= ~obj->owornmask;
  8.      if (obj->owornmask)
  9.          update_mon_intrinsics(mon, obj, FALSE, FALSE);
  10.      obj->owornmask = 0L;
  11.  
  12.      obj_extract_self(obj);
  13.      place_object(obj, mon->mx, mon->my);
  14.      /* call stackobj() if we ever drop anything that can merge */
  15.      newsym(mon->mx, mon->my);
  16.  }
  17.  

clear_bypasses

  1.  /* all objects with their bypass bit set should now be reset to normal */
  2.  void
  3.  clear_bypasses()
  4.  {
  5.      struct obj *otmp, *nobj;
  6.      struct monst *mtmp;
  7.  
  8.      for (otmp = fobj; otmp; otmp = nobj) {
  9.          nobj = otmp->nobj;
  10.          if (otmp->bypass) {
  11.              otmp->bypass = 0;
  12.  
  13.              /* bypass will have inhibited any stacking, but since it's
  14.               * used for polymorph handling, the objects here probably
  15.               * have been transformed and won't be stacked in the usual
  16.               * manner afterwards; so don't bother with this.
  17.               * [Changing the fobj chain mid-traversal would also be risky.]
  18.               */
  19.  #if 0
  20.              if (objects[otmp->otyp].oc_merge) {
  21.                  xchar ox, oy;
  22.  
  23.                  (void) get_obj_location(otmp, &ox, &oy, 0);
  24.                  stack_object(otmp);
  25.                  newsym(ox, oy);
  26.              }
  27.  #endif /*0*/
  28.          }
  29.      }
  30.      for (otmp = invent; otmp; otmp = otmp->nobj)
  31.          otmp->bypass = 0;
  32.      for (otmp = migrating_objs; otmp; otmp = otmp->nobj)
  33.          otmp->bypass = 0;
  34.      for (mtmp = fmon; mtmp; mtmp = mtmp->nmon) {
  35.          if (DEADMONSTER(mtmp))
  36.              continue;
  37.          for (otmp = mtmp->minvent; otmp; otmp = otmp->nobj)
  38.              otmp->bypass = 0;
  39.      }
  40.      for (mtmp = migrating_mons; mtmp; mtmp = mtmp->nmon) {
  41.          for (otmp = mtmp->minvent; otmp; otmp = otmp->nobj)
  42.              otmp->bypass = 0;
  43.      }
  44.      /* billobjs and mydogs chains don't matter here */
  45.      context.bypasses = FALSE;
  46.  }
  47.  

bypass_obj

  1.  void
  2.  bypass_obj(obj)
  3.  struct obj *obj;
  4.  {
  5.      obj->bypass = 1;
  6.      context.bypasses = TRUE;
  7.  }
  8.  

bypass_objlist

  1.  /* set or clear the bypass bit in a list of objects */
  2.  void
  3.  bypass_objlist(objchain, on)
  4.  struct obj *objchain;
  5.  boolean on; /* TRUE => set, FALSE => clear */
  6.  {
  7.      if (on && objchain)
  8.          context.bypasses = TRUE;
  9.      while (objchain) {
  10.          objchain->bypass = on ? 1 : 0;
  11.          objchain = objchain->nobj;
  12.      }
  13.  }
  14.  

nxt_unbypassed_obj

  1.  /* return the first object without its bypass bit set; set that bit
  2.     before returning so that successive calls will find further objects */
  3.  struct obj *
  4.  nxt_unbypassed_obj(objchain)
  5.  struct obj *objchain;
  6.  {
  7.      while (objchain) {
  8.          if (!objchain->bypass) {
  9.              bypass_obj(objchain);
  10.              break;
  11.          }
  12.          objchain = objchain->nobj;
  13.      }
  14.      return objchain;
  15.  }
  16.  

mon_break_armor

  1.  void
  2.  mon_break_armor(mon, polyspot)
  3.  struct monst *mon;
  4.  boolean polyspot;
  5.  {
  6.      register struct obj *otmp;
  7.      struct permonst *mdat = mon->data;
  8.      boolean vis = cansee(mon->mx, mon->my);
  9.      boolean handless_or_tiny = (nohands(mdat) || verysmall(mdat));
  10.      const char *pronoun = mhim(mon), *ppronoun = mhis(mon);
  11.  
  12.      if (breakarm(mdat)) {
  13.          if ((otmp = which_armor(mon, W_ARM)) != 0) {
  14.              if ((Is_dragon_scales(otmp) && mdat == Dragon_scales_to_pm(otmp))
  15.                  || (Is_dragon_mail(otmp) && mdat == Dragon_mail_to_pm(otmp)))
  16.                  ; /* no message here;
  17.                       "the dragon merges with his scaly armor" is odd
  18.                       and the monster's previous form is already gone */
  19.              else if (vis)
  20.                  pline("%s breaks out of %s armor!", Monnam(mon), ppronoun);
  21.              else
  22.                  You_hear("a cracking sound.");
  23.              m_useup(mon, otmp);
  24.          }
  25.          if ((otmp = which_armor(mon, W_ARMC)) != 0) {
  26.              if (otmp->oartifact) {
  27.                  if (vis)
  28.                      pline("%s %s falls off!", s_suffix(Monnam(mon)),
  29.                            cloak_simple_name(otmp));
  30.                  if (polyspot)
  31.                      bypass_obj(otmp);
  32.                  m_lose_armor(mon, otmp);
  33.              } else {
  34.                  if (vis)
  35.                      pline("%s %s tears apart!", s_suffix(Monnam(mon)),
  36.                            cloak_simple_name(otmp));
  37.                  else
  38.                      You_hear("a ripping sound.");
  39.                  m_useup(mon, otmp);
  40.              }
  41.          }
  42.          if ((otmp = which_armor(mon, W_ARMU)) != 0) {
  43.              if (vis)
  44.                  pline("%s shirt rips to shreds!", s_suffix(Monnam(mon)));
  45.              else
  46.                  You_hear("a ripping sound.");
  47.              m_useup(mon, otmp);
  48.          }
  49.      } else if (sliparm(mdat)) {
  50.          if ((otmp = which_armor(mon, W_ARM)) != 0) {
  51.              if (vis)
  52.                  pline("%s armor falls around %s!", s_suffix(Monnam(mon)),
  53.                        pronoun);
  54.              else
  55.                  You_hear("a thud.");
  56.              if (polyspot)
  57.                  bypass_obj(otmp);
  58.              m_lose_armor(mon, otmp);
  59.          }
  60.          if ((otmp = which_armor(mon, W_ARMC)) != 0) {
  61.              if (vis) {
  62.                  if (is_whirly(mon->data))
  63.                      pline("%s %s falls, unsupported!", s_suffix(Monnam(mon)),
  64.                            cloak_simple_name(otmp));
  65.                  else
  66.                      pline("%s shrinks out of %s %s!", Monnam(mon), ppronoun,
  67.                            cloak_simple_name(otmp));
  68.              }
  69.              if (polyspot)
  70.                  bypass_obj(otmp);
  71.              m_lose_armor(mon, otmp);
  72.          }
  73.          if ((otmp = which_armor(mon, W_ARMU)) != 0) {
  74.              if (vis) {
  75.                  if (sliparm(mon->data))
  76.                      pline("%s seeps right through %s shirt!", Monnam(mon),
  77.                            ppronoun);
  78.                  else
  79.                      pline("%s becomes much too small for %s shirt!",
  80.                            Monnam(mon), ppronoun);
  81.              }
  82.              if (polyspot)
  83.                  bypass_obj(otmp);
  84.              m_lose_armor(mon, otmp);
  85.          }
  86.      }
  87.      if (handless_or_tiny) {
  88.          /* [caller needs to handle weapon checks] */
  89.          if ((otmp = which_armor(mon, W_ARMG)) != 0) {
  90.              if (vis)
  91.                  pline("%s drops %s gloves%s!", Monnam(mon), ppronoun,
  92.                        MON_WEP(mon) ? " and weapon" : "");
  93.              if (polyspot)
  94.                  bypass_obj(otmp);
  95.              m_lose_armor(mon, otmp);
  96.          }
  97.          if ((otmp = which_armor(mon, W_ARMS)) != 0) {
  98.              if (vis)
  99.                  pline("%s can no longer hold %s shield!", Monnam(mon),
  100.                        ppronoun);
  101.              else
  102.                  You_hear("a clank.");
  103.              if (polyspot)
  104.                  bypass_obj(otmp);
  105.              m_lose_armor(mon, otmp);
  106.          }
  107.      }
  108.      if (handless_or_tiny || has_horns(mdat)) {
  109.          if ((otmp = which_armor(mon, W_ARMH)) != 0
  110.              /* flimsy test for horns matches polyself handling */
  111.              && (handless_or_tiny || !is_flimsy(otmp))) {
  112.              if (vis)
  113.                  pline("%s helmet falls to the %s!", s_suffix(Monnam(mon)),
  114.                        surface(mon->mx, mon->my));
  115.              else
  116.                  You_hear("a clank.");
  117.              if (polyspot)
  118.                  bypass_obj(otmp);
  119.              m_lose_armor(mon, otmp);
  120.          }
  121.      }
  122.      if (handless_or_tiny || slithy(mdat) || mdat->mlet == S_CENTAUR) {
  123.          if ((otmp = which_armor(mon, W_ARMF)) != 0) {
  124.              if (vis) {
  125.                  if (is_whirly(mon->data))
  126.                      pline("%s boots fall away!", s_suffix(Monnam(mon)));
  127.                  else
  128.                      pline("%s boots %s off %s feet!", s_suffix(Monnam(mon)),
  129.                            verysmall(mdat) ? "slide" : "are pushed", ppronoun);
  130.              }
  131.              if (polyspot)
  132.                  bypass_obj(otmp);
  133.              m_lose_armor(mon, otmp);
  134.          }
  135.      }
  136.      if (!can_saddle(mon)) {
  137.          if ((otmp = which_armor(mon, W_SADDLE)) != 0) {
  138.              if (polyspot)
  139.                  bypass_obj(otmp);
  140.              m_lose_armor(mon, otmp);
  141.              if (vis)
  142.                  pline("%s saddle falls off.", s_suffix(Monnam(mon)));
  143.          }
  144.          if (mon == u.usteed)
  145.              goto noride;
  146.      } else if (mon == u.usteed && !can_ride(mon)) {
  147.      noride:
  148.          You("can no longer ride %s.", mon_nam(mon));
  149.          if (touch_petrifies(u.usteed->data) && !Stone_resistance && rnl(3)) {
  150.              char buf[BUFSZ];
  151.  
  152.              You("touch %s.", mon_nam(u.usteed));
  153.              Sprintf(buf, "falling off %s", an(u.usteed->data->mname));
  154.              instapetrify(buf);
  155.          }
  156.          dismount_steed(DISMOUNT_FELL);
  157.      }
  158.      return;
  159.  }
  160.  

extra_pref

  1.  /* bias a monster's preferences towards armor that has special benefits. */
  2.  STATIC_OVL int
  3.  extra_pref(mon, obj)
  4.  struct monst *mon;
  5.  struct obj *obj;
  6.  {
  7.      /* currently only does speed boots, but might be expanded if monsters
  8.       * get to use more armor abilities
  9.       */
  10.      if (obj) {
  11.          if (obj->otyp == SPEED_BOOTS && mon->permspeed != MFAST)
  12.              return 20;
  13.      }
  14.      return 0;
  15.  }
  16.  

racial_exception

  1.  /*
  2.   * Exceptions to things based on race.
  3.   * Correctly checks polymorphed player race.
  4.   * Returns:
  5.   *       0 No exception, normal rules apply.
  6.   *       1 If the race/object combination is acceptable.
  7.   *      -1 If the race/object combination is unacceptable.
  8.   */
  9.  int
  10.  racial_exception(mon, obj)
  11.  struct monst *mon;
  12.  struct obj *obj;
  13.  {
  14.      const struct permonst *ptr = raceptr(mon);
  15.  
  16.      /* Acceptable Exceptions: */
  17.      /* Allow hobbits to wear elven armor - LoTR */
  18.      if (ptr == &mons[PM_HOBBIT] && is_elven_armor(obj))
  19.          return 1;
  20.      /* Unacceptable Exceptions: */
  21.      /* Checks for object that certain races should never use go here */
  22.      /*  return -1; */
  23.  
  24.      return 0;
  25.  }
  26.  /*worn.c*/