Source:NetHack 3.6.2/src/mhitm.c

From NetHackWiki
Jump to navigation Jump to search

Below is the full text to mhitm.c from the source code of NetHack 3.6.2. To link to a particular line, write [[Source:NetHack 3.6.2/src/mhitm.c#line12]], for example.

The NetHack General Public License applies to screenshots, source code and other content from NetHack.

This content was modified from the original NetHack source code distribution (by splitting up NetHack content between wiki pages, and possibly further editing). See the page history for a list of who changed it, and on what dates.

1. /* NetHack 3.6	mhitm.c	$NHDT-Date: 1555720096 2019/04/20 00:28:16 $  $NHDT-Branch: NetHack-3.6.2-beta01 $:$NHDT-Revision: 1.113 $ */
2. /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
3. /*-Copyright (c) Robert Patrick Rankin, 2011. */
4. /* NetHack may be freely redistributed.  See license for details. */
5. 
6. #include "hack.h"
7. #include "artifact.h"
8. 
9. extern boolean notonhead;
10. 
11. static NEARDATA boolean vis, far_noise;
12. static NEARDATA long noisetime;
13. static NEARDATA struct obj *otmp;
14. 
15. static const char brief_feeling[] =
16.     "have a %s feeling for a moment, then it passes.";
17. 
18. STATIC_DCL char *FDECL(mon_nam_too, (char *, struct monst *, struct monst *));
19. STATIC_DCL int FDECL(hitmm, (struct monst *, struct monst *,
20.                              struct attack *));
21. STATIC_DCL int FDECL(gazemm, (struct monst *, struct monst *,
22.                               struct attack *));
23. STATIC_DCL int FDECL(gulpmm, (struct monst *, struct monst *,
24.                               struct attack *));
25. STATIC_DCL int FDECL(explmm, (struct monst *, struct monst *,
26.                               struct attack *));
27. STATIC_DCL int FDECL(mdamagem, (struct monst *, struct monst *,
28.                                 struct attack *));
29. STATIC_DCL void FDECL(mswingsm, (struct monst *, struct monst *,
30.                                  struct obj *));
31. STATIC_DCL void FDECL(noises, (struct monst *, struct attack *));
32. STATIC_DCL void FDECL(missmm, (struct monst *, struct monst *,
33.                                struct attack *));
34. STATIC_DCL int FDECL(passivemm, (struct monst *, struct monst *,
35.                                  BOOLEAN_P, int));
36. 
37. /* Needed for the special case of monsters wielding vorpal blades (rare).
38.  * If we use this a lot it should probably be a parameter to mdamagem()
39.  * instead of a global variable.
40.  */
41. static int dieroll;
42. 
43. /* returns mon_nam(mon) relative to other_mon; normal name unless they're
44.    the same, in which case the reference is to {him|her|it} self */
45. STATIC_OVL char *
46. mon_nam_too(outbuf, mon, other_mon)
47. char *outbuf;
48. struct monst *mon, *other_mon;
49. {
50.     if (mon != other_mon)
51.         Strcpy(outbuf, mon_nam(mon));
52.     else
53.         switch (pronoun_gender(mon, FALSE)) {
54.         case 0:
55.             Strcpy(outbuf, "himself");
56.             break;
57.         case 1:
58.             Strcpy(outbuf, "herself");
59.             break;
60.         default:
61.             Strcpy(outbuf, "itself");
62.             break;
63.         }
64.     return outbuf;
65. }
66. 
67. STATIC_OVL void
68. noises(magr, mattk)
69. register struct monst *magr;
70. register struct attack *mattk;
71. {
72.     boolean farq = (distu(magr->mx, magr->my) > 15);
73. 
74.     if (!Deaf && (farq != far_noise || moves - noisetime > 10)) {
75.         far_noise = farq;
76.         noisetime = moves;
77.         You_hear("%s%s.",
78.                  (mattk->aatyp == AT_EXPL) ? "an explosion" : "some noises",
79.                  farq ? " in the distance" : "");
80.     }
81. }
82. 
83. STATIC_OVL
84. void
85. missmm(magr, mdef, mattk)
86. register struct monst *magr, *mdef;
87. struct attack *mattk;
88. {
89.     const char *fmt;
90.     char buf[BUFSZ], mdef_name[BUFSZ];
91. 
92.     if (vis) {
93.         if (!canspotmon(magr))
94.             map_invisible(magr->mx, magr->my);
95.         if (!canspotmon(mdef))
96.             map_invisible(mdef->mx, mdef->my);
97.         if (M_AP_TYPE(mdef))
98.             seemimic(mdef);
99.         if (M_AP_TYPE(magr))
100.             seemimic(magr);
101.         fmt = (could_seduce(magr, mdef, mattk) && !magr->mcan)
102.                   ? "%s pretends to be friendly to"
103.                   : "%s misses";
104.         Sprintf(buf, fmt, Monnam(magr));
105.         pline("%s %s.", buf, mon_nam_too(mdef_name, mdef, magr));
106.     } else
107.         noises(magr, mattk);
108. }
109. 
110. /*
111.  *  fightm()  -- fight some other monster
112.  *
113.  *  Returns:
114.  *      0 - Monster did nothing.
115.  *      1 - If the monster made an attack.  The monster might have died.
116.  *
117.  *  There is an exception to the above.  If mtmp has the hero swallowed,
118.  *  then we report that the monster did nothing so it will continue to
119.  *  digest the hero.
120.  */
121.  /* have monsters fight each other */
122. int
123. fightm(mtmp)
124. register struct monst *mtmp;
125. {
126.     register struct monst *mon, *nmon;
127.     int result, has_u_swallowed;
128. #ifdef LINT
129.     nmon = 0;
130. #endif
131.     /* perhaps the monster will resist Conflict */
132.     if (resist(mtmp, RING_CLASS, 0, 0))
133.         return 0;
134. 
135.     if (u.ustuck == mtmp) {
136.         /* perhaps we're holding it... */
137.         if (itsstuck(mtmp))
138.             return 0;
139.     }
140.     has_u_swallowed = (u.uswallow && (mtmp == u.ustuck));
141. 
142.     for (mon = fmon; mon; mon = nmon) {
143.         nmon = mon->nmon;
144.         if (nmon == mtmp)
145.             nmon = mtmp->nmon;
146.         /* Be careful to ignore monsters that are already dead, since we
147.          * might be calling this before we've cleaned them up.  This can
148.          * happen if the monster attacked a cockatrice bare-handedly, for
149.          * instance.
150.          */
151.         if (mon != mtmp && !DEADMONSTER(mon)) {
152.             if (monnear(mtmp, mon->mx, mon->my)) {
153.                 if (!u.uswallow && (mtmp == u.ustuck)) {
154.                     if (!rn2(4)) {
155.                         pline("%s releases you!", Monnam(mtmp));
156.                         u.ustuck = 0;
157.                     } else
158.                         break;
159.                 }
160. 
161.                 /* mtmp can be killed */
162.                 bhitpos.x = mon->mx;
163.                 bhitpos.y = mon->my;
164.                 notonhead = 0;
165.                 result = mattackm(mtmp, mon);
166. 
167.                 if (result & MM_AGR_DIED)
168.                     return 1; /* mtmp died */
169.                 /*
170.                  * If mtmp has the hero swallowed, lie and say there
171.                  * was no attack (this allows mtmp to digest the hero).
172.                  */
173.                 if (has_u_swallowed)
174.                     return 0;
175. 
176.                 /* Allow attacked monsters a chance to hit back. Primarily
177.                  * to allow monsters that resist conflict to respond.
178.                  */
179.                 if ((result & MM_HIT) && !(result & MM_DEF_DIED) && rn2(4)
180.                     && mon->movement >= NORMAL_SPEED) {
181.                     mon->movement -= NORMAL_SPEED;
182.                     notonhead = 0;
183.                     (void) mattackm(mon, mtmp); /* return attack */
184.                 }
185. 
186.                 return (result & MM_HIT) ? 1 : 0;
187.             }
188.         }
189.     }
190.     return 0;
191. }
192. 
193. /*
194.  * mdisplacem() -- attacker moves defender out of the way;
195.  *                 returns same results as mattackm().
196.  */
197. int
198. mdisplacem(magr, mdef, quietly)
199. register struct monst *magr, *mdef;
200. boolean quietly;
201. {
202.     struct permonst *pa, *pd;
203.     int tx, ty, fx, fy;
204. 
205.     /* sanity checks; could matter if we unexpectedly get a long worm */
206.     if (!magr || !mdef || magr == mdef)
207.         return MM_MISS;
208.     pa = magr->data, pd = mdef->data;
209.     tx = mdef->mx, ty = mdef->my; /* destination */
210.     fx = magr->mx, fy = magr->my; /* current location */
211.     if (m_at(fx, fy) != magr || m_at(tx, ty) != mdef)
212.         return MM_MISS;
213. 
214.     /* The 1 in 7 failure below matches the chance in attack()
215.      * for pet displacement.
216.      */
217.     if (!rn2(7))
218.         return MM_MISS;
219. 
220.     /* Grid bugs cannot displace at an angle. */
221.     if (pa == &mons[PM_GRID_BUG] && magr->mx != mdef->mx
222.         && magr->my != mdef->my)
223.         return MM_MISS;
224. 
225.     /* undetected monster becomes un-hidden if it is displaced */
226.     if (mdef->mundetected)
227.         mdef->mundetected = 0;
228.     if (M_AP_TYPE(mdef) && M_AP_TYPE(mdef) != M_AP_MONSTER)
229.         seemimic(mdef);
230.     /* wake up the displaced defender */
231.     mdef->msleeping = 0;
232.     mdef->mstrategy &= ~STRAT_WAITMASK;
233.     finish_meating(mdef);
234. 
235.     /*
236.      * Set up the visibility of action.
237.      * You can observe monster displacement if you can see both of
238.      * the monsters involved.
239.      */
240.     vis = (canspotmon(magr) && canspotmon(mdef));
241. 
242.     if (touch_petrifies(pd) && !resists_ston(magr)) {
243.         if (which_armor(magr, W_ARMG) != 0) {
244.             if (poly_when_stoned(pa)) {
245.                 mon_to_stone(magr);
246.                 return MM_HIT; /* no damage during the polymorph */
247.             }
248.             if (!quietly && canspotmon(magr))
249.                 pline("%s turns to stone!", Monnam(magr));
250.             monstone(magr);
251.             if (!DEADMONSTER(magr))
252.                 return MM_HIT; /* lifesaved */
253.             else if (magr->mtame && !vis)
254.                 You(brief_feeling, "peculiarly sad");
255.             return MM_AGR_DIED;
256.         }
257.     }
258. 
259.     remove_monster(fx, fy); /* pick up from orig position */
260.     remove_monster(tx, ty);
261.     place_monster(magr, tx, ty); /* put down at target spot */
262.     place_monster(mdef, fx, fy);
263.     if (vis && !quietly)
264.         pline("%s moves %s out of %s way!", Monnam(magr), mon_nam(mdef),
265.               is_rider(pa) ? "the" : mhis(magr));
266.     newsym(fx, fy);  /* see it */
267.     newsym(tx, ty);  /*   all happen */
268.     flush_screen(0); /* make sure it shows up */
269. 
270.     return MM_HIT;
271. }
272. 
273. /*
274.  * mattackm() -- a monster attacks another monster.
275.  *
276.  *          --------- aggressor died
277.  *         /  ------- defender died
278.  *        /  /  ----- defender was hit
279.  *       /  /  /
280.  *      x  x  x
281.  *
282.  *      0x4     MM_AGR_DIED
283.  *      0x2     MM_DEF_DIED
284.  *      0x1     MM_HIT
285.  *      0x0     MM_MISS
286.  *
287.  * Each successive attack has a lower probability of hitting.  Some rely on
288.  * success of previous attacks.  ** this doen't seem to be implemented -dl **
289.  *
290.  * In the case of exploding monsters, the monster dies as well.
291.  */
292. int
293. mattackm(magr, mdef)
294. register struct monst *magr, *mdef;
295. {
296.     int i,          /* loop counter */
297.         tmp,        /* amour class difference */
298.         strike = 0, /* hit this attack */
299.         attk,       /* attack attempted this time */
300.         struck = 0, /* hit at least once */
301.         res[NATTK]; /* results of all attacks */
302.     struct attack *mattk, alt_attk;
303.     struct permonst *pa, *pd;
304. 
305.     if (!magr || !mdef)
306.         return MM_MISS; /* mike@genat */
307.     if (!magr->mcanmove || magr->msleeping)
308.         return MM_MISS;
309.     pa = magr->data;
310.     pd = mdef->data;
311. 
312.     /* Grid bugs cannot attack at an angle. */
313.     if (pa == &mons[PM_GRID_BUG] && magr->mx != mdef->mx
314.         && magr->my != mdef->my)
315.         return MM_MISS;
316. 
317.     /* Calculate the armour class differential. */
318.     tmp = find_mac(mdef) + magr->m_lev;
319.     if (mdef->mconf || !mdef->mcanmove || mdef->msleeping) {
320.         tmp += 4;
321.         mdef->msleeping = 0;
322.     }
323. 
324.     /* undetect monsters become un-hidden if they are attacked */
325.     if (mdef->mundetected) {
326.         mdef->mundetected = 0;
327.         newsym(mdef->mx, mdef->my);
328.         if (canseemon(mdef) && !sensemon(mdef)) {
329.             if (Unaware) {
330.                 boolean justone = (mdef->data->geno & G_UNIQ) != 0L;
331.                 const char *montype;
332. 
333.                 montype = noname_monnam(mdef, justone ? ARTICLE_THE
334.                                                       : ARTICLE_NONE);
335.                 if (!justone)
336.                     montype = makeplural(montype);
337.                 You("dream of %s.", montype);
338.             } else
339.                 pline("Suddenly, you notice %s.", a_monnam(mdef));
340.         }
341.     }
342. 
343.     /* Elves hate orcs. */
344.     if (is_elf(pa) && is_orc(pd))
345.         tmp++;
346. 
347.     /* Set up the visibility of action */
348.     vis = (cansee(magr->mx, magr->my) && cansee(mdef->mx, mdef->my)
349.            && (canspotmon(magr) || canspotmon(mdef)));
350. 
351.     /* Set flag indicating monster has moved this turn.  Necessary since a
352.      * monster might get an attack out of sequence (i.e. before its move) in
353.      * some cases, in which case this still counts as its move for the round
354.      * and it shouldn't move again.
355.      */
356.     magr->mlstmv = monstermoves;
357. 
358.     /* Now perform all attacks for the monster. */
359.     for (i = 0; i < NATTK; i++) {
360.         res[i] = MM_MISS;
361.         mattk = getmattk(magr, mdef, i, res, &alt_attk);
362.         otmp = (struct obj *) 0;
363.         attk = 1;
364.         switch (mattk->aatyp) {
365.         case AT_WEAP: /* "hand to hand" attacks */
366.             if (distmin(magr->mx, magr->my, mdef->mx, mdef->my) > 1) {
367.                 /* D: Do a ranged attack here! */
368.                 strike = thrwmm(magr, mdef);
369.                 if (DEADMONSTER(mdef))
370.                     res[i] = MM_DEF_DIED;
371.                 if (DEADMONSTER(magr))
372.                     res[i] |= MM_AGR_DIED;
373.                 break;
374.             }
375.             if (magr->weapon_check == NEED_WEAPON || !MON_WEP(magr)) {
376.                 magr->weapon_check = NEED_HTH_WEAPON;
377.                 if (mon_wield_item(magr) != 0)
378.                     return 0;
379.             }
380.             possibly_unwield(magr, FALSE);
381.             otmp = MON_WEP(magr);
382. 
383.             if (otmp) {
384.                 if (vis)
385.                     mswingsm(magr, mdef, otmp);
386.                 tmp += hitval(otmp, mdef);
387.             }
388.             /*FALLTHRU*/
389.         case AT_CLAW:
390.         case AT_KICK:
391.         case AT_BITE:
392.         case AT_STNG:
393.         case AT_TUCH:
394.         case AT_BUTT:
395.         case AT_TENT:
396.             /* Nymph that teleported away on first attack? */
397.             if (distmin(magr->mx, magr->my, mdef->mx, mdef->my) > 1)
398.                 /* Continue because the monster may have a ranged attack. */
399.                 continue;
400.             /* Monsters won't attack cockatrices physically if they
401.              * have a weapon instead.  This instinct doesn't work for
402.              * players, or under conflict or confusion.
403.              */
404.             if (!magr->mconf && !Conflict && otmp && mattk->aatyp != AT_WEAP
405.                 && touch_petrifies(mdef->data)) {
406.                 strike = 0;
407.                 break;
408.             }
409.             dieroll = rnd(20 + i);
410.             strike = (tmp > dieroll);
411.             /* KMH -- don't accumulate to-hit bonuses */
412.             if (otmp)
413.                 tmp -= hitval(otmp, mdef);
414.             if (strike) {
415.                 res[i] = hitmm(magr, mdef, mattk);
416.                 if ((mdef->data == &mons[PM_BLACK_PUDDING]
417.                      || mdef->data == &mons[PM_BROWN_PUDDING])
418.                     && (otmp && (objects[otmp->otyp].oc_material == IRON
419.                                  || objects[otmp->otyp].oc_material == METAL))
420.                     && mdef->mhp > 1
421.                     && !mdef->mcan) {
422.                     struct monst *mclone;
423.                     if ((mclone = clone_mon(mdef, 0, 0)) != 0) {
424.                         if (vis && canspotmon(mdef)) {
425.                             char buf[BUFSZ];
426. 
427.                             Strcpy(buf, Monnam(mdef));
428.                             pline("%s divides as %s hits it!", buf,
429.                                   mon_nam(magr));
430.                         }
431.                         mintrap(mclone);
432.                     }
433.                 }
434.             } else
435.                 missmm(magr, mdef, mattk);
436.             break;
437. 
438.         case AT_HUGS: /* automatic if prev two attacks succeed */
439.             strike = (i >= 2 && res[i - 1] == MM_HIT && res[i - 2] == MM_HIT);
440.             if (strike)
441.                 res[i] = hitmm(magr, mdef, mattk);
442. 
443.             break;
444. 
445.         case AT_GAZE:
446.             strike = 0;
447.             res[i] = gazemm(magr, mdef, mattk);
448.             break;
449. 
450.         case AT_EXPL:
451.             /* D: Prevent explosions from a distance */
452.             if (distmin(magr->mx,magr->my,mdef->mx,mdef->my) > 1)
453.                 continue;
454. 
455.             res[i] = explmm(magr, mdef, mattk);
456.             if (res[i] == MM_MISS) { /* cancelled--no attack */
457.                 strike = 0;
458.                 attk = 0;
459.             } else
460.                 strike = 1; /* automatic hit */
461.             break;
462. 
463.         case AT_ENGL:
464.             if (u.usteed && mdef == u.usteed) {
465.                 strike = 0;
466.                 break;
467.             }
468.             /* D: Prevent engulf from a distance */
469.             if (distmin(magr->mx, magr->my, mdef->mx, mdef->my) > 1)
470.                 continue;
471.             /* Engulfing attacks are directed at the hero if possible. -dlc */
472.             if (u.uswallow && magr == u.ustuck)
473.                 strike = 0;
474.             else if ((strike = (tmp > rnd(20 + i))) != 0)
475.                 res[i] = gulpmm(magr, mdef, mattk);
476.             else
477.                 missmm(magr, mdef, mattk);
478.             break;
479. 
480.         case AT_BREA:
481.             if (!monnear(magr, mdef->mx, mdef->my)) {
482.                 strike = breamm(magr, mattk, mdef);
483. 
484.                 /* We don't really know if we hit or not; pretend we did. */
485.                 if (strike)
486.                     res[i] |= MM_HIT;
487.                 if (DEADMONSTER(mdef))
488.                     res[i] = MM_DEF_DIED;
489.                 if (DEADMONSTER(magr))
490.                     res[i] |= MM_AGR_DIED;
491.             }
492.             else
493.                 strike = 0;
494.             break;
495. 
496.         case AT_SPIT:
497.             if (!monnear(magr, mdef->mx, mdef->my)) {
498.                 strike = spitmm(magr, mattk, mdef);
499. 
500.                 /* We don't really know if we hit or not; pretend we did. */
501.                 if (strike)
502.                     res[i] |= MM_HIT;
503.                 if (DEADMONSTER(mdef))
504.                     res[i] = MM_DEF_DIED;
505.                 if (DEADMONSTER(magr))
506.                     res[i] |= MM_AGR_DIED;
507.             }
508.             break;
509. 
510.         default: /* no attack */
511.             strike = 0;
512.             attk = 0;
513.             break;
514.         }
515. 
516.         if (attk && !(res[i] & MM_AGR_DIED)
517.             && distmin(magr->mx, magr->my, mdef->mx, mdef->my) <= 1)
518.             res[i] = passivemm(magr, mdef, strike, res[i] & MM_DEF_DIED);
519. 
520.         if (res[i] & MM_DEF_DIED)
521.             return res[i];
522.         if (res[i] & MM_AGR_DIED)
523.             return res[i];
524.         /* return if aggressor can no longer attack */
525.         if (!magr->mcanmove || magr->msleeping)
526.             return res[i];
527.         if (res[i] & MM_HIT)
528.             struck = 1; /* at least one hit */
529.     }
530. 
531.     return (struck ? MM_HIT : MM_MISS);
532. }
533. 
534. /* Returns the result of mdamagem(). */
535. STATIC_OVL int
536. hitmm(magr, mdef, mattk)
537. register struct monst *magr, *mdef;
538. struct attack *mattk;
539. {
540.     if (vis) {
541.         int compat;
542.         char buf[BUFSZ], mdef_name[BUFSZ];
543. 
544.         if (!canspotmon(magr))
545.             map_invisible(magr->mx, magr->my);
546.         if (!canspotmon(mdef))
547.             map_invisible(mdef->mx, mdef->my);
548.         if (M_AP_TYPE(mdef))
549.             seemimic(mdef);
550.         if (M_AP_TYPE(magr))
551.             seemimic(magr);
552.         if ((compat = could_seduce(magr, mdef, mattk)) && !magr->mcan) {
553.             Sprintf(buf, "%s %s", Monnam(magr),
554.                     mdef->mcansee ? "smiles at" : "talks to");
555.             pline("%s %s %s.", buf, mon_nam(mdef),
556.                   compat == 2 ? "engagingly" : "seductively");
557.         } else {
558.             char magr_name[BUFSZ];
559. 
560.             Strcpy(magr_name, Monnam(magr));
561.             switch (mattk->aatyp) {
562.             case AT_BITE:
563.                 Sprintf(buf, "%s bites", magr_name);
564.                 break;
565.             case AT_STNG:
566.                 Sprintf(buf, "%s stings", magr_name);
567.                 break;
568.             case AT_BUTT:
569.                 Sprintf(buf, "%s butts", magr_name);
570.                 break;
571.             case AT_TUCH:
572.                 Sprintf(buf, "%s touches", magr_name);
573.                 break;
574.             case AT_TENT:
575.                 Sprintf(buf, "%s tentacles suck", s_suffix(magr_name));
576.                 break;
577.             case AT_HUGS:
578.                 if (magr != u.ustuck) {
579.                     Sprintf(buf, "%s squeezes", magr_name);
580.                     break;
581.                 }
582.                 /*FALLTHRU*/
583.             default:
584.                 Sprintf(buf, "%s hits", magr_name);
585.             }
586.             pline("%s %s.", buf, mon_nam_too(mdef_name, mdef, magr));
587.         }
588.     } else
589.         noises(magr, mattk);
590. 
591.     return mdamagem(magr, mdef, mattk);
592. }
593. 
594. /* Returns the same values as mdamagem(). */
595. STATIC_OVL int
596. gazemm(magr, mdef, mattk)
597. register struct monst *magr, *mdef;
598. struct attack *mattk;
599. {
600.     char buf[BUFSZ];
601. 
602.     if (vis) {
603.         if (mdef->data->mlet == S_MIMIC
604.             && M_AP_TYPE(mdef) != M_AP_NOTHING)
605.             seemimic(mdef);
606.         Sprintf(buf, "%s gazes at", Monnam(magr));
607.         pline("%s %s...", buf,
608.               canspotmon(mdef) ? mon_nam(mdef) : "something");
609.     }
610. 
611.     if (magr->mcan || !magr->mcansee || !mdef->mcansee
612.         || (magr->minvis && !perceives(mdef->data)) || mdef->msleeping) {
613.         if (vis && canspotmon(mdef))
614.             pline("but nothing happens.");
615.         return MM_MISS;
616.     }
617.     /* call mon_reflects 2x, first test, then, if visible, print message */
618.     if (magr->data == &mons[PM_MEDUSA] && mon_reflects(mdef, (char *) 0)) {
619.         if (canseemon(mdef))
620.             (void) mon_reflects(mdef, "The gaze is reflected away by %s %s.");
621.         if (mdef->mcansee) {
622.             if (mon_reflects(magr, (char *) 0)) {
623.                 if (canseemon(magr))
624.                     (void) mon_reflects(magr,
625.                                       "The gaze is reflected away by %s %s.");
626.                 return MM_MISS;
627.             }
628.             if (mdef->minvis && !perceives(magr->data)) {
629.                 if (canseemon(magr)) {
630.                     pline(
631.                       "%s doesn't seem to notice that %s gaze was reflected.",
632.                           Monnam(magr), mhis(magr));
633.                 }
634.                 return MM_MISS;
635.             }
636.             if (canseemon(magr))
637.                 pline("%s is turned to stone!", Monnam(magr));
638.             monstone(magr);
639.             if (!DEADMONSTER(magr))
640.                 return MM_MISS;
641.             return MM_AGR_DIED;
642.         }
643.     }
644. 
645.     return mdamagem(magr, mdef, mattk);
646. }
647. 
648. /* return True if magr is allowed to swallow mdef, False otherwise */
649. boolean
650. engulf_target(magr, mdef)
651. struct monst *magr, *mdef;
652. {
653.     struct rm *lev;
654.     int dx, dy;
655. 
656.     /* can't swallow something that's too big */
657.     if (mdef->data->msize >= MZ_HUGE)
658.         return FALSE;
659. 
660.     /* (hypothetical) engulfers who can pass through walls aren't
661.      limited by rock|trees|bars */
662.     if ((magr == &youmonst) ? Passes_walls : passes_walls(magr->data))
663.         return TRUE;
664. 
665.     /* don't swallow something in a spot where attacker wouldn't
666.        otherwise be able to move onto; we don't want to engulf
667.        a wall-phaser and end up with a non-phaser inside a wall */
668.     dx = mdef->mx, dy = mdef->my;
669.     if (mdef == &youmonst)
670.         dx = u.ux, dy = u.uy;
671.     lev = &levl[dx][dy];
672.     if (IS_ROCK(lev->typ) || closed_door(dx, dy) || IS_TREE(lev->typ)
673.         /* not passes_bars(); engulfer isn't squeezing through */
674.         || (lev->typ == IRONBARS && !is_whirly(magr->data)))
675.         return FALSE;
676. 
677.     return TRUE;
678. }
679. 
680. /* Returns the same values as mattackm(). */
681. STATIC_OVL int
682. gulpmm(magr, mdef, mattk)
683. register struct monst *magr, *mdef;
684. register struct attack *mattk;
685. {
686.     xchar ax, ay, dx, dy;
687.     int status;
688.     char buf[BUFSZ];
689.     struct obj *obj;
690. 
691.     if (!engulf_target(magr, mdef))
692.         return MM_MISS;
693. 
694.     if (vis) {
695.         /* [this two-part formatting dates back to when only one x_monnam
696.            result could be included in an expression because the next one
697.            would overwrite first's result -- that's no longer the case] */
698.         Sprintf(buf, "%s swallows", Monnam(magr));
699.         pline("%s %s.", buf, mon_nam(mdef));
700.     }
701.     for (obj = mdef->minvent; obj; obj = obj->nobj)
702.         (void) snuff_lit(obj);
703. 
704.     if (is_vampshifter(mdef)
705.         && newcham(mdef, &mons[mdef->cham], FALSE, FALSE)) {
706.         if (vis) {
707.             /* 'it' -- previous form is no longer available and
708.                using that would be excessively verbose */
709.             pline("%s expels %s.", Monnam(magr),
710.                   canspotmon(mdef) ? "it" : something);
711.             if (canspotmon(mdef))
712.                 pline("It turns into %s.", a_monnam(mdef));
713.         }
714.         return MM_HIT; /* bypass mdamagem() */
715.     }
716. 
717.     /*
718.      *  All of this manipulation is needed to keep the display correct.
719.      *  There is a flush at the next pline().
720.      */
721.     ax = magr->mx;
722.     ay = magr->my;
723.     dx = mdef->mx;
724.     dy = mdef->my;
725.     /*
726.      *  Leave the defender in the monster chain at it's current position,
727.      *  but don't leave it on the screen.  Move the aggressor to the
728.      *  defender's position.
729.      */
730.     remove_monster(dx, dy);
731.     remove_monster(ax, ay);
732.     place_monster(magr, dx, dy);
733.     newsym(ax, ay); /* erase old position */
734.     newsym(dx, dy); /* update new position */
735. 
736.     status = mdamagem(magr, mdef, mattk);
737. 
738.     if ((status & (MM_AGR_DIED | MM_DEF_DIED))
739.         == (MM_AGR_DIED | MM_DEF_DIED)) {
740.         ;                              /* both died -- do nothing  */
741.     } else if (status & MM_DEF_DIED) { /* defender died */
742.         /*
743.          *  Note:  remove_monster() was called in relmon(), wiping out
744.          *  magr from level.monsters[mdef->mx][mdef->my].  We need to
745.          *  put it back and display it.  -kd
746.          */
747.         place_monster(magr, dx, dy);
748.         newsym(dx, dy);
749.         /* aggressor moves to <dx,dy> and might encounter trouble there */
750.         if (minliquid(magr) || (t_at(dx, dy) && mintrap(magr) == 2))
751.             status |= MM_AGR_DIED;
752.     } else if (status & MM_AGR_DIED) { /* aggressor died */
753.         place_monster(mdef, dx, dy);
754.         newsym(dx, dy);
755.     } else {                           /* both alive, put them back */
756.         if (cansee(dx, dy))
757.             pline("%s is regurgitated!", Monnam(mdef));
758. 
759.         remove_monster(dx,dy);
760.         place_monster(magr, ax, ay);
761.         place_monster(mdef, dx, dy);
762.         newsym(ax, ay);
763.         newsym(dx, dy);
764.     }
765. 
766.     return status;
767. }
768. 
769. STATIC_OVL int
770. explmm(magr, mdef, mattk)
771. struct monst *magr, *mdef;
772. struct attack *mattk;
773. {
774.     int result;
775. 
776.     if (magr->mcan)
777.         return MM_MISS;
778. 
779.     if (cansee(magr->mx, magr->my))
780.         pline("%s explodes!", Monnam(magr));
781.     else
782.         noises(magr, mattk);
783. 
784.     result = mdamagem(magr, mdef, mattk);
785. 
786.     /* Kill off aggressor if it didn't die. */
787.     if (!(result & MM_AGR_DIED)) {
788.         boolean was_leashed = (magr->mleashed != 0);
789. 
790.         mondead(magr);
791.         if (!DEADMONSTER(magr))
792.             return result; /* life saved */
793.         result |= MM_AGR_DIED;
794. 
795.         /* mondead() -> m_detach() -> m_unleash() always suppresses
796.            the m_unleash() slack message, so deliver it here instead */
797.         if (was_leashed)
798.             Your("leash falls slack.");
799.     }
800.     if (magr->mtame) /* give this one even if it was visible */
801.         You(brief_feeling, "melancholy");
802. 
803.     return result;
804. }
805. 
806. /*
807.  *  See comment at top of mattackm(), for return values.
808.  */
809. STATIC_OVL int
810. mdamagem(magr, mdef, mattk)
811. register struct monst *magr, *mdef;
812. register struct attack *mattk;
813. {
814.     struct obj *obj;
815.     char buf[BUFSZ];
816.     struct permonst *pa = magr->data, *pd = mdef->data;
817.     int armpro, num, tmp = d((int) mattk->damn, (int) mattk->damd),
818.                      res = MM_MISS;
819.     boolean cancelled;
820. 
821.     if ((touch_petrifies(pd) /* or flesh_petrifies() */
822.          || (mattk->adtyp == AD_DGST && pd == &mons[PM_MEDUSA]))
823.         && !resists_ston(magr)) {
824.         long protector = attk_protection((int) mattk->aatyp),
825.              wornitems = magr->misc_worn_check;
826. 
827.         /* wielded weapon gives same protection as gloves here */
828.         if (otmp != 0)
829.             wornitems |= W_ARMG;
830. 
831.         if (protector == 0L
832.             || (protector != ~0L && (wornitems & protector) != protector)) {
833.             if (poly_when_stoned(pa)) {
834.                 mon_to_stone(magr);
835.                 return MM_HIT; /* no damage during the polymorph */
836.             }
837.             if (vis && canspotmon(magr))
838.                 pline("%s turns to stone!", Monnam(magr));
839.             monstone(magr);
840.             if (!DEADMONSTER(magr))
841.                 return MM_HIT; /* lifesaved */
842.             else if (magr->mtame && !vis)
843.                 You(brief_feeling, "peculiarly sad");
844.             return MM_AGR_DIED;
845.         }
846.     }
847. 
848.     /* cancellation factor is the same as when attacking the hero */
849.     armpro = magic_negation(mdef);
850.     cancelled = magr->mcan || !(rn2(10) >= 3 * armpro);
851. 
852.     switch (mattk->adtyp) {
853.     case AD_DGST:
854.         /* eating a Rider or its corpse is fatal */
855.         if (is_rider(pd)) {
856.             if (vis && canseemon(magr))
857.                 pline("%s %s!", Monnam(magr),
858.                       (pd == &mons[PM_FAMINE])
859.                           ? "belches feebly, shrivels up and dies"
860.                           : (pd == &mons[PM_PESTILENCE])
861.                                 ? "coughs spasmodically and collapses"
862.                                 : "vomits violently and drops dead");
863.             mondied(magr);
864.             if (!DEADMONSTER(magr))
865.                 return 0; /* lifesaved */
866.             else if (magr->mtame && !vis)
867.                 You(brief_feeling, "queasy");
868.             return MM_AGR_DIED;
869.         }
870.         if (flags.verbose && !Deaf)
871.             verbalize("Burrrrp!");
872.         tmp = mdef->mhp;
873.         /* Use up amulet of life saving */
874.         if (!!(obj = mlifesaver(mdef)))
875.             m_useup(mdef, obj);
876. 
877.         /* Is a corpse for nutrition possible?  It may kill magr */
878.         if (!corpse_chance(mdef, magr, TRUE) || DEADMONSTER(magr))
879.             break;
880. 
881.         /* Pets get nutrition from swallowing monster whole.
882.          * No nutrition from G_NOCORPSE monster, eg, undead.
883.          * DGST monsters don't die from undead corpses
884.          */
885.         num = monsndx(pd);
886.         if (magr->mtame && !magr->isminion
887.             && !(mvitals[num].mvflags & G_NOCORPSE)) {
888.             struct obj *virtualcorpse = mksobj(CORPSE, FALSE, FALSE);
889.             int nutrit;
890. 
891.             set_corpsenm(virtualcorpse, num);
892.             nutrit = dog_nutrition(magr, virtualcorpse);
893.             dealloc_obj(virtualcorpse);
894. 
895.             /* only 50% nutrition, 25% of normal eating time */
896.             if (magr->meating > 1)
897.                 magr->meating = (magr->meating + 3) / 4;
898.             if (nutrit > 1)
899.                 nutrit /= 2;
900.             EDOG(magr)->hungrytime += nutrit;
901.         }
902.         break;
903.     case AD_STUN:
904.         if (magr->mcan)
905.             break;
906.         if (canseemon(mdef))
907.             pline("%s %s for a moment.", Monnam(mdef),
908.                   makeplural(stagger(pd, "stagger")));
909.         mdef->mstun = 1;
910.         goto physical;
911.     case AD_LEGS:
912.         if (magr->mcan) {
913.             tmp = 0;
914.             break;
915.         }
916.         goto physical;
917.     case AD_WERE:
918.     case AD_HEAL:
919.     case AD_PHYS:
920.  physical:
921.         if (mattk->aatyp == AT_KICK && thick_skinned(pd)) {
922.             tmp = 0;
923.         } else if (mattk->aatyp == AT_WEAP) {
924.             if (otmp) {
925.                 struct obj *marmg;
926. 
927.                 if (otmp->otyp == CORPSE
928.                     && touch_petrifies(&mons[otmp->corpsenm]))
929.                     goto do_stone;
930.                 tmp += dmgval(otmp, mdef);
931.                 if ((marmg = which_armor(magr, W_ARMG)) != 0
932.                     && marmg->otyp == GAUNTLETS_OF_POWER)
933.                     tmp += rn1(4, 3); /* 3..6 */
934.                 if (tmp < 1) /* is this necessary?  mhitu.c has it... */
935.                     tmp = 1;
936.                 if (otmp->oartifact) {
937.                     (void) artifact_hit(magr, mdef, otmp, &tmp, dieroll);
938.                     if (DEADMONSTER(mdef))
939.                         return (MM_DEF_DIED
940.                                 | (grow_up(magr, mdef) ? 0 : MM_AGR_DIED));
941.                 }
942.                 if (tmp)
943.                     rustm(mdef, otmp);
944.             }
945.         } else if (pa == &mons[PM_PURPLE_WORM] && pd == &mons[PM_SHRIEKER]) {
946.             /* hack to enhance mm_aggression(); we don't want purple
947.                worm's bite attack to kill a shrieker because then it
948.                won't swallow the corpse; but if the target survives,
949.                the subsequent engulf attack should accomplish that */
950.             if (tmp >= mdef->mhp && mdef->mhp > 1)
951.                 tmp = mdef->mhp - 1;
952.         }
953.         break;
954.     case AD_FIRE:
955.         if (cancelled) {
956.             tmp = 0;
957.             break;
958.         }
959.         if (vis && canseemon(mdef))
960.             pline("%s is %s!", Monnam(mdef), on_fire(pd, mattk));
961.         if (completelyburns(pd)) { /* paper golem or straw golem */
962.             if (vis && canseemon(mdef))
963.                 pline("%s burns completely!", Monnam(mdef));
964.             mondead(mdef); /* was mondied() but that dropped paper scrolls */
965.             if (!DEADMONSTER(mdef))
966.                 return 0;
967.             else if (mdef->mtame && !vis)
968.                 pline("May %s roast in peace.", mon_nam(mdef));
969.             return (MM_DEF_DIED | (grow_up(magr, mdef) ? 0 : MM_AGR_DIED));
970.         }
971.         tmp += destroy_mitem(mdef, SCROLL_CLASS, AD_FIRE);
972.         tmp += destroy_mitem(mdef, SPBOOK_CLASS, AD_FIRE);
973.         if (resists_fire(mdef)) {
974.             if (vis && canseemon(mdef))
975.                 pline_The("fire doesn't seem to burn %s!", mon_nam(mdef));
976.             shieldeff(mdef->mx, mdef->my);
977.             golemeffects(mdef, AD_FIRE, tmp);
978.             tmp = 0;
979.         }
980.         /* only potions damage resistant players in destroy_item */
981.         tmp += destroy_mitem(mdef, POTION_CLASS, AD_FIRE);
982.         break;
983.     case AD_COLD:
984.         if (cancelled) {
985.             tmp = 0;
986.             break;
987.         }
988.         if (vis && canseemon(mdef))
989.             pline("%s is covered in frost!", Monnam(mdef));
990.         if (resists_cold(mdef)) {
991.             if (vis && canseemon(mdef))
992.                 pline_The("frost doesn't seem to chill %s!", mon_nam(mdef));
993.             shieldeff(mdef->mx, mdef->my);
994.             golemeffects(mdef, AD_COLD, tmp);
995.             tmp = 0;
996.         }
997.         tmp += destroy_mitem(mdef, POTION_CLASS, AD_COLD);
998.         break;
999.     case AD_ELEC:
1000.         if (cancelled) {
1001.             tmp = 0;
1002.             break;
1003.         }
1004.         if (vis && canseemon(mdef))
1005.             pline("%s gets zapped!", Monnam(mdef));
1006.         tmp += destroy_mitem(mdef, WAND_CLASS, AD_ELEC);
1007.         if (resists_elec(mdef)) {
1008.             if (vis && canseemon(mdef))
1009.                 pline_The("zap doesn't shock %s!", mon_nam(mdef));
1010.             shieldeff(mdef->mx, mdef->my);
1011.             golemeffects(mdef, AD_ELEC, tmp);
1012.             tmp = 0;
1013.         }
1014.         /* only rings damage resistant players in destroy_item */
1015.         tmp += destroy_mitem(mdef, RING_CLASS, AD_ELEC);
1016.         break;
1017.     case AD_ACID:
1018.         if (magr->mcan) {
1019.             tmp = 0;
1020.             break;
1021.         }
1022.         if (resists_acid(mdef)) {
1023.             if (vis && canseemon(mdef))
1024.                 pline("%s is covered in %s, but it seems harmless.",
1025.                       Monnam(mdef), hliquid("acid"));
1026.             tmp = 0;
1027.         } else if (vis && canseemon(mdef)) {
1028.             pline("%s is covered in %s!", Monnam(mdef), hliquid("acid"));
1029.             pline("It burns %s!", mon_nam(mdef));
1030.         }
1031.         if (!rn2(30))
1032.             erode_armor(mdef, ERODE_CORRODE);
1033.         if (!rn2(6))
1034.             acid_damage(MON_WEP(mdef));
1035.         break;
1036.     case AD_RUST:
1037.         if (magr->mcan)
1038.             break;
1039.         if (pd == &mons[PM_IRON_GOLEM]) {
1040.             if (vis && canseemon(mdef))
1041.                 pline("%s falls to pieces!", Monnam(mdef));
1042.             mondied(mdef);
1043.             if (!DEADMONSTER(mdef))
1044.                 return 0;
1045.             else if (mdef->mtame && !vis)
1046.                 pline("May %s rust in peace.", mon_nam(mdef));
1047.             return (MM_DEF_DIED | (grow_up(magr, mdef) ? 0 : MM_AGR_DIED));
1048.         }
1049.         erode_armor(mdef, ERODE_RUST);
1050.         mdef->mstrategy &= ~STRAT_WAITFORU;
1051.         tmp = 0;
1052.         break;
1053.     case AD_CORR:
1054.         if (magr->mcan)
1055.             break;
1056.         erode_armor(mdef, ERODE_CORRODE);
1057.         mdef->mstrategy &= ~STRAT_WAITFORU;
1058.         tmp = 0;
1059.         break;
1060.     case AD_DCAY:
1061.         if (magr->mcan)
1062.             break;
1063.         if (pd == &mons[PM_WOOD_GOLEM] || pd == &mons[PM_LEATHER_GOLEM]) {
1064.             if (vis && canseemon(mdef))
1065.                 pline("%s falls to pieces!", Monnam(mdef));
1066.             mondied(mdef);
1067.             if (!DEADMONSTER(mdef))
1068.                 return 0;
1069.             else if (mdef->mtame && !vis)
1070.                 pline("May %s rot in peace.", mon_nam(mdef));
1071.             return (MM_DEF_DIED | (grow_up(magr, mdef) ? 0 : MM_AGR_DIED));
1072.         }
1073.         erode_armor(mdef, ERODE_CORRODE);
1074.         mdef->mstrategy &= ~STRAT_WAITFORU;
1075.         tmp = 0;
1076.         break;
1077.     case AD_STON:
1078.         if (magr->mcan)
1079.             break;
1080.  do_stone:
1081.         /* may die from the acid if it eats a stone-curing corpse */
1082.         if (munstone(mdef, FALSE))
1083.             goto post_stone;
1084.         if (poly_when_stoned(pd)) {
1085.             mon_to_stone(mdef);
1086.             tmp = 0;
1087.             break;
1088.         }
1089.         if (!resists_ston(mdef)) {
1090.             if (vis && canseemon(mdef))
1091.                 pline("%s turns to stone!", Monnam(mdef));
1092.             monstone(mdef);
1093.  post_stone:
1094.             if (!DEADMONSTER(mdef))
1095.                 return 0;
1096.             else if (mdef->mtame && !vis)
1097.                 You(brief_feeling, "peculiarly sad");
1098.             return (MM_DEF_DIED | (grow_up(magr, mdef) ? 0 : MM_AGR_DIED));
1099.         }
1100.         tmp = (mattk->adtyp == AD_STON ? 0 : 1);
1101.         break;
1102.     case AD_TLPT:
1103.         if (!cancelled && tmp < mdef->mhp && !tele_restrict(mdef)) {
1104.             char mdef_Monnam[BUFSZ];
1105.             boolean wasseen = canspotmon(mdef);
1106. 
1107.             /* save the name before monster teleports, otherwise
1108.                we'll get "it" in the suddenly disappears message */
1109.             if (vis && wasseen)
1110.                 Strcpy(mdef_Monnam, Monnam(mdef));
1111.             mdef->mstrategy &= ~STRAT_WAITFORU;
1112.             (void) rloc(mdef, TRUE);
1113.             if (vis && wasseen && !canspotmon(mdef) && mdef != u.usteed)
1114.                 pline("%s suddenly disappears!", mdef_Monnam);
1115.             if (tmp >= mdef->mhp) { /* see hitmu(mhitu.c) */
1116.                 if (mdef->mhp == 1)
1117.                     ++mdef->mhp;
1118.                 tmp = mdef->mhp - 1;
1119.             }
1120.         }
1121.         break;
1122.     case AD_SLEE:
1123.         if (!cancelled && !mdef->msleeping
1124.             && sleep_monst(mdef, rnd(10), -1)) {
1125.             if (vis && canspotmon(mdef)) {
1126.                 Strcpy(buf, Monnam(mdef));
1127.                 pline("%s is put to sleep by %s.", buf, mon_nam(magr));
1128.             }
1129.             mdef->mstrategy &= ~STRAT_WAITFORU;
1130.             slept_monst(mdef);
1131.         }
1132.         break;
1133.     case AD_PLYS:
1134.         if (!cancelled && mdef->mcanmove) {
1135.             if (vis && canspotmon(mdef)) {
1136.                 Strcpy(buf, Monnam(mdef));
1137.                 pline("%s is frozen by %s.", buf, mon_nam(magr));
1138.             }
1139.             paralyze_monst(mdef, rnd(10));
1140.         }
1141.         break;
1142.     case AD_SLOW:
1143.         if (!cancelled && mdef->mspeed != MSLOW) {
1144.             unsigned int oldspeed = mdef->mspeed;
1145. 
1146.             mon_adjust_speed(mdef, -1, (struct obj *) 0);
1147.             mdef->mstrategy &= ~STRAT_WAITFORU;
1148.             if (mdef->mspeed != oldspeed && vis && canspotmon(mdef))
1149.                 pline("%s slows down.", Monnam(mdef));
1150.         }
1151.         break;
1152.     case AD_CONF:
1153.         /* Since confusing another monster doesn't have a real time
1154.          * limit, setting spec_used would not really be right (though
1155.          * we still should check for it).
1156.          */
1157.         if (!magr->mcan && !mdef->mconf && !magr->mspec_used) {
1158.             if (vis && canseemon(mdef))
1159.                 pline("%s looks confused.", Monnam(mdef));
1160.             mdef->mconf = 1;
1161.             mdef->mstrategy &= ~STRAT_WAITFORU;
1162.         }
1163.         break;
1164.     case AD_BLND:
1165.         if (can_blnd(magr, mdef, mattk->aatyp, (struct obj *) 0)) {
1166.             register unsigned rnd_tmp;
1167. 
1168.             if (vis && mdef->mcansee && canspotmon(mdef))
1169.                 pline("%s is blinded.", Monnam(mdef));
1170.             rnd_tmp = d((int) mattk->damn, (int) mattk->damd);
1171.             if ((rnd_tmp += mdef->mblinded) > 127)
1172.                 rnd_tmp = 127;
1173.             mdef->mblinded = rnd_tmp;
1174.             mdef->mcansee = 0;
1175.             mdef->mstrategy &= ~STRAT_WAITFORU;
1176.         }
1177.         tmp = 0;
1178.         break;
1179.     case AD_HALU:
1180.         if (!magr->mcan && haseyes(pd) && mdef->mcansee) {
1181.             if (vis && canseemon(mdef))
1182.                 pline("%s looks %sconfused.", Monnam(mdef),
1183.                       mdef->mconf ? "more " : "");
1184.             mdef->mconf = 1;
1185.             mdef->mstrategy &= ~STRAT_WAITFORU;
1186.         }
1187.         tmp = 0;
1188.         break;
1189.     case AD_CURS:
1190.         if (!night() && (pa == &mons[PM_GREMLIN]))
1191.             break;
1192.         if (!magr->mcan && !rn2(10)) {
1193.             mdef->mcan = 1; /* cancelled regardless of lifesave */
1194.             mdef->mstrategy &= ~STRAT_WAITFORU;
1195.             if (is_were(pd) && pd->mlet != S_HUMAN)
1196.                 were_change(mdef);
1197.             if (pd == &mons[PM_CLAY_GOLEM]) {
1198.                 if (vis && canseemon(mdef)) {
1199.                     pline("Some writing vanishes from %s head!",
1200.                           s_suffix(mon_nam(mdef)));
1201.                     pline("%s is destroyed!", Monnam(mdef));
1202.                 }
1203.                 mondied(mdef);
1204.                 if (!DEADMONSTER(mdef))
1205.                     return 0;
1206.                 else if (mdef->mtame && !vis)
1207.                     You(brief_feeling, "strangely sad");
1208.                 return (MM_DEF_DIED
1209.                         | (grow_up(magr, mdef) ? 0 : MM_AGR_DIED));
1210.             }
1211.             if (!Deaf) {
1212.                 if (!vis)
1213.                     You_hear("laughter.");
1214.                 else if (canseemon(magr))
1215.                     pline("%s chuckles.", Monnam(magr));
1216.             }
1217.         }
1218.         break;
1219.     case AD_SGLD:
1220.         tmp = 0;
1221.         if (magr->mcan)
1222.             break;
1223.         /* technically incorrect; no check for stealing gold from
1224.          * between mdef's feet...
1225.          */
1226.         {
1227.             struct obj *gold = findgold(mdef->minvent);
1228. 
1229.             if (!gold)
1230.                 break;
1231.             obj_extract_self(gold);
1232.             add_to_minv(magr, gold);
1233.         }
1234.         mdef->mstrategy &= ~STRAT_WAITFORU;
1235.         if (vis && canseemon(mdef)) {
1236.             Strcpy(buf, Monnam(magr));
1237.             pline("%s steals some gold from %s.", buf, mon_nam(mdef));
1238.         }
1239.         if (!tele_restrict(magr)) {
1240.             boolean couldspot = canspotmon(magr);
1241.             (void) rloc(magr, TRUE);
1242.             if (vis && couldspot && !canspotmon(magr))
1243.                 pline("%s suddenly disappears!", buf);
1244.         }
1245.         break;
1246.     case AD_DRLI:
1247.         if (!cancelled && !rn2(3) && !resists_drli(mdef)) {
1248.             tmp = d(2, 6);
1249.             if (vis && canspotmon(mdef))
1250.                 pline("%s suddenly seems weaker!", Monnam(mdef));
1251.             mdef->mhpmax -= tmp;
1252.             if (mdef->m_lev == 0)
1253.                 tmp = mdef->mhp;
1254.             else
1255.                 mdef->m_lev--;
1256.             /* Automatic kill if drained past level 0 */
1257.         }
1258.         break;
1259.     case AD_SSEX:
1260.     case AD_SITM: /* for now these are the same */
1261.     case AD_SEDU:
1262.         if (magr->mcan)
1263.             break;
1264.         /* find an object to steal, non-cursed if magr is tame */
1265.         for (obj = mdef->minvent; obj; obj = obj->nobj)
1266.             if (!magr->mtame || !obj->cursed)
1267.                 break;
1268. 
1269.         if (obj) {
1270.             char onambuf[BUFSZ], mdefnambuf[BUFSZ];
1271. 
1272.             /* make a special x_monnam() call that never omits
1273.                the saddle, and save it for later messages */
1274.             Strcpy(mdefnambuf,
1275.                    x_monnam(mdef, ARTICLE_THE, (char *) 0, 0, FALSE));
1276. 
1277.             otmp = obj;
1278.             if (u.usteed == mdef && otmp == which_armor(mdef, W_SADDLE))
1279.                 /* "You can no longer ride <steed>." */
1280.                 dismount_steed(DISMOUNT_POLY);
1281.             obj_extract_self(otmp);
1282.             if (otmp->owornmask) {
1283.                 mdef->misc_worn_check &= ~otmp->owornmask;
1284.                 if (otmp->owornmask & W_WEP)
1285.                     mwepgone(mdef);
1286.                 otmp->owornmask = 0L;
1287.                 update_mon_intrinsics(mdef, otmp, FALSE, FALSE);
1288.                 /* give monster a chance to wear other equipment on its next
1289.                    move instead of waiting until it picks something up */
1290.                 mdef->misc_worn_check |= I_SPECIAL;
1291.             }
1292.             /* add_to_minv() might free otmp [if it merges] */
1293.             if (vis)
1294.                 Strcpy(onambuf, doname(otmp));
1295.             (void) add_to_minv(magr, otmp);
1296.             if (vis && canseemon(mdef)) {
1297.                 Strcpy(buf, Monnam(magr));
1298.                 pline("%s steals %s from %s!", buf, onambuf, mdefnambuf);
1299.             }
1300.             possibly_unwield(mdef, FALSE);
1301.             mdef->mstrategy &= ~STRAT_WAITFORU;
1302.             mselftouch(mdef, (const char *) 0, FALSE);
1303.             if (DEADMONSTER(mdef))
1304.                 return (MM_DEF_DIED
1305.                         | (grow_up(magr, mdef) ? 0 : MM_AGR_DIED));
1306.             if (pa->mlet == S_NYMPH && !tele_restrict(magr)) {
1307.                 boolean couldspot = canspotmon(magr);
1308. 
1309.                 (void) rloc(magr, TRUE);
1310.                 if (vis && couldspot && !canspotmon(magr))
1311.                     pline("%s suddenly disappears!", buf);
1312.             }
1313.         }
1314.         tmp = 0;
1315.         break;
1316.     case AD_DREN:
1317.         if (!cancelled && !rn2(4))
1318.             xdrainenergym(mdef, (boolean) (vis && canspotmon(mdef)
1319.                                            && mattk->aatyp != AT_ENGL));
1320.         tmp = 0;
1321.         break;
1322.     case AD_DRST:
1323.     case AD_DRDX:
1324.     case AD_DRCO:
1325.         if (!cancelled && !rn2(8)) {
1326.             if (vis && canspotmon(magr))
1327.                 pline("%s %s was poisoned!", s_suffix(Monnam(magr)),
1328.                       mpoisons_subj(magr, mattk));
1329.             if (resists_poison(mdef)) {
1330.                 if (vis && canspotmon(mdef) && canspotmon(magr))
1331.                     pline_The("poison doesn't seem to affect %s.",
1332.                               mon_nam(mdef));
1333.             } else {
1334.                 if (rn2(10))
1335.                     tmp += rn1(10, 6);
1336.                 else {
1337.                     if (vis && canspotmon(mdef))
1338.                         pline_The("poison was deadly...");
1339.                     tmp = mdef->mhp;
1340.                 }
1341.             }
1342.         }
1343.         break;
1344.     case AD_DRIN:
1345.         if (notonhead || !has_head(pd)) {
1346.             if (vis && canspotmon(mdef))
1347.                 pline("%s doesn't seem harmed.", Monnam(mdef));
1348.             /* Not clear what to do for green slimes */
1349.             tmp = 0;
1350.             break;
1351.         }
1352.         if ((mdef->misc_worn_check & W_ARMH) && rn2(8)) {
1353.             if (vis && canspotmon(magr) && canseemon(mdef)) {
1354.                 Strcpy(buf, s_suffix(Monnam(mdef)));
1355.                 pline("%s helmet blocks %s attack to %s head.", buf,
1356.                       s_suffix(mon_nam(magr)), mhis(mdef));
1357.             }
1358.             break;
1359.         }
1360.         res = eat_brains(magr, mdef, vis, &tmp);
1361.         break;
1362.     case AD_SLIM:
1363.         if (cancelled)
1364.             break; /* physical damage only */
1365.         if (!rn2(4) && !slimeproof(pd)) {
1366.             if (!munslime(mdef, FALSE) && !DEADMONSTER(mdef)) {
1367.                 if (newcham(mdef, &mons[PM_GREEN_SLIME], FALSE,
1368.                             (boolean) (vis && canseemon(mdef))))
1369.                     pd = mdef->data;
1370.                 mdef->mstrategy &= ~STRAT_WAITFORU;
1371.                 res = MM_HIT;
1372.             }
1373.             /* munslime attempt could have been fatal,
1374.                potentially to multiple monsters (SCR_FIRE) */
1375.             if (DEADMONSTER(magr))
1376.                 res |= MM_AGR_DIED;
1377.             if (DEADMONSTER(mdef))
1378.                 res |= MM_DEF_DIED;
1379.             tmp = 0;
1380.         }
1381.         break;
1382.     case AD_STCK:
1383.         if (cancelled)
1384.             tmp = 0;
1385.         break;
1386.     case AD_WRAP: /* monsters cannot grab one another, it's too hard */
1387.         if (magr->mcan)
1388.             tmp = 0;
1389.         break;
1390.     case AD_ENCH:
1391.         /* there's no msomearmor() function, so just do damage */
1392.         /* if (cancelled) break; */
1393.         break;
1394.     default:
1395.         tmp = 0;
1396.         break;
1397.     }
1398.     if (!tmp)
1399.         return res;
1400. 
1401.     if ((mdef->mhp -= tmp) < 1) {
1402.         if (m_at(mdef->mx, mdef->my) == magr) { /* see gulpmm() */
1403.             remove_monster(mdef->mx, mdef->my);
1404.             mdef->mhp = 1; /* otherwise place_monster will complain */
1405.             place_monster(mdef, mdef->mx, mdef->my);
1406.             mdef->mhp = 0;
1407.         }
1408.         monkilled(mdef, "", (int) mattk->adtyp);
1409.         if (!DEADMONSTER(mdef))
1410.             return res; /* mdef lifesaved */
1411.         else if (res == MM_AGR_DIED)
1412.             return (MM_DEF_DIED | MM_AGR_DIED);
1413. 
1414.         if (mattk->adtyp == AD_DGST) {
1415.             /* various checks similar to dog_eat and meatobj.
1416.              * after monkilled() to provide better message ordering */
1417.             if (mdef->cham >= LOW_PM) {
1418.                 (void) newcham(magr, (struct permonst *) 0, FALSE, TRUE);
1419.             } else if (pd == &mons[PM_GREEN_SLIME] && !slimeproof(pa)) {
1420.                 (void) newcham(magr, &mons[PM_GREEN_SLIME], FALSE, TRUE);
1421.             } else if (pd == &mons[PM_WRAITH]) {
1422.                 (void) grow_up(magr, (struct monst *) 0);
1423.                 /* don't grow up twice */
1424.                 return (MM_DEF_DIED | (!DEADMONSTER(magr) ? 0 : MM_AGR_DIED));
1425.             } else if (pd == &mons[PM_NURSE]) {
1426.                 magr->mhp = magr->mhpmax;
1427.             }
1428.         }
1429.         /* caveat: above digestion handling doesn't keep `pa' up to date */
1430. 
1431.         return (MM_DEF_DIED | (grow_up(magr, mdef) ? 0 : MM_AGR_DIED));
1432.     }
1433.     return (res == MM_AGR_DIED) ? MM_AGR_DIED : MM_HIT;
1434. }
1435. 
1436. void
1437. paralyze_monst(mon, amt)
1438. struct monst *mon;
1439. int amt;
1440. {
1441.     if (amt > 127)
1442.         amt = 127;
1443. 
1444.     mon->mcanmove = 0;
1445.     mon->mfrozen = amt;
1446.     mon->meating = 0; /* terminate any meal-in-progress */
1447.     mon->mstrategy &= ~STRAT_WAITFORU;
1448. }
1449. 
1450. /* `mon' is hit by a sleep attack; return 1 if it's affected, 0 otherwise */
1451. int
1452. sleep_monst(mon, amt, how)
1453. struct monst *mon;
1454. int amt, how;
1455. {
1456.     if (resists_sleep(mon)
1457.         || (how >= 0 && resist(mon, (char) how, 0, NOTELL))) {
1458.         shieldeff(mon->mx, mon->my);
1459.     } else if (mon->mcanmove) {
1460.         finish_meating(mon); /* terminate any meal-in-progress */
1461.         amt += (int) mon->mfrozen;
1462.         if (amt > 0) { /* sleep for N turns */
1463.             mon->mcanmove = 0;
1464.             mon->mfrozen = min(amt, 127);
1465.         } else { /* sleep until awakened */
1466.             mon->msleeping = 1;
1467.         }
1468.         return 1;
1469.     }
1470.     return 0;
1471. }
1472. 
1473. /* sleeping grabber releases, engulfer doesn't; don't use for paralysis! */
1474. void
1475. slept_monst(mon)
1476. struct monst *mon;
1477. {
1478.     if ((mon->msleeping || !mon->mcanmove) && mon == u.ustuck
1479.         && !sticks(youmonst.data) && !u.uswallow) {
1480.         pline("%s grip relaxes.", s_suffix(Monnam(mon)));
1481.         unstuck(mon);
1482.     }
1483. }
1484. 
1485. void
1486. rustm(mdef, obj)
1487. struct monst *mdef;
1488. struct obj *obj;
1489. {
1490.     int dmgtyp = -1, chance = 1;
1491. 
1492.     if (!mdef || !obj)
1493.         return; /* just in case */
1494.     /* AD_ACID and AD_ENCH are handled in passivemm() and passiveum() */
1495.     if (dmgtype(mdef->data, AD_CORR)) {
1496.         dmgtyp = ERODE_CORRODE;
1497.     } else if (dmgtype(mdef->data, AD_RUST)) {
1498.         dmgtyp = ERODE_RUST;
1499.     } else if (dmgtype(mdef->data, AD_FIRE)
1500.                /* steam vortex: fire resist applies, fire damage doesn't */
1501.                && mdef->data != &mons[PM_STEAM_VORTEX]) {
1502.         dmgtyp = ERODE_BURN;
1503.         chance = 6;
1504.     }
1505. 
1506.     if (dmgtyp >= 0 && !rn2(chance))
1507.         (void) erode_obj(obj, (char *) 0, dmgtyp, EF_GREASE | EF_VERBOSE);
1508. }
1509. 
1510. STATIC_OVL void
1511. mswingsm(magr, mdef, otemp)
1512. struct monst *magr, *mdef;
1513. struct obj *otemp;
1514. {
1515.     if (flags.verbose && !Blind && mon_visible(magr)) {
1516.         pline("%s %s %s%s %s at %s.", Monnam(magr),
1517.               (objects[otemp->otyp].oc_dir & PIERCE) ? "thrusts" : "swings",
1518.               (otemp->quan > 1L) ? "one of " : "", mhis(magr), xname(otemp),
1519.               mon_nam(mdef));
1520.     }
1521. }
1522. 
1523. /*
1524.  * Passive responses by defenders.  Does not replicate responses already
1525.  * handled above.  Returns same values as mattackm.
1526.  */
1527. STATIC_OVL int
1528. passivemm(magr, mdef, mhit, mdead)
1529. register struct monst *magr, *mdef;
1530. boolean mhit;
1531. int mdead;
1532. {
1533.     register struct permonst *mddat = mdef->data;
1534.     register struct permonst *madat = magr->data;
1535.     char buf[BUFSZ];
1536.     int i, tmp;
1537. 
1538.     for (i = 0;; i++) {
1539.         if (i >= NATTK)
1540.             return (mdead | mhit); /* no passive attacks */
1541.         if (mddat->mattk[i].aatyp == AT_NONE)
1542.             break;
1543.     }
1544.     if (mddat->mattk[i].damn)
1545.         tmp = d((int) mddat->mattk[i].damn, (int) mddat->mattk[i].damd);
1546.     else if (mddat->mattk[i].damd)
1547.         tmp = d((int) mddat->mlevel + 1, (int) mddat->mattk[i].damd);
1548.     else
1549.         tmp = 0;
1550. 
1551.     /* These affect the enemy even if defender killed */
1552.     switch (mddat->mattk[i].adtyp) {
1553.     case AD_ACID:
1554.         if (mhit && !rn2(2)) {
1555.             Strcpy(buf, Monnam(magr));
1556.             if (canseemon(magr))
1557.                 pline("%s is splashed by %s %s!", buf,
1558.                       s_suffix(mon_nam(mdef)), hliquid("acid"));
1559.             if (resists_acid(magr)) {
1560.                 if (canseemon(magr))
1561.                     pline("%s is not affected.", Monnam(magr));
1562.                 tmp = 0;
1563.             }
1564.         } else
1565.             tmp = 0;
1566.         if (!rn2(30))
1567.             erode_armor(magr, ERODE_CORRODE);
1568.         if (!rn2(6))
1569.             acid_damage(MON_WEP(magr));
1570.         goto assess_dmg;
1571.     case AD_ENCH: /* KMH -- remove enchantment (disenchanter) */
1572.         if (mhit && !mdef->mcan && otmp) {
1573.             (void) drain_item(otmp, FALSE);
1574.             /* No message */
1575.         }
1576.         break;
1577.     default:
1578.         break;
1579.     }
1580.     if (mdead || mdef->mcan)
1581.         return (mdead | mhit);
1582. 
1583.     /* These affect the enemy only if defender is still alive */
1584.     if (rn2(3))
1585.         switch (mddat->mattk[i].adtyp) {
1586.         case AD_PLYS: /* Floating eye */
1587.             if (tmp > 127)
1588.                 tmp = 127;
1589.             if (mddat == &mons[PM_FLOATING_EYE]) {
1590.                 if (!rn2(4))
1591.                     tmp = 127;
1592.                 if (magr->mcansee && haseyes(madat) && mdef->mcansee
1593.                     && (perceives(madat) || !mdef->minvis)) {
1594.                     /* construct format string; guard against '%' in Monnam */
1595.                     Strcpy(buf, s_suffix(Monnam(mdef)));
1596.                     (void) strNsubst(buf, "%", "%%", 0);
1597.                     Strcat(buf, " gaze is reflected by %s %s.");
1598.                     if (mon_reflects(magr,
1599.                                      canseemon(magr) ? buf : (char *) 0))
1600.                         return (mdead | mhit);
1601.                     Strcpy(buf, Monnam(magr));
1602.                     if (canseemon(magr))
1603.                         pline("%s is frozen by %s gaze!", buf,
1604.                               s_suffix(mon_nam(mdef)));
1605.                     paralyze_monst(magr, tmp);
1606.                     return (mdead | mhit);
1607.                 }
1608.             } else { /* gelatinous cube */
1609.                 Strcpy(buf, Monnam(magr));
1610.                 if (canseemon(magr))
1611.                     pline("%s is frozen by %s.", buf, mon_nam(mdef));
1612.                 paralyze_monst(magr, tmp);
1613.                 return (mdead | mhit);
1614.             }
1615.             return 1;
1616.         case AD_COLD:
1617.             if (resists_cold(magr)) {
1618.                 if (canseemon(magr)) {
1619.                     pline("%s is mildly chilly.", Monnam(magr));
1620.                     golemeffects(magr, AD_COLD, tmp);
1621.                 }
1622.                 tmp = 0;
1623.                 break;
1624.             }
1625.             if (canseemon(magr))
1626.                 pline("%s is suddenly very cold!", Monnam(magr));
1627.             mdef->mhp += tmp / 2;
1628.             if (mdef->mhpmax < mdef->mhp)
1629.                 mdef->mhpmax = mdef->mhp;
1630.             if (mdef->mhpmax > ((int) (mdef->m_lev + 1) * 8))
1631.                 (void) split_mon(mdef, magr);
1632.             break;
1633.         case AD_STUN:
1634.             if (!magr->mstun) {
1635.                 magr->mstun = 1;
1636.                 if (canseemon(magr))
1637.                     pline("%s %s...", Monnam(magr),
1638.                           makeplural(stagger(magr->data, "stagger")));
1639.             }
1640.             tmp = 0;
1641.             break;
1642.         case AD_FIRE:
1643.             if (resists_fire(magr)) {
1644.                 if (canseemon(magr)) {
1645.                     pline("%s is mildly warmed.", Monnam(magr));
1646.                     golemeffects(magr, AD_FIRE, tmp);
1647.                 }
1648.                 tmp = 0;
1649.                 break;
1650.             }
1651.             if (canseemon(magr))
1652.                 pline("%s is suddenly very hot!", Monnam(magr));
1653.             break;
1654.         case AD_ELEC:
1655.             if (resists_elec(magr)) {
1656.                 if (canseemon(magr)) {
1657.                     pline("%s is mildly tingled.", Monnam(magr));
1658.                     golemeffects(magr, AD_ELEC, tmp);
1659.                 }
1660.                 tmp = 0;
1661.                 break;
1662.             }
1663.             if (canseemon(magr))
1664.                 pline("%s is jolted with electricity!", Monnam(magr));
1665.             break;
1666.         default:
1667.             tmp = 0;
1668.             break;
1669.         }
1670.     else
1671.         tmp = 0;
1672. 
1673.  assess_dmg:
1674.     if ((magr->mhp -= tmp) <= 0) {
1675.         monkilled(magr, "", (int) mddat->mattk[i].adtyp);
1676.         return (mdead | mhit | MM_AGR_DIED);
1677.     }
1678.     return (mdead | mhit);
1679. }
1680. 
1681. /* hero or monster has successfully hit target mon with drain energy attack */
1682. void
1683. xdrainenergym(mon, givemsg)
1684. struct monst *mon;
1685. boolean givemsg;
1686. {
1687.     if (mon->mspec_used < 20 /* limit draining */
1688.         && (attacktype(mon->data, AT_MAGC)
1689.             || attacktype(mon->data, AT_BREA))) {
1690.         mon->mspec_used += d(2, 2);
1691.         if (givemsg)
1692.             pline("%s seems lethargic.", Monnam(mon));
1693.     }
1694. }
1695. 
1696. /* "aggressive defense"; what type of armor prevents specified attack
1697.    from touching its target? */
1698. long
1699. attk_protection(aatyp)
1700. int aatyp;
1701. {
1702.     long w_mask = 0L;
1703. 
1704.     switch (aatyp) {
1705.     case AT_NONE:
1706.     case AT_SPIT:
1707.     case AT_EXPL:
1708.     case AT_BOOM:
1709.     case AT_GAZE:
1710.     case AT_BREA:
1711.     case AT_MAGC:
1712.         w_mask = ~0L; /* special case; no defense needed */
1713.         break;
1714.     case AT_CLAW:
1715.     case AT_TUCH:
1716.     case AT_WEAP:
1717.         w_mask = W_ARMG; /* caller needs to check for weapon */
1718.         break;
1719.     case AT_KICK:
1720.         w_mask = W_ARMF;
1721.         break;
1722.     case AT_BUTT:
1723.         w_mask = W_ARMH;
1724.         break;
1725.     case AT_HUGS:
1726.         w_mask = (W_ARMC | W_ARMG); /* attacker needs both to be protected */
1727.         break;
1728.     case AT_BITE:
1729.     case AT_STNG:
1730.     case AT_ENGL:
1731.     case AT_TENT:
1732.     default:
1733.         w_mask = 0L; /* no defense available */
1734.         break;
1735.     }
1736.     return w_mask;
1737. }
1738. 
1739. /*mhitm.c*/