/
hack.c
3331 lines (3083 loc) · 118 KB
/
hack.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 hack.c $NHDT-Date: 1627951429 2021/08/03 00:43:49 $ $NHDT-Branch: NetHack-3.7 $:$NHDT-Revision: 1.291 $ */
/* 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"
/* #define DEBUG */ /* uncomment for debugging */
static void maybe_wail(void);
static int moverock(void);
static void dosinkfall(void);
static boolean findtravelpath(int);
static boolean trapmove(int, int, struct trap *);
static struct monst *monstinroom(struct permonst *, int);
static boolean doorless_door(int, int);
static void move_update(boolean);
static int pickup_checks(void);
static void maybe_smudge_engr(int, int, int, int);
static void check_buried_zombies(xchar, xchar);
static void domove_core(void);
#define IS_SHOP(x) (g.rooms[x].rtype >= SHOPBASE)
/* mode values for findtravelpath() */
#define TRAVP_TRAVEL 0
#define TRAVP_GUESS 1
#define TRAVP_VALID 2
anything *
uint_to_any(unsigned ui)
{
g.tmp_anything = cg.zeroany;
g.tmp_anything.a_uint = ui;
return &g.tmp_anything;
}
anything *
long_to_any(long lng)
{
g.tmp_anything = cg.zeroany;
g.tmp_anything.a_long = lng;
return &g.tmp_anything;
}
anything *
monst_to_any(struct monst *mtmp)
{
g.tmp_anything = cg.zeroany;
g.tmp_anything.a_monst = mtmp;
return &g.tmp_anything;
}
anything *
obj_to_any(struct obj *obj)
{
g.tmp_anything = cg.zeroany;
g.tmp_anything.a_obj = obj;
return &g.tmp_anything;
}
boolean
revive_nasty(int x, int y, const char *msg)
{
register struct obj *otmp, *otmp2;
struct monst *mtmp;
coord cc;
boolean revived = FALSE;
for (otmp = g.level.objects[x][y]; otmp; otmp = otmp2) {
otmp2 = otmp->nexthere;
if (otmp->otyp == CORPSE
&& (is_rider(&mons[otmp->corpsenm])
|| otmp->corpsenm == PM_WIZARD_OF_YENDOR)) {
/* move any living monster already at that location */
if ((mtmp = m_at(x, y)) && enexto(&cc, x, y, mtmp->data))
rloc_to(mtmp, cc.x, cc.y);
if (msg)
Norep("%s", msg);
revived = revive_corpse(otmp);
}
}
/* this location might not be safe, if not, move revived monster */
if (revived) {
mtmp = m_at(x, y);
if (mtmp && !goodpos(x, y, mtmp, 0)
&& enexto(&cc, x, y, mtmp->data)) {
rloc_to(mtmp, cc.x, cc.y);
}
/* else impossible? */
}
return revived;
}
static int
moverock(void)
{
register xchar rx, ry, sx, sy;
register struct obj *otmp;
register struct trap *ttmp;
register struct monst *mtmp;
sx = u.ux + u.dx, sy = u.uy + u.dy; /* boulder starting position */
while ((otmp = sobj_at(BOULDER, sx, sy)) != 0) {
/* make sure that this boulder is visible as the top object */
if (otmp != g.level.objects[sx][sy])
movobj(otmp, sx, sy);
rx = u.ux + 2 * u.dx; /* boulder destination position */
ry = u.uy + 2 * u.dy;
nomul(0);
if (Levitation || Is_airlevel(&u.uz)) {
if (Blind)
feel_location(sx, sy);
You("don't have enough leverage to push %s.", the(xname(otmp)));
/* Give them a chance to climb over it? */
return -1;
}
if (verysmall(g.youmonst.data) && !u.usteed) {
if (Blind)
feel_location(sx, sy);
pline("You're too small to push that %s.", xname(otmp));
goto cannot_push;
}
if (isok(rx, ry) && !IS_ROCK(levl[rx][ry].typ)
&& levl[rx][ry].typ != IRONBARS
&& (!IS_DOOR(levl[rx][ry].typ) || !(u.dx && u.dy)
|| doorless_door(rx, ry)) && !sobj_at(BOULDER, rx, ry)) {
ttmp = t_at(rx, ry);
mtmp = m_at(rx, ry);
/* KMH -- Sokoban doesn't let you push boulders diagonally */
if (Sokoban && u.dx && u.dy) {
if (Blind)
feel_location(sx, sy);
pline("%s won't roll diagonally on this %s.",
The(xname(otmp)), surface(sx, sy));
goto cannot_push;
}
if (revive_nasty(rx, ry, "You sense movement on the other side."))
return -1;
if (mtmp && !noncorporeal(mtmp->data)
&& (!mtmp->mtrapped
|| !(ttmp && is_pit(ttmp->ttyp)))) {
boolean deliver_part1 = FALSE;
if (Blind)
feel_location(sx, sy);
if (canspotmon(mtmp)) {
pline("There's %s on the other side.", a_monnam(mtmp));
deliver_part1 = TRUE;
} else {
You_hear("a monster behind %s.", the(xname(otmp)));
if (!Deaf)
deliver_part1 = TRUE;
map_invisible(rx, ry);
}
if (flags.verbose) {
char you_or_steed[BUFSZ];
Strcpy(you_or_steed,
u.usteed ? y_monnam(u.usteed) : "you");
pline("%s%s cannot move %s.",
deliver_part1
? "Perhaps that's why "
: "",
deliver_part1
? you_or_steed
: upstart(you_or_steed),
deliver_part1
? "it"
: the(xname(otmp)));
}
goto cannot_push;
}
if (ttmp) {
int newlev = 0; /* lint suppression */
d_level dest;
/* if a trap operates on the boulder, don't attempt
to move any others at this location; return -1
if another boulder is in hero's way, or 0 if he
should advance to the vacated boulder position */
switch (ttmp->ttyp) {
case LANDMINE:
if (rn2(10)) {
obj_extract_self(otmp);
place_object(otmp, rx, ry);
newsym(sx, sy);
pline("KAABLAMM!!! %s %s land mine.",
Tobjnam(otmp, "trigger"),
ttmp->madeby_u ? "your" : "a");
blow_up_landmine(ttmp);
/* if the boulder remains, it should fill the pit */
fill_pit(u.ux, u.uy);
if (cansee(rx, ry))
newsym(rx, ry);
return sobj_at(BOULDER, sx, sy) ? -1 : 0;
}
break;
case SPIKED_PIT:
case PIT:
obj_extract_self(otmp);
/* vision kludge to get messages right;
the pit will temporarily be seen even
if this is one among multiple boulders */
if (!Blind)
g.viz_array[ry][rx] |= IN_SIGHT;
if (!flooreffects(otmp, rx, ry, "fall")) {
place_object(otmp, rx, ry);
}
if (mtmp && !Blind)
newsym(rx, ry);
return sobj_at(BOULDER, sx, sy) ? -1 : 0;
case HOLE:
case TRAPDOOR:
if (Blind)
pline("Kerplunk! You no longer feel %s.",
the(xname(otmp)));
else
pline("%s%s and %s a %s in the %s!",
Tobjnam(otmp, (ttmp->ttyp == TRAPDOOR)
? "trigger"
: "fall"),
(ttmp->ttyp == TRAPDOOR) ? "" : " into",
otense(otmp, "plug"),
(ttmp->ttyp == TRAPDOOR) ? "trap door" : "hole",
surface(rx, ry));
deltrap(ttmp);
delobj(otmp);
bury_objs(rx, ry);
levl[rx][ry].wall_info &= ~W_NONDIGGABLE;
levl[rx][ry].candig = 1;
if (cansee(rx, ry))
newsym(rx, ry);
return sobj_at(BOULDER, sx, sy) ? -1 : 0;
case LEVEL_TELEP:
/* 20% chance of picking current level; 100% chance for
that if in single-level branch (Knox) or in endgame */
newlev = random_teleport_level();
/* if trap doesn't work, skip "disappears" message */
if (newlev == depth(&u.uz))
goto dopush;
/*FALLTHRU*/
case TELEP_TRAP:
if (u.usteed)
pline("%s pushes %s and suddenly it disappears!",
upstart(y_monnam(u.usteed)), the(xname(otmp)));
else
You("push %s and suddenly it disappears!",
the(xname(otmp)));
if (ttmp->ttyp == TELEP_TRAP) {
(void) rloco(otmp);
} else {
obj_extract_self(otmp);
add_to_migration(otmp);
get_level(&dest, newlev);
otmp->ox = dest.dnum;
otmp->oy = dest.dlevel;
otmp->owornmask = (long) MIGR_RANDOM;
}
seetrap(ttmp);
return sobj_at(BOULDER, sx, sy) ? -1 : 0;
default:
break; /* boulder not affected by this trap */
}
}
if (closed_door(rx, ry))
goto nopushmsg;
if (boulder_hits_pool(otmp, rx, ry, TRUE))
continue;
/*
* Re-link at top of fobj chain so that pile order is preserved
* when level is restored.
*/
if (otmp != fobj) {
remove_object(otmp);
place_object(otmp, otmp->ox, otmp->oy);
}
{
#ifdef LINT /* static long lastmovetime; */
long lastmovetime;
lastmovetime = 0;
#else
/* note: reset to zero after save/restore cycle */
static NEARDATA long lastmovetime;
#endif
dopush:
if (!u.usteed) {
if (g.moves > lastmovetime + 2 || g.moves < lastmovetime)
pline("With %s effort you move %s.",
throws_rocks(g.youmonst.data) ? "little"
: "great",
the(xname(otmp)));
exercise(A_STR, TRUE);
} else
pline("%s moves %s.", upstart(y_monnam(u.usteed)),
the(xname(otmp)));
lastmovetime = g.moves;
}
/* Move the boulder *after* the message. */
if (glyph_is_invisible(levl[rx][ry].glyph))
unmap_object(rx, ry);
movobj(otmp, rx, ry); /* does newsym(rx,ry) */
if (Blind) {
feel_location(rx, ry);
feel_location(sx, sy);
} else {
newsym(sx, sy);
}
} else {
nopushmsg:
if (u.usteed)
pline("%s tries to move %s, but cannot.",
upstart(y_monnam(u.usteed)), the(xname(otmp)));
else
You("try to move %s, but in vain.", the(xname(otmp)));
if (Blind)
feel_location(sx, sy);
cannot_push:
if (throws_rocks(g.youmonst.data)) {
boolean
canpickup = (!Sokoban
/* similar exception as in can_lift():
when poly'd into a giant, you can
pick up a boulder if you have a free
slot or into the overflow ('#') slot
unless already carrying at least one */
&& (inv_cnt(FALSE) < 52 || !carrying(BOULDER))),
willpickup = (canpickup
&& (flags.pickup && !g.context.nopick)
&& autopick_testobj(otmp, TRUE));
if (u.usteed && P_SKILL(P_RIDING) < P_BASIC) {
You("aren't skilled enough to %s %s from %s.",
willpickup ? "pick up" : "push aside",
the(xname(otmp)), y_monnam(u.usteed));
} else {
/*
* will pick up: you easily pick it up
* can but won't: you maneuver over it and could pick it up
* can't pick up: you maneuver over it (possibly followed
* by feedback from failed auto-pickup attempt)
*/
pline("However, you %s%s.",
willpickup ? "easily pick it up"
: "maneuver over it",
(canpickup && !willpickup)
? " and could pick it up"
: "");
/* similar to dropping everything and squeezing onto
a Sokoban boulder's spot, moving to same breaks the
Sokoban rules because on next step you could go
past it without pushing it to plug a pit or hole */
sokoban_guilt();
break;
}
break;
}
if (!u.usteed
&& (((!g.invent || inv_weight() <= -850)
&& (!u.dx || !u.dy || (IS_ROCK(levl[u.ux][sy].typ)
&& IS_ROCK(levl[sx][u.uy].typ))))
|| verysmall(g.youmonst.data))) {
pline(
"However, you can squeeze yourself into a small opening.");
sokoban_guilt();
break;
} else
return -1;
}
}
return 0;
}
/*
* still_chewing()
*
* Chew on a wall, door, or boulder. [What about statues?]
* Returns TRUE if still eating, FALSE when done.
*/
int
still_chewing(xchar x, xchar y)
{
struct rm *lev = &levl[x][y];
struct obj *boulder = sobj_at(BOULDER, x, y);
const char *digtxt = (char *) 0, *dmgtxt = (char *) 0;
if (g.context.digging.down) /* not continuing previous dig (w/ pick-axe) */
(void) memset((genericptr_t) &g.context.digging, 0,
sizeof (struct dig_info));
if (!boulder
&& ((IS_ROCK(lev->typ) && !may_dig(x, y))
/* may_dig() checks W_NONDIGGABLE but doesn't handle iron bars */
|| (lev->typ == IRONBARS && (lev->wall_info & W_NONDIGGABLE)))) {
You("hurt your teeth on the %s.",
(lev->typ == IRONBARS)
? "bars"
: IS_TREE(lev->typ)
? "tree"
: "hard stone");
nomul(0);
return 1;
} else if (lev->typ == IRONBARS
&& metallivorous(g.youmonst.data) && u.uhunger > 1500) {
/* finishing eating via 'morehungry()' doesn't handle choking */
You("are too full to eat the bars.");
nomul(0);
return 1;
} else if (!g.context.digging.chew
|| g.context.digging.pos.x != x
|| g.context.digging.pos.y != y
|| !on_level(&g.context.digging.level, &u.uz)) {
g.context.digging.down = FALSE;
g.context.digging.chew = TRUE;
g.context.digging.warned = FALSE;
g.context.digging.pos.x = x;
g.context.digging.pos.y = y;
assign_level(&g.context.digging.level, &u.uz);
/* solid rock takes more work & time to dig through */
g.context.digging.effort =
(IS_ROCK(lev->typ) && !IS_TREE(lev->typ) ? 30 : 60) + u.udaminc;
You("start chewing %s %s.",
(boulder || IS_TREE(lev->typ) || lev->typ == IRONBARS)
? "on a"
: "a hole in the",
boulder
? "boulder"
: IS_TREE(lev->typ)
? "tree"
: IS_ROCK(lev->typ)
? "rock"
: (lev->typ == IRONBARS)
? "bar"
: "door");
watch_dig((struct monst *) 0, x, y, FALSE);
return 1;
} else if ((g.context.digging.effort += (30 + u.udaminc)) <= 100) {
if (flags.verbose)
You("%s chewing on the %s.",
g.context.digging.chew ? "continue" : "begin",
boulder
? "boulder"
: IS_TREE(lev->typ)
? "tree"
: IS_ROCK(lev->typ)
? "rock"
: (lev->typ == IRONBARS)
? "bars"
: "door");
g.context.digging.chew = TRUE;
watch_dig((struct monst *) 0, x, y, FALSE);
return 1;
}
/* Okay, you've chewed through something */
u.uconduct.food++;
u.uhunger += rnd(20);
if (boulder) {
delobj(boulder); /* boulder goes bye-bye */
You("eat the boulder."); /* yum */
/*
* The location could still block because of
* 1. More than one boulder
* 2. Boulder stuck in a wall/stone/door.
*
* [perhaps use does_block() below (from vision.c)]
*/
if (IS_ROCK(lev->typ) || closed_door(x, y)
|| sobj_at(BOULDER, x, y)) {
block_point(x, y); /* delobj will unblock the point */
/* reset dig state */
(void) memset((genericptr_t) &g.context.digging, 0,
sizeof (struct dig_info));
return 1;
}
} else if (IS_WALL(lev->typ)) {
if (*in_rooms(x, y, SHOPBASE)) {
add_damage(x, y, SHOP_WALL_DMG);
dmgtxt = "damage";
}
digtxt = "chew a hole in the wall.";
if (g.level.flags.is_maze_lev) {
lev->typ = ROOM;
} else if (g.level.flags.is_cavernous_lev && !in_town(x, y)) {
lev->typ = CORR;
} else {
lev->typ = DOOR;
lev->doormask = D_NODOOR;
}
} else if (IS_TREE(lev->typ)) {
digtxt = "chew through the tree.";
lev->typ = ROOM;
} else if (lev->typ == IRONBARS) {
if (metallivorous(g.youmonst.data)) { /* should always be True here */
/* arbitrary amount; unlike proper eating, nutrition is
bestowed in a lump sum at the end */
int nut = (int) objects[HEAVY_IRON_BALL].oc_weight;
/* lesshungry() requires that victual be set up, so skip it;
morehungry() of a negative amount will increase nutrition
without any possibility of choking to death on the meal;
updates hunger state and requests status update if changed */
morehungry(-nut);
}
digtxt = (x == u.ux && y == u.uy)
? "devour the iron bars."
: "eat through the bars.";
dissolve_bars(x, y);
} else if (lev->typ == SDOOR) {
if (lev->doormask & D_TRAPPED) {
lev->doormask = D_NODOOR;
b_trapped("secret door", 0);
} else {
digtxt = "chew through the secret door.";
lev->doormask = D_BROKEN;
}
lev->typ = DOOR;
} else if (IS_DOOR(lev->typ)) {
if (*in_rooms(x, y, SHOPBASE)) {
add_damage(x, y, SHOP_DOOR_COST);
dmgtxt = "break";
}
if (lev->doormask & D_TRAPPED) {
lev->doormask = D_NODOOR;
b_trapped("door", 0);
} else {
digtxt = "chew through the door.";
lev->doormask = D_BROKEN;
}
} else { /* STONE or SCORR */
digtxt = "chew a passage through the rock.";
lev->typ = CORR;
}
unblock_point(x, y); /* vision */
newsym(x, y);
if (digtxt)
You1(digtxt); /* after newsym */
if (dmgtxt)
pay_for_damage(dmgtxt, FALSE);
(void) memset((genericptr_t) &g.context.digging, 0,
sizeof (struct dig_info));
return 0;
}
void
movobj(register struct obj *obj, register xchar ox, register xchar oy)
{
/* optimize by leaving on the fobj chain? */
remove_object(obj);
maybe_unhide_at(obj->ox, obj->oy);
newsym(obj->ox, obj->oy);
place_object(obj, ox, oy);
newsym(ox, oy);
}
static NEARDATA const char fell_on_sink[] = "fell onto a sink";
static void
dosinkfall(void)
{
register struct obj *obj;
int dmg;
boolean lev_boots = (uarmf && uarmf->otyp == LEVITATION_BOOTS),
innate_lev = ((HLevitation & (FROMOUTSIDE | FROMFORM)) != 0L),
/* to handle being chained to buried iron ball, trying to
levitate but being blocked, then moving onto adjacent sink;
no need to worry about being blocked by terrain because we
couldn't be over a sink at the same time */
blockd_lev = (BLevitation == I_SPECIAL),
ufall = (!innate_lev && !blockd_lev
&& !(HFlying || EFlying)); /* BFlying */
if (!ufall) {
You((innate_lev || blockd_lev) ? "wobble unsteadily for a moment."
: "gain control of your flight.");
} else {
long save_ELev = ELevitation, save_HLev = HLevitation;
/* fake removal of levitation in advance so that final
disclosure will be right in case this turns out to
be fatal; fortunately the fact that rings and boots
are really still worn has no effect on bones data */
ELevitation = HLevitation = 0L;
You("crash to the floor!");
dmg = rn1(8, 25 - (int) ACURR(A_CON));
losehp(Maybe_Half_Phys(dmg), fell_on_sink, NO_KILLER_PREFIX);
exercise(A_DEX, FALSE);
selftouch("Falling, you");
for (obj = g.level.objects[u.ux][u.uy]; obj; obj = obj->nexthere)
if (obj->oclass == WEAPON_CLASS || is_weptool(obj)) {
You("fell on %s.", doname(obj));
losehp(Maybe_Half_Phys(rnd(3)), fell_on_sink,
NO_KILLER_PREFIX);
exercise(A_CON, FALSE);
}
ELevitation = save_ELev;
HLevitation = save_HLev;
}
/*
* Interrupt multi-turn putting on/taking off of armor (in which
* case we reached the sink due to being teleported while busy;
* in 3.4.3, Boots_on()/Boots_off() [called via (*afternmv)() when
* 'multi' reaches 0] triggered a crash if we were donning/doffing
* levitation boots [because the Boots_off() below causes 'uarmf'
* to be null by the time 'afternmv' gets called]).
*
* Interrupt donning/doffing if we fall onto the sink, or if the
* code below is going to remove levitation boots even when we
* haven't fallen (innate floating or flying becoming unblocked).
*/
if (ufall || lev_boots) {
(void) stop_donning(lev_boots ? uarmf : (struct obj *) 0);
/* recalculate in case uarmf just got set to null */
lev_boots = (uarmf && uarmf->otyp == LEVITATION_BOOTS);
}
/* remove worn levitation items */
ELevitation &= ~W_ARTI;
HLevitation &= ~(I_SPECIAL | TIMEOUT);
HLevitation++;
if (uleft && uleft->otyp == RIN_LEVITATION) {
obj = uleft;
Ring_off(obj);
off_msg(obj);
}
if (uright && uright->otyp == RIN_LEVITATION) {
obj = uright;
Ring_off(obj);
off_msg(obj);
}
if (lev_boots) {
obj = uarmf;
(void) Boots_off();
off_msg(obj);
}
HLevitation--;
/* probably moot; we're either still levitating or went
through float_down(), but make sure BFlying is up to date */
float_vs_flight();
}
/* intended to be called only on ROCKs or TREEs */
boolean
may_dig(register xchar x, register xchar y)
{
struct rm *lev = &levl[x][y];
return (boolean) !((IS_STWALL(lev->typ) || IS_TREE(lev->typ))
&& (lev->wall_info & W_NONDIGGABLE));
}
boolean
may_passwall(register xchar x, register xchar y)
{
return (boolean) !(IS_STWALL(levl[x][y].typ)
&& (levl[x][y].wall_info & W_NONPASSWALL));
}
boolean
bad_rock(struct permonst *mdat, register xchar x, register xchar y)
{
return (boolean) ((Sokoban && sobj_at(BOULDER, x, y))
|| (IS_ROCK(levl[x][y].typ)
&& (!tunnels(mdat) || needspick(mdat)
|| !may_dig(x, y))
&& !(passes_walls(mdat) && may_passwall(x, y))));
}
/* caller has already decided that it's a tight diagonal; check whether a
monster--who might be the hero--can fit through, and if not then return
the reason why: 1: can't fit, 2: possessions won't fit, 3: sokoban
returns 0 if we can squeeze through */
int
cant_squeeze_thru(struct monst *mon)
{
int amt;
struct permonst *ptr = mon->data;
/* too big? */
if (bigmonst(ptr)
&& !(amorphous(ptr) || is_whirly(ptr) || noncorporeal(ptr)
|| slithy(ptr) || can_fog(mon)))
return 1;
/* lugging too much junk? */
amt = (mon == &g.youmonst) ? inv_weight() + weight_cap()
: curr_mon_load(mon);
if (amt > 600)
return 2;
/* Sokoban restriction applies to hero only */
if (mon == &g.youmonst && Sokoban)
return 3;
/* can squeeze through */
return 0;
}
boolean
invocation_pos(xchar x, xchar y)
{
return (boolean) (Invocation_lev(&u.uz)
&& x == g.inv_pos.x && y == g.inv_pos.y);
}
/* return TRUE if (dx,dy) is an OK place to move
* mode is one of DO_MOVE, TEST_MOVE, TEST_TRAV, or TEST_TRAP
*/
boolean
test_move(int ux, int uy, int dx, int dy, int mode)
{
int x = ux + dx;
int y = uy + dy;
register struct rm *tmpr = &levl[x][y];
register struct rm *ust;
g.context.door_opened = FALSE;
/*
* Check for physical obstacles. First, the place we are going.
*/
if (IS_ROCK(tmpr->typ) || tmpr->typ == IRONBARS) {
if (Blind && mode == DO_MOVE)
feel_location(x, y);
if (Passes_walls && may_passwall(x, y)) {
; /* do nothing */
} else if (Underwater) {
/* note: if water_friction() changes direction due to
turbulence, new target destination will always be water,
so we won't get here, hence don't need to worry about
"there" being somewhere the player isn't sure of */
if (mode == DO_MOVE)
pline("There is an obstacle there.");
return FALSE;
} else if (tmpr->typ == IRONBARS) {
if (mode == DO_MOVE
&& (dmgtype(g.youmonst.data, AD_RUST)
|| dmgtype(g.youmonst.data, AD_CORR)
|| metallivorous(g.youmonst.data))
&& still_chewing(x, y)) {
return FALSE;
}
if (!(Passes_walls || passes_bars(g.youmonst.data))) {
if (mode == DO_MOVE && flags.mention_walls)
You("cannot pass through the bars.");
return FALSE;
}
} else if (tunnels(g.youmonst.data) && !needspick(g.youmonst.data)) {
/* Eat the rock. */
if (mode == DO_MOVE && still_chewing(x, y))
return FALSE;
} else if (flags.autodig && !g.context.run && !g.context.nopick
&& uwep && is_pick(uwep)) {
/* MRKR: Automatic digging when wielding the appropriate tool */
if (mode == DO_MOVE)
(void) use_pick_axe2(uwep);
return FALSE;
} else {
if (mode == DO_MOVE) {
if (is_db_wall(x, y))
pline("That drawbridge is up!");
/* sokoban restriction stays even after puzzle is solved */
else if (Passes_walls && !may_passwall(x, y)
&& In_sokoban(&u.uz))
pline_The("Sokoban walls resist your ability.");
else if (flags.mention_walls)
pline("It's %s.",
(IS_WALL(tmpr->typ) || tmpr->typ == SDOOR) ? "a wall"
: IS_TREE(tmpr->typ) ? "a tree"
: "solid stone");
}
return FALSE;
}
} else if (IS_DOOR(tmpr->typ)) {
if (closed_door(x, y)) {
if (Blind && mode == DO_MOVE)
feel_location(x, y);
if (Passes_walls) {
; /* do nothing */
} else if (can_ooze(&g.youmonst)) {
if (mode == DO_MOVE)
You("ooze under the door.");
} else if (Underwater) {
if (mode == DO_MOVE)
pline("There is an obstacle there.");
return FALSE;
} else if (tunnels(g.youmonst.data)
&& !needspick(g.youmonst.data)) {
/* Eat the door. */
if (mode == DO_MOVE && still_chewing(x, y))
return FALSE;
} else {
if (mode == DO_MOVE) {
if (amorphous(g.youmonst.data))
You(
"try to ooze under the door, but can't squeeze your possessions through.");
if (flags.autoopen && !g.context.run
&& !Confusion && !Stunned && !Fumbling) {
g.context.door_opened
= g.context.move = doopen_indir(x, y);
} else if (x == ux || y == uy) {
if (Blind || Stunned || ACURR(A_DEX) < 10
|| Fumbling) {
if (u.usteed) {
You_cant("lead %s through that closed door.",
y_monnam(u.usteed));
} else {
pline("Ouch! You bump into a door.");
exercise(A_DEX, FALSE);
}
/* use current move; needed for the "ouch" case
but done for steed case too for consistency;
we haven't opened a door but we're going to
return False and without having 'door_opened'
set, 'move' would get reset by caller */
g.context.door_opened
= g.context.move = TRUE;
/* since we've just lied about successfully
moving, we need to manually stop running */
nomul(0);
} else
pline("That door is closed.");
}
} else if (mode == TEST_TRAV || mode == TEST_TRAP)
goto testdiag;
return FALSE;
}
} else {
testdiag:
if (dx && dy && !Passes_walls
&& (!doorless_door(x, y) || block_door(x, y))) {
/* Diagonal moves into a door are not allowed. */
if (mode == DO_MOVE) {
if (Blind)
feel_location(x, y);
if (Underwater || flags.mention_walls)
You_cant("move diagonally into an intact doorway.");
}
return FALSE;
}
}
}
if (dx && dy && bad_rock(g.youmonst.data, ux, y)
&& bad_rock(g.youmonst.data, x, uy)) {
/* Move at a diagonal. */
switch (cant_squeeze_thru(&g.youmonst)) {
case 3:
if (mode == DO_MOVE)
You("cannot pass that way.");
return FALSE;
case 2:
if (mode == DO_MOVE)
You("are carrying too much to get through.");
return FALSE;
case 1:
if (mode == DO_MOVE)
Your("body is too large to fit through.");
return FALSE;
default:
break; /* can squeeze through */
}
} else if (dx && dy && worm_cross(ux, uy, x, y)) {
/* consecutive long worm segments are at <ux,y> and <x,uy> */
if (mode == DO_MOVE)
pline("%s is in your way.", Monnam(m_at(ux, y)));
return FALSE;
}
/* Pick travel path that does not require crossing a trap.
* Avoid water and lava using the usual running rules.
* (but not u.ux/u.uy because findtravelpath walks toward u.ux/u.uy) */
if (g.context.run == 8 && (mode != DO_MOVE)
&& (x != u.ux || y != u.uy)) {
struct trap *t = t_at(x, y);
if ((t && t->tseen)
|| (!Levitation && !Flying && !is_clinger(g.youmonst.data)
&& is_pool_or_lava(x, y) && levl[x][y].seenv))
return (mode == TEST_TRAP);
}
if (mode == TEST_TRAP)
return FALSE; /* do not move through traps */
ust = &levl[ux][uy];
/* Now see if other things block our way . . */
if (dx && dy && !Passes_walls && IS_DOOR(ust->typ)
&& (!doorless_door(ux, uy) || block_entry(x, y))) {
/* Can't move at a diagonal out of a doorway with door. */
if (mode == DO_MOVE && flags.mention_walls)
You_cant("move diagonally out of an intact doorway.");
return FALSE;
}
if (sobj_at(BOULDER, x, y) && (Sokoban || !Passes_walls)) {
if (!(Blind || Hallucination) && (g.context.run >= 2)
&& mode != TEST_TRAV) {
if (mode == DO_MOVE && flags.mention_walls)
pline("A boulder blocks your path.");
return FALSE;
}
if (mode == DO_MOVE) {
/* tunneling monsters will chew before pushing */
if (tunnels(g.youmonst.data) && !needspick(g.youmonst.data)
&& !Sokoban) {
if (still_chewing(x, y))
return FALSE;
} else if (moverock() < 0)
return FALSE;
} else if (mode == TEST_TRAV) {
struct obj *obj;
/* never travel through boulders in Sokoban */
if (Sokoban)
return FALSE;
/* don't pick two boulders in a row, unless there's a way thru */
if (sobj_at(BOULDER, ux, uy) && !Sokoban) {
if (!Passes_walls
&& !(tunnels(g.youmonst.data) && !needspick(g.youmonst.data))
&& !carrying(PICK_AXE) && !carrying(DWARVISH_MATTOCK)
&& !((obj = carrying(WAN_DIGGING))
&& !objects[obj->otyp].oc_name_known))
return FALSE;
}
}
/* assume you'll be able to push it when you get there... */
}
/* OK, it is a legal place to move. */
return TRUE;
}
/*
* Find a path from the destination (u.tx,u.ty) back to (u.ux,u.uy).
* A shortest path is returned. If guess is TRUE, consider various
* inaccessible locations as valid intermediate path points.
* Returns TRUE if a path was found.
*/
static boolean
findtravelpath(int mode)
{
/* if travel to adjacent, reachable location, use normal movement rules */
if ((mode == TRAVP_TRAVEL || mode == TRAVP_VALID) && g.context.travel1
/* was '&& distmin(u.ux, u.uy, u.tx, u.ty) == 1' */
&& distu(u.tx, u.ty) <= 2 /* one step away */
/* handle restricted diagonals */
&& crawl_destination(u.tx, u.ty)) {
end_running(FALSE);
if (test_move(u.ux, u.uy, u.tx - u.ux, u.ty - u.uy, TEST_MOVE)) {
if (mode == TRAVP_TRAVEL) {
u.dx = u.tx - u.ux;
u.dy = u.ty - u.uy;
nomul(0);
iflags.travelcc.x = iflags.travelcc.y = 0;
}
return TRUE;
}
if (mode == TRAVP_TRAVEL)
g.context.run = 8;
}
if (u.tx != u.ux || u.ty != u.uy) {
xchar travel[COLNO][ROWNO];
xchar travelstepx[2][COLNO * ROWNO];
xchar travelstepy[2][COLNO * ROWNO];
xchar tx, ty, ux, uy;
int n = 1; /* max offset in travelsteps */
int set = 0; /* two sets current and previous */
int radius = 1; /* search radius */
int i;
/* If guessing, first find an "obvious" goal location. The obvious
* goal is the position the player knows of, or might figure out
* (couldsee) that is closest to the target on a straight path.
*/
if (mode == TRAVP_GUESS || mode == TRAVP_VALID) {
tx = u.ux;
ty = u.uy;
ux = u.tx;
uy = u.ty;
} else {
tx = u.tx;
ty = u.ty;