Source:NetHack 5.0.0/src/muse.c

From NetHackWiki
Jump to navigation Jump to search

Below is the full text to muse.c from the source code of NetHack 5.0.0. To link to a particular line, write [[Source:NetHack 5.0.0/src/muse.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 5.0	muse.c	$NHDT-Date: 1770949988 2026/02/12 18:33:08 $  $NHDT-Branch: NetHack-3.7 $:$NHDT-Revision: 1.241 $ */
2. /*      Copyright (C) 1990 by Ken Arromdee                         */
3. /* NetHack may be freely redistributed.  See license for details.  */
4. 
5. /*
6.  * Monster item usage routines.
7.  */
8. 
9. #include "hack.h"
10. 
11. /* Let monsters use magic items.  Arbitrary assumptions: Monsters only use
12.  * scrolls when they can see, monsters know when wands have 0 charges,
13.  * monsters cannot recognize if items are cursed are not, monsters which
14.  * are confused don't know not to read scrolls, etc....
15.  */
16. 
17. staticfn int precheck(struct monst *, struct obj *);
18. staticfn void mzapwand(struct monst *, struct obj *, boolean) NONNULLPTRS;
19. staticfn void mplayhorn(struct monst *, struct obj *, boolean) NONNULLPTRS;
20. staticfn void mreadmsg(struct monst *, struct obj *) NONNULLPTRS;
21. staticfn void mquaffmsg(struct monst *, struct obj *) NONNULLPTRS;
22. staticfn boolean m_use_healing(struct monst *);
23. staticfn boolean m_sees_sleepy_soldier(struct monst *);
24. staticfn void m_tele(struct monst *, boolean, boolean, int);
25. staticfn boolean m_next2m(struct monst *);
26. staticfn void reveal_trap(struct trap *, boolean);
27. staticfn int mon_escape(struct monst *, boolean);
28. staticfn boolean linedup_chk_corpse(coordxy, coordxy);
29. staticfn void m_use_undead_turning(struct monst *, struct obj *);
30. staticfn boolean hero_behind_chokepoint(struct monst *);
31. staticfn boolean mon_has_friends(struct monst *);
32. staticfn boolean mon_likes_objpile_at(struct monst *mtmp, coordxy x, coordxy y) NONNULLARG1;
33. staticfn int mbhitm(struct monst *, struct obj *);
34. staticfn void buzz_force_miss(int, int, coordxy, coordxy, int, int);
35. staticfn boolean fhito_loc(struct obj *obj, coordxy x, coordxy y,
36.                            int (*fhito)(OBJ_P, OBJ_P));
37. staticfn void mbhit(struct monst *, int, int (*)(MONST_P, OBJ_P),
38.                   int (*)(OBJ_P, OBJ_P), struct obj *);
39. staticfn struct permonst *muse_newcham_mon(struct monst *);
40. staticfn int mloot_container(struct monst *mon, struct obj *, boolean);
41. staticfn void you_aggravate(struct monst *);
42. #if 0
43. staticfn boolean necrophiliac(struct obj *, boolean);
44. #endif
45. staticfn void mon_consume_unstone(struct monst *, struct obj *, boolean,
46.                                 boolean);
47. staticfn boolean cures_stoning(struct monst *, struct obj *, boolean);
48. staticfn boolean mcould_eat_tin(struct monst *);
49. staticfn boolean muse_unslime(struct monst *, struct obj *, struct trap *,
50.                             boolean);
51. staticfn int cures_sliming(struct monst *, struct obj *);
52. staticfn boolean green_mon(struct monst *);
53. 
54. /* Any preliminary checks which may result in the monster being unable to use
55.  * the item.  Returns 0 if nothing happened, 2 if the monster can't do
56.  * anything (i.e. it teleported) and 1 if it's dead.
57.  */
58. staticfn int
59. precheck(struct monst *mon, struct obj *obj)
60. {
61.     boolean vis;
62. 
63.     if (!obj)
64.         return 0;
65.     vis = cansee(mon->mx, mon->my);
66. 
67.     if (obj->oclass == POTION_CLASS) {
68.         coord cc;
69.         static const char *const empty = "The potion turns out to be empty.";
70.         struct monst *mtmp;
71. 
72.         if (objdescr_is(obj, "milky")) {
73.             if (!(svm.mvitals[PM_GHOST].mvflags & G_GONE)
74.                 && !rn2(POTION_OCCUPANT_CHANCE(svm.mvitals[PM_GHOST].born))) {
75.                 if (!enexto(&cc, mon->mx, mon->my, &mons[PM_GHOST]))
76.                     return 0;
77.                 mquaffmsg(mon, obj);
78.                 m_useup(mon, obj);
79.                 mtmp = makemon(&mons[PM_GHOST], cc.x, cc.y, MM_NOMSG);
80.                 if (!mtmp) {
81.                     if (vis)
82.                         pline1(empty);
83.                 } else {
84.                     if (vis) {
85.                         pline(
86.                             "As %s opens the bottle, an enormous %s emerges!",
87.                               mon_nam(mon),
88.                               Hallucination ? rndmonnam(NULL)
89.                                             : (const char *) "ghost");
90.                         pline("%s is frightened to death,"
91.                               " and unable to move.",
92.                               Monnam(mon));
93.                     }
94.                     paralyze_monst(mon, 3);
95.                 }
96.                 return 2;
97.             }
98.         }
99.         if (objdescr_is(obj, "smoky")
100.             && !(svm.mvitals[PM_DJINNI].mvflags & G_GONE)
101.             && !rn2(POTION_OCCUPANT_CHANCE(svm.mvitals[PM_DJINNI].born))) {
102.             if (!enexto(&cc, mon->mx, mon->my, &mons[PM_DJINNI]))
103.                 return 0;
104.             mquaffmsg(mon, obj);
105.             m_useup(mon, obj);
106.             mtmp = makemon(&mons[PM_DJINNI], cc.x, cc.y, MM_NOMSG);
107.             if (!mtmp) {
108.                 if (vis)
109.                     pline1(empty);
110.             } else {
111.                 if (vis)
112.                     pline_mon(mtmp, "In a cloud of smoke, %s emerges!", a_monnam(mtmp));
113.                 pline("%s speaks.", vis ? Monnam(mtmp) : Something);
114.                 /* I suspect few players will be upset that monsters */
115.                 /* can't wish for wands of death here.... */
116.                 SetVoice(mtmp, 0, 80, 0);
117.                 if (rn2(2)) {
118.                     verbalize("You freed me!");
119.                     mtmp->mpeaceful = 1;
120.                     set_malign(mtmp);
121.                 } else {
122.                     verbalize("It is about time.");
123.                     if (vis)
124.                         pline("%s vanishes.", Monnam(mtmp));
125.                     mongone(mtmp);
126.                 }
127.             }
128.             return 2;
129.         }
130.     }
131.     if (obj->oclass == WAND_CLASS && obj->cursed
132.         && !rn2(WAND_BACKFIRE_CHANCE)) {
133.         int dam = d(obj->spe + 2, 6);
134. 
135.         /* 3.6.1: no Deaf filter; 'if' message doesn't warrant it, 'else'
136.            message doesn't need it since You_hear() has one of its own */
137.         if (vis) {
138.             pline_mon(mon, "%s zaps %s, which suddenly explodes!", Monnam(mon),
139.                   an(xname(obj)));
140.         } else {
141.             /* same near/far threshold as mzapwand() */
142.             int range = couldsee(mon->mx, mon->my) /* 9 or 5 */
143.                            ? (BOLT_LIM + 1) : (BOLT_LIM - 3);
144. 
145.             Soundeffect(se_zap_then_explosion, 100);
146.             You_hear("a zap and an explosion %s.",
147.                      (mdistu(mon) <= range * range)
148.                         ? "nearby" : "in the distance");
149.         }
150.         m_useup(mon, obj);
151.         mon->mhp -= dam;
152.         if (DEADMONSTER(mon)) {
153.             monkilled(mon, "", AD_RBRE);
154.             return 1;
155.         }
156.         gm.m.has_defense = gm.m.has_offense = gm.m.has_misc = 0;
157.         /* Only one needed to be set to 0 but the others are harmless */
158.     }
159.     return 0;
160. }
161. 
162. /* when a monster zaps a wand give a message, deduct a charge, and if it
163.    isn't directly seen, remove hero's memory of the number of charges */
164. staticfn void
165. mzapwand(
166.     struct monst *mtmp,
167.     struct obj *otmp,
168.     boolean self)
169. {
170.     if (otmp->spe < 1) {
171.         impossible("Mon zapping wand with %d charges?", otmp->spe);
172.         return;
173.     }
174.     if (!canseemon(mtmp)) {
175.         int range = couldsee(mtmp->mx, mtmp->my) /* 9 or 5 */
176.                        ? (BOLT_LIM + 1) : (BOLT_LIM - 3);
177. 
178.         Soundeffect(se_zap, 100);
179.         You_hear("a %s zap.", (mdistu(mtmp) <= range * range)
180.                                  ? "nearby" : "distant");
181.         unknow_object(otmp); /* hero loses info when unseen obj is used */
182.     } else if (self) {
183.         pline("%s with %s!",
184.               monverbself(mtmp, Monnam(mtmp), "zap", (char *) 0),
185.               doname(otmp));
186.     } else {
187.         pline_mon(mtmp, "%s zaps %s!", Monnam(mtmp), an(xname(otmp)));
188.         stop_occupation();
189.     }
190.     otmp->spe -= 1;
191. }
192. 
193. /* similar to mzapwand() but for magical horns (only instrument mons play) */
194. staticfn void
195. mplayhorn(
196.     struct monst *mtmp,
197.     struct obj *otmp,
198.     boolean self)
199. {
200.     char *objnamp, objbuf[BUFSZ];
201. 
202.     if (!canseemon(mtmp)) {
203.         int range = couldsee(mtmp->mx, mtmp->my) /* 9 or 5 */
204.                        ? (BOLT_LIM + 1) : (BOLT_LIM - 3);
205. 
206.         Soundeffect(se_horn_being_played, 50);
207.         You_hear("a horn being played %s.",
208.                  (mdistu(mtmp) <= range * range)
209.                     ? "nearby" : "in the distance");
210.         unknow_object(otmp); /* hero loses info when unseen obj is used */
211.     } else if (self) {
212.         observe_object(otmp);
213.         objnamp = xname(otmp);
214.         if (strlen(objnamp) >= QBUFSZ)
215.             objnamp = simpleonames(otmp);
216.         Sprintf(objbuf, "a %s directed at", objnamp);
217.         /* "<mon> plays a <horn> directed at himself!" */
218.         pline("%s!", monverbself(mtmp, Monnam(mtmp), "play", objbuf));
219.         makeknown(otmp->otyp); /* (wands handle this slightly differently) */
220.     } else {
221.         observe_object(otmp);
222.         objnamp = xname(otmp);
223.         if (strlen(objnamp) >= QBUFSZ)
224.             objnamp = simpleonames(otmp);
225.         pline("%s %s %s directed at you!",
226.               /* monverbself() would adjust the verb if hallucination made
227.                  subject plural; stick with singular here, at least for now */
228.               Monnam(mtmp), "plays", an(objnamp));
229.         makeknown(otmp->otyp);
230.         stop_occupation();
231.     }
232.     otmp->spe -= 1; /* use a charge */
233. }
234. 
235. /* see or hear a monster reading a scroll;
236.    when scroll hasn't been seen, its label is revealed unless hero is deaf */
237. staticfn void
238. mreadmsg(struct monst *mtmp, struct obj *otmp)
239. {
240.     char onambuf[BUFSZ];
241.     boolean vismon = canseemon(mtmp),
242.             tpindicator = (!vismon && sensemon(mtmp));
243. 
244.     if (!vismon && Deaf)
245.         return; /* no feedback */
246. 
247.     observe_object(otmp); /* seeing/hearing scroll read reveals its label */
248.     Strcpy(onambuf, singular(otmp, vismon ? doname : ansimpleoname));
249. 
250.     if (vismon) {
251.         /* directly see the monster reading the scroll */
252.         pline_mon(mtmp, "%s reads %s!", Monnam(mtmp), onambuf);
253.     } else { /* !Deaf, otherwise we wouldn't reach here */
254.         char blindbuf[BUFSZ];
255.         boolean similar = same_race(gy.youmonst.data, mtmp->data),
256.                 uniqmon = ((mtmp->data->geno & G_UNIQ) != 0
257.                            /* shopkeepers aren't unique monsters but since
258.                               they have distinct names, treat them as such */
259.                            || mtmp->isshk),
260.                 recognize = (!Hallucination
261.                              && (mtmp->meverseen || (similar && !uniqmon)));
262.         /* describe unseen monster accurately when not hallucinating if it
263.            has ever been seen or is the same race as the hero (not yet seen
264.            unique monsters excepted) */
265.         int mflags = (SUPPRESS_INVISIBLE | SUPPRESS_SADDLE
266.                       | (recognize ? SUPPRESS_IT : AUGMENT_IT));
267. 
268.         if (sensemon(mtmp)) {
269.             tpindicator = TRUE;
270.         } else if (couldsee(mtmp->mx, mtmp->my) && mdistu(mtmp) <= 10 * 10) {
271.             /* monster can't be seen or sensed; hero might be blind or monster
272.                might be at a spot that isn't in view or might be invisible;
273.                remember it if the spot is within line of sight and relatively
274.                close */
275.             map_invisible(mtmp->mx, mtmp->my);
276.         }
277. 
278.         Snprintf(blindbuf, sizeof blindbuf, "reading %s", onambuf);
279.         strsubst(blindbuf, "reading a scroll labeled",
280.                  mtmp->mconf ? "attempting to incant" : "incant");
281.         You_hear("%s %s.",
282.                  x_monnam(mtmp, ARTICLE_A, (char *) 0, mflags, FALSE),
283.                  blindbuf);
284.         if (tpindicator)
285.             flash_mon(mtmp);
286.     }
287.     if (mtmp->mconf) /* (note: won't get if not seen and hero can't hear) */
288.         pline("Being confused, %s mispronounces the magic words...",
289.               vismon ? mon_nam(mtmp) : mhe(mtmp));
290. }
291. 
292. staticfn void
293. mquaffmsg(struct monst *mtmp, struct obj *otmp)
294. {
295.     if (canseemon(mtmp)) {
296.         observe_object(otmp);
297.         pline_mon(mtmp, "%s drinks %s!", Monnam(mtmp), singular(otmp, doname));
298.     } else if (!Deaf) {
299.         Soundeffect(se_mon_chugging_potion, 25);
300.         You_hear("a chugging sound.");
301.     }
302. }
303. 
304. /* Defines for various types of stuff.  The order in which monsters prefer
305.  * to use them is determined by the order of the code logic, not the
306.  * numerical order in which they are defined.
307.  */
308. #define MUSE_SCR_TELEPORTATION 1
309. #define MUSE_WAN_TELEPORTATION_SELF 2
310. #define MUSE_POT_HEALING 3
311. #define MUSE_POT_EXTRA_HEALING 4
312. #define MUSE_WAN_DIGGING 5
313. #define MUSE_TRAPDOOR 6
314. #define MUSE_TELEPORT_TRAP 7
315. #define MUSE_UPSTAIRS 8
316. #define MUSE_DOWNSTAIRS 9
317. #define MUSE_WAN_CREATE_MONSTER 10
318. #define MUSE_SCR_CREATE_MONSTER 11
319. #define MUSE_UP_LADDER 12
320. #define MUSE_DN_LADDER 13
321. #define MUSE_SSTAIRS 14
322. #define MUSE_WAN_TELEPORTATION 15
323. #define MUSE_BUGLE 16
324. #define MUSE_UNICORN_HORN 17
325. #define MUSE_POT_FULL_HEALING 18
326. #define MUSE_LIZARD_CORPSE 19
327. #define MUSE_WAN_UNDEAD_TURNING 20 /* also an offensive item */
328. /*
329. #define MUSE_INNATE_TPT 9999
330.  * We cannot use this.  Since monsters get unlimited teleportation, if they
331.  * were allowed to teleport at will you could never catch them.  Instead,
332.  * assume they only teleport at random times, despite the inconsistency
333.  * that if you polymorph into one you teleport at will.
334.  */
335. 
336. staticfn boolean
337. m_use_healing(struct monst *mtmp)
338. {
339.     struct obj *obj;
340. 
341.     if ((obj = m_carrying(mtmp, POT_FULL_HEALING)) != 0) {
342.         gm.m.defensive = obj;
343.         gm.m.has_defense = MUSE_POT_FULL_HEALING;
344.         return TRUE;
345.     }
346.     if ((obj = m_carrying(mtmp, POT_EXTRA_HEALING)) != 0) {
347.         gm.m.defensive = obj;
348.         gm.m.has_defense = MUSE_POT_EXTRA_HEALING;
349.         return TRUE;
350.     }
351.     if ((obj = m_carrying(mtmp, POT_HEALING)) != 0) {
352.         gm.m.defensive = obj;
353.         gm.m.has_defense = MUSE_POT_HEALING;
354.         return TRUE;
355.     }
356.     return FALSE;
357. }
358. 
359. /* return TRUE if monster mtmp can see at least one sleeping soldier */
360. staticfn boolean
361. m_sees_sleepy_soldier(struct monst *mtmp)
362. {
363.     coordxy x = mtmp->mx, y = mtmp->my;
364.     coordxy xx, yy;
365.     struct monst *mon;
366. 
367.     /* Distance is arbitrary.  What we really want to do is
368.      * have the soldier play the bugle when it sees or
369.      * remembers soldiers nearby...
370.      */
371.     for (xx = x - 3; xx <= x + 3; xx++)
372.         for (yy = y - 3; yy <= y + 3; yy++) {
373.             if (!isok(xx, yy) || (xx == x && yy == y))
374.                 continue;
375.             if ((mon = m_at(xx, yy)) != 0 && is_mercenary(mon->data)
376.                 && mon->data != &mons[PM_GUARD]
377.                 && helpless(mon))
378.                 return TRUE;
379.         }
380.     return FALSE;
381. }
382. 
383. staticfn void
384. m_tele(
385.     struct monst *mtmp, /* monst that might be teleported */
386.     boolean vismon,     /* can see it */
387.     boolean oseen,      /* have seen the object that triggered this */
388.     int how)            /* type of that object */
389. {
390.     if (tele_restrict(mtmp)) { /* mysterious force... */
391.         if (vismon && how)     /* mentions 'teleport' */
392.             makeknown(how);
393.         /* monster learns that teleportation isn't useful here */
394.         if (noteleport_level(mtmp))
395.             mon_learns_traps(mtmp, TELEP_TRAP);
396.     } else if ((mon_has_amulet(mtmp) || On_W_tower_level(&u.uz)) && !rn2(3)) {
397.         if (vismon)
398.             pline_mon(mtmp, "%s seems disoriented for a moment.", Monnam(mtmp));
399.     } else {
400.         /* teleport monster 'mtmp' */
401.         if (how) {
402.             /* teleporation has been triggered by an object */
403.             if (oseen)
404.                 makeknown(how);
405.             (void) rloc(mtmp, RLOC_MSG);
406.         } else {
407.             /* monster is voluntarily entering a teleporation trap; use the
408.                trap instead of rloc() in case it sends 'victim' to a vault */
409.             mtmp->mx = gt.trapx, mtmp->my = gt.trapy;
410.             (void) mintrap(mtmp, FORCETRAP);
411.         }
412.     }
413. }
414. 
415. /* return TRUE if monster mtmp has another monster next to it.
416.  * Called from find_defensive() where it is limited to Is_knox()
417.  * only, otherwise you could trap two monsters next to each other
418.  * in a boulder fort, and they would be happy to stay in there. */
419. staticfn boolean
420. m_next2m(struct monst *mtmp)
421. {
422.     coordxy x, y;
423.     struct monst *m2;
424. 
425.     if (DEADMONSTER(mtmp) || mon_offmap(mtmp))
426.         return FALSE;
427. 
428.     for (x = mtmp->mx - 1; x <= mtmp->mx + 1; x++)
429.         for (y = mtmp->my - 1; y <= mtmp->my + 1; y++) {
430.             if (!isok(x,y))
431.                 continue;
432.             if ((m2 = m_at(x, y)) && m2 != mtmp)
433.                 return TRUE;
434.         }
435.     return FALSE;
436. }
437. 
438. /* Select a defensive item/action for a monster.  Returns TRUE iff one is
439.    found. */
440. boolean
441. find_defensive(struct monst *mtmp, boolean tryescape)
442. {
443.     struct obj *obj;
444.     struct trap *t;
445.     int fraction;
446.     coordxy x = mtmp->mx, y = mtmp->my;
447.     boolean stuck = (mtmp == u.ustuck),
448.             immobile = (mtmp->data->mmove == 0);
449.     stairway *stway;
450. 
451.     gm.m.defensive = (struct obj *) 0;
452.     gm.m.has_defense = 0;
453. 
454.     if (is_animal(mtmp->data) || mindless(mtmp->data))
455.         return FALSE;
456.     if (!tryescape && dist2(x, y, mtmp->mux, mtmp->muy) > 25)
457.         return FALSE;
458.     if (tryescape && Is_knox(&u.uz)
459.         && !m_next2u(mtmp) && m_next2m(mtmp))
460.         return FALSE;
461.     if (u.uswallow && stuck)
462.         return FALSE;
463. 
464.     /*
465.      * Since unicorn horns don't get used up, the monster would look
466.      * silly trying to use the same cursed horn round after round,
467.      * so skip cursed unicorn horns.
468.      *
469.      * Unicorns use their own horns; they're excluded from inventory
470.      * scanning by nohands().  Ki-rin is depicted in the AD&D Monster
471.      * Manual with same horn as a unicorn, so let it use its horn too.
472.      * is_unicorn() doesn't include it; the class differs and it has
473.      * no interest in gems.
474.      */
475.     if (mtmp->mconf || mtmp->mstun || !mtmp->mcansee) {
476.         obj = 0;
477.         if (!nohands(mtmp->data)) {
478.             for (obj = mtmp->minvent; obj; obj = obj->nobj)
479.                 if (obj->otyp == UNICORN_HORN && !obj->cursed)
480.                     break;
481.         }
482.         if (obj || is_unicorn(mtmp->data) || mtmp->data == &mons[PM_KI_RIN]) {
483.             gm.m.defensive = obj;
484.             gm.m.has_defense = MUSE_UNICORN_HORN;
485.             return TRUE;
486.         }
487.     }
488. 
489.     if (mtmp->mconf || mtmp->mstun) {
490.         struct obj *liztin = 0;
491. 
492.         for (obj = mtmp->minvent; obj; obj = obj->nobj) {
493.             if (obj->otyp == CORPSE && obj->corpsenm == PM_LIZARD) {
494.                 gm.m.defensive = obj;
495.                 gm.m.has_defense = MUSE_LIZARD_CORPSE;
496.                 return TRUE;
497.             } else if (obj->otyp == TIN && obj->corpsenm == PM_LIZARD) {
498.                 liztin = obj;
499.             }
500.         }
501.         /* confused or stunned monster might not be able to open tin */
502.         if (liztin && mcould_eat_tin(mtmp) && rn2(3)) {
503.             gm.m.defensive = liztin;
504.             /* tin and corpse ultimately end up being handled the same */
505.             gm.m.has_defense = MUSE_LIZARD_CORPSE;
506.             return TRUE;
507.         }
508.     }
509. 
510.     /* It so happens there are two unrelated cases when we might want to
511.      * check specifically for healing alone.  The first is when the monster
512.      * is blind (healing cures blindness).  The second is when the monster
513.      * is peaceful; then we don't want to flee the player, and by
514.      * coincidence healing is all there is that doesn't involve fleeing.
515.      * These would be hard to combine because of the control flow.
516.      * Pestilence won't use healing even when blind.
517.      */
518.     if (!mtmp->mcansee && !nohands(mtmp->data)
519.         && mtmp->data != &mons[PM_PESTILENCE]) {
520.         if (m_use_healing(mtmp))
521.             return TRUE;
522.     }
523. 
524.     /* monsters aren't given wands of undead turning but if they
525.        happen to have picked one up, use it against corpse wielder;
526.        when applicable, use it now even if 'mtmp' isn't wounded */
527.     if (!mtmp->mpeaceful && !nohands(mtmp->data)
528.         && uwep && uwep->otyp == CORPSE
529.         && touch_petrifies(&mons[uwep->corpsenm])
530.         && !poly_when_stoned(mtmp->data) && !resists_ston(mtmp)
531.         && lined_up(mtmp)) { /* only lines up if distu range is within 5*5 */
532.         /* could use m_carrying(), then nxtobj() when matching wand
533.            is empty, but direct traversal is actually simpler here */
534.         for (obj = mtmp->minvent; obj; obj = obj->nobj)
535.             if (obj->otyp == WAN_UNDEAD_TURNING && obj->spe > 0) {
536.                 gm.m.defensive = obj;
537.                 gm.m.has_defense = MUSE_WAN_UNDEAD_TURNING;
538.                 return TRUE;
539.             }
540.     }
541. 
542.     if (!tryescape) {
543.         /* do we try to heal? */
544.         fraction = u.ulevel < 10 ? 5 : u.ulevel < 14 ? 4 : 3;
545.         if (mtmp->mhp >= mtmp->mhpmax
546.             || (mtmp->mhp >= 10 && mtmp->mhp * fraction >= mtmp->mhpmax))
547.             return FALSE;
548. 
549.         if (mtmp->mpeaceful) {
550.             if (!nohands(mtmp->data)) {
551.                 if (m_use_healing(mtmp))
552.                     return TRUE;
553.             }
554.             return FALSE;
555.         }
556.     }
557. 
558.     if (stuck || immobile || mtmp->mtrapped) {
559.         ; /* fleeing by stairs or traps is not possible */
560.     } else if (levl[x][y].typ == STAIRS) {
561.         stway = stairway_at(x,y);
562.         if (stway && !stway->up && stway->tolev.dnum == u.uz.dnum) {
563.             if (!is_floater(mtmp->data))
564.                 gm.m.has_defense = MUSE_DOWNSTAIRS;
565.         } else if (stway && stway->up && stway->tolev.dnum == u.uz.dnum) {
566.             gm.m.has_defense = MUSE_UPSTAIRS;
567.         } else if (stway &&  stway->tolev.dnum != u.uz.dnum) {
568.             if (stway->up || !is_floater(mtmp->data))
569.                 gm.m.has_defense = MUSE_SSTAIRS;
570.         }
571.     } else if (levl[x][y].typ == LADDER) {
572.         stway = stairway_at(x,y);
573.         if (stway && stway->up && stway->tolev.dnum == u.uz.dnum) {
574.             gm.m.has_defense = MUSE_UP_LADDER;
575.         } else if (stway && !stway->up && stway->tolev.dnum == u.uz.dnum) {
576.             if (!is_floater(mtmp->data))
577.                 gm.m.has_defense = MUSE_DN_LADDER;
578.         } else if (stway && stway->tolev.dnum != u.uz.dnum) {
579.             if (stway->up || !is_floater(mtmp->data))
580.                 gm.m.has_defense = MUSE_SSTAIRS;
581.         }
582.     } else {
583.         /* Note: trap doors take precedence over teleport traps. */
584.         coordxy xx, yy, i, locs[10][2];
585.         boolean ignore_boulders = (verysmall(mtmp->data)
586.                                    || throws_rocks(mtmp->data)
587.                                    || passes_walls(mtmp->data)),
588.             diag_ok = !NODIAG(monsndx(mtmp->data));
589. 
590.         for (i = 0; i < 10; ++i) /* 10: 9 spots plus sentinel */
591.             locs[i][0] = locs[i][1] = 0;
592.         /* collect viable spots; monster's <mx,my> comes first */
593.         locs[0][0] = x, locs[0][1] = y;
594.         i = 1;
595.         for (xx = x - 1; xx <= x + 1; xx++)
596.             for (yy = y - 1; yy <= y + 1; yy++)
597.                 if (isok(xx, yy) && (xx != x || yy != y)) {
598.                     locs[i][0] = xx, locs[i][1] = yy;
599.                     ++i;
600.                 }
601.         /* look for a suitable trap among the viable spots */
602.         for (i = 0; i < 10; ++i) {
603.             xx = locs[i][0], yy = locs[i][1];
604.             if (!xx)
605.                 break; /* we've run out of spots */
606.             /* skip if it's hero's location
607.                or a diagonal spot and monster can't move diagonally
608.                or some other monster is there */
609.             if (u_at(xx, yy)
610.                 || (xx != x && yy != y && !diag_ok)
611.                 || (svl.level.monsters[xx][yy] && !(xx == x && yy == y)))
612.                 continue;
613.             /* skip if there's no trap or can't/won't move onto trap */
614.             if ((t = t_at(xx, yy)) == 0
615.                 || (!ignore_boulders && sobj_at(BOULDER, xx, yy))
616.                 || onscary(xx, yy, mtmp))
617.                 continue;
618.             /* use trap if it's the correct type */
619.             if (is_hole(t->ttyp)
620.                 && !is_floater(mtmp->data)
621.                 && !mtmp->isshk && !mtmp->isgd && !mtmp->ispriest
622.                 && Can_fall_thru(&u.uz)) {
623.                 gt.trapx = xx;
624.                 gt.trapy = yy;
625.                 gm.m.has_defense = MUSE_TRAPDOOR;
626.                 break; /* no need to look at any other spots */
627.             } else if (t->ttyp == TELEP_TRAP) {
628.                 gt.trapx = xx;
629.                 gt.trapy = yy;
630.                 gm.m.has_defense = MUSE_TELEPORT_TRAP;
631.             }
632.         }
633.     }
634. 
635.     if (nohands(mtmp->data)) /* can't use objects */
636.         goto botm;
637. 
638.     if (is_mercenary(mtmp->data) && (obj = m_carrying(mtmp, BUGLE)) != 0
639.         && m_sees_sleepy_soldier(mtmp)) {
640.         gm.m.defensive = obj;
641.         gm.m.has_defense = MUSE_BUGLE;
642.     }
643. 
644.     /* use immediate physical escape prior to attempting magic */
645.     if (gm.m.has_defense) /* stairs, trap door or tele-trap, bugle alert */
646.         goto botm;
647. 
648.     /* kludge to cut down on trap destruction (particularly portals) */
649.     t = t_at(x, y);
650.     if (t && (is_pit(t->ttyp) || t->ttyp == WEB
651.               || t->ttyp == BEAR_TRAP))
652.         t = 0; /* ok for monster to dig here */
653. 
654. #define nomore(x)       if (gm.m.has_defense == x) continue;
655.     /* selection could be improved by collecting all possibilities
656.        into an array and then picking one at random */
657.     for (obj = mtmp->minvent; obj; obj = obj->nobj) {
658.         /* don't always use the same selection pattern */
659.         if (gm.m.has_defense && !rn2(3))
660.             break;
661. 
662.         /* nomore(MUSE_WAN_DIGGING); */
663.         if (gm.m.has_defense == MUSE_WAN_DIGGING)
664.             break;
665.         if (obj->otyp == WAN_DIGGING && obj->spe > 0 && !stuck && !t
666.             && !mtmp->isshk && !mtmp->isgd && !mtmp->ispriest
667.             && !is_floater(mtmp->data)
668.             /* monsters digging in Sokoban can ruin things */
669.             && !Sokoban
670.             /* digging wouldn't be effective; assume they know that */
671.             && !(levl[x][y].wall_info & W_NONDIGGABLE)
672.             && !(Is_botlevel(&u.uz) || In_endgame(&u.uz))
673.             && !(is_ice(x, y) || is_pool(x, y) || is_lava(x, y))
674.             && !(is_Vlad(mtmp) && In_V_tower(&u.uz))) {
675.             gm.m.defensive = obj;
676.             gm.m.has_defense = MUSE_WAN_DIGGING;
677.         }
678.         nomore(MUSE_WAN_TELEPORTATION_SELF);
679.         nomore(MUSE_WAN_TELEPORTATION);
680.         if (obj->otyp == WAN_TELEPORTATION && obj->spe > 0) {
681.             /* use the TELEP_TRAP bit to determine if they know
682.              * about noteleport on this level or not.  Avoids
683.              * ineffective re-use of teleportation.  This does
684.              * mean if the monster leaves the level, they'll know
685.              * about teleport traps.
686.              */
687.             if (!noteleport_level(mtmp)
688.                 || !mon_knows_traps(mtmp, TELEP_TRAP)) {
689.                 gm.m.defensive = obj;
690.                 gm.m.has_defense = (mon_has_amulet(mtmp))
691.                                     ? MUSE_WAN_TELEPORTATION
692.                                     : MUSE_WAN_TELEPORTATION_SELF;
693.             }
694.         }
695.         nomore(MUSE_SCR_TELEPORTATION);
696.         if (obj->otyp == SCR_TELEPORTATION && mtmp->mcansee
697.             && haseyes(mtmp->data)
698.             && (!obj->cursed || (!(mtmp->isshk && inhishop(mtmp))
699.                                  && !mtmp->isgd && !mtmp->ispriest))) {
700.             /* see WAN_TELEPORTATION case above */
701.             if (!noteleport_level(mtmp)
702.                 || !mon_knows_traps(mtmp, TELEP_TRAP)) {
703.                 gm.m.defensive = obj;
704.                 gm.m.has_defense = MUSE_SCR_TELEPORTATION;
705.             }
706.         }
707. 
708.         if (mtmp->data != &mons[PM_PESTILENCE]) {
709.             nomore(MUSE_POT_FULL_HEALING);
710.             if (obj->otyp == POT_FULL_HEALING) {
711.                 gm.m.defensive = obj;
712.                 gm.m.has_defense = MUSE_POT_FULL_HEALING;
713.             }
714.             nomore(MUSE_POT_EXTRA_HEALING);
715.             if (obj->otyp == POT_EXTRA_HEALING) {
716.                 gm.m.defensive = obj;
717.                 gm.m.has_defense = MUSE_POT_EXTRA_HEALING;
718.             }
719.             nomore(MUSE_WAN_CREATE_MONSTER);
720.             if (obj->otyp == WAN_CREATE_MONSTER && obj->spe > 0) {
721.                 gm.m.defensive = obj;
722.                 gm.m.has_defense = MUSE_WAN_CREATE_MONSTER;
723.             }
724.             nomore(MUSE_POT_HEALING);
725.             if (obj->otyp == POT_HEALING) {
726.                 gm.m.defensive = obj;
727.                 gm.m.has_defense = MUSE_POT_HEALING;
728.             }
729.         } else { /* Pestilence */
730.             nomore(MUSE_POT_FULL_HEALING);
731.             if (obj->otyp == POT_SICKNESS) {
732.                 gm.m.defensive = obj;
733.                 gm.m.has_defense = MUSE_POT_FULL_HEALING;
734.             }
735.             nomore(MUSE_WAN_CREATE_MONSTER);
736.             if (obj->otyp == WAN_CREATE_MONSTER && obj->spe > 0) {
737.                 gm.m.defensive = obj;
738.                 gm.m.has_defense = MUSE_WAN_CREATE_MONSTER;
739.             }
740.         }
741.         nomore(MUSE_SCR_CREATE_MONSTER);
742.         if (obj->otyp == SCR_CREATE_MONSTER) {
743.             gm.m.defensive = obj;
744.             gm.m.has_defense = MUSE_SCR_CREATE_MONSTER;
745.         }
746.     }
747.  botm:
748.     return (boolean) !!gm.m.has_defense;
749. #undef nomore
750. }
751. 
752. /* when a monster deliberately enters a trap, make sure the spot becomes
753.    accessible (trap doors and teleporters inside niches are located at
754.    secret corridor locations; convert such into normal corridor even if
755.    hero doesn't see it happen) */
756. staticfn void
757. reveal_trap(struct trap *t, boolean seeit)
758. {
759.     struct rm *lev = &levl[t->tx][t->ty];
760. 
761.     if (lev->typ == SCORR) {
762.         lev->typ = CORR, lev->flags = 0; /* set_levltyp(,,CORR) */
763.         unblock_point(t->tx, t->ty);
764.     }
765.     if (seeit)
766.         seetrap(t);
767. }
768. 
769. /* Monsters without the Amulet escape the dungeon and
770.  * are gone for good when they leave up the up stairs.
771.  * A monster with the Amulet would leave it behind
772.  * (mongone -> mdrop_special_objs) but we force any
773.  * monster who manages to acquire it or the invocation
774.  * tools to stick around instead of letting it escape.
775.  * Don't let the Wizard escape even when not carrying
776.  * anything of interest unless there are more than 1
777.  * of him.
778.  */
779. staticfn int
780. mon_escape(struct monst *mtmp, boolean vismon)
781. {
782.     if (mon_has_special(mtmp)
783.         || (mtmp->iswiz && svc.context.no_of_wizards < 2))
784.         return 0;
785.     if (vismon)
786.         pline_mon(mtmp, "%s escapes the dungeon!", Monnam(mtmp));
787.     mongone(mtmp);
788.     return 2;
789. }
790. 
791. /* Perform a defensive action for a monster.  Must be called immediately
792.  * after find_defensive().  Return values are 0: did something, 1: died,
793.  * 2: did something and can't attack again (i.e. teleported).
794.  */
795. int
796. use_defensive(struct monst *mtmp)
797. {
798.     static const char MissingDefensiveItem[] = "use_defensive: no %s";
799.     int i, fleetim;
800.     struct obj *otmp = gm.m.defensive;
801.     boolean vis, vismon, oseen;
802.     struct trap *t;
803.     stairway *stway;
804. 
805.     if ((i = precheck(mtmp, otmp)) != 0)
806.         return i;
807.     vis = cansee(mtmp->mx, mtmp->my);
808.     vismon = canseemon(mtmp);
809.     oseen = otmp && vismon;
810. 
811.     /* when using defensive choice to run away, we want monster to avoid
812.        rushing right straight back; don't override if already scared */
813.     fleetim = !mtmp->mflee ? (33 - (30 * mtmp->mhp / mtmp->mhpmax)) : 0;
814. #define m_flee(m)                          \
815.     if (fleetim && !m->iswiz) {            \
816.         monflee(m, fleetim, FALSE, FALSE); \
817.     }
818. 
819.     switch (gm.m.has_defense) {
820.     case MUSE_UNICORN_HORN:
821.         /* unlike most defensive cases, unicorn horn object is optional */
822.         if (vismon) {
823.             if (otmp)
824.                 pline_mon(mtmp, "%s uses a unicorn horn!", Monnam(mtmp));
825.             else
826.                 pline_The("tip of %s's horn glows!", mon_nam(mtmp));
827.         }
828.         if (!mtmp->mcansee) {
829.             mcureblindness(mtmp, vismon);
830.         } else if (mtmp->mconf || mtmp->mstun) {
831.             mtmp->mconf = mtmp->mstun = 0;
832.             if (vismon)
833.                 pline_mon(mtmp, "%s seems steadier now.", Monnam(mtmp));
834.         } else {
835.             impossible("No need for unicorn horn?");
836.         }
837.         return 2;
838.     case MUSE_BUGLE:
839.         if (!otmp)
840.             panic(MissingDefensiveItem, "bugle");
841.         if (vismon) {
842.             pline_mon(mtmp, "%s plays %s!", Monnam(mtmp), doname(otmp));
843.         } else if (!Deaf) {
844.             Soundeffect(se_bugle_playing_reveille, 100);
845.             You_hear("a bugle playing reveille!");
846.         }
847.         awaken_soldiers(mtmp);
848.         return 2;
849.     case MUSE_WAN_TELEPORTATION_SELF:
850.         if (!otmp)
851.             panic(MissingDefensiveItem, "wand of teleportation");
852.         if ((mtmp->isshk && inhishop(mtmp)) || mtmp->isgd || mtmp->ispriest)
853.             return 2;
854.         m_flee(mtmp);
855.         mzapwand(mtmp, otmp, TRUE);
856.         m_tele(mtmp, vismon, oseen, WAN_TELEPORTATION);
857.         return 2;
858.     case MUSE_WAN_TELEPORTATION:
859.         if (!otmp)
860.             panic(MissingDefensiveItem, "wand of teleportation");
861.         gz.zap_oseen = oseen;
862.         mzapwand(mtmp, otmp, FALSE);
863.         gm.m_using = TRUE;
864.         mbhit(mtmp, rn1(8, 6), mbhitm, bhito, otmp);
865.         /* monster learns that teleportation isn't useful here */
866.         if (noteleport_level(mtmp))
867.             mon_learns_traps(mtmp, TELEP_TRAP);
868.         gm.m_using = FALSE;
869.         return 2;
870.     case MUSE_SCR_TELEPORTATION: {
871.         int obj_is_cursed;
872. 
873.         if (!otmp)
874.             panic(MissingDefensiveItem, "scroll of teleportation");
875.         obj_is_cursed = otmp->cursed;
876.         if (mtmp->isshk || mtmp->isgd || mtmp->ispriest)
877.             return 2;
878.         m_flee(mtmp);
879.         /* we want to be able to access otmp after the teleport but it
880.            might get destroyed if still in mtmp's inventory (maybe mtmp
881.            lands in lava or on a fire trap) so take it out in advance */
882.         if (otmp->quan > 1L)
883.             otmp = splitobj(otmp, 1L);
884.         extract_from_minvent(mtmp, otmp, FALSE, FALSE);
885.         /* 'last_msg' will be changed to PLNMSG_UNKNOWN if any messages
886.            are issued by mreadmsg(), 'if (vismon) pline()', or m_tele() */
887.         iflags.last_msg = PLNMSG_enum;
888.         mreadmsg(mtmp, otmp); /* sets otmp->dknown if !Blind or !Deaf */
889.         if (obj_is_cursed || mtmp->mconf) {
890.             int nlev;
891.             d_level flev;
892. 
893.             nlev = random_teleport_level();
894.             if (mon_has_amulet(mtmp) || In_endgame(&u.uz)) {
895.                 if (vismon)
896.                     pline_mon(mtmp, "%s seems very disoriented for a moment.",
897.                           Monnam(mtmp));
898.             } else if (nlev == depth(&u.uz)) {
899.                 if (vismon)
900.                     pline_mon(mtmp, "%s shudders for a moment.", Monnam(mtmp));
901.             } else {
902.                 get_level(&flev, nlev);
903.                 migrate_to_level(mtmp, ledger_no(&flev), MIGR_RANDOM,
904.                                  (coord *) 0);
905.             }
906.         } else {
907.             m_tele(mtmp, vismon, oseen, SCR_TELEPORTATION);
908.         }
909.         /* m_tele() handles makeknown(); trycall() will be a no-op when
910.            otmp->otyp is already discovered */
911.         if (otmp->dknown && iflags.last_msg != PLNMSG_enum)
912.             trycall(otmp);
913.         /* already removed from mtmp->minvent so not 'm_useup(mtmp, otmp)' */
914.         obfree(otmp, (struct obj *) 0);
915.         return 2;
916.     }
917.     case MUSE_WAN_DIGGING:
918.         if (!otmp)
919.             panic(MissingDefensiveItem, "wand of digging");
920.         m_flee(mtmp);
921.         mzapwand(mtmp, otmp, FALSE);
922.         if (oseen)
923.             makeknown(WAN_DIGGING);
924.         if (IS_FURNITURE(levl[mtmp->mx][mtmp->my].typ)
925.             || IS_DRAWBRIDGE(levl[mtmp->mx][mtmp->my].typ)
926.             || (is_drawbridge_wall(mtmp->mx, mtmp->my) >= 0)
927.             || stairway_at(mtmp->mx, mtmp->my)) {
928.             pline_The("digging ray is ineffective.");
929.             return 2;
930.         }
931.         if (!Can_dig_down(&u.uz) && !levl[mtmp->mx][mtmp->my].candig) {
932.             /* can't dig further if there's already a pit (or other trap)
933.                here, or if pit creation fails for some reason */
934.             if (t_at(mtmp->mx, mtmp->my)
935.                 || !(t = maketrap(mtmp->mx, mtmp->my, PIT))) {
936.                 if (vismon) {
937.                     pline_The("%s here is too hard to dig in.",
938.                               surface(mtmp->mx, mtmp->my));
939.                 }
940.                 return 2;
941.             }
942.             /* pit creation succeeded */
943.             if (vis) {
944.                 seetrap(t);
945.                 pline_mon(mtmp, "%s has made a pit in the %s.", Monnam(mtmp),
946.                       surface(mtmp->mx, mtmp->my));
947.             }
948.             fill_pit(mtmp->mx, mtmp->my);
949.             recalc_block_point(mtmp->mx, mtmp->my);
950.             return (mintrap(mtmp, FORCEBUNGLE) == Trap_Killed_Mon) ? 1 : 2;
951.         }
952.         t = maketrap(mtmp->mx, mtmp->my, HOLE);
953.         if (!t)
954.             return 2;
955.         recalc_block_point(mtmp->mx, mtmp->my);
956.         seetrap(t);
957.         if (vis) {
958.             pline_mon(mtmp, "%s has made a hole in the %s.", Monnam(mtmp),
959.                   surface(mtmp->mx, mtmp->my));
960.             pline_mon(mtmp, "%s %s through...", Monnam(mtmp),
961.                   is_flyer(mtmp->data) ? "dives" : "falls");
962.         } else if (!Deaf) {
963.             Soundeffect(se_crash_through_floor, 100);
964.             You_hear("%s crash through the %s.", something,
965.                      surface(mtmp->mx, mtmp->my));
966.         }
967.         fill_pit(mtmp->mx, mtmp->my);
968.         /* we made sure that there is a level for mtmp to go to */
969.         migrate_to_level(mtmp, ledger_no(&u.uz) + 1, MIGR_RANDOM,
970.                          (coord *) 0);
971.         return 2;
972.     case MUSE_WAN_UNDEAD_TURNING:
973.         if (!otmp)
974.             panic(MissingDefensiveItem, "wand of undead turning");
975.         gz.zap_oseen = oseen;
976.         mzapwand(mtmp, otmp, FALSE);
977.         gm.m_using = TRUE;
978.         mbhit(mtmp, rn1(8, 6), mbhitm, bhito, otmp);
979.         gm.m_using = FALSE;
980.         return 2;
981.     case MUSE_WAN_CREATE_MONSTER: {
982.         coord cc;
983.         struct monst *mon;
984.         /* pm: 0 => random, eel => aquatic, croc => amphibious */
985.         struct permonst *pm = !is_pool(mtmp->mx, mtmp->my) ? 0
986.                             : &mons[u.uinwater ? PM_GIANT_EEL : PM_CROCODILE];
987. 
988.         if (!otmp)
989.             panic(MissingDefensiveItem, "wand of create monster");
990.         if (!enexto(&cc, mtmp->mx, mtmp->my, pm))
991.             return 0;
992.         mzapwand(mtmp, otmp, FALSE);
993.         mon = makemon((struct permonst *) 0, cc.x, cc.y, NO_MM_FLAGS);
994.         if (mon && canspotmon(mon) && oseen)
995.             makeknown(WAN_CREATE_MONSTER);
996.         return 2;
997.     }
998.     case MUSE_SCR_CREATE_MONSTER: {
999.         coord cc;
1000.         struct permonst *pm = 0, *fish = 0;
1001.         int cnt = 1;
1002.         struct monst *mon;
1003.         boolean known = FALSE;
1004. 
1005.         if (!otmp)
1006.             panic(MissingDefensiveItem, "scroll of create monster");
1007.         if (!rn2(73))
1008.             cnt += rnd(4);
1009.         if (mtmp->mconf || otmp->cursed)
1010.             cnt += 12;
1011.         if (mtmp->mconf)
1012.             pm = fish = &mons[PM_ACID_BLOB];
1013.         else if (is_pool(mtmp->mx, mtmp->my))
1014.             fish = &mons[u.uinwater ? PM_GIANT_EEL : PM_CROCODILE];
1015.         mreadmsg(mtmp, otmp);
1016.         while (cnt--) {
1017.             /* `fish' potentially gives bias towards water locations;
1018.                `pm' is what to actually create (0 => random) */
1019.             if (!enexto(&cc, mtmp->mx, mtmp->my, fish))
1020.                 break;
1021.             mon = makemon(pm, cc.x, cc.y, NO_MM_FLAGS);
1022.             if (mon && canspotmon(mon))
1023.                 known = TRUE;
1024.         }
1025.         /* The only case where we don't use oseen.  For wands, you
1026.          * have to be able to see the monster zap the wand to know
1027.          * what type it is.  For teleport scrolls, you have to see
1028.          * the monster to know it teleported.
1029.          */
1030.         if (known)
1031.             makeknown(SCR_CREATE_MONSTER);
1032.         else
1033.             trycall(otmp);
1034.         m_useup(mtmp, otmp);
1035.         return 2;
1036.     }
1037.     case MUSE_TRAPDOOR:
1038.         /* trap doors on "bottom" levels of dungeons are rock-drop
1039.          * trap doors, not holes in the floor.  We check here for
1040.          * safety.
1041.          */
1042.         if (Is_botlevel(&u.uz))
1043.             return 0;
1044.         m_flee(mtmp);
1045.         t = t_at(gt.trapx, gt.trapy);
1046.         if (vis) {
1047.             pline_mon(mtmp, "%s %s into a %s!", Monnam(mtmp),
1048.                   vtense(fakename[0], locomotion(mtmp->data, "jump")),
1049.                   trapname(t->ttyp, FALSE));
1050.         }
1051.         /* if trap was in a concealed niche, it's no longer concealed */
1052.         reveal_trap(t, vis);
1053. 
1054.         /*  don't use rloc_to() because worm tails must "move" */
1055.         remove_monster(mtmp->mx, mtmp->my);
1056.         newsym(mtmp->mx, mtmp->my); /* update old location */
1057.         place_monster(mtmp, gt.trapx, gt.trapy);
1058.         if (mtmp->wormno)
1059.             worm_move(mtmp);
1060.         newsym(gt.trapx, gt.trapy);
1061. 
1062.         migrate_to_level(mtmp, ledger_no(&u.uz) + 1, MIGR_RANDOM,
1063.                          (coord *) 0);
1064.         return 2;
1065.     case MUSE_UPSTAIRS:
1066.         m_flee(mtmp);
1067.         stway = stairway_at(mtmp->mx, mtmp->my);
1068.         if (!stway)
1069.             return 0;
1070.         if (ledger_no(&u.uz) == 1)
1071.             /* impossible; level 1 upstairs are SSTAIRS */
1072.             return mon_escape(mtmp, vismon);
1073.         if (Inhell && mon_has_amulet(mtmp) && !rn2(4)
1074.             && (dunlev(&u.uz) < dunlevs_in_dungeon(&u.uz) - 3)) {
1075.             if (vismon)
1076.                 pline("As %s climbs the stairs, a mysterious force"
1077.                       " momentarily surrounds %s...",
1078.                       mon_nam(mtmp), mhim(mtmp));
1079.             /* simpler than for the player; this will usually be
1080.                the Wizard and he'll immediately go right to the
1081.                upstairs, so there's not much point in having any
1082.                chance for a random position on the current level */
1083.             migrate_to_level(mtmp, ledger_no(&u.uz) + 1, MIGR_RANDOM,
1084.                              (coord *) 0);
1085.         } else {
1086.             if (vismon)
1087.                 pline_mon(mtmp, "%s escapes upstairs!", Monnam(mtmp));
1088.             migrate_to_level(mtmp, ledger_no(&(stway->tolev)),
1089.                              MIGR_STAIRS_DOWN, (coord *) 0);
1090.         }
1091.         return 2;
1092.     case MUSE_DOWNSTAIRS:
1093.         m_flee(mtmp);
1094.         stway = stairway_at(mtmp->mx, mtmp->my);
1095.         if (!stway)
1096.             return 0;
1097.         if (vismon)
1098.             pline_mon(mtmp, "%s escapes downstairs!", Monnam(mtmp));
1099.         migrate_to_level(mtmp, ledger_no(&(stway->tolev)), MIGR_STAIRS_UP,
1100.                          (coord *) 0);
1101.         return 2;
1102.     case MUSE_UP_LADDER:
1103.         m_flee(mtmp);
1104.         stway = stairway_at(mtmp->mx, mtmp->my);
1105.         if (!stway)
1106.             return 0;
1107.         if (vismon)
1108.             pline_mon(mtmp, "%s escapes up the ladder!", Monnam(mtmp));
1109.         migrate_to_level(mtmp, ledger_no(&(stway->tolev)), MIGR_LADDER_DOWN,
1110.                          (coord *) 0);
1111.         return 2;
1112.     case MUSE_DN_LADDER:
1113.         m_flee(mtmp);
1114.         stway = stairway_at(mtmp->mx, mtmp->my);
1115.         if (!stway)
1116.             return 0;
1117.         if (vismon)
1118.             pline_mon(mtmp, "%s escapes down the ladder!", Monnam(mtmp));
1119.         migrate_to_level(mtmp, ledger_no(&(stway->tolev)), MIGR_LADDER_UP,
1120.                          (coord *) 0);
1121.         return 2;
1122.     case MUSE_SSTAIRS:
1123.         m_flee(mtmp);
1124.         stway = stairway_at(mtmp->mx, mtmp->my);
1125.         if (!stway)
1126.             return 0;
1127.         if (ledger_no(&u.uz) == 1) {
1128.             return mon_escape(mtmp, vismon);
1129.         }
1130.         if (vismon)
1131.             pline_mon(mtmp, "%s escapes %sstairs!", Monnam(mtmp),
1132.                   stway->up ? "up" : "down");
1133.         /* going from the Valley to Castle (Stronghold) has no sstairs
1134.            to target, but having gs.sstairs.<sx,sy> == <0,0> will work the
1135.            same as specifying MIGR_RANDOM when mon_arrive() eventually
1136.            places the monster, so we can use MIGR_SSTAIRS unconditionally */
1137.         migrate_to_level(mtmp, ledger_no(&(stway->tolev)), MIGR_SSTAIRS,
1138.                          (coord *) 0);
1139.         return 2;
1140.     case MUSE_TELEPORT_TRAP:
1141.         m_flee(mtmp);
1142.         t = t_at(gt.trapx, gt.trapy);
1143.         if (vis) {
1144.             pline_mon(mtmp, "%s %s onto a %s!", Monnam(mtmp),
1145.                   vtense(fakename[0], locomotion(mtmp->data, "jump")),
1146.                   trapname(t->ttyp, FALSE));
1147.         }
1148.         /* if trap was in a concealed niche, it's no longer concealed */
1149.         reveal_trap(t, vis);
1150.         /*  don't use rloc_to() because worm tails must "move" */
1151.         remove_monster(mtmp->mx, mtmp->my);
1152.         newsym(mtmp->mx, mtmp->my); /* update old location */
1153.         place_monster(mtmp, gt.trapx, gt.trapy);
1154.         if (mtmp->wormno)
1155.             worm_move(mtmp);
1156.         maybe_unhide_at(mtmp->mx, mtmp->my);
1157.         newsym(gt.trapx, gt.trapy);
1158.         /* 0: 'no object' rather than STRANGE_OBJECT; FALSE: obj not seen */
1159.         m_tele(mtmp, vismon, FALSE, 0);
1160.         return 2;
1161.     case MUSE_POT_HEALING:
1162.         if (!otmp)
1163.             panic(MissingDefensiveItem, "potioh of healing");
1164.         mquaffmsg(mtmp, otmp);
1165.         i = d(6 + 2 * bcsign(otmp), 4);
1166.         healmon(mtmp, i, 1);
1167.         if (!otmp->cursed && !mtmp->mcansee)
1168.             mcureblindness(mtmp, vismon);
1169.         if (vismon)
1170.             pline_mon(mtmp, "%s looks better.", Monnam(mtmp));
1171.         if (oseen)
1172.             makeknown(POT_HEALING);
1173.         m_useup(mtmp, otmp);
1174.         return 2;
1175.     case MUSE_POT_EXTRA_HEALING:
1176.         if (!otmp)
1177.             panic(MissingDefensiveItem, "potioh of extra healing");
1178.         mquaffmsg(mtmp, otmp);
1179.         i = d(6 + 2 * bcsign(otmp), 8);
1180.         healmon(mtmp, i, otmp->blessed ? 5 : 2);
1181.         if (!mtmp->mcansee)
1182.             mcureblindness(mtmp, vismon);
1183.         if (vismon)
1184.             pline_mon(mtmp, "%s looks much better.", Monnam(mtmp));
1185.         if (oseen)
1186.             makeknown(POT_EXTRA_HEALING);
1187.         m_useup(mtmp, otmp);
1188.         return 2;
1189.     case MUSE_POT_FULL_HEALING:
1190.         if (!otmp)
1191.             panic(MissingDefensiveItem, "potioh of full healing");
1192.         mquaffmsg(mtmp, otmp);
1193.         if (otmp->otyp == POT_SICKNESS)
1194.             unbless(otmp); /* Pestilence */
1195.         healmon(mtmp, mtmp->mhpmax, otmp->blessed ? 8 : 4);
1196.         if (!mtmp->mcansee && otmp->otyp != POT_SICKNESS)
1197.             mcureblindness(mtmp, vismon);
1198.         if (vismon)
1199.             pline_mon(mtmp, "%s looks completely healed.", Monnam(mtmp));
1200.         if (oseen)
1201.             makeknown(otmp->otyp);
1202.         m_useup(mtmp, otmp);
1203.         return 2;
1204.     case MUSE_LIZARD_CORPSE:
1205.         if (!otmp)
1206.             panic(MissingDefensiveItem, "lizard corpse");
1207.         /* not actually called for its unstoning effect */
1208.         mon_consume_unstone(mtmp, otmp, FALSE, FALSE);
1209.         return 2;
1210.     case 0:
1211.         return 0; /* i.e. an exploded wand */
1212.     default:
1213.         impossible("%s wanted to perform action %d?", Monnam(mtmp),
1214.                    gm.m.has_defense);
1215.         break;
1216.     }
1217.     return 0;
1218. #undef m_flee
1219. }
1220. 
1221. int
1222. rnd_defensive_item(struct monst *mtmp)
1223. {
1224.     struct permonst *pm = mtmp->data;
1225.     int difficulty = mons[(monsndx(pm))].difficulty;
1226.     int trycnt = 0;
1227. 
1228.     if (is_animal(pm) || attacktype(pm, AT_EXPL) || mindless(mtmp->data)
1229.         || pm->mlet == S_GHOST || pm->mlet == S_KOP)
1230.         return 0;
1231.  try_again:
1232.     switch (rn2(8 + (difficulty > 3) + (difficulty > 6) + (difficulty > 8))) {
1233.     case 6:
1234.     case 9:
1235.         if (noteleport_level(mtmp) && ++trycnt < 2)
1236.             goto try_again;
1237.         if (!rn2(3))
1238.             return WAN_TELEPORTATION;
1239.         FALLTHROUGH;
1240.         /*FALLTHRU*/
1241.     case 0:
1242.     case 1:
1243.         return SCR_TELEPORTATION;
1244.     case 8:
1245.     case 10:
1246.         if (!rn2(3))
1247.             return WAN_CREATE_MONSTER;
1248.         FALLTHROUGH;
1249.         /*FALLTHRU*/
1250.     case 2:
1251.         return SCR_CREATE_MONSTER;
1252.     case 3:
1253.         return POT_HEALING;
1254.     case 4:
1255.         return POT_EXTRA_HEALING;
1256.     case 5:
1257.         return (mtmp->data != &mons[PM_PESTILENCE]) ? POT_FULL_HEALING
1258.                                                     : POT_SICKNESS;
1259.     case 7: /* wand of digging */
1260.         /* usually avoid digging in Sokoban */
1261.         if (Sokoban && rn2(4))
1262.             goto try_again;
1263.         /* some creatures shouldn't dig down to another level when hurt */
1264.         if (is_floater(pm) || mtmp->isshk || mtmp->isgd || mtmp->ispriest)
1265.             return 0;
1266.         return WAN_DIGGING;
1267.     }
1268.     /*NOTREACHED*/
1269.     return 0;
1270. }
1271. 
1272. #define MUSE_WAN_DEATH 1
1273. #define MUSE_WAN_SLEEP 2
1274. #define MUSE_WAN_FIRE 3
1275. #define MUSE_WAN_COLD 4
1276. #define MUSE_WAN_LIGHTNING 5
1277. #define MUSE_WAN_MAGIC_MISSILE 6
1278. #define MUSE_WAN_STRIKING 7
1279. #define MUSE_SCR_FIRE 8
1280. #define MUSE_POT_PARALYSIS 9
1281. #define MUSE_POT_BLINDNESS 10
1282. #define MUSE_POT_CONFUSION 11
1283. #define MUSE_FROST_HORN 12
1284. #define MUSE_FIRE_HORN 13
1285. #define MUSE_POT_ACID 14
1286. #define MUSE_WAN_TELEPORTATION 15
1287. #define MUSE_POT_SLEEPING 16
1288. #define MUSE_SCR_EARTH 17
1289. #define MUSE_CAMERA 18
1290. /*#define MUSE_WAN_UNDEAD_TURNING 20*/ /* also a defensive item so don't
1291.                                      * redefine; nonconsecutive value is ok */
1292. 
1293. staticfn boolean
1294. linedup_chk_corpse(coordxy x, coordxy y)
1295. {
1296.     return (sobj_at(CORPSE, x, y) != 0);
1297. }
1298. 
1299. staticfn void
1300. m_use_undead_turning(struct monst *mtmp, struct obj *obj)
1301. {
1302.     coordxy ax = u.ux + sgn(mtmp->mux - mtmp->mx) * 3,
1303.             ay = u.uy + sgn(mtmp->muy - mtmp->my) * 3;
1304.     coordxy bx = mtmp->mx, by = mtmp->my;
1305. 
1306.     if (!(obj->otyp == WAN_UNDEAD_TURNING && obj->spe > 0))
1307.         return;
1308. 
1309.     /* not necrophiliac(); unlike deciding whether to pick this
1310.        type of wand up, we aren't interested in corpses within
1311.        carried containers until they're moved into open inventory;
1312.        we don't check whether hero is poly'd into an undead--the
1313.        wand's turning effect is too weak to be a useful direct
1314.        attack--only whether hero is carrying at least one corpse */
1315.     if (carrying(CORPSE)
1316.         /*
1317.          * If hero is carrying one or more corpses but isn't wielding
1318.          * a cockatrice corpse (unless being hit by one won't do
1319.          * the monster much harm); otherwise we'd be using this wand
1320.          * as a defensive item with higher priority.
1321.          *
1322.          * Might be cockatrice intended as a weapon (or being denied
1323.          * to glove-wearing monsters for use as a weapon) or lizard
1324.          * intended as a cure or lichen intended as veggy food or
1325.          * sacrifice fodder being lugged to an altar.  Zapping with
1326.          * this will deprive hero of one from each stack although
1327.          * they might subsequently be recovered after killing again.
1328.          * In the sacrifice fodder case, it could even be to the
1329.          * player's advantage (fresher corpse if a new one gets
1330.          * dropped; player might not choose to spend a wand charge
1331.          * on that when/if hero acquires this wand).
1332.          */
1333.         || linedup_callback(ax, ay, bx, by, linedup_chk_corpse)
1334.         /* or there's a corpse on the ground in a direct line from the
1335.            monster to the hero, and up to 3 steps beyond. */
1336.         ) {
1337.         gm.m.offensive = obj;
1338.         gm.m.has_offense = MUSE_WAN_UNDEAD_TURNING;
1339.     }
1340. }
1341. 
1342. /* from monster's point of view, is hero behind a chokepoint? */
1343. staticfn boolean
1344. hero_behind_chokepoint(struct monst *mtmp)
1345. {
1346.     coordxy dx = sgn(mtmp->mx - mtmp->mux);
1347.     coordxy dy = sgn(mtmp->my - mtmp->muy);
1348. 
1349.     coordxy x = mtmp->mux + dx;
1350.     coordxy y = mtmp->muy + dy;
1351. 
1352.     int dir = xytodir(dx, dy);
1353.     int dir_l = DIR_CLAMP(DIR_LEFT2(dir));
1354.     int dir_r = DIR_CLAMP(DIR_RIGHT2(dir));
1355. 
1356.     coord c1, c2;
1357. 
1358.     dirtocoord(&c1, dir_l);
1359.     dirtocoord(&c2, dir_r);
1360.     c1.x += x, c2.x += x;
1361.     c1.y += y, c2.y += y;
1362. 
1363.     if ((!isok(c1.x, c1.y) || !accessible(c1.x, c1.y))
1364.         && (!isok(c2.x, c2.y) || !accessible(c2.x, c2.y)))
1365.         return TRUE;
1366.     return FALSE;
1367. }
1368. 
1369. /* hostile monster has another hostile next to it */
1370. staticfn boolean
1371. mon_has_friends(struct monst *mtmp)
1372. {
1373.     coordxy dx, dy;
1374.     struct monst *mon2;
1375. 
1376.     if (mtmp->mtame || mtmp->mpeaceful)
1377.         return FALSE;
1378. 
1379.     for (dx = -1; dx <= 1; dx++)
1380.         for (dy = -1; dy <= 1; dy++) {
1381.             coordxy x = mtmp->mx + dx;
1382.             coordxy y = mtmp->my + dy;
1383. 
1384.             if (isok(x, y) && (mon2 = m_at(x, y)) != 0
1385.                 && mon2 != mtmp
1386.                 && !mon2->mtame && !mon2->mpeaceful)
1387.                 return TRUE;
1388.         }
1389. 
1390.     return FALSE;
1391. }
1392. 
1393. /* does monster like object pile at x,y? */
1394. staticfn boolean
1395. mon_likes_objpile_at(struct monst *mtmp, coordxy x, coordxy y)
1396. {
1397.     int i;
1398.     struct obj *otmp;
1399. 
1400.     if (!isok(x,y) || !OBJ_AT(x,y))
1401.         return FALSE;
1402. 
1403.     /* monster likes any of the top 3 items in the pile? */
1404.     for (i = 0, otmp = svl.level.objects[x][y]; otmp && i < 3; i++) {
1405.         if (mon_would_take_item(mtmp, otmp))
1406.             return TRUE;
1407.         otmp = otmp->nexthere;
1408.     }
1409. 
1410.     /* pile is larger than 3 stacks? */
1411.     if (i >= 3)
1412.         return TRUE;
1413. 
1414.     return FALSE;
1415. }
1416. 
1417. /* Select an offensive item/action for a monster.  Returns TRUE iff one is
1418.  * found.
1419.  */
1420. boolean
1421. find_offensive(struct monst *mtmp)
1422. {
1423.     struct obj *obj, *mtmp_helmet;
1424.     boolean reflection_skip;
1425. 
1426.     gm.m.offensive = (struct obj *) 0;
1427.     gm.m.has_offense = 0;
1428.     if (mtmp->mpeaceful || is_animal(mtmp->data) || mindless(mtmp->data)
1429.         || nohands(mtmp->data))
1430.         return FALSE;
1431.     if (u.uswallow)
1432.         return FALSE;
1433.     if (in_your_sanctuary(mtmp, 0, 0))
1434.         return FALSE;
1435.     if (dmgtype(mtmp->data, AD_HEAL)
1436.         && !uwep && !uarmu && !uarm && !uarmh
1437.         && !uarms && !uarmg && !uarmc && !uarmf)
1438.         return FALSE;
1439.     /* all offensive items require orthogonal or diagonal targeting */
1440.     if (!lined_up(mtmp))
1441.         return FALSE;
1442. 
1443. #define nomore(x)       if (gm.m.has_offense == x) continue;
1444.     reflection_skip = (m_seenres(mtmp, M_SEEN_REFL) != 0
1445.                        || monnear(mtmp, mtmp->mux, mtmp->muy));
1446.     mtmp_helmet = which_armor(mtmp, W_ARMH);
1447.     /* this picks the last viable item rather than prioritizing choices */
1448.     for (obj = mtmp->minvent; obj; obj = obj->nobj) {
1449.         if (!reflection_skip) {
1450.             nomore(MUSE_WAN_DEATH);
1451.             if (obj->otyp == WAN_DEATH && obj->spe > 0
1452.                 && !m_seenres(mtmp, M_SEEN_MAGR)) {
1453.                 gm.m.offensive = obj;
1454.                 gm.m.has_offense = MUSE_WAN_DEATH;
1455.             }
1456.             nomore(MUSE_WAN_SLEEP);
1457.             if (obj->otyp == WAN_SLEEP && obj->spe > 0 && gm.multi >= 0
1458.                 && !m_seenres(mtmp, M_SEEN_SLEEP)) {
1459.                 gm.m.offensive = obj;
1460.                 gm.m.has_offense = MUSE_WAN_SLEEP;
1461.             }
1462.             nomore(MUSE_WAN_FIRE);
1463.             if (obj->otyp == WAN_FIRE && obj->spe > 0
1464.                 && !m_seenres(mtmp, M_SEEN_FIRE)) {
1465.                 gm.m.offensive = obj;
1466.                 gm.m.has_offense = MUSE_WAN_FIRE;
1467.             }
1468.             nomore(MUSE_FIRE_HORN);
1469.             if (obj->otyp == FIRE_HORN && obj->spe > 0 && can_blow(mtmp)
1470.                 && !m_seenres(mtmp, M_SEEN_FIRE)) {
1471.                 gm.m.offensive = obj;
1472.                 gm.m.has_offense = MUSE_FIRE_HORN;
1473.             }
1474.             nomore(MUSE_WAN_COLD);
1475.             if (obj->otyp == WAN_COLD && obj->spe > 0
1476.                 && !m_seenres(mtmp, M_SEEN_COLD)) {
1477.                 gm.m.offensive = obj;
1478.                 gm.m.has_offense = MUSE_WAN_COLD;
1479.             }
1480.             nomore(MUSE_FROST_HORN);
1481.             if (obj->otyp == FROST_HORN && obj->spe > 0 && can_blow(mtmp)
1482.                 && !m_seenres(mtmp, M_SEEN_COLD)) {
1483.                 gm.m.offensive = obj;
1484.                 gm.m.has_offense = MUSE_FROST_HORN;
1485.             }
1486.             nomore(MUSE_WAN_LIGHTNING);
1487.             if (obj->otyp == WAN_LIGHTNING && obj->spe > 0
1488.                 && !m_seenres(mtmp, M_SEEN_ELEC)) {
1489.                 gm.m.offensive = obj;
1490.                 gm.m.has_offense = MUSE_WAN_LIGHTNING;
1491.             }
1492.             nomore(MUSE_WAN_MAGIC_MISSILE);
1493.             if (obj->otyp == WAN_MAGIC_MISSILE && obj->spe > 0
1494.                 && !m_seenres(mtmp, M_SEEN_MAGR)) {
1495.                 gm.m.offensive = obj;
1496.                 gm.m.has_offense = MUSE_WAN_MAGIC_MISSILE;
1497.             }
1498.         }
1499.         nomore(MUSE_WAN_UNDEAD_TURNING);
1500.         m_use_undead_turning(mtmp, obj);
1501.         nomore(MUSE_WAN_STRIKING);
1502.         if (obj->otyp == WAN_STRIKING && obj->spe > 0
1503.             && !m_seenres(mtmp, M_SEEN_MAGR)) {
1504.             gm.m.offensive = obj;
1505.             gm.m.has_offense = MUSE_WAN_STRIKING;
1506.         }
1507.         nomore(MUSE_WAN_TELEPORTATION);
1508.         if (obj->otyp == WAN_TELEPORTATION && obj->spe > 0
1509.             /* don't give controlled hero a free teleport */
1510.             && !Teleport_control
1511.             /* same hack as MUSE_WAN_TELEPORTATION_SELF */
1512.             && (!noteleport_level(mtmp)
1513.                 || !mon_knows_traps(mtmp, TELEP_TRAP))
1514.             /* do try to move hero to a more vulnerable spot */
1515.             && (onscary(u.ux, u.uy, mtmp)
1516.                 || (hero_behind_chokepoint(mtmp) && mon_has_friends(mtmp))
1517.                 || mon_likes_objpile_at(mtmp, u.ux, u.uy)
1518.                 || stairway_at(u.ux, u.uy))) {
1519.             gm.m.offensive = obj;
1520.             gm.m.has_offense = MUSE_WAN_TELEPORTATION;
1521.         }
1522.         nomore(MUSE_POT_PARALYSIS);
1523.         if (obj->otyp == POT_PARALYSIS && gm.multi >= 0) {
1524.             gm.m.offensive = obj;
1525.             gm.m.has_offense = MUSE_POT_PARALYSIS;
1526.         }
1527.         nomore(MUSE_POT_BLINDNESS);
1528.         if (obj->otyp == POT_BLINDNESS && !attacktype(mtmp->data, AT_GAZE)) {
1529.             gm.m.offensive = obj;
1530.             gm.m.has_offense = MUSE_POT_BLINDNESS;
1531.         }
1532.         nomore(MUSE_POT_CONFUSION);
1533.         if (obj->otyp == POT_CONFUSION) {
1534.             gm.m.offensive = obj;
1535.             gm.m.has_offense = MUSE_POT_CONFUSION;
1536.         }
1537.         nomore(MUSE_POT_SLEEPING);
1538.         if (obj->otyp == POT_SLEEPING
1539.             && !m_seenres(mtmp, M_SEEN_SLEEP)) {
1540.             gm.m.offensive = obj;
1541.             gm.m.has_offense = MUSE_POT_SLEEPING;
1542.         }
1543.         nomore(MUSE_POT_ACID);
1544.         if (obj->otyp == POT_ACID
1545.             && !m_seenres(mtmp, M_SEEN_ACID)) {
1546.             gm.m.offensive = obj;
1547.             gm.m.has_offense = MUSE_POT_ACID;
1548.         }
1549.         /* we can safely put this scroll here since the locations that
1550.          * are in a 1 square radius are a subset of the locations that
1551.          * are in wand or throwing range (in other words, always lined_up())
1552.          */
1553.         nomore(MUSE_SCR_EARTH);
1554.         if (obj->otyp == SCR_EARTH
1555.             && (hard_helmet(mtmp_helmet) || mtmp->mconf
1556.                 || amorphous(mtmp->data) || passes_walls(mtmp->data)
1557.                 || noncorporeal(mtmp->data) || unsolid(mtmp->data)
1558.                 || !rn2(10))
1559.             && dist2(mtmp->mx, mtmp->my, mtmp->mux, mtmp->muy) <= 2
1560.             && mtmp->mcansee && haseyes(mtmp->data)
1561.             && !Is_rogue_level(&u.uz)
1562.             && (!In_endgame(&u.uz) || Is_earthlevel(&u.uz))) {
1563.             gm.m.offensive = obj;
1564.             gm.m.has_offense = MUSE_SCR_EARTH;
1565.         }
1566.         nomore(MUSE_CAMERA);
1567.         if (obj->otyp == EXPENSIVE_CAMERA
1568.             && ((!Blind && !resists_blnd(&gy.youmonst))
1569.                 || hates_light(gy.youmonst.data))
1570.             && dist2(mtmp->mx, mtmp->my, mtmp->mux, mtmp->muy) <= 2
1571.             && obj->spe > 0 && !rn2(6)) {
1572.             gm.m.offensive = obj;
1573.             gm.m.has_offense = MUSE_CAMERA;
1574.         }
1575. #if 0
1576.         nomore(MUSE_SCR_FIRE);
1577.         if (obj->otyp == SCR_FIRE && resists_fire(mtmp)
1578.             && dist2(mtmp->mx, mtmp->my, mtmp->mux, mtmp->muy) <= 2
1579.             && mtmp->mcansee && haseyes(mtmp->data)
1580.             && !m_seenres(mtmp, M_SEEN_FIRE)) {
1581.             /*
1582.              * TODO?
1583.              *  Could choose scroll if <mux,muy> (where attacker thinks
1584.              *  hero is located) is ice and attacker either isn't also
1585.              *  on ice or is able to fly/float/swim.
1586.              */
1587.             gm.m.offensive = obj;
1588.             gm.m.has_offense = MUSE_SCR_FIRE;
1589.         }
1590. #endif /* 0 */
1591.     }
1592.     return (boolean) !!gm.m.has_offense;
1593. #undef nomore
1594. }
1595. 
1596. staticfn int
1597. mbhitm(struct monst *mtmp, struct obj *otmp)
1598. {
1599.     int tmp;
1600.     boolean reveal_invis = FALSE, learnit = FALSE,
1601.             hits_you = (mtmp == &gy.youmonst);
1602. 
1603.     if (!hits_you && otmp->otyp != WAN_UNDEAD_TURNING) {
1604.         mtmp->msleeping = 0;
1605.         if (mtmp->m_ap_type)
1606.             seemimic(mtmp);
1607.     }
1608.     switch (otmp->otyp) {
1609.     case WAN_STRIKING:
1610.         reveal_invis = TRUE;
1611.         if (hits_you) {
1612.             if (Antimagic) {
1613.                 monstseesu(M_SEEN_MAGR); /* monsters notice hero resisting */
1614.                 shieldeff(u.ux, u.uy);
1615.                 Soundeffect(se_boing, 40);
1616.                 pline("Boing!");
1617.                 learnit = TRUE;
1618.             } else if (rnd(20) < 10 + u.uac &&
1619.                        !(gb.buzzer && !gb.buzzer->mwandexp)) {
1620.                 monstunseesu(M_SEEN_MAGR); /* mons see hero not resisting */
1621.                 pline_The("wand hits you!");
1622.                 tmp = d(2, 12);
1623.                 if (Half_spell_damage)
1624.                     tmp = (tmp + 1) / 2;
1625.                 losehp(tmp, "wand", KILLED_BY_AN);
1626.                 learnit = TRUE;
1627.             } else {
1628.                 pline_The("wand misses you.");
1629.             }
1630.             stop_occupation();
1631.             nomul(0);
1632.         } else if (resists_magm(mtmp)) {
1633.             shieldeff(mtmp->mx, mtmp->my);
1634.             Soundeffect(se_boing, 40);
1635.             pline("Boing!");
1636.             learnit = TRUE;
1637.         } else if (rnd(20) < 10 + find_mac(mtmp)) {
1638.             tmp = d(2, 12);
1639.             hit("wand", mtmp, exclam(tmp));
1640.             (void) resist(mtmp, otmp->oclass, tmp, TELL);
1641.             learnit = TRUE;
1642.         } else {
1643.             miss("wand", mtmp);
1644.         }
1645.         /* need to see the wand being zapped and also the spot where the
1646.            target is hit; don't have to see the target itself though */
1647.         if (learnit && gz.zap_oseen && (hits_you
1648.                                         || cansee(mtmp->mx, mtmp->my)))
1649.             makeknown(WAN_STRIKING);
1650.         break;
1651.     case WAN_TELEPORTATION:
1652.         if (hits_you) {
1653.             tele();
1654.             if (gz.zap_oseen)
1655.                 makeknown(WAN_TELEPORTATION);
1656.         } else {
1657.             /* for consistency with zap.c, don't identify */
1658.             if (mtmp->ispriest && *in_rooms(mtmp->mx, mtmp->my, TEMPLE)) {
1659.                 if (cansee(mtmp->mx, mtmp->my))
1660.                     pline_mon(mtmp, "%s resists the magic!", Monnam(mtmp));
1661.             } else if (!tele_restrict(mtmp))
1662.                 (void) rloc(mtmp, RLOC_MSG);
1663.         }
1664.         break;
1665.     case WAN_CANCELLATION:
1666.     case SPE_CANCELLATION:
1667.         (void) cancel_monst(mtmp, otmp, FALSE, TRUE, FALSE);
1668.         break;
1669.     case WAN_UNDEAD_TURNING:
1670.         if (hits_you) {
1671.             unturn_you();
1672.             learnit = gz.zap_oseen;
1673.         } else {
1674.             boolean wake = FALSE;
1675. 
1676.             if (unturn_dead(mtmp)) /* affects mtmp's invent, not mtmp */
1677.                 wake = TRUE;
1678.             if (is_undead(mtmp->data) || is_vampshifter(mtmp)) {
1679.                 wake = reveal_invis = TRUE;
1680.                 /* context.bypasses=True: if resist() happens to be fatal,
1681.                    make_corpse() will set obj->bypass on the new corpse
1682.                    so that mbhito() will skip it instead of reviving it */
1683.                 svc.context.bypasses = TRUE; /* for make_corpse() */
1684.                 (void) resist(mtmp, WAND_CLASS, rnd(8), NOTELL);
1685.             }
1686.             if (wake) {
1687.                 if (!DEADMONSTER(mtmp))
1688.                     wakeup(mtmp, FALSE);
1689.                 learnit = gz.zap_oseen;
1690.             }
1691.         }
1692.         if (learnit)
1693.             makeknown(WAN_UNDEAD_TURNING);
1694.         break;
1695.     default:
1696.         break;
1697.     }
1698.     if (reveal_invis && !DEADMONSTER(mtmp)
1699.         && cansee(gb.bhitpos.x, gb.bhitpos.y) && !canspotmon(mtmp))
1700.         map_invisible(gb.bhitpos.x, gb.bhitpos.y);
1701. 
1702.     return 0;
1703. }
1704. 
1705. /* hit all objects at x,y with fhito function */
1706. staticfn boolean
1707. fhito_loc(struct obj *obj,
1708.           coordxy tx, coordxy ty,
1709.           int (*fhito)(OBJ_P, OBJ_P))
1710. {
1711.     struct obj *otmp, *next_obj;
1712.     int hitanything = 0;
1713. 
1714.     if (!fhito || !OBJ_AT(tx, ty))
1715.         return FALSE;
1716. 
1717.     for (otmp = svl.level.objects[tx][ty]; otmp; otmp = next_obj) {
1718.         next_obj = otmp->nexthere;
1719. 
1720.         if (otmp->where != OBJ_FLOOR || otmp->ox != tx || otmp->oy != ty)
1721.             continue;
1722.         hitanything += (*fhito)(otmp, obj);
1723.     }
1724. 
1725.     return hitanything ? TRUE : FALSE;
1726. }
1727. 
1728. /* A modified bhit() for monsters.  Based on bhit() in zap.c.  Unlike
1729.  * buzz(), bhit() doesn't take into account the possibility of a monster
1730.  * zapping you, so we need a special function for it.  (Unless someone wants
1731.  * to merge the two functions...)
1732.  */
1733. staticfn void
1734. mbhit(
1735.     struct monst *mon,            /* monster shooting the wand */
1736.     int range,                    /* direction and range */
1737.     int (*fhitm)(MONST_P, OBJ_P), /* must be non-Null */
1738.     int (*fhito)(OBJ_P, OBJ_P),   /* fns called when mon/obj hit */
1739.     struct obj *obj)              /* 2nd arg to fhitm/fhito */
1740. {
1741.     struct monst *mtmp;
1742.     uchar ltyp;
1743.     int ddx, ddy, otyp = obj->otyp;
1744. 
1745.     gb.bhitpos.x = mon->mx;
1746.     gb.bhitpos.y = mon->my;
1747.     ddx = sgn(mon->mux - mon->mx);
1748.     ddy = sgn(mon->muy - mon->my);
1749. 
1750.     while (range-- > 0) {
1751.         coordxy x, y, dbx, dby;
1752. 
1753.         gb.bhitpos.x += ddx;
1754.         gb.bhitpos.y += ddy;
1755.         x = gb.bhitpos.x;
1756.         y = gb.bhitpos.y;
1757. 
1758.         if (!isok(x, y)) {
1759.             gb.bhitpos.x -= ddx;
1760.             gb.bhitpos.y -= ddy;
1761.             break;
1762.         }
1763.         if (u_at(gb.bhitpos.x, gb.bhitpos.y)) {
1764.             (*fhitm)(&gy.youmonst, obj);
1765.             range -= 3;
1766.         } else if ((mtmp = m_at(gb.bhitpos.x, gb.bhitpos.y)) != 0) {
1767.             if (cansee(gb.bhitpos.x, gb.bhitpos.y) && !canspotmon(mtmp))
1768.                 map_invisible(gb.bhitpos.x, gb.bhitpos.y);
1769.             (*fhitm)(mtmp, obj);
1770.             range -= 3;
1771.         }
1772.         if (fhito_loc(obj, gb.bhitpos.x, gb.bhitpos.y, fhito))
1773.             range--;
1774.         ltyp = levl[gb.bhitpos.x][gb.bhitpos.y].typ;
1775.         dbx = x, dby = y;
1776.         if (otyp == WAN_STRIKING
1777.             /* if levl[x][y].typ is DRAWBRIDGE_UP then the zap is passing
1778.                over the moat in front of a closed drawbridge and doesn't
1779.                hit any part of the bridge's mechanism (yet; it might be
1780.                about to hit the closed portcullis on the next iteration) */
1781.             && ltyp != DRAWBRIDGE_UP && find_drawbridge(&dbx, &dby)) {
1782.             /* this might kill mon and destroy obj but they'll remain
1783.                accessible; (*fhitm)() and (*fhito)() use obj for zap type */
1784.             destroy_drawbridge(dbx, dby);
1785.         } else if (IS_DOOR(ltyp) || ltyp == SDOOR) {
1786.             switch (otyp) {
1787.             /* note: monsters don't use opening or locking magic
1788.                at present, but keep these as placeholders */
1789.             case WAN_OPENING:
1790.             case WAN_LOCKING:
1791.             case WAN_STRIKING:
1792.                 if (doorlock(obj, gb.bhitpos.x, gb.bhitpos.y)) {
1793.                     if (gz.zap_oseen)
1794.                         makeknown(otyp);
1795.                     /* if a shop door gets broken, add it to
1796.                        the shk's fix list (no cost to player) */
1797.                     if (levl[gb.bhitpos.x][gb.bhitpos.y].doormask == D_BROKEN
1798.                         && *in_rooms(gb.bhitpos.x, gb.bhitpos.y, SHOPBASE))
1799.                         add_damage(gb.bhitpos.x, gb.bhitpos.y, 0L);
1800.                 }
1801.                 break;
1802.             }
1803.         }
1804.         if (!ZAP_POS(ltyp)
1805.             || (IS_DOOR(ltyp) && (levl[gb.bhitpos.x][gb.bhitpos.y].doormask
1806.                                   & (D_LOCKED | D_CLOSED)))) {
1807.             gb.bhitpos.x -= ddx;
1808.             gb.bhitpos.y -= ddy;
1809.             break;
1810.         }
1811.     }
1812. }
1813. 
1814. staticfn void
1815. buzz_force_miss(int type, int nd, coordxy sx, coordxy sy, int dx, int dy)
1816. {
1817.     dobuzz(type, nd, sx, sy, dx, dy, TRUE, FALSE, TRUE);
1818. }
1819. 
1820. /* Perform an offensive action for a monster.  Must be called immediately
1821.  * after find_offensive().  Return values are same as use_defensive().
1822.  */
1823. int
1824. use_offensive(struct monst *mtmp)
1825. {
1826.     int i;
1827.     struct obj *otmp = gm.m.offensive;
1828.     boolean oseen;
1829. 
1830.     /* if a monster has never used an attack wand before, it takes them some
1831.        time to get used to holding that much power, so the first shot always
1832.        misses */
1833.     void (*buzzfn)(int, int, coordxy, coordxy, int, int) =
1834.         mtmp->mwandexp ? buzz : buzz_force_miss;
1835. 
1836.     /* offensive potions are not drunk, they're thrown */
1837.     if (otmp->oclass != POTION_CLASS && (i = precheck(mtmp, otmp)) != 0)
1838.         return i;
1839.     oseen = canseemon(mtmp);
1840. 
1841.     switch (gm.m.has_offense) {
1842.     case MUSE_WAN_DEATH:
1843.     case MUSE_WAN_SLEEP:
1844.     case MUSE_WAN_FIRE:
1845.     case MUSE_WAN_COLD:
1846.     case MUSE_WAN_LIGHTNING:
1847.     case MUSE_WAN_MAGIC_MISSILE:
1848.         mzapwand(mtmp, otmp, FALSE);
1849.         if (oseen)
1850.             makeknown(otmp->otyp);
1851.         gm.m_using = TRUE;
1852.         gc.current_wand = otmp;
1853.         gb.buzzer = mtmp;
1854.         buzzfn(BZ_M_WAND(BZ_OFS_WAN(otmp->otyp)),
1855.                (otmp->otyp == WAN_MAGIC_MISSILE) ? 2 : 6, mtmp->mx, mtmp->my,
1856.                sgn(mtmp->mux - mtmp->mx), sgn(mtmp->muy - mtmp->my));
1857.         gb.buzzer = 0;
1858.         gc.current_wand = 0;
1859.         gm.m_using = FALSE;
1860.         mtmp->mwandexp = TRUE;
1861.         return (DEADMONSTER(mtmp)) ? 1 : 2;
1862.     case MUSE_FIRE_HORN:
1863.     case MUSE_FROST_HORN:
1864.         mplayhorn(mtmp, otmp, FALSE);
1865.         gm.m_using = TRUE;
1866.         gb.buzzer = mtmp;
1867.         gc.current_wand = otmp; /* needed by zhitu() */
1868.         buzzfn(BZ_M_WAND(BZ_OFS_AD(
1869.                              (otmp->otyp == FROST_HORN) ? AD_COLD : AD_FIRE)),
1870.              rn1(6, 6), mtmp->mx, mtmp->my, sgn(mtmp->mux - mtmp->mx),
1871.              sgn(mtmp->muy - mtmp->my));
1872.         gb.buzzer = 0;
1873.         gc.current_wand = 0;
1874.         gm.m_using = FALSE;
1875.         mtmp->mwandexp = TRUE;
1876.         return (DEADMONSTER(mtmp)) ? 1 : 2;
1877.     case MUSE_WAN_TELEPORTATION:
1878.     case MUSE_WAN_UNDEAD_TURNING:
1879.     case MUSE_WAN_STRIKING:
1880.         gz.zap_oseen = oseen;
1881.         mzapwand(mtmp, otmp, FALSE);
1882.         gm.m_using = TRUE;
1883.         gb.buzzer = mtmp;
1884.         mbhit(mtmp, rn1(8, 6), mbhitm, bhito, otmp);
1885.         gb.buzzer = 0;
1886.         /* note: 'otmp' might have been destroyed (drawbridge destruction) */
1887.         gm.m_using = FALSE;
1888.         if (gm.m.has_offense == MUSE_WAN_STRIKING)
1889.             mtmp->mwandexp = TRUE;
1890.         return 2;
1891.     case MUSE_SCR_EARTH: {
1892.         /* TODO: handle steeds */
1893.         coordxy x, y;
1894.         /* don't use monster fields after killing it */
1895.         boolean confused = (mtmp->mconf ? TRUE : FALSE);
1896.         int mmx = mtmp->mx, mmy = mtmp->my;
1897.         boolean is_cursed = otmp->cursed, is_blessed = otmp->blessed;
1898. 
1899.         mreadmsg(mtmp, otmp);
1900.         /* Identify the scroll */
1901.         if (canspotmon(mtmp)) {
1902.             pline_The("%s rumbles %s %s!", ceiling(mtmp->mx, mtmp->my),
1903.                       otmp->blessed ? "around" : "above", mon_nam(mtmp));
1904.             if (oseen)
1905.                 makeknown(otmp->otyp);
1906.         } else if (cansee(mtmp->mx, mtmp->my)) {
1907.             pline_The("%s rumbles in the middle of nowhere!",
1908.                       ceiling(mtmp->mx, mtmp->my));
1909.             if (mtmp->minvis)
1910.                 map_invisible(mtmp->mx, mtmp->my);
1911.             if (oseen)
1912.                 makeknown(otmp->otyp);
1913.         }
1914. 
1915.         /* could be fatal to monster, so use up the scroll before
1916.            there's a chance that monster's inventory will be dropped */
1917.         m_useup(mtmp, otmp);
1918. 
1919.         /* Loop through the surrounding squares */
1920.         for (x = mmx - 1; x <= mmx + 1; x++) {
1921.             for (y = mmy - 1; y <= mmy + 1; y++) {
1922.                 /* Is this a suitable spot? */
1923.                 if (isok(x, y) && !closed_door(x, y)
1924.                     && !IS_OBSTRUCTED(levl[x][y].typ) && !IS_AIR(levl[x][y].typ)
1925.                     && (((x == mmx) && (y == mmy)) ? !is_blessed : !is_cursed)
1926.                     && (x != u.ux || y != u.uy)) {
1927.                     (void) drop_boulder_on_monster(x, y, confused, FALSE);
1928.                 }
1929.             }
1930.         }
1931.         /* Attack the player */
1932.         if (distmin(mmx, mmy, u.ux, u.uy) == 1 && !is_cursed) {
1933.             drop_boulder_on_player(confused, !is_cursed, FALSE, TRUE);
1934.         }
1935. 
1936.         return (DEADMONSTER(mtmp)) ? 1 : 2;
1937.     } /* case MUSE_SCR_EARTH */
1938.     case MUSE_CAMERA: {
1939.         if (Hallucination) {
1940.             SetVoice(mtmp, 0, 80, 0);
1941.             verbalize("Say cheese!");
1942.         } else if (!Blind) {
1943.             pline("%s takes a picture of you with %s!",
1944.                   Monnam(mtmp), an(xname(otmp)));
1945.         }
1946.         gm.m_using = TRUE;
1947.         if (!Blind && !resists_blnd(&gy.youmonst)) {
1948.             You("are blinded by the flash of light!");
1949.             make_blinded(BlindedTimeout + (long) rnd(1 + 50), FALSE);
1950.         }
1951.         lightdamage(otmp, TRUE, 5);
1952.         gm.m_using = FALSE;
1953.         otmp->spe--;
1954.         return 1;
1955.     } /* case MUSE_CAMERA */
1956. #if 0
1957.     case MUSE_SCR_FIRE: {
1958.         boolean vis = cansee(mtmp->mx, mtmp->my);
1959. 
1960.         mreadmsg(mtmp, otmp);
1961.         if (mtmp->mconf) {
1962.             if (vis)
1963.                 pline("Oh, what a pretty fire!");
1964.         } else {
1965.             struct monst *mtmp2;
1966.             int num;
1967. 
1968.             if (vis)
1969.                 pline_The("scroll erupts in a tower of flame!");
1970.             shieldeff(mtmp->mx, mtmp->my);
1971.             pline_mon(mtmp, "%s is uninjured.", Monnam(mtmp));
1972.             (void) destroy_mitem(mtmp, SCROLL_CLASS, AD_FIRE);
1973.             (void) destroy_mitem(mtmp, SPBOOK_CLASS, AD_FIRE);
1974.             (void) destroy_mitem(mtmp, POTION_CLASS, AD_FIRE);
1975.             ignite_items(mtmp->minvent);
1976.             num = (2 * (rn1(3, 3) + 2 * bcsign(otmp)) + 1) / 3;
1977.             if (Fire_resistance)
1978.                 You("are not harmed.");
1979.             burn_away_slime();
1980.             if (Half_spell_damage)
1981.                 num = (num + 1) / 2;
1982.             else
1983.                 losehp(num, "scroll of fire", KILLED_BY_AN);
1984.             for (mtmp2 = fmon; mtmp2; mtmp2 = mtmp2->nmon) {
1985.                 if (DEADMONSTER(mtmp2))
1986.                     continue;
1987.                 if (mtmp == mtmp2)
1988.                     continue;
1989.                 if (dist2(mtmp2->mx, mtmp2->my, mtmp->mx, mtmp->my) < 3) {
1990.                     if (resists_fire(mtmp2))
1991.                         continue;
1992.                     mtmp2->mhp -= num;
1993.                     if (resists_cold(mtmp2))
1994.                         mtmp2->mhp -= 3 * num;
1995.                     if (DEADMONSTER(mtmp2)) {
1996.                         mondied(mtmp2);
1997.                         break;
1998.                     }
1999.                 }
2000.             }
2001.         }
2002.         return 2;
2003.     }
2004. #endif /* 0 */
2005.     case MUSE_POT_PARALYSIS:
2006.     case MUSE_POT_BLINDNESS:
2007.     case MUSE_POT_CONFUSION:
2008.     case MUSE_POT_SLEEPING:
2009.     case MUSE_POT_ACID:
2010.         /* Note: this setting of dknown doesn't suffice.  A monster
2011.          * which is out of sight might throw and it hits something _in_
2012.          * sight, a problem not existing with wands because wand rays
2013.          * are not objects.  Also set dknown in mthrowu.c.
2014.          */
2015.         if (cansee(mtmp->mx, mtmp->my)) {
2016.             observe_object(otmp);
2017.             pline_mon(mtmp, "%s hurls %s!",
2018.                       Monnam(mtmp), singular(otmp, doname));
2019.         }
2020.         m_throw(mtmp, mtmp->mx, mtmp->my, sgn(mtmp->mux - mtmp->mx),
2021.                 sgn(mtmp->muy - mtmp->my),
2022.                 distmin(mtmp->mx, mtmp->my, mtmp->mux, mtmp->muy), otmp);
2023.         return 2;
2024.     case 0:
2025.         return 0; /* i.e. an exploded wand */
2026.     default:
2027.         impossible("%s wanted to perform action %d?", Monnam(mtmp),
2028.                    gm.m.has_offense);
2029.         break;
2030.     }
2031.     return 0;
2032. }
2033. 
2034. int
2035. rnd_offensive_item(struct monst *mtmp)
2036. {
2037.     struct permonst *pm = mtmp->data;
2038.     int difficulty = mons[(monsndx(pm))].difficulty;
2039. 
2040.     if (is_animal(pm) || attacktype(pm, AT_EXPL) || mindless(mtmp->data)
2041.         || pm->mlet == S_GHOST || pm->mlet == S_KOP)
2042.         return 0;
2043.     if (difficulty > 7 && !rn2(35))
2044.         return WAN_DEATH;
2045.     switch (rn2(9 - (difficulty < 4) + 4 * (difficulty > 6))) {
2046.     case 0: {
2047.         struct obj *mtmp_helmet = which_armor(mtmp, W_ARMH);
2048. 
2049.         if (hard_helmet(mtmp_helmet) || amorphous(pm)
2050.             || passes_walls(pm) || noncorporeal(pm) || unsolid(pm))
2051.             return SCR_EARTH;
2052.     }
2053.     FALLTHROUGH;
2054.     /* FALLTHRU */
2055.     case 1:
2056.         return WAN_STRIKING;
2057.     case 2:
2058.         return POT_ACID;
2059.     case 3:
2060.         return POT_CONFUSION;
2061.     case 4:
2062.         return POT_BLINDNESS;
2063.     case 5:
2064.         return POT_SLEEPING;
2065.     case 6:
2066.         return POT_PARALYSIS;
2067.     case 7:
2068.     case 8:
2069.         return WAN_MAGIC_MISSILE;
2070.     case 9:
2071.         return WAN_SLEEP;
2072.     case 10:
2073.         return WAN_FIRE;
2074.     case 11:
2075.         return WAN_COLD;
2076.     case 12:
2077.         return WAN_LIGHTNING;
2078.     }
2079.     /*NOTREACHED*/
2080.     return 0;
2081. }
2082. 
2083. #define MUSE_POT_GAIN_LEVEL 1
2084. #define MUSE_WAN_MAKE_INVISIBLE 2
2085. #define MUSE_POT_INVISIBILITY 3
2086. #define MUSE_POLY_TRAP 4
2087. #define MUSE_WAN_POLYMORPH 5
2088. #define MUSE_POT_SPEED 6
2089. #define MUSE_WAN_SPEED_MONSTER 7
2090. #define MUSE_BULLWHIP 8
2091. #define MUSE_POT_POLYMORPH 9
2092. #define MUSE_BAG 10
2093. 
2094. boolean
2095. find_misc(struct monst *mtmp)
2096. {
2097.     struct obj *obj;
2098.     struct permonst *mdat = mtmp->data;
2099.     coordxy x = mtmp->mx, y = mtmp->my;
2100.     struct trap *t;
2101.     coordxy xx, yy;
2102.     int pmidx = NON_PM;
2103.     boolean immobile = (mdat->mmove == 0);
2104.     boolean stuck = (mtmp == u.ustuck);
2105. 
2106.     gm.m.misc = (struct obj *) 0;
2107.     gm.m.has_misc = 0;
2108.     if (is_animal(mdat) || mindless(mdat))
2109.         return 0;
2110.     if (u.uswallow && stuck)
2111.         return FALSE;
2112. 
2113.     /* We arbitrarily limit to times when a player is nearby for the
2114.      * same reason as Junior Pac-Man doesn't have energizers eaten until
2115.      * you can see them...
2116.      */
2117.     if (dist2(x, y, mtmp->mux, mtmp->muy) > 36)
2118.         return FALSE;
2119. 
2120.     if (!stuck && !immobile && !mtmp->mtrapped && (mtmp->cham == NON_PM)
2121.         && mons[(pmidx = monsndx(mdat))].difficulty < 6) {
2122.         boolean ignore_boulders = (verysmall(mdat) || throws_rocks(mdat)
2123.                                    || passes_walls(mdat)),
2124.             diag_ok = !NODIAG(pmidx);
2125. 
2126.         for (xx = x - 1; xx <= x + 1; xx++)
2127.             for (yy = y - 1; yy <= y + 1; yy++)
2128.                 if (isok(xx, yy) && !u_at(xx, yy)
2129.                     && (diag_ok || xx == x || yy == y)
2130.                     && ((xx == x && yy == y) || !svl.level.monsters[xx][yy]))
2131.                     if ((t = t_at(xx, yy)) != 0
2132.                         && (ignore_boulders || !sobj_at(BOULDER, xx, yy))
2133.                         && !onscary(xx, yy, mtmp)) {
2134.                         /* use trap if it's the correct type and will
2135.                            polymorph the monster */
2136.                         if (t->ttyp == POLY_TRAP &&
2137.                             !wearing_iron_shoes(mtmp)) {
2138.                             gt.trapx = xx;
2139.                             gt.trapy = yy;
2140.                             gm.m.has_misc = MUSE_POLY_TRAP;
2141.                             return TRUE;
2142.                         }
2143.                     }
2144.     }
2145.     if (nohands(mdat))
2146.         return 0;
2147. 
2148.     /* normally we would want to bracket a macro expansion containing
2149.        'if' without matching 'else' with 'do { ... } while (0)' but we
2150.        can't do that here because it would intercept 'continue' */
2151. #define nomore(x)       if (gm.m.has_misc == (x)) continue
2152.     /*
2153.      * [bug?]  Choice of item is not prioritized; the last viable one
2154.      * in the monster's inventory will be chosen.
2155.      * 'nomore()' is nearly worthless because it only screens checking
2156.      * of duplicates when there is no alternate type in between them.
2157.      *
2158.      * MUSE_BAG issues:
2159.      * should allow looting floor container instead of needing the
2160.      * monster to have picked it up and now be carrying it which takes
2161.      * extra time and renders heavily filled containers immune;
2162.      * hero should have a chance to see the monster fail to open a
2163.      * locked container instead of monster always knowing lock state
2164.      * (may not be feasible to implement--requires too much per-object
2165.      * info for each monster);
2166.      * monster with key should be able to unlock a locked floor
2167.      * container and not know whether it is trapped.
2168.      */
2169.     for (obj = mtmp->minvent; obj; obj = obj->nobj) {
2170.         /* Monsters shouldn't recognize cursed items; this kludge is
2171.            necessary to prevent serious problems though... */
2172.         if (obj->otyp == POT_GAIN_LEVEL
2173.             && (!obj->cursed
2174.                 || (!mtmp->isgd && !mtmp->isshk && !mtmp->ispriest))) {
2175.             gm.m.misc = obj;
2176.             gm.m.has_misc = MUSE_POT_GAIN_LEVEL;
2177.         }
2178.         nomore(MUSE_BULLWHIP);
2179.         if (obj->otyp == BULLWHIP && !mtmp->mpeaceful
2180.             /* the random test prevents whip-wielding
2181.                monster from attempting disarm every turn */
2182.             && uwep && !rn2(5) && obj == MON_WEP(mtmp)
2183.             /* hero's location must be known and adjacent */
2184.             && u_at(mtmp->mux, mtmp->muy)
2185.             && m_next2u(mtmp)
2186.             /* don't bother if it can't work (this doesn't
2187.                prevent cursed weapons from being targeted) */
2188.             && !u.uswallow
2189.             && (canletgo(uwep, "")
2190.                 || (u.twoweap && canletgo(uswapwep, "")))) {
2191.             gm.m.misc = obj;
2192.             gm.m.has_misc = MUSE_BULLWHIP;
2193.         }
2194.         /* Note: peaceful/tame monsters won't make themselves
2195.          * invisible unless you can see them.  Not really right, but...
2196.          */
2197.         nomore(MUSE_WAN_MAKE_INVISIBLE);
2198.         if (obj->otyp == WAN_MAKE_INVISIBLE && obj->spe > 0 && !mtmp->minvis
2199.             && !mtmp->invis_blkd && (!mtmp->mpeaceful || See_invisible)
2200.             && (!attacktype(mtmp->data, AT_GAZE) || mtmp->mcan)) {
2201.             gm.m.misc = obj;
2202.             gm.m.has_misc = MUSE_WAN_MAKE_INVISIBLE;
2203.         }
2204.         nomore(MUSE_POT_INVISIBILITY);
2205.         if (obj->otyp == POT_INVISIBILITY && !mtmp->minvis
2206.             && !mtmp->invis_blkd && (!mtmp->mpeaceful || See_invisible)
2207.             && (!attacktype(mtmp->data, AT_GAZE) || mtmp->mcan)) {
2208.             gm.m.misc = obj;
2209.             gm.m.has_misc = MUSE_POT_INVISIBILITY;
2210.         }
2211.         nomore(MUSE_WAN_SPEED_MONSTER);
2212.         if (obj->otyp == WAN_SPEED_MONSTER && obj->spe > 0
2213.             && mtmp->mspeed != MFAST && !mtmp->isgd) {
2214.             gm.m.misc = obj;
2215.             gm.m.has_misc = MUSE_WAN_SPEED_MONSTER;
2216.         }
2217.         nomore(MUSE_POT_SPEED);
2218.         if (obj->otyp == POT_SPEED && mtmp->mspeed != MFAST && !mtmp->isgd) {
2219.             gm.m.misc = obj;
2220.             gm.m.has_misc = MUSE_POT_SPEED;
2221.         }
2222.         nomore(MUSE_WAN_POLYMORPH);
2223.         if (obj->otyp == WAN_POLYMORPH && obj->spe > 0
2224.             && (mtmp->cham == NON_PM) && mons[monsndx(mdat)].difficulty < 6) {
2225.             gm.m.misc = obj;
2226.             gm.m.has_misc = MUSE_WAN_POLYMORPH;
2227.         }
2228.         nomore(MUSE_POT_POLYMORPH);
2229.         if (obj->otyp == POT_POLYMORPH && (mtmp->cham == NON_PM)
2230.             && mons[monsndx(mdat)].difficulty < 6) {
2231.             gm.m.misc = obj;
2232.             gm.m.has_misc = MUSE_POT_POLYMORPH;
2233.         }
2234.         nomore(MUSE_BAG);
2235.         if (Is_container(obj) && obj->otyp != BAG_OF_TRICKS && !rn2(5)
2236.             && !SchroedingersBox(obj)
2237.             && !gm.m.has_misc && Has_contents(obj)
2238.             && !obj->olocked && !obj->otrapped) {
2239.             gm.m.misc = obj;
2240.             gm.m.has_misc = MUSE_BAG;
2241.         }
2242.     }
2243.     return (boolean) !!gm.m.has_misc;
2244. #undef nomore
2245. }
2246. 
2247. /* type of monster to polymorph into; defaults to one suitable for the
2248.    current level rather than the totally arbitrary choice of newcham() */
2249. staticfn struct permonst *
2250. muse_newcham_mon(struct monst *mon)
2251. {
2252.     struct obj *m_armr;
2253. 
2254.     if ((m_armr = which_armor(mon, W_ARM)) != 0) {
2255.         if (Is_dragon_scales(m_armr))
2256.             return Dragon_scales_to_pm(m_armr);
2257.         else if (Is_dragon_mail(m_armr))
2258.             return Dragon_mail_to_pm(m_armr);
2259.     }
2260.     return rndmonst();
2261. }
2262. 
2263. staticfn int
2264. mloot_container(
2265.     struct monst *mon,
2266.     struct obj *container,
2267.     boolean vismon)
2268. {
2269.     char contnr_nam[BUFSZ], mpronounbuf[20];
2270.     boolean nearby;
2271.     int takeout_indx, takeout_count, howfar, res = 0;
2272. 
2273.     if (!container || !Has_contents(container) || container->olocked)
2274.         return res; /* 0 */
2275.     /* FIXME: handle cursed bag of holding */
2276.     if (Is_mbag(container) && container->cursed)
2277.         return res; /* 0 */
2278.     if (SchroedingersBox(container))
2279.         return res;
2280. 
2281.     switch (rn2(10)) {
2282.     default: /* case 0, 1, 2, 3: */
2283.         takeout_count = 1;
2284.         break;
2285.     case 4: case 5: case 6:
2286.         takeout_count = 2;
2287.         break;
2288.     case 7: case 8:
2289.         takeout_count = 3;
2290.         break;
2291.     case 9:
2292.         takeout_count = 4;
2293.         break;
2294.     }
2295.     howfar = mdistu(mon);
2296.     nearby = (howfar <= 7 * 7);
2297.     contnr_nam[0] = mpronounbuf[0] = '\0';
2298.     if (vismon) {
2299.         /* do this once so that when hallucinating it won't change
2300.            from one item to the next */
2301.         Strcpy(mpronounbuf, mhe(mon));
2302.     }
2303. 
2304.     for (takeout_indx = 0; takeout_indx < takeout_count; ++takeout_indx) {
2305.         struct obj *xobj;
2306.         int nitems;
2307. 
2308.         if (!Has_contents(container)) /* might have removed all items */
2309.             break;
2310.         /* TODO?
2311.          *  Monster ought to prioritize on something it wants to use.
2312.          */
2313.         nitems = 0;
2314.         for (xobj = container->cobj; xobj != 0; xobj = xobj->nobj)
2315.             ++nitems;
2316.         /* nitems is always greater than 0 due to Has_contents() check;
2317.            throttle item removal as the container becomes less filled */
2318.         if (!rn2(nitems + 1))
2319.             break;
2320.         nitems = rn2(nitems);
2321.         for (xobj = container->cobj; xobj != 0; xobj = xobj->nobj)
2322.             if (--nitems < 0)
2323.                 break;
2324.         assert(xobj != NULL);
2325. 
2326.         container->cknown = 0; /* hero no longer knows container's contents
2327.                                 * even if [attempted] removal is observed */
2328.         if (!*contnr_nam) {
2329.             /* xname sets dknown, distant_name might depending on its own
2330.                idea about nearness */
2331.             Strcpy(contnr_nam, an(nearby ? xname(container)
2332.                                          : distant_name(container, xname)));
2333.         }
2334.         /* this was originally just 'can_carry(mon, xobj)' which
2335.            covers objects a monster shouldn't pick up but also
2336.            checks carrying capacity; for that, it ended up counting
2337.            xobj's weight twice when container is carried; so take
2338.            xobj out, check whether it can be carried, and then put
2339.            it back (below) if it can't be */
2340.         obj_extract_self(xobj); /* this reduces container's weight */
2341.         /* check whether mon can handle xobj and whether weight of xobj plus
2342.            minvent (including container, now without xobj) can be carried */
2343.         if (can_carry(mon, xobj)) {
2344.             if (vismon) {
2345.                 if (howfar > 2) /* not adjacent */
2346.                     Norep("%s rummages through %s.", Monnam(mon), contnr_nam);
2347.                 else if (takeout_indx == 0) /* adjacent, first item */
2348.                     pline_mon(mon, "%s removes %s from %s.", Monnam(mon),
2349.                           doname(xobj), contnr_nam);
2350.                 else /* adjacent, additional items */
2351.                     pline("%s removes %s.", upstart(mpronounbuf),
2352.                           doname(xobj));
2353.             }
2354.             if (container->otyp == ICE_BOX)
2355.                 removed_from_icebox(xobj); /* resume rotting for corpse */
2356.             /* obj_extract_self(xobj); -- already done above */
2357.             (void) mpickobj(mon, xobj);
2358.             res = 2;
2359.         } else { /* couldn't carry xobj separately so put back inside */
2360.             /* an achievement prize (castle's wand?) might already be
2361.                marked nomerge (when it hasn't been in invent yet) */
2362.             boolean already_nomerge = xobj->nomerge != 0,
2363.                     just_xobj = !Has_contents(container);
2364. 
2365.             /* this doesn't restore the original contents ordering
2366.                [shouldn't be a problem; even though this item didn't
2367.                give the rummage message, that's what mon was doing] */
2368.             xobj->nomerge = 1;
2369.             xobj = add_to_container(container, xobj);
2370.             if (!already_nomerge)
2371.                 xobj->nomerge = 0;
2372.             container->owt = weight(container);
2373.             if (just_xobj)
2374.                 break; /* out of takeout_count loop */
2375.         } /* can_carry */
2376.     } /* takeout_count */
2377.     return res;
2378. }
2379. 
2380. DISABLE_WARNING_UNREACHABLE_CODE
2381. 
2382. int
2383. use_misc(struct monst *mtmp)
2384. {
2385.     static const char MissingMiscellaneousItem[] = "use_misc: no %s";
2386.     char nambuf[BUFSZ];
2387.     boolean vis, vismon, vistrapspot, oseen;
2388.     int i;
2389.     struct trap *t;
2390.     struct obj *otmp = gm.m.misc;
2391. 
2392.     if ((i = precheck(mtmp, otmp)) != 0)
2393.         return i;
2394.     vis = cansee(mtmp->mx, mtmp->my);
2395.     vismon = canseemon(mtmp);
2396.     oseen = otmp && vismon;
2397. 
2398.     switch (gm.m.has_misc) {
2399.     case MUSE_POT_GAIN_LEVEL:
2400.         if (!otmp)
2401.             panic(MissingMiscellaneousItem, "potion of gain level");
2402.         mquaffmsg(mtmp, otmp);
2403.         if (otmp->cursed) {
2404.             if (Can_rise_up(mtmp->mx, mtmp->my, &u.uz)) {
2405.                 int tolev = depth(&u.uz) - 1;
2406.                 d_level tolevel;
2407. 
2408.                 get_level(&tolevel, tolev);
2409.                 /* insurance against future changes... */
2410.                 if (on_level(&tolevel, &u.uz))
2411.                     goto skipmsg;
2412.                 if (vismon) {
2413.                     pline_mon(mtmp, "%s rises up, through the %s!",
2414.                               Monnam(mtmp),
2415.                               ceiling(mtmp->mx, mtmp->my));
2416.                     trycall(otmp);
2417.                 }
2418.                 m_useup(mtmp, otmp);
2419.                 migrate_to_level(mtmp, ledger_no(&tolevel), MIGR_RANDOM,
2420.                                  (coord *) 0);
2421.                 return 2;
2422.             } else {
2423.  skipmsg:
2424.                 if (vismon) {
2425.                     pline_mon(mtmp, "%s looks uneasy.", Monnam(mtmp));
2426.                     trycall(otmp);
2427.                 }
2428.                 m_useup(mtmp, otmp);
2429.                 return 2;
2430.             }
2431.         }
2432.         if (vismon)
2433.             pline_mon(mtmp, "%s seems more experienced.", Monnam(mtmp));
2434.         if (oseen)
2435.             makeknown(POT_GAIN_LEVEL);
2436.         m_useup(mtmp, otmp);
2437.         if (!grow_up(mtmp, (struct monst *) 0))
2438.             return 1;
2439.         /* grew into genocided monster */
2440.         return 2;
2441.     case MUSE_WAN_MAKE_INVISIBLE:
2442.     case MUSE_POT_INVISIBILITY:
2443.         if (!otmp)
2444.             panic(MissingMiscellaneousItem, "potion of invisibility");
2445.         if (otmp->otyp == WAN_MAKE_INVISIBLE) {
2446.             mzapwand(mtmp, otmp, TRUE);
2447.         } else
2448.             mquaffmsg(mtmp, otmp);
2449.         /* format monster's name before altering its visibility */
2450.         Strcpy(nambuf, mon_nam(mtmp));
2451.         mon_set_minvis(mtmp, !otmp->cursed ? FALSE : TRUE);
2452.         if (vismon && mtmp->minvis) { /* was seen, now invisible */
2453.             if (canspotmon(mtmp)) {
2454.                 pline("%s body takes on a %s transparency.",
2455.                       upstart(s_suffix(nambuf)),
2456.                       Hallucination ? "normal" : "strange");
2457.             } else {
2458.                 pline("Suddenly you cannot see %s.", nambuf);
2459.                 if (vis)
2460.                     map_invisible(mtmp->mx, mtmp->my);
2461.             }
2462.             if (oseen)
2463.                 makeknown(otmp->otyp);
2464.         } else if (vismon && !mtmp->minvis) {
2465.             /* cursed potion; mon tried to make itself invisible but failed */
2466.             pline("%s briefly seems to be transparent.", Monnam(mtmp));
2467.             /* we could call map_invisible() before the pline(), then
2468.                newsym() after; unseen monster glyph would be visible during
2469.                the pline, but hero would forget any remembered object under
2470.                the monster */
2471.         } else if (!vismon && canseemon(mtmp)) {
2472.             /* cursed potion; this won't happen because a monster will only
2473.                drink a potion of invisibility when not already invisible */
2474.             pline("%s suddenly appears!", Monnam(mtmp));
2475.         }
2476.         if (otmp->otyp == POT_INVISIBILITY) {
2477.             if (otmp->cursed)
2478.                 you_aggravate(mtmp);
2479.             m_useup(mtmp, otmp);
2480.         }
2481.         return 2;
2482.     case MUSE_WAN_SPEED_MONSTER:
2483.         if (!otmp)
2484.             panic(MissingMiscellaneousItem, "wand of speed monster");
2485.         mzapwand(mtmp, otmp, TRUE);
2486.         mon_adjust_speed(mtmp, 1, otmp);
2487.         return 2;
2488.     case MUSE_POT_SPEED:
2489.         if (!otmp)
2490.             panic(MissingMiscellaneousItem, "potion of speed");
2491.         mquaffmsg(mtmp, otmp);
2492.         /* note difference in potion effect due to substantially
2493.            different methods of maintaining speed ratings:
2494.            player's character becomes "very fast" temporarily;
2495.            monster becomes "one stage faster" permanently */
2496.         mon_adjust_speed(mtmp, 1, otmp);
2497.         m_useup(mtmp, otmp);
2498.         return 2;
2499.     case MUSE_WAN_POLYMORPH:
2500.         if (!otmp)
2501.             panic(MissingMiscellaneousItem, "wand of polymorph");
2502.         mzapwand(mtmp, otmp, TRUE);
2503.         (void) newcham(mtmp, muse_newcham_mon(mtmp),
2504.                        NC_VIA_WAND_OR_SPELL | NC_SHOW_MSG);
2505.         if (oseen)
2506.             makeknown(WAN_POLYMORPH);
2507.         return 2;
2508.     case MUSE_POT_POLYMORPH:
2509.         if (!otmp)
2510.             panic(MissingMiscellaneousItem, "potion of polymorph");
2511.         mquaffmsg(mtmp, otmp);
2512.         m_useup(mtmp, otmp);
2513.         if (vismon)
2514.             pline_mon(mtmp, "%s suddenly mutates!", Monnam(mtmp));
2515.         (void) newcham(mtmp, muse_newcham_mon(mtmp), NC_SHOW_MSG);
2516.         if (oseen)
2517.             makeknown(POT_POLYMORPH);
2518.         return 2;
2519.     case MUSE_POLY_TRAP:
2520.         t = t_at(gt.trapx, gt.trapy);
2521.         vistrapspot = cansee(t->tx, t->ty);
2522.         if (vis || vistrapspot)
2523.             seetrap(t);
2524.         if (vismon || vistrapspot) {
2525.             pline_mon(mtmp, "%s deliberately %s onto a %s!", Some_Monnam(mtmp),
2526.                   vtense(fakename[0], locomotion(mtmp->data, "jump")),
2527.                   t->tseen ? trapname(t->ttyp, FALSE) : "hidden trap");
2528.             /* note: if mtmp is unseen because it is invisible, its new
2529.                shape will also be invisible and could produce "Its armor
2530.                falls off" messages during the transformation; those make
2531.                more sense after we've given "Someone jumps onto a trap." */
2532.         }
2533. 
2534.         /*  don't use rloc() due to worms */
2535.         remove_monster(mtmp->mx, mtmp->my);
2536.         newsym(mtmp->mx, mtmp->my);
2537.         place_monster(mtmp, gt.trapx, gt.trapy);
2538.         maybe_unhide_at(gt.trapx, gt.trapy);
2539.         if (mtmp->wormno)
2540.             worm_move(mtmp);
2541.         newsym(gt.trapx, gt.trapy);
2542. 
2543.         (void) newcham(mtmp, (struct permonst *) 0, NC_SHOW_MSG);
2544.         return 2;
2545.     case MUSE_BAG:
2546.         if (!otmp)
2547.             panic(MissingMiscellaneousItem, "container");
2548.         return mloot_container(mtmp, otmp, vismon);
2549.     case MUSE_BULLWHIP:
2550.         /* attempt to disarm hero */
2551.         {
2552.             const char *The_whip = vismon ? "The bullwhip" : "A whip";
2553.             int where_to = rn2(4);
2554.             struct obj *obj = uwep;
2555.             const char *hand;
2556.             char the_weapon[BUFSZ], hand_buf[BUFSZ];
2557. 
2558.             if (!obj || !canletgo(obj, "")
2559.                 || (u.twoweap && canletgo(uswapwep, "") && rn2(2)))
2560.                 obj = uswapwep;
2561.             if (!obj)
2562.                 break; /* shouldn't happen after find_misc() */
2563. 
2564.             Strcpy(the_weapon, the(xname(obj)));
2565.             hand = body_part(HAND);
2566.             if (bimanual(obj))
2567.                 hand = makeplural(hand);
2568.             (void) strncpy(hand_buf, hand, sizeof hand_buf - 1);
2569.             hand_buf[sizeof hand_buf - 1] = '\0';
2570. 
2571.             if (vismon)
2572.                 pline_mon(mtmp, "%s flicks a bullwhip towards your %s!",
2573.                           Monnam(mtmp), hand_buf);
2574.             if (obj->otyp == HEAVY_IRON_BALL) {
2575.                 pline("%s fails to wrap around %s.", The_whip, the_weapon);
2576.                 return 1;
2577.             }
2578.             urgent_pline("%s wraps around %s you're wielding!", The_whip,
2579.                          the_weapon);
2580.             if (welded(obj)) {
2581.                 pline("%s welded to your %s%c",
2582.                       !is_plural(obj) ? "It is" : "They are", hand_buf,
2583.                       !obj->bknown ? '!' : '.');
2584.                 /* obj->bknown = 1; */ /* welded() takes care of this */
2585.                 where_to = 0;
2586.             }
2587.             if (!where_to) {
2588.                 pline_The("whip slips free."); /* not `The_whip' */
2589.                 return 1;
2590.             } else if (where_to == 3 && mon_hates_silver(mtmp)
2591.                        && objects[obj->otyp].oc_material == SILVER) {
2592.                 /* this monster won't want to catch a silver
2593.                    weapon; drop it at hero's feet instead */
2594.                 where_to = 2;
2595.             }
2596.             remove_worn_item(obj, FALSE);
2597.             freeinv(obj);
2598.             switch (where_to) {
2599.             case 1: /* onto floor beneath mon */
2600.                 pline_mon(mtmp, "%s yanks %s from your %s!", Monnam(mtmp),
2601.                           the_weapon, hand_buf);
2602.                 place_object(obj, mtmp->mx, mtmp->my);
2603.                 break;
2604.             case 2: /* onto floor beneath you */
2605.                 pline_mon(mtmp, "%s yanks %s to the %s!", Monnam(mtmp),
2606.                           the_weapon, surface(u.ux, u.uy));
2607.                 dropy(obj);
2608.                 break;
2609.             case 3: /* into mon's inventory */
2610.                 pline_mon(mtmp, "%s snatches %s!", Monnam(mtmp), the_weapon);
2611.                 (void) mpickobj(mtmp, obj);
2612.                 break;
2613.             }
2614.             return 1;
2615.         }
2616.         /*NOTREACHED*/
2617.         return 0;
2618.     case 0:
2619.         return 0; /* i.e. an exploded wand */
2620.     default:
2621.         impossible("%s wanted to perform action %d?", Monnam(mtmp),
2622.                    gm.m.has_misc);
2623.         break;
2624.     }
2625.     return 0;
2626. }
2627. 
2628. RESTORE_WARNINGS
2629. 
2630. staticfn void
2631. you_aggravate(struct monst *mtmp)
2632. {
2633.     pline("For some reason, %s presence is known to you.",
2634.           s_suffix(noit_mon_nam(mtmp)));
2635.     cls();
2636. #ifdef CLIPPING
2637.     cliparound(mtmp->mx, mtmp->my);
2638. #endif
2639.     show_glyph(mtmp->mx, mtmp->my, mon_to_glyph(mtmp, rn2_on_display_rng));
2640.     display_self();
2641.     You_feel("aggravated at %s.", noit_mon_nam(mtmp));
2642.     display_nhwindow(WIN_MAP, TRUE);
2643.     docrt();
2644.     if (unconscious()) {
2645.         gm.multi = -1;
2646.         gn.nomovemsg = "Aggravated, you are jolted into full consciousness.";
2647.     }
2648.     newsym(mtmp->mx, mtmp->my);
2649.     if (!canspotmon(mtmp))
2650.         map_invisible(mtmp->mx, mtmp->my);
2651. }
2652. 
2653. int
2654. rnd_misc_item(struct monst *mtmp)
2655. {
2656.     struct permonst *pm = mtmp->data;
2657.     int difficulty = mons[(monsndx(pm))].difficulty;
2658. 
2659.     if (is_animal(pm) || attacktype(pm, AT_EXPL) || mindless(mtmp->data)
2660.         || pm->mlet == S_GHOST || pm->mlet == S_KOP)
2661.         return 0;
2662.     /* Unlike other rnd_item functions, we only allow _weak_ monsters
2663.      * to have this item; after all, the item will be used to strengthen
2664.      * the monster and strong monsters won't use it at all...
2665.      */
2666.     if (difficulty < 6 && !rn2(30))
2667.         return rn2(6) ? POT_POLYMORPH : WAN_POLYMORPH;
2668. 
2669.     if (!rn2(40) && !nonliving(pm) && !is_vampshifter(mtmp))
2670.         return AMULET_OF_LIFE_SAVING;
2671. 
2672.     switch (rn2(3)) {
2673.     case 0:
2674.         if (mtmp->isgd)
2675.             return 0;
2676.         return rn2(6) ? POT_SPEED : WAN_SPEED_MONSTER;
2677.     case 1:
2678.         if (mtmp->mpeaceful && !See_invisible)
2679.             return 0;
2680.         return rn2(6) ? POT_INVISIBILITY : WAN_MAKE_INVISIBLE;
2681.     case 2:
2682.         return POT_GAIN_LEVEL;
2683.     }
2684.     /*NOTREACHED*/
2685.     return 0;
2686. }
2687. 
2688. #if 0
2689. /* check whether hero is carrying a corpse or contained petrifier corpse */
2690. staticfn boolean
2691. necrophiliac(struct obj *objlist, boolean any_corpse)
2692. {
2693.     while (objlist) {
2694.         if (objlist->otyp == CORPSE
2695.             && (any_corpse || touch_petrifies(&mons[objlist->corpsenm])))
2696.             return TRUE;
2697.         if (Has_contents(objlist) && necrophiliac(objlist->cobj, FALSE))
2698.             return TRUE;
2699.         objlist = objlist->nobj;
2700.     }
2701.     return FALSE;
2702. }
2703. #endif
2704. 
2705. boolean
2706. searches_for_item(struct monst *mon, struct obj *obj)
2707. {
2708.     int typ = obj->otyp;
2709. 
2710.     /* don't let monsters interact with protected items on the floor */
2711.     if (obj->where == OBJ_FLOOR
2712.         && (obj->ox == mon->mx && obj->oy == mon->my)
2713.         && onscary(obj->ox, obj->oy, mon)) {
2714.         return FALSE;
2715.     }
2716. 
2717.     if (is_animal(mon->data) || mindless(mon->data)
2718.         || mon->data == &mons[PM_GHOST]) /* don't loot bones piles */
2719.         return FALSE;
2720. 
2721.     if (typ == WAN_MAKE_INVISIBLE || typ == POT_INVISIBILITY)
2722.         return (boolean) (!mon->minvis && !mon->invis_blkd
2723.                           && !attacktype(mon->data, AT_GAZE));
2724.     if (typ == WAN_SPEED_MONSTER || typ == POT_SPEED)
2725.         return (boolean) (mon->mspeed != MFAST);
2726. 
2727.     switch (obj->oclass) {
2728.     case WAND_CLASS:
2729.         if (obj->spe <= 0)
2730.             return FALSE;
2731.         if (typ == WAN_DIGGING)
2732.             return (boolean) !is_floater(mon->data);
2733.         if (typ == WAN_POLYMORPH)
2734.             return (boolean) (mons[monsndx(mon->data)].difficulty < 6);
2735.         if (objects[typ].oc_dir == RAY || typ == WAN_STRIKING
2736.             || typ == WAN_UNDEAD_TURNING
2737.             || typ == WAN_TELEPORTATION || typ == WAN_CREATE_MONSTER)
2738.             return TRUE;
2739.         break;
2740.     case POTION_CLASS:
2741.         if (typ == POT_HEALING || typ == POT_EXTRA_HEALING
2742.             || typ == POT_FULL_HEALING || typ == POT_POLYMORPH
2743.             || typ == POT_GAIN_LEVEL || typ == POT_PARALYSIS
2744.             || typ == POT_SLEEPING || typ == POT_ACID || typ == POT_CONFUSION)
2745.             return TRUE;
2746.         if (typ == POT_BLINDNESS && !attacktype(mon->data, AT_GAZE))
2747.             return TRUE;
2748.         break;
2749.     case SCROLL_CLASS:
2750.         if (typ == SCR_TELEPORTATION || typ == SCR_CREATE_MONSTER
2751.             || typ == SCR_EARTH || typ == SCR_FIRE)
2752.             return TRUE;
2753.         break;
2754.     case AMULET_CLASS:
2755.         if (typ == AMULET_OF_LIFE_SAVING)
2756.             return (boolean) !(nonliving(mon->data) || is_vampshifter(mon));
2757.         if (typ == AMULET_OF_REFLECTION || typ == AMULET_OF_GUARDING)
2758.             return TRUE;
2759.         break;
2760.     case TOOL_CLASS:
2761.         if (typ == PICK_AXE)
2762.             return (boolean) needspick(mon->data);
2763.         if (typ == UNICORN_HORN)
2764.             return (boolean) (!obj->cursed && !is_unicorn(mon->data)
2765.                               && mon->data != &mons[PM_KI_RIN]);
2766.         if (typ == FROST_HORN || typ == FIRE_HORN)
2767.             return (obj->spe > 0 && can_blow(mon));
2768.         if (Is_container(obj) && !(Is_mbag(obj) && obj->cursed)
2769.             && !obj->olocked)
2770.             return TRUE;
2771.         if (typ == EXPENSIVE_CAMERA)
2772.             return (obj->spe > 0);
2773.         break;
2774.     case FOOD_CLASS:
2775.         if (typ == CORPSE)
2776.             return (boolean) (((mon->misc_worn_check & W_ARMG) != 0L
2777.                                && touch_petrifies(&mons[obj->corpsenm]))
2778.                               || (!resists_ston(mon)
2779.                                   && cures_stoning(mon, obj, FALSE)));
2780.         if (typ == TIN)
2781.             return (boolean) (mcould_eat_tin(mon)
2782.                               && (!resists_ston(mon)
2783.                                   && cures_stoning(mon, obj, TRUE)));
2784.         if (typ == EGG && ismnum(obj->corpsenm))
2785.             return (boolean) touch_petrifies(&mons[obj->corpsenm]);
2786.         break;
2787.     default:
2788.         break;
2789.     }
2790. 
2791.     return FALSE;
2792. }
2793. 
2794. DISABLE_WARNING_FORMAT_NONLITERAL
2795. 
2796. boolean
2797. mon_reflects(struct monst *mon, const char *str)
2798. {
2799.     struct obj *orefl = which_armor(mon, W_ARMS);
2800. 
2801.     if (orefl && orefl->otyp == SHIELD_OF_REFLECTION) {
2802.         if (str) {
2803.             pline(str, s_suffix(mon_nam(mon)), "shield");
2804.             makeknown(SHIELD_OF_REFLECTION);
2805.         }
2806.         return TRUE;
2807.     } else if (arti_reflects(MON_WEP(mon))) {
2808.         /* due to wielded artifact weapon */
2809.         if (str)
2810.             pline(str, s_suffix(mon_nam(mon)), "weapon");
2811.         return TRUE;
2812.     } else if ((orefl = which_armor(mon, W_AMUL))
2813.                && orefl->otyp == AMULET_OF_REFLECTION) {
2814.         if (str) {
2815.             pline(str, s_suffix(mon_nam(mon)), "amulet");
2816.             makeknown(AMULET_OF_REFLECTION);
2817.         }
2818.         return TRUE;
2819.     } else if ((orefl = which_armor(mon, W_ARM))
2820.                && (orefl->otyp == SILVER_DRAGON_SCALES
2821.                    || orefl->otyp == SILVER_DRAGON_SCALE_MAIL)) {
2822.         if (str)
2823.             pline(str, s_suffix(mon_nam(mon)), "armor");
2824.         return TRUE;
2825.     } else if (mon->data == &mons[PM_SILVER_DRAGON]
2826.                || mon->data == &mons[PM_CHROMATIC_DRAGON]) {
2827.         /* Silver dragons only reflect when mature; babies do not */
2828.         if (str)
2829.             pline(str, s_suffix(mon_nam(mon)), "scales");
2830.         return TRUE;
2831.     }
2832.     return FALSE;
2833. }
2834. 
2835. boolean
2836. ureflects(const char *fmt, const char *str)
2837. {
2838.     /* Check from outermost to innermost objects */
2839.     if (EReflecting & W_ARMS) {
2840.         if (fmt && str) {
2841.             pline(fmt, str, "shield");
2842.             makeknown(SHIELD_OF_REFLECTION);
2843.         }
2844.         return TRUE;
2845.     } else if (EReflecting & W_WEP) {
2846.         /* Due to wielded artifact weapon */
2847.         if (fmt && str)
2848.             pline(fmt, str, "weapon");
2849.         return TRUE;
2850.     } else if (EReflecting & W_AMUL) {
2851.         if (fmt && str) {
2852.             pline(fmt, str, "medallion");
2853.             makeknown(AMULET_OF_REFLECTION);
2854.         }
2855.         return TRUE;
2856.     } else if (EReflecting & W_ARM) {
2857.         if (fmt && str)
2858.             pline(fmt, str, uskin ? "luster" : "armor");
2859.         return TRUE;
2860.     } else if (gy.youmonst.data == &mons[PM_SILVER_DRAGON]) {
2861.         if (fmt && str)
2862.             pline(fmt, str, "scales");
2863.         return TRUE;
2864.     }
2865.     return FALSE;
2866. }
2867. 
2868. RESTORE_WARNING_FORMAT_NONLITERAL
2869. 
2870. /* cure mon's blindness (use_defensive, dog_eat, meatobj) */
2871. void
2872. mcureblindness(struct monst *mon, boolean verbos)
2873. {
2874.     if (!mon->mcansee) {
2875.         mon->mcansee = 1;
2876.         mon->mblinded = 0;
2877.         if (verbos && haseyes(mon->data))
2878.             pline_mon(mon, "%s can see again.", Monnam(mon));
2879.     }
2880. }
2881. 
2882. /* TRUE if the monster ate something */
2883. boolean
2884. munstone(struct monst *mon, boolean by_you)
2885. {
2886.     struct obj *obj;
2887.     boolean tinok;
2888. 
2889.     if (resists_ston(mon))
2890.         return FALSE;
2891.     if (mon->meating || helpless(mon))
2892.         return FALSE;
2893.     mon->mstrategy &= ~STRAT_WAITFORU;
2894. 
2895.     tinok = mcould_eat_tin(mon);
2896.     for (obj = mon->minvent; obj; obj = obj->nobj) {
2897.         if (cures_stoning(mon, obj, tinok)) {
2898.             mon_consume_unstone(mon, obj, by_you, TRUE);
2899.             return TRUE;
2900.         }
2901.     }
2902.     return FALSE;
2903. }
2904. 
2905. staticfn void
2906. mon_consume_unstone(
2907.     struct monst *mon,
2908.     struct obj *obj,
2909.     boolean by_you,
2910.     boolean stoning) /* T: stop petrification, F: cure stun && confusion */
2911. {
2912.     boolean vis = canseemon(mon), tinned = obj->otyp == TIN,
2913.             food = obj->otyp == CORPSE || tinned,
2914.             acid = obj->otyp == POT_ACID
2915.                    || (food && acidic(&mons[obj->corpsenm])),
2916.             lizard = food && obj->corpsenm == PM_LIZARD;
2917.     int nutrit = food ? dog_nutrition(mon, obj) : 0; /* also sets meating */
2918. 
2919.     /* give a "<mon> is slowing down" message and also remove
2920.        intrinsic speed (comparable to similar effect on the hero) */
2921.     if (stoning)
2922.         mon_adjust_speed(mon, -3, (struct obj *) 0);
2923. 
2924.     if (vis) {
2925.         long save_quan = obj->quan;
2926. 
2927.         obj->quan = 1L;
2928.         pline_mon(mon, "%s %s %s.", Monnam(mon),
2929.               ((obj->oclass == POTION_CLASS) ? "quaffs"
2930.                : (obj->otyp == TIN) ? "opens and eats the contents of"
2931.                  : "eats"),
2932.               distant_name(obj, doname));
2933.         obj->quan = save_quan;
2934.     } else if (!Deaf)
2935.         You_hear("%s.",
2936.                  (obj->oclass == POTION_CLASS) ? "drinking" : "chewing");
2937. 
2938.     m_useup(mon, obj);
2939.     /* obj is now gone */
2940. 
2941.     if (acid && !tinned && !resists_acid(mon)) {
2942.         mon->mhp -= rnd(15);
2943.         if (vis)
2944.             pline_mon(mon, "%s has a very bad case of stomach acid.", Monnam(mon));
2945.         if (DEADMONSTER(mon)) {
2946.             pline_mon(mon, "%s dies!", Monnam(mon));
2947.             if (by_you)
2948.                 /* hero gets credit (experience) and blame (possible loss
2949.                    of alignment and/or luck and/or telepathy depending on
2950.                    mon) for the kill but does not break pacifism conduct */
2951.                 xkilled(mon, XKILL_NOMSG | XKILL_NOCONDUCT);
2952.             else
2953.                 mondead(mon);
2954.             return;
2955.         }
2956.     }
2957.     if (stoning && vis) {
2958.         if (Hallucination)
2959.             pline("What a pity - %s just ruined a future piece of art!",
2960.                   mon_nam(mon));
2961.         else
2962.             pline_mon(mon, "%s seems limber!", Monnam(mon));
2963.     }
2964.     if (lizard && (mon->mconf || mon->mstun)) {
2965.         mon->mconf = 0;
2966.         mon->mstun = 0;
2967.         if (vis && !is_bat(mon->data) && mon->data != &mons[PM_STALKER])
2968.             pline_mon(mon, "%s seems steadier now.", Monnam(mon));
2969.     }
2970.     if (mon->mtame && !mon->isminion && nutrit > 0) {
2971.         struct edog *edog = EDOG(mon);
2972. 
2973.         if (edog->hungrytime < svm.moves)
2974.             edog->hungrytime = svm.moves;
2975.         edog->hungrytime += nutrit;
2976.         mon->mconf = 0;
2977.     }
2978.     /* use up monster's next move */
2979.     mon->movement -= NORMAL_SPEED;
2980.     mon->mlstmv = svm.moves;
2981. }
2982. 
2983. /* decide whether obj can cure petrification; also used when picking up */
2984. staticfn boolean
2985. cures_stoning(struct monst *mon, struct obj *obj, boolean tinok)
2986. {
2987.     if (obj->otyp == POT_ACID)
2988.         return TRUE;
2989.     if (obj->otyp == GLOB_OF_GREEN_SLIME)
2990.         return (boolean) slimeproof(mon->data);
2991.     if (obj->otyp != CORPSE && (obj->otyp != TIN || !tinok))
2992.         return FALSE;
2993.     /* corpse, or tin that mon can open */
2994.     if (obj->corpsenm == NON_PM) /* empty/special tin */
2995.         return FALSE;
2996.     return (boolean) (obj->corpsenm == PM_LIZARD
2997.                       || acidic(&mons[obj->corpsenm]));
2998. }
2999. 
3000. staticfn boolean
3001. mcould_eat_tin(struct monst *mon)
3002. {
3003.     struct obj *obj, *mwep;
3004.     boolean welded_wep;
3005. 
3006.     /* monkeys who manage to steal tins can't open and eat them
3007.        even if they happen to also have the appropriate tool */
3008.     if (is_animal(mon->data))
3009.         return FALSE;
3010. 
3011.     mwep = MON_WEP(mon);
3012.     welded_wep = mwep && mwelded(mwep);
3013.     /* this is different from the player; tin opener or dagger doesn't
3014.        have to be wielded, and knife can be used instead of dagger */
3015.     for (obj = mon->minvent; obj; obj = obj->nobj) {
3016.         /* if stuck with a cursed weapon, don't check rest of inventory */
3017.         if (welded_wep && obj != mwep)
3018.             continue;
3019. 
3020.         if (obj->otyp == TIN_OPENER
3021.             || (obj->oclass == WEAPON_CLASS
3022.                 && (objects[obj->otyp].oc_skill == P_DAGGER
3023.                     || objects[obj->otyp].oc_skill == P_KNIFE)))
3024.             return TRUE;
3025.     }
3026.     return FALSE;
3027. }
3028. 
3029. /* TRUE if monster does something to avoid turning into green slime */
3030. boolean
3031. munslime(struct monst *mon, boolean by_you)
3032. {
3033.     struct obj *obj, odummy;
3034.     struct permonst *mptr = mon->data;
3035. 
3036.     /*
3037.      * muse_unslime() gives "mon starts turning green", "mon zaps
3038.      * itself with a wand of fire", and "mon's slime burns away"
3039.      * messages.  Monsters who don't get any chance at that just have
3040.      * (via our caller) newcham()'s "mon turns into slime" feedback.
3041.      */
3042. 
3043.     if (slimeproof(mptr))
3044.         return FALSE;
3045.     if (mon->meating || helpless(mon))
3046.         return FALSE;
3047.     mon->mstrategy &= ~STRAT_WAITFORU;
3048. 
3049.     /* if monster can breathe fire, do so upon self; a monster who deals
3050.        fire damage by biting, clawing, gazing, and especially exploding
3051.        isn't able to cure itself of green slime with its own attack
3052.        [possible extension: monst capable of casting high level clerical
3053.        spells could toss pillar of fire at self--probably too suicidal] */
3054.     if (!mon->mcan && !mon->mspec_used
3055.         && attacktype_fordmg(mptr, AT_BREA, AD_FIRE)) {
3056.         odummy = cg.zeroobj; /* otyp == STRANGE_OBJECT */
3057.         return muse_unslime(mon, &odummy, (struct trap *) 0, by_you);
3058.     }
3059. 
3060.     /* same MUSE criteria as use_defensive() */
3061.     if (!is_animal(mptr) && !mindless(mptr)) {
3062.         struct trap *t;
3063. 
3064.         for (obj = mon->minvent; obj; obj = obj->nobj)
3065.             if (cures_sliming(mon, obj))
3066.                 return muse_unslime(mon, obj, (struct trap *) 0, by_you);
3067. 
3068.         if (((t = t_at(mon->mx, mon->my)) == 0 || t->ttyp != FIRE_TRAP)
3069.             && mptr->mmove && !mon->mtrapped) {
3070.             coordxy xy[2][8], x, y, idx, ridx, nxy = 0;
3071. 
3072.             for (x = mon->mx - 1; x <= mon->mx + 1; ++x)
3073.                 for (y = mon->my - 1; y <= mon->my + 1; ++y)
3074.                     if (isok(x, y) && accessible(x, y)
3075.                         && !m_at(x, y) && (x != u.ux || y != u.uy)) {
3076.                         xy[0][nxy] = x, xy[1][nxy] = y;
3077.                         ++nxy;
3078.                     }
3079.             for (idx = 0; idx < nxy; ++idx) {
3080.                 ridx = rn1(nxy - idx, idx);
3081.                 if (ridx != idx) {
3082.                     x = xy[0][idx];
3083.                     xy[0][idx] = xy[0][ridx];
3084.                     xy[0][ridx] = x;
3085.                     y = xy[1][idx];
3086.                     xy[1][idx] = xy[1][ridx];
3087.                     xy[1][ridx] = y;
3088.                 }
3089.                 if ((t = t_at(xy[0][idx], xy[1][idx])) != 0
3090.                     && t->ttyp == FIRE_TRAP)
3091.                     break;
3092.             }
3093.         }
3094.         if (t && t->ttyp == FIRE_TRAP)
3095.             return muse_unslime(mon, &hands_obj, t, by_you);
3096. 
3097.     } /* MUSE */
3098. 
3099.     return FALSE;
3100. }
3101. 
3102. /* mon uses an item--selected by caller--to burn away incipient slime */
3103. staticfn boolean
3104. muse_unslime(
3105.     struct monst *mon,
3106.     struct obj *obj,
3107.     struct trap *trap,
3108.     boolean by_you) /* true: if mon kills itself, hero gets credit/blame */
3109. {                   /* [by_you not honored if 'mon' triggers fire trap]. */
3110.     struct obj *odummyp;
3111.     int otyp = obj->otyp, dmg = 0;
3112.     boolean vis = canseemon(mon), res = TRUE;
3113. 
3114.     if (vis)
3115.         pline_mon(mon, "%s starts turning %s.", Monnam(mon),
3116.               green_mon(mon) ? "into ooze" : hcolor(NH_GREEN));
3117.     /* -4 => sliming, causes quiet loss of enhanced speed */
3118.     mon_adjust_speed(mon, -4, (struct obj *) 0);
3119. 
3120.     if (trap) {
3121.         const char *Mnam = vis ? Monnam(mon) : 0;
3122. 
3123.         if (mon->mx == trap->tx && mon->my == trap->ty) {
3124.             if (vis)
3125.                 pline("%s triggers %s fire trap!", Mnam,
3126.                       trap->tseen ? "the" : "a");
3127.         } else {
3128.             remove_monster(mon->mx, mon->my);
3129.             newsym(mon->mx, mon->my);
3130.             place_monster(mon, trap->tx, trap->ty);
3131.             if (mon->wormno) /* won't happen; worms don't MUSE to unslime */
3132.                 worm_move(mon);
3133.             newsym(mon->mx, mon->my);
3134.             if (vis)
3135.                 pline("%s %s %s %s fire trap!", Mnam,
3136.                       vtense(fakename[0], locomotion(mon->data, "move")),
3137.                       is_floater(mon->data) ? "over" : "onto",
3138.                       trap->tseen ? "the" : "a");
3139.         }
3140.         (void) mintrap(mon, FORCETRAP);
3141.     } else if (otyp == STRANGE_OBJECT) {
3142.         /* monster is using fire breath on self */
3143.         if (vis)
3144.             pline_mon(mon, "%s.",
3145.                       monverbself(mon, Monnam(mon), "breath", "fire on"));
3146.         if (!rn2(3))
3147.             mon->mspec_used = rn1(10, 5);
3148.         /* -21 => monster's fire breath; 1 => # of damage dice */
3149.         dmg = zhitm(mon, by_you ? 21 : -21, 1, &odummyp);
3150.     } else if (otyp == SCR_FIRE) {
3151.         mreadmsg(mon, obj);
3152.         if (mon->mconf) {
3153.             if (cansee(mon->mx, mon->my))
3154.                 pline("Oh, what a pretty fire!");
3155.             if (vis)
3156.                 trycall(obj);
3157.             m_useup(mon, obj); /* after trycall() */
3158.             vis = FALSE;       /* skip makeknown() below */
3159.             res = FALSE;       /* failed to cure sliming */
3160.         } else {
3161.             dmg = (2 * (rn1(3, 3) + 2 * bcsign(obj)) + 1) / 3;
3162.             m_useup(mon, obj); /* before explode() */
3163.             /* -11 => monster's fireball */
3164.             explode(mon->mx, mon->my, -11, dmg, SCROLL_CLASS,
3165.                     /* by_you: override -11 for mon but not others */
3166.                     by_you ? -EXPL_FIERY : EXPL_FIERY);
3167.             dmg = 0; /* damage has been applied by explode() */
3168.         }
3169.     } else if (otyp == POT_OIL) {
3170.         char Pronoun[40];
3171.         boolean was_lit = obj->lamplit ? TRUE : FALSE, saw_lit = FALSE;
3172.         /*
3173.          * If not already lit, requires two actions.  We cheat and let
3174.          * monster do both rather than render the potion unusable.
3175.          *
3176.          * Monsters don't start with oil and don't actively pick up oil
3177.          * so this may never occur in a real game.  (Possible though;
3178.          * nymph can steal potions of oil; shapechanger could take on
3179.          * nymph form or vacuum up stuff as a gel.cube and then eventually
3180.          * engage with a green slime.)
3181.          */
3182. 
3183.         if (obj->quan > 1L)
3184.             obj = splitobj(obj, 1L);
3185.         if (vis && !was_lit) {
3186.             pline_mon(mon, "%s ignites %s.", Monnam(mon), ansimpleoname(obj));
3187.             saw_lit = TRUE;
3188.         }
3189.         begin_burn(obj, was_lit);
3190.         vis |= canseemon(mon); /* burning potion may improve visibility */
3191.         if (vis) {
3192.             if (!Unaware)
3193.                 observe_object(obj); /* hero is watching mon drink obj */
3194.             pline("%s quaffs a burning %s",
3195.                   saw_lit ? upstart(strcpy(Pronoun, mhe(mon))) : Monnam(mon),
3196.                   simpleonames(obj));
3197.             makeknown(POT_OIL);
3198.         }
3199.         dmg = d(3, 4); /* [**TEMP** (different from hero)] */
3200.         m_useup(mon, obj);
3201.     } else { /* wand/horn of fire w/ positive charge count */
3202.         if (obj->otyp == FIRE_HORN)
3203.             mplayhorn(mon, obj, TRUE);
3204.         else
3205.             mzapwand(mon, obj, TRUE);
3206.         /* -1 => monster's wand of fire; 2 => # of damage dice */
3207.         dmg = zhitm(mon, by_you ? 1 : -1, 2, &odummyp);
3208.     }
3209. 
3210.     if (dmg) {
3211.         /* zhitm() applies damage but doesn't kill creature off;
3212.            for fire breath, dmg is going to be 0 (fire breathers are
3213.            immune to fire damage) but for wand of fire or fire horn,
3214.            'mon' could have taken damage so might die */
3215.         if (DEADMONSTER(mon)) {
3216.             if (by_you) {
3217.                 /* mon killed self but hero gets credit and blame (except
3218.                    for pacifist conduct); xkilled()'s message would say
3219.                    "You killed/destroyed <mon>" so give our own message */
3220.                 if (vis)
3221.                     pline_mon(mon, "%s is %s by the fire!", Monnam(mon),
3222.                           nonliving(mon->data) ? "destroyed" : "killed");
3223.                 xkilled(mon, XKILL_NOMSG | XKILL_NOCONDUCT);
3224.             } else
3225.                 monkilled(mon, "fire", AD_FIRE);
3226.         } else {
3227.             /* non-fatal damage occurred */
3228.             if (vis)
3229.                 pline_mon(mon, "%s is burned%s", Monnam(mon), exclam(dmg));
3230.         }
3231.     }
3232.     if (vis) {
3233.         if (res && !DEADMONSTER(mon))
3234.             pline_mon(mon, "%s slime is burned away!", s_suffix(Monnam(mon)));
3235.         if (otyp != STRANGE_OBJECT)
3236.             makeknown(otyp);
3237.     }
3238.     /* use up monster's next move */
3239.     mon->movement -= NORMAL_SPEED;
3240.     mon->mlstmv = svm.moves;
3241.     return res;
3242. }
3243. 
3244. /* decide whether obj can be used to cure green slime */
3245. staticfn int
3246. cures_sliming(struct monst *mon, struct obj *obj)
3247. {
3248.     /* scroll of fire */
3249.     if (obj->otyp == SCR_FIRE)
3250.         return (haseyes(mon->data) && mon->mcansee && !nohands(mon->data));
3251. 
3252.     /* potion of oil; will be set burning if not already */
3253.     if (obj->otyp == POT_OIL)
3254.         return !nohands(mon->data);
3255. 
3256.     /* non-empty wand or horn of fire;
3257.        hero doesn't need hands or even limbs to zap, so mon doesn't either */
3258.     return ((obj->otyp == WAN_FIRE
3259.              || (obj->otyp == FIRE_HORN && can_blow(mon)))
3260.             && obj->spe > 0);
3261. }
3262. 
3263. /* TRUE if monster appears to be green; we go by the display color.
3264.    The alternative was to just pick things that
3265.    seem plausibly green (which didn't necessarily match the categorization
3266.    by the color of the text).
3267.    iflags.use_color is not meant for game behavior decisions */
3268. staticfn boolean
3269. green_mon(struct monst *mon)
3270. {
3271.     struct permonst *ptr = mon->data;
3272. 
3273.     if (Hallucination)
3274.         return FALSE;
3275.     return (ptr->mcolor == CLR_GREEN || ptr->mcolor == CLR_BRIGHT_GREEN);
3276. #if 0
3277.     if (iflags.use_color)
3278.         return (ptr->mcolor == CLR_GREEN || ptr->mcolor == CLR_BRIGHT_GREEN);
3279.     else {
3280.         /* approximation */
3281.         if (strstri(ptr->pmnames[NEUTRAL], "green")
3282.             || (ptr->pmnames[MALE] && strstri(ptr->pmnames[MALE], "green"))
3283.             || (ptr->pmnames[FEMALE]
3284.                 && strstri(ptr->pmnames[FEMALE], "green")))
3285.             return TRUE;
3286.         switch (monsndx(ptr)) {
3287.         case PM_FOREST_CENTAUR:
3288.         case PM_GARTER_SNAKE:
3289.         case PM_GECKO:
3290.         case PM_GREMLIN:
3291.         case PM_HOMUNCULUS:
3292.         case PM_JUIBLEX:
3293.         case PM_LEPRECHAUN:
3294.         case PM_LICHEN:
3295.         case PM_LIZARD:
3296.         case PM_WOOD_NYMPH:
3297.             return TRUE;
3298.         default:
3299.             if (is_elf(ptr) && !is_prince(ptr) && !is_lord(ptr)
3300.                 && ptr != &mons[PM_GREY_ELF])
3301.                 return TRUE;
3302.             break;
3303.         }
3304.     }
3305.     return FALSE;
3306. #endif
3307. }
3308. 
3309. /*muse.c*/