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