OSDN Git Service

import nethack-3.6.0
[jnethack/source.git] / src / muse.c
1 /* NetHack 3.6  muse.c  $NHDT-Date: 1447987786 2015/11/20 02:49:46 $  $NHDT-Branch: master $:$NHDT-Revision: 1.68 $ */
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 extern const int monstr[];
12
13 boolean m_using = FALSE;
14
15 /* Let monsters use magic items.  Arbitrary assumptions: Monsters only use
16  * scrolls when they can see, monsters know when wands have 0 charges,
17  * monsters cannot recognize if items are cursed are not, monsters which
18  * are confused don't know not to read scrolls, etc....
19  */
20
21 STATIC_DCL struct permonst *FDECL(muse_newcham_mon, (struct monst *));
22 STATIC_DCL int FDECL(precheck, (struct monst *, struct obj *));
23 STATIC_DCL void FDECL(mzapmsg, (struct monst *, struct obj *, BOOLEAN_P));
24 STATIC_DCL void FDECL(mreadmsg, (struct monst *, struct obj *));
25 STATIC_DCL void FDECL(mquaffmsg, (struct monst *, struct obj *));
26 STATIC_PTR int FDECL(mbhitm, (struct monst *, struct obj *));
27 STATIC_DCL void FDECL(mbhit, (struct monst *, int,
28                               int FDECL((*), (MONST_P, OBJ_P)),
29                               int FDECL((*), (OBJ_P, OBJ_P)), struct obj *));
30 STATIC_DCL void FDECL(you_aggravate, (struct monst *));
31 STATIC_DCL void FDECL(mon_consume_unstone, (struct monst *, struct obj *,
32                                             BOOLEAN_P, BOOLEAN_P));
33 STATIC_DCL boolean FDECL(cures_stoning, (struct monst *, struct obj *,
34                                          BOOLEAN_P));
35 STATIC_DCL boolean FDECL(mcould_eat_tin, (struct monst *));
36 STATIC_DCL boolean FDECL(muse_unslime, (struct monst *, struct obj *,
37                                         BOOLEAN_P));
38 STATIC_DCL int FDECL(cures_sliming, (struct monst *, struct obj *));
39 STATIC_DCL boolean FDECL(green_mon, (struct monst *));
40
41 static struct musable {
42     struct obj *offensive;
43     struct obj *defensive;
44     struct obj *misc;
45     int has_offense, has_defense, has_misc;
46     /* =0, no capability; otherwise, different numbers.
47      * If it's an object, the object is also set (it's 0 otherwise).
48      */
49 } m;
50 static int trapx, trapy;
51 static boolean zap_oseen; /* for wands which use mbhitm and are zapped at
52                            * players.  We usually want an oseen local to
53                            * the function, but this is impossible since the
54                            * function mbhitm has to be compatible with the
55                            * normal zap routines, and those routines don't
56                            * remember who zapped the wand. */
57
58 /* Any preliminary checks which may result in the monster being unable to use
59  * the item.  Returns 0 if nothing happened, 2 if the monster can't do
60  * anything (i.e. it teleported) and 1 if it's dead.
61  */
62 STATIC_OVL int
63 precheck(mon, obj)
64 struct monst *mon;
65 struct obj *obj;
66 {
67     boolean vis;
68
69     if (!obj)
70         return 0;
71     vis = cansee(mon->mx, mon->my);
72
73     if (obj->oclass == POTION_CLASS) {
74         coord cc;
75         static const char *empty = "The potion turns out to be empty.";
76         const char *potion_descr;
77         struct monst *mtmp;
78
79         potion_descr = OBJ_DESCR(objects[obj->otyp]);
80         if (potion_descr && !strcmp(potion_descr, "milky")) {
81             if (!(mvitals[PM_GHOST].mvflags & G_GONE)
82                 && !rn2(POTION_OCCUPANT_CHANCE(mvitals[PM_GHOST].born))) {
83                 if (!enexto(&cc, mon->mx, mon->my, &mons[PM_GHOST]))
84                     return 0;
85                 mquaffmsg(mon, obj);
86                 m_useup(mon, obj);
87                 mtmp = makemon(&mons[PM_GHOST], cc.x, cc.y, NO_MM_FLAGS);
88                 if (!mtmp) {
89                     if (vis)
90                         pline1(empty);
91                 } else {
92                     if (vis) {
93                         pline(
94                             "As %s opens the bottle, an enormous %s emerges!",
95                               mon_nam(mon),
96                               Hallucination ? rndmonnam(NULL)
97                                             : (const char *) "ghost");
98                         pline("%s is frightened to death, and unable to move.",
99                               Monnam(mon));
100                     }
101                     paralyze_monst(mon, 3);
102                 }
103                 return 2;
104             }
105         }
106         if (potion_descr && !strcmp(potion_descr, "smoky")
107             && !(mvitals[PM_DJINNI].mvflags & G_GONE)
108             && !rn2(POTION_OCCUPANT_CHANCE(mvitals[PM_DJINNI].born))) {
109             if (!enexto(&cc, mon->mx, mon->my, &mons[PM_DJINNI]))
110                 return 0;
111             mquaffmsg(mon, obj);
112             m_useup(mon, obj);
113             mtmp = makemon(&mons[PM_DJINNI], cc.x, cc.y, NO_MM_FLAGS);
114             if (!mtmp) {
115                 if (vis)
116                     pline1(empty);
117             } else {
118                 if (vis)
119                     pline("In a cloud of smoke, %s emerges!", a_monnam(mtmp));
120                 pline("%s speaks.", vis ? Monnam(mtmp) : Something);
121                 /* I suspect few players will be upset that monsters */
122                 /* can't wish for wands of death here.... */
123                 if (rn2(2)) {
124                     verbalize("You freed me!");
125                     mtmp->mpeaceful = 1;
126                     set_malign(mtmp);
127                 } else {
128                     verbalize("It is about time.");
129                     if (vis)
130                         pline("%s vanishes.", Monnam(mtmp));
131                     mongone(mtmp);
132                 }
133             }
134             return 2;
135         }
136     }
137     if (obj->oclass == WAND_CLASS && obj->cursed
138         && !rn2(WAND_BACKFIRE_CHANCE)) {
139         int dam = d(obj->spe + 2, 6);
140
141         if (!Deaf) {
142             if (vis)
143                 pline("%s zaps %s, which suddenly explodes!", Monnam(mon),
144                       an(xname(obj)));
145             else
146                 You_hear("a zap and an explosion in the distance.");
147         }
148         m_useup(mon, obj);
149         if (mon->mhp <= dam) {
150             monkilled(mon, "", AD_RBRE);
151             return 1;
152         } else
153             mon->mhp -= dam;
154         m.has_defense = m.has_offense = m.has_misc = 0;
155         /* Only one needed to be set to 0 but the others are harmless */
156     }
157     return 0;
158 }
159
160 STATIC_OVL void
161 mzapmsg(mtmp, otmp, self)
162 struct monst *mtmp;
163 struct obj *otmp;
164 boolean self;
165 {
166     if (!canseemon(mtmp)) {
167         if (!Deaf)
168             You_hear("a %s zap.", (distu(mtmp->mx, mtmp->my)
169                                    <= (BOLT_LIM + 1) * (BOLT_LIM + 1))
170                                       ? "nearby"
171                                       : "distant");
172     } else if (self) {
173         pline("%s zaps %sself with %s!", Monnam(mtmp), mhim(mtmp),
174               doname(otmp));
175     } else {
176         pline("%s zaps %s!", Monnam(mtmp), an(xname(otmp)));
177         stop_occupation();
178     }
179 }
180
181 STATIC_OVL void
182 mreadmsg(mtmp, otmp)
183 struct monst *mtmp;
184 struct obj *otmp;
185 {
186     boolean vismon = canseemon(mtmp);
187     char onambuf[BUFSZ];
188     short saverole;
189     unsigned savebknown;
190
191     if (!vismon && Deaf)
192         return; /* no feedback */
193
194     otmp->dknown = 1; /* seeing or hearing it read reveals its label */
195     /* shouldn't be able to hear curse/bless status of unseen scrolls;
196        for priest characters, bknown will always be set during naming */
197     savebknown = otmp->bknown;
198     saverole = Role_switch;
199     if (!vismon) {
200         otmp->bknown = 0;
201         if (Role_if(PM_PRIEST))
202             Role_switch = 0;
203     }
204     Strcpy(onambuf, singular(otmp, doname));
205     Role_switch = saverole;
206     otmp->bknown = savebknown;
207
208     if (vismon)
209         pline("%s reads %s!", Monnam(mtmp), onambuf);
210     else
211         You_hear("%s reading %s.",
212                  x_monnam(mtmp, ARTICLE_A, (char *) 0,
213                           (SUPPRESS_IT | SUPPRESS_INVISIBLE | SUPPRESS_SADDLE),
214                           FALSE),
215                  onambuf);
216
217     if (mtmp->mconf)
218         pline("Being confused, %s mispronounces the magic words...",
219               vismon ? mon_nam(mtmp) : mhe(mtmp));
220 }
221
222 STATIC_OVL void
223 mquaffmsg(mtmp, otmp)
224 struct monst *mtmp;
225 struct obj *otmp;
226 {
227     if (canseemon(mtmp)) {
228         otmp->dknown = 1;
229         pline("%s drinks %s!", Monnam(mtmp), singular(otmp, doname));
230     } else if (!Deaf)
231         You_hear("a chugging sound.");
232 }
233
234 /* Defines for various types of stuff.  The order in which monsters prefer
235  * to use them is determined by the order of the code logic, not the
236  * numerical order in which they are defined.
237  */
238 #define MUSE_SCR_TELEPORTATION 1
239 #define MUSE_WAN_TELEPORTATION_SELF 2
240 #define MUSE_POT_HEALING 3
241 #define MUSE_POT_EXTRA_HEALING 4
242 #define MUSE_WAN_DIGGING 5
243 #define MUSE_TRAPDOOR 6
244 #define MUSE_TELEPORT_TRAP 7
245 #define MUSE_UPSTAIRS 8
246 #define MUSE_DOWNSTAIRS 9
247 #define MUSE_WAN_CREATE_MONSTER 10
248 #define MUSE_SCR_CREATE_MONSTER 11
249 #define MUSE_UP_LADDER 12
250 #define MUSE_DN_LADDER 13
251 #define MUSE_SSTAIRS 14
252 #define MUSE_WAN_TELEPORTATION 15
253 #define MUSE_BUGLE 16
254 #define MUSE_UNICORN_HORN 17
255 #define MUSE_POT_FULL_HEALING 18
256 #define MUSE_LIZARD_CORPSE 19
257 /*
258 #define MUSE_INNATE_TPT 9999
259  * We cannot use this.  Since monsters get unlimited teleportation, if they
260  * were allowed to teleport at will you could never catch them.  Instead,
261  * assume they only teleport at random times, despite the inconsistency
262  * that if you polymorph into one you teleport at will.
263  */
264
265 /* Select a defensive item/action for a monster.  Returns TRUE iff one is
266    found. */
267 boolean
268 find_defensive(mtmp)
269 struct monst *mtmp;
270 {
271     register struct obj *obj = 0;
272     struct trap *t;
273     int x = mtmp->mx, y = mtmp->my;
274     boolean stuck = (mtmp == u.ustuck);
275     boolean immobile = (mtmp->data->mmove == 0);
276     int fraction;
277
278     if (is_animal(mtmp->data) || mindless(mtmp->data))
279         return FALSE;
280     if (dist2(x, y, mtmp->mux, mtmp->muy) > 25)
281         return FALSE;
282     if (u.uswallow && stuck)
283         return FALSE;
284
285     m.defensive = (struct obj *) 0;
286     m.has_defense = 0;
287
288     /* since unicorn horns don't get used up, the monster would look
289      * silly trying to use the same cursed horn round after round
290      */
291     if (mtmp->mconf || mtmp->mstun || !mtmp->mcansee) {
292         if (!is_unicorn(mtmp->data) && !nohands(mtmp->data)) {
293             for (obj = mtmp->minvent; obj; obj = obj->nobj)
294                 if (obj->otyp == UNICORN_HORN && !obj->cursed)
295                     break;
296         }
297         if (obj || is_unicorn(mtmp->data)) {
298             m.defensive = obj;
299             m.has_defense = MUSE_UNICORN_HORN;
300             return TRUE;
301         }
302     }
303
304     if (mtmp->mconf || mtmp->mstun) {
305         struct obj *liztin = 0;
306
307         for (obj = mtmp->minvent; obj; obj = obj->nobj) {
308             if (obj->otyp == CORPSE && obj->corpsenm == PM_LIZARD) {
309                 m.defensive = obj;
310                 m.has_defense = MUSE_LIZARD_CORPSE;
311                 return TRUE;
312             } else if (obj->otyp == TIN && obj->corpsenm == PM_LIZARD) {
313                 liztin = obj;
314             }
315         }
316         /* confused or stunned monster might not be able to open tin */
317         if (liztin && mcould_eat_tin(mtmp) && rn2(3)) {
318             m.defensive = liztin;
319             /* tin and corpse ultimately end up being handled the same */
320             m.has_defense = MUSE_LIZARD_CORPSE;
321             return TRUE;
322         }
323     }
324
325     /* It so happens there are two unrelated cases when we might want to
326      * check specifically for healing alone.  The first is when the monster
327      * is blind (healing cures blindness).  The second is when the monster
328      * is peaceful; then we don't want to flee the player, and by
329      * coincidence healing is all there is that doesn't involve fleeing.
330      * These would be hard to combine because of the control flow.
331      * Pestilence won't use healing even when blind.
332      */
333     if (!mtmp->mcansee && !nohands(mtmp->data)
334         && mtmp->data != &mons[PM_PESTILENCE]) {
335         if ((obj = m_carrying(mtmp, POT_FULL_HEALING)) != 0) {
336             m.defensive = obj;
337             m.has_defense = MUSE_POT_FULL_HEALING;
338             return TRUE;
339         }
340         if ((obj = m_carrying(mtmp, POT_EXTRA_HEALING)) != 0) {
341             m.defensive = obj;
342             m.has_defense = MUSE_POT_EXTRA_HEALING;
343             return TRUE;
344         }
345         if ((obj = m_carrying(mtmp, POT_HEALING)) != 0) {
346             m.defensive = obj;
347             m.has_defense = MUSE_POT_HEALING;
348             return TRUE;
349         }
350     }
351
352     fraction = u.ulevel < 10 ? 5 : u.ulevel < 14 ? 4 : 3;
353     if (mtmp->mhp >= mtmp->mhpmax
354         || (mtmp->mhp >= 10 && mtmp->mhp * fraction >= mtmp->mhpmax))
355         return FALSE;
356
357     if (mtmp->mpeaceful) {
358         if (!nohands(mtmp->data)) {
359             if ((obj = m_carrying(mtmp, POT_FULL_HEALING)) != 0) {
360                 m.defensive = obj;
361                 m.has_defense = MUSE_POT_FULL_HEALING;
362                 return TRUE;
363             }
364             if ((obj = m_carrying(mtmp, POT_EXTRA_HEALING)) != 0) {
365                 m.defensive = obj;
366                 m.has_defense = MUSE_POT_EXTRA_HEALING;
367                 return TRUE;
368             }
369             if ((obj = m_carrying(mtmp, POT_HEALING)) != 0) {
370                 m.defensive = obj;
371                 m.has_defense = MUSE_POT_HEALING;
372                 return TRUE;
373             }
374         }
375         return FALSE;
376     }
377
378     if (stuck || immobile) {
379         ; /* fleeing by stairs or traps is not possible */
380     } else if (levl[x][y].typ == STAIRS) {
381         if (x == xdnstair && y == ydnstair) {
382             if (!is_floater(mtmp->data))
383                 m.has_defense = MUSE_DOWNSTAIRS;
384         } else if (x == xupstair && y == yupstair) {
385             /* don't let monster leave the dungeon with the Amulet */
386             if (ledger_no(&u.uz) != 1)
387                 m.has_defense = MUSE_UPSTAIRS;
388         } else if (sstairs.sx && x == sstairs.sx && y == sstairs.sy) {
389             if (sstairs.up || !is_floater(mtmp->data))
390                 m.has_defense = MUSE_SSTAIRS;
391         }
392     } else if (levl[x][y].typ == LADDER) {
393         if (x == xupladder && y == yupladder) {
394             m.has_defense = MUSE_UP_LADDER;
395         } else if (x == xdnladder && y == ydnladder) {
396             if (!is_floater(mtmp->data))
397                 m.has_defense = MUSE_DN_LADDER;
398         } else if (sstairs.sx && x == sstairs.sx && y == sstairs.sy) {
399             if (sstairs.up || !is_floater(mtmp->data))
400                 m.has_defense = MUSE_SSTAIRS;
401         }
402     } else {
403         /* Note: trap doors take precedence over teleport traps. */
404         int xx, yy, i, locs[10][2];
405         boolean ignore_boulders = (verysmall(mtmp->data)
406                                    || throws_rocks(mtmp->data)
407                                    || passes_walls(mtmp->data)),
408             diag_ok = !NODIAG(monsndx(mtmp->data));
409
410         for (i = 0; i < 10; ++i) /* 10: 9 spots plus sentinel */
411             locs[i][0] = locs[i][1] = 0;
412         /* collect viable spots; monster's <mx,my> comes first */
413         locs[0][0] = x, locs[0][1] = y;
414         i = 1;
415         for (xx = x - 1; xx <= x + 1; xx++)
416             for (yy = y - 1; yy <= y + 1; yy++)
417                 if (isok(xx, yy) && (xx != x || yy != y)) {
418                     locs[i][0] = xx, locs[i][1] = yy;
419                     ++i;
420                 }
421         /* look for a suitable trap among the viable spots */
422         for (i = 0; i < 10; ++i) {
423             xx = locs[i][0], yy = locs[i][1];
424             if (!xx)
425                 break; /* we've run out of spots */
426             /* skip if it's hero's location
427                or a diagonal spot and monster can't move diagonally
428                or some other monster is there */
429             if ((xx == u.ux && yy == u.uy)
430                 || (xx != x && yy != y && !diag_ok)
431                 || (level.monsters[xx][yy] && !(xx == x && yy == y)))
432                 continue;
433             /* skip if there's no trap or can't/won't move onto trap */
434             if ((t = t_at(xx, yy)) == 0
435                 || (!ignore_boulders && sobj_at(BOULDER, xx, yy))
436                 || onscary(xx, yy, mtmp))
437                 continue;
438             /* use trap if it's the correct type */
439             if ((t->ttyp == TRAPDOOR || t->ttyp == HOLE)
440                 && !is_floater(mtmp->data)
441                 && !mtmp->isshk && !mtmp->isgd && !mtmp->ispriest
442                 && Can_fall_thru(&u.uz)) {
443                 trapx = xx;
444                 trapy = yy;
445                 m.has_defense = MUSE_TRAPDOOR;
446                 break; /* no need to look at any other spots */
447             } else if (t->ttyp == TELEP_TRAP) {
448                 trapx = xx;
449                 trapy = yy;
450                 m.has_defense = MUSE_TELEPORT_TRAP;
451             }
452         }
453     }
454
455     if (nohands(mtmp->data)) /* can't use objects */
456         goto botm;
457
458     if (is_mercenary(mtmp->data) && (obj = m_carrying(mtmp, BUGLE)) != 0) {
459         int xx, yy;
460         struct monst *mon;
461
462         /* Distance is arbitrary.  What we really want to do is
463          * have the soldier play the bugle when it sees or
464          * remembers soldiers nearby...
465          */
466         for (xx = x - 3; xx <= x + 3; xx++) {
467             for (yy = y - 3; yy <= y + 3; yy++) {
468                 if (!isok(xx, yy) || (xx == x && yy == y))
469                     continue;
470                 if ((mon = m_at(xx, yy)) != 0 && is_mercenary(mon->data)
471                     && mon->data != &mons[PM_GUARD]
472                     && (mon->msleeping || !mon->mcanmove)) {
473                     m.defensive = obj;
474                     m.has_defense = MUSE_BUGLE;
475                     goto toot; /* double break */
476                 }
477             }
478         }
479     toot:
480         ;
481     }
482
483     /* use immediate physical escape prior to attempting magic */
484     if (m.has_defense) /* stairs, trap door or tele-trap, bugle alert */
485         goto botm;
486
487     /* kludge to cut down on trap destruction (particularly portals) */
488     t = t_at(x, y);
489     if (t && (t->ttyp == PIT || t->ttyp == SPIKED_PIT || t->ttyp == WEB
490               || t->ttyp == BEAR_TRAP))
491         t = 0; /* ok for monster to dig here */
492
493 #define nomore(x)       if (m.has_defense == x) continue;
494     /* selection could be improved by collecting all possibilities
495        into an array and then picking one at random */
496     for (obj = mtmp->minvent; obj; obj = obj->nobj) {
497         /* don't always use the same selection pattern */
498         if (m.has_defense && !rn2(3))
499             break;
500
501         /* nomore(MUSE_WAN_DIGGING); */
502         if (m.has_defense == MUSE_WAN_DIGGING)
503             break;
504         if (obj->otyp == WAN_DIGGING && obj->spe > 0 && !stuck && !t
505             && !mtmp->isshk && !mtmp->isgd && !mtmp->ispriest
506             && !is_floater(mtmp->data)
507             /* monsters digging in Sokoban can ruin things */
508             && !Sokoban
509             /* digging wouldn't be effective; assume they know that */
510             && !(levl[x][y].wall_info & W_NONDIGGABLE)
511             && !(Is_botlevel(&u.uz) || In_endgame(&u.uz))
512             && !(is_ice(x, y) || is_pool(x, y) || is_lava(x, y))
513             && !(mtmp->data == &mons[PM_VLAD_THE_IMPALER]
514                  && In_V_tower(&u.uz))) {
515             m.defensive = obj;
516             m.has_defense = MUSE_WAN_DIGGING;
517         }
518         nomore(MUSE_WAN_TELEPORTATION_SELF);
519         nomore(MUSE_WAN_TELEPORTATION);
520         if (obj->otyp == WAN_TELEPORTATION && obj->spe > 0) {
521             /* use the TELEP_TRAP bit to determine if they know
522              * about noteleport on this level or not.  Avoids
523              * ineffective re-use of teleportation.  This does
524              * mean if the monster leaves the level, they'll know
525              * about teleport traps.
526              */
527             if (!level.flags.noteleport
528                 || !(mtmp->mtrapseen & (1 << (TELEP_TRAP - 1)))) {
529                 m.defensive = obj;
530                 m.has_defense = (mon_has_amulet(mtmp))
531                                     ? MUSE_WAN_TELEPORTATION
532                                     : MUSE_WAN_TELEPORTATION_SELF;
533             }
534         }
535         nomore(MUSE_SCR_TELEPORTATION);
536         if (obj->otyp == SCR_TELEPORTATION && mtmp->mcansee
537             && haseyes(mtmp->data)
538             && (!obj->cursed || (!(mtmp->isshk && inhishop(mtmp))
539                                  && !mtmp->isgd && !mtmp->ispriest))) {
540             /* see WAN_TELEPORTATION case above */
541             if (!level.flags.noteleport
542                 || !(mtmp->mtrapseen & (1 << (TELEP_TRAP - 1)))) {
543                 m.defensive = obj;
544                 m.has_defense = MUSE_SCR_TELEPORTATION;
545             }
546         }
547
548         if (mtmp->data != &mons[PM_PESTILENCE]) {
549             nomore(MUSE_POT_FULL_HEALING);
550             if (obj->otyp == POT_FULL_HEALING) {
551                 m.defensive = obj;
552                 m.has_defense = MUSE_POT_FULL_HEALING;
553             }
554             nomore(MUSE_POT_EXTRA_HEALING);
555             if (obj->otyp == POT_EXTRA_HEALING) {
556                 m.defensive = obj;
557                 m.has_defense = MUSE_POT_EXTRA_HEALING;
558             }
559             nomore(MUSE_WAN_CREATE_MONSTER);
560             if (obj->otyp == WAN_CREATE_MONSTER && obj->spe > 0) {
561                 m.defensive = obj;
562                 m.has_defense = MUSE_WAN_CREATE_MONSTER;
563             }
564             nomore(MUSE_POT_HEALING);
565             if (obj->otyp == POT_HEALING) {
566                 m.defensive = obj;
567                 m.has_defense = MUSE_POT_HEALING;
568             }
569         } else { /* Pestilence */
570             nomore(MUSE_POT_FULL_HEALING);
571             if (obj->otyp == POT_SICKNESS) {
572                 m.defensive = obj;
573                 m.has_defense = MUSE_POT_FULL_HEALING;
574             }
575             nomore(MUSE_WAN_CREATE_MONSTER);
576             if (obj->otyp == WAN_CREATE_MONSTER && obj->spe > 0) {
577                 m.defensive = obj;
578                 m.has_defense = MUSE_WAN_CREATE_MONSTER;
579             }
580         }
581         nomore(MUSE_SCR_CREATE_MONSTER);
582         if (obj->otyp == SCR_CREATE_MONSTER) {
583             m.defensive = obj;
584             m.has_defense = MUSE_SCR_CREATE_MONSTER;
585         }
586     }
587 botm:
588     return (boolean) !!m.has_defense;
589 #undef nomore
590 }
591
592 /* Perform a defensive action for a monster.  Must be called immediately
593  * after find_defensive().  Return values are 0: did something, 1: died,
594  * 2: did something and can't attack again (i.e. teleported).
595  */
596 int
597 use_defensive(mtmp)
598 struct monst *mtmp;
599 {
600     int i, fleetim, how = 0;
601     struct obj *otmp = m.defensive;
602     boolean vis, vismon, oseen;
603     const char *mcsa = "%s can see again.";
604
605     if ((i = precheck(mtmp, otmp)) != 0)
606         return i;
607     vis = cansee(mtmp->mx, mtmp->my);
608     vismon = canseemon(mtmp);
609     oseen = otmp && vismon;
610
611     /* when using defensive choice to run away, we want monster to avoid
612        rushing right straight back; don't override if already scared */
613     fleetim = !mtmp->mflee ? (33 - (30 * mtmp->mhp / mtmp->mhpmax)) : 0;
614 #define m_flee(m)                          \
615     if (fleetim && !m->iswiz) {            \
616         monflee(m, fleetim, FALSE, FALSE); \
617     }
618
619     switch (m.has_defense) {
620     case MUSE_UNICORN_HORN:
621         if (vismon) {
622             if (otmp)
623                 pline("%s uses a unicorn horn!", Monnam(mtmp));
624             else
625                 pline_The("tip of %s's horn glows!", mon_nam(mtmp));
626         }
627         if (!mtmp->mcansee) {
628             mtmp->mcansee = 1;
629             mtmp->mblinded = 0;
630             if (vismon)
631                 pline(mcsa, Monnam(mtmp));
632         } else if (mtmp->mconf || mtmp->mstun) {
633             mtmp->mconf = mtmp->mstun = 0;
634             if (vismon)
635                 pline("%s seems steadier now.", Monnam(mtmp));
636         } else
637             impossible("No need for unicorn horn?");
638         return 2;
639     case MUSE_BUGLE:
640         if (vismon)
641             pline("%s plays %s!", Monnam(mtmp), doname(otmp));
642         else if (!Deaf)
643             You_hear("a bugle playing reveille!");
644         awaken_soldiers(mtmp);
645         return 2;
646     case MUSE_WAN_TELEPORTATION_SELF:
647         if ((mtmp->isshk && inhishop(mtmp)) || mtmp->isgd || mtmp->ispriest)
648             return 2;
649         m_flee(mtmp);
650         mzapmsg(mtmp, otmp, TRUE);
651         otmp->spe--;
652         how = WAN_TELEPORTATION;
653     mon_tele:
654         if (tele_restrict(mtmp)) { /* mysterious force... */
655             if (vismon && how)     /* mentions 'teleport' */
656                 makeknown(how);
657             /* monster learns that teleportation isn't useful here */
658             if (level.flags.noteleport)
659                 mtmp->mtrapseen |= (1 << (TELEP_TRAP - 1));
660             return 2;
661         }
662         if ((mon_has_amulet(mtmp) || On_W_tower_level(&u.uz)) && !rn2(3)) {
663             if (vismon)
664                 pline("%s seems disoriented for a moment.", Monnam(mtmp));
665             return 2;
666         }
667         if (oseen && how)
668             makeknown(how);
669         (void) rloc(mtmp, TRUE);
670         return 2;
671     case MUSE_WAN_TELEPORTATION:
672         zap_oseen = oseen;
673         mzapmsg(mtmp, otmp, FALSE);
674         otmp->spe--;
675         m_using = TRUE;
676         mbhit(mtmp, rn1(8, 6), mbhitm, bhito, otmp);
677         /* monster learns that teleportation isn't useful here */
678         if (level.flags.noteleport)
679             mtmp->mtrapseen |= (1 << (TELEP_TRAP - 1));
680         m_using = FALSE;
681         return 2;
682     case MUSE_SCR_TELEPORTATION: {
683         int obj_is_cursed = otmp->cursed;
684
685         if (mtmp->isshk || mtmp->isgd || mtmp->ispriest)
686             return 2;
687         m_flee(mtmp);
688         mreadmsg(mtmp, otmp);
689         m_useup(mtmp, otmp); /* otmp might be free'ed */
690         how = SCR_TELEPORTATION;
691         if (obj_is_cursed || mtmp->mconf) {
692             int nlev;
693             d_level flev;
694
695             if (mon_has_amulet(mtmp) || In_endgame(&u.uz)) {
696                 if (vismon)
697                     pline("%s seems very disoriented for a moment.",
698                           Monnam(mtmp));
699                 return 2;
700             }
701             nlev = random_teleport_level();
702             if (nlev == depth(&u.uz)) {
703                 if (vismon)
704                     pline("%s shudders for a moment.", Monnam(mtmp));
705                 return 2;
706             }
707             get_level(&flev, nlev);
708             migrate_to_level(mtmp, ledger_no(&flev), MIGR_RANDOM,
709                              (coord *) 0);
710             if (oseen)
711                 makeknown(SCR_TELEPORTATION);
712         } else
713             goto mon_tele;
714         return 2;
715     }
716     case MUSE_WAN_DIGGING: {
717         struct trap *ttmp;
718
719         m_flee(mtmp);
720         mzapmsg(mtmp, otmp, FALSE);
721         otmp->spe--;
722         if (oseen)
723             makeknown(WAN_DIGGING);
724         if (IS_FURNITURE(levl[mtmp->mx][mtmp->my].typ)
725             || IS_DRAWBRIDGE(levl[mtmp->mx][mtmp->my].typ)
726             || (is_drawbridge_wall(mtmp->mx, mtmp->my) >= 0)
727             || (sstairs.sx && sstairs.sx == mtmp->mx
728                 && sstairs.sy == mtmp->my)) {
729             pline_The("digging ray is ineffective.");
730             return 2;
731         }
732         if (!Can_dig_down(&u.uz) && !levl[mtmp->mx][mtmp->my].candig) {
733             if (canseemon(mtmp))
734                 pline_The("%s here is too hard to dig in.",
735                           surface(mtmp->mx, mtmp->my));
736             return 2;
737         }
738         ttmp = maketrap(mtmp->mx, mtmp->my, HOLE);
739         if (!ttmp)
740             return 2;
741         seetrap(ttmp);
742         if (vis) {
743             pline("%s has made a hole in the %s.", Monnam(mtmp),
744                   surface(mtmp->mx, mtmp->my));
745             pline("%s %s through...", Monnam(mtmp),
746                   is_flyer(mtmp->data) ? "dives" : "falls");
747         } else if (!Deaf)
748             You_hear("%s crash through the %s.", something,
749                      surface(mtmp->mx, mtmp->my));
750         /* we made sure that there is a level for mtmp to go to */
751         migrate_to_level(mtmp, ledger_no(&u.uz) + 1, MIGR_RANDOM,
752                          (coord *) 0);
753         return 2;
754     }
755     case MUSE_WAN_CREATE_MONSTER: {
756         coord cc;
757         /* pm: 0 => random, eel => aquatic, croc => amphibious */
758         struct permonst *pm =
759             !is_pool(mtmp->mx, mtmp->my)
760                 ? 0
761                 : &mons[u.uinwater ? PM_GIANT_EEL : PM_CROCODILE];
762         struct monst *mon;
763
764         if (!enexto(&cc, mtmp->mx, mtmp->my, pm))
765             return 0;
766         mzapmsg(mtmp, otmp, FALSE);
767         otmp->spe--;
768         mon = makemon((struct permonst *) 0, cc.x, cc.y, NO_MM_FLAGS);
769         if (mon && canspotmon(mon) && oseen)
770             makeknown(WAN_CREATE_MONSTER);
771         return 2;
772     }
773     case MUSE_SCR_CREATE_MONSTER: {
774         coord cc;
775         struct permonst *pm = 0, *fish = 0;
776         int cnt = 1;
777         struct monst *mon;
778         boolean known = FALSE;
779
780         if (!rn2(73))
781             cnt += rnd(4);
782         if (mtmp->mconf || otmp->cursed)
783             cnt += 12;
784         if (mtmp->mconf)
785             pm = fish = &mons[PM_ACID_BLOB];
786         else if (is_pool(mtmp->mx, mtmp->my))
787             fish = &mons[u.uinwater ? PM_GIANT_EEL : PM_CROCODILE];
788         mreadmsg(mtmp, otmp);
789         while (cnt--) {
790             /* `fish' potentially gives bias towards water locations;
791                `pm' is what to actually create (0 => random) */
792             if (!enexto(&cc, mtmp->mx, mtmp->my, fish))
793                 break;
794             mon = makemon(pm, cc.x, cc.y, NO_MM_FLAGS);
795             if (mon && canspotmon(mon))
796                 known = TRUE;
797         }
798         /* The only case where we don't use oseen.  For wands, you
799          * have to be able to see the monster zap the wand to know
800          * what type it is.  For teleport scrolls, you have to see
801          * the monster to know it teleported.
802          */
803         if (known)
804             makeknown(SCR_CREATE_MONSTER);
805         else if (!objects[SCR_CREATE_MONSTER].oc_name_known
806                  && !objects[SCR_CREATE_MONSTER].oc_uname)
807             docall(otmp);
808         m_useup(mtmp, otmp);
809         return 2;
810     }
811     case MUSE_TRAPDOOR:
812         /* trap doors on "bottom" levels of dungeons are rock-drop
813          * trap doors, not holes in the floor.  We check here for
814          * safety.
815          */
816         if (Is_botlevel(&u.uz))
817             return 0;
818         m_flee(mtmp);
819         if (vis) {
820             struct trap *t = t_at(trapx, trapy);
821
822             pline("%s %s into a %s!", Monnam(mtmp),
823                   makeplural(locomotion(mtmp->data, "jump")),
824                   t->ttyp == TRAPDOOR ? "trap door" : "hole");
825             if (levl[trapx][trapy].typ == SCORR) {
826                 levl[trapx][trapy].typ = CORR;
827                 unblock_point(trapx, trapy);
828             }
829             seetrap(t_at(trapx, trapy));
830         }
831
832         /*  don't use rloc_to() because worm tails must "move" */
833         remove_monster(mtmp->mx, mtmp->my);
834         newsym(mtmp->mx, mtmp->my); /* update old location */
835         place_monster(mtmp, trapx, trapy);
836         if (mtmp->wormno)
837             worm_move(mtmp);
838         newsym(trapx, trapy);
839
840         migrate_to_level(mtmp, ledger_no(&u.uz) + 1, MIGR_RANDOM,
841                          (coord *) 0);
842         return 2;
843     case MUSE_UPSTAIRS:
844         /* Monsters without amulets escape the dungeon and are
845          * gone for good when they leave up the up stairs.
846          * Monsters with amulets would reach the endlevel,
847          * which we cannot allow since that would leave the
848          * player stranded.
849          */
850         if (ledger_no(&u.uz) == 1) {
851             if (mon_has_special(mtmp))
852                 return 0;
853             if (vismon)
854                 pline("%s escapes the dungeon!", Monnam(mtmp));
855             mongone(mtmp);
856             return 2;
857         }
858         m_flee(mtmp);
859         if (Inhell && mon_has_amulet(mtmp) && !rn2(4)
860             && (dunlev(&u.uz) < dunlevs_in_dungeon(&u.uz) - 3)) {
861             if (vismon)
862                 pline(
863     "As %s climbs the stairs, a mysterious force momentarily surrounds %s...",
864                       mon_nam(mtmp), mhim(mtmp));
865             /* simpler than for the player; this will usually be
866                the Wizard and he'll immediately go right to the
867                upstairs, so there's not much point in having any
868                chance for a random position on the current level */
869             migrate_to_level(mtmp, ledger_no(&u.uz) + 1, MIGR_RANDOM,
870                              (coord *) 0);
871         } else {
872             if (vismon)
873                 pline("%s escapes upstairs!", Monnam(mtmp));
874             migrate_to_level(mtmp, ledger_no(&u.uz) - 1, MIGR_STAIRS_DOWN,
875                              (coord *) 0);
876         }
877         return 2;
878     case MUSE_DOWNSTAIRS:
879         m_flee(mtmp);
880         if (vismon)
881             pline("%s escapes downstairs!", Monnam(mtmp));
882         migrate_to_level(mtmp, ledger_no(&u.uz) + 1, MIGR_STAIRS_UP,
883                          (coord *) 0);
884         return 2;
885     case MUSE_UP_LADDER:
886         m_flee(mtmp);
887         if (vismon)
888             pline("%s escapes up the ladder!", Monnam(mtmp));
889         migrate_to_level(mtmp, ledger_no(&u.uz) - 1, MIGR_LADDER_DOWN,
890                          (coord *) 0);
891         return 2;
892     case MUSE_DN_LADDER:
893         m_flee(mtmp);
894         if (vismon)
895             pline("%s escapes down the ladder!", Monnam(mtmp));
896         migrate_to_level(mtmp, ledger_no(&u.uz) + 1, MIGR_LADDER_UP,
897                          (coord *) 0);
898         return 2;
899     case MUSE_SSTAIRS:
900         m_flee(mtmp);
901         if (vismon)
902             pline("%s escapes %sstairs!", Monnam(mtmp),
903                   sstairs.up ? "up" : "down");
904         /* going from the Valley to Castle (Stronghold) has no sstairs
905            to target, but having sstairs.<sx,sy> == <0,0> will work the
906            same as specifying MIGR_RANDOM when mon_arrive() eventually
907            places the monster, so we can use MIGR_SSTAIRS unconditionally */
908         migrate_to_level(mtmp, ledger_no(&sstairs.tolev), MIGR_SSTAIRS,
909                          (coord *) 0);
910         return 2;
911     case MUSE_TELEPORT_TRAP:
912         m_flee(mtmp);
913         if (vis) {
914             pline("%s %s onto a teleport trap!", Monnam(mtmp),
915                   makeplural(locomotion(mtmp->data, "jump")));
916             seetrap(t_at(trapx, trapy));
917         }
918         /*  don't use rloc_to() because worm tails must "move" */
919         remove_monster(mtmp->mx, mtmp->my);
920         newsym(mtmp->mx, mtmp->my); /* update old location */
921         place_monster(mtmp, trapx, trapy);
922         if (mtmp->wormno)
923             worm_move(mtmp);
924         newsym(trapx, trapy);
925
926         goto mon_tele;
927     case MUSE_POT_HEALING:
928         mquaffmsg(mtmp, otmp);
929         i = d(6 + 2 * bcsign(otmp), 4);
930         mtmp->mhp += i;
931         if (mtmp->mhp > mtmp->mhpmax)
932             mtmp->mhp = ++mtmp->mhpmax;
933         if (!otmp->cursed && !mtmp->mcansee) {
934             mtmp->mcansee = 1;
935             mtmp->mblinded = 0;
936             if (vismon)
937                 pline(mcsa, Monnam(mtmp));
938         }
939         if (vismon)
940             pline("%s looks better.", Monnam(mtmp));
941         if (oseen)
942             makeknown(POT_HEALING);
943         m_useup(mtmp, otmp);
944         return 2;
945     case MUSE_POT_EXTRA_HEALING:
946         mquaffmsg(mtmp, otmp);
947         i = d(6 + 2 * bcsign(otmp), 8);
948         mtmp->mhp += i;
949         if (mtmp->mhp > mtmp->mhpmax)
950             mtmp->mhp = (mtmp->mhpmax += (otmp->blessed ? 5 : 2));
951         if (!mtmp->mcansee) {
952             mtmp->mcansee = 1;
953             mtmp->mblinded = 0;
954             if (vismon)
955                 pline(mcsa, Monnam(mtmp));
956         }
957         if (vismon)
958             pline("%s looks much better.", Monnam(mtmp));
959         if (oseen)
960             makeknown(POT_EXTRA_HEALING);
961         m_useup(mtmp, otmp);
962         return 2;
963     case MUSE_POT_FULL_HEALING:
964         mquaffmsg(mtmp, otmp);
965         if (otmp->otyp == POT_SICKNESS)
966             unbless(otmp); /* Pestilence */
967         mtmp->mhp = (mtmp->mhpmax += (otmp->blessed ? 8 : 4));
968         if (!mtmp->mcansee && otmp->otyp != POT_SICKNESS) {
969             mtmp->mcansee = 1;
970             mtmp->mblinded = 0;
971             if (vismon)
972                 pline(mcsa, Monnam(mtmp));
973         }
974         if (vismon)
975             pline("%s looks completely healed.", Monnam(mtmp));
976         if (oseen)
977             makeknown(otmp->otyp);
978         m_useup(mtmp, otmp);
979         return 2;
980     case MUSE_LIZARD_CORPSE:
981         /* not actually called for its unstoning effect */
982         mon_consume_unstone(mtmp, otmp, FALSE, FALSE);
983         return 2;
984     case 0:
985         return 0; /* i.e. an exploded wand */
986     default:
987         impossible("%s wanted to perform action %d?", Monnam(mtmp),
988                    m.has_defense);
989         break;
990     }
991     return 0;
992 #undef m_flee
993 }
994
995 int
996 rnd_defensive_item(mtmp)
997 struct monst *mtmp;
998 {
999     struct permonst *pm = mtmp->data;
1000     int difficulty = monstr[(monsndx(pm))];
1001     int trycnt = 0;
1002
1003     if (is_animal(pm) || attacktype(pm, AT_EXPL) || mindless(mtmp->data)
1004         || pm->mlet == S_GHOST || pm->mlet == S_KOP)
1005         return 0;
1006 try_again:
1007     switch (rn2(8 + (difficulty > 3) + (difficulty > 6) + (difficulty > 8))) {
1008     case 6:
1009     case 9:
1010         if (level.flags.noteleport && ++trycnt < 2)
1011             goto try_again;
1012         if (!rn2(3))
1013             return WAN_TELEPORTATION;
1014     /* else FALLTHRU */
1015     case 0:
1016     case 1:
1017         return SCR_TELEPORTATION;
1018     case 8:
1019     case 10:
1020         if (!rn2(3))
1021             return WAN_CREATE_MONSTER;
1022     /* else FALLTHRU */
1023     case 2:
1024         return SCR_CREATE_MONSTER;
1025     case 3:
1026         return POT_HEALING;
1027     case 4:
1028         return POT_EXTRA_HEALING;
1029     case 5:
1030         return (mtmp->data != &mons[PM_PESTILENCE]) ? POT_FULL_HEALING
1031                                                     : POT_SICKNESS;
1032     case 7:
1033         if (is_floater(pm) || mtmp->isshk || mtmp->isgd || mtmp->ispriest)
1034             return 0;
1035         else
1036             return WAN_DIGGING;
1037     }
1038     /*NOTREACHED*/
1039     return 0;
1040 }
1041
1042 #define MUSE_WAN_DEATH 1
1043 #define MUSE_WAN_SLEEP 2
1044 #define MUSE_WAN_FIRE 3
1045 #define MUSE_WAN_COLD 4
1046 #define MUSE_WAN_LIGHTNING 5
1047 #define MUSE_WAN_MAGIC_MISSILE 6
1048 #define MUSE_WAN_STRIKING 7
1049 #define MUSE_SCR_FIRE 8
1050 #define MUSE_POT_PARALYSIS 9
1051 #define MUSE_POT_BLINDNESS 10
1052 #define MUSE_POT_CONFUSION 11
1053 #define MUSE_FROST_HORN 12
1054 #define MUSE_FIRE_HORN 13
1055 #define MUSE_POT_ACID 14
1056 /*#define MUSE_WAN_TELEPORTATION 15*/
1057 #define MUSE_POT_SLEEPING 16
1058 #define MUSE_SCR_EARTH 17
1059
1060 /* Select an offensive item/action for a monster.  Returns TRUE iff one is
1061  * found.
1062  */
1063 boolean
1064 find_offensive(mtmp)
1065 struct monst *mtmp;
1066 {
1067     register struct obj *obj;
1068     boolean reflection_skip = (Reflecting && rn2(2));
1069     struct obj *helmet = which_armor(mtmp, W_ARMH);
1070
1071     m.offensive = (struct obj *) 0;
1072     m.has_offense = 0;
1073     if (mtmp->mpeaceful || is_animal(mtmp->data) || mindless(mtmp->data)
1074         || nohands(mtmp->data))
1075         return FALSE;
1076     if (u.uswallow)
1077         return FALSE;
1078     if (in_your_sanctuary(mtmp, 0, 0))
1079         return FALSE;
1080     if (dmgtype(mtmp->data, AD_HEAL)
1081         && !uwep && !uarmu && !uarm && !uarmh
1082         && !uarms && !uarmg && !uarmc && !uarmf)
1083         return FALSE;
1084     /* all offensive items require orthogonal or diagonal targetting */
1085     if (!lined_up(mtmp))
1086         return FALSE;
1087
1088 #define nomore(x)       if (m.has_offense == x) continue;
1089     /* this picks the last viable item rather than prioritizing choices */
1090     for (obj = mtmp->minvent; obj; obj = obj->nobj) {
1091         if (!reflection_skip) {
1092             nomore(MUSE_WAN_DEATH);
1093             if (obj->otyp == WAN_DEATH && obj->spe > 0) {
1094                 m.offensive = obj;
1095                 m.has_offense = MUSE_WAN_DEATH;
1096             }
1097             nomore(MUSE_WAN_SLEEP);
1098             if (obj->otyp == WAN_SLEEP && obj->spe > 0 && multi >= 0) {
1099                 m.offensive = obj;
1100                 m.has_offense = MUSE_WAN_SLEEP;
1101             }
1102             nomore(MUSE_WAN_FIRE);
1103             if (obj->otyp == WAN_FIRE && obj->spe > 0) {
1104                 m.offensive = obj;
1105                 m.has_offense = MUSE_WAN_FIRE;
1106             }
1107             nomore(MUSE_FIRE_HORN);
1108             if (obj->otyp == FIRE_HORN && obj->spe > 0) {
1109                 m.offensive = obj;
1110                 m.has_offense = MUSE_FIRE_HORN;
1111             }
1112             nomore(MUSE_WAN_COLD);
1113             if (obj->otyp == WAN_COLD && obj->spe > 0) {
1114                 m.offensive = obj;
1115                 m.has_offense = MUSE_WAN_COLD;
1116             }
1117             nomore(MUSE_FROST_HORN);
1118             if (obj->otyp == FROST_HORN && obj->spe > 0) {
1119                 m.offensive = obj;
1120                 m.has_offense = MUSE_FROST_HORN;
1121             }
1122             nomore(MUSE_WAN_LIGHTNING);
1123             if (obj->otyp == WAN_LIGHTNING && obj->spe > 0) {
1124                 m.offensive = obj;
1125                 m.has_offense = MUSE_WAN_LIGHTNING;
1126             }
1127             nomore(MUSE_WAN_MAGIC_MISSILE);
1128             if (obj->otyp == WAN_MAGIC_MISSILE && obj->spe > 0) {
1129                 m.offensive = obj;
1130                 m.has_offense = MUSE_WAN_MAGIC_MISSILE;
1131             }
1132         }
1133         nomore(MUSE_WAN_STRIKING);
1134         if (obj->otyp == WAN_STRIKING && obj->spe > 0) {
1135             m.offensive = obj;
1136             m.has_offense = MUSE_WAN_STRIKING;
1137         }
1138         nomore(MUSE_POT_PARALYSIS);
1139         if (obj->otyp == POT_PARALYSIS && multi >= 0) {
1140             m.offensive = obj;
1141             m.has_offense = MUSE_POT_PARALYSIS;
1142         }
1143         nomore(MUSE_POT_BLINDNESS);
1144         if (obj->otyp == POT_BLINDNESS && !attacktype(mtmp->data, AT_GAZE)) {
1145             m.offensive = obj;
1146             m.has_offense = MUSE_POT_BLINDNESS;
1147         }
1148         nomore(MUSE_POT_CONFUSION);
1149         if (obj->otyp == POT_CONFUSION) {
1150             m.offensive = obj;
1151             m.has_offense = MUSE_POT_CONFUSION;
1152         }
1153         nomore(MUSE_POT_SLEEPING);
1154         if (obj->otyp == POT_SLEEPING) {
1155             m.offensive = obj;
1156             m.has_offense = MUSE_POT_SLEEPING;
1157         }
1158         nomore(MUSE_POT_ACID);
1159         if (obj->otyp == POT_ACID) {
1160             m.offensive = obj;
1161             m.has_offense = MUSE_POT_ACID;
1162         }
1163         /* we can safely put this scroll here since the locations that
1164          * are in a 1 square radius are a subset of the locations that
1165          * are in wand or throwing range (in other words, always lined_up())
1166          */
1167         nomore(MUSE_SCR_EARTH);
1168         if (obj->otyp == SCR_EARTH
1169             && ((helmet && is_metallic(helmet)) || mtmp->mconf
1170                 || amorphous(mtmp->data) || passes_walls(mtmp->data)
1171                 || noncorporeal(mtmp->data) || unsolid(mtmp->data)
1172                 || !rn2(10))
1173             && dist2(mtmp->mx, mtmp->my, mtmp->mux, mtmp->muy) <= 2
1174             && mtmp->mcansee && haseyes(mtmp->data)
1175             && !Is_rogue_level(&u.uz)
1176             && (!In_endgame(&u.uz) || Is_earthlevel(&u.uz))) {
1177             m.offensive = obj;
1178             m.has_offense = MUSE_SCR_EARTH;
1179         }
1180 #if 0
1181         nomore(MUSE_SCR_FIRE);
1182         if (obj->otyp == SCR_FIRE && resists_fire(mtmp)
1183             && dist2(mtmp->mx, mtmp->my, mtmp->mux, mtmp->muy) <= 2
1184             && mtmp->mcansee && haseyes(mtmp->data)) {
1185             m.offensive = obj;
1186             m.has_offense = MUSE_SCR_FIRE;
1187         }
1188 #endif /* 0 */
1189     }
1190     return (boolean) !!m.has_offense;
1191 #undef nomore
1192 }
1193
1194 STATIC_PTR
1195 int
1196 mbhitm(mtmp, otmp)
1197 register struct monst *mtmp;
1198 register struct obj *otmp;
1199 {
1200     int tmp;
1201
1202     boolean reveal_invis = FALSE;
1203     if (mtmp != &youmonst) {
1204         mtmp->msleeping = 0;
1205         if (mtmp->m_ap_type)
1206             seemimic(mtmp);
1207     }
1208     switch (otmp->otyp) {
1209     case WAN_STRIKING:
1210         reveal_invis = TRUE;
1211         if (mtmp == &youmonst) {
1212             if (zap_oseen)
1213                 makeknown(WAN_STRIKING);
1214             if (Antimagic) {
1215                 shieldeff(u.ux, u.uy);
1216                 pline("Boing!");
1217             } else if (rnd(20) < 10 + u.uac) {
1218                 pline_The("wand hits you!");
1219                 tmp = d(2, 12);
1220                 if (Half_spell_damage)
1221                     tmp = (tmp + 1) / 2;
1222                 losehp(tmp, "wand", KILLED_BY_AN);
1223             } else
1224                 pline_The("wand misses you.");
1225             stop_occupation();
1226             nomul(0);
1227         } else if (resists_magm(mtmp)) {
1228             shieldeff(mtmp->mx, mtmp->my);
1229             pline("Boing!");
1230         } else if (rnd(20) < 10 + find_mac(mtmp)) {
1231             tmp = d(2, 12);
1232             hit("wand", mtmp, exclam(tmp));
1233             (void) resist(mtmp, otmp->oclass, tmp, TELL);
1234             if (cansee(mtmp->mx, mtmp->my) && zap_oseen)
1235                 makeknown(WAN_STRIKING);
1236         } else {
1237             miss("wand", mtmp);
1238             if (cansee(mtmp->mx, mtmp->my) && zap_oseen)
1239                 makeknown(WAN_STRIKING);
1240         }
1241         break;
1242     case WAN_TELEPORTATION:
1243         if (mtmp == &youmonst) {
1244             if (zap_oseen)
1245                 makeknown(WAN_TELEPORTATION);
1246             tele();
1247         } else {
1248             /* for consistency with zap.c, don't identify */
1249             if (mtmp->ispriest && *in_rooms(mtmp->mx, mtmp->my, TEMPLE)) {
1250                 if (cansee(mtmp->mx, mtmp->my))
1251                     pline("%s resists the magic!", Monnam(mtmp));
1252             } else if (!tele_restrict(mtmp))
1253                 (void) rloc(mtmp, TRUE);
1254         }
1255         break;
1256     case WAN_CANCELLATION:
1257     case SPE_CANCELLATION:
1258         (void) cancel_monst(mtmp, otmp, FALSE, TRUE, FALSE);
1259         break;
1260     }
1261     if (reveal_invis) {
1262         if (mtmp->mhp > 0 && cansee(bhitpos.x, bhitpos.y)
1263             && !canspotmon(mtmp))
1264             map_invisible(bhitpos.x, bhitpos.y);
1265     }
1266     return 0;
1267 }
1268
1269 /* A modified bhit() for monsters.  Based on bhit() in zap.c.  Unlike
1270  * buzz(), bhit() doesn't take into account the possibility of a monster
1271  * zapping you, so we need a special function for it.  (Unless someone wants
1272  * to merge the two functions...)
1273  */
1274 STATIC_OVL void
1275 mbhit(mon, range, fhitm, fhito, obj)
1276 struct monst *mon;  /* monster shooting the wand */
1277 register int range; /* direction and range */
1278 int FDECL((*fhitm), (MONST_P, OBJ_P));
1279 int FDECL((*fhito), (OBJ_P, OBJ_P)); /* fns called when mon/obj hit */
1280 struct obj *obj;                     /* 2nd arg to fhitm/fhito */
1281 {
1282     register struct monst *mtmp;
1283     register struct obj *otmp;
1284     register uchar typ;
1285     int ddx, ddy;
1286
1287     bhitpos.x = mon->mx;
1288     bhitpos.y = mon->my;
1289     ddx = sgn(mon->mux - mon->mx);
1290     ddy = sgn(mon->muy - mon->my);
1291
1292     while (range-- > 0) {
1293         int x, y;
1294
1295         bhitpos.x += ddx;
1296         bhitpos.y += ddy;
1297         x = bhitpos.x;
1298         y = bhitpos.y;
1299
1300         if (!isok(x, y)) {
1301             bhitpos.x -= ddx;
1302             bhitpos.y -= ddy;
1303             break;
1304         }
1305         if (find_drawbridge(&x, &y))
1306             switch (obj->otyp) {
1307             case WAN_STRIKING:
1308                 destroy_drawbridge(x, y);
1309             }
1310         if (bhitpos.x == u.ux && bhitpos.y == u.uy) {
1311             (*fhitm)(&youmonst, obj);
1312             range -= 3;
1313         } else if ((mtmp = m_at(bhitpos.x, bhitpos.y)) != 0) {
1314             if (cansee(bhitpos.x, bhitpos.y) && !canspotmon(mtmp))
1315                 map_invisible(bhitpos.x, bhitpos.y);
1316             (*fhitm)(mtmp, obj);
1317             range -= 3;
1318         }
1319         /* modified by GAN to hit all objects */
1320         if (fhito) {
1321             int hitanything = 0;
1322             register struct obj *next_obj;
1323
1324             for (otmp = level.objects[bhitpos.x][bhitpos.y]; otmp;
1325                  otmp = next_obj) {
1326                 /* Fix for polymorph bug, Tim Wright */
1327                 next_obj = otmp->nexthere;
1328                 hitanything += (*fhito)(otmp, obj);
1329             }
1330             if (hitanything)
1331                 range--;
1332         }
1333         typ = levl[bhitpos.x][bhitpos.y].typ;
1334         if (IS_DOOR(typ) || typ == SDOOR) {
1335             switch (obj->otyp) {
1336             /* note: monsters don't use opening or locking magic
1337                at present, but keep these as placeholders */
1338             case WAN_OPENING:
1339             case WAN_LOCKING:
1340             case WAN_STRIKING:
1341                 if (doorlock(obj, bhitpos.x, bhitpos.y)) {
1342                     if (zap_oseen)
1343                         makeknown(obj->otyp);
1344                     /* if a shop door gets broken, add it to
1345                        the shk's fix list (no cost to player) */
1346                     if (levl[bhitpos.x][bhitpos.y].doormask == D_BROKEN
1347                         && *in_rooms(bhitpos.x, bhitpos.y, SHOPBASE))
1348                         add_damage(bhitpos.x, bhitpos.y, 0L);
1349                 }
1350                 break;
1351             }
1352         }
1353         if (!ZAP_POS(typ)
1354             || (IS_DOOR(typ) && (levl[bhitpos.x][bhitpos.y].doormask
1355                                  & (D_LOCKED | D_CLOSED)))) {
1356             bhitpos.x -= ddx;
1357             bhitpos.y -= ddy;
1358             break;
1359         }
1360     }
1361 }
1362
1363 /* Perform an offensive action for a monster.  Must be called immediately
1364  * after find_offensive().  Return values are same as use_defensive().
1365  */
1366 int
1367 use_offensive(mtmp)
1368 struct monst *mtmp;
1369 {
1370     int i;
1371     struct obj *otmp = m.offensive;
1372     boolean oseen;
1373
1374     /* offensive potions are not drunk, they're thrown */
1375     if (otmp->oclass != POTION_CLASS && (i = precheck(mtmp, otmp)) != 0)
1376         return i;
1377     oseen = otmp && canseemon(mtmp);
1378
1379     switch (m.has_offense) {
1380     case MUSE_WAN_DEATH:
1381     case MUSE_WAN_SLEEP:
1382     case MUSE_WAN_FIRE:
1383     case MUSE_WAN_COLD:
1384     case MUSE_WAN_LIGHTNING:
1385     case MUSE_WAN_MAGIC_MISSILE:
1386         mzapmsg(mtmp, otmp, FALSE);
1387         otmp->spe--;
1388         if (oseen)
1389             makeknown(otmp->otyp);
1390         m_using = TRUE;
1391         buzz((int) (-30 - (otmp->otyp - WAN_MAGIC_MISSILE)),
1392              (otmp->otyp == WAN_MAGIC_MISSILE) ? 2 : 6, mtmp->mx, mtmp->my,
1393              sgn(mtmp->mux - mtmp->mx), sgn(mtmp->muy - mtmp->my));
1394         m_using = FALSE;
1395         return (mtmp->mhp <= 0) ? 1 : 2;
1396     case MUSE_FIRE_HORN:
1397     case MUSE_FROST_HORN:
1398         if (oseen) {
1399             makeknown(otmp->otyp);
1400             pline("%s plays a %s!", Monnam(mtmp), xname(otmp));
1401         } else
1402             You_hear("a horn being played.");
1403         otmp->spe--;
1404         m_using = TRUE;
1405         buzz(-30 - ((otmp->otyp == FROST_HORN) ? AD_COLD - 1 : AD_FIRE - 1),
1406              rn1(6, 6), mtmp->mx, mtmp->my, sgn(mtmp->mux - mtmp->mx),
1407              sgn(mtmp->muy - mtmp->my));
1408         m_using = FALSE;
1409         return (mtmp->mhp <= 0) ? 1 : 2;
1410     case MUSE_WAN_TELEPORTATION:
1411     case MUSE_WAN_STRIKING:
1412         zap_oseen = oseen;
1413         mzapmsg(mtmp, otmp, FALSE);
1414         otmp->spe--;
1415         m_using = TRUE;
1416         mbhit(mtmp, rn1(8, 6), mbhitm, bhito, otmp);
1417         m_using = FALSE;
1418         return 2;
1419     case MUSE_SCR_EARTH: {
1420         /* TODO: handle steeds */
1421         register int x, y;
1422         /* don't use monster fields after killing it */
1423         boolean confused = (mtmp->mconf ? TRUE : FALSE);
1424         int mmx = mtmp->mx, mmy = mtmp->my;
1425         boolean is_cursed = otmp->cursed;
1426
1427         mreadmsg(mtmp, otmp);
1428         /* Identify the scroll */
1429         if (canspotmon(mtmp)) {
1430             pline_The("%s rumbles %s %s!", ceiling(mtmp->mx, mtmp->my),
1431                       otmp->blessed ? "around" : "above", mon_nam(mtmp));
1432             if (oseen)
1433                 makeknown(otmp->otyp);
1434         } else if (cansee(mtmp->mx, mtmp->my)) {
1435             pline_The("%s rumbles in the middle of nowhere!",
1436                       ceiling(mtmp->mx, mtmp->my));
1437             if (mtmp->minvis)
1438                 map_invisible(mtmp->mx, mtmp->my);
1439             if (oseen)
1440                 makeknown(otmp->otyp);
1441         }
1442
1443         /* Loop through the surrounding squares */
1444         for (x = mmx - 1; x <= mmx + 1; x++) {
1445             for (y = mmy - 1; y <= mmy + 1; y++) {
1446                 /* Is this a suitable spot? */
1447                 if (isok(x, y) && !closed_door(x, y)
1448                     && !IS_ROCK(levl[x][y].typ) && !IS_AIR(levl[x][y].typ)
1449                     && (((x == mmx) && (y == mmy)) ? !otmp->blessed
1450                                                    : !otmp->cursed)
1451                     && (x != u.ux || y != u.uy)) {
1452                     (void) drop_boulder_on_monster(x, y, confused, FALSE);
1453                 }
1454             }
1455         }
1456         m_useup(mtmp, otmp);
1457         /* Attack the player */
1458         if (distmin(mmx, mmy, u.ux, u.uy) == 1 && !is_cursed) {
1459             drop_boulder_on_player(confused, !is_cursed, FALSE, TRUE);
1460         }
1461
1462         return (mtmp->mhp <= 0) ? 1 : 2;
1463     }
1464 #if 0
1465     case MUSE_SCR_FIRE: {
1466         boolean vis = cansee(mtmp->mx, mtmp->my);
1467
1468         mreadmsg(mtmp, otmp);
1469         if (mtmp->mconf) {
1470             if (vis)
1471                 pline("Oh, what a pretty fire!");
1472         } else {
1473             struct monst *mtmp2;
1474             int num;
1475
1476             if (vis)
1477                 pline_The("scroll erupts in a tower of flame!");
1478             shieldeff(mtmp->mx, mtmp->my);
1479             pline("%s is uninjured.", Monnam(mtmp));
1480             (void) destroy_mitem(mtmp, SCROLL_CLASS, AD_FIRE);
1481             (void) destroy_mitem(mtmp, SPBOOK_CLASS, AD_FIRE);
1482             (void) destroy_mitem(mtmp, POTION_CLASS, AD_FIRE);
1483             num = (2 * (rn1(3, 3) + 2 * bcsign(otmp)) + 1) / 3;
1484             if (Fire_resistance)
1485                 You("are not harmed.");
1486             burn_away_slime();
1487             if (Half_spell_damage)
1488                 num = (num + 1) / 2;
1489             else
1490                 losehp(num, "scroll of fire", KILLED_BY_AN);
1491             for (mtmp2 = fmon; mtmp2; mtmp2 = mtmp2->nmon) {
1492                 if (DEADMONSTER(mtmp2)) continue;
1493                 if (mtmp == mtmp2) continue;
1494                 if (dist2(mtmp2->mx, mtmp2->my, mtmp->mx, mtmp->my) < 3) {
1495                     if (resists_fire(mtmp2)) continue;
1496                     mtmp2->mhp -= num;
1497                     if (resists_cold(mtmp2))
1498                         mtmp2->mhp -= 3 * num;
1499                     if (mtmp2->mhp < 1) {
1500                         mondied(mtmp2);
1501                         break;
1502                     }
1503                 }
1504             }
1505         }
1506         return 2;
1507     }
1508 #endif /* 0 */
1509     case MUSE_POT_PARALYSIS:
1510     case MUSE_POT_BLINDNESS:
1511     case MUSE_POT_CONFUSION:
1512     case MUSE_POT_SLEEPING:
1513     case MUSE_POT_ACID:
1514         /* Note: this setting of dknown doesn't suffice.  A monster
1515          * which is out of sight might throw and it hits something _in_
1516          * sight, a problem not existing with wands because wand rays
1517          * are not objects.  Also set dknown in mthrowu.c.
1518          */
1519         if (cansee(mtmp->mx, mtmp->my)) {
1520             otmp->dknown = 1;
1521             pline("%s hurls %s!", Monnam(mtmp), singular(otmp, doname));
1522         }
1523         m_throw(mtmp, mtmp->mx, mtmp->my, sgn(mtmp->mux - mtmp->mx),
1524                 sgn(mtmp->muy - mtmp->my),
1525                 distmin(mtmp->mx, mtmp->my, mtmp->mux, mtmp->muy), otmp);
1526         return 2;
1527     case 0:
1528         return 0; /* i.e. an exploded wand */
1529     default:
1530         impossible("%s wanted to perform action %d?", Monnam(mtmp),
1531                    m.has_offense);
1532         break;
1533     }
1534     return 0;
1535 }
1536
1537 int
1538 rnd_offensive_item(mtmp)
1539 struct monst *mtmp;
1540 {
1541     struct permonst *pm = mtmp->data;
1542     int difficulty = monstr[(monsndx(pm))];
1543
1544     if (is_animal(pm) || attacktype(pm, AT_EXPL) || mindless(mtmp->data)
1545         || pm->mlet == S_GHOST || pm->mlet == S_KOP)
1546         return 0;
1547     if (difficulty > 7 && !rn2(35))
1548         return WAN_DEATH;
1549     switch (rn2(9 - (difficulty < 4) + 4 * (difficulty > 6))) {
1550     case 0: {
1551         struct obj *helmet = which_armor(mtmp, W_ARMH);
1552
1553         if ((helmet && is_metallic(helmet)) || amorphous(pm)
1554             || passes_walls(pm) || noncorporeal(pm) || unsolid(pm))
1555             return SCR_EARTH;
1556     } /* fall through */
1557     case 1:
1558         return WAN_STRIKING;
1559     case 2:
1560         return POT_ACID;
1561     case 3:
1562         return POT_CONFUSION;
1563     case 4:
1564         return POT_BLINDNESS;
1565     case 5:
1566         return POT_SLEEPING;
1567     case 6:
1568         return POT_PARALYSIS;
1569     case 7:
1570     case 8:
1571         return WAN_MAGIC_MISSILE;
1572     case 9:
1573         return WAN_SLEEP;
1574     case 10:
1575         return WAN_FIRE;
1576     case 11:
1577         return WAN_COLD;
1578     case 12:
1579         return WAN_LIGHTNING;
1580     }
1581     /*NOTREACHED*/
1582     return 0;
1583 }
1584
1585 #define MUSE_POT_GAIN_LEVEL 1
1586 #define MUSE_WAN_MAKE_INVISIBLE 2
1587 #define MUSE_POT_INVISIBILITY 3
1588 #define MUSE_POLY_TRAP 4
1589 #define MUSE_WAN_POLYMORPH 5
1590 #define MUSE_POT_SPEED 6
1591 #define MUSE_WAN_SPEED_MONSTER 7
1592 #define MUSE_BULLWHIP 8
1593 #define MUSE_POT_POLYMORPH 9
1594
1595 boolean
1596 find_misc(mtmp)
1597 struct monst *mtmp;
1598 {
1599     register struct obj *obj;
1600     struct permonst *mdat = mtmp->data;
1601     int x = mtmp->mx, y = mtmp->my;
1602     struct trap *t;
1603     int xx, yy, pmidx = NON_PM;
1604     boolean immobile = (mdat->mmove == 0);
1605     boolean stuck = (mtmp == u.ustuck);
1606
1607     m.misc = (struct obj *) 0;
1608     m.has_misc = 0;
1609     if (is_animal(mdat) || mindless(mdat))
1610         return 0;
1611     if (u.uswallow && stuck)
1612         return FALSE;
1613
1614     /* We arbitrarily limit to times when a player is nearby for the
1615      * same reason as Junior Pac-Man doesn't have energizers eaten until
1616      * you can see them...
1617      */
1618     if (dist2(x, y, mtmp->mux, mtmp->muy) > 36)
1619         return FALSE;
1620
1621     if (!stuck && !immobile && (mtmp->cham == NON_PM)
1622         && monstr[(pmidx = monsndx(mdat))] < 6) {
1623         boolean ignore_boulders = (verysmall(mdat) || throws_rocks(mdat)
1624                                    || passes_walls(mdat)),
1625             diag_ok = !NODIAG(pmidx);
1626
1627         for (xx = x - 1; xx <= x + 1; xx++)
1628             for (yy = y - 1; yy <= y + 1; yy++)
1629                 if (isok(xx, yy) && (xx != u.ux || yy != u.uy)
1630                     && (diag_ok || xx == x || yy == y)
1631                     && ((xx == x && yy == y) || !level.monsters[xx][yy]))
1632                     if ((t = t_at(xx, yy)) != 0
1633                         && (ignore_boulders || !sobj_at(BOULDER, xx, yy))
1634                         && !onscary(xx, yy, mtmp)) {
1635                         /* use trap if it's the correct type */
1636                         if (t->ttyp == POLY_TRAP) {
1637                             trapx = xx;
1638                             trapy = yy;
1639                             m.has_misc = MUSE_POLY_TRAP;
1640                             return TRUE;
1641                         }
1642                     }
1643     }
1644     if (nohands(mdat))
1645         return 0;
1646
1647 #define nomore(x)       if (m.has_misc == x) continue
1648     /*
1649      * [bug?]  Choice of item is not prioritized; the last viable one
1650      * in the monster's inventory will be chosen.
1651      * 'nomore()' is nearly worthless because it only screens checking
1652      * of duplicates when there is no alternate type in between them.
1653      */
1654     for (obj = mtmp->minvent; obj; obj = obj->nobj) {
1655         /* Monsters shouldn't recognize cursed items; this kludge is
1656            necessary to prevent serious problems though... */
1657         if (obj->otyp == POT_GAIN_LEVEL
1658             && (!obj->cursed
1659                 || (!mtmp->isgd && !mtmp->isshk && !mtmp->ispriest))) {
1660             m.misc = obj;
1661             m.has_misc = MUSE_POT_GAIN_LEVEL;
1662         }
1663         nomore(MUSE_BULLWHIP);
1664         if (obj->otyp == BULLWHIP && !mtmp->mpeaceful
1665             /* the random test prevents whip-wielding
1666                monster from attempting disarm every turn */
1667             && uwep && !rn2(5) && obj == MON_WEP(mtmp)
1668             /* hero's location must be known and adjacent */
1669             && mtmp->mux == u.ux && mtmp->muy == u.uy
1670             && distu(mtmp->mx, mtmp->my) <= 2
1671             /* don't bother if it can't work (this doesn't
1672                prevent cursed weapons from being targetted) */
1673             && (canletgo(uwep, "")
1674                 || (u.twoweap && canletgo(uswapwep, "")))) {
1675             m.misc = obj;
1676             m.has_misc = MUSE_BULLWHIP;
1677         }
1678         /* Note: peaceful/tame monsters won't make themselves
1679          * invisible unless you can see them.  Not really right, but...
1680          */
1681         nomore(MUSE_WAN_MAKE_INVISIBLE);
1682         if (obj->otyp == WAN_MAKE_INVISIBLE && obj->spe > 0 && !mtmp->minvis
1683             && !mtmp->invis_blkd && (!mtmp->mpeaceful || See_invisible)
1684             && (!attacktype(mtmp->data, AT_GAZE) || mtmp->mcan)) {
1685             m.misc = obj;
1686             m.has_misc = MUSE_WAN_MAKE_INVISIBLE;
1687         }
1688         nomore(MUSE_POT_INVISIBILITY);
1689         if (obj->otyp == POT_INVISIBILITY && !mtmp->minvis
1690             && !mtmp->invis_blkd && (!mtmp->mpeaceful || See_invisible)
1691             && (!attacktype(mtmp->data, AT_GAZE) || mtmp->mcan)) {
1692             m.misc = obj;
1693             m.has_misc = MUSE_POT_INVISIBILITY;
1694         }
1695         nomore(MUSE_WAN_SPEED_MONSTER);
1696         if (obj->otyp == WAN_SPEED_MONSTER && obj->spe > 0
1697             && mtmp->mspeed != MFAST && !mtmp->isgd) {
1698             m.misc = obj;
1699             m.has_misc = MUSE_WAN_SPEED_MONSTER;
1700         }
1701         nomore(MUSE_POT_SPEED);
1702         if (obj->otyp == POT_SPEED && mtmp->mspeed != MFAST && !mtmp->isgd) {
1703             m.misc = obj;
1704             m.has_misc = MUSE_POT_SPEED;
1705         }
1706         nomore(MUSE_WAN_POLYMORPH);
1707         if (obj->otyp == WAN_POLYMORPH && obj->spe > 0
1708             && (mtmp->cham == NON_PM) && monstr[monsndx(mdat)] < 6) {
1709             m.misc = obj;
1710             m.has_misc = MUSE_WAN_POLYMORPH;
1711         }
1712         nomore(MUSE_POT_POLYMORPH);
1713         if (obj->otyp == POT_POLYMORPH && (mtmp->cham == NON_PM)
1714             && monstr[monsndx(mdat)] < 6) {
1715             m.misc = obj;
1716             m.has_misc = MUSE_POT_POLYMORPH;
1717         }
1718     }
1719     return (boolean) !!m.has_misc;
1720 #undef nomore
1721 }
1722
1723 /* type of monster to polymorph into; defaults to one suitable for the
1724    current level rather than the totally arbitrary choice of newcham() */
1725 static struct permonst *
1726 muse_newcham_mon(mon)
1727 struct monst *mon;
1728 {
1729     struct obj *m_armr;
1730
1731     if ((m_armr = which_armor(mon, W_ARM)) != 0) {
1732         if (Is_dragon_scales(m_armr))
1733             return Dragon_scales_to_pm(m_armr);
1734         else if (Is_dragon_mail(m_armr))
1735             return Dragon_mail_to_pm(m_armr);
1736     }
1737     return rndmonst();
1738 }
1739
1740 int
1741 use_misc(mtmp)
1742 struct monst *mtmp;
1743 {
1744     int i;
1745     struct obj *otmp = m.misc;
1746     boolean vis, vismon, oseen;
1747     char nambuf[BUFSZ];
1748
1749     if ((i = precheck(mtmp, otmp)) != 0)
1750         return i;
1751     vis = cansee(mtmp->mx, mtmp->my);
1752     vismon = canseemon(mtmp);
1753     oseen = otmp && vismon;
1754
1755     switch (m.has_misc) {
1756     case MUSE_POT_GAIN_LEVEL:
1757         mquaffmsg(mtmp, otmp);
1758         if (otmp->cursed) {
1759             if (Can_rise_up(mtmp->mx, mtmp->my, &u.uz)) {
1760                 register int tolev = depth(&u.uz) - 1;
1761                 d_level tolevel;
1762
1763                 get_level(&tolevel, tolev);
1764                 /* insurance against future changes... */
1765                 if (on_level(&tolevel, &u.uz))
1766                     goto skipmsg;
1767                 if (vismon) {
1768                     pline("%s rises up, through the %s!", Monnam(mtmp),
1769                           ceiling(mtmp->mx, mtmp->my));
1770                     if (!objects[POT_GAIN_LEVEL].oc_name_known
1771                         && !objects[POT_GAIN_LEVEL].oc_uname)
1772                         docall(otmp);
1773                 }
1774                 m_useup(mtmp, otmp);
1775                 migrate_to_level(mtmp, ledger_no(&tolevel), MIGR_RANDOM,
1776                                  (coord *) 0);
1777                 return 2;
1778             } else {
1779             skipmsg:
1780                 if (vismon) {
1781                     pline("%s looks uneasy.", Monnam(mtmp));
1782                     if (!objects[POT_GAIN_LEVEL].oc_name_known
1783                         && !objects[POT_GAIN_LEVEL].oc_uname)
1784                         docall(otmp);
1785                 }
1786                 m_useup(mtmp, otmp);
1787                 return 2;
1788             }
1789         }
1790         if (vismon)
1791             pline("%s seems more experienced.", Monnam(mtmp));
1792         if (oseen)
1793             makeknown(POT_GAIN_LEVEL);
1794         m_useup(mtmp, otmp);
1795         if (!grow_up(mtmp, (struct monst *) 0))
1796             return 1;
1797         /* grew into genocided monster */
1798         return 2;
1799     case MUSE_WAN_MAKE_INVISIBLE:
1800     case MUSE_POT_INVISIBILITY:
1801         if (otmp->otyp == WAN_MAKE_INVISIBLE) {
1802             mzapmsg(mtmp, otmp, TRUE);
1803             otmp->spe--;
1804         } else
1805             mquaffmsg(mtmp, otmp);
1806         /* format monster's name before altering its visibility */
1807         Strcpy(nambuf, mon_nam(mtmp));
1808         mon_set_minvis(mtmp);
1809         if (vismon && mtmp->minvis) { /* was seen, now invisible */
1810             if (canspotmon(mtmp))
1811                 pline("%s body takes on a %s transparency.",
1812                       upstart(s_suffix(nambuf)),
1813                       Hallucination ? "normal" : "strange");
1814             else
1815                 pline("Suddenly you cannot see %s.", nambuf);
1816             if (oseen)
1817                 makeknown(otmp->otyp);
1818         }
1819         if (otmp->otyp == POT_INVISIBILITY) {
1820             if (otmp->cursed)
1821                 you_aggravate(mtmp);
1822             m_useup(mtmp, otmp);
1823         }
1824         return 2;
1825     case MUSE_WAN_SPEED_MONSTER:
1826         mzapmsg(mtmp, otmp, TRUE);
1827         otmp->spe--;
1828         mon_adjust_speed(mtmp, 1, otmp);
1829         return 2;
1830     case MUSE_POT_SPEED:
1831         mquaffmsg(mtmp, otmp);
1832         /* note difference in potion effect due to substantially
1833            different methods of maintaining speed ratings:
1834            player's character becomes "very fast" temporarily;
1835            monster becomes "one stage faster" permanently */
1836         mon_adjust_speed(mtmp, 1, otmp);
1837         m_useup(mtmp, otmp);
1838         return 2;
1839     case MUSE_WAN_POLYMORPH:
1840         mzapmsg(mtmp, otmp, TRUE);
1841         otmp->spe--;
1842         (void) newcham(mtmp, muse_newcham_mon(mtmp), TRUE, FALSE);
1843         if (oseen)
1844             makeknown(WAN_POLYMORPH);
1845         return 2;
1846     case MUSE_POT_POLYMORPH:
1847         mquaffmsg(mtmp, otmp);
1848         if (vismon)
1849             pline("%s suddenly mutates!", Monnam(mtmp));
1850         (void) newcham(mtmp, muse_newcham_mon(mtmp), FALSE, FALSE);
1851         if (oseen)
1852             makeknown(POT_POLYMORPH);
1853         m_useup(mtmp, otmp);
1854         return 2;
1855     case MUSE_POLY_TRAP:
1856         if (vismon)
1857             pline("%s deliberately %s onto a polymorph trap!", Monnam(mtmp),
1858                   makeplural(locomotion(mtmp->data, "jump")));
1859         if (vis)
1860             seetrap(t_at(trapx, trapy));
1861
1862         /*  don't use rloc() due to worms */
1863         remove_monster(mtmp->mx, mtmp->my);
1864         newsym(mtmp->mx, mtmp->my);
1865         place_monster(mtmp, trapx, trapy);
1866         if (mtmp->wormno)
1867             worm_move(mtmp);
1868         newsym(trapx, trapy);
1869
1870         (void) newcham(mtmp, (struct permonst *) 0, FALSE, FALSE);
1871         return 2;
1872     case MUSE_BULLWHIP:
1873         /* attempt to disarm hero */
1874         {
1875             const char *The_whip = vismon ? "The bullwhip" : "A whip";
1876             int where_to = rn2(4);
1877             struct obj *obj = uwep;
1878             const char *hand;
1879             char the_weapon[BUFSZ];
1880
1881             if (!obj || !canletgo(obj, "")
1882                 || (u.twoweap && canletgo(uswapwep, "") && rn2(2)))
1883                 obj = uswapwep;
1884             if (!obj)
1885                 break; /* shouldn't happen after find_misc() */
1886
1887             Strcpy(the_weapon, the(xname(obj)));
1888             hand = body_part(HAND);
1889             if (bimanual(obj))
1890                 hand = makeplural(hand);
1891
1892             if (vismon)
1893                 pline("%s flicks a bullwhip towards your %s!", Monnam(mtmp),
1894                       hand);
1895             if (obj->otyp == HEAVY_IRON_BALL) {
1896                 pline("%s fails to wrap around %s.", The_whip, the_weapon);
1897                 return 1;
1898             }
1899             pline("%s wraps around %s you're wielding!", The_whip,
1900                   the_weapon);
1901             if (welded(obj)) {
1902                 pline("%s welded to your %s%c",
1903                       !is_plural(obj) ? "It is" : "They are", hand,
1904                       !obj->bknown ? '!' : '.');
1905                 /* obj->bknown = 1; */ /* welded() takes care of this */
1906                 where_to = 0;
1907             }
1908             if (!where_to) {
1909                 pline_The("whip slips free."); /* not `The_whip' */
1910                 return 1;
1911             } else if (where_to == 3 && mon_hates_silver(mtmp)
1912                        && objects[obj->otyp].oc_material == SILVER) {
1913                 /* this monster won't want to catch a silver
1914                    weapon; drop it at hero's feet instead */
1915                 where_to = 2;
1916             }
1917             remove_worn_item(obj, FALSE);
1918             freeinv(obj);
1919             switch (where_to) {
1920             case 1: /* onto floor beneath mon */
1921                 pline("%s yanks %s from your %s!", Monnam(mtmp), the_weapon,
1922                       hand);
1923                 place_object(obj, mtmp->mx, mtmp->my);
1924                 break;
1925             case 2: /* onto floor beneath you */
1926                 pline("%s yanks %s to the %s!", Monnam(mtmp), the_weapon,
1927                       surface(u.ux, u.uy));
1928                 dropy(obj);
1929                 break;
1930             case 3: /* into mon's inventory */
1931                 pline("%s snatches %s!", Monnam(mtmp), the_weapon);
1932                 (void) mpickobj(mtmp, obj);
1933                 break;
1934             }
1935             return 1;
1936         }
1937         return 0;
1938     case 0:
1939         return 0; /* i.e. an exploded wand */
1940     default:
1941         impossible("%s wanted to perform action %d?", Monnam(mtmp),
1942                    m.has_misc);
1943         break;
1944     }
1945     return 0;
1946 }
1947
1948 STATIC_OVL void
1949 you_aggravate(mtmp)
1950 struct monst *mtmp;
1951 {
1952     pline("For some reason, %s presence is known to you.",
1953           s_suffix(noit_mon_nam(mtmp)));
1954     cls();
1955 #ifdef CLIPPING
1956     cliparound(mtmp->mx, mtmp->my);
1957 #endif
1958     show_glyph(mtmp->mx, mtmp->my, mon_to_glyph(mtmp));
1959     display_self();
1960     You_feel("aggravated at %s.", noit_mon_nam(mtmp));
1961     display_nhwindow(WIN_MAP, TRUE);
1962     docrt();
1963     if (unconscious()) {
1964         multi = -1;
1965         nomovemsg = "Aggravated, you are jolted into full consciousness.";
1966     }
1967     newsym(mtmp->mx, mtmp->my);
1968     if (!canspotmon(mtmp))
1969         map_invisible(mtmp->mx, mtmp->my);
1970 }
1971
1972 int
1973 rnd_misc_item(mtmp)
1974 struct monst *mtmp;
1975 {
1976     struct permonst *pm = mtmp->data;
1977     int difficulty = monstr[(monsndx(pm))];
1978
1979     if (is_animal(pm) || attacktype(pm, AT_EXPL) || mindless(mtmp->data)
1980         || pm->mlet == S_GHOST || pm->mlet == S_KOP)
1981         return 0;
1982     /* Unlike other rnd_item functions, we only allow _weak_ monsters
1983      * to have this item; after all, the item will be used to strengthen
1984      * the monster and strong monsters won't use it at all...
1985      */
1986     if (difficulty < 6 && !rn2(30))
1987         return rn2(6) ? POT_POLYMORPH : WAN_POLYMORPH;
1988
1989     if (!rn2(40) && !nonliving(pm) && !is_vampshifter(mtmp))
1990         return AMULET_OF_LIFE_SAVING;
1991
1992     switch (rn2(3)) {
1993     case 0:
1994         if (mtmp->isgd)
1995             return 0;
1996         return rn2(6) ? POT_SPEED : WAN_SPEED_MONSTER;
1997     case 1:
1998         if (mtmp->mpeaceful && !See_invisible)
1999             return 0;
2000         return rn2(6) ? POT_INVISIBILITY : WAN_MAKE_INVISIBLE;
2001     case 2:
2002         return POT_GAIN_LEVEL;
2003     }
2004     /*NOTREACHED*/
2005     return 0;
2006 }
2007
2008 boolean
2009 searches_for_item(mon, obj)
2010 struct monst *mon;
2011 struct obj *obj;
2012 {
2013     int typ = obj->otyp;
2014
2015     if (is_animal(mon->data) || mindless(mon->data)
2016         || mon->data == &mons[PM_GHOST]) /* don't loot bones piles */
2017         return FALSE;
2018
2019     if (typ == WAN_MAKE_INVISIBLE || typ == POT_INVISIBILITY)
2020         return (boolean) (!mon->minvis && !mon->invis_blkd
2021                           && !attacktype(mon->data, AT_GAZE));
2022     if (typ == WAN_SPEED_MONSTER || typ == POT_SPEED)
2023         return (boolean) (mon->mspeed != MFAST);
2024
2025     switch (obj->oclass) {
2026     case WAND_CLASS:
2027         if (obj->spe <= 0)
2028             return FALSE;
2029         if (typ == WAN_DIGGING)
2030             return (boolean) !is_floater(mon->data);
2031         if (typ == WAN_POLYMORPH)
2032             return (boolean) (monstr[monsndx(mon->data)] < 6);
2033         if (objects[typ].oc_dir == RAY || typ == WAN_STRIKING
2034             || typ == WAN_TELEPORTATION || typ == WAN_CREATE_MONSTER)
2035             return TRUE;
2036         break;
2037     case POTION_CLASS:
2038         if (typ == POT_HEALING || typ == POT_EXTRA_HEALING
2039             || typ == POT_FULL_HEALING || typ == POT_POLYMORPH
2040             || typ == POT_GAIN_LEVEL || typ == POT_PARALYSIS
2041             || typ == POT_SLEEPING || typ == POT_ACID || typ == POT_CONFUSION)
2042             return TRUE;
2043         if (typ == POT_BLINDNESS && !attacktype(mon->data, AT_GAZE))
2044             return TRUE;
2045         break;
2046     case SCROLL_CLASS:
2047         if (typ == SCR_TELEPORTATION || typ == SCR_CREATE_MONSTER
2048             || typ == SCR_EARTH || typ == SCR_FIRE)
2049             return TRUE;
2050         break;
2051     case AMULET_CLASS:
2052         if (typ == AMULET_OF_LIFE_SAVING)
2053             return (boolean) !(nonliving(mon->data) || is_vampshifter(mon));
2054         if (typ == AMULET_OF_REFLECTION)
2055             return TRUE;
2056         break;
2057     case TOOL_CLASS:
2058         if (typ == PICK_AXE)
2059             return (boolean) needspick(mon->data);
2060         if (typ == UNICORN_HORN)
2061             return (boolean) (!obj->cursed && !is_unicorn(mon->data));
2062         if (typ == FROST_HORN || typ == FIRE_HORN)
2063             return (obj->spe > 0);
2064         break;
2065     case FOOD_CLASS:
2066         if (typ == CORPSE)
2067             return (boolean) (((mon->misc_worn_check & W_ARMG) != 0L
2068                                && touch_petrifies(&mons[obj->corpsenm]))
2069                               || (!resists_ston(mon)
2070                                   && cures_stoning(mon, obj, FALSE)));
2071         if (typ == TIN)
2072             return (boolean) (mcould_eat_tin(mon)
2073                               && (!resists_ston(mon)
2074                                   && cures_stoning(mon, obj, TRUE)));
2075         if (typ == EGG)
2076             return (boolean) touch_petrifies(&mons[obj->corpsenm]);
2077         break;
2078     default:
2079         break;
2080     }
2081
2082     return FALSE;
2083 }
2084
2085 boolean
2086 mon_reflects(mon, str)
2087 struct monst *mon;
2088 const char *str;
2089 {
2090     struct obj *orefl = which_armor(mon, W_ARMS);
2091
2092     if (orefl && orefl->otyp == SHIELD_OF_REFLECTION) {
2093         if (str) {
2094             pline(str, s_suffix(mon_nam(mon)), "shield");
2095             makeknown(SHIELD_OF_REFLECTION);
2096         }
2097         return TRUE;
2098     } else if (arti_reflects(MON_WEP(mon))) {
2099         /* due to wielded artifact weapon */
2100         if (str)
2101             pline(str, s_suffix(mon_nam(mon)), "weapon");
2102         return TRUE;
2103     } else if ((orefl = which_armor(mon, W_AMUL))
2104                && orefl->otyp == AMULET_OF_REFLECTION) {
2105         if (str) {
2106             pline(str, s_suffix(mon_nam(mon)), "amulet");
2107             makeknown(AMULET_OF_REFLECTION);
2108         }
2109         return TRUE;
2110     } else if ((orefl = which_armor(mon, W_ARM))
2111                && (orefl->otyp == SILVER_DRAGON_SCALES
2112                    || orefl->otyp == SILVER_DRAGON_SCALE_MAIL)) {
2113         if (str)
2114             pline(str, s_suffix(mon_nam(mon)), "armor");
2115         return TRUE;
2116     } else if (mon->data == &mons[PM_SILVER_DRAGON]
2117                || mon->data == &mons[PM_CHROMATIC_DRAGON]) {
2118         /* Silver dragons only reflect when mature; babies do not */
2119         if (str)
2120             pline(str, s_suffix(mon_nam(mon)), "scales");
2121         return TRUE;
2122     }
2123     return FALSE;
2124 }
2125
2126 boolean
2127 ureflects(fmt, str)
2128 const char *fmt, *str;
2129 {
2130     /* Check from outermost to innermost objects */
2131     if (EReflecting & W_ARMS) {
2132         if (fmt && str) {
2133             pline(fmt, str, "shield");
2134             makeknown(SHIELD_OF_REFLECTION);
2135         }
2136         return TRUE;
2137     } else if (EReflecting & W_WEP) {
2138         /* Due to wielded artifact weapon */
2139         if (fmt && str)
2140             pline(fmt, str, "weapon");
2141         return TRUE;
2142     } else if (EReflecting & W_AMUL) {
2143         if (fmt && str) {
2144             pline(fmt, str, "medallion");
2145             makeknown(AMULET_OF_REFLECTION);
2146         }
2147         return TRUE;
2148     } else if (EReflecting & W_ARM) {
2149         if (fmt && str)
2150             pline(fmt, str, uskin ? "luster" : "armor");
2151         return TRUE;
2152     } else if (youmonst.data == &mons[PM_SILVER_DRAGON]) {
2153         if (fmt && str)
2154             pline(fmt, str, "scales");
2155         return TRUE;
2156     }
2157     return FALSE;
2158 }
2159
2160 /* TRUE if the monster ate something */
2161 boolean
2162 munstone(mon, by_you)
2163 struct monst *mon;
2164 boolean by_you;
2165 {
2166     struct obj *obj;
2167     boolean tinok;
2168
2169     if (resists_ston(mon))
2170         return FALSE;
2171     if (mon->meating || !mon->mcanmove || mon->msleeping)
2172         return FALSE;
2173     mon->mstrategy &= ~STRAT_WAITFORU;
2174
2175     tinok = mcould_eat_tin(mon);
2176     for (obj = mon->minvent; obj; obj = obj->nobj) {
2177         if (cures_stoning(mon, obj, tinok)) {
2178             mon_consume_unstone(mon, obj, by_you, TRUE);
2179             return TRUE;
2180         }
2181     }
2182     return FALSE;
2183 }
2184
2185 STATIC_OVL void
2186 mon_consume_unstone(mon, obj, by_you, stoning)
2187 struct monst *mon;
2188 struct obj *obj;
2189 boolean by_you;
2190 boolean stoning;
2191 {
2192     boolean vis = canseemon(mon), tinned = obj->otyp == TIN,
2193             food = obj->otyp == CORPSE || tinned,
2194             acid = obj->otyp == POT_ACID
2195                    || (food && acidic(&mons[obj->corpsenm])),
2196             lizard = food && obj->corpsenm == PM_LIZARD;
2197     int nutrit = food ? dog_nutrition(mon, obj) : 0; /* also sets meating */
2198
2199     /* give a "<mon> is slowing down" message and also remove
2200        intrinsic speed (comparable to similar effect on the hero) */
2201     if (stoning)
2202         mon_adjust_speed(mon, -3, (struct obj *) 0);
2203
2204     if (vis) {
2205         long save_quan = obj->quan;
2206
2207         obj->quan = 1L;
2208         pline("%s %s %s.", Monnam(mon),
2209               (obj->oclass == POTION_CLASS)
2210                   ? "quaffs"
2211                   : (obj->otyp == TIN) ? "opens and eats the contents of"
2212                                        : "eats",
2213               distant_name(obj, doname));
2214         obj->quan = save_quan;
2215     } else if (!Deaf)
2216         You_hear("%s.",
2217                  (obj->oclass == POTION_CLASS) ? "drinking" : "chewing");
2218
2219     m_useup(mon, obj);
2220     /* obj is now gone */
2221
2222     if (acid && !tinned && !resists_acid(mon)) {
2223         mon->mhp -= rnd(15);
2224         if (vis)
2225             pline("%s has a very bad case of stomach acid.", Monnam(mon));
2226         if (mon->mhp <= 0) {
2227             pline("%s dies!", Monnam(mon));
2228             if (by_you)
2229                 xkilled(mon, 0);
2230             else
2231                 mondead(mon);
2232             return;
2233         }
2234     }
2235     if (stoning && vis) {
2236         if (Hallucination)
2237             pline("What a pity - %s just ruined a future piece of art!",
2238                   mon_nam(mon));
2239         else
2240             pline("%s seems limber!", Monnam(mon));
2241     }
2242     if (lizard && (mon->mconf || mon->mstun)) {
2243         mon->mconf = 0;
2244         mon->mstun = 0;
2245         if (vis && !is_bat(mon->data) && mon->data != &mons[PM_STALKER])
2246             pline("%s seems steadier now.", Monnam(mon));
2247     }
2248     if (mon->mtame && !mon->isminion && nutrit > 0) {
2249         struct edog *edog = EDOG(mon);
2250
2251         if (edog->hungrytime < monstermoves)
2252             edog->hungrytime = monstermoves;
2253         edog->hungrytime += nutrit;
2254         mon->mconf = 0;
2255     }
2256     /* use up monster's next move */
2257     mon->movement -= NORMAL_SPEED;
2258     mon->mlstmv = monstermoves;
2259 }
2260
2261 /* decide whether obj can cure petrification; also used when picking up */
2262 STATIC_OVL boolean
2263 cures_stoning(mon, obj, tinok)
2264 struct monst *mon;
2265 struct obj *obj;
2266 boolean tinok;
2267 {
2268     if (obj->otyp == POT_ACID)
2269         return TRUE;
2270     if (obj->otyp != CORPSE && (obj->otyp != TIN || !tinok))
2271         return FALSE;
2272     /* corpse, or tin that mon can open */
2273     return (boolean) (obj->corpsenm == PM_LIZARD
2274                       || (acidic(&mons[obj->corpsenm])
2275                           && (obj->corpsenm != PM_GREEN_SLIME
2276                               || slimeproof(mon->data))));
2277 }
2278
2279 STATIC_OVL boolean
2280 mcould_eat_tin(mon)
2281 struct monst *mon;
2282 {
2283     struct obj *obj, *mwep;
2284     boolean welded_wep;
2285
2286     /* monkeys who manage to steal tins can't open and eat them
2287        even if they happen to also have the appropriate tool */
2288     if (is_animal(mon->data))
2289         return FALSE;
2290
2291     mwep = MON_WEP(mon);
2292     welded_wep = mwep && mwelded(mwep);
2293     /* this is different from the player; tin opener or dagger doesn't
2294        have to be wielded, and knife can be used instead of dagger */
2295     for (obj = mon->minvent; obj; obj = obj->nobj) {
2296         /* if stuck with a cursed weapon, don't check rest of inventory */
2297         if (welded_wep && obj != mwep)
2298             continue;
2299
2300         if (obj->otyp == TIN_OPENER
2301             || (obj->oclass == WEAPON_CLASS
2302                 && (objects[obj->otyp].oc_skill == P_DAGGER
2303                     || objects[obj->otyp].oc_skill == P_KNIFE)))
2304             return TRUE;
2305     }
2306     return FALSE;
2307 }
2308
2309 /* TRUE if monster does something to avoid turning into green slime */
2310 boolean
2311 munslime(mon, by_you)
2312 struct monst *mon;
2313 boolean by_you;
2314 {
2315     struct obj *obj, odummy;
2316
2317     /*
2318      * muse_unslime() gives "mon starts turning green", "mon zaps
2319      * itself with a wand of fire", and "mon's slime burns away"
2320      * messages.  Monsters who don't get any chance at that just have
2321      * (via our caller) newcham()'s "mon turns into slime" feedback.
2322      */
2323
2324     if (slimeproof(mon->data))
2325         return FALSE;
2326     if (mon->meating || !mon->mcanmove || mon->msleeping)
2327         return FALSE;
2328     mon->mstrategy &= ~STRAT_WAITFORU;
2329
2330     /* if monster can breathe fire, do so upon self; a monster who deals
2331        fire damage by biting, clawing, gazing, and especially exploding
2332        isn't able to cure itself of green slime with its own attack
2333        [possible extension: monst capable of casting high level clerical
2334        spells could toss pillar of fire at self--probably too suicidal] */
2335     if (!mon->mcan && !mon->mspec_used
2336         && attacktype_fordmg(mon->data, AT_BREA, AD_FIRE)) {
2337         odummy = zeroobj; /* otyp == STRANGE_OBJECT */
2338         return muse_unslime(mon, &odummy, by_you);
2339     }
2340
2341     for (obj = mon->minvent; obj; obj = obj->nobj)
2342         if (cures_sliming(mon, obj))
2343             return muse_unslime(mon, obj, by_you);
2344
2345     /* TODO: check for and move onto an adjacent fire trap */
2346
2347     return FALSE;
2348 }
2349
2350 /* mon uses an item--selected by caller--to burn away incipient slime */
2351 STATIC_OVL boolean
2352 muse_unslime(mon, obj, by_you)
2353 struct monst *mon;
2354 struct obj *obj;
2355 boolean by_you; /* true: if mon kills itself, hero gets credit/blame */
2356 {
2357     struct obj *odummyp;
2358     int otyp = obj->otyp, dmg;
2359     boolean vis = canseemon(mon), res = TRUE;
2360
2361     if (vis)
2362         pline("%s starts turning %s.", Monnam(mon),
2363               green_mon(mon) ? "into ooze" : hcolor(NH_GREEN));
2364     /* -4 => sliming, causes quiet loss of enhanced speed */
2365     mon_adjust_speed(mon, -4, (struct obj *) 0);
2366
2367     if (otyp == STRANGE_OBJECT) {
2368         /* monster is using fire breath on self */
2369         if (vis)
2370             pline("%s breathes fire on %sself.", Monnam(mon), mhim(mon));
2371         if (!rn2(3))
2372             mon->mspec_used = rn1(10, 5);
2373         /* -21 => monster's fire breath; 1 => # of damage dice */
2374         (void) zhitm(mon, by_you ? 21 : -21, 1, &odummyp);
2375     } else if (otyp == SCR_FIRE) {
2376         mreadmsg(mon, obj);
2377         if (mon->mconf) {
2378             if (cansee(mon->mx, mon->my))
2379                 pline("Oh, what a pretty fire!");
2380             if (vis && !objects[otyp].oc_name_known
2381                 && !objects[otyp].oc_uname)
2382                 docall(obj);
2383             m_useup(mon, obj); /* after docall() */
2384             vis = FALSE;       /* skip makeknown() below */
2385             res = FALSE;       /* failed to cure sliming */
2386         } else {
2387             m_useup(mon, obj); /* before explode() */
2388             dmg = (2 * (rn1(3, 3) + 2 * bcsign(obj)) + 1) / 3;
2389             /* -11 => monster's fireball */
2390             explode(mon->mx, mon->my, -11, dmg, SCROLL_CLASS,
2391                     /* by_you: override -11 for mon but not others */
2392                     by_you ? -EXPL_FIERY : EXPL_FIERY);
2393         }
2394     } else { /* wand/horn of fire w/ positive charge count */
2395         mzapmsg(mon, obj, TRUE);
2396         obj->spe--;
2397         /* -1 => monster's wand of fire; 2 => # of damage dice */
2398         (void) zhitm(mon, by_you ? 1 : -1, 2, &odummyp);
2399     }
2400
2401     if (vis) {
2402         if (res && mon->mhp > 0)
2403             pline("%s slime is burned away!", s_suffix(Monnam(mon)));
2404         if (otyp != STRANGE_OBJECT)
2405             makeknown(otyp);
2406     }
2407     /* use up monster's next move */
2408     mon->movement -= NORMAL_SPEED;
2409     mon->mlstmv = monstermoves;
2410     return res;
2411 }
2412
2413 /* decide whether obj can be used to cure green slime */
2414 STATIC_OVL int
2415 cures_sliming(mon, obj)
2416 struct monst *mon;
2417 struct obj *obj;
2418 {
2419     /* scroll of fire, non-empty wand or horn of fire */
2420     if (obj->otyp == SCR_FIRE)
2421         return (haseyes(mon->data) && mon->mcansee);
2422     /* hero doesn't need hands or even limbs to zap, so mon doesn't either */
2423     return ((obj->otyp == WAN_FIRE || obj->otyp == FIRE_HORN)
2424             && obj->spe > 0);
2425 }
2426
2427 /* TRUE if monster appears to be green; for active TEXTCOLOR, we go by
2428    the display color, otherwise we just pick things that seem plausibly
2429    green (which doesn't necessarily match the TEXTCOLOR categorization) */
2430 STATIC_OVL boolean
2431 green_mon(mon)
2432 struct monst *mon;
2433 {
2434     struct permonst *ptr = mon->data;
2435
2436     if (Hallucination)
2437         return FALSE;
2438 #ifdef TEXTCOLOR
2439     if (iflags.use_color)
2440         return (ptr->mcolor == CLR_GREEN || ptr->mcolor == CLR_BRIGHT_GREEN);
2441 #endif
2442     /* approximation */
2443     if (strstri(ptr->mname, "green"))
2444         return TRUE;
2445     switch (monsndx(ptr)) {
2446     case PM_FOREST_CENTAUR:
2447     case PM_GARTER_SNAKE:
2448     case PM_GECKO:
2449     case PM_GREMLIN:
2450     case PM_HOMUNCULUS:
2451     case PM_JUIBLEX:
2452     case PM_LEPRECHAUN:
2453     case PM_LICHEN:
2454     case PM_LIZARD:
2455     case PM_WOOD_NYMPH:
2456         return TRUE;
2457     default:
2458         if (is_elf(ptr) && !is_prince(ptr) && !is_lord(ptr)
2459             && ptr != &mons[PM_GREY_ELF])
2460             return TRUE;
2461         break;
2462     }
2463     return FALSE;
2464 }
2465
2466 /*muse.c*/