Source:NetHack 3.6.6/src/monmove.c

From NetHackWiki
Jump to navigation Jump to search

Below is the full text to monmove.c from the source code of NetHack 3.6.6. To link to a particular line, write [[Source:NetHack 3.6.6/src/monmove.c#line12]], 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.

1. /* NetHack 3.6	monmove.c	$NHDT-Date: 1575245074 2019/12/02 00:04:34 $  $NHDT-Branch: NetHack-3.6 $:$NHDT-Revision: 1.116 $ */
2. /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
3. /*-Copyright (c) Michael Allison, 2006. */
4. /* NetHack may be freely redistributed.  See license for details. */
5. 
6. #include "hack.h"
7. #include "mfndpos.h"
8. #include "artifact.h"
9. 
10. extern boolean notonhead;
11. 
12. STATIC_DCL void FDECL(watch_on_duty, (struct monst *));
13. STATIC_DCL int FDECL(disturb, (struct monst *));
14. STATIC_DCL void FDECL(release_hero, (struct monst *));
15. STATIC_DCL void FDECL(distfleeck, (struct monst *, int *, int *, int *));
16. STATIC_DCL int FDECL(m_arrival, (struct monst *));
17. STATIC_DCL boolean FDECL(stuff_prevents_passage, (struct monst *));
18. STATIC_DCL int FDECL(vamp_shift, (struct monst *, struct permonst *,
19.                                   BOOLEAN_P));
20. 
21. /* True if mtmp died */
22. boolean
23. mb_trapped(mtmp)
24. struct monst *mtmp;
25. {
26.     if (flags.verbose) {
27.         if (cansee(mtmp->mx, mtmp->my) && !Unaware)
28.             pline("KABOOM!!  You see a door explode.");
29.         else if (!Deaf)
30.             You_hear("a distant explosion.");
31.     }
32.     wake_nearto(mtmp->mx, mtmp->my, 7 * 7);
33.     mtmp->mstun = 1;
34.     mtmp->mhp -= rnd(15);
35.     if (DEADMONSTER(mtmp)) {
36.         mondied(mtmp);
37.         if (!DEADMONSTER(mtmp)) /* lifesaved */
38.             return FALSE;
39.         else
40.             return TRUE;
41.     }
42.     return FALSE;
43. }
44. 
45. /* check whether a monster is carrying a locking/unlocking tool */
46. boolean
47. monhaskey(mon, for_unlocking)
48. struct monst *mon;
49. boolean for_unlocking; /* true => credit card ok, false => not ok */
50. {
51.     if (for_unlocking && m_carrying(mon, CREDIT_CARD))
52.         return TRUE;
53.     return m_carrying(mon, SKELETON_KEY) || m_carrying(mon, LOCK_PICK);
54. }
55. 
56. void
57. mon_yells(mon, shout)
58. struct monst *mon;
59. const char *shout;
60. {
61.     if (Deaf) {
62.         if (canspotmon(mon))
63.             /* Sidenote on "A watchman angrily waves her arms!"
64.              * Female being called watchman is correct (career name).
65.              */
66.             pline("%s angrily %s %s %s!",
67.                 Amonnam(mon),
68.                 nolimbs(mon->data) ? "shakes" : "waves",
69.                 mhis(mon),
70.                 nolimbs(mon->data) ? mbodypart(mon, HEAD)
71.                                    : makeplural(mbodypart(mon, ARM)));
72.     } else {
73.         if (canspotmon(mon))
74.             pline("%s yells:", Amonnam(mon));
75.         else
76.             You_hear("someone yell:");
77.         verbalize1(shout);
78.     }
79. }
80. 
81. STATIC_OVL void
82. watch_on_duty(mtmp)
83. register struct monst *mtmp;
84. {
85.     int x, y;
86. 
87.     if (mtmp->mpeaceful && in_town(u.ux + u.dx, u.uy + u.dy)
88.         && mtmp->mcansee && m_canseeu(mtmp) && !rn2(3)) {
89.         if (picking_lock(&x, &y) && IS_DOOR(levl[x][y].typ)
90.             && (levl[x][y].doormask & D_LOCKED)) {
91.             if (couldsee(mtmp->mx, mtmp->my)) {
92.                 if (levl[x][y].looted & D_WARNED) {
93.                     mon_yells(mtmp, "Halt, thief!  You're under arrest!");
94.                     (void) angry_guards(!!Deaf);
95.                 } else {
96.                     mon_yells(mtmp, "Hey, stop picking that lock!");
97.                     levl[x][y].looted |= D_WARNED;
98.                 }
99.                 stop_occupation();
100.             }
101.         } else if (is_digging()) {
102.             /* chewing, wand/spell of digging are checked elsewhere */
103.             watch_dig(mtmp, context.digging.pos.x, context.digging.pos.y,
104.                       FALSE);
105.         }
106.     }
107. }
108. 
109. int
110. dochugw(mtmp)
111. register struct monst *mtmp;
112. {
113.     int x = mtmp->mx, y = mtmp->my;
114.     boolean already_saw_mon = !occupation ? 0 : canspotmon(mtmp);
115.     int rd = dochug(mtmp);
116. 
117.     /* a similar check is in monster_nearby() in hack.c */
118.     /* check whether hero notices monster and stops current activity */
119.     if (occupation && !rd && !Confusion && (!mtmp->mpeaceful || Hallucination)
120.         /* it's close enough to be a threat */
121.         && distu(x, y) <= (BOLT_LIM + 1) * (BOLT_LIM + 1)
122.         /* and either couldn't see it before, or it was too far away */
123.         && (!already_saw_mon || !couldsee(x, y)
124.             || distu(x, y) > (BOLT_LIM + 1) * (BOLT_LIM + 1))
125.         /* can see it now, or sense it and would normally see it */
126.         && (canseemon(mtmp) || (sensemon(mtmp) && couldsee(x, y)))
127.         && mtmp->mcanmove && !noattacks(mtmp->data)
128.         && !onscary(u.ux, u.uy, mtmp))
129.         stop_occupation();
130. 
131.     return rd;
132. }
133. 
134. boolean
135. onscary(x, y, mtmp)
136. int x, y;
137. struct monst *mtmp;
138. {
139.     /* creatures who are directly resistant to magical scaring:
140.      * Rodney, lawful minions, Angels, the Riders, shopkeepers
141.      * inside their own shop, priests inside their own temple */
142.     if (mtmp->iswiz || is_lminion(mtmp) || mtmp->data == &mons[PM_ANGEL]
143.         || is_rider(mtmp->data)
144.         || (mtmp->isshk && inhishop(mtmp))
145.         || (mtmp->ispriest && inhistemple(mtmp)))
146.         return FALSE;
147. 
148.     /* <0,0> is used by musical scaring to check for the above;
149.      * it doesn't care about scrolls or engravings or dungeon branch */
150.     if (x == 0 && y == 0)
151.         return TRUE;
152. 
153.     /* should this still be true for defiled/molochian altars? */
154.     if (IS_ALTAR(levl[x][y].typ)
155.         && (mtmp->data->mlet == S_VAMPIRE || is_vampshifter(mtmp)))
156.         return TRUE;
157. 
158.     /* the scare monster scroll doesn't have any of the below
159.      * restrictions, being its own source of power */
160.     if (sobj_at(SCR_SCARE_MONSTER, x, y))
161.         return TRUE;
162. 
163.     /*
164.      * Creatures who don't (or can't) fear a written Elbereth:
165.      * all the above plus shopkeepers (even if poly'd into non-human),
166.      * vault guards (also even if poly'd), blind or peaceful monsters,
167.      * humans and elves, and minotaurs.
168.      *
169.      * If the player isn't actually on the square OR the player's image
170.      * isn't displaced to the square, no protection is being granted.
171.      *
172.      * Elbereth doesn't work in Gehennom, the Elemental Planes, or the
173.      * Astral Plane; the influence of the Valar only reaches so far.
174.      */
175.     return (sengr_at("Elbereth", x, y, TRUE)
176.             && ((u.ux == x && u.uy == y)
177.                 || (Displaced && mtmp->mux == x && mtmp->muy == y))
178.             && !(mtmp->isshk || mtmp->isgd || !mtmp->mcansee
179.                  || mtmp->mpeaceful || mtmp->data->mlet == S_HUMAN
180.                  || mtmp->data == &mons[PM_MINOTAUR]
181.                  || Inhell || In_endgame(&u.uz)));
182. }
183. 
184. 
185. /* regenerate lost hit points */
186. void
187. mon_regen(mon, digest_meal)
188. struct monst *mon;
189. boolean digest_meal;
190. {
191.     if (mon->mhp < mon->mhpmax && (moves % 20 == 0 || regenerates(mon->data)))
192.         mon->mhp++;
193.     if (mon->mspec_used)
194.         mon->mspec_used--;
195.     if (digest_meal) {
196.         if (mon->meating) {
197.             mon->meating--;
198.             if (mon->meating <= 0)
199.                 finish_meating(mon);
200.         }
201.     }
202. }
203. 
204. /*
205.  * Possibly awaken the given monster.  Return a 1 if the monster has been
206.  * jolted awake.
207.  */
208. STATIC_OVL int
209. disturb(mtmp)
210. register struct monst *mtmp;
211. {
212.     /*
213.      * + Ettins are hard to surprise.
214.      * + Nymphs, jabberwocks, and leprechauns do not easily wake up.
215.      *
216.      * Wake up if:
217.      *  in direct LOS                                           AND
218.      *  within 10 squares                                       AND
219.      *  not stealthy or (mon is an ettin and 9/10)              AND
220.      *  (mon is not a nymph, jabberwock, or leprechaun) or 1/50 AND
221.      *  Aggravate or mon is (dog or human) or
222.      *      (1/7 and mon is not mimicing furniture or object)
223.      */
224.     if (couldsee(mtmp->mx, mtmp->my) && distu(mtmp->mx, mtmp->my) <= 100
225.         && (!Stealth || (mtmp->data == &mons[PM_ETTIN] && rn2(10)))
226.         && (!(mtmp->data->mlet == S_NYMPH
227.               || mtmp->data == &mons[PM_JABBERWOCK]
228. #if 0 /* DEFERRED */
229.               || mtmp->data == &mons[PM_VORPAL_JABBERWOCK]
230. #endif
231.               || mtmp->data->mlet == S_LEPRECHAUN) || !rn2(50))
232.         && (Aggravate_monster
233.             || (mtmp->data->mlet == S_DOG || mtmp->data->mlet == S_HUMAN)
234.             || (!rn2(7) && M_AP_TYPE(mtmp) != M_AP_FURNITURE
235.                 && M_AP_TYPE(mtmp) != M_AP_OBJECT))) {
236.         mtmp->msleeping = 0;
237.         return 1;
238.     }
239.     return 0;
240. }
241. 
242. /* ungrab/expel held/swallowed hero */
243. STATIC_OVL void
244. release_hero(mon)
245. struct monst *mon;
246. {
247.     if (mon == u.ustuck) {
248.         if (u.uswallow) {
249.             expels(mon, mon->data, TRUE);
250.         } else if (!sticks(youmonst.data)) {
251.             unstuck(mon); /* let go */
252.             You("get released!");
253.         }
254.     }
255. }
256. 
257. #define flees_light(mon) ((mon)->data == &mons[PM_GREMLIN]     \
258.                           && (uwep && artifact_light(uwep) && uwep->lamplit))
259. /* we could include this in the above macro, but probably overkill/overhead */
260. /*      && (!(which_armor((mon), W_ARMC) != 0                               */
261. /*            && which_armor((mon), W_ARMH) != 0))                          */
262. 
263. /* monster begins fleeing for the specified time, 0 means untimed flee
264.  * if first, only adds fleetime if monster isn't already fleeing
265.  * if fleemsg, prints a message about new flight, otherwise, caller should */
266. void
267. monflee(mtmp, fleetime, first, fleemsg)
268. struct monst *mtmp;
269. int fleetime;
270. boolean first;
271. boolean fleemsg;
272. {
273.     /* shouldn't happen; maybe warrants impossible()? */
274.     if (DEADMONSTER(mtmp))
275.         return;
276. 
277.     if (mtmp == u.ustuck)
278.         release_hero(mtmp); /* expels/unstuck */
279. 
280.     if (!first || !mtmp->mflee) {
281.         /* don't lose untimed scare */
282.         if (!fleetime)
283.             mtmp->mfleetim = 0;
284.         else if (!mtmp->mflee || mtmp->mfleetim) {
285.             fleetime += (int) mtmp->mfleetim;
286.             /* ensure monster flees long enough to visibly stop fighting */
287.             if (fleetime == 1)
288.                 fleetime++;
289.             mtmp->mfleetim = (unsigned) min(fleetime, 127);
290.         }
291.         if (!mtmp->mflee && fleemsg && canseemon(mtmp)
292.             && M_AP_TYPE(mtmp) != M_AP_FURNITURE
293.             && M_AP_TYPE(mtmp) != M_AP_OBJECT) {
294.             /* unfortunately we can't distinguish between temporary
295.                sleep and temporary paralysis, so both conditions
296.                receive the same alternate message */
297.             if (!mtmp->mcanmove || !mtmp->data->mmove) {
298.                 pline("%s seems to flinch.", Adjmonnam(mtmp, "immobile"));
299.             } else if (flees_light(mtmp)) {
300.                 if (rn2(10) || Deaf)
301.                     pline("%s flees from the painful light of %s.",
302.                           Monnam(mtmp), bare_artifactname(uwep));
303.                 else
304.                     verbalize("Bright light!");
305.             } else
306.                 pline("%s turns to flee.", Monnam(mtmp));
307.         }
308.         mtmp->mflee = 1;
309.     }
310.     /* ignore recently-stepped spaces when made to flee */
311.     memset(mtmp->mtrack, 0, sizeof(mtmp->mtrack));
312. }
313. 
314. STATIC_OVL void
315. distfleeck(mtmp, inrange, nearby, scared)
316. register struct monst *mtmp;
317. int *inrange, *nearby, *scared;
318. {
319.     int seescaryx, seescaryy;
320.     boolean sawscary = FALSE, bravegremlin = (rn2(5) == 0);
321. 
322.     *inrange = (dist2(mtmp->mx, mtmp->my, mtmp->mux, mtmp->muy)
323.                 <= (BOLT_LIM * BOLT_LIM));
324.     *nearby = *inrange && monnear(mtmp, mtmp->mux, mtmp->muy);
325. 
326.     /* Note: if your image is displaced, the monster sees the Elbereth
327.      * at your displaced position, thus never attacking your displaced
328.      * position, but possibly attacking you by accident.  If you are
329.      * invisible, it sees the Elbereth at your real position, thus never
330.      * running into you by accident but possibly attacking the spot
331.      * where it guesses you are.
332.      */
333.     if (!mtmp->mcansee || (Invis && !perceives(mtmp->data))) {
334.         seescaryx = mtmp->mux;
335.         seescaryy = mtmp->muy;
336.     } else {
337.         seescaryx = u.ux;
338.         seescaryy = u.uy;
339.     }
340. 
341.     sawscary = onscary(seescaryx, seescaryy, mtmp);
342.     if (*nearby && (sawscary
343.                     || (flees_light(mtmp) && !bravegremlin)
344.                     || (!mtmp->mpeaceful && in_your_sanctuary(mtmp, 0, 0)))) {
345.         *scared = 1;
346.         monflee(mtmp, rnd(rn2(7) ? 10 : 100), TRUE, TRUE);
347.     } else
348.         *scared = 0;
349. }
350. 
351. #undef flees_light
352. 
353. /* perform a special one-time action for a monster; returns -1 if nothing
354.    special happened, 0 if monster uses up its turn, 1 if monster is killed */
355. STATIC_OVL int
356. m_arrival(mon)
357. struct monst *mon;
358. {
359.     mon->mstrategy &= ~STRAT_ARRIVE; /* always reset */
360. 
361.     return -1;
362. }
363. 
364. /* returns 1 if monster died moving, 0 otherwise */
365. /* The whole dochugw/m_move/distfleeck/mfndpos section is serious spaghetti
366.  * code. --KAA
367.  */
368. int
369. dochug(mtmp)
370. register struct monst *mtmp;
371. {
372.     register struct permonst *mdat;
373.     register int tmp = 0;
374.     int inrange, nearby, scared;
375. 
376.     /*  Pre-movement adjustments
377.      */
378. 
379.     mdat = mtmp->data;
380. 
381.     if (mtmp->mstrategy & STRAT_ARRIVE) {
382.         int res = m_arrival(mtmp);
383.         if (res >= 0)
384.             return res;
385.     }
386. 
387.     /* check for waitmask status change */
388.     if ((mtmp->mstrategy & STRAT_WAITFORU)
389.         && (m_canseeu(mtmp) || mtmp->mhp < mtmp->mhpmax))
390.         mtmp->mstrategy &= ~STRAT_WAITFORU;
391. 
392.     /* update quest status flags */
393.     quest_stat_check(mtmp);
394. 
395.     if (!mtmp->mcanmove || (mtmp->mstrategy & STRAT_WAITMASK)) {
396.         if (Hallucination)
397.             newsym(mtmp->mx, mtmp->my);
398.         if (mtmp->mcanmove && (mtmp->mstrategy & STRAT_CLOSE)
399.             && !mtmp->msleeping && monnear(mtmp, u.ux, u.uy))
400.             quest_talk(mtmp); /* give the leaders a chance to speak */
401.         return 0;             /* other frozen monsters can't do anything */
402.     }
403. 
404.     /* there is a chance we will wake it */
405.     if (mtmp->msleeping && !disturb(mtmp)) {
406.         if (Hallucination)
407.             newsym(mtmp->mx, mtmp->my);
408.         return 0;
409.     }
410. 
411.     /* not frozen or sleeping: wipe out texts written in the dust */
412.     wipe_engr_at(mtmp->mx, mtmp->my, 1, FALSE);
413. 
414.     /* confused monsters get unconfused with small probability */
415.     if (mtmp->mconf && !rn2(50))
416.         mtmp->mconf = 0;
417. 
418.     /* stunned monsters get un-stunned with larger probability */
419.     if (mtmp->mstun && !rn2(10))
420.         mtmp->mstun = 0;
421. 
422.     /* some monsters teleport */
423.     if (mtmp->mflee && !rn2(40) && can_teleport(mdat) && !mtmp->iswiz
424.         && !level.flags.noteleport) {
425.         (void) rloc(mtmp, TRUE);
426.         return 0;
427.     }
428.     if (mdat->msound == MS_SHRIEK && !um_dist(mtmp->mx, mtmp->my, 1))
429.         m_respond(mtmp);
430.     if (mdat == &mons[PM_MEDUSA] && couldsee(mtmp->mx, mtmp->my))
431.         m_respond(mtmp);
432.     if (DEADMONSTER(mtmp))
433.         return 1; /* m_respond gaze can kill medusa */
434. 
435.     /* fleeing monsters might regain courage */
436.     if (mtmp->mflee && !mtmp->mfleetim && mtmp->mhp == mtmp->mhpmax
437.         && !rn2(25))
438.         mtmp->mflee = 0;
439. 
440.     /* cease conflict-induced swallow/grab if conflict has ended */
441.     if (mtmp == u.ustuck && mtmp->mpeaceful && !mtmp->mconf && !Conflict) {
442.         release_hero(mtmp);
443.         return 0; /* uses up monster's turn */
444.     }
445. 
446.     set_apparxy(mtmp);
447.     /* Must be done after you move and before the monster does.  The
448.      * set_apparxy() call in m_move() doesn't suffice since the variables
449.      * inrange, etc. all depend on stuff set by set_apparxy().
450.      */
451. 
452.     /* Monsters that want to acquire things */
453.     /* may teleport, so do it before inrange is set */
454.     if (is_covetous(mdat))
455.         (void) tactics(mtmp);
456. 
457.     /* check distance and scariness of attacks */
458.     distfleeck(mtmp, &inrange, &nearby, &scared);
459. 
460.     if (find_defensive(mtmp)) {
461.         if (use_defensive(mtmp) != 0)
462.             return 1;
463.     } else if (find_misc(mtmp)) {
464.         if (use_misc(mtmp) != 0)
465.             return 1;
466.     }
467. 
468.     /* Demonic Blackmail! */
469.     if (nearby && mdat->msound == MS_BRIBE && mtmp->mpeaceful && !mtmp->mtame
470.         && !u.uswallow) {
471.         if (mtmp->mux != u.ux || mtmp->muy != u.uy) {
472.             pline("%s whispers at thin air.",
473.                   cansee(mtmp->mux, mtmp->muy) ? Monnam(mtmp) : "It");
474. 
475.             if (is_demon(youmonst.data)) {
476.                 /* "Good hunting, brother" */
477.                 if (!tele_restrict(mtmp))
478.                     (void) rloc(mtmp, TRUE);
479.             } else {
480.                 mtmp->minvis = mtmp->perminvis = 0;
481.                 /* Why?  For the same reason in real demon talk */
482.                 pline("%s gets angry!", Amonnam(mtmp));
483.                 mtmp->mpeaceful = 0;
484.                 set_malign(mtmp);
485.                 /* since no way is an image going to pay it off */
486.             }
487.         } else if (demon_talk(mtmp))
488.             return 1; /* you paid it off */
489.     }
490. 
491.     /* the watch will look around and see if you are up to no good :-) */
492.     if (is_watch(mdat)) {
493.         watch_on_duty(mtmp);
494. 
495.     } else if (is_mind_flayer(mdat) && !rn2(20)) {
496.         struct monst *m2, *nmon = (struct monst *) 0;
497. 
498.         if (canseemon(mtmp))
499.             pline("%s concentrates.", Monnam(mtmp));
500.         if (distu(mtmp->mx, mtmp->my) > BOLT_LIM * BOLT_LIM) {
501.             You("sense a faint wave of psychic energy.");
502.             goto toofar;
503.         }
504.         pline("A wave of psychic energy pours over you!");
505.         if (mtmp->mpeaceful
506.             && (!Conflict || resist(mtmp, RING_CLASS, 0, 0))) {
507.             pline("It feels quite soothing.");
508.         } else if (!u.uinvulnerable) {
509.             register boolean m_sen = sensemon(mtmp);
510. 
511.             if (m_sen || (Blind_telepat && rn2(2)) || !rn2(10)) {
512.                 int dmg;
513.                 pline("It locks on to your %s!",
514.                       m_sen ? "telepathy" : Blind_telepat ? "latent telepathy"
515.                                                           : "mind");
516.                 dmg = rnd(15);
517.                 if (Half_spell_damage)
518.                     dmg = (dmg + 1) / 2;
519.                 losehp(dmg, "psychic blast", KILLED_BY_AN);
520.             }
521.         }
522.         for (m2 = fmon; m2; m2 = nmon) {
523.             nmon = m2->nmon;
524.             if (DEADMONSTER(m2))
525.                 continue;
526.             if (m2->mpeaceful == mtmp->mpeaceful)
527.                 continue;
528.             if (mindless(m2->data))
529.                 continue;
530.             if (m2 == mtmp)
531.                 continue;
532.             if ((telepathic(m2->data) && (rn2(2) || m2->mblinded))
533.                 || !rn2(10)) {
534.                 if (cansee(m2->mx, m2->my))
535.                     pline("It locks on to %s.", mon_nam(m2));
536.                 m2->mhp -= rnd(15);
537.                 if (DEADMONSTER(m2))
538.                     monkilled(m2, "", AD_DRIN);
539.                 else
540.                     m2->msleeping = 0;
541.             }
542.         }
543.     }
544.  toofar:
545. 
546.     /* If monster is nearby you, and has to wield a weapon, do so.   This
547.      * costs the monster a move, of course.
548.      */
549.     if ((!mtmp->mpeaceful || Conflict) && inrange
550.         && dist2(mtmp->mx, mtmp->my, mtmp->mux, mtmp->muy) <= 8
551.         && attacktype(mdat, AT_WEAP)) {
552.         struct obj *mw_tmp;
553. 
554.         /* The scared check is necessary.  Otherwise a monster that is
555.          * one square near the player but fleeing into a wall would keep
556.          * switching between pick-axe and weapon.  If monster is stuck
557.          * in a trap, prefer ranged weapon (wielding is done in thrwmu).
558.          * This may cost the monster an attack, but keeps the monster
559.          * from switching back and forth if carrying both.
560.          */
561.         mw_tmp = MON_WEP(mtmp);
562.         if (!(scared && mw_tmp && is_pick(mw_tmp))
563.             && mtmp->weapon_check == NEED_WEAPON
564.             && !(mtmp->mtrapped && !nearby && select_rwep(mtmp))) {
565.             mtmp->weapon_check = NEED_HTH_WEAPON;
566.             if (mon_wield_item(mtmp) != 0)
567.                 return 0;
568.         }
569.     }
570. 
571.     /*  Now the actual movement phase
572.      */
573. 
574.     if (!nearby || mtmp->mflee || scared || mtmp->mconf || mtmp->mstun
575.         || (mtmp->minvis && !rn2(3))
576.         || (mdat->mlet == S_LEPRECHAUN && !findgold(invent)
577.             && (findgold(mtmp->minvent) || rn2(2)))
578.         || (is_wanderer(mdat) && !rn2(4)) || (Conflict && !mtmp->iswiz)
579.         || (!mtmp->mcansee && !rn2(4)) || mtmp->mpeaceful) {
580.         /* Possibly cast an undirected spell if not attacking you */
581.         /* note that most of the time castmu() will pick a directed
582.            spell and do nothing, so the monster moves normally */
583.         /* arbitrary distance restriction to keep monster far away
584.            from you from having cast dozens of sticks-to-snakes
585.            or similar spells by the time you reach it */
586.         if (dist2(mtmp->mx, mtmp->my, u.ux, u.uy) <= 49
587.             && !mtmp->mspec_used) {
588.             struct attack *a;
589. 
590.             for (a = &mdat->mattk[0]; a < &mdat->mattk[NATTK]; a++) {
591.                 if (a->aatyp == AT_MAGC
592.                     && (a->adtyp == AD_SPEL || a->adtyp == AD_CLRC)) {
593.                     if (castmu(mtmp, a, FALSE, FALSE)) {
594.                         tmp = 3;
595.                         break;
596.                     }
597.                 }
598.             }
599.         }
600. 
601.         tmp = m_move(mtmp, 0);
602.         if (tmp != 2)
603.             distfleeck(mtmp, &inrange, &nearby, &scared); /* recalc */
604. 
605.         switch (tmp) { /* for pets, cases 0 and 3 are equivalent */
606.         case 0: /* no movement, but it can still attack you */
607.         case 3: /* absolutely no movement */
608.             /* vault guard might have vanished */
609.             if (mtmp->isgd && (DEADMONSTER(mtmp) || mtmp->mx == 0))
610.                 return 1; /* behave as if it died */
611.             /* During hallucination, monster appearance should
612.              * still change - even if it doesn't move.
613.              */
614.             if (Hallucination)
615.                 newsym(mtmp->mx, mtmp->my);
616.             break;
617.         case 1: /* monster moved */
618.             /* Maybe it stepped on a trap and fell asleep... */
619.             if (mtmp->msleeping || !mtmp->mcanmove)
620.                 return 0;
621.             /* Monsters can move and then shoot on same turn;
622.                our hero can't.  Is that fair? */
623.             if (!nearby && (ranged_attk(mdat) || find_offensive(mtmp)))
624.                 break;
625.             /* engulfer/grabber checks */
626.             if (mtmp == u.ustuck) {
627.                 /* a monster that's digesting you can move at the
628.                  * same time -dlc
629.                  */
630.                 if (u.uswallow)
631.                     return mattacku(mtmp);
632.                 /* if confused grabber has wandered off, let go */
633.                 if (distu(mtmp->mx, mtmp->my) > 2)
634.                     unstuck(mtmp);
635.             }
636.             return 0;
637.         case 2: /* monster died */
638.             return 1;
639.         }
640.     }
641. 
642.     /*  Now, attack the player if possible - one attack set per monst
643.      */
644. 
645.     if (!mtmp->mpeaceful || (Conflict && !resist(mtmp, RING_CLASS, 0, 0))) {
646.         if (inrange && !noattacks(mdat)
647.             && (Upolyd ? u.mh : u.uhp) > 0 && !scared && tmp != 3)
648.             if (mattacku(mtmp))
649.                 return 1; /* monster died (e.g. exploded) */
650. 
651.         if (mtmp->wormno)
652.             wormhitu(mtmp);
653.     }
654.     /* special speeches for quest monsters */
655.     if (!mtmp->msleeping && mtmp->mcanmove && nearby)
656.         quest_talk(mtmp);
657.     /* extra emotional attack for vile monsters */
658.     if (inrange && mtmp->data->msound == MS_CUSS && !mtmp->mpeaceful
659.         && couldsee(mtmp->mx, mtmp->my) && !mtmp->minvis && !rn2(5))
660.         cuss(mtmp);
661. 
662.     return (tmp == 2);
663. }
664. 
665. static NEARDATA const char practical[] = { WEAPON_CLASS, ARMOR_CLASS,
666.                                            GEM_CLASS, FOOD_CLASS, 0 };
667. static NEARDATA const char magical[] = { AMULET_CLASS, POTION_CLASS,
668.                                          SCROLL_CLASS, WAND_CLASS,
669.                                          RING_CLASS,   SPBOOK_CLASS, 0 };
670. static NEARDATA const char indigestion[] = { BALL_CLASS, ROCK_CLASS, 0 };
671. static NEARDATA const char boulder_class[] = { ROCK_CLASS, 0 };
672. static NEARDATA const char gem_class[] = { GEM_CLASS, 0 };
673. 
674. boolean
675. itsstuck(mtmp)
676. register struct monst *mtmp;
677. {
678.     if (sticks(youmonst.data) && mtmp == u.ustuck && !u.uswallow) {
679.         pline("%s cannot escape from you!", Monnam(mtmp));
680.         return TRUE;
681.     }
682.     return FALSE;
683. }
684. 
685. /*
686.  * should_displace()
687.  *
688.  * Displacement of another monster is a last resort and only
689.  * used on approach. If there are better ways to get to target,
690.  * those should be used instead. This function does that evaluation.
691.  */
692. boolean
693. should_displace(mtmp, poss, info, cnt, gx, gy)
694. struct monst *mtmp;
695. coord *poss; /* coord poss[9] */
696. long *info;  /* long info[9] */
697. int cnt;
698. xchar gx, gy;
699. {
700.     int shortest_with_displacing = -1;
701.     int shortest_without_displacing = -1;
702.     int count_without_displacing = 0;
703.     register int i, nx, ny;
704.     int ndist;
705. 
706.     for (i = 0; i < cnt; i++) {
707.         nx = poss[i].x;
708.         ny = poss[i].y;
709.         ndist = dist2(nx, ny, gx, gy);
710.         if (MON_AT(nx, ny) && (info[i] & ALLOW_MDISP) && !(info[i] & ALLOW_M)
711.             && !undesirable_disp(mtmp, nx, ny)) {
712.             if (shortest_with_displacing == -1
713.                 || (ndist < shortest_with_displacing))
714.                 shortest_with_displacing = ndist;
715.         } else {
716.             if ((shortest_without_displacing == -1)
717.                 || (ndist < shortest_without_displacing))
718.                 shortest_without_displacing = ndist;
719.             count_without_displacing++;
720.         }
721.     }
722.     if (shortest_with_displacing > -1
723.         && (shortest_with_displacing < shortest_without_displacing
724.             || !count_without_displacing))
725.         return TRUE;
726.     return FALSE;
727. }
728. 
729. boolean
730. m_digweapon_check(mtmp, nix, niy)
731. struct monst *mtmp;
732. xchar nix,niy;
733. {
734.     boolean can_tunnel = 0;
735.     struct obj *mw_tmp = MON_WEP(mtmp);
736. 
737.     if (!Is_rogue_level(&u.uz))
738.         can_tunnel = tunnels(mtmp->data);
739. 
740.     if (can_tunnel && needspick(mtmp->data) && !mwelded(mw_tmp)
741.         && (may_dig(nix, niy) || closed_door(nix, niy))) {
742.         /* may_dig() is either IS_STWALL or IS_TREE */
743.         if (closed_door(nix, niy)) {
744.             if (!mw_tmp
745.                 || !is_pick(mw_tmp)
746.                 || !is_axe(mw_tmp))
747.                 mtmp->weapon_check = NEED_PICK_OR_AXE;
748.         } else if (IS_TREE(levl[nix][niy].typ)) {
749.             if (!(mw_tmp = MON_WEP(mtmp)) || !is_axe(mw_tmp))
750.                 mtmp->weapon_check = NEED_AXE;
751.         } else if (IS_STWALL(levl[nix][niy].typ)) {
752.             if (!(mw_tmp = MON_WEP(mtmp)) || !is_pick(mw_tmp))
753.                 mtmp->weapon_check = NEED_PICK_AXE;
754.         }
755.         if (mtmp->weapon_check >= NEED_PICK_AXE && mon_wield_item(mtmp))
756.             return TRUE;
757.     }
758.     return FALSE;
759. }
760. 
761. /* Return values:
762.  * 0: did not move, but can still attack and do other stuff.
763.  * 1: moved, possibly can attack.
764.  * 2: monster died.
765.  * 3: did not move, and can't do anything else either.
766.  */
767. int
768. m_move(mtmp, after)
769. register struct monst *mtmp;
770. register int after;
771. {
772.     register int appr;
773.     xchar gx, gy, nix, niy, chcnt;
774.     int chi; /* could be schar except for stupid Sun-2 compiler */
775.     boolean likegold = 0, likegems = 0, likeobjs = 0, likemagic = 0,
776.             conceals = 0;
777.     boolean likerock = 0, can_tunnel = 0;
778.     boolean can_open = 0, can_unlock = 0, doorbuster = 0;
779.     boolean uses_items = 0, setlikes = 0;
780.     boolean avoid = FALSE;
781.     boolean better_with_displacing = FALSE;
782.     boolean sawmon = canspotmon(mtmp); /* before it moved */
783.     struct permonst *ptr;
784.     struct monst *mtoo;
785.     schar mmoved = 0; /* not strictly nec.: chi >= 0 will do */
786.     long info[9];
787.     long flag;
788.     int omx = mtmp->mx, omy = mtmp->my;
789. 
790.     if (mtmp->mtrapped) {
791.         int i = mintrap(mtmp);
792. 
793.         if (i >= 2) {
794.             newsym(mtmp->mx, mtmp->my);
795.             return 2;
796.         } /* it died */
797.         if (i == 1)
798.             return 0; /* still in trap, so didn't move */
799.     }
800.     ptr = mtmp->data; /* mintrap() can change mtmp->data -dlc */
801. 
802.     if (mtmp->meating) {
803.         mtmp->meating--;
804.         if (mtmp->meating <= 0)
805.             finish_meating(mtmp);
806.         return 3; /* still eating */
807.     }
808.     if (hides_under(ptr) && OBJ_AT(mtmp->mx, mtmp->my) && rn2(10))
809.         return 0; /* do not leave hiding place */
810. 
811.     set_apparxy(mtmp);
812.     /* where does mtmp think you are? */
813.     /* Not necessary if m_move called from this file, but necessary in
814.      * other calls of m_move (ex. leprechauns dodging)
815.      */
816.     if (!Is_rogue_level(&u.uz))
817.         can_tunnel = tunnels(ptr);
818.     can_open = !(nohands(ptr) || verysmall(ptr));
819.     can_unlock =
820.         ((can_open && monhaskey(mtmp, TRUE)) || mtmp->iswiz || is_rider(ptr));
821.     doorbuster = is_giant(ptr);
822.     if (mtmp->wormno)
823.         goto not_special;
824.     /* my dog gets special treatment */
825.     if (mtmp->mtame) {
826.         mmoved = dog_move(mtmp, after);
827.         goto postmov;
828.     }
829. 
830.     /* likewise for shopkeeper */
831.     if (mtmp->isshk) {
832.         mmoved = shk_move(mtmp);
833.         if (mmoved == -2)
834.             return 2;
835.         if (mmoved >= 0)
836.             goto postmov;
837.         mmoved = 0; /* follow player outside shop */
838.     }
839. 
840.     /* and for the guard */
841.     if (mtmp->isgd) {
842.         mmoved = gd_move(mtmp);
843.         if (mmoved == -2)
844.             return 2;
845.         if (mmoved >= 0)
846.             goto postmov;
847.         mmoved = 0;
848.     }
849. 
850.     /* and the acquisitive monsters get special treatment */
851.     if (is_covetous(ptr)) {
852.         xchar tx = STRAT_GOALX(mtmp->mstrategy),
853.               ty = STRAT_GOALY(mtmp->mstrategy);
854.         struct monst *intruder = m_at(tx, ty);
855.         /*
856.          * if there's a monster on the object or in possession of it,
857.          * attack it.
858.          */
859.         if ((dist2(mtmp->mx, mtmp->my, tx, ty) < 2) && intruder
860.             && (intruder != mtmp)) {
861.             notonhead = (intruder->mx != tx || intruder->my != ty);
862.             if (mattackm(mtmp, intruder) == 2)
863.                 return 2;
864.             mmoved = 1;
865.         } else
866.             mmoved = 0;
867.         goto postmov;
868.     }
869. 
870.     /* and for the priest */
871.     if (mtmp->ispriest) {
872.         mmoved = pri_move(mtmp);
873.         if (mmoved == -2)
874.             return 2;
875.         if (mmoved >= 0)
876.             goto postmov;
877.         mmoved = 0;
878.     }
879. 
880. #ifdef MAIL
881.     if (ptr == &mons[PM_MAIL_DAEMON]) {
882.         if (!Deaf && canseemon(mtmp))
883.             verbalize("I'm late!");
884.         mongone(mtmp);
885.         return 2;
886.     }
887. #endif
888. 
889.     /* teleport if that lies in our nature */
890.     if (ptr == &mons[PM_TENGU] && !rn2(5) && !mtmp->mcan
891.         && !tele_restrict(mtmp)) {
892.         if (mtmp->mhp < 7 || mtmp->mpeaceful || rn2(2))
893.             (void) rloc(mtmp, TRUE);
894.         else
895.             mnexto(mtmp);
896.         mmoved = 1;
897.         goto postmov;
898.     }
899.  not_special:
900.     if (u.uswallow && !mtmp->mflee && u.ustuck != mtmp)
901.         return 1;
902.     omx = mtmp->mx;
903.     omy = mtmp->my;
904.     gx = mtmp->mux;
905.     gy = mtmp->muy;
906.     appr = mtmp->mflee ? -1 : 1;
907.     if (mtmp->mconf || (u.uswallow && mtmp == u.ustuck)) {
908.         appr = 0;
909.     } else {
910.         struct obj *lepgold, *ygold;
911.         boolean should_see = (couldsee(omx, omy)
912.                               && (levl[gx][gy].lit || !levl[omx][omy].lit)
913.                               && (dist2(omx, omy, gx, gy) <= 36));
914. 
915.         if (!mtmp->mcansee
916.             || (should_see && Invis && !perceives(ptr) && rn2(11))
917.             || is_obj_mappear(&youmonst,STRANGE_OBJECT) || u.uundetected
918.             || (is_obj_mappear(&youmonst,GOLD_PIECE) && !likes_gold(ptr))
919.             || (mtmp->mpeaceful && !mtmp->isshk) /* allow shks to follow */
920.             || ((monsndx(ptr) == PM_STALKER || ptr->mlet == S_BAT
921.                  || ptr->mlet == S_LIGHT) && !rn2(3)))
922.             appr = 0;
923. 
924.         if (monsndx(ptr) == PM_LEPRECHAUN && (appr == 1)
925.             && ((lepgold = findgold(mtmp->minvent))
926.                 && (lepgold->quan
927.                     > ((ygold = findgold(invent)) ? ygold->quan : 0L))))
928.             appr = -1;
929. 
930.         if (!should_see && can_track(ptr)) {
931.             register coord *cp;
932. 
933.             cp = gettrack(omx, omy);
934.             if (cp) {
935.                 gx = cp->x;
936.                 gy = cp->y;
937.             }
938.         }
939.     }
940. 
941.     if ((!mtmp->mpeaceful || !rn2(10)) && (!Is_rogue_level(&u.uz))) {
942.         boolean in_line = (lined_up(mtmp)
943.                && (distmin(mtmp->mx, mtmp->my, mtmp->mux, mtmp->muy)
944.                    <= (throws_rocks(youmonst.data) ? 20 : ACURRSTR / 2 + 1)));
945. 
946.         if (appr != 1 || !in_line) {
947.             /* Monsters in combat won't pick stuff up, avoiding the
948.              * situation where you toss arrows at it and it has nothing
949.              * better to do than pick the arrows up.
950.              */
951.             register int pctload =
952.                 (curr_mon_load(mtmp) * 100) / max_mon_load(mtmp);
953. 
954.             /* look for gold or jewels nearby */
955.             likegold = (likes_gold(ptr) && pctload < 95);
956.             likegems = (likes_gems(ptr) && pctload < 85);
957.             uses_items = (!mindless(ptr) && !is_animal(ptr) && pctload < 75);
958.             likeobjs = (likes_objs(ptr) && pctload < 75);
959.             likemagic = (likes_magic(ptr) && pctload < 85);
960.             likerock = (throws_rocks(ptr) && pctload < 50 && !Sokoban);
961.             conceals = hides_under(ptr);
962.             setlikes = TRUE;
963.         }
964.     }
965. 
966. #define SQSRCHRADIUS 5
967. 
968.     {
969.         register int minr = SQSRCHRADIUS; /* not too far away */
970.         register struct obj *otmp;
971.         register int xx, yy;
972.         int oomx, oomy, lmx, lmy;
973. 
974.         /* cut down the search radius if it thinks character is closer. */
975.         if (distmin(mtmp->mux, mtmp->muy, omx, omy) < SQSRCHRADIUS
976.             && !mtmp->mpeaceful)
977.             minr--;
978.         /* guards shouldn't get too distracted */
979.         if (!mtmp->mpeaceful && is_mercenary(ptr))
980.             minr = 1;
981. 
982.         if ((likegold || likegems || likeobjs || likemagic || likerock
983.              || conceals) && (!*in_rooms(omx, omy, SHOPBASE)
984.                               || (!rn2(25) && !mtmp->isshk))) {
985.  look_for_obj:
986.             oomx = min(COLNO - 1, omx + minr);
987.             oomy = min(ROWNO - 1, omy + minr);
988.             lmx = max(1, omx - minr);
989.             lmy = max(0, omy - minr);
990.             for (otmp = fobj; otmp; otmp = otmp->nobj) {
991.                 /* monsters may pick rocks up, but won't go out of their way
992.                    to grab them; this might hamper sling wielders, but it cuts
993.                    down on move overhead by filtering out most common item */
994.                 if (otmp->otyp == ROCK)
995.                     continue;
996.                 xx = otmp->ox;
997.                 yy = otmp->oy;
998.                 /* Nymphs take everything.  Most other creatures should not
999.                  * pick up corpses except as a special case like in
1000.                  * searches_for_item().  We need to do this check in
1001.                  * mpickstuff() as well.
1002.                  */
1003.                 if (xx >= lmx && xx <= oomx && yy >= lmy && yy <= oomy) {
1004.                     /* don't get stuck circling around object that's
1005.                        underneath an immobile or hidden monster;
1006.                        paralysis victims excluded */
1007.                     if ((mtoo = m_at(xx, yy)) != 0
1008.                         && (mtoo->msleeping || mtoo->mundetected
1009.                             || (mtoo->mappearance && !mtoo->iswiz)
1010.                             || !mtoo->data->mmove))
1011.                         continue;
1012.                     /* the mfndpos() test for whether to allow a move to a
1013.                        water location accepts flyers, but they can't reach
1014.                        underwater objects, so being able to move to a spot
1015.                        is insufficient for deciding whether to do so */
1016.                     if ((is_pool(xx, yy) && !is_swimmer(ptr))
1017.                         || (is_lava(xx, yy) && !likes_lava(ptr)))
1018.                         continue;
1019. 
1020.                     if (((likegold && otmp->oclass == COIN_CLASS)
1021.                          || (likeobjs && index(practical, otmp->oclass)
1022.                              && (otmp->otyp != CORPSE
1023.                                  || (ptr->mlet == S_NYMPH
1024.                                      && !is_rider(&mons[otmp->corpsenm]))))
1025.                          || (likemagic && index(magical, otmp->oclass))
1026.                          || (uses_items && searches_for_item(mtmp, otmp))
1027.                          || (likerock && otmp->otyp == BOULDER)
1028.                          || (likegems && otmp->oclass == GEM_CLASS
1029.                              && objects[otmp->otyp].oc_material != MINERAL)
1030.                          || (conceals && !cansee(otmp->ox, otmp->oy))
1031.                          || (ptr == &mons[PM_GELATINOUS_CUBE]
1032.                              && !index(indigestion, otmp->oclass)
1033.                              && !(otmp->otyp == CORPSE
1034.                                   && touch_petrifies(&mons[otmp->corpsenm]))))
1035.                         && touch_artifact(otmp, mtmp)) {
1036.                         if (can_carry(mtmp, otmp) > 0
1037.                             && (throws_rocks(ptr) || !sobj_at(BOULDER, xx, yy))
1038.                             && (!is_unicorn(ptr)
1039.                                 || objects[otmp->otyp].oc_material == GEMSTONE)
1040.                             /* Don't get stuck circling an Elbereth */
1041.                             && !onscary(xx, yy, mtmp)) {
1042.                             minr = distmin(omx, omy, xx, yy);
1043.                             oomx = min(COLNO - 1, omx + minr);
1044.                             oomy = min(ROWNO - 1, omy + minr);
1045.                             lmx = max(1, omx - minr);
1046.                             lmy = max(0, omy - minr);
1047.                             gx = otmp->ox;
1048.                             gy = otmp->oy;
1049.                             if (gx == omx && gy == omy) {
1050.                                 mmoved = 3; /* actually unnecessary */
1051.                                 goto postmov;
1052.                             }
1053.                         }
1054.                     }
1055.                 }
1056.             }
1057.         } else if (likegold) {
1058.             /* don't try to pick up anything else, but use the same loop */
1059.             uses_items = 0;
1060.             likegems = likeobjs = likemagic = likerock = conceals = 0;
1061.             goto look_for_obj;
1062.         }
1063. 
1064.         if (minr < SQSRCHRADIUS && appr == -1) {
1065.             if (distmin(omx, omy, mtmp->mux, mtmp->muy) <= 3) {
1066.                 gx = mtmp->mux;
1067.                 gy = mtmp->muy;
1068.             } else
1069.                 appr = 1;
1070.         }
1071.     }
1072. 
1073.     /* don't tunnel if hostile and close enough to prefer a weapon */
1074.     if (can_tunnel && needspick(ptr)
1075.         && ((!mtmp->mpeaceful || Conflict)
1076.             && dist2(mtmp->mx, mtmp->my, mtmp->mux, mtmp->muy) <= 8))
1077.         can_tunnel = FALSE;
1078. 
1079.     nix = omx;
1080.     niy = omy;
1081.     flag = 0L;
1082.     if (mtmp->mpeaceful && (!Conflict || resist(mtmp, RING_CLASS, 0, 0)))
1083.         flag |= (ALLOW_SANCT | ALLOW_SSM);
1084.     else
1085.         flag |= ALLOW_U;
1086.     if (is_minion(ptr) || is_rider(ptr))
1087.         flag |= ALLOW_SANCT;
1088.     /* unicorn may not be able to avoid hero on a noteleport level */
1089.     if (is_unicorn(ptr) && !level.flags.noteleport)
1090.         flag |= NOTONL;
1091.     if (passes_walls(ptr))
1092.         flag |= (ALLOW_WALL | ALLOW_ROCK);
1093.     if (passes_bars(ptr))
1094.         flag |= ALLOW_BARS;
1095.     if (can_tunnel)
1096.         flag |= ALLOW_DIG;
1097.     if (is_human(ptr) || ptr == &mons[PM_MINOTAUR])
1098.         flag |= ALLOW_SSM;
1099.     if ((is_undead(ptr) && ptr->mlet != S_GHOST) || is_vampshifter(mtmp))
1100.         flag |= NOGARLIC;
1101.     if (throws_rocks(ptr))
1102.         flag |= ALLOW_ROCK;
1103.     if (can_open)
1104.         flag |= OPENDOOR;
1105.     if (can_unlock)
1106.         flag |= UNLOCKDOOR;
1107.     if (doorbuster)
1108.         flag |= BUSTDOOR;
1109.     {
1110.         register int i, j, nx, ny, nearer;
1111.         int jcnt, cnt;
1112.         int ndist, nidist;
1113.         register coord *mtrk;
1114.         coord poss[9];
1115. 
1116.         cnt = mfndpos(mtmp, poss, info, flag);
1117.         chcnt = 0;
1118.         jcnt = min(MTSZ, cnt - 1);
1119.         chi = -1;
1120.         nidist = dist2(nix, niy, gx, gy);
1121.         /* allow monsters be shortsighted on some levels for balance */
1122.         if (!mtmp->mpeaceful && level.flags.shortsighted
1123.             && nidist > (couldsee(nix, niy) ? 144 : 36) && appr == 1)
1124.             appr = 0;
1125.         if (is_unicorn(ptr) && level.flags.noteleport) {
1126.             /* on noteleport levels, perhaps we cannot avoid hero */
1127.             for (i = 0; i < cnt; i++)
1128.                 if (!(info[i] & NOTONL))
1129.                     avoid = TRUE;
1130.         }
1131.         better_with_displacing =
1132.             should_displace(mtmp, poss, info, cnt, gx, gy);
1133.         for (i = 0; i < cnt; i++) {
1134.             if (avoid && (info[i] & NOTONL))
1135.                 continue;
1136.             nx = poss[i].x;
1137.             ny = poss[i].y;
1138. 
1139.             if (MON_AT(nx, ny) && (info[i] & ALLOW_MDISP)
1140.                 && !(info[i] & ALLOW_M) && !better_with_displacing)
1141.                 continue;
1142.             if (appr != 0) {
1143.                 mtrk = &mtmp->mtrack[0];
1144.                 for (j = 0; j < jcnt; mtrk++, j++)
1145.                     if (nx == mtrk->x && ny == mtrk->y)
1146.                         if (rn2(4 * (cnt - j)))
1147.                             goto nxti;
1148.             }
1149. 
1150.             nearer = ((ndist = dist2(nx, ny, gx, gy)) < nidist);
1151. 
1152.             if ((appr == 1 && nearer) || (appr == -1 && !nearer)
1153.                 || (!appr && !rn2(++chcnt)) || !mmoved) {
1154.                 nix = nx;
1155.                 niy = ny;
1156.                 nidist = ndist;
1157.                 chi = i;
1158.                 mmoved = 1;
1159.             }
1160.  nxti:
1161.             ;
1162.         }
1163.     }
1164. 
1165.     if (mmoved) {
1166.         register int j;
1167. 
1168.         if (mmoved == 1 && (u.ux != nix || u.uy != niy) && itsstuck(mtmp))
1169.             return 3;
1170. 
1171.         if (mmoved == 1 && m_digweapon_check(mtmp, nix,niy))
1172.             return 3;
1173. 
1174.         /* If ALLOW_U is set, either it's trying to attack you, or it
1175.          * thinks it is.  In either case, attack this spot in preference to
1176.          * all others.
1177.          */
1178.         /* Actually, this whole section of code doesn't work as you'd expect.
1179.          * Most attacks are handled in dochug().  It calls distfleeck(), which
1180.          * among other things sets nearby if the monster is near you--and if
1181.          * nearby is set, we never call m_move unless it is a special case
1182.          * (confused, stun, etc.)  The effect is that this ALLOW_U (and
1183.          * mfndpos) has no effect for normal attacks, though it lets a
1184.          * confused monster attack you by accident.
1185.          */
1186.         if (info[chi] & ALLOW_U) {
1187.             nix = mtmp->mux;
1188.             niy = mtmp->muy;
1189.         }
1190.         if (nix == u.ux && niy == u.uy) {
1191.             mtmp->mux = u.ux;
1192.             mtmp->muy = u.uy;
1193.             return 0;
1194.         }
1195.         /* The monster may attack another based on 1 of 2 conditions:
1196.          * 1 - It may be confused.
1197.          * 2 - It may mistake the monster for your (displaced) image.
1198.          * Pets get taken care of above and shouldn't reach this code.
1199.          * Conflict gets handled even farther away (movemon()).
1200.          */
1201.         if ((info[chi] & ALLOW_M) || (nix == mtmp->mux && niy == mtmp->muy)) {
1202.             struct monst *mtmp2;
1203.             int mstatus;
1204. 
1205.             mtmp2 = m_at(nix, niy);
1206. 
1207.             notonhead = mtmp2 && (nix != mtmp2->mx || niy != mtmp2->my);
1208.             /* note: mstatus returns 0 if mtmp2 is nonexistent */
1209.             mstatus = mattackm(mtmp, mtmp2);
1210. 
1211.             if (mstatus & MM_AGR_DIED) /* aggressor died */
1212.                 return 2;
1213. 
1214.             if ((mstatus & MM_HIT) && !(mstatus & MM_DEF_DIED) && rn2(4)
1215.                 && mtmp2->movement >= NORMAL_SPEED) {
1216.                 mtmp2->movement -= NORMAL_SPEED;
1217.                 notonhead = 0;
1218.                 mstatus = mattackm(mtmp2, mtmp); /* return attack */
1219.                 if (mstatus & MM_DEF_DIED)
1220.                     return 2;
1221.             }
1222.             return 3;
1223.         }
1224. 
1225.         if ((info[chi] & ALLOW_MDISP)) {
1226.             struct monst *mtmp2;
1227.             int mstatus;
1228. 
1229.             mtmp2 = m_at(nix, niy);
1230.             mstatus = mdisplacem(mtmp, mtmp2, FALSE);
1231.             if ((mstatus & MM_AGR_DIED) || (mstatus & MM_DEF_DIED))
1232.                 return 2;
1233.             if (mstatus & MM_HIT)
1234.                 return 1;
1235.             return 3;
1236.         }
1237. 
1238.         if (!m_in_out_region(mtmp, nix, niy))
1239.             return 3;
1240. 
1241.         remove_monster(omx, omy);
1242.         place_monster(mtmp, nix, niy);
1243.         for (j = MTSZ - 1; j > 0; j--)
1244.             mtmp->mtrack[j] = mtmp->mtrack[j - 1];
1245.         mtmp->mtrack[0].x = omx;
1246.         mtmp->mtrack[0].y = omy;
1247.         /* Place a segment at the old position. */
1248.         if (mtmp->wormno)
1249.             worm_move(mtmp);
1250.     } else {
1251.         if (is_unicorn(ptr) && rn2(2) && !tele_restrict(mtmp)) {
1252.             (void) rloc(mtmp, TRUE);
1253.             return 1;
1254.         }
1255.         if (mtmp->wormno)
1256.             worm_nomove(mtmp);
1257.     }
1258.  postmov:
1259.     if (mmoved == 1 || mmoved == 3) {
1260.         boolean canseeit = cansee(mtmp->mx, mtmp->my);
1261. 
1262.         if (mmoved == 1) {
1263.             /* normal monster move will already have <nix,niy>,
1264.                but pet dog_move() with 'goto postmov' won't */
1265.             nix = mtmp->mx, niy = mtmp->my;
1266.             /* sequencing issue:  when monster movement decides that a
1267.                monster can move to a door location, it moves the monster
1268.                there before dealing with the door rather than after;
1269.                so a vampire/bat that is going to shift to fog cloud and
1270.                pass under the door is already there but transformation
1271.                into fog form--and its message, when in sight--has not
1272.                happened yet; we have to move monster back to previous
1273.                location before performing the vamp_shift() to make the
1274.                message happen at right time, then back to the door again
1275.                [if we did the shift above, before moving the monster,
1276.                we would need to duplicate it in dog_move()...] */
1277.             if (is_vampshifter(mtmp) && !amorphous(mtmp->data)
1278.                 && IS_DOOR(levl[nix][niy].typ)
1279.                 && ((levl[nix][niy].doormask & (D_LOCKED | D_CLOSED)) != 0)
1280.                 && can_fog(mtmp)) {
1281.                 if (sawmon) {
1282.                     remove_monster(nix, niy);
1283.                     place_monster(mtmp, omx, omy);
1284.                     newsym(nix, niy), newsym(omx, omy);
1285.                 }
1286.                 if (vamp_shift(mtmp, &mons[PM_FOG_CLOUD], sawmon)) {
1287.                     ptr = mtmp->data; /* update cached value */
1288.                 }
1289.                 if (sawmon) {
1290.                     remove_monster(omx, omy);
1291.                     place_monster(mtmp, nix, niy);
1292.                     newsym(omx, omy), newsym(nix, niy);
1293.                 }
1294.             }
1295. 
1296.             newsym(omx, omy); /* update the old position */
1297.             if (mintrap(mtmp) >= 2) {
1298.                 if (mtmp->mx)
1299.                     newsym(mtmp->mx, mtmp->my);
1300.                 return 2; /* it died */
1301.             }
1302.             ptr = mtmp->data; /* in case mintrap() caused polymorph */
1303. 
1304.             /* open a door, or crash through it, if 'mtmp' can */
1305.             if (IS_DOOR(levl[mtmp->mx][mtmp->my].typ)
1306.                 && !passes_walls(ptr) /* doesn't need to open doors */
1307.                 && !can_tunnel) {     /* taken care of below */
1308.                 struct rm *here = &levl[mtmp->mx][mtmp->my];
1309.                 boolean btrapped = (here->doormask & D_TRAPPED) != 0,
1310.                         observeit = canseeit && canspotmon(mtmp);
1311. 
1312.                 /* if mon has MKoT, disarm door trap; no message given */
1313.                 if (btrapped && has_magic_key(mtmp)) {
1314.                     /* BUG: this lets a vampire or blob or a doorbuster
1315.                        holding the Key disarm the trap even though it isn't
1316.                        using that Key when squeezing under or smashing the
1317.                        door.  Not significant enough to worry about; perhaps
1318.                        the Key's magic is more powerful for monsters? */
1319.                     here->doormask &= ~D_TRAPPED;
1320.                     btrapped = FALSE;
1321.                 }
1322.                 if ((here->doormask & (D_LOCKED | D_CLOSED)) != 0
1323.                     && amorphous(ptr)) {
1324.                     if (flags.verbose && canseemon(mtmp))
1325.                         pline("%s %s under the door.", Monnam(mtmp),
1326.                               (ptr == &mons[PM_FOG_CLOUD]
1327.                                || ptr->mlet == S_LIGHT) ? "flows" : "oozes");
1328.                 } else if (here->doormask & D_LOCKED && can_unlock) {
1329.                     if (btrapped) {
1330.                         here->doormask = D_NODOOR;
1331.                         newsym(mtmp->mx, mtmp->my);
1332.                         unblock_point(mtmp->mx, mtmp->my); /* vision */
1333.                         if (mb_trapped(mtmp))
1334.                             return 2;
1335.                     } else {
1336.                         if (flags.verbose) {
1337.                             if (observeit)
1338.                                 pline("%s unlocks and opens a door.",
1339.                                       Monnam(mtmp));
1340.                             else if (canseeit)
1341.                                 You_see("a door unlock and open.");
1342.                             else if (!Deaf)
1343.                                 You_hear("a door unlock and open.");
1344.                         }
1345.                         here->doormask = D_ISOPEN;
1346.                         /* newsym(mtmp->mx, mtmp->my); */
1347.                         unblock_point(mtmp->mx, mtmp->my); /* vision */
1348.                     }
1349.                 } else if (here->doormask == D_CLOSED && can_open) {
1350.                     if (btrapped) {
1351.                         here->doormask = D_NODOOR;
1352.                         newsym(mtmp->mx, mtmp->my);
1353.                         unblock_point(mtmp->mx, mtmp->my); /* vision */
1354.                         if (mb_trapped(mtmp))
1355.                             return 2;
1356.                     } else {
1357.                         if (flags.verbose) {
1358.                             if (observeit)
1359.                                 pline("%s opens a door.", Monnam(mtmp));
1360.                             else if (canseeit)
1361.                                 You_see("a door open.");
1362.                             else if (!Deaf)
1363.                                 You_hear("a door open.");
1364.                         }
1365.                         here->doormask = D_ISOPEN;
1366.                         /* newsym(mtmp->mx, mtmp->my); */  /* done below */
1367.                         unblock_point(mtmp->mx, mtmp->my); /* vision */
1368.                     }
1369.                 } else if (here->doormask & (D_LOCKED | D_CLOSED)) {
1370.                     /* mfndpos guarantees this must be a doorbuster */
1371.                     if (btrapped) {
1372.                         here->doormask = D_NODOOR;
1373.                         newsym(mtmp->mx, mtmp->my);
1374.                         unblock_point(mtmp->mx, mtmp->my); /* vision */
1375.                         if (mb_trapped(mtmp))
1376.                             return 2;
1377.                     } else {
1378.                         if (flags.verbose) {
1379.                             if (observeit)
1380.                                 pline("%s smashes down a door.",
1381.                                       Monnam(mtmp));
1382.                             else if (canseeit)
1383.                                 You_see("a door crash open.");
1384.                             else if (!Deaf)
1385.                                 You_hear("a door crash open.");
1386.                         }
1387.                         if ((here->doormask & D_LOCKED) != 0 && !rn2(2))
1388.                             here->doormask = D_NODOOR;
1389.                         else
1390.                             here->doormask = D_BROKEN;
1391.                         /* newsym(mtmp->mx, mtmp->my); */  /* done below */
1392.                         unblock_point(mtmp->mx, mtmp->my); /* vision */
1393.                     }
1394.                     /* if it's a shop door, schedule repair */
1395.                     if (*in_rooms(mtmp->mx, mtmp->my, SHOPBASE))
1396.                         add_damage(mtmp->mx, mtmp->my, 0L);
1397.                 }
1398.             } else if (levl[mtmp->mx][mtmp->my].typ == IRONBARS) {
1399.                 /* As of 3.6.2: was using may_dig() but it doesn't handle bars */
1400.                 if (!(levl[mtmp->mx][mtmp->my].wall_info & W_NONDIGGABLE)
1401.                     && (dmgtype(ptr, AD_RUST) || dmgtype(ptr, AD_CORR))) {
1402.                     if (canseemon(mtmp))
1403.                         pline("%s eats through the iron bars.", Monnam(mtmp));
1404.                     dissolve_bars(mtmp->mx, mtmp->my);
1405.                     return 3;
1406.                 } else if (flags.verbose && canseemon(mtmp))
1407.                     Norep("%s %s %s the iron bars.", Monnam(mtmp),
1408.                           /* pluralization fakes verb conjugation */
1409.                           makeplural(locomotion(ptr, "pass")),
1410.                           passes_walls(ptr) ? "through" : "between");
1411.             }
1412. 
1413.             /* possibly dig */
1414.             if (can_tunnel && mdig_tunnel(mtmp))
1415.                 return 2; /* mon died (position already updated) */
1416. 
1417.             /* set also in domove(), hack.c */
1418.             if (u.uswallow && mtmp == u.ustuck
1419.                 && (mtmp->mx != omx || mtmp->my != omy)) {
1420.                 /* If the monster moved, then update */
1421.                 u.ux0 = u.ux;
1422.                 u.uy0 = u.uy;
1423.                 u.ux = mtmp->mx;
1424.                 u.uy = mtmp->my;
1425.                 swallowed(0);
1426.             } else
1427.                 newsym(mtmp->mx, mtmp->my);
1428.         }
1429.         if (OBJ_AT(mtmp->mx, mtmp->my) && mtmp->mcanmove) {
1430.             /* recompute the likes tests, in case we polymorphed
1431.              * or if the "likegold" case got taken above */
1432.             if (setlikes) {
1433.                 int pctload = (curr_mon_load(mtmp) * 100) / max_mon_load(mtmp);
1434. 
1435.                 /* look for gold or jewels nearby */
1436.                 likegold = (likes_gold(ptr) && pctload < 95);
1437.                 likegems = (likes_gems(ptr) && pctload < 85);
1438.                 uses_items =
1439.                     (!mindless(ptr) && !is_animal(ptr) && pctload < 75);
1440.                 likeobjs = (likes_objs(ptr) && pctload < 75);
1441.                 likemagic = (likes_magic(ptr) && pctload < 85);
1442.                 likerock = (throws_rocks(ptr) && pctload < 50 && !Sokoban);
1443.                 conceals = hides_under(ptr);
1444.             }
1445. 
1446.             /* Maybe a rock mole just ate some metal object */
1447.             if (metallivorous(ptr)) {
1448.                 if (meatmetal(mtmp) == 2)
1449.                     return 2; /* it died */
1450.             }
1451. 
1452.             if (g_at(mtmp->mx, mtmp->my) && likegold)
1453.                 mpickgold(mtmp);
1454. 
1455.             /* Maybe a cube ate just about anything */
1456.             if (ptr == &mons[PM_GELATINOUS_CUBE]) {
1457.                 if (meatobj(mtmp) == 2)
1458.                     return 2; /* it died */
1459.             }
1460. 
1461.             if (!*in_rooms(mtmp->mx, mtmp->my, SHOPBASE) || !rn2(25)) {
1462.                 boolean picked = FALSE;
1463. 
1464.                 if (likeobjs)
1465.                     picked |= mpickstuff(mtmp, practical);
1466.                 if (likemagic)
1467.                     picked |= mpickstuff(mtmp, magical);
1468.                 if (likerock)
1469.                     picked |= mpickstuff(mtmp, boulder_class);
1470.                 if (likegems)
1471.                     picked |= mpickstuff(mtmp, gem_class);
1472.                 if (uses_items)
1473.                     picked |= mpickstuff(mtmp, (char *) 0);
1474.                 if (picked)
1475.                     mmoved = 3;
1476.             }
1477. 
1478.             if (mtmp->minvis) {
1479.                 newsym(mtmp->mx, mtmp->my);
1480.                 if (mtmp->wormno)
1481.                     see_wsegs(mtmp);
1482.             }
1483.         }
1484. 
1485.         if (hides_under(ptr) || ptr->mlet == S_EEL) {
1486.             /* Always set--or reset--mundetected if it's already hidden
1487.                (just in case the object it was hiding under went away);
1488.                usually set mundetected unless monster can't move.  */
1489.             if (mtmp->mundetected
1490.                 || (mtmp->mcanmove && !mtmp->msleeping && rn2(5)))
1491.                 (void) hideunder(mtmp);
1492.             newsym(mtmp->mx, mtmp->my);
1493.         }
1494.         if (mtmp->isshk) {
1495.             after_shk_move(mtmp);
1496.         }
1497.     }
1498.     return mmoved;
1499. }
1500. 
1501. void
1502. dissolve_bars(x, y)
1503. register int x, y;
1504. {
1505.     levl[x][y].typ = (Is_special(&u.uz) || *in_rooms(x, y, 0)) ? ROOM : CORR;
1506.     levl[x][y].flags = 0;
1507.     newsym(x, y);
1508. }
1509. 
1510. boolean
1511. closed_door(x, y)
1512. register int x, y;
1513. {
1514.     return (boolean) (IS_DOOR(levl[x][y].typ)
1515.                       && (levl[x][y].doormask & (D_LOCKED | D_CLOSED)));
1516. }
1517. 
1518. boolean
1519. accessible(x, y)
1520. register int x, y;
1521. {
1522.     int levtyp = levl[x][y].typ;
1523. 
1524.     /* use underlying terrain in front of closed drawbridge */
1525.     if (levtyp == DRAWBRIDGE_UP)
1526.         levtyp = db_under_typ(levl[x][y].drawbridgemask);
1527. 
1528.     return (boolean) (ACCESSIBLE(levtyp) && !closed_door(x, y));
1529. }
1530. 
1531. /* decide where the monster thinks you are standing */
1532. void
1533. set_apparxy(mtmp)
1534. register struct monst *mtmp;
1535. {
1536.     boolean notseen, gotu;
1537.     register int disp, mx = mtmp->mux, my = mtmp->muy;
1538.     long umoney = money_cnt(invent);
1539. 
1540.     /*
1541.      * do cheapest and/or most likely tests first
1542.      */
1543. 
1544.     /* pet knows your smell; grabber still has hold of you */
1545.     if (mtmp->mtame || mtmp == u.ustuck)
1546.         goto found_you;
1547. 
1548.     /* monsters which know where you are don't suddenly forget,
1549.        if you haven't moved away */
1550.     if (mx == u.ux && my == u.uy)
1551.         goto found_you;
1552. 
1553.     notseen = (!mtmp->mcansee || (Invis && !perceives(mtmp->data)));
1554.     /* add cases as required.  eg. Displacement ... */
1555.     if (notseen || Underwater) {
1556.         /* Xorns can smell quantities of valuable metal
1557.             like that in solid gold coins, treat as seen */
1558.         if ((mtmp->data == &mons[PM_XORN]) && umoney && !Underwater)
1559.             disp = 0;
1560.         else
1561.             disp = 1;
1562.     } else if (Displaced) {
1563.         disp = couldsee(mx, my) ? 2 : 1;
1564.     } else
1565.         disp = 0;
1566.     if (!disp)
1567.         goto found_you;
1568. 
1569.     /* without something like the following, invisibility and displacement
1570.        are too powerful */
1571.     gotu = notseen ? !rn2(3) : Displaced ? !rn2(4) : FALSE;
1572. 
1573.     if (!gotu) {
1574.         register int try_cnt = 0;
1575. 
1576.         do {
1577.             if (++try_cnt > 200)
1578.                 goto found_you; /* punt */
1579.             mx = u.ux - disp + rn2(2 * disp + 1);
1580.             my = u.uy - disp + rn2(2 * disp + 1);
1581.         } while (!isok(mx, my)
1582.                  || (disp != 2 && mx == mtmp->mx && my == mtmp->my)
1583.                  || ((mx != u.ux || my != u.uy) && !passes_walls(mtmp->data)
1584.                      && !(accessible(mx, my)
1585.                           || (closed_door(mx, my)
1586.                               && (can_ooze(mtmp) || can_fog(mtmp)))))
1587.                  || !couldsee(mx, my));
1588.     } else {
1589.  found_you:
1590.         mx = u.ux;
1591.         my = u.uy;
1592.     }
1593. 
1594.     mtmp->mux = mx;
1595.     mtmp->muy = my;
1596. }
1597. 
1598. /*
1599.  * mon-to-mon displacement is a deliberate "get out of my way" act,
1600.  * not an accidental bump, so we don't consider mstun or mconf in
1601.  * undesired_disp().
1602.  *
1603.  * We do consider many other things about the target and its
1604.  * location however.
1605.  */
1606. boolean
1607. undesirable_disp(mtmp, x, y)
1608. struct monst *mtmp; /* barging creature */
1609. xchar x, y; /* spot 'mtmp' is considering moving to */
1610. {
1611.     boolean is_pet = (mtmp && mtmp->mtame && !mtmp->isminion);
1612.     struct trap *trap = t_at(x, y);
1613. 
1614.     if (is_pet) {
1615.         /* Pets avoid a trap if you've seen it usually. */
1616.         if (trap && trap->tseen && rn2(40))
1617.             return TRUE;
1618.         /* Pets avoid cursed locations */
1619.         if (cursed_object_at(x, y))
1620.             return TRUE;
1621. 
1622.     /* Monsters avoid a trap if they've seen that type before */
1623.     } else if (trap && rn2(40)
1624.                && (mtmp->mtrapseen & (1 << (trap->ttyp - 1))) != 0) {
1625.         return TRUE;
1626.     }
1627. 
1628.     /* oversimplification:  creatures that bargethrough can't swap places
1629.        when target monster is in rock or closed door or water (in particular,
1630.        avoid moving to spots where mondied() won't leave a corpse; doesn't
1631.        matter whether barger is capable of moving to such a target spot if
1632.        it were unoccupied) */
1633.     if (!accessible(x, y)
1634.         /* mondied() allows is_pool() as an exception to !accessible(),
1635.            but we'll only do that if 'mtmp' is already at a water location
1636.            so that we don't swap a water critter onto land */
1637.         && !(is_pool(x, y) && is_pool(mtmp->mx, mtmp->my)))
1638.         return TRUE;
1639. 
1640.     return FALSE;
1641. }
1642. 
1643. /*
1644.  * Inventory prevents passage under door.
1645.  * Used by can_ooze() and can_fog().
1646.  */
1647. STATIC_OVL boolean
1648. stuff_prevents_passage(mtmp)
1649. struct monst *mtmp;
1650. {
1651.     struct obj *chain, *obj;
1652. 
1653.     if (mtmp == &youmonst) {
1654.         chain = invent;
1655.     } else {
1656.         chain = mtmp->minvent;
1657.     }
1658.     for (obj = chain; obj; obj = obj->nobj) {
1659.         int typ = obj->otyp;
1660. 
1661.         if (typ == COIN_CLASS && obj->quan > 100L)
1662.             return TRUE;
1663.         if (obj->oclass != GEM_CLASS && !(typ >= ARROW && typ <= BOOMERANG)
1664.             && !(typ >= DAGGER && typ <= CRYSKNIFE) && typ != SLING
1665.             && !is_cloak(obj) && typ != FEDORA && !is_gloves(obj)
1666.             && typ != LEATHER_JACKET && typ != CREDIT_CARD && !is_shirt(obj)
1667.             && !(typ == CORPSE && verysmall(&mons[obj->corpsenm]))
1668.             && typ != FORTUNE_COOKIE && typ != CANDY_BAR && typ != PANCAKE
1669.             && typ != LEMBAS_WAFER && typ != LUMP_OF_ROYAL_JELLY
1670.             && obj->oclass != AMULET_CLASS && obj->oclass != RING_CLASS
1671.             && obj->oclass != VENOM_CLASS && typ != SACK
1672.             && typ != BAG_OF_HOLDING && typ != BAG_OF_TRICKS
1673.             && !Is_candle(obj) && typ != OILSKIN_SACK && typ != LEASH
1674.             && typ != STETHOSCOPE && typ != BLINDFOLD && typ != TOWEL
1675.             && typ != TIN_WHISTLE && typ != MAGIC_WHISTLE
1676.             && typ != MAGIC_MARKER && typ != TIN_OPENER && typ != SKELETON_KEY
1677.             && typ != LOCK_PICK)
1678.             return TRUE;
1679.         if (Is_container(obj) && obj->cobj)
1680.             return TRUE;
1681.     }
1682.     return FALSE;
1683. }
1684. 
1685. boolean
1686. can_ooze(mtmp)
1687. struct monst *mtmp;
1688. {
1689.     if (!amorphous(mtmp->data) || stuff_prevents_passage(mtmp))
1690.         return FALSE;
1691.     return TRUE;
1692. }
1693. 
1694. /* monster can change form into a fog if necessary */
1695. boolean
1696. can_fog(mtmp)
1697. struct monst *mtmp;
1698. {
1699.     if (!(mvitals[PM_FOG_CLOUD].mvflags & G_GENOD) && is_vampshifter(mtmp)
1700.         && !Protection_from_shape_changers && !stuff_prevents_passage(mtmp))
1701.         return TRUE;
1702.     return FALSE;
1703. }
1704. 
1705. STATIC_OVL int
1706. vamp_shift(mon, ptr, domsg)
1707. struct monst *mon;
1708. struct permonst *ptr;
1709. boolean domsg;
1710. {
1711.     int reslt = 0;
1712.     char oldmtype[BUFSZ];
1713. 
1714.     /* remember current monster type before shapechange */
1715.     Strcpy(oldmtype, domsg ? noname_monnam(mon, ARTICLE_THE) : "");
1716. 
1717.     if (mon->data == ptr) {
1718.         /* already right shape */
1719.         reslt = 1;
1720.         domsg = FALSE;
1721.     } else if (is_vampshifter(mon)) {
1722.         reslt = newcham(mon, ptr, FALSE, FALSE);
1723.     }
1724. 
1725.     if (reslt && domsg) {
1726.         pline("You %s %s where %s was.",
1727.               !canseemon(mon) ? "now detect" : "observe",
1728.               noname_monnam(mon, ARTICLE_A), oldmtype);
1729.         /* this message is given when it turns into a fog cloud
1730.            in order to move under a closed door */
1731.         display_nhwindow(WIN_MESSAGE, FALSE);
1732.     }
1733. 
1734.     return reslt;
1735. }
1736. 
1737. /*monmove.c*/