forked from NetHack/NetHack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmonmove.c
2205 lines (1991 loc) · 78.2 KB
/
monmove.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 monmove.c $NHDT-Date: 1684621592 2023/05/20 22:26:32 $ $NHDT-Branch: NetHack-3.7 $:$NHDT-Revision: 1.218 $ */
/* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
/*-Copyright (c) Michael Allison, 2006. */
/* NetHack may be freely redistributed. See license for details. */
#include "hack.h"
#include "mfndpos.h"
#include "artifact.h"
static void watch_on_duty(struct monst *);
static int disturb(struct monst *);
static void release_hero(struct monst *);
static void distfleeck(struct monst *, int *, int *, int *);
static int m_arrival(struct monst *);
static void mind_blast(struct monst *);
static boolean holds_up_web(coordxy, coordxy);
static int count_webbing_walls(coordxy, coordxy);
static boolean soko_allow_web(struct monst *);
static boolean m_search_items(struct monst *, coordxy *, coordxy *, schar *,
int *);
static boolean leppie_avoidance(struct monst *);
static void leppie_stash(struct monst *);
static boolean m_balks_at_approaching(struct monst *);
static boolean stuff_prevents_passage(struct monst *);
static int vamp_shift(struct monst *, struct permonst *, boolean);
/* monster has triggered trapped door lock or was present when it got
triggered remotely (at door spot, door hit by zap);
returns True if mtmp dies */
boolean
mb_trapped(struct monst *mtmp, boolean canseeit)
{
if (Verbose(2, mb_trapped)) {
if (canseeit && !Unaware)
pline("KABOOM!! You see a door explode.");
else if (!Deaf)
You_hear("a %s explosion.",
(mdistu(mtmp) > 7 * 7) ? "distant" : "nearby");
}
wake_nearto(mtmp->mx, mtmp->my, 7 * 7);
mtmp->mstun = 1;
mtmp->mhp -= rnd(15);
if (DEADMONSTER(mtmp)) {
mondied(mtmp);
if (DEADMONSTER(mtmp))
return TRUE;
/* will get here if lifesaved */
}
mon_learns_traps(mtmp, TRAPPED_DOOR);
return FALSE;
}
/* push coordinate x,y to mtrack, making monster remember where it was */
void
mon_track_add(struct monst *mtmp, coordxy x, coordxy y)
{
int j;
for (j = MTSZ - 1; j > 0; j--)
mtmp->mtrack[j] = mtmp->mtrack[j - 1];
mtmp->mtrack[0].x = x;
mtmp->mtrack[0].y = y;
}
void
mon_track_clear(struct monst *mtmp)
{
memset(mtmp->mtrack, 0, sizeof(mtmp->mtrack));
}
/* check whether a monster is carrying a locking/unlocking tool */
boolean
monhaskey(
struct monst *mon,
boolean for_unlocking) /* true => credit card ok, false => not ok */
{
if (for_unlocking && m_carrying(mon, CREDIT_CARD))
return TRUE;
return m_carrying(mon, SKELETON_KEY) || m_carrying(mon, LOCK_PICK);
}
void
mon_yells(struct monst* mon, const char* shout)
{
if (Deaf) {
if (canspotmon(mon))
/* Sidenote on "A watchman angrily waves her arms!"
* Female being called watchman is correct (career name).
*/
pline("%s angrily %s %s %s!",
Amonnam(mon),
nolimbs(mon->data) ? "shakes" : "waves",
mhis(mon),
nolimbs(mon->data) ? mbodypart(mon, HEAD)
: makeplural(mbodypart(mon, ARM)));
} else {
if (canspotmon(mon)) {
pline("%s yells:", Amonnam(mon));
} else {
/* Soundeffect(se_someone_yells, 75); */
You_hear("someone yell:");
}
SetVoice(mon, 0, 80, 0);
verbalize1(shout);
}
}
/* can monster mtmp break boulders? */
boolean
m_can_break_boulder(struct monst *mtmp)
{
return (is_rider(mtmp->data)
|| (!mtmp->mspec_used
&& (mtmp->isshk || mtmp->ispriest
|| (mtmp->data->msound == MS_LEADER))));
}
/* monster mtmp breaks boulder at x,y */
void
m_break_boulder(struct monst *mtmp, coordxy x, coordxy y)
{
struct obj *otmp;
if (m_can_break_boulder(mtmp) && ((otmp = sobj_at(BOULDER, x, y)) != 0)) {
if (!is_rider(mtmp->data)) {
if (!Deaf && (mdistu(mtmp) < 4*4))
pline("%s mutters %s.",
Monnam(mtmp),
mtmp->ispriest ? "a prayer" : "an incantation");
mtmp->mspec_used += rn1(20, 10);
}
if (cansee(x, y))
pline_The("boulder falls apart.");
fracture_rock(otmp);
}
}
static void
watch_on_duty(register struct monst* mtmp)
{
coordxy x, y;
if (mtmp->mpeaceful && in_town(u.ux + u.dx, u.uy + u.dy)
&& mtmp->mcansee && m_canseeu(mtmp) && !rn2(3)) {
if (picking_lock(&x, &y) && IS_DOOR(levl[x][y].typ)
&& (levl[x][y].doormask & D_LOCKED)) {
if (couldsee(mtmp->mx, mtmp->my)) {
if (levl[x][y].looted & D_WARNED) {
mon_yells(mtmp, "Halt, thief! You're under arrest!");
(void) angry_guards(!!Deaf);
} else {
mon_yells(mtmp, "Hey, stop picking that lock!");
levl[x][y].looted |= D_WARNED;
}
stop_occupation();
}
} else if (is_digging()) {
/* chewing, wand/spell of digging are checked elsewhere */
watch_dig(mtmp, gc.context.digging.pos.x, gc.context.digging.pos.y,
FALSE);
}
}
}
/* move a monster; if a threat to busy hero, stop doing whatever it is */
int
dochugw(
register struct monst *mtmp,
boolean chug) /* True: monster is moving;
* False: monster was just created or has teleported
* so perform stop-what-you're-doing-if-close-enough-
* to-be-a-threat check but don't move mtmp */
{
coordxy x = mtmp->mx, y = mtmp->my; /* 'mtmp's location before dochug() */
/* skip canspotmon() if occupation is Null */
boolean already_saw_mon = (chug && go.occupation) ? canspotmon(mtmp) : 0;
int rd = chug ? dochug(mtmp) : 0;
/*
* A similar check is in monster_nearby() in hack.c.
* [The two checks have a lot of differences and chances are high
* that some of those are unintentional.]
*/
/* check whether hero notices monster and stops current activity */
if (go.occupation && !rd
/* monster is hostile and can attack (or hallu distorts knowledge) */
&& (Hallucination || (!mtmp->mpeaceful && !noattacks(mtmp->data)))
/* it's close enough to be a threat */
&& mdistu(mtmp) <= (BOLT_LIM + 1) * (BOLT_LIM + 1)
/* and either couldn't see it before, or it was too far away */
&& (!already_saw_mon || !couldsee(x, y)
|| distu(x, y) > (BOLT_LIM + 1) * (BOLT_LIM + 1))
/* can see it now, or sense it and would normally see it */
&& canspotmon(mtmp) && couldsee(mtmp->mx, mtmp->my)
/* monster isn't paralyzed or afraid (scare monster/Elbereth) */
&& mtmp->mcanmove && !onscary(u.ux, u.uy, mtmp))
stop_occupation();
return rd;
}
boolean
onscary(coordxy x, coordxy y, struct monst *mtmp)
{
struct engr *ep;
/* creatures who are directly resistant to magical scaring:
* humans aren't monsters
* uniques have ascended their base monster instincts
* Rodney, lawful minions, Angels, the Riders, shopkeepers
* inside their own shop, priests inside their own temple */
if (mtmp->iswiz || is_lminion(mtmp) || mtmp->data == &mons[PM_ANGEL]
|| is_rider(mtmp->data)
|| mtmp->data->mlet == S_HUMAN || unique_corpstat(mtmp->data)
|| (mtmp->isshk && inhishop(mtmp))
|| (mtmp->ispriest && inhistemple(mtmp)))
return FALSE;
/* <0,0> is used by musical scaring to check for the above;
* it doesn't care about scrolls or engravings or dungeon branch */
if (x == 0 && y == 0)
return TRUE;
/* should this still be true for defiled/molochian altars? */
if (IS_ALTAR(levl[x][y].typ)
&& (mtmp->data->mlet == S_VAMPIRE || is_vampshifter(mtmp)))
return TRUE;
/* the scare monster scroll doesn't have any of the below
* restrictions, being its own source of power */
if (sobj_at(SCR_SCARE_MONSTER, x, y))
return TRUE;
/*
* Creatures who don't (or can't) fear a written Elbereth:
* all the above plus shopkeepers (even if poly'd into non-human),
* vault guards (also even if poly'd), blind or peaceful monsters,
* humans and elves, and minotaurs.
*
* If the player isn't actually on the square OR the player's image
* isn't displaced to the square, no protection is being granted.
*
* Elbereth doesn't work in Gehennom, the Elemental Planes, or the
* Astral Plane; the influence of the Valar only reaches so far.
*/
return ((ep = sengr_at("Elbereth", x, y, TRUE)) != 0
&& (u_at(x, y)
|| (Displaced && mtmp->mux == x && mtmp->muy == y)
|| (ep->guardobjects && vobj_at(x, y)))
&& !(mtmp->isshk || mtmp->isgd || !mtmp->mcansee
|| mtmp->mpeaceful || mtmp->data->mlet == S_HUMAN
|| mtmp->data == &mons[PM_MINOTAUR]
|| Inhell || In_endgame(&u.uz)));
}
/* regenerate lost hit points */
void
mon_regen(struct monst *mon, boolean digest_meal)
{
if (mon->mhp < mon->mhpmax
&& (gm.moves % 20 == 0 || regenerates(mon->data)))
mon->mhp++;
if (mon->mspec_used)
mon->mspec_used--;
if (digest_meal) {
if (mon->meating) {
mon->meating--;
if (mon->meating <= 0)
finish_meating(mon);
}
}
}
/*
* Possibly awaken the given monster. Return a 1 if the monster has been
* jolted awake.
*/
static int
disturb(register struct monst *mtmp)
{
/*
* + Ettins are hard to surprise.
* + Nymphs, jabberwocks, and leprechauns do not easily wake up.
*
* Wake up if:
* in direct LOS AND
* within 10 squares AND
* not stealthy or (mon is an ettin and 9/10) AND
* (mon is not a nymph, jabberwock, or leprechaun) or 1/50 AND
* Aggravate or mon is (dog or human) or
* (1/7 and mon is not mimicing furniture or object)
*/
if (couldsee(mtmp->mx, mtmp->my) && mdistu(mtmp) <= 100
&& (!Stealth || (mtmp->data == &mons[PM_ETTIN] && rn2(10)))
&& (!(mtmp->data->mlet == S_NYMPH
|| mtmp->data == &mons[PM_JABBERWOCK]
#if 0 /* DEFERRED */
|| mtmp->data == &mons[PM_VORPAL_JABBERWOCK]
#endif
|| mtmp->data->mlet == S_LEPRECHAUN) || !rn2(50))
&& (Aggravate_monster
|| (mtmp->data->mlet == S_DOG || mtmp->data->mlet == S_HUMAN)
|| (!rn2(7) && M_AP_TYPE(mtmp) != M_AP_FURNITURE
&& M_AP_TYPE(mtmp) != M_AP_OBJECT))) {
wake_msg(mtmp, !mtmp->mpeaceful);
mtmp->msleeping = 0;
return 1;
}
return 0;
}
/* ungrab/expel held/swallowed hero */
static void
release_hero(struct monst *mon)
{
if (mon == u.ustuck) {
if (u.uswallow) {
expels(mon, mon->data, TRUE);
} else if (!sticks(gy.youmonst.data)) {
unstuck(mon); /* let go */
You("get released!");
}
}
}
struct monst *
find_pmmonst(int pm)
{
struct monst *mtmp = 0;
if ((gm.mvitals[pm].mvflags & G_GENOD) == 0)
for (mtmp = fmon; mtmp; mtmp = mtmp->nmon) {
if (DEADMONSTER(mtmp))
continue;
if (mtmp->data == &mons[pm])
break;
}
return mtmp;
}
/* killer bee 'mon' is on a spot containing lump of royal jelly 'obj' and
will eat it if there is no queen bee on the level; return 1: mon died,
0: mon ate jelly and lived; -1: mon didn't eat jelly to use its move */
int
bee_eat_jelly(struct monst* mon, struct obj* obj)
{
int m_delay;
struct monst *mtmp = find_pmmonst(PM_QUEEN_BEE);
/* if there's no queen on the level, eat the royal jelly and become one */
if (!mtmp) {
m_delay = obj->blessed ? 3 : !obj->cursed ? 5 : 7;
if (obj->quan > 1L)
obj = splitobj(obj, 1L);
if (canseemon(mon))
pline("%s eats %s.", Monnam(mon), an(xname(obj)));
delobj(obj);
if ((int) mon->m_lev < mons[PM_QUEEN_BEE].mlevel - 1)
mon->m_lev = (uchar) (mons[PM_QUEEN_BEE].mlevel - 1);
/* there should be delay after eating, but that's too much
hassle; transform immediately, then have a short delay */
(void) grow_up(mon, (struct monst *) 0);
if (DEADMONSTER(mon))
return 1; /* dead; apparently queen bees have been genocided */
mon->mfrozen = m_delay, mon->mcanmove = 0;
return 0; /* bee used its move */
}
return -1; /* a queen is already present; ordinary bee hasn't moved yet */
}
/* FIXME: gremlins don't flee from monsters wielding Sunsword or wearing
gold dragon scales/mail, nor from gold dragons, only from the hero */
#define flees_light(mon) \
((mon)->data == &mons[PM_GREMLIN] \
&& ((uwep && uwep->lamplit && artifact_light(uwep)) \
|| (uarm && uarm->lamplit && artifact_light(uarm))) \
/* not applicable if mon can't see or hero isn't in line of sight */ \
&& mon->mcansee && couldsee(mon->mx, mon->my)) \
/* doesn't matter if hero is invisible--light being emitted isn't */
/* monster begins fleeing for the specified time, 0 means untimed flee
* if first, only adds fleetime if monster isn't already fleeing
* if fleemsg, prints a message about new flight, otherwise, caller should */
void
monflee(
struct monst *mtmp,
int fleetime,
boolean first,
boolean fleemsg)
{
/* shouldn't happen; maybe warrants impossible()? */
if (DEADMONSTER(mtmp))
return;
if (mtmp == u.ustuck)
release_hero(mtmp); /* expels/unstuck */
if (!first || !mtmp->mflee) {
/* don't lose untimed scare */
if (!fleetime)
mtmp->mfleetim = 0;
else if (!mtmp->mflee || mtmp->mfleetim) {
fleetime += (int) mtmp->mfleetim;
/* ensure monster flees long enough to visibly stop fighting */
if (fleetime == 1)
fleetime++;
mtmp->mfleetim = (unsigned) min(fleetime, 127);
}
if (!mtmp->mflee && fleemsg && canseemon(mtmp)
&& M_AP_TYPE(mtmp) != M_AP_FURNITURE
&& M_AP_TYPE(mtmp) != M_AP_OBJECT) {
/* unfortunately we can't distinguish between temporary
sleep and temporary paralysis, so both conditions
receive the same alternate message */
if (!mtmp->mcanmove || !mtmp->data->mmove) {
pline("%s seems to flinch.", Adjmonnam(mtmp, "immobile"));
} else if (flees_light(mtmp)) {
if (Unaware) {
/* tell the player even if the hero is unconscious */
pline("%s is frightened.", Monnam(mtmp));
} else if (rn2(10) || Deaf) {
/* via flees_light(), will always be either via uwep
(Sunsword) or uarm (gold dragon scales/mail) or both;
TODO? check for both and describe the one which is
emitting light with a bigger radius */
const char *lsrc = (uwep && artifact_light(uwep))
? bare_artifactname(uwep)
: (uarm && artifact_light(uarm))
? yname(uarm)
: "[its imagination?]";
pline("%s flees from the painful light of %s.",
Monnam(mtmp), lsrc);
} else {
SetVoice(mtmp, 0, 80, 0);
verbalize("Bright light!");
}
} else {
pline("%s turns to flee.", Monnam(mtmp));
}
}
if (mtmp->data == &mons[PM_VROCK] && !mtmp->mspec_used) {
mtmp->mspec_used = 75 + rn2(25);
(void) create_gas_cloud(mtmp->mx, mtmp->my, 5, 8);
}
mtmp->mflee = 1;
}
/* ignore recently-stepped spaces when made to flee */
mon_track_clear(mtmp);
}
static void
distfleeck(
struct monst *mtmp,
int *inrange, int *nearby, int *scared) /* output */
{
int seescaryx, seescaryy;
boolean sawscary = FALSE, bravegremlin = (rn2(5) == 0);
*inrange = (dist2(mtmp->mx, mtmp->my, mtmp->mux, mtmp->muy)
<= (BOLT_LIM * BOLT_LIM));
*nearby = *inrange && monnear(mtmp, mtmp->mux, mtmp->muy);
/* Note: if your image is displaced, the monster sees the Elbereth
* at your displaced position, thus never attacking your displaced
* position, but possibly attacking you by accident. If you are
* invisible, it sees the Elbereth at your real position, thus never
* running into you by accident but possibly attacking the spot
* where it guesses you are.
*/
if (!mtmp->mcansee || (Invis && !perceives(mtmp->data))) {
seescaryx = mtmp->mux;
seescaryy = mtmp->muy;
} else {
seescaryx = u.ux;
seescaryy = u.uy;
}
sawscary = onscary(seescaryx, seescaryy, mtmp);
if (*nearby && (sawscary
|| (flees_light(mtmp) && !bravegremlin)
|| (!mtmp->mpeaceful && in_your_sanctuary(mtmp, 0, 0)))) {
*scared = 1;
monflee(mtmp, rnd(rn2(7) ? 10 : 100), TRUE, TRUE);
} else
*scared = 0;
}
#undef flees_light
/* perform a special one-time action for a monster; returns -1 if nothing
special happened, 0 if monster uses up its turn, 1 if monster is killed */
static int
m_arrival(struct monst* mon)
{
mon->mstrategy &= ~STRAT_ARRIVE; /* always reset */
return -1;
}
/* a mind flayer unleashes a mind blast */
static void
mind_blast(register struct monst* mtmp)
{
struct monst *m2, *nmon = (struct monst *) 0;
if (canseemon(mtmp))
pline("%s concentrates.", Monnam(mtmp));
if (mdistu(mtmp) > BOLT_LIM * BOLT_LIM) {
You("sense a faint wave of psychic energy.");
return;
}
pline("A wave of psychic energy pours over you!");
if (mtmp->mpeaceful
&& (!Conflict || resist_conflict(mtmp))) {
pline("It feels quite soothing.");
} else if (!u.uinvulnerable) {
int dmg;
boolean m_sen = sensemon(mtmp);
if (m_sen || (Blind_telepat && rn2(2)) || !rn2(10)) {
/* hiding monsters are brought out of hiding when hit by
a psychic blast, so do the same for hiding poly'd hero */
if (u.uundetected) {
u.uundetected = 0;
newsym(u.ux, u.uy);
} else if (U_AP_TYPE != M_AP_NOTHING
/* hero has no way to hide as monster but
check for that theoretical case anyway */
&& U_AP_TYPE != M_AP_MONSTER) {
gy.youmonst.m_ap_type = M_AP_NOTHING;
gy.youmonst.mappearance = 0;
newsym(u.ux, u.uy);
}
pline("It locks on to your %s!",
m_sen ? "telepathy"
: Blind_telepat ? "latent telepathy"
: "mind"); /* note: hero is never mindless */
dmg = rnd(15);
if (Half_spell_damage)
dmg = (dmg + 1) / 2;
losehp(dmg, "psychic blast", KILLED_BY_AN);
}
}
for (m2 = fmon; m2; m2 = nmon) {
nmon = m2->nmon;
if (DEADMONSTER(m2))
continue;
if (m2->mpeaceful == mtmp->mpeaceful)
continue;
if (mindless(m2->data))
continue;
if (m2 == mtmp)
continue;
if ((telepathic(m2->data) && (rn2(2) || m2->mblinded)) || !rn2(10)) {
/* wake it up first, to bring hidden monster out of hiding */
wakeup(m2, FALSE);
if (cansee(m2->mx, m2->my))
pline("It locks on to %s.", mon_nam(m2));
m2->mhp -= rnd(15);
if (DEADMONSTER(m2))
monkilled(m2, "", AD_DRIN);
}
}
}
/* returns 1 if monster died moving, 0 otherwise */
/* The whole dochugw/m_move/distfleeck/mfndpos section is serious spaghetti
* code. --KAA
*/
int
dochug(register struct monst* mtmp)
{
register struct permonst *mdat;
register int status = MMOVE_NOTHING;
int inrange, nearby, scared, res;
struct obj *otmp;
boolean panicattk = FALSE;
/*
* PHASE ONE: Pre-movement adjustments
*/
mdat = mtmp->data;
if (mtmp->mstrategy & STRAT_ARRIVE) {
res = m_arrival(mtmp);
if (res >= 0)
return res;
}
/* check for waitmask status change */
if ((mtmp->mstrategy & STRAT_WAITFORU)
&& (m_canseeu(mtmp) || mtmp->mhp < mtmp->mhpmax))
mtmp->mstrategy &= ~STRAT_WAITFORU;
/* update quest status flags */
quest_stat_check(mtmp);
if (!mtmp->mcanmove || (mtmp->mstrategy & STRAT_WAITMASK)) {
if (Hallucination)
newsym(mtmp->mx, mtmp->my);
if (mtmp->mcanmove && (mtmp->mstrategy & STRAT_CLOSE)
&& !mtmp->msleeping && monnear(mtmp, u.ux, u.uy))
quest_talk(mtmp); /* give the leaders a chance to speak */
return 0; /* other frozen monsters can't do anything */
}
/* there is a chance we will wake it */
if (mtmp->msleeping && !disturb(mtmp)) {
if (Hallucination)
newsym(mtmp->mx, mtmp->my);
return 0;
}
/* not frozen or sleeping: wipe out texts written in the dust */
wipe_engr_at(mtmp->mx, mtmp->my, 1, FALSE);
/* confused monsters get unconfused with small probability */
if (mtmp->mconf && !rn2(50))
mtmp->mconf = 0;
/* stunned monsters get un-stunned with larger probability */
if (mtmp->mstun && !rn2(10))
mtmp->mstun = 0;
/* Some monsters teleport. Teleportation costs a turn. */
if (mtmp->mflee && !rn2(40) && can_teleport(mdat) && !mtmp->iswiz
&& !noteleport_level(mtmp)) {
if (rloc(mtmp, RLOC_MSG))
leppie_stash(mtmp);
return 0;
}
/* Shriekers and Medusa have irregular abilities which must be
checked every turn. These abilities do not cost a turn when
used. */
if (mdat->msound == MS_SHRIEK && !um_dist(mtmp->mx, mtmp->my, 1))
m_respond(mtmp);
if (mdat == &mons[PM_MEDUSA] && couldsee(mtmp->mx, mtmp->my))
m_respond(mtmp);
if (DEADMONSTER(mtmp))
return 1; /* m_respond gaze can kill medusa */
/* fleeing monsters might regain courage */
if (mtmp->mflee && !mtmp->mfleetim && mtmp->mhp == mtmp->mhpmax
&& !rn2(25))
mtmp->mflee = 0;
/* Cease conflict-induced swallow/grab if conflict has ended. Releasing
the hero in this way uses up the monster's turn. */
if (mtmp == u.ustuck && mtmp->mpeaceful && !mtmp->mconf && !Conflict) {
release_hero(mtmp);
return 0;
}
/*
* PHASE TWO: Special Movements and Actions
*/
/* The monster decides where it thinks you are. This call to set_apparxy()
must be done after you move and before the monster does. The
set_apparxy() call in m_move() doesn't suffice since the variables
inrange, etc. all depend on stuff set by set_apparxy().
*/
set_apparxy(mtmp);
/* Monsters that want to acquire things may teleport, so do it before
inrange is set. This costs a turn only if mstate is set. */
if (is_covetous(mdat)) {
(void) tactics(mtmp);
/* tactics -> mnexto -> deal_with_overcrowding */
if (mtmp->mstate)
return 0;
}
/* check distance and scariness of attacks */
distfleeck(mtmp, &inrange, &nearby, &scared);
/* search for and potentially use defensive or miscellaneous items. */
if (find_defensive(mtmp, FALSE)) {
if (use_defensive(mtmp) != 0)
return 1;
} else if (find_misc(mtmp)) {
if (use_misc(mtmp) != 0)
return 1;
}
/* Demonic Blackmail! */
if (nearby && mdat->msound == MS_BRIBE && mtmp->mpeaceful && !mtmp->mtame
&& !u.uswallow) {
if (mtmp->mux != u.ux || mtmp->muy != u.uy) {
pline("%s whispers at thin air.",
cansee(mtmp->mux, mtmp->muy) ? Monnam(mtmp) : "It");
if (is_demon(gy.youmonst.data)) {
/* "Good hunting, brother" */
if (!tele_restrict(mtmp))
(void) rloc(mtmp, RLOC_MSG);
} else {
mtmp->minvis = mtmp->perminvis = 0;
/* Why? For the same reason in real demon talk */
pline("%s gets angry!", Amonnam(mtmp));
mtmp->mpeaceful = 0;
set_malign(mtmp);
/* since no way is an image going to pay it off */
}
} else if (demon_talk(mtmp))
return 1; /* you paid it off */
}
/* the watch will look around and see if you are up to no good :-) */
if (is_watch(mdat)) {
watch_on_duty(mtmp);
/* mind flayers can make psychic attacks! */
} else if (is_mind_flayer(mdat) && !rn2(20)) {
mind_blast(mtmp);
distfleeck(mtmp, &inrange, &nearby, &scared);
}
/* If monster is nearby you, and has to wield a weapon, do so. This
* costs the monster a move, of course.
*/
if ((!mtmp->mpeaceful || Conflict) && inrange
&& dist2(mtmp->mx, mtmp->my, mtmp->mux, mtmp->muy) <= 8
&& attacktype(mdat, AT_WEAP)) {
struct obj *mw_tmp;
/* The scared check is necessary. Otherwise a monster that is
* one square near the player but fleeing into a wall would keep
* switching between pick-axe and weapon. If monster is stuck
* in a trap, prefer ranged weapon (wielding is done in thrwmu).
* This may cost the monster an attack, but keeps the monster
* from switching back and forth if carrying both.
*/
mw_tmp = MON_WEP(mtmp);
if (!(scared && mw_tmp && is_pick(mw_tmp))
&& mtmp->weapon_check == NEED_WEAPON
&& !(mtmp->mtrapped && !nearby && select_rwep(mtmp))) {
mtmp->weapon_check = NEED_HTH_WEAPON;
if (mon_wield_item(mtmp) != 0)
return 0;
}
}
/*
* PHASE THREE: Now the actual movement phase
*/
/* Hezrous create clouds of stench. This does not cost a move. */
if (mtmp->data == &mons[PM_HEZROU]) /* stench */
create_gas_cloud(mtmp->mx, mtmp->my, 1, 8);
else if (mtmp->data == &mons[PM_STEAM_VORTEX] && !mtmp->mcan)
create_gas_cloud(mtmp->mx, mtmp->my, 1, 0); /* harmless vapor */
/* A killer bee may eat honey in order to turn into a queen bee,
costing it a move. */
if (mdat == &mons[PM_KILLER_BEE]
/* could be smarter and deliberately move to royal jelly, but
then we'd need to scan the level for queen bee in advance;
avoid that overhead and rely on serendipity... */
&& (otmp = sobj_at(LUMP_OF_ROYAL_JELLY, mtmp->mx, mtmp->my)) != 0
&& (res = bee_eat_jelly(mtmp, otmp)) >= 0)
return res;
/* A monster that passes the following checks has the opportunity
to move. Movement itself is handled by the m_move() function. */
if (!nearby || mtmp->mflee || scared || mtmp->mconf || mtmp->mstun
|| (mtmp->minvis && !rn2(3))
|| (mdat->mlet == S_LEPRECHAUN && !findgold(gi.invent)
&& (findgold(mtmp->minvent) || rn2(2)))
|| (is_wanderer(mdat) && !rn2(4)) || (Conflict && !mtmp->iswiz)
|| (!mtmp->mcansee && !rn2(4)) || mtmp->mpeaceful) {
/* Possibly cast an undirected spell if not attacking you */
/* note that most of the time castmu() will pick a directed
spell and do nothing, so the monster moves normally */
/* arbitrary distance restriction to keep monster far away
from you from having cast dozens of sticks-to-snakes
or similar spells by the time you reach it */
if (!mtmp->mspec_used
&& dist2(mtmp->mx, mtmp->my, u.ux, u.uy) <= 49) {
struct attack *a;
for (a = &mdat->mattk[0]; a < &mdat->mattk[NATTK]; a++) {
if (a->aatyp == AT_MAGC
&& (a->adtyp == AD_SPEL || a->adtyp == AD_CLRC)) {
if ((castmu(mtmp, a, FALSE, FALSE) & M_ATTK_HIT)) {
status = MMOVE_DONE; /* bypass m_move() */
break;
}
}
}
}
if (!status)
status = m_move(mtmp, 0);
if (status != MMOVE_DIED)
distfleeck(mtmp, &inrange, &nearby, &scared); /* recalc */
switch (status) { /* for pets, cases 0 and 3 are equivalent */
case MMOVE_NOMOVES:
if (scared)
panicattk = TRUE;
/*FALLTHRU*/
case MMOVE_NOTHING: /* no movement, but it can still attack you */
case MMOVE_DONE: /* absolutely no movement */
/* vault guard might have vanished */
if (mtmp->isgd && (DEADMONSTER(mtmp) || mtmp->mx == 0))
return 1; /* behave as if it died */
/* During hallucination, monster appearance should
* still change - even if it doesn't move.
*/
if (Hallucination)
newsym(mtmp->mx, mtmp->my);
break;
case MMOVE_MOVED: /* monster moved */
/* if confused grabber has wandered off, let go */
if (mtmp == u.ustuck && !next2u(mtmp->mx, mtmp->my))
unstuck(mtmp);
/* Maybe it stepped on a trap and fell asleep... */
if (helpless(mtmp))
return 0;
/* Monsters can move and then shoot on same turn;
our hero can't. Is that fair? */
if (!nearby && (ranged_attk(mdat)
|| attacktype(mdat, AT_WEAP)
|| find_offensive(mtmp)))
break;
/* a monster that's digesting you can move at the
* same time -dlc
*/
if (engulfing_u(mtmp))
return mattacku(mtmp);
return 0;
case MMOVE_DIED: /* monster died */
return 1;
}
}
/*
* PHASE FOUR: Standard Attacks
*/
/* Now, attack the player if possible - one attack set per monst */
if (status != MMOVE_DONE && (!mtmp->mpeaceful
|| (Conflict && !resist_conflict(mtmp)))) {
if (((inrange && !scared) || panicattk) && !noattacks(mdat)
/* [is this hp check really needed?] */
&& (Upolyd ? u.mh : u.uhp) > 0) {
if (mattacku(mtmp))
return 1; /* monster died (e.g. exploded) */
}
if (mtmp->wormno) {
if (wormhitu(mtmp))
return 1; /* worm died (poly'd hero passive counter-attack) */
}
}
/* special speeches for quest monsters */
if (!helpless(mtmp) && nearby)
quest_talk(mtmp);
/* extra emotional attack for vile monsters */
if (inrange && mtmp->data->msound == MS_CUSS && !mtmp->mpeaceful
&& couldsee(mtmp->mx, mtmp->my) && !mtmp->minvis && !rn2(5))
cuss(mtmp);
/* note: can't get here when monster is dead, so this always returns 0 */
return (status == MMOVE_DIED);
}
static NEARDATA const char practical[] = { WEAPON_CLASS, ARMOR_CLASS,
GEM_CLASS, FOOD_CLASS, 0 };
static NEARDATA const char magical[] = { AMULET_CLASS, POTION_CLASS,
SCROLL_CLASS, WAND_CLASS,
RING_CLASS, SPBOOK_CLASS, 0 };
/* monster mtmp would love to take object otmp? */
boolean
mon_would_take_item(struct monst *mtmp, struct obj *otmp)
{
int pctload = (curr_mon_load(mtmp) * 100) / max_mon_load(mtmp);
if (otmp == uball || otmp == uchain)
return FALSE;
if (mtmp->mtame && otmp->cursed)
return FALSE; /* note: will get overridden if mtmp will eat otmp */
if (is_unicorn(mtmp->data) && objects[otmp->otyp].oc_material != GEMSTONE)
return FALSE;
if (!mindless(mtmp->data) && !is_animal(mtmp->data) && pctload < 75
&& searches_for_item(mtmp, otmp))
return TRUE;
if (likes_gold(mtmp->data) && otmp->otyp == GOLD_PIECE && pctload < 95)
return TRUE;
if (likes_gems(mtmp->data) && otmp->oclass == GEM_CLASS
&& objects[otmp->otyp].oc_material != MINERAL && pctload < 85)
return TRUE;
if (likes_objs(mtmp->data) && strchr(practical, otmp->oclass)
&& pctload < 75)
return TRUE;
if (likes_magic(mtmp->data) && strchr(magical, otmp->oclass)
&& pctload < 85)
return TRUE;
if (throws_rocks(mtmp->data) && otmp->otyp == BOULDER
&& pctload < 50 && !Sokoban)
return TRUE;
if (mtmp->data == &mons[PM_GELATINOUS_CUBE]
&& otmp->oclass != ROCK_CLASS && otmp->oclass != BALL_CLASS
&& !(otmp->otyp == CORPSE && touch_petrifies(&mons[otmp->corpsenm])))
return TRUE;
return FALSE;
}
/* monster mtmp would love to consume object otmp, without picking it up */
boolean
mon_would_consume_item(struct monst *mtmp, struct obj *otmp)
{
int ftyp;
if (otmp->otyp == CORPSE && !touch_petrifies(&mons[otmp->corpsenm])
&& corpse_eater(mtmp->data))
return TRUE;
if (mtmp->mtame && has_edog(mtmp) /* has_edog(): not guardian angel */
&& (ftyp = dogfood(mtmp, otmp)) < MANFOOD
&& (ftyp < ACCFOOD || EDOG(mtmp)->hungrytime <= gm.moves))
return TRUE;
return FALSE;
}
boolean
itsstuck(register struct monst* mtmp)
{
if (sticks(gy.youmonst.data) && mtmp == u.ustuck && !u.uswallow) {
pline("%s cannot escape from you!", Monnam(mtmp));
return TRUE;
}
return FALSE;
}
/*
* should_displace()
*
* Displacement of another monster is a last resort and only
* used on approach. If there are better ways to get to target,
* those should be used instead. This function does that evaluation.
*/
boolean
should_displace(
struct monst *mtmp,
coord *poss, /* coord poss[9] */
long *info, /* long info[9] */
int cnt,
coordxy ggx,
coordxy ggy)
{
int shortest_with_displacing = -1;
int shortest_without_displacing = -1;
int count_without_displacing = 0;
register int i, nx, ny;
int ndist;
for (i = 0; i < cnt; i++) {
nx = poss[i].x;
ny = poss[i].y;
ndist = dist2(nx, ny, ggx, ggy);
if (MON_AT(nx, ny) && (info[i] & ALLOW_MDISP) && !(info[i] & ALLOW_M)
&& !undesirable_disp(mtmp, nx, ny)) {
if (shortest_with_displacing == -1
|| (ndist < shortest_with_displacing))
shortest_with_displacing = ndist;
} else {
if ((shortest_without_displacing == -1)
|| (ndist < shortest_without_displacing))
shortest_without_displacing = ndist;
count_without_displacing++;
}
}
if (shortest_with_displacing > -1
&& (shortest_with_displacing < shortest_without_displacing
|| !count_without_displacing))
return TRUE;
return FALSE;
}
/* have monster wield a pick-axe if it wants to dig and it has one;
return True if it spends this move wielding one, False otherwise */
boolean
m_digweapon_check(
struct monst *mtmp,
coordxy nix, coordxy niy)
{
boolean can_tunnel = FALSE;
struct obj *mw_tmp = MON_WEP(mtmp);