-
Notifications
You must be signed in to change notification settings - Fork 540
Expand file tree
/
Copy pathmhitm.c
More file actions
1514 lines (1387 loc) · 52.9 KB
/
mhitm.c
File metadata and controls
1514 lines (1387 loc) · 52.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* NetHack 3.7 mhitm.c $NHDT-Date: 1732979463 2024/11/30 07:11:03 $ $NHDT-Branch: NetHack-3.7 $:$NHDT-Revision: 1.253 $ */
/* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
/*-Copyright (c) Robert Patrick Rankin, 2011. */
/* NetHack may be freely redistributed. See license for details. */
#include "hack.h"
#include "artifact.h"
static const char brief_feeling[] =
"have a %s feeling for a moment, then it passes.";
staticfn void noises(struct monst *, struct attack *);
staticfn void pre_mm_attack(struct monst *, struct monst *);
staticfn void missmm(struct monst *, struct monst *, struct attack *);
staticfn int hitmm(struct monst *, struct monst *, struct attack *,
struct obj *, int);
staticfn int gazemm(struct monst *, struct monst *, struct attack *);
staticfn int gulpmm(struct monst *, struct monst *, struct attack *);
staticfn int explmm(struct monst *, struct monst *, struct attack *);
staticfn int mdamagem(struct monst *, struct monst *, struct attack *,
struct obj *, int);
staticfn void mswingsm(struct monst *, struct monst *, struct obj *);
staticfn int passivemm(struct monst *, struct monst *, boolean, int,
struct obj *);
staticfn void
noises(struct monst *magr, struct attack *mattk)
{
boolean farq = (mdistu(magr) > 15);
if (!Deaf && (farq != gf.far_noise || svm.moves - gn.noisetime > 10)) {
gf.far_noise = farq;
gn.noisetime = svm.moves;
You_hear("%s%s.",
(mattk->aatyp == AT_EXPL) ? "an explosion" : "some noises",
farq ? " in the distance" : "");
}
}
staticfn void
pre_mm_attack(struct monst *magr, struct monst *mdef)
{
boolean showit = FALSE;
/* unhiding or unmimicking happens even if hero can't see it
because the formerly concealed monster is now in action */
if (M_AP_TYPE(mdef)) {
seemimic(mdef);
showit |= gv.vis;
} else if (mdef->mundetected) {
mdef->mundetected = 0;
showit |= gv.vis;
}
if (M_AP_TYPE(magr)) {
seemimic(magr);
showit |= gv.vis;
} else if (magr->mundetected) {
magr->mundetected = 0;
showit |= gv.vis;
}
if (gv.vis) {
if (!canspotmon(magr))
map_invisible(magr->mx, magr->my);
else if (showit)
newsym(magr->mx, magr->my);
if (!canspotmon(mdef))
map_invisible(mdef->mx, mdef->my);
else if (showit)
newsym(mdef->mx, mdef->my);
}
}
/* feedback for when a monster-vs-monster attack misses */
staticfn void
missmm(
struct monst *magr, /* attacker */
struct monst *mdef, /* defender */
struct attack *mattk) /* attack and damage types */
{
pre_mm_attack(magr, mdef);
if (gv.vis) {
pline("%s %s %s.", Monnam(magr),
(magr->mcan || !could_seduce(magr, mdef, mattk)) ? "misses"
: "pretends to be friendly to",
mon_nam_too(mdef, magr));
} else {
noises(magr, mattk);
}
}
/*
* fightm() -- fight some other monster
*
* Returns:
* 0 - Monster did nothing.
* 1 - If the monster made an attack. The monster might have died.
*
* There is an exception to the above. If mtmp has the hero swallowed,
* then we report that the monster did nothing so it will continue to
* digest the hero.
*/
/* have monsters fight each other */
int
fightm(struct monst *mtmp)
{
struct monst *mon, *nmon;
int result, has_u_swallowed;
/* perhaps the monster will resist Conflict */
if (resist_conflict(mtmp))
return 0;
if (u.ustuck == mtmp) {
/* perhaps we're holding it... */
if (itsstuck(mtmp))
return 0;
}
has_u_swallowed = engulfing_u(mtmp);
for (mon = fmon; mon; mon = nmon) {
nmon = mon->nmon;
if (nmon == mtmp)
nmon = mtmp->nmon;
/* Be careful to ignore monsters that are already dead, since we
* might be calling this before we've cleaned them up. This can
* happen if the monster attacked a cockatrice bare-handedly, for
* instance.
*/
if (mon != mtmp && !DEADMONSTER(mon)) {
if (monnear(mtmp, mon->mx, mon->my)) {
if (!u.uswallow && (mtmp == u.ustuck)) {
if (!rn2(4)) {
set_ustuck((struct monst *) 0);
pline("%s releases you!", Monnam(mtmp));
} else
break;
}
/* mtmp can be killed */
gb.bhitpos.x = mon->mx, gb.bhitpos.y = mon->my;
gn.notonhead = FALSE;
result = mattackm(mtmp, mon);
if (result & M_ATTK_AGR_DIED)
return 1; /* mtmp died */
/*
* If mtmp has the hero swallowed, lie and say there
* was no attack (this allows mtmp to digest the hero).
*/
if (has_u_swallowed)
return 0;
/* allow attacked monsters a chance to hit back, primarily
to allow monsters that resist conflict to respond */
if ((result & (M_ATTK_HIT | M_ATTK_DEF_DIED)) == M_ATTK_HIT
&& rn2(4) && mon->movement > rn2(NORMAL_SPEED)) {
if (mon->movement > NORMAL_SPEED)
mon->movement -= NORMAL_SPEED;
else
mon->movement = 0;
gb.bhitpos.x = mtmp->mx, gb.bhitpos.y = mtmp->my;
gn.notonhead = FALSE;
(void) mattackm(mon, mtmp); /* return attack */
}
return (result & M_ATTK_HIT) ? 1 : 0;
}
}
}
return 0;
}
/*
* mdisplacem() -- attacker moves defender out of the way;
* returns same results as mattackm().
*/
int
mdisplacem(
struct monst *magr,
struct monst *mdef,
boolean quietly)
{
struct permonst *pa, *pd;
int tx, ty, fx, fy;
/* sanity checks; could matter if we unexpectedly get a long worm */
if (!magr || !mdef || magr == mdef)
return M_ATTK_MISS;
pa = magr->data, pd = mdef->data;
tx = mdef->mx, ty = mdef->my; /* destination */
fx = magr->mx, fy = magr->my; /* current location */
if (m_at(fx, fy) != magr || m_at(tx, ty) != mdef)
return M_ATTK_MISS;
/* The 1 in 7 failure below matches the chance in do_attack()
* for pet displacement.
*/
if (!rn2(7))
return M_ATTK_MISS;
/* Grid bugs cannot displace at an angle. */
if (pa == &mons[PM_GRID_BUG] && magr->mx != mdef->mx
&& magr->my != mdef->my)
return M_ATTK_MISS;
/* undetected monster becomes un-hidden if it is displaced */
if (mdef->mundetected)
mdef->mundetected = 0;
if (M_AP_TYPE(mdef) && M_AP_TYPE(mdef) != M_AP_MONSTER)
seemimic(mdef);
/* wake up the displaced defender */
mdef->msleeping = 0;
mdef->mstrategy &= ~STRAT_WAITMASK;
finish_meating(mdef);
/*
* Set up the visibility of action.
* You can observe monster displacement if you can see both of
* the monsters involved.
*/
gv.vis = (canspotmon(magr) && canspotmon(mdef));
if (touch_petrifies(pd) && !resists_ston(magr)) {
if (!which_armor(magr, W_ARMG)) {
if (poly_when_stoned(pa)) {
mon_to_stone(magr);
return M_ATTK_HIT; /* no damage during the polymorph */
}
if (!quietly && canspotmon(magr)) {
if (gv.vis) {
pline("%s tries to move %s out of %s way.", Monnam(magr),
mon_nam(mdef), is_rider(pa) ? "the" : mhis(magr));
}
pline_mon(magr, "%s turns to stone!", Monnam(magr));
}
monstone(magr);
if (!DEADMONSTER(magr))
return M_ATTK_HIT; /* lifesaved */
else if (magr->mtame && !gv.vis)
You(brief_feeling, "peculiarly sad");
return M_ATTK_AGR_DIED;
}
}
remove_monster(fx, fy); /* pick up from orig position */
if (mdef->wormno)
remove_worm(mdef);
else
remove_monster(tx, ty);
place_monster(magr, tx, ty); /* put down at target spot */
place_monster(mdef, fx, fy);
if (mdef->wormno) /* now put down tail */
place_worm_tail_randomly(mdef, fx, fy);
/* either creature might move into or out of a poison gas cloud */
update_monster_region(magr);
update_monster_region(mdef);
if (gv.vis && !quietly)
pline("%s moves %s out of %s way!", Monnam(magr), mon_nam(mdef),
is_rider(pa) ? "the" : mhis(magr));
newsym(fx, fy); /* see it */
newsym(tx, ty); /* all happen */
flush_screen(0); /* make sure it shows up */
return M_ATTK_HIT;
}
/*
* mattackm() -- a monster attacks another monster.
*
* --------- aggressor died
* / ------- defender died
* / / ----- defender was hit
* / / /
* x x x
*
* 0x8 M_ATTK_AGR_DONE
* 0x4 M_ATTK_AGR_DIED
* 0x2 M_ATTK_DEF_DIED
* 0x1 M_ATTK_HIT
* 0x0 M_ATTK_MISS
*
* Each successive attack has a lower probability of hitting. Some rely on
* success of previous attacks. ** this doesn't seem to be implemented -dl **
*
* Attacker has targeted <bhitpos.x,bhitpos.y> rather than
* <mdef->mx,mdef->my>; matters for long worms.
*
* In the case of exploding monsters, the monster dies as well.
*/
int
mattackm(
struct monst *magr,
struct monst *mdef)
{
int i, /* loop counter */
tmp, /* armor class difference */
strike = 0, /* hit this attack */
attk, /* attack attempted this time */
struck = 0, /* hit at least once */
res[NATTK], /* results of all attacks */
dieroll = 0;
struct attack *mattk, alt_attk;
struct obj *mwep;
struct permonst *pa, *pd;
if (!magr || !mdef)
return M_ATTK_MISS; /* mike@genat */
if (helpless(magr))
return M_ATTK_MISS;
pa = magr->data;
pd = mdef->data;
/* Grid bugs cannot attack at an angle. */
if (pa == &mons[PM_GRID_BUG] && magr->mx != mdef->mx
&& magr->my != mdef->my)
return M_ATTK_MISS;
/* Calculate the armour class differential. */
tmp = find_mac(mdef) + magr->m_lev;
if (mdef->mconf || helpless(mdef)) {
tmp += 4;
mdef->msleeping = 0;
}
/* mundetected monsters become un-hidden if they are attacked */
if (mdef->mundetected) {
mdef->mundetected = 0;
newsym(mdef->mx, mdef->my);
if (canseemon(mdef) && !sensemon(mdef)) {
if (Unaware) {
boolean justone = (mdef->data->geno & G_UNIQ) != 0L;
const char *montype;
montype = noname_monnam(mdef, justone ? ARTICLE_THE
: ARTICLE_NONE);
if (!justone)
montype = makeplural(montype);
You("dream of %s.", montype);
} else {
if (iflags.last_msg == PLNMSG_HIDE_UNDER
&& mdef->m_id == gl.last_hider)
pline_mon(mdef, "%s emerges from hiding.", Monnam(mdef));
else if (mdef->m_id == gl.last_hider)
You("notice %s.", mon_nam(mdef));
else
pline("Suddenly, you notice %s.", a_monnam(mdef));
}
}
}
/* Elves hate orcs. */
if (is_elf(pa) && is_orc(pd))
tmp++;
/* Set up the visibility of action */
gv.vis = ((cansee(magr->mx, magr->my) && canspotmon(magr))
|| (cansee(mdef->mx, mdef->my) && canspotmon(mdef)));
/* Set flag indicating monster has moved this turn. Necessary since a
* monster might get an attack out of sequence (i.e. before its move) in
* some cases, in which case this still counts as its move for the round
* and it shouldn't move again.
*/
magr->mlstmv = svm.moves;
/* controls whether a mind flayer uses all of its tentacle-for-DRIN
attacks; when fighting a headless monster, stop after the first
one because repeating the same failing hit (or even an ordinary
tentacle miss) is very verbose and makes the flayer look stupid */
gs.skipdrin = FALSE;
/* Now perform all attacks for the monster. */
for (i = 0; i < NATTK; i++) {
res[i] = M_ATTK_MISS;
/* target might no longer be there */
if (i > 0 && (m_at(gb.bhitpos.x, gb.bhitpos.y) != mdef
|| DEADMONSTER(magr) || DEADMONSTER(mdef)))
continue;
mattk = getmattk(magr, mdef, i, res, &alt_attk);
/* reduce verbosity for mind flayer attacking creature without a
head (or worm's tail); this is similar to monster with multiple
attacks after a wildmiss against displaced or invisible hero */
if (gs.skipdrin && mattk->aatyp == AT_TENT && mattk->adtyp == AD_DRIN)
continue;
mwep = (struct obj *) 0;
attk = 1;
switch (mattk->aatyp) {
case AT_WEAP: /* "hand to hand" attacks */
if (distmin(magr->mx, magr->my, mdef->mx, mdef->my) > 1) {
/* D: Do a ranged attack here! */
strike = (thrwmm(magr, mdef) == M_ATTK_MISS) ? 0 : 1;
if (strike)
/* don't really know if we hit or not; pretend we did */
res[i] |= M_ATTK_HIT;
if (DEADMONSTER(mdef))
res[i] = M_ATTK_DEF_DIED;
if (DEADMONSTER(magr))
res[i] |= M_ATTK_AGR_DIED;
break;
}
if (magr->weapon_check == NEED_WEAPON || !MON_WEP(magr)) {
magr->weapon_check = NEED_HTH_WEAPON;
if (mon_wield_item(magr) != 0)
return M_ATTK_MISS;
}
possibly_unwield(magr, FALSE);
if ((mwep = MON_WEP(magr)) != 0) {
if (gv.vis)
mswingsm(magr, mdef, mwep);
tmp += hitval(mwep, mdef);
}
FALLTHROUGH;
/*FALLTHRU*/
case AT_CLAW:
case AT_KICK:
case AT_BITE:
case AT_STNG:
case AT_TUCH:
case AT_BUTT:
case AT_TENT:
if (mattk->aatyp == AT_KICK && mtrapped_in_pit(magr))
continue;
/* Nymph that teleported away on first attack? */
if (distmin(magr->mx, magr->my, mdef->mx, mdef->my) > 1)
/* Continue because the monster may have a ranged attack. */
continue;
/* Monsters won't attack cockatrices physically if they
* have a weapon instead. This instinct doesn't work for
* players, or under conflict or confusion.
*/
if (!magr->mconf && !Conflict && mwep && mattk->aatyp != AT_WEAP
&& touch_petrifies(mdef->data)) {
strike = 0;
break;
}
dieroll = rnd(20 + i);
strike = (tmp > dieroll);
/* KMH -- don't accumulate to-hit bonuses */
if (mwep)
tmp -= hitval(mwep, mdef);
if (strike) {
/* for eel AT_TUCH+AD_WRAP attack: can't grab an unsolid
target; the unsolid test is redundant since failed_grab
checks it too, but is cheap and avoids calling failed_grab
for ordinary targets */
if (unsolid(mdef->data) && failed_grab(magr, mdef, mattk)) {
strike = 0;
break;
}
res[i] = hitmm(magr, mdef, mattk, mwep, dieroll);
if ((mdef->data == &mons[PM_BLACK_PUDDING]
|| mdef->data == &mons[PM_BROWN_PUDDING])
&& (mwep && (objects[mwep->otyp].oc_material == IRON
|| objects[mwep->otyp].oc_material == METAL))
&& mdef->mhp > 1 && !mdef->mcan) {
struct monst *mclone;
if ((mclone = clone_mon(mdef, 0, 0)) != 0) {
if (gv.vis && canspotmon(mdef))
pline("%s divides as %s hits it!",
Monnam(mdef), mon_nam(magr));
(void) mintrap(mclone, NO_TRAP_FLAGS);
if (DEADMONSTER(magr))
res[i] |= M_ATTK_AGR_DIED;
}
}
} else
missmm(magr, mdef, mattk);
break;
case AT_HUGS: /* automatic if prev two attacks succeed */
strike = (i >= 2 && res[i - 1] == M_ATTK_HIT
&& res[i - 2] == M_ATTK_HIT);
if (strike) {
/* note: monsters with hug attacks don't wear cloaks or gloves
so this doesn't need a special case for hugging a shade
while covered by blessed armor (which does damage but does
not achieve a successful hold); likewise, rope golems can't
wield weapons so ability to choke isn't affected by such */
if (failed_grab(magr, mdef, mattk))
strike = 0;
else
res[i] = hitmm(magr, mdef, mattk, (struct obj *) 0, 0);
}
break;
case AT_GAZE:
strike = 0;
res[i] = gazemm(magr, mdef, mattk);
break;
case AT_EXPL:
/* D: Prevent explosions from a distance */
if (distmin(magr->mx, magr->my, mdef->mx, mdef->my) > 1)
continue;
res[i] = explmm(magr, mdef, mattk);
if (res[i] == M_ATTK_MISS) { /* cancelled--no attack */
strike = 0;
attk = 0;
} else
strike = 1; /* automatic hit */
break;
case AT_ENGL:
if (mdef->data == &mons[PM_SHADE]) { /* no silver teeth... */
if (gv.vis)
pline("%s attempt to engulf %s is futile.",
s_suffix(Monnam(magr)), mon_nam(mdef));
strike = 0;
break;
}
if (u.usteed && mdef == u.usteed) {
strike = 0;
break;
}
/* D: Prevent engulf from a distance */
if (distmin(magr->mx, magr->my, mdef->mx, mdef->my) > 1)
continue;
/* Engulfing attacks are directed at the hero if possible. -dlc */
if (engulfing_u(magr)) {
strike = 0;
} else if ((strike = (tmp > rnd(20 + i))) != 0) {
if (failed_grab(magr, mdef, mattk))
strike = 0; /* purple worm can't swallow unsolid mons */
else
res[i] = gulpmm(magr, mdef, mattk);
} else {
missmm(magr, mdef, mattk);
}
break;
case AT_BREA:
case AT_SPIT:
/*
* Ranged attacks aren't allowed at point blank range.
*
* That impacts pet use of ranged attacks. It's rather arbitrary
* but various parts of the code assume it to be the case, not to
* mention a part of player tactics when fighting dragons.
*/
if (!monnear(magr, mdef->mx, mdef->my)) {
int mmtmp = ((mattk->aatyp == AT_BREA)
? breamm(magr, mattk, mdef)
: spitmm(magr, mattk, mdef));
strike = (mmtmp == M_ATTK_MISS) ? 0 : 1;
/* We don't really know if we hit or not; pretend we did. */
if (strike)
res[i] |= M_ATTK_HIT;
if (DEADMONSTER(mdef))
res[i] = M_ATTK_DEF_DIED;
if (DEADMONSTER(magr))
res[i] |= M_ATTK_AGR_DIED;
} else {
strike = 0;
attk = 0;
}
break;
default: /* no attack */
strike = 0;
attk = 0;
break;
}
if (attk && !(res[i] & M_ATTK_AGR_DIED)
&& distmin(magr->mx, magr->my, mdef->mx, mdef->my) <= 1)
res[i] = passivemm(magr, mdef, strike,
(res[i] & M_ATTK_DEF_DIED), mwep);
if (res[i] & M_ATTK_DEF_DIED)
return res[i];
if (res[i] & M_ATTK_AGR_DIED)
return res[i];
/* return if aggressor can no longer attack */
if ((res[i] & M_ATTK_AGR_DONE) || helpless(magr))
return res[i];
/* eg. defender was knocked into a level teleport trap */
if (mon_offmap(mdef))
return res[i];
if (res[i] & M_ATTK_HIT)
struck = 1; /* at least one hit */
} /* for (;i < NATTK;) loop */
return (struck ? M_ATTK_HIT : M_ATTK_MISS);
}
/* can't hold an unsolid target (ghosts, lights, vortices, most elementals)
or a long worm tail */
boolean
failed_grab(
struct monst *magr,
struct monst *mdef,
struct attack *mattk)
{
if ((unsolid(mdef->data) || gn.notonhead)
/* hug attack: most holders (owlbear, python, pit fiend, &c);
wrap damage: eel grabbing, trapper/lurker-above engulfing;
stick-to damage: mimic, lichen;
digestion damage: purple worm swallowing */
&& (mattk->aatyp == AT_HUGS || mattk->adtyp == AD_WRAP
|| mattk->adtyp == AD_STCK || mattk->adtyp == AD_DGST)) {
if ((gv.vis && canspotmon(mdef)) /* mon-vs-mon */
|| magr == &gy.youmonst || mdef == &gy.youmonst) {
char magrnam[BUFSZ], mdefnam[BUFSZ];
boolean tailmiss = gn.notonhead;
const char *verb = (mattk->adtyp == AD_DGST) ? "gulp"
: (mattk->adtyp == AD_STCK) ? "adhere"
: "grab";
/* beware of "Foo's grab passes through Bar's ghost";
mon_nam(x_monnam) calls s_suffix() for named ghosts and
s_suffix() uses a single static buffer; make copies of both
names to overcome that [note: comment predates 'tailmiss'] */
Strcpy(magrnam, (magr == &gy.youmonst) ? "Your"
: s_suffix(Monnam(magr)));
if (!tailmiss) {
Strcpy(mdefnam, (mdef == &gy.youmonst) ? "you"
: mon_nam(mdef));
} else {
/* hero poly'd into long worm can't grow tail
so no 'youmonst' handling is needed here */
Sprintf(mdefnam, "%s tail", s_suffix(some_mon_nam(mdef)));
}
/* unsolid grab misses are actually somewhat iffy--how come
ordinary attacks don't also pass right through? */
pline("%.99s %s attempt %s %.99s!", magrnam, verb,
!tailmiss ? "passes right through" : "fails to hold",
mdefnam);
}
return TRUE;
}
return FALSE;
}
/* Returns the result of mdamagem(). */
staticfn int
hitmm(
struct monst *magr,
struct monst *mdef,
struct attack *mattk,
struct obj *mwep,
int dieroll)
{
int compat;
boolean weaponhit = (mattk->aatyp == AT_WEAP
|| (mattk->aatyp == AT_CLAW && mwep)),
silverhit = (weaponhit && mwep
&& objects[mwep->otyp].oc_material == SILVER);
pre_mm_attack(magr, mdef);
compat = !magr->mcan ? could_seduce(magr, mdef, mattk) : 0;
if (!compat && shade_miss(magr, mdef, mwep, FALSE, gv.vis))
return M_ATTK_MISS; /* bypass mdamagem() */
if (gv.vis) {
char buf[BUFSZ], magr_name[BUFSZ];
Strcpy(magr_name, Monnam(magr));
if (compat) {
Snprintf(buf, sizeof buf, "%s %s", magr_name,
mdef->mcansee ? "smiles at" : "talks to");
pline("%s %s %s.", buf, mon_nam(mdef),
(compat == 2) ? "engagingly" : "seductively");
} else {
buf[0] = '\0';
switch (mattk->aatyp) {
case AT_BITE:
Snprintf(buf, sizeof buf, "%s bites", magr_name);
break;
case AT_STNG:
Snprintf(buf, sizeof buf, "%s stings", magr_name);
break;
case AT_BUTT:
Snprintf(buf, sizeof buf, "%s butts", magr_name);
break;
case AT_TUCH:
Snprintf(buf, sizeof buf, "%s touches", magr_name);
break;
case AT_TENT:
Snprintf(buf, sizeof buf, "%s tentacles suck",
s_suffix(magr_name));
break;
case AT_HUGS:
if (magr != u.ustuck) {
Snprintf(buf, sizeof buf, "%s squeezes", magr_name);
break;
}
FALLTHROUGH;
/*FALLTHRU*/
default:
if (!weaponhit || !mwep || !mwep->oartifact)
Snprintf(buf, sizeof buf, "%s hits", magr_name);
break;
}
if (*buf)
pline("%s %s.", buf, mon_nam_too(mdef, magr));
if (mon_hates_silver(mdef) && silverhit) {
char *mdef_name = mon_nam_too(mdef, magr);
/* note: mon_nam_too returns a modifiable buffer; so
does s_suffix, but it returns a single static buffer
and we might be calling it twice for this message */
Strcpy(magr_name, s_suffix(magr_name));
if (!noncorporeal(mdef->data) && !amorphous(mdef->data)) {
if (mdef != magr) {
mdef_name = s_suffix(mdef_name);
} else {
(void) strsubst(mdef_name, "himself", "his own");
(void) strsubst(mdef_name, "herself", "her own");
(void) strsubst(mdef_name, "itself", "its own");
}
Strcat(mdef_name, " flesh");
}
pline("%s %s sears %s!", magr_name, /* s_suffix(magr_name), */
simpleonames(mwep), mdef_name);
}
}
} else
noises(magr, mattk);
return mdamagem(magr, mdef, mattk, mwep, dieroll);
}
/* Returns the same values as mdamagem(). */
staticfn int
gazemm(struct monst *magr, struct monst *mdef, struct attack *mattk)
{
char buf[BUFSZ];
/* an Archon's gaze affects target even if Archon itself is blinded */
boolean archon = (magr->data == &mons[PM_ARCHON]
&& mattk->adtyp == AD_BLND),
altmesg = (archon && !magr->mcansee);
/* bring target out of hiding even if hero doesn't see it happen (this
is already done in pre_mm_attack() and shouldn't be needed here) */
if (mdef->data->mlet == S_MIMIC && M_AP_TYPE(mdef) != M_AP_NOTHING)
seemimic(mdef);
mdef->mundetected = 0;
if (gv.vis) {
Sprintf(buf, "%s gazes %s",
altmesg ? Adjmonnam(magr, "blinded") : Monnam(magr),
altmesg ? "toward" : "at");
pline("%s %s...", buf,
canspotmon(mdef) ? mon_nam(mdef) : "something");
}
if (magr->mcan || !mdef->mcansee
|| (archon ? resists_blnd(mdef) : !magr->mcansee)
|| (magr->minvis && !perceives(mdef->data)) || mdef->msleeping) {
if (gv.vis && canspotmon(mdef))
pline("but nothing happens.");
return M_ATTK_MISS;
}
/* call mon_reflects 2x, first test, then, if visible, print message */
if (magr->data == &mons[PM_MEDUSA] && mon_reflects(mdef, (char *) 0)) {
if (canseemon(mdef))
(void) mon_reflects(mdef, "The gaze is reflected away by %s %s.");
if (mdef->mcansee) {
if (mon_reflects(magr, (char *) 0)) {
if (canseemon(magr))
(void) mon_reflects(magr,
"The gaze is reflected away by %s %s.");
return M_ATTK_MISS;
}
if (mdef->minvis && !perceives(magr->data)) {
if (canseemon(magr)) {
pline(
"%s doesn't seem to notice that %s gaze was reflected.",
Monnam(magr), mhis(magr));
}
return M_ATTK_MISS;
}
if (canseemon(magr))
pline_mon(magr, "%s is turned to stone!", Monnam(magr));
monstone(magr);
if (!DEADMONSTER(magr))
return M_ATTK_MISS;
return M_ATTK_AGR_DIED;
}
} else if (archon) {
mhitm_ad_blnd(magr, mattk, mdef, (struct mhitm_data *) 0);
/* an Archon's blinding radiance also stuns;
this is different from the way the hero gets stunned because
a stunned monster recovers randomly instead of via countdown;
both cases make an effort to prevent the target from being
continuously stunned due to repeated gaze attacks */
if (rn2(2))
mdef->mstun = 1;
}
return mdamagem(magr, mdef, mattk, (struct obj *) 0, 0);
}
/* return True if magr is allowed to swallow mdef, False otherwise */
boolean
engulf_target(struct monst *magr, struct monst *mdef)
{
struct rm *lev;
int ax, ay, dx, dy;
boolean uatk = (magr == &gy.youmonst),
udef = (mdef == &gy.youmonst);
/* can't swallow something that's too big */
if (mdef->data->msize >= MZ_HUGE
|| (magr->data->msize < mdef->data->msize && !is_whirly(magr->data)))
return FALSE;
/* can't (move to) swallow if trapped. TODO: could do some? */
if (mdef->mtrapped || magr->mtrapped)
return FALSE;
/* if attacker is phasing in solid rock and defender can't move there,
or vice versa, don't allow engulf to succeed; otherwise expelling
might not be able to place attacker and defender both back on map;
when defender is the hero, a sanity_check complaint about placing
the hero on top of a monster can occur */
dx = (mdef == &gy.youmonst) ? u.ux : mdef->mx;
dy = (mdef == &gy.youmonst) ? u.uy : mdef->my;
lev = &levl[dx][dy];
if (!(udef ? Passes_walls : passes_walls(mdef->data))
&& (IS_OBSTRUCTED(lev->typ) || closed_door(dx, dy) || IS_TREE(lev->typ)
/* not passes_bars(); engulfer isn't squeezing through */
|| (lev->typ == IRONBARS && !is_whirly(magr->data))))
return FALSE;
ax = (magr == &gy.youmonst) ? u.ux : magr->mx;
ay = (magr == &gy.youmonst) ? u.uy : magr->my;
lev = &levl[ax][ay];
if (!(uatk ? Passes_walls : passes_walls(magr->data))
&& (IS_OBSTRUCTED(lev->typ) || closed_door(ax, ay) || IS_TREE(lev->typ)
|| (lev->typ == IRONBARS && !is_whirly(mdef->data))))
return FALSE;
return TRUE;
}
/* Returns the same values as mattackm(). */
staticfn int
gulpmm(
struct monst *magr,
struct monst *mdef,
struct attack *mattk)
{
coordxy ax, ay, dx, dy;
int status;
struct obj *obj;
if (!engulf_target(magr, mdef))
return M_ATTK_MISS;
if (gv.vis) {
pline("%s %s %s.", Monnam(magr),
digests(magr->data) ? "swallows"
: enfolds(magr->data) ? "encloses"
: "engulfs",
mon_nam(mdef));
}
if (!flaming(magr->data)) {
for (obj = mdef->minvent; obj; obj = obj->nobj)
(void) snuff_lit(obj);
}
if (is_vampshifter(mdef)
&& newcham(mdef, &mons[mdef->cham], NO_NC_FLAGS)) {
if (gv.vis) {
/* 'it' -- previous form is no longer available and
using that would be excessively verbose */
pline("%s expels %s.", Monnam(magr),
canspotmon(mdef) ? "it" : something);
if (canspotmon(mdef)) {
pline("It turns into %s.",
x_monnam(mdef, ARTICLE_A, (char *) 0,
(SUPPRESS_NAME | SUPPRESS_IT
| SUPPRESS_INVISIBLE), FALSE));
}
}
return M_ATTK_HIT; /* bypass mdamagem() */
}
/*
* All of this manipulation is needed to keep the display correct.
* There is a flush at the next pline().
*/
ax = magr->mx;
ay = magr->my;
dx = mdef->mx;
dy = mdef->my;
/*
* Leave the defender in the monster chain at its current position,
* but don't leave it on the screen. Move the aggressor to the
* defender's position.
*/
remove_monster(dx, dy);
remove_monster(ax, ay);
place_monster(magr, dx, dy);
newsym(ax, ay); /* erase old position */
newsym(dx, dy); /* update new position */
gm.mswallower = magr; /* corpse_chance() wants this */
status = mdamagem(magr, mdef, mattk, (struct obj *) 0, 0);
gm.mswallower = (struct monst *) 0; /* reset */
if ((status & (M_ATTK_AGR_DIED | M_ATTK_DEF_DIED))
== (M_ATTK_AGR_DIED | M_ATTK_DEF_DIED)) {
; /* both died -- do nothing */
} else if (status & M_ATTK_DEF_DIED) { /* defender died */
/*
* Note: mdamagem() -> monkilled() -> mondead() -> m_detach()
* -> relmon() used to call remove_monster() for the dead
* monster even when it wasn't the one on the map, so we
* needed to put magr back after mdef was killed and removed
* from their shared spot. But now [3.7] relmon() calls
* mon_leaving_level() and that checks whether the monster at
* dying monster's coordinates is that dying monster and only
* removes it when they match. So magr is still at mdef's
* former spot these days.
*
* We still potentially do one fixup: if the gulp targeted
* an inhospitable location, magr will return to its previous
* spot instead of staying.
*/
if (!goodpos(dx, dy, magr, MM_IGNOREWATER)) {
if (m_at(dx, dy) == magr) {
remove_monster(dx, dy);
newsym(dx, dy);
}
dx = ax, dy = ay; /* magr's spot at start of the attack */
}
if (m_at(dx, dy) != magr) {
place_monster(magr, dx, dy);
newsym(dx, dy);
}
/* aggressor moves to <dx,dy> and might encounter trouble there */
if (minliquid(magr)
|| (t_at(dx, dy)
&& mintrap(magr, NO_TRAP_FLAGS) == Trap_Killed_Mon))
status |= M_ATTK_AGR_DIED;
} else if (status & M_ATTK_AGR_DIED) { /* aggressor died */
place_monster(mdef, dx, dy);
newsym(dx, dy);
} else { /* both alive, put them back */
if (cansee(dx, dy)) {
pline("%s is %s!", Monnam(mdef),
digests(magr->data) ? "regurgitated"
: enfolds(magr->data) ? "released"
: "expelled");
}
remove_monster(dx,dy);
place_monster(magr, ax, ay);
place_monster(mdef, dx, dy);
newsym(ax, ay);
newsym(dx, dy);
}
return status;
}
staticfn int
explmm(struct monst *magr, struct monst *mdef, struct attack *mattk)
{
int result;
if (magr->mcan)
return M_ATTK_MISS;
if (cansee(magr->mx, magr->my))
pline_mon(magr, "%s explodes!", Monnam(magr));
else
noises(magr, mattk);
/* monster explosion types which actually create an explosion */
if (mattk->adtyp == AD_FIRE || mattk->adtyp == AD_COLD
|| mattk->adtyp == AD_ELEC) {
mon_explodes(magr, mattk);
/* unconditionally set AGR_DIED here; lifesaving is accounted below */
result = M_ATTK_AGR_DIED | (DEADMONSTER(mdef) ? M_ATTK_DEF_DIED : 0);
} else {
result = mdamagem(magr, mdef, mattk, (struct obj *) 0, 0);
}
/* Kill off aggressor if it didn't die. */
if (!(result & M_ATTK_AGR_DIED)) {
boolean was_leashed = (magr->mleashed != 0);
mondead(magr);
if (!DEADMONSTER(magr))
return result; /* life saved */
result |= M_ATTK_AGR_DIED;