/
mon.c
5267 lines (4805 loc) · 182 KB
/
mon.c
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 mon.c $NHDT-Date: 1681429657 2023/04/13 23:47:37 $ $NHDT-Branch: NetHack-3.7 $:$NHDT-Revision: 1.495 $ */
/* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
/*-Copyright (c) Derek S. Ray, 2015. */
/* NetHack may be freely redistributed. See license for details. */
#include "hack.h"
#include "mfndpos.h"
#include <ctype.h>
static void sanity_check_single_mon(struct monst *, boolean, const char *);
static struct obj *make_corpse(struct monst *, unsigned);
static int minliquid_core(struct monst *);
static void m_calcdistress(struct monst *);
static boolean monlineu(struct monst *, int, int);
static long mm_2way_aggression(struct monst *, struct monst *);
static long mm_aggression(struct monst *, struct monst *);
static long mm_displacement(struct monst *, struct monst *);
static void mon_leaving_level(struct monst *);
static void m_detach(struct monst *, struct permonst *);
static void set_mon_min_mhpmax(struct monst *, int);
static void lifesaved_monster(struct monst *);
static boolean ok_to_obliterate(struct monst *);
static void qst_guardians_respond(void);
static void peacefuls_respond(struct monst *);
static void m_restartcham(struct monst *);
static boolean restrap(struct monst *);
static int pick_animal(void);
static int pickvampshape(struct monst *);
static boolean isspecmon(struct monst *);
static boolean validspecmon(struct monst *, int);
static struct permonst *accept_newcham_form(struct monst *, int);
static void kill_eggs(struct obj *);
static void pacify_guard(struct monst *);
#define LEVEL_SPECIFIC_NOCORPSE(mdat) \
(Is_rogue_level(&u.uz) \
|| !gl.level.flags.deathdrops \
|| (gl.level.flags.graveyard && is_undead(mdat) && rn2(3)))
/* A specific combination of x_monnam flags for livelogging. The livelog
* shouldn't show that you killed a hallucinatory monster and not what it
* actually is. */
#define livelog_mon_nam(mtmp) \
x_monnam(mtmp, ARTICLE_THE, (char *) 0, \
(SUPPRESS_IT | SUPPRESS_HALLUCINATION), FALSE)
#if 0
/* part of the original warning code which was replaced in 3.3.1 */
const char *warnings[] = {
"white", "pink", "red", "ruby", "purple", "black"
};
#endif /* 0 */
static void
sanity_check_single_mon(
struct monst *mtmp,
boolean chk_geno,
const char *msg)
{
struct permonst *mptr = mtmp->data;
coordxy mx = mtmp->mx, my = mtmp->my;
if (!mptr || mptr < &mons[LOW_PM] || mptr >= &mons[NUMMONS]) {
/* most sanity checks issue warnings if they detect a problem,
but this would be too extreme to keep going */
panic("illegal mon data %s; mnum=%d (%s)",
fmt_ptr((genericptr_t) mptr), mtmp->mnum, msg);
} else {
int mndx = monsndx(mptr);
if (mtmp->mnum != mndx) {
impossible("monster mnum=%d, monsndx=%d (%s)",
mtmp->mnum, mndx, msg);
mtmp->mnum = mndx;
}
#if 0 /*
* Gremlins don't obey the (mhpmax >= m_lev) rule so disable
* this check, at least for the time being. We could skip it
* when the cloned flag is set, but the original gremlin would
* still be an issue.
*/
/* check before DEADMONSTER() because dead monsters should still
have sane mhpmax */
if (mtmp->mhpmax < 1
|| mtmp->mhpmax < (int) mtmp->m_lev
|| mtmp->mhp > mtmp->mhpmax)
impossible(
"%s: level %d monster #%u [%s] has %d cur HP, %d max HP",
msg, (int) mtmp->m_lev,
mtmp->m_id, fmt_ptr((genericptr_t) mtmp),
mtmp->mhp, mtmp->mhpmax);
#endif
if (DEADMONSTER(mtmp)) {
#if 0
/* bad if not fmon list or if not vault guard */
if (strcmp(msg, "fmon") || !mtmp->isgd)
impossible("dead monster on %s; %s at <%d,%d>",
msg, mons[mndx].pmnames[NEUTRAL],
mx, my);
#endif
return;
}
if (chk_geno && (gm.mvitals[mndx].mvflags & G_GENOD) != 0)
impossible("genocided %s in play (%s)",
pmname(&mons[mndx], Mgender(mtmp)), msg);
if (mtmp->mtame && !mtmp->mpeaceful)
impossible("tame %s is not peaceful (%s)",
pmname(&mons[mndx], Mgender(mtmp)), msg);
}
if (mtmp->isshk && !has_eshk(mtmp))
impossible("shk without eshk (%s)", msg);
if (mtmp->ispriest && !has_epri(mtmp))
impossible("priest without epri (%s)", msg);
if (mtmp->isgd && !has_egd(mtmp))
impossible("guard without egd (%s)", msg);
if (mtmp->isminion && !has_emin(mtmp))
impossible("minion without emin (%s)", msg);
/* guardian angel on astral level is tame but has emin rather than edog */
if (mtmp->mtame && !has_edog(mtmp) && !mtmp->isminion)
impossible("pet without edog (%s)", msg);
/* steed should be tame and saddled */
if (mtmp == u.usteed) {
const char *ns, *nt = !mtmp->mtame ? "not tame" : 0;
ns = !m_carrying(mtmp, SADDLE) ? "no saddle"
: !which_armor(mtmp, W_SADDLE) ? "saddle not worn"
: 0;
if (ns || nt)
impossible("steed: %s%s%s (%s)",
ns ? ns : "", (ns && nt) ? ", " : "", nt ? nt : "",
msg);
}
if (mtmp->mtrapped) {
if (mtmp->wormno) {
/* TODO: how to check worm in trap? */
} else if (!t_at(mx, my))
impossible("trapped without a trap (%s)", msg);
}
/* monster is hiding? */
if (mtmp->mundetected) {
struct trap *t;
if (!isok(mx, my)) /* caller will have checked this but not fixed it */
mx = my = 0;
if (mtmp == u.ustuck)
impossible("hiding monster stuck to you (%s)", msg);
if (m_at(mx, my) == mtmp && hides_under(mptr) && !OBJ_AT(mx, my))
impossible("mon hiding under nonexistent obj (%s)", msg);
if (mptr->mlet == S_EEL
&& !is_pool(mx, my) && !Is_waterlevel(&u.uz))
impossible("eel hiding out of water (%s)", msg);
if (ceiling_hider(mptr)
/* normally !accessible would be overridable with passes_walls,
but not for hiding on the ceiling */
&& (!has_ceiling(&u.uz) ||
!(levl[mx][my].typ == POOL
|| levl[mx][my].typ == MOAT
|| levl[mx][my].typ == LAVAPOOL
|| accessible(mx, my))))
impossible("ceiling hider hiding %s (%s)",
!has_ceiling(&u.uz) ? "without ceiling"
: "in solid stone",
msg);
if (mtmp->mtrapped && (t = t_at(mx, my)) != 0 && !is_pit(t->ttyp))
impossible("hiding while trapped in a non-pit (%s)", msg);
} else if (M_AP_TYPE(mtmp) != M_AP_NOTHING) {
boolean is_mimic = (mptr->mlet == S_MIMIC);
const char *what = (M_AP_TYPE(mtmp) == M_AP_FURNITURE) ? "furniture"
: (M_AP_TYPE(mtmp) == M_AP_MONSTER) ? "a monster"
: (M_AP_TYPE(mtmp) == M_AP_OBJECT) ? "an object"
: "something strange";
if (Protection_from_shape_changers)
impossible(
"mimic%s concealed as %s despite Prot-from-shape-changers %s",
is_mimic ? "" : "ker", what, msg);
/* the Wizard's clone after "double trouble" starts out mimicking
some other monster; pet's quickmimic effect can temporarily take
on furniture, object, or monster shape, but only until the pet
finishes eating a mimic corpse */
if (!(is_mimic || mtmp->meating
|| (mtmp->iswiz && M_AP_TYPE(mtmp) == M_AP_MONSTER)))
impossible("non-mimic (%s) posing as %s (%s)",
mptr->pmnames[NEUTRAL], what, msg);
#if 0 /* mimics who end up in strange locations do still hide while there */
if (!(accessible(mx, my) || passes_walls(mptr))) {
char buf[BUFSZ];
const char *typnam = levltyp_to_name(levl[mx][my].typ);
if (!typnam) {
Sprintf(buf, "[%d]", levl[mx][my].typ);
typnam = buf;
}
impossible("mimic%s concealed in inaccessible location: %s (%s)",
is_mimic ? "" : "ker", typnam, msg);
}
#endif
}
}
void
mon_sanity_check(void)
{
coordxy x, y;
struct monst *mtmp, *m;
for (mtmp = fmon; mtmp; mtmp = mtmp->nmon) {
/* dead monsters should still have sane data */
sanity_check_single_mon(mtmp, TRUE, "fmon");
if (DEADMONSTER(mtmp) && !mtmp->isgd)
continue;
x = mtmp->mx, y = mtmp->my;
if (!isok(x, y) && !(mtmp->isgd && x == 0 && y == 0)) {
impossible("mon (%s) claims to be at <%d,%d>?",
fmt_ptr((genericptr_t) mtmp), x, y);
} else if (mtmp == u.usteed) {
/* steed is in fmon list but not on the map; its
<mx,my> coordinates should match hero's location */
if (x != u.ux || y != u.uy)
impossible("steed (%s) claims to be at <%d,%d>?",
fmt_ptr((genericptr_t) mtmp), x, y);
} else if (gl.level.monsters[x][y] != mtmp) {
impossible("mon (%s) at <%d,%d> is not there!",
fmt_ptr((genericptr_t) mtmp), x, y);
} else if (mtmp->wormno) {
sanity_check_worm(mtmp);
}
}
for (x = 1; x < COLNO; x++)
for (y = 0; y < ROWNO; y++)
if ((mtmp = gl.level.monsters[x][y]) != 0) {
for (m = fmon; m; m = m->nmon)
if (m == mtmp)
break;
if (!m)
impossible("map mon (%s) at <%d,%d> not in fmon list!",
fmt_ptr((genericptr_t) mtmp), x, y);
else if (mtmp == u.usteed)
impossible("steed (%s) is on the map at <%d,%d>!",
fmt_ptr((genericptr_t) mtmp), x, y);
else if ((mtmp->mx != x || mtmp->my != y)
&& mtmp->data != &mons[PM_LONG_WORM])
impossible("map mon (%s) at <%d,%d> is found at <%d,%d>?",
fmt_ptr((genericptr_t) mtmp),
mtmp->mx, mtmp->my, x, y);
}
for (mtmp = gm.migrating_mons; mtmp; mtmp = mtmp->nmon) {
sanity_check_single_mon(mtmp, FALSE, "migr");
}
wormno_sanity_check(); /* test for bogus worm tail */
}
/* Would monster be OK with poison gas? */
/* Does not check for actual poison gas at the location. */
/* Returns one of M_POISONGAS_foo */
int
m_poisongas_ok(struct monst* mtmp)
{
int px, py;
boolean is_you = (mtmp == &gy.youmonst);
/* Non living, non breathing, immune monsters are not concerned */
if (nonliving(mtmp->data) || is_vampshifter(mtmp)
|| breathless(mtmp->data) || immune_poisongas(mtmp->data))
return M_POISONGAS_OK;
/* not is_swimmer(); assume that non-fish are swimming on
the surface and breathing the air above it periodically
unless located at water spot on plane of water */
px = is_you ? u.ux : mtmp->mx;
py = is_you ? u.uy : mtmp->my;
if ((mtmp->data->mlet == S_EEL || Is_waterlevel(&u.uz))
&& is_pool(px, py))
return M_POISONGAS_OK;
/* exclude monsters with poison gas breath attack:
adult green dragon and Chromatic Dragon (and iron golem,
but nonliving() and breathless() tests also catch that) */
if (attacktype_fordmg(mtmp->data, AT_BREA, AD_DRST)
|| attacktype_fordmg(mtmp->data, AT_BREA, AD_RBRE))
return M_POISONGAS_OK;
if (is_you && (u.uinvulnerable || Breathless || Underwater))
return M_POISONGAS_OK;
if (is_you ? Poison_resistance : resists_poison(mtmp))
return M_POISONGAS_MINOR;
return M_POISONGAS_BAD;
}
/* return True if mon is capable of converting other monsters into zombies */
boolean
zombie_maker(struct monst *mon)
{
struct permonst *pm = mon->data;
if (mon->mcan)
return FALSE;
switch (pm->mlet) {
case S_ZOMBIE:
/* Z-class monsters that aren't actually zombies go here */
if (pm == &mons[PM_GHOUL] || pm == &mons[PM_SKELETON])
return FALSE;
return TRUE;
case S_LICH:
/* all liches will create zombies as well */
return TRUE;
}
return FALSE;
}
/* Return monster index of zombie monster which this monster could
be turned into, or NON_PM if it doesn't have a direct counterpart.
Sort of the zombie-specific inverse of undead_to_corpse. */
int
zombie_form(struct permonst *pm)
{
switch (pm->mlet) {
case S_ZOMBIE: /* when already a zombie/ghoul/skeleton, will stay as is */
return NON_PM;
case S_KOBOLD:
return PM_KOBOLD_ZOMBIE;
case S_ORC:
return PM_ORC_ZOMBIE;
case S_GIANT:
if (pm == &mons[PM_ETTIN])
return PM_ETTIN_ZOMBIE;
return PM_GIANT_ZOMBIE;
case S_HUMAN:
case S_KOP:
if (is_elf(pm))
return PM_ELF_ZOMBIE;
return PM_HUMAN_ZOMBIE;
case S_HUMANOID:
if (is_dwarf(pm))
return PM_DWARF_ZOMBIE;
else
break;
case S_GNOME:
return PM_GNOME_ZOMBIE;
}
return NON_PM;
}
/* convert the monster index of an undead to its living counterpart */
int
undead_to_corpse(int mndx)
{
switch (mndx) {
case PM_KOBOLD_ZOMBIE:
case PM_KOBOLD_MUMMY:
mndx = PM_KOBOLD;
break;
case PM_DWARF_ZOMBIE:
case PM_DWARF_MUMMY:
mndx = PM_DWARF;
break;
case PM_GNOME_ZOMBIE:
case PM_GNOME_MUMMY:
mndx = PM_GNOME;
break;
case PM_ORC_ZOMBIE:
case PM_ORC_MUMMY:
mndx = PM_ORC;
break;
case PM_ELF_ZOMBIE:
case PM_ELF_MUMMY:
mndx = PM_ELF;
break;
case PM_VAMPIRE:
case PM_VAMPIRE_LEADER:
#if 0 /* DEFERRED */
case PM_VAMPIRE_MAGE:
#endif
case PM_HUMAN_ZOMBIE:
case PM_HUMAN_MUMMY:
mndx = PM_HUMAN;
break;
case PM_GIANT_ZOMBIE:
case PM_GIANT_MUMMY:
mndx = PM_GIANT;
break;
case PM_ETTIN_ZOMBIE:
case PM_ETTIN_MUMMY:
mndx = PM_ETTIN;
break;
default:
break;
}
return mndx;
}
/* Convert the monster index of some monsters (such as quest guardians)
* to their generic species type.
*
* Return associated character class monster, rather than species
* if mode is 1.
*/
int
genus(int mndx, int mode)
{
switch (mndx) {
/* Quest guardians */
case PM_STUDENT:
mndx = mode ? PM_ARCHEOLOGIST : PM_HUMAN;
break;
case PM_CHIEFTAIN:
mndx = mode ? PM_BARBARIAN : PM_HUMAN;
break;
case PM_NEANDERTHAL:
mndx = mode ? PM_CAVE_DWELLER : PM_HUMAN;
break;
case PM_ATTENDANT:
mndx = mode ? PM_HEALER : PM_HUMAN;
break;
case PM_PAGE:
mndx = mode ? PM_KNIGHT : PM_HUMAN;
break;
case PM_ABBOT:
mndx = mode ? PM_MONK : PM_HUMAN;
break;
case PM_ACOLYTE:
mndx = mode ? PM_CLERIC : PM_HUMAN;
break;
case PM_HUNTER:
mndx = mode ? PM_RANGER : PM_HUMAN;
break;
case PM_THUG:
mndx = mode ? PM_ROGUE : PM_HUMAN;
break;
case PM_ROSHI:
mndx = mode ? PM_SAMURAI : PM_HUMAN;
break;
case PM_GUIDE:
mndx = mode ? PM_TOURIST : PM_HUMAN;
break;
case PM_APPRENTICE:
mndx = mode ? PM_WIZARD : PM_HUMAN;
break;
case PM_WARRIOR:
mndx = mode ? PM_VALKYRIE : PM_HUMAN;
break;
default:
if (mndx >= LOW_PM && mndx < NUMMONS) {
struct permonst *ptr = &mons[mndx];
if (is_human(ptr))
mndx = PM_HUMAN;
else if (is_elf(ptr))
mndx = PM_ELF;
else if (is_dwarf(ptr))
mndx = PM_DWARF;
else if (is_gnome(ptr))
mndx = PM_GNOME;
else if (is_orc(ptr))
mndx = PM_ORC;
}
break;
}
return mndx;
}
/* return monster index if chameleon, or NON_PM if not */
int
pm_to_cham(int mndx)
{
int mcham = NON_PM;
/*
* As of 3.6.0 we just check M2_SHAPESHIFTER instead of having a
* big switch statement with hardcoded shapeshifter types here.
*/
if (mndx >= LOW_PM && is_shapeshifter(&mons[mndx]))
mcham = mndx;
return mcham;
}
/* for deciding whether corpse will carry along full monster data */
#define KEEPTRAITS(mon) \
((mon)->isshk || (mon)->mtame || unique_corpstat((mon)->data) \
|| is_reviver((mon)->data) \
/* normally quest leader will be unique, */ \
/* but he or she might have been polymorphed */ \
|| (mon)->m_id == gq.quest_status.leader_m_id \
/* special cancellation handling for these */ \
|| (dmgtype((mon)->data, AD_SEDU) || dmgtype((mon)->data, AD_SSEX)))
/* Creates a monster corpse, a "special" corpse, or nothing if it doesn't
* leave corpses. Monsters which leave "special" corpses should have
* G_NOCORPSE set in order to prevent wishing for one, finding tins of one,
* etc....
*/
static struct obj *
make_corpse(struct monst *mtmp, unsigned int corpseflags)
{
register struct permonst *mdat = mtmp->data;
int num;
struct obj *obj = (struct obj *) 0;
struct obj *otmp = (struct obj *) 0;
coordxy x = mtmp->mx, y = mtmp->my;
int mndx = monsndx(mdat);
unsigned corpstatflags = corpseflags;
boolean burythem = ((corpstatflags & CORPSTAT_BURIED) != 0);
if (mtmp->female)
corpstatflags |= CORPSTAT_FEMALE;
else if (!is_neuter(mtmp->data))
corpstatflags |= CORPSTAT_MALE;
switch (mndx) {
case PM_GRAY_DRAGON:
case PM_SILVER_DRAGON:
#if 0 /* DEFERRED */
case PM_SHIMMERING_DRAGON:
#endif
case PM_RED_DRAGON:
case PM_ORANGE_DRAGON:
case PM_WHITE_DRAGON:
case PM_BLACK_DRAGON:
case PM_BLUE_DRAGON:
case PM_GREEN_DRAGON:
case PM_YELLOW_DRAGON:
/* Make dragon scales. This assumes that the order of the
dragons is the same as the order of the scales. */
if (!rn2(mtmp->mrevived ? 20 : 3)) {
num = GRAY_DRAGON_SCALES + monsndx(mdat) - PM_GRAY_DRAGON;
obj = mksobj_at(num, x, y, FALSE, FALSE);
obj->spe = 0;
obj->cursed = obj->blessed = FALSE;
}
goto default_1;
case PM_WHITE_UNICORN:
case PM_GRAY_UNICORN:
case PM_BLACK_UNICORN:
if (mtmp->mrevived && rn2(2)) {
if (canseemon(mtmp))
pline("%s recently regrown horn crumbles to dust.",
s_suffix(Monnam(mtmp)));
} else {
obj = mksobj_at(UNICORN_HORN, x, y, TRUE, FALSE);
if (obj && mtmp->mrevived)
obj->degraded_horn = 1;
}
goto default_1;
case PM_LONG_WORM:
(void) mksobj_at(WORM_TOOTH, x, y, TRUE, FALSE);
goto default_1;
case PM_VAMPIRE:
case PM_VAMPIRE_LEADER:
/* include mtmp in the mkcorpstat() call */
num = undead_to_corpse(mndx);
corpstatflags |= CORPSTAT_INIT;
obj = mkcorpstat(CORPSE, mtmp, &mons[num], x, y, corpstatflags);
obj->age -= (TAINT_AGE + 1); /* this is an *OLD* corpse */
break;
case PM_KOBOLD_MUMMY:
case PM_DWARF_MUMMY:
case PM_GNOME_MUMMY:
case PM_ORC_MUMMY:
case PM_ELF_MUMMY:
case PM_HUMAN_MUMMY:
case PM_GIANT_MUMMY:
case PM_ETTIN_MUMMY:
case PM_KOBOLD_ZOMBIE:
case PM_DWARF_ZOMBIE:
case PM_GNOME_ZOMBIE:
case PM_ORC_ZOMBIE:
case PM_ELF_ZOMBIE:
case PM_HUMAN_ZOMBIE:
case PM_GIANT_ZOMBIE:
case PM_ETTIN_ZOMBIE:
num = undead_to_corpse(mndx);
corpstatflags |= CORPSTAT_INIT;
obj = mkcorpstat(CORPSE, mtmp, &mons[num], x, y, corpstatflags);
obj->age -= (TAINT_AGE + 1); /* this is an *OLD* corpse */
break;
case PM_IRON_GOLEM:
num = d(2, 6);
while (num--)
obj = mksobj_at(IRON_CHAIN, x, y, TRUE, FALSE);
free_mgivenname(mtmp); /* don't christen obj */
break;
case PM_GLASS_GOLEM:
num = d(2, 4); /* very low chance of creating all glass gems */
while (num--)
obj = mksobj_at(FIRST_GLASS_GEM + rn2(NUM_GLASS_GEMS),
x, y, TRUE, FALSE);
free_mgivenname(mtmp);
break;
case PM_CLAY_GOLEM:
obj = mksobj_at(ROCK, x, y, FALSE, FALSE);
obj->quan = (long) (rn2(20) + 50);
obj->owt = weight(obj);
free_mgivenname(mtmp);
break;
case PM_STONE_GOLEM:
corpstatflags &= ~CORPSTAT_INIT;
obj = mkcorpstat(STATUE, (struct monst *) 0, mdat, x, y,
corpstatflags);
break;
case PM_WOOD_GOLEM:
num = d(2, 4);
while (num--) {
obj = mksobj_at(
rn2(2) ? QUARTERSTAFF
: rn2(3) ? SMALL_SHIELD
: rn2(3) ? CLUB
: rn2(3) ? ELVEN_SPEAR : BOOMERANG,
x, y, TRUE, FALSE);
}
free_mgivenname(mtmp);
break;
case PM_ROPE_GOLEM:
num = rn2(3);
while (num-- > 0) {
obj = mksobj_at(rn2(2) ? LEASH : BULLWHIP, x, y, TRUE, FALSE);
}
free_mgivenname(mtmp);
break;
case PM_LEATHER_GOLEM:
num = d(2, 4);
while (num--)
obj = mksobj_at(LEATHER_ARMOR, x, y, TRUE, FALSE);
free_mgivenname(mtmp);
break;
case PM_GOLD_GOLEM:
/* Good luck gives more coins */
obj = mkgold((long) (200 - rnl(101)), x, y);
free_mgivenname(mtmp);
break;
case PM_PAPER_GOLEM:
num = rnd(4);
while (num--)
obj = mksobj_at(SCR_BLANK_PAPER, x, y, TRUE, FALSE);
free_mgivenname(mtmp);
break;
/* expired puddings will congeal into a large blob;
like dragons, relies on the order remaining consistent */
case PM_GRAY_OOZE:
case PM_BROWN_PUDDING:
case PM_GREEN_SLIME:
case PM_BLACK_PUDDING:
/* we have to do this here because most other places
expect there to be an object coming back; not this one */
obj = mksobj_at(GLOB_OF_BLACK_PUDDING - (PM_BLACK_PUDDING - mndx),
x, y, TRUE, FALSE);
while (obj && (otmp = obj_nexto(obj)) != (struct obj *) 0) {
pudding_merge_message(obj, otmp);
obj = obj_meld(&obj, &otmp);
}
free_mgivenname(mtmp);
newsym(x, y);
return obj;
default:
default_1:
if (gm.mvitals[mndx].mvflags & G_NOCORPSE) {
return (struct obj *) 0;
} else {
corpstatflags |= CORPSTAT_INIT;
/* preserve the unique traits of some creatures */
obj = mkcorpstat(CORPSE, KEEPTRAITS(mtmp) ? mtmp : 0,
mdat, x, y, corpstatflags);
if (burythem) {
boolean dealloc;
(void) bury_an_obj(obj, &dealloc);
newsym(x, y);
return dealloc ? (struct obj *) 0 : obj;
}
}
break;
}
/* All special cases should precede the G_NOCORPSE check */
if (!obj)
return (struct obj *) 0;
/* if polymorph or undead turning has killed this monster,
prevent the same attack beam from hitting its corpse */
if (gc.context.bypasses)
bypass_obj(obj);
if (has_mgivenname(mtmp))
obj = oname(obj, MGIVENNAME(mtmp), ONAME_NO_FLAGS);
/* Avoid "It was hidden under a green mold corpse!"
* during Blind combat. An unseen monster referred to as "it"
* could be killed and leave a corpse. If a hider then hid
* underneath it, you could be told the corpse type of a
* monster that you never knew was there without this.
* The code in hitmu() substitutes the word "something"
* if the corpse's obj->dknown is 0.
*/
if (Blind && !sensemon(mtmp))
clear_dknown(obj); /* obj->dknown = 0; */
stackobj(obj); /* 'obj' remains valid if stacking happens */
newsym(x, y);
/* in case the corpse was placed at a different spot from where
the monster was (not expected to happen) */
if (obj->ox != x || obj->oy != y)
newsym(obj->ox, obj->oy);
return obj;
}
/* check mtmp and water/lava for compatibility, 0 (survived), 1 (died) */
int
minliquid(struct monst* mtmp)
{
int res;
/* set up flag for mondead() and xkilled() */
iflags.sad_feeling = (mtmp->mtame && !canseemon(mtmp));
res = minliquid_core(mtmp);
/* always clear the flag */
iflags.sad_feeling = FALSE;
return res;
}
/* guts of minliquid() */
static int
minliquid_core(struct monst* mtmp)
{
boolean inpool, inlava, infountain;
boolean waterwall = is_waterwall(mtmp->mx,mtmp->my);
/* [ceiling clingers are handled below] */
inpool = (is_pool(mtmp->mx, mtmp->my)
&& (!(is_flyer(mtmp->data) || is_floater(mtmp->data))
/* there's no "above the surface" on the plane of water */
|| Is_waterlevel(&u.uz)));
inlava = (is_lava(mtmp->mx, mtmp->my)
&& !(is_flyer(mtmp->data) || is_floater(mtmp->data)));
infountain = IS_FOUNTAIN(levl[mtmp->mx][mtmp->my].typ);
/* Flying and levitation keeps our steed out of the liquid
(but not water-walking or swimming; note: if hero is in a
water location on the Plane of Water, flight and levitating
are blocked so this (Flying || Levitation) test fails there
and steed will be subject to water effects, as intended) */
if (mtmp == u.usteed && (Flying || Levitation) && !waterwall)
return 0;
/* Gremlin multiplying won't go on forever since the hit points
* keep going down, and when it gets to 1 hit point the clone
* function will fail.
*/
if (mtmp->data == &mons[PM_GREMLIN] && (inpool || infountain) && rn2(3)) {
if (split_mon(mtmp, (struct monst *) 0))
dryup(mtmp->mx, mtmp->my, FALSE);
if (inpool)
water_damage_chain(mtmp->minvent, FALSE);
return 0;
} else if (mtmp->data == &mons[PM_IRON_GOLEM] && inpool && !rn2(5)) {
int dam = d(2, 6);
if (cansee(mtmp->mx, mtmp->my))
pline("%s rusts.", Monnam(mtmp));
mtmp->mhp -= dam;
if (mtmp->mhpmax > dam)
mtmp->mhpmax -= dam;
if (DEADMONSTER(mtmp)) {
mondied(mtmp);
if (DEADMONSTER(mtmp))
return 1;
}
water_damage_chain(mtmp->minvent, FALSE);
return 0;
}
if (inlava) {
/*
* Lava effects much as water effects. Lava likers are able to
* protect their stuff. Fire resistant monsters can only protect
* themselves --ALI
*/
if (!is_clinger(mtmp->data) && !likes_lava(mtmp->data)) {
/* not fair...? hero doesn't automatically teleport away
from lava, just from water */
if (can_teleport(mtmp->data) && !tele_restrict(mtmp)) {
if (rloc(mtmp, RLOC_MSG))
return 0;
}
if (!resists_fire(mtmp)) {
if (cansee(mtmp->mx, mtmp->my)) {
struct attack *dummy = &mtmp->data->mattk[0];
const char *how = on_fire(mtmp->data, dummy);
pline("%s %s.", Monnam(mtmp),
!strcmp(how, "boiling") ? "boils away"
: !strcmp(how, "melting") ? "melts away"
: "burns to a crisp");
}
/* unlike fire -> melt ice -> pool, there's no way for the
hero to create lava beneath a monster, so the !mon_moving
case is not expected to happen (and we haven't made a
player-against-monster variation of the message above) */
if (gc.context.mon_moving)
mondead(mtmp); /* no corpse */
else
xkilled(mtmp, XKILL_NOMSG);
} else {
mtmp->mhp -= 1;
if (DEADMONSTER(mtmp)) {
if (cansee(mtmp->mx, mtmp->my))
pline("%s surrenders to the fire.", Monnam(mtmp));
mondead(mtmp); /* no corpse */
} else if (cansee(mtmp->mx, mtmp->my))
pline("%s burns slightly.", Monnam(mtmp));
}
if (!DEADMONSTER(mtmp)) {
(void) fire_damage_chain(mtmp->minvent, FALSE, FALSE,
mtmp->mx, mtmp->my);
(void) rloc(mtmp, RLOC_ERR|RLOC_NOMSG);
return 0;
}
return 1;
}
} else if (inpool || waterwall) {
/* Most monsters drown in pools. flooreffects() will take care of
* water damage to dead monsters' inventory, but survivors need to
* be handled here. Swimmers are able to protect their stuff...
*/
if ((waterwall || !is_clinger(mtmp->data)) && !cant_drown(mtmp->data)) {
/* like hero with teleport intrinsic or spell, teleport away
if possible */
if (can_teleport(mtmp->data) && !tele_restrict(mtmp)) {
if (rloc(mtmp, RLOC_MSG))
return 0;
}
if (cansee(mtmp->mx, mtmp->my)) {
if (gc.context.mon_moving)
pline("%s drowns.", Monnam(mtmp));
else
/* hero used fire to melt ice that monster was on */
You("drown %s.", mon_nam(mtmp));
}
if (engulfing_u(mtmp)) {
/* This can happen after a purple worm plucks you off a
flying steed while you are over water. */
pline("%s sinks as %s rushes in and flushes you out.",
Monnam(mtmp), hliquid("water"));
}
if (gc.context.mon_moving)
mondied(mtmp); /* ok to leave corpse despite water */
else
xkilled(mtmp, XKILL_NOMSG);
if (!DEADMONSTER(mtmp)) {
water_damage_chain(mtmp->minvent, FALSE);
if (!rloc(mtmp, RLOC_NOMSG))
deal_with_overcrowding(mtmp);
return 0;
}
return 1;
}
} else {
/* but eels have a difficult time outside */
if (mtmp->data->mlet == S_EEL && !Is_waterlevel(&u.uz)
&& !breathless(mtmp->data)) {
/* as mhp gets lower, the rate of further loss slows down */
if (mtmp->mhp > 1 && rn2(mtmp->mhp) > rn2(8))
mtmp->mhp--;
monflee(mtmp, 2, FALSE, FALSE);
}
}
return 0;
}
/* calculate 'mon's movement for current turn; called from moveloop() */
int
mcalcmove(
struct monst *mon,
boolean m_moving) /* True: adjust for moving; False: just adjust for speed */
{
int mmove = mon->data->mmove;
int mmove_adj;
/* Note: MSLOW's `+ 1' prevents slowed speed 1 getting reduced to 0;
* MFAST's `+ 2' prevents hasted speed 1 from becoming a no-op;
* both adjustments have negligible effect on higher speeds.
*/
if (mon->mspeed == MSLOW)
mmove = (2 * mmove + 1) / 3;
else if (mon->mspeed == MFAST)
mmove = (4 * mmove + 2) / 3;
if (mon == u.usteed && u.ugallop && gc.context.mv) {
/* increase movement by a factor of 1.5; also increase variance of
movement speed (if it's naturally 24, we don't want it to always
become 36) */
mmove = ((rn2(2) ? 4 : 5) * mmove) / 3;
}
if (m_moving) {
/* Randomly round the monster's speed to a multiple of NORMAL_SPEED.
This makes it impossible for the player to predict when they'll
get a free turn (thus preventing exploits like "melee kiting"),
while retaining guarantees about shopkeepers not being outsped
by a normal-speed player, normal-speed players being unable
to open up a gap when fleeing a normal-speed monster, etc. */
mmove_adj = mmove % NORMAL_SPEED;
mmove -= mmove_adj;
if (rn2(NORMAL_SPEED) < mmove_adj)
mmove += NORMAL_SPEED;
}
return mmove;
}
/* actions that happen once per ``turn'', regardless of each
individual monster's metabolism; some of these might need to
be reclassified to occur more in proportion with movement rate */
void
mcalcdistress(void)
{
iter_mons(m_calcdistress);
}
static void
m_calcdistress(struct monst *mtmp)
{
/* must check non-moving monsters once/turn in case they managed
to end up in water or lava; note: when not in liquid they regen,
shape-shift, timeout temporary maladies just like other monsters */
if (mtmp->data->mmove == 0) {
if (gv.vision_full_recalc)
vision_recalc(0);
if (minliquid(mtmp))
return;
}
/* regenerate hit points */
mon_regen(mtmp, FALSE);
/* possibly polymorph shapechangers and lycanthropes */
if (mtmp->cham >= LOW_PM)
decide_to_shapeshift(mtmp, (canspotmon(mtmp)
|| engulfing_u(mtmp))
? SHIFT_MSG : 0);
were_change(mtmp);
/* gradually time out temporary problems */
if (mtmp->mblinded && !--mtmp->mblinded)
mtmp->mcansee = 1;
if (mtmp->mfrozen && !--mtmp->mfrozen)
mtmp->mcanmove = 1;
if (mtmp->mfleetim && !--mtmp->mfleetim)
mtmp->mflee = 0;
/* FIXME: mtmp->mlstmv ought to be updated here */
}
/* perform movement for a single monster.
meant to be used with iter_mons_safe. */
boolean
movemon_singlemon(struct monst *mtmp)
{
/* end monster movement early if hero is flagged to leave the level */
if (u.utotype
#ifdef SAFERHANGUP
/* or if the program has lost contact with the user */
|| gp.program_state.done_hup
#endif
) {
gs.somebody_can_move = FALSE;
return TRUE;
}
/* one dead monster needs to perform a move after death: vault
guard whose temporary corridor is still on the map; live
guards who have led the hero back to civilization get moved
off the map too; gd_move() decides whether the temporary
corridor can be removed and guard discarded (via clearing
mon->isgd flag so that dmonsfree() will get rid of mon) */
if (mtmp->isgd && !mtmp->mx && !(mtmp->mstate & MON_MIGRATING)) {
/* parked at <0,0>; eventually isgd should get set to false */
if (gm.moves > mtmp->mlstmv) {
(void) gd_move(mtmp);
mtmp->mlstmv = gm.moves;
}
return FALSE;
}
if (DEADMONSTER(mtmp))
return FALSE;
/* monster isn't on this map anymore */
if ((mtmp->mstate & (MON_DETACH|MON_MIGRATING|MON_LIMBO|MON_OFFMAP)) != 0)
return FALSE;
/* Find a monster that we have not treated yet. */
if (mtmp->movement < NORMAL_SPEED)
return FALSE;
mtmp->movement -= NORMAL_SPEED;
if (mtmp->movement >= NORMAL_SPEED)