Source:NetHack 3.6.0/src/monmove.c

From NetHackWiki
(Redirected from Source:Monmove.c)
Jump to: navigation, search

Below is the full text to monmove.c from the source code of NetHack 3.6.0. To link to a particular line, write [[Source:NetHack 3.6.0/src/monmove.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	monmove.c	$NHDT-Date: 1446808446 2015/11/06 11:14:06 $  $NHDT-Branch: master $:$NHDT-Revision: 1.78 $ */
  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 "mfndpos.h"
  7.  #include "artifact.h"
  8.  
  9.  extern boolean notonhead;
  10.  
  11.  STATIC_DCL void FDECL(watch_on_duty, (struct monst *));
  12.  STATIC_DCL int FDECL(disturb, (struct monst *));
  13.  STATIC_DCL void FDECL(release_hero, (struct monst *));
  14.  STATIC_DCL void FDECL(distfleeck, (struct monst *, int *, int *, int *));
  15.  STATIC_DCL int FDECL(m_arrival, (struct monst *));
  16.  STATIC_DCL boolean FDECL(stuff_prevents_passage, (struct monst *));
  17.  STATIC_DCL int FDECL(vamp_shift, (struct monst *, struct permonst *));
  18.  

mb_trapped

  1.  /* True if mtmp died */
  2.  boolean
  3.  mb_trapped(mtmp)
  4.  struct monst *mtmp;
  5.  {
  6.      if (flags.verbose) {
  7.          if (cansee(mtmp->mx, mtmp->my) && !Unaware)
  8.              pline("KABOOM!!  You see a door explode.");
  9.          else if (!Deaf)
  10.              You_hear("a distant explosion.");
  11.      }
  12.      wake_nearto(mtmp->mx, mtmp->my, 7 * 7);
  13.      mtmp->mstun = 1;
  14.      mtmp->mhp -= rnd(15);
  15.      if (mtmp->mhp <= 0) {
  16.          mondied(mtmp);
  17.          if (mtmp->mhp > 0) /* lifesaved */
  18.              return FALSE;
  19.          else
  20.              return TRUE;
  21.      }
  22.      return FALSE;
  23.  }
  24.  

monhaskey

  1.  /* check whether a monster is carrying a locking/unlocking tool */
  2.  boolean
  3.  monhaskey(mon, for_unlocking)
  4.  struct monst *mon;
  5.  boolean for_unlocking; /* true => credit card ok, false => not ok */
  6.  {
  7.      if (for_unlocking && m_carrying(mon, CREDIT_CARD))
  8.          return TRUE;
  9.      return m_carrying(mon, SKELETON_KEY) || m_carrying(mon, LOCK_PICK);
  10.  }
  11.  

mon_yells

  1.  void
  2.  mon_yells(mon, shout)
  3.  struct monst *mon;
  4.  const char *shout;
  5.  {
  6.      if (canspotmon(mon))
  7.          pline("%s yells:", Amonnam(mon));
  8.      else
  9.          You_hear("someone yell:");
  10.      verbalize1(shout);
  11.  }
  12.  

watch_on_duty

  1.  STATIC_OVL void
  2.  watch_on_duty(mtmp)
  3.  register struct monst *mtmp;
  4.  {
  5.      int x, y;
  6.  
  7.      if (mtmp->mpeaceful && in_town(u.ux + u.dx, u.uy + u.dy)
  8.          && mtmp->mcansee && m_canseeu(mtmp) && !rn2(3)) {
  9.          if (picking_lock(&x, &y) && IS_DOOR(levl[x][y].typ)
  10.              && (levl[x][y].doormask & D_LOCKED)) {
  11.              if (couldsee(mtmp->mx, mtmp->my)) {
  12.                  if (levl[x][y].looted & D_WARNED) {
  13.                      mon_yells(mtmp, "Halt, thief!  You're under arrest!");
  14.                      (void) angry_guards(!!Deaf);
  15.                  } else {
  16.                      mon_yells(mtmp, "Hey, stop picking that lock!");
  17.                      levl[x][y].looted |= D_WARNED;
  18.                  }
  19.                  stop_occupation();
  20.              }
  21.          } else if (is_digging()) {
  22.              /* chewing, wand/spell of digging are checked elsewhere */
  23.              watch_dig(mtmp, context.digging.pos.x, context.digging.pos.y,
  24.                        FALSE);
  25.          }
  26.      }
  27.  }
  28.  

dochugw

  1.  int
  2.  dochugw(mtmp)
  3.  register struct monst *mtmp;
  4.  {
  5.      int x = mtmp->mx, y = mtmp->my;
  6.      boolean already_saw_mon = !occupation ? 0 : canspotmon(mtmp);
  7.      int rd = dochug(mtmp);
  8.  
  9.      /* a similar check is in monster_nearby() in hack.c */
  10.      /* check whether hero notices monster and stops current activity */
  11.      if (occupation && !rd && !Confusion && (!mtmp->mpeaceful || Hallucination)
  12.          /* it's close enough to be a threat */
  13.          && distu(x, y) <= (BOLT_LIM + 1) * (BOLT_LIM + 1)
  14.          /* and either couldn't see it before, or it was too far away */
  15.          && (!already_saw_mon || !couldsee(x, y)
  16.              || distu(x, y) > (BOLT_LIM + 1) * (BOLT_LIM + 1))
  17.          /* can see it now, or sense it and would normally see it */
  18.          && (canseemon(mtmp) || (sensemon(mtmp) && couldsee(x, y)))
  19.          && mtmp->mcanmove && !noattacks(mtmp->data)
  20.          && !onscary(u.ux, u.uy, mtmp))
  21.          stop_occupation();
  22.  
  23.      return rd;
  24.  }
  25.  

onscary

  1.  boolean
  2.  onscary(x, y, mtmp)
  3.  int x, y;
  4.  struct monst *mtmp;
  5.  {
  6.      boolean epresent = sengr_at("Elbereth", x, y, TRUE);
  7.  
  8.      /* creatures who are directly resistant to magical scaring:
  9.       * Rodney, lawful minions, angels, the Riders */
  10.      if (mtmp->iswiz || is_lminion(mtmp) || mtmp->data == &mons[PM_ANGEL]
  11.          || is_rider(mtmp->data))
  12.          return FALSE;
  13.  
  14.      /* should this still be true for defiled/molochian altars? */
  15.      if (IS_ALTAR(levl[x][y].typ)
  16.          && (mtmp->data->mlet == S_VAMPIRE || is_vampshifter(mtmp)))
  17.          return TRUE;
  18.  
  19.      /* the scare monster scroll doesn't have any of the below
  20.       * restrictions, being its own source of power */
  21.      if (sobj_at(SCR_SCARE_MONSTER, x, y))
  22.          return TRUE;
  23.  
  24.      /* creatures who don't (or can't) fear a written Elbereth:
  25.       * all the above plus shopkeepers, guards, blind or
  26.       * peaceful monsters, humans, and minotaurs.
  27.       *
  28.       * if the player isn't actually on the square OR the player's image
  29.       * isn't displaced to the square, no protection is being granted
  30.       *
  31.       * Elbereth doesn't work in Gehennom, the Elemental Planes, or the
  32.       * Astral Plane; the influence of the Valar only reaches so far.  */
  33.      return (epresent
  34.              && ((u.ux == x && u.uy == y)
  35.                  || (Displaced && mtmp->mux == x && mtmp->muy == y))
  36.              && !(mtmp->isshk || mtmp->isgd || !mtmp->mcansee
  37.                   || mtmp->mpeaceful || mtmp->data->mlet == S_HUMAN
  38.                   || mtmp->data == &mons[PM_MINOTAUR]
  39.                   || Inhell || In_endgame(&u.uz)));
  40.  }
  41.  
  42.  

mon_regen

  1.  /* regenerate lost hit points */
  2.  void
  3.  mon_regen(mon, digest_meal)
  4.  struct monst *mon;
  5.  boolean digest_meal;
  6.  {
  7.      if (mon->mhp < mon->mhpmax && (moves % 20 == 0 || regenerates(mon->data)))
  8.          mon->mhp++;
  9.      if (mon->mspec_used)
  10.          mon->mspec_used--;
  11.      if (digest_meal) {
  12.          if (mon->meating) {
  13.              mon->meating--;
  14.              if (mon->meating <= 0)
  15.                  finish_meating(mon);
  16.          }
  17.      }
  18.  }
  19.  

disturb

  1.  /*
  2.   * Possibly awaken the given monster.  Return a 1 if the monster has been
  3.   * jolted awake.
  4.   */
  5.  STATIC_OVL int
  6.  disturb(mtmp)
  7.  register struct monst *mtmp;
  8.  {
  9.      /*
  10.       * + Ettins are hard to surprise.
  11.       * + Nymphs, jabberwocks, and leprechauns do not easily wake up.
  12.       *
  13.       * Wake up if:
  14.       *  in direct LOS                                           AND
  15.       *  within 10 squares                                       AND
  16.       *  not stealthy or (mon is an ettin and 9/10)              AND
  17.       *  (mon is not a nymph, jabberwock, or leprechaun) or 1/50 AND
  18.       *  Aggravate or mon is (dog or human) or
  19.       *      (1/7 and mon is not mimicing furniture or object)
  20.       */
  21.      if (couldsee(mtmp->mx, mtmp->my) && distu(mtmp->mx, mtmp->my) <= 100
  22.          && (!Stealth || (mtmp->data == &mons[PM_ETTIN] && rn2(10)))
  23.          && (!(mtmp->data->mlet == S_NYMPH
  24.                || mtmp->data == &mons[PM_JABBERWOCK]
  25.  #if 0 /* DEFERRED */
  26.                || mtmp->data == &mons[PM_VORPAL_JABBERWOCK]
  27.  #endif
  28.                || mtmp->data->mlet == S_LEPRECHAUN) || !rn2(50))
  29.          && (Aggravate_monster
  30.              || (mtmp->data->mlet == S_DOG || mtmp->data->mlet == S_HUMAN)
  31.              || (!rn2(7) && mtmp->m_ap_type != M_AP_FURNITURE
  32.                  && mtmp->m_ap_type != M_AP_OBJECT))) {
  33.          mtmp->msleeping = 0;
  34.          return 1;
  35.      }
  36.      return 0;
  37.  }
  38.  

release_hero

  1.  /* ungrab/expel held/swallowed hero */
  2.  STATIC_OVL void
  3.  release_hero(mon)
  4.  struct monst *mon;
  5.  {
  6.      if (mon == u.ustuck) {
  7.          if (u.uswallow) {
  8.              expels(mon, mon->data, TRUE);
  9.          } else if (!sticks(youmonst.data)) {
  10.              unstuck(mon); /* let go */
  11.              You("get released!");
  12.          }
  13.      }
  14.  }
  15.  

monflee

  1.  /* monster begins fleeing for the specified time, 0 means untimed flee
  2.   * if first, only adds fleetime if monster isn't already fleeing
  3.   * if fleemsg, prints a message about new flight, otherwise, caller should */
  4.  void
  5.  monflee(mtmp, fleetime, first, fleemsg)
  6.  struct monst *mtmp;
  7.  int fleetime;
  8.  boolean first;
  9.  boolean fleemsg;
  10.  {
  11.      /* shouldn't happen; maybe warrants impossible()? */
  12.      if (DEADMONSTER(mtmp))
  13.          return;
  14.  
  15.      if (mtmp == u.ustuck)
  16.          release_hero(mtmp); /* expels/unstuck */
  17.  
  18.      if (!first || !mtmp->mflee) {
  19.          /* don't lose untimed scare */
  20.          if (!fleetime)
  21.              mtmp->mfleetim = 0;
  22.          else if (!mtmp->mflee || mtmp->mfleetim) {
  23.              fleetime += (int) mtmp->mfleetim;
  24.              /* ensure monster flees long enough to visibly stop fighting */
  25.              if (fleetime == 1)
  26.                  fleetime++;
  27.              mtmp->mfleetim = (unsigned) min(fleetime, 127);
  28.          }
  29.          if (!mtmp->mflee && fleemsg && canseemon(mtmp)
  30.              && mtmp->m_ap_type != M_AP_FURNITURE
  31.              && mtmp->m_ap_type != M_AP_OBJECT) {
  32.              /* unfortunately we can't distinguish between temporary
  33.                 sleep and temporary paralysis, so both conditions
  34.                 receive the same alternate message */
  35.              if (!mtmp->mcanmove || !mtmp->data->mmove)
  36.                  pline("%s seems to flinch.", Adjmonnam(mtmp, "immobile"));
  37.              else
  38.                  pline("%s turns to flee.", Monnam(mtmp));
  39.          }
  40.          mtmp->mflee = 1;
  41.      }
  42.  }
  43.  

distfleeck

  1.  STATIC_OVL void
  2.  distfleeck(mtmp, inrange, nearby, scared)
  3.  register struct monst *mtmp;
  4.  int *inrange, *nearby, *scared;
  5.  {
  6.      int seescaryx, seescaryy;
  7.      boolean sawscary = FALSE;
  8.  
  9.      *inrange = (dist2(mtmp->mx, mtmp->my, mtmp->mux, mtmp->muy)
  10.                  <= (BOLT_LIM * BOLT_LIM));
  11.      *nearby = *inrange && monnear(mtmp, mtmp->mux, mtmp->muy);
  12.  
  13.      /* Note: if your image is displaced, the monster sees the Elbereth
  14.       * at your displaced position, thus never attacking your displaced
  15.       * position, but possibly attacking you by accident.  If you are
  16.       * invisible, it sees the Elbereth at your real position, thus never
  17.       * running into you by accident but possibly attacking the spot
  18.       * where it guesses you are.
  19.       */
  20.      if (!mtmp->mcansee || (Invis && !perceives(mtmp->data))) {
  21.          seescaryx = mtmp->mux;
  22.          seescaryy = mtmp->muy;
  23.      } else {
  24.          seescaryx = u.ux;
  25.          seescaryy = u.uy;
  26.      }
  27.  
  28.      sawscary = onscary(seescaryx, seescaryy, mtmp);
  29.      if (*nearby && (sawscary
  30.                      || (!mtmp->mpeaceful && in_your_sanctuary(mtmp, 0, 0)))) {
  31.          *scared = 1;
  32.          monflee(mtmp, rnd(rn2(7) ? 10 : 100), TRUE, TRUE);
  33.  
  34.          /* magical protection won't last forever, so there'll be a
  35.           * chance of the magic being used up regardless of type */
  36.          if (sawscary) {
  37.              wipe_engr_at(seescaryx, seescaryy, 1, TRUE);
  38.          }
  39.      } else
  40.          *scared = 0;
  41.  }
  42.  

m_arrival

  1.  /* perform a special one-time action for a monster; returns -1 if nothing
  2.     special happened, 0 if monster uses up its turn, 1 if monster is killed */
  3.  STATIC_OVL int
  4.  m_arrival(mon)
  5.  struct monst *mon;
  6.  {
  7.      mon->mstrategy &= ~STRAT_ARRIVE; /* always reset */
  8.  
  9.      return -1;
  10.  }
  11.  

dochug

  1.  /* returns 1 if monster died moving, 0 otherwise */
  2.  /* The whole dochugw/m_move/distfleeck/mfndpos section is serious spaghetti
  3.   * code. --KAA
  4.   */
  5.  int
  6.  dochug(mtmp)
  7.  register struct monst *mtmp;
  8.  {
  9.      register struct permonst *mdat;
  10.      register int tmp = 0;
  11.      int inrange, nearby, scared;
  12.  
  13.      /*  Pre-movement adjustments
  14.       */
  15.  
  16.      mdat = mtmp->data;
  17.  
  18.      if (mtmp->mstrategy & STRAT_ARRIVE) {
  19.          int res = m_arrival(mtmp);
  20.          if (res >= 0)
  21.              return res;
  22.      }
  23.  
  24.      /* check for waitmask status change */
  25.      if ((mtmp->mstrategy & STRAT_WAITFORU)
  26.          && (m_canseeu(mtmp) || mtmp->mhp < mtmp->mhpmax))
  27.          mtmp->mstrategy &= ~STRAT_WAITFORU;
  28.  
  29.      /* update quest status flags */
  30.      quest_stat_check(mtmp);
  31.  
  32.      if (!mtmp->mcanmove || (mtmp->mstrategy & STRAT_WAITMASK)) {
  33.          if (Hallucination)
  34.              newsym(mtmp->mx, mtmp->my);
  35.          if (mtmp->mcanmove && (mtmp->mstrategy & STRAT_CLOSE)
  36.              && !mtmp->msleeping && monnear(mtmp, u.ux, u.uy))
  37.              quest_talk(mtmp); /* give the leaders a chance to speak */
  38.          return 0;             /* other frozen monsters can't do anything */
  39.      }
  40.  
  41.      /* there is a chance we will wake it */
  42.      if (mtmp->msleeping && !disturb(mtmp)) {
  43.          if (Hallucination)
  44.              newsym(mtmp->mx, mtmp->my);
  45.          return 0;
  46.      }
  47.  
  48.      /* not frozen or sleeping: wipe out texts written in the dust */
  49.      wipe_engr_at(mtmp->mx, mtmp->my, 1, FALSE);
  50.  
  51.      /* confused monsters get unconfused with small probability */
  52.      if (mtmp->mconf && !rn2(50))
  53.          mtmp->mconf = 0;
  54.  
  55.      /* stunned monsters get un-stunned with larger probability */
  56.      if (mtmp->mstun && !rn2(10))
  57.          mtmp->mstun = 0;
  58.  
  59.      /* some monsters teleport */
  60.      if (mtmp->mflee && !rn2(40) && can_teleport(mdat) && !mtmp->iswiz
  61.          && !level.flags.noteleport) {
  62.          (void) rloc(mtmp, TRUE);
  63.          return 0;
  64.      }
  65.      if (mdat->msound == MS_SHRIEK && !um_dist(mtmp->mx, mtmp->my, 1))
  66.          m_respond(mtmp);
  67.      if (mdat == &mons[PM_MEDUSA] && couldsee(mtmp->mx, mtmp->my))
  68.          m_respond(mtmp);
  69.      if (mtmp->mhp <= 0)
  70.          return 1; /* m_respond gaze can kill medusa */
  71.  
  72.      /* fleeing monsters might regain courage */
  73.      if (mtmp->mflee && !mtmp->mfleetim && mtmp->mhp == mtmp->mhpmax
  74.          && !rn2(25))
  75.          mtmp->mflee = 0;
  76.  
  77.      /* cease conflict-induced swallow/grab if conflict has ended */
  78.      if (mtmp == u.ustuck && mtmp->mpeaceful && !mtmp->mconf && !Conflict) {
  79.          release_hero(mtmp);
  80.          return 0; /* uses up monster's turn */
  81.      }
  82.  
  83.      set_apparxy(mtmp);
  84.      /* Must be done after you move and before the monster does.  The
  85.       * set_apparxy() call in m_move() doesn't suffice since the variables
  86.       * inrange, etc. all depend on stuff set by set_apparxy().
  87.       */
  88.  
  89.      /* Monsters that want to acquire things */
  90.      /* may teleport, so do it before inrange is set */
  91.      if (is_covetous(mdat))
  92.          (void) tactics(mtmp);
  93.  
  94.      /* check distance and scariness of attacks */
  95.      distfleeck(mtmp, &inrange, &nearby, &scared);
  96.  
  97.      if (find_defensive(mtmp)) {
  98.          if (use_defensive(mtmp) != 0)
  99.              return 1;
  100.      } else if (find_misc(mtmp)) {
  101.          if (use_misc(mtmp) != 0)
  102.              return 1;
  103.      }
  104.  
  105.      /* Demonic Blackmail! */
  106.      if (nearby && mdat->msound == MS_BRIBE && mtmp->mpeaceful && !mtmp->mtame
  107.          && !u.uswallow) {
  108.          if (mtmp->mux != u.ux || mtmp->muy != u.uy) {
  109.              pline("%s whispers at thin air.",
  110.                    cansee(mtmp->mux, mtmp->muy) ? Monnam(mtmp) : "It");
  111.  
  112.              if (is_demon(youmonst.data)) {
  113.                  /* "Good hunting, brother" */
  114.                  if (!tele_restrict(mtmp))
  115.                      (void) rloc(mtmp, TRUE);
  116.              } else {
  117.                  mtmp->minvis = mtmp->perminvis = 0;
  118.                  /* Why?  For the same reason in real demon talk */
  119.                  pline("%s gets angry!", Amonnam(mtmp));
  120.                  mtmp->mpeaceful = 0;
  121.                  set_malign(mtmp);
  122.                  /* since no way is an image going to pay it off */
  123.              }
  124.          } else if (demon_talk(mtmp))
  125.              return 1; /* you paid it off */
  126.      }
  127.  
  128.      /* the watch will look around and see if you are up to no good :-) */
  129.      if (is_watch(mdat)) {
  130.          watch_on_duty(mtmp);
  131.  
  132.      } else if (is_mind_flayer(mdat) && !rn2(20)) {
  133.          struct monst *m2, *nmon = (struct monst *) 0;
  134.  
  135.          if (canseemon(mtmp))
  136.              pline("%s concentrates.", Monnam(mtmp));
  137.          if (distu(mtmp->mx, mtmp->my) > BOLT_LIM * BOLT_LIM) {
  138.              You("sense a faint wave of psychic energy.");
  139.              goto toofar;
  140.          }
  141.          pline("A wave of psychic energy pours over you!");
  142.          if (mtmp->mpeaceful
  143.              && (!Conflict || resist(mtmp, RING_CLASS, 0, 0))) {
  144.              pline("It feels quite soothing.");
  145.          } else if (!u.uinvulnerable) {
  146.              register boolean m_sen = sensemon(mtmp);
  147.  
  148.              if (m_sen || (Blind_telepat && rn2(2)) || !rn2(10)) {
  149.                  int dmg;
  150.                  pline("It locks on to your %s!",
  151.                        m_sen ? "telepathy" : Blind_telepat ? "latent telepathy"
  152.                                                            : "mind");
  153.                  dmg = rnd(15);
  154.                  if (Half_spell_damage)
  155.                      dmg = (dmg + 1) / 2;
  156.                  losehp(dmg, "psychic blast", KILLED_BY_AN);
  157.              }
  158.          }
  159.          for (m2 = fmon; m2; m2 = nmon) {
  160.              nmon = m2->nmon;
  161.              if (DEADMONSTER(m2))
  162.                  continue;
  163.              if (m2->mpeaceful == mtmp->mpeaceful)
  164.                  continue;
  165.              if (mindless(m2->data))
  166.                  continue;
  167.              if (m2 == mtmp)
  168.                  continue;
  169.              if ((telepathic(m2->data) && (rn2(2) || m2->mblinded))
  170.                  || !rn2(10)) {
  171.                  if (cansee(m2->mx, m2->my))
  172.                      pline("It locks on to %s.", mon_nam(m2));
  173.                  m2->mhp -= rnd(15);
  174.                  if (m2->mhp <= 0)
  175.                      monkilled(m2, "", AD_DRIN);
  176.                  else
  177.                      m2->msleeping = 0;
  178.              }
  179.          }
  180.      }
  181.  toofar:
  182.  
  183.      /* If monster is nearby you, and has to wield a weapon, do so.   This
  184.       * costs the monster a move, of course.
  185.       */
  186.      if ((!mtmp->mpeaceful || Conflict) && inrange
  187.          && dist2(mtmp->mx, mtmp->my, mtmp->mux, mtmp->muy) <= 8
  188.          && attacktype(mdat, AT_WEAP)) {
  189.          struct obj *mw_tmp;
  190.  
  191.          /* The scared check is necessary.  Otherwise a monster that is
  192.           * one square near the player but fleeing into a wall would keep
  193.           * switching between pick-axe and weapon.  If monster is stuck
  194.           * in a trap, prefer ranged weapon (wielding is done in thrwmu).
  195.           * This may cost the monster an attack, but keeps the monster
  196.           * from switching back and forth if carrying both.
  197.           */
  198.          mw_tmp = MON_WEP(mtmp);
  199.          if (!(scared && mw_tmp && is_pick(mw_tmp))
  200.              && mtmp->weapon_check == NEED_WEAPON
  201.              && !(mtmp->mtrapped && !nearby && select_rwep(mtmp))) {
  202.              mtmp->weapon_check = NEED_HTH_WEAPON;
  203.              if (mon_wield_item(mtmp) != 0)
  204.                  return 0;
  205.          }
  206.      }
  207.  
  208.      /*  Now the actual movement phase
  209.       */
  210.  
  211.      if (!nearby || mtmp->mflee || scared || mtmp->mconf || mtmp->mstun
  212.          || (mtmp->minvis && !rn2(3))
  213.          || (mdat->mlet == S_LEPRECHAUN && !findgold(invent)
  214.              && (findgold(mtmp->minvent) || rn2(2)))
  215.          || (is_wanderer(mdat) && !rn2(4)) || (Conflict && !mtmp->iswiz)
  216.          || (!mtmp->mcansee && !rn2(4)) || mtmp->mpeaceful) {
  217.          /* Possibly cast an undirected spell if not attacking you */
  218.          /* note that most of the time castmu() will pick a directed
  219.             spell and do nothing, so the monster moves normally */
  220.          /* arbitrary distance restriction to keep monster far away
  221.             from you from having cast dozens of sticks-to-snakes
  222.             or similar spells by the time you reach it */
  223.          if (dist2(mtmp->mx, mtmp->my, u.ux, u.uy) <= 49
  224.              && !mtmp->mspec_used) {
  225.              struct attack *a;
  226.  
  227.              for (a = &mdat->mattk[0]; a < &mdat->mattk[NATTK]; a++) {
  228.                  if (a->aatyp == AT_MAGC
  229.                      && (a->adtyp == AD_SPEL || a->adtyp == AD_CLRC)) {
  230.                      if (castmu(mtmp, a, FALSE, FALSE)) {
  231.                          tmp = 3;
  232.                          break;
  233.                      }
  234.                  }
  235.              }
  236.          }
  237.  
  238.          tmp = m_move(mtmp, 0);
  239.          if (tmp != 2)
  240.              distfleeck(mtmp, &inrange, &nearby, &scared); /* recalc */
  241.  
  242.          switch (tmp) { /* for pets, cases 0 and 3 are equivalent */
  243.          case 0: /* no movement, but it can still attack you */
  244.          case 3: /* absolutely no movement */
  245.              /* vault guard might have vanished */
  246.              if (mtmp->isgd && (mtmp->mhp < 1 || !mtmp->mx == 0))
  247.                  return 1; /* behave as if it died */
  248.              /* During hallucination, monster appearance should
  249.               * still change - even if it doesn't move.
  250.               */
  251.              if (Hallucination)
  252.                  newsym(mtmp->mx, mtmp->my);
  253.              break;
  254.          case 1: /* monster moved */
  255.              /* Maybe it stepped on a trap and fell asleep... */
  256.              if (mtmp->msleeping || !mtmp->mcanmove)
  257.                  return 0;
  258.              /* Monsters can move and then shoot on same turn;
  259.                 our hero can't.  Is that fair? */
  260.              if (!nearby && (ranged_attk(mdat) || find_offensive(mtmp)))
  261.                  break;
  262.              /* engulfer/grabber checks */
  263.              if (mtmp == u.ustuck) {
  264.                  /* a monster that's digesting you can move at the
  265.                   * same time -dlc
  266.                   */
  267.                  if (u.uswallow)
  268.                      return mattacku(mtmp);
  269.                  /* if confused grabber has wandered off, let go */
  270.                  if (distu(mtmp->mx, mtmp->my) > 2)
  271.                      unstuck(mtmp);
  272.              }
  273.              return 0;
  274.          case 2: /* monster died */
  275.              return 1;
  276.          }
  277.      }
  278.  
  279.      /*  Now, attack the player if possible - one attack set per monst
  280.       */
  281.  
  282.      if (!mtmp->mpeaceful || (Conflict && !resist(mtmp, RING_CLASS, 0, 0))) {
  283.          if (inrange && !noattacks(mdat) && u.uhp > 0 && !scared && tmp != 3)
  284.              if (mattacku(mtmp))
  285.                  return 1; /* monster died (e.g. exploded) */
  286.  
  287.          if (mtmp->wormno)
  288.              wormhitu(mtmp);
  289.      }
  290.      /* special speeches for quest monsters */
  291.      if (!mtmp->msleeping && mtmp->mcanmove && nearby)
  292.          quest_talk(mtmp);
  293.      /* extra emotional attack for vile monsters */
  294.      if (inrange && mtmp->data->msound == MS_CUSS && !mtmp->mpeaceful
  295.          && couldsee(mtmp->mx, mtmp->my) && !mtmp->minvis && !rn2(5))
  296.          cuss(mtmp);
  297.  
  298.      return (tmp == 2);
  299.  }
  300.  


  1.  static NEARDATA const char practical[] = { WEAPON_CLASS, ARMOR_CLASS,
  2.                                             GEM_CLASS, FOOD_CLASS, 0 };
  3.  static NEARDATA const char magical[] = { AMULET_CLASS, POTION_CLASS,
  4.                                           SCROLL_CLASS, WAND_CLASS,
  5.                                           RING_CLASS,   SPBOOK_CLASS, 0 };
  6.  static NEARDATA const char indigestion[] = { BALL_CLASS, ROCK_CLASS, 0 };
  7.  static NEARDATA const char boulder_class[] = { ROCK_CLASS, 0 };
  8.  static NEARDATA const char gem_class[] = { GEM_CLASS, 0 };
  9.  

itsstuck

  1.  boolean
  2.  itsstuck(mtmp)
  3.  register struct monst *mtmp;
  4.  {
  5.      if (sticks(youmonst.data) && mtmp == u.ustuck && !u.uswallow) {
  6.          pline("%s cannot escape from you!", Monnam(mtmp));
  7.          return TRUE;
  8.      }
  9.      return FALSE;
  10.  }
  11.  

should_displace

  1.  /*
  2.   * should_displace()
  3.   *
  4.   * Displacement of another monster is a last resort and only
  5.   * used on approach. If there are better ways to get to target,
  6.   * those should be used instead. This function does that evaluation.
  7.   */
  8.  boolean
  9.  should_displace(mtmp, poss, info, cnt, gx, gy)
  10.  struct monst *mtmp;
  11.  coord *poss; /* coord poss[9] */
  12.  long *info;  /* long info[9] */
  13.  int cnt;
  14.  xchar gx, gy;
  15.  {
  16.      int shortest_with_displacing = -1;
  17.      int shortest_without_displacing = -1;
  18.      int count_without_displacing = 0;
  19.      register int i, nx, ny;
  20.      int ndist;
  21.  
  22.      for (i = 0; i < cnt; i++) {
  23.          nx = poss[i].x;
  24.          ny = poss[i].y;
  25.          ndist = dist2(nx, ny, gx, gy);
  26.          if (MON_AT(nx, ny) && (info[i] & ALLOW_MDISP) && !(info[i] & ALLOW_M)
  27.              && !undesirable_disp(mtmp, nx, ny)) {
  28.              if (shortest_with_displacing == -1
  29.                  || (ndist < shortest_with_displacing))
  30.                  shortest_with_displacing = ndist;
  31.          } else {
  32.              if ((shortest_without_displacing == -1)
  33.                  || (ndist < shortest_without_displacing))
  34.                  shortest_without_displacing = ndist;
  35.              count_without_displacing++;
  36.          }
  37.      }
  38.      if (shortest_with_displacing > -1
  39.          && (shortest_with_displacing < shortest_without_displacing
  40.              || !count_without_displacing))
  41.          return TRUE;
  42.      return FALSE;
  43.  }
  44.  

m_move

  1.  /* Return values:
  2.   * 0: did not move, but can still attack and do other stuff.
  3.   * 1: moved, possibly can attack.
  4.   * 2: monster died.
  5.   * 3: did not move, and can't do anything else either.
  6.   */
  7.  int
  8.  m_move(mtmp, after)
  9.  register struct monst *mtmp;
  10.  register int after;
  11.  {
  12.      register int appr;
  13.      xchar gx, gy, nix, niy, chcnt;
  14.      int chi; /* could be schar except for stupid Sun-2 compiler */
  15.      boolean likegold = 0, likegems = 0, likeobjs = 0, likemagic = 0,
  16.              conceals = 0;
  17.      boolean likerock = 0, can_tunnel = 0;
  18.      boolean can_open = 0, can_unlock = 0, doorbuster = 0;
  19.      boolean uses_items = 0, setlikes = 0;
  20.      boolean avoid = FALSE;
  21.      boolean better_with_displacing = FALSE;
  22.      struct permonst *ptr;
  23.      struct monst *mtoo;
  24.      schar mmoved = 0; /* not strictly nec.: chi >= 0 will do */
  25.      long info[9];
  26.      long flag;
  27.      int omx = mtmp->mx, omy = mtmp->my;
  28.      struct obj *mw_tmp;
  29.  
  30.      if (mtmp->mtrapped) {
  31.          int i = mintrap(mtmp);
  32.          if (i >= 2) {
  33.              newsym(mtmp->mx, mtmp->my);
  34.              return 2;
  35.          } /* it died */
  36.          if (i == 1)
  37.              return 0; /* still in trap, so didn't move */
  38.      }
  39.      ptr = mtmp->data; /* mintrap() can change mtmp->data -dlc */
  40.  
  41.      if (mtmp->meating) {
  42.          mtmp->meating--;
  43.          if (mtmp->meating <= 0)
  44.              finish_meating(mtmp);
  45.          return 3; /* still eating */
  46.      }
  47.      if (hides_under(ptr) && OBJ_AT(mtmp->mx, mtmp->my) && rn2(10))
  48.          return 0; /* do not leave hiding place */
  49.  
  50.      set_apparxy(mtmp);
  51.      /* where does mtmp think you are? */
  52.      /* Not necessary if m_move called from this file, but necessary in
  53.       * other calls of m_move (ex. leprechauns dodging)
  54.       */
  55.      if (!Is_rogue_level(&u.uz))
  56.          can_tunnel = tunnels(ptr);
  57.      can_open = !(nohands(ptr) || verysmall(ptr));
  58.      can_unlock =
  59.          ((can_open && monhaskey(mtmp, TRUE)) || mtmp->iswiz || is_rider(ptr));
  60.      doorbuster = is_giant(ptr);
  61.      if (mtmp->wormno)
  62.          goto not_special;
  63.      /* my dog gets special treatment */
  64.      if (mtmp->mtame) {
  65.          mmoved = dog_move(mtmp, after);
  66.          goto postmov;
  67.      }
  68.  
  69.      /* likewise for shopkeeper */
  70.      if (mtmp->isshk) {
  71.          mmoved = shk_move(mtmp);
  72.          if (mmoved == -2)
  73.              return 2;
  74.          if (mmoved >= 0)
  75.              goto postmov;
  76.          mmoved = 0; /* follow player outside shop */
  77.      }
  78.  
  79.      /* and for the guard */
  80.      if (mtmp->isgd) {
  81.          mmoved = gd_move(mtmp);
  82.          if (mmoved == -2)
  83.              return 2;
  84.          if (mmoved >= 0)
  85.              goto postmov;
  86.          mmoved = 0;
  87.      }
  88.  
  89.      /* and the acquisitive monsters get special treatment */
  90.      if (is_covetous(ptr)) {
  91.          xchar tx = STRAT_GOALX(mtmp->mstrategy),
  92.                ty = STRAT_GOALY(mtmp->mstrategy);
  93.          struct monst *intruder = m_at(tx, ty);
  94.          /*
  95.           * if there's a monster on the object or in possession of it,
  96.           * attack it.
  97.           */
  98.          if ((dist2(mtmp->mx, mtmp->my, tx, ty) < 2) && intruder
  99.              && (intruder != mtmp)) {
  100.              notonhead = (intruder->mx != tx || intruder->my != ty);
  101.              if (mattackm(mtmp, intruder) == 2)
  102.                  return 2;
  103.              mmoved = 1;
  104.          } else
  105.              mmoved = 0;
  106.          goto postmov;
  107.      }
  108.  
  109.      /* and for the priest */
  110.      if (mtmp->ispriest) {
  111.          mmoved = pri_move(mtmp);
  112.          if (mmoved == -2)
  113.              return 2;
  114.          if (mmoved >= 0)
  115.              goto postmov;
  116.          mmoved = 0;
  117.      }
  118.  
  119.  #ifdef MAIL
  120.      if (ptr == &mons[PM_MAIL_DAEMON]) {
  121.          if (!Deaf && canseemon(mtmp))
  122.              verbalize("I'm late!");
  123.          mongone(mtmp);
  124.          return 2;
  125.      }
  126.  #endif
  127.  
  128.      /* teleport if that lies in our nature */
  129.      if (ptr == &mons[PM_TENGU] && !rn2(5) && !mtmp->mcan
  130.          && !tele_restrict(mtmp)) {
  131.          if (mtmp->mhp < 7 || mtmp->mpeaceful || rn2(2))
  132.              (void) rloc(mtmp, TRUE);
  133.          else
  134.              mnexto(mtmp);
  135.          mmoved = 1;
  136.          goto postmov;
  137.      }
  138.  not_special:
  139.      if (u.uswallow && !mtmp->mflee && u.ustuck != mtmp)
  140.          return 1;
  141.      omx = mtmp->mx;
  142.      omy = mtmp->my;
  143.      gx = mtmp->mux;
  144.      gy = mtmp->muy;
  145.      appr = mtmp->mflee ? -1 : 1;
  146.      if (mtmp->mconf || (u.uswallow && mtmp == u.ustuck)) {
  147.          appr = 0;
  148.      } else {
  149.          struct obj *lepgold, *ygold;
  150.          boolean should_see = (couldsee(omx, omy)
  151.                                && (levl[gx][gy].lit || !levl[omx][omy].lit)
  152.                                && (dist2(omx, omy, gx, gy) <= 36));
  153.  
  154.          if (!mtmp->mcansee
  155.              || (should_see && Invis && !perceives(ptr) && rn2(11))
  156.              || is_obj_mappear(&youmonst,STRANGE_OBJECT) || u.uundetected
  157.              || (is_obj_mappear(&youmonst,GOLD_PIECE) && !likes_gold(ptr))
  158.              || (mtmp->mpeaceful && !mtmp->isshk) /* allow shks to follow */
  159.              || ((monsndx(ptr) == PM_STALKER || ptr->mlet == S_BAT
  160.                   || ptr->mlet == S_LIGHT) && !rn2(3)))
  161.              appr = 0;
  162.  
  163.          if (monsndx(ptr) == PM_LEPRECHAUN && (appr == 1)
  164.              && ((lepgold = findgold(mtmp->minvent))
  165.                  && (lepgold->quan
  166.                      > ((ygold = findgold(invent)) ? ygold->quan : 0L))))
  167.              appr = -1;
  168.  
  169.          if (!should_see && can_track(ptr)) {
  170.              register coord *cp;
  171.  
  172.              cp = gettrack(omx, omy);
  173.              if (cp) {
  174.                  gx = cp->x;
  175.                  gy = cp->y;
  176.              }
  177.          }
  178.      }
  179.  
  180.      if ((!mtmp->mpeaceful || !rn2(10)) && (!Is_rogue_level(&u.uz))) {
  181.          boolean in_line = (lined_up(mtmp)
  182.                 && (distmin(mtmp->mx, mtmp->my, mtmp->mux, mtmp->muy)
  183.                     <= (throws_rocks(youmonst.data) ? 20 : ACURRSTR / 2 + 1)));
  184.  
  185.          if (appr != 1 || !in_line) {
  186.              /* Monsters in combat won't pick stuff up, avoiding the
  187.               * situation where you toss arrows at it and it has nothing
  188.               * better to do than pick the arrows up.
  189.               */
  190.              register int pctload =
  191.                  (curr_mon_load(mtmp) * 100) / max_mon_load(mtmp);
  192.  
  193.              /* look for gold or jewels nearby */
  194.              likegold = (likes_gold(ptr) && pctload < 95);
  195.              likegems = (likes_gems(ptr) && pctload < 85);
  196.              uses_items = (!mindless(ptr) && !is_animal(ptr) && pctload < 75);
  197.              likeobjs = (likes_objs(ptr) && pctload < 75);
  198.              likemagic = (likes_magic(ptr) && pctload < 85);
  199.              likerock = (throws_rocks(ptr) && pctload < 50 && !Sokoban);
  200.              conceals = hides_under(ptr);
  201.              setlikes = TRUE;
  202.          }
  203.      }
  204.  
  205.  #define SQSRCHRADIUS 5
  206.  
  207.      {
  208.          register int minr = SQSRCHRADIUS; /* not too far away */
  209.          register struct obj *otmp;
  210.          register int xx, yy;
  211.          int oomx, oomy, lmx, lmy;
  212.  
  213.          /* cut down the search radius if it thinks character is closer. */
  214.          if (distmin(mtmp->mux, mtmp->muy, omx, omy) < SQSRCHRADIUS
  215.              && !mtmp->mpeaceful)
  216.              minr--;
  217.          /* guards shouldn't get too distracted */
  218.          if (!mtmp->mpeaceful && is_mercenary(ptr))
  219.              minr = 1;
  220.  
  221.          if ((likegold || likegems || likeobjs || likemagic || likerock
  222.               || conceals) && (!*in_rooms(omx, omy, SHOPBASE)
  223.                                || (!rn2(25) && !mtmp->isshk))) {
  224.          look_for_obj:
  225.              oomx = min(COLNO - 1, omx + minr);
  226.              oomy = min(ROWNO - 1, omy + minr);
  227.              lmx = max(1, omx - minr);
  228.              lmy = max(0, omy - minr);
  229.              for (otmp = fobj; otmp; otmp = otmp->nobj) {
  230.                  /* monsters may pick rocks up, but won't go out of their way
  231.                     to grab them; this might hamper sling wielders, but it cuts
  232.                     down on move overhead by filtering out most common item */
  233.                  if (otmp->otyp == ROCK)
  234.                      continue;
  235.                  xx = otmp->ox;
  236.                  yy = otmp->oy;
  237.                  /* Nymphs take everything.  Most other creatures should not
  238.                   * pick up corpses except as a special case like in
  239.                   * searches_for_item().  We need to do this check in
  240.                   * mpickstuff() as well.
  241.                   */
  242.                  if (xx >= lmx && xx <= oomx && yy >= lmy && yy <= oomy) {
  243.                      /* don't get stuck circling around an object that's
  244.                         underneath
  245.                         an immobile or hidden monster; paralysis victims
  246.                         excluded */
  247.                      if ((mtoo = m_at(xx, yy)) != 0
  248.                          && (mtoo->msleeping || mtoo->mundetected
  249.                              || (mtoo->mappearance && !mtoo->iswiz)
  250.                              || !mtoo->data->mmove))
  251.                          continue;
  252.  
  253.                      if (((likegold && otmp->oclass == COIN_CLASS)
  254.                           || (likeobjs && index(practical, otmp->oclass)
  255.                               && (otmp->otyp != CORPSE
  256.                                   || (ptr->mlet == S_NYMPH
  257.                                       && !is_rider(&mons[otmp->corpsenm]))))
  258.                           || (likemagic && index(magical, otmp->oclass))
  259.                           || (uses_items && searches_for_item(mtmp, otmp))
  260.                           || (likerock && otmp->otyp == BOULDER)
  261.                           || (likegems && otmp->oclass == GEM_CLASS
  262.                               && objects[otmp->otyp].oc_material != MINERAL)
  263.                           || (conceals && !cansee(otmp->ox, otmp->oy))
  264.                           || (ptr == &mons[PM_GELATINOUS_CUBE]
  265.                               && !index(indigestion, otmp->oclass)
  266.                               && !(otmp->otyp == CORPSE
  267.                                    && touch_petrifies(&mons[otmp->corpsenm]))))
  268.                          && touch_artifact(otmp, mtmp)) {
  269.                          if (can_carry(mtmp, otmp) > 0
  270.                              && (throws_rocks(ptr) || !sobj_at(BOULDER, xx, yy))
  271.                              && (!is_unicorn(ptr)
  272.                                  || objects[otmp->otyp].oc_material == GEMSTONE)
  273.                              /* Don't get stuck circling an Elbereth */
  274.                              && !onscary(xx, yy, mtmp)) {
  275.                              minr = distmin(omx, omy, xx, yy);
  276.                              oomx = min(COLNO - 1, omx + minr);
  277.                              oomy = min(ROWNO - 1, omy + minr);
  278.                              lmx = max(1, omx - minr);
  279.                              lmy = max(0, omy - minr);
  280.                              gx = otmp->ox;
  281.                              gy = otmp->oy;
  282.                              if (gx == omx && gy == omy) {
  283.                                  mmoved = 3; /* actually unnecessary */
  284.                                  goto postmov;
  285.                              }
  286.                          }
  287.                      }
  288.                  }
  289.              }
  290.          } else if (likegold) {
  291.              /* don't try to pick up anything else, but use the same loop */
  292.              uses_items = 0;
  293.              likegems = likeobjs = likemagic = likerock = conceals = 0;
  294.              goto look_for_obj;
  295.          }
  296.  
  297.          if (minr < SQSRCHRADIUS && appr == -1) {
  298.              if (distmin(omx, omy, mtmp->mux, mtmp->muy) <= 3) {
  299.                  gx = mtmp->mux;
  300.                  gy = mtmp->muy;
  301.              } else
  302.                  appr = 1;
  303.          }
  304.      }
  305.  
  306.      /* don't tunnel if hostile and close enough to prefer a weapon */
  307.      if (can_tunnel && needspick(ptr)
  308.          && ((!mtmp->mpeaceful || Conflict)
  309.              && dist2(mtmp->mx, mtmp->my, mtmp->mux, mtmp->muy) <= 8))
  310.          can_tunnel = FALSE;
  311.  
  312.      nix = omx;
  313.      niy = omy;
  314.      flag = 0L;
  315.      if (mtmp->mpeaceful && (!Conflict || resist(mtmp, RING_CLASS, 0, 0)))
  316.          flag |= (ALLOW_SANCT | ALLOW_SSM);
  317.      else
  318.          flag |= ALLOW_U;
  319.      if (is_minion(ptr) || is_rider(ptr))
  320.          flag |= ALLOW_SANCT;
  321.      /* unicorn may not be able to avoid hero on a noteleport level */
  322.      if (is_unicorn(ptr) && !level.flags.noteleport)
  323.          flag |= NOTONL;
  324.      if (passes_walls(ptr))
  325.          flag |= (ALLOW_WALL | ALLOW_ROCK);
  326.      if (passes_bars(ptr))
  327.          flag |= ALLOW_BARS;
  328.      if (can_tunnel)
  329.          flag |= ALLOW_DIG;
  330.      if (is_human(ptr) || ptr == &mons[PM_MINOTAUR])
  331.          flag |= ALLOW_SSM;
  332.      if (is_undead(ptr) && ptr->mlet != S_GHOST)
  333.          flag |= NOGARLIC;
  334.      if (is_vampshifter(mtmp))
  335.          flag |= NOGARLIC;
  336.      if (throws_rocks(ptr))
  337.          flag |= ALLOW_ROCK;
  338.      if (can_open)
  339.          flag |= OPENDOOR;
  340.      if (can_unlock)
  341.          flag |= UNLOCKDOOR;
  342.      if (doorbuster)
  343.          flag |= BUSTDOOR;
  344.      {
  345.          register int i, j, nx, ny, nearer;
  346.          int jcnt, cnt;
  347.          int ndist, nidist;
  348.          register coord *mtrk;
  349.          coord poss[9];
  350.  
  351.          cnt = mfndpos(mtmp, poss, info, flag);
  352.          chcnt = 0;
  353.          jcnt = min(MTSZ, cnt - 1);
  354.          chi = -1;
  355.          nidist = dist2(nix, niy, gx, gy);
  356.          /* allow monsters be shortsighted on some levels for balance */
  357.          if (!mtmp->mpeaceful && level.flags.shortsighted
  358.              && nidist > (couldsee(nix, niy) ? 144 : 36) && appr == 1)
  359.              appr = 0;
  360.          if (is_unicorn(ptr) && level.flags.noteleport) {
  361.              /* on noteleport levels, perhaps we cannot avoid hero */
  362.              for (i = 0; i < cnt; i++)
  363.                  if (!(info[i] & NOTONL))
  364.                      avoid = TRUE;
  365.          }
  366.          better_with_displacing =
  367.              should_displace(mtmp, poss, info, cnt, gx, gy);
  368.          for (i = 0; i < cnt; i++) {
  369.              if (avoid && (info[i] & NOTONL))
  370.                  continue;
  371.              nx = poss[i].x;
  372.              ny = poss[i].y;
  373.  
  374.              if (MON_AT(nx, ny) && (info[i] & ALLOW_MDISP)
  375.                  && !(info[i] & ALLOW_M) && !better_with_displacing)
  376.                  continue;
  377.              if (appr != 0) {
  378.                  mtrk = &mtmp->mtrack[0];
  379.                  for (j = 0; j < jcnt; mtrk++, j++)
  380.                      if (nx == mtrk->x && ny == mtrk->y)
  381.                          if (rn2(4 * (cnt - j)))
  382.                              goto nxti;
  383.              }
  384.  
  385.              nearer = ((ndist = dist2(nx, ny, gx, gy)) < nidist);
  386.  
  387.              if ((appr == 1 && nearer) || (appr == -1 && !nearer)
  388.                  || (!appr && !rn2(++chcnt)) || !mmoved) {
  389.                  nix = nx;
  390.                  niy = ny;
  391.                  nidist = ndist;
  392.                  chi = i;
  393.                  mmoved = 1;
  394.              }
  395.          nxti:
  396.              ;
  397.          }
  398.      }
  399.  
  400.      if (mmoved) {
  401.          register int j;
  402.  
  403.          if (mmoved == 1 && (u.ux != nix || u.uy != niy) && itsstuck(mtmp))
  404.              return 3;
  405.  
  406.          if (mmoved == 1 && can_tunnel && needspick(ptr)
  407.              && ((IS_ROCK(levl[nix][niy].typ) && may_dig(nix, niy))
  408.                  || closed_door(nix, niy))) {
  409.              if (closed_door(nix, niy)) {
  410.                  if (!(mw_tmp = MON_WEP(mtmp)) || !is_pick(mw_tmp)
  411.                      || !is_axe(mw_tmp))
  412.                      mtmp->weapon_check = NEED_PICK_OR_AXE;
  413.              } else if (IS_TREE(levl[nix][niy].typ)) {
  414.                  if (!(mw_tmp = MON_WEP(mtmp)) || !is_axe(mw_tmp))
  415.                      mtmp->weapon_check = NEED_AXE;
  416.              } else if (!(mw_tmp = MON_WEP(mtmp)) || !is_pick(mw_tmp)) {
  417.                  mtmp->weapon_check = NEED_PICK_AXE;
  418.              }
  419.              if (mtmp->weapon_check >= NEED_PICK_AXE && mon_wield_item(mtmp))
  420.                  return 3;
  421.          }
  422.          /* If ALLOW_U is set, either it's trying to attack you, or it
  423.           * thinks it is.  In either case, attack this spot in preference to
  424.           * all others.
  425.           */
  426.          /* Actually, this whole section of code doesn't work as you'd expect.
  427.           * Most attacks are handled in dochug().  It calls distfleeck(), which
  428.           * among other things sets nearby if the monster is near you--and if
  429.           * nearby is set, we never call m_move unless it is a special case
  430.           * (confused, stun, etc.)  The effect is that this ALLOW_U (and
  431.           * mfndpos) has no effect for normal attacks, though it lets a
  432.           * confused monster attack you by accident.
  433.           */
  434.          if (info[chi] & ALLOW_U) {
  435.              nix = mtmp->mux;
  436.              niy = mtmp->muy;
  437.          }
  438.          if (nix == u.ux && niy == u.uy) {
  439.              mtmp->mux = u.ux;
  440.              mtmp->muy = u.uy;
  441.              return 0;
  442.          }
  443.          /* The monster may attack another based on 1 of 2 conditions:
  444.           * 1 - It may be confused.
  445.           * 2 - It may mistake the monster for your (displaced) image.
  446.           * Pets get taken care of above and shouldn't reach this code.
  447.           * Conflict gets handled even farther away (movemon()).
  448.           */
  449.          if ((info[chi] & ALLOW_M) || (nix == mtmp->mux && niy == mtmp->muy)) {
  450.              struct monst *mtmp2;
  451.              int mstatus;
  452.              mtmp2 = m_at(nix, niy);
  453.  
  454.              notonhead = mtmp2 && (nix != mtmp2->mx || niy != mtmp2->my);
  455.              /* note: mstatus returns 0 if mtmp2 is nonexistent */
  456.              mstatus = mattackm(mtmp, mtmp2);
  457.  
  458.              if (mstatus & MM_AGR_DIED) /* aggressor died */
  459.                  return 2;
  460.  
  461.              if ((mstatus & MM_HIT) && !(mstatus & MM_DEF_DIED) && rn2(4)
  462.                  && mtmp2->movement >= NORMAL_SPEED) {
  463.                  mtmp2->movement -= NORMAL_SPEED;
  464.                  notonhead = 0;
  465.                  mstatus = mattackm(mtmp2, mtmp); /* return attack */
  466.                  if (mstatus & MM_DEF_DIED)
  467.                      return 2;
  468.              }
  469.              return 3;
  470.          }
  471.  
  472.          if ((info[chi] & ALLOW_MDISP)) {
  473.              struct monst *mtmp2;
  474.              int mstatus;
  475.              mtmp2 = m_at(nix, niy);
  476.              mstatus = mdisplacem(mtmp, mtmp2, FALSE);
  477.              if ((mstatus & MM_AGR_DIED) || (mstatus & MM_DEF_DIED))
  478.                  return 2;
  479.              if (mstatus & MM_HIT)
  480.                  return 1;
  481.              return 3;
  482.          }
  483.  
  484.          if (!m_in_out_region(mtmp, nix, niy))
  485.              return 3;
  486.          remove_monster(omx, omy);
  487.          place_monster(mtmp, nix, niy);
  488.          for (j = MTSZ - 1; j > 0; j--)
  489.              mtmp->mtrack[j] = mtmp->mtrack[j - 1];
  490.          mtmp->mtrack[0].x = omx;
  491.          mtmp->mtrack[0].y = omy;
  492.          /* Place a segment at the old position. */
  493.          if (mtmp->wormno)
  494.              worm_move(mtmp);
  495.      } else {
  496.          if (is_unicorn(ptr) && rn2(2) && !tele_restrict(mtmp)) {
  497.              (void) rloc(mtmp, TRUE);
  498.              return 1;
  499.          }
  500.          if (mtmp->wormno)
  501.              worm_nomove(mtmp);
  502.      }
  503.  postmov:
  504.      if (mmoved == 1 || mmoved == 3) {
  505.          boolean canseeit = cansee(mtmp->mx, mtmp->my);
  506.  
  507.          if (mmoved == 1) {
  508.              newsym(omx, omy); /* update the old position */
  509.              if (mintrap(mtmp) >= 2) {
  510.                  if (mtmp->mx)
  511.                      newsym(mtmp->mx, mtmp->my);
  512.                  return 2; /* it died */
  513.              }
  514.              ptr = mtmp->data;
  515.  
  516.              /* open a door, or crash through it, if you can */
  517.              if (IS_DOOR(levl[mtmp->mx][mtmp->my].typ)
  518.                  && !passes_walls(ptr) /* doesn't need to open doors */
  519.                  && !can_tunnel) {     /* taken care of below */
  520.                  struct rm *here = &levl[mtmp->mx][mtmp->my];
  521.                  boolean btrapped = (here->doormask & D_TRAPPED),
  522.                          observeit = canseeit && canspotmon(mtmp);
  523.  
  524.                  if (here->doormask & (D_LOCKED | D_CLOSED)
  525.                      && (amorphous(ptr)
  526.                          || (!amorphous(ptr) && can_fog(mtmp)
  527.                              && vamp_shift(mtmp, &mons[PM_FOG_CLOUD])))) {
  528.                      if (flags.verbose && canseemon(mtmp))
  529.                          pline("%s %s under the door.", Monnam(mtmp),
  530.                                (ptr == &mons[PM_FOG_CLOUD]
  531.                                 || ptr == &mons[PM_YELLOW_LIGHT])
  532.                                    ? "flows"
  533.                                    : "oozes");
  534.                  } else if (here->doormask & D_LOCKED && can_unlock) {
  535.                      if (btrapped) {
  536.                          here->doormask = D_NODOOR;
  537.                          newsym(mtmp->mx, mtmp->my);
  538.                          unblock_point(mtmp->mx, mtmp->my); /* vision */
  539.                          if (mb_trapped(mtmp))
  540.                              return 2;
  541.                      } else {
  542.                          if (flags.verbose) {
  543.                              if (observeit)
  544.                                  pline("%s unlocks and opens a door.",
  545.                                        Monnam(mtmp));
  546.                              else if (canseeit)
  547.                                  You_see("a door unlock and open.");
  548.                              else if (!Deaf)
  549.                                  You_hear("a door unlock and open.");
  550.                          }
  551.                          here->doormask = D_ISOPEN;
  552.                          /* newsym(mtmp->mx, mtmp->my); */
  553.                          unblock_point(mtmp->mx, mtmp->my); /* vision */
  554.                      }
  555.                  } else if (here->doormask == D_CLOSED && can_open) {
  556.                      if (btrapped) {
  557.                          here->doormask = D_NODOOR;
  558.                          newsym(mtmp->mx, mtmp->my);
  559.                          unblock_point(mtmp->mx, mtmp->my); /* vision */
  560.                          if (mb_trapped(mtmp))
  561.                              return 2;
  562.                      } else {
  563.                          if (flags.verbose) {
  564.                              if (observeit)
  565.                                  pline("%s opens a door.", Monnam(mtmp));
  566.                              else if (canseeit)
  567.                                  You_see("a door open.");
  568.                              else if (!Deaf)
  569.                                  You_hear("a door open.");
  570.                          }
  571.                          here->doormask = D_ISOPEN;
  572.                          /* newsym(mtmp->mx, mtmp->my); */  /* done below */
  573.                          unblock_point(mtmp->mx, mtmp->my); /* vision */
  574.                      }
  575.                  } else if (here->doormask & (D_LOCKED | D_CLOSED)) {
  576.                      /* mfndpos guarantees this must be a doorbuster */
  577.                      if (btrapped) {
  578.                          here->doormask = D_NODOOR;
  579.                          newsym(mtmp->mx, mtmp->my);
  580.                          unblock_point(mtmp->mx, mtmp->my); /* vision */
  581.                          if (mb_trapped(mtmp))
  582.                              return 2;
  583.                      } else {
  584.                          if (flags.verbose) {
  585.                              if (observeit)
  586.                                  pline("%s smashes down a door.",
  587.                                        Monnam(mtmp));
  588.                              else if (canseeit)
  589.                                  You_see("a door crash open.");
  590.                              else if (!Deaf)
  591.                                  You_hear("a door crash open.");
  592.                          }
  593.                          if (here->doormask & D_LOCKED && !rn2(2))
  594.                              here->doormask = D_NODOOR;
  595.                          else
  596.                              here->doormask = D_BROKEN;
  597.                          /* newsym(mtmp->mx, mtmp->my); */  /* done below */
  598.                          unblock_point(mtmp->mx, mtmp->my); /* vision */
  599.                      }
  600.                      /* if it's a shop door, schedule repair */
  601.                      if (*in_rooms(mtmp->mx, mtmp->my, SHOPBASE))
  602.                          add_damage(mtmp->mx, mtmp->my, 0L);
  603.                  }
  604.              } else if (levl[mtmp->mx][mtmp->my].typ == IRONBARS) {
  605.                  if (may_dig(mtmp->mx, mtmp->my)
  606.                      && (dmgtype(ptr, AD_RUST) || dmgtype(ptr, AD_CORR))) {
  607.                      if (canseemon(mtmp))
  608.                          pline("%s eats through the iron bars.", Monnam(mtmp));
  609.                      dissolve_bars(mtmp->mx, mtmp->my);
  610.                      return 3;
  611.                  } else if (flags.verbose && canseemon(mtmp))
  612.                      Norep("%s %s %s the iron bars.", Monnam(mtmp),
  613.                            /* pluralization fakes verb conjugation */
  614.                            makeplural(locomotion(ptr, "pass")),
  615.                            passes_walls(ptr) ? "through" : "between");
  616.              }
  617.  
  618.              /* possibly dig */
  619.              if (can_tunnel && mdig_tunnel(mtmp))
  620.                  return 2; /* mon died (position already updated) */
  621.  
  622.              /* set also in domove(), hack.c */
  623.              if (u.uswallow && mtmp == u.ustuck
  624.                  && (mtmp->mx != omx || mtmp->my != omy)) {
  625.                  /* If the monster moved, then update */
  626.                  u.ux0 = u.ux;
  627.                  u.uy0 = u.uy;
  628.                  u.ux = mtmp->mx;
  629.                  u.uy = mtmp->my;
  630.                  swallowed(0);
  631.              } else
  632.                  newsym(mtmp->mx, mtmp->my);
  633.          }
  634.          if (OBJ_AT(mtmp->mx, mtmp->my) && mtmp->mcanmove) {
  635.              /* recompute the likes tests, in case we polymorphed
  636.               * or if the "likegold" case got taken above */
  637.              if (setlikes) {
  638.                  int pctload = (curr_mon_load(mtmp) * 100) / max_mon_load(mtmp);
  639.  
  640.                  /* look for gold or jewels nearby */
  641.                  likegold = (likes_gold(ptr) && pctload < 95);
  642.                  likegems = (likes_gems(ptr) && pctload < 85);
  643.                  uses_items =
  644.                      (!mindless(ptr) && !is_animal(ptr) && pctload < 75);
  645.                  likeobjs = (likes_objs(ptr) && pctload < 75);
  646.                  likemagic = (likes_magic(ptr) && pctload < 85);
  647.                  likerock = (throws_rocks(ptr) && pctload < 50 && !Sokoban);
  648.                  conceals = hides_under(ptr);
  649.              }
  650.  
  651.              /* Maybe a rock mole just ate some metal object */
  652.              if (metallivorous(ptr)) {
  653.                  if (meatmetal(mtmp) == 2)
  654.                      return 2; /* it died */
  655.              }
  656.  
  657.              if (g_at(mtmp->mx, mtmp->my) && likegold)
  658.                  mpickgold(mtmp);
  659.  
  660.              /* Maybe a cube ate just about anything */
  661.              if (ptr == &mons[PM_GELATINOUS_CUBE]) {
  662.                  if (meatobj(mtmp) == 2)
  663.                      return 2; /* it died */
  664.              }
  665.  
  666.              if (!*in_rooms(mtmp->mx, mtmp->my, SHOPBASE) || !rn2(25)) {
  667.                  boolean picked = FALSE;
  668.  
  669.                  if (likeobjs)
  670.                      picked |= mpickstuff(mtmp, practical);
  671.                  if (likemagic)
  672.                      picked |= mpickstuff(mtmp, magical);
  673.                  if (likerock)
  674.                      picked |= mpickstuff(mtmp, boulder_class);
  675.                  if (likegems)
  676.                      picked |= mpickstuff(mtmp, gem_class);
  677.                  if (uses_items)
  678.                      picked |= mpickstuff(mtmp, (char *) 0);
  679.                  if (picked)
  680.                      mmoved = 3;
  681.              }
  682.  
  683.              if (mtmp->minvis) {
  684.                  newsym(mtmp->mx, mtmp->my);
  685.                  if (mtmp->wormno)
  686.                      see_wsegs(mtmp);
  687.              }
  688.          }
  689.  
  690.          if (hides_under(ptr) || ptr->mlet == S_EEL) {
  691.              /* Always set--or reset--mundetected if it's already hidden
  692.                 (just in case the object it was hiding under went away);
  693.                 usually set mundetected unless monster can't move.  */
  694.              if (mtmp->mundetected
  695.                  || (mtmp->mcanmove && !mtmp->msleeping && rn2(5)))
  696.                  (void) hideunder(mtmp);
  697.              newsym(mtmp->mx, mtmp->my);
  698.          }
  699.          if (mtmp->isshk) {
  700.              after_shk_move(mtmp);
  701.          }
  702.      }
  703.      return mmoved;
  704.  }
  705.  

dissolve_bars

  1.  void
  2.  dissolve_bars(x, y)
  3.  register int x, y;
  4.  {
  5.      levl[x][y].typ = (Is_special(&u.uz) || *in_rooms(x, y, 0)) ? ROOM : CORR;
  6.      newsym(x, y);
  7.  }
  8.  

closed_door

  1.  boolean
  2.  closed_door(x, y)
  3.  register int x, y;
  4.  {
  5.      return (boolean) (IS_DOOR(levl[x][y].typ)
  6.                        && (levl[x][y].doormask & (D_LOCKED | D_CLOSED)));
  7.  }
  8.  

accessible

  1.  boolean
  2.  accessible(x, y)
  3.  register int x, y;
  4.  {
  5.      int levtyp = levl[x][y].typ;
  6.  
  7.      /* use underlying terrain in front of closed drawbridge */
  8.      if (levtyp == DRAWBRIDGE_UP)
  9.          levtyp = db_under_typ(levl[x][y].drawbridgemask);
  10.  
  11.      return (boolean) (ACCESSIBLE(levtyp) && !closed_door(x, y));
  12.  }
  13.  

set_apparxy

  1.  /* decide where the monster thinks you are standing */
  2.  void
  3.  set_apparxy(mtmp)
  4.  register struct monst *mtmp;
  5.  {
  6.      boolean notseen, gotu;
  7.      register int disp, mx = mtmp->mux, my = mtmp->muy;
  8.      long umoney = money_cnt(invent);
  9.  
  10.      /*
  11.       * do cheapest and/or most likely tests first
  12.       */
  13.  
  14.      /* pet knows your smell; grabber still has hold of you */
  15.      if (mtmp->mtame || mtmp == u.ustuck)
  16.          goto found_you;
  17.  
  18.      /* monsters which know where you are don't suddenly forget,
  19.         if you haven't moved away */
  20.      if (mx == u.ux && my == u.uy)
  21.          goto found_you;
  22.  
  23.      notseen = (!mtmp->mcansee || (Invis && !perceives(mtmp->data)));
  24.      /* add cases as required.  eg. Displacement ... */
  25.      if (notseen || Underwater) {
  26.          /* Xorns can smell quantities of valuable metal
  27.              like that in solid gold coins, treat as seen */
  28.          if ((mtmp->data == &mons[PM_XORN]) && umoney && !Underwater)
  29.              disp = 0;
  30.          else
  31.              disp = 1;
  32.      } else if (Displaced) {
  33.          disp = couldsee(mx, my) ? 2 : 1;
  34.      } else
  35.          disp = 0;
  36.      if (!disp)
  37.          goto found_you;
  38.  
  39.      /* without something like the following, invisibility and displacement
  40.         are too powerful */
  41.      gotu = notseen ? !rn2(3) : Displaced ? !rn2(4) : FALSE;
  42.  
  43.      if (!gotu) {
  44.          register int try_cnt = 0;
  45.          do {
  46.              if (++try_cnt > 200)
  47.                  goto found_you; /* punt */
  48.              mx = u.ux - disp + rn2(2 * disp + 1);
  49.              my = u.uy - disp + rn2(2 * disp + 1);
  50.          } while (!isok(mx, my)
  51.                   || (disp != 2 && mx == mtmp->mx && my == mtmp->my)
  52.                   || ((mx != u.ux || my != u.uy) && !passes_walls(mtmp->data)
  53.                       && !(accessible(mx, my)
  54.                            || (closed_door(mx, my)
  55.                                && (can_ooze(mtmp) || can_fog(mtmp)))))
  56.                   || !couldsee(mx, my));
  57.      } else {
  58.      found_you:
  59.          mx = u.ux;
  60.          my = u.uy;
  61.      }
  62.  
  63.      mtmp->mux = mx;
  64.      mtmp->muy = my;
  65.  }
  66.  

undesirable_disp

  1.  /*
  2.   * mon-to-mon displacement is a deliberate "get out of my way" act,
  3.   * not an accidental bump, so we don't consider mstun or mconf in
  4.   * undesired_disp().
  5.   *
  6.   * We do consider many other things about the target and its
  7.   * location however.
  8.   */
  9.  boolean
  10.  undesirable_disp(mtmp, x, y)
  11.  struct monst *mtmp;
  12.  xchar x, y;
  13.  {
  14.      boolean is_pet = (mtmp && mtmp->mtame && !mtmp->isminion);
  15.      struct trap *trap = t_at(x, y);
  16.  
  17.      if (is_pet) {
  18.          /* Pets avoid a trap if you've seen it usually. */
  19.          if (trap && trap->tseen && rn2(40))
  20.              return TRUE;
  21.          /* Pets avoid cursed locations */
  22.          if (cursed_object_at(x, y))
  23.              return TRUE;
  24.  
  25.      /* Monsters avoid a trap if they've seen that type before */
  26.      } else if (trap && rn2(40)
  27.                 && (mtmp->mtrapseen & (1 << (trap->ttyp - 1))) != 0) {
  28.          return TRUE;
  29.      }
  30.  
  31.      return FALSE;
  32.  }
  33.  

stuff_prevents_passage

  1.  /*
  2.   * Inventory prevents passage under door.
  3.   * Used by can_ooze() and can_fog().
  4.   */
  5.  STATIC_OVL boolean
  6.  stuff_prevents_passage(mtmp)
  7.  struct monst *mtmp;
  8.  {
  9.      struct obj *chain, *obj;
  10.  
  11.      if (mtmp == &youmonst) {
  12.          chain = invent;
  13.      } else {
  14.          chain = mtmp->minvent;
  15.      }
  16.      for (obj = chain; obj; obj = obj->nobj) {
  17.          int typ = obj->otyp;
  18.  
  19.          if (typ == COIN_CLASS && obj->quan > 100L)
  20.              return TRUE;
  21.          if (obj->oclass != GEM_CLASS && !(typ >= ARROW && typ <= BOOMERANG)
  22.              && !(typ >= DAGGER && typ <= CRYSKNIFE) && typ != SLING
  23.              && !is_cloak(obj) && typ != FEDORA && !is_gloves(obj)
  24.              && typ != LEATHER_JACKET && typ != CREDIT_CARD && !is_shirt(obj)
  25.              && !(typ == CORPSE && verysmall(&mons[obj->corpsenm]))
  26.              && typ != FORTUNE_COOKIE && typ != CANDY_BAR && typ != PANCAKE
  27.              && typ != LEMBAS_WAFER && typ != LUMP_OF_ROYAL_JELLY
  28.              && obj->oclass != AMULET_CLASS && obj->oclass != RING_CLASS
  29.              && obj->oclass != VENOM_CLASS && typ != SACK
  30.              && typ != BAG_OF_HOLDING && typ != BAG_OF_TRICKS
  31.              && !Is_candle(obj) && typ != OILSKIN_SACK && typ != LEASH
  32.              && typ != STETHOSCOPE && typ != BLINDFOLD && typ != TOWEL
  33.              && typ != TIN_WHISTLE && typ != MAGIC_WHISTLE
  34.              && typ != MAGIC_MARKER && typ != TIN_OPENER && typ != SKELETON_KEY
  35.              && typ != LOCK_PICK)
  36.              return TRUE;
  37.          if (Is_container(obj) && obj->cobj)
  38.              return TRUE;
  39.      }
  40.      return FALSE;
  41.  }
  42.  

can_ooze

  1.  boolean
  2.  can_ooze(mtmp)
  3.  struct monst *mtmp;
  4.  {
  5.      if (!amorphous(mtmp->data) || stuff_prevents_passage(mtmp))
  6.          return FALSE;
  7.      return TRUE;
  8.  }
  9.  

can_fog

  1.  /* monster can change form into a fog if necessary */
  2.  boolean
  3.  can_fog(mtmp)
  4.  struct monst *mtmp;
  5.  {
  6.      if ((is_vampshifter(mtmp) || mtmp->data->mlet == S_VAMPIRE)
  7.          && !Protection_from_shape_changers && !stuff_prevents_passage(mtmp))
  8.          return TRUE;
  9.      return FALSE;
  10.  }
  11.  

vamp_shift

  1.  STATIC_OVL int
  2.  vamp_shift(mon, ptr)
  3.  struct monst *mon;
  4.  struct permonst *ptr;
  5.  {
  6.      int reslt = 0;
  7.  
  8.      if (mon->cham >= LOW_PM) {
  9.          if (ptr == &mons[mon->cham])
  10.              mon->cham = NON_PM;
  11.          reslt = newcham(mon, ptr, FALSE, FALSE);
  12.      } else if (mon->cham == NON_PM && ptr != mon->data) {
  13.          mon->cham = monsndx(mon->data);
  14.          reslt = newcham(mon, ptr, FALSE, FALSE);
  15.      }
  16.      return reslt;
  17.  }
  18.  
  19.  /*monmove.c*/