forked from NetHack/NetHack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhack.c
More file actions
2916 lines (2700 loc) · 98.7 KB
/
hack.c
File metadata and controls
2916 lines (2700 loc) · 98.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* NetHack 3.6 hack.c $NHDT-Date: 1464485934 2016/05/29 01:38:54 $ $NHDT-Branch: NetHack-3.6.0 $:$NHDT-Revision: 1.168 $ */
/* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
/* NetHack may be freely redistributed. See license for details. */
#include "hack.h"
/* #define DEBUG */ /* uncomment for debugging */
STATIC_DCL void NDECL(maybe_wail);
STATIC_DCL int NDECL(moverock);
STATIC_DCL int FDECL(still_chewing, (XCHAR_P, XCHAR_P));
STATIC_DCL void NDECL(dosinkfall);
STATIC_DCL boolean FDECL(findtravelpath, (BOOLEAN_P));
STATIC_DCL boolean FDECL(trapmove, (int, int, struct trap *));
STATIC_DCL void NDECL(switch_terrain);
STATIC_DCL struct monst *FDECL(monstinroom, (struct permonst *, int));
STATIC_DCL boolean FDECL(doorless_door, (int, int));
STATIC_DCL void FDECL(move_update, (BOOLEAN_P));
#define IS_SHOP(x) (rooms[x].rtype >= SHOPBASE)
static anything tmp_anything;
anything *
uint_to_any(ui)
unsigned ui;
{
tmp_anything = zeroany;
tmp_anything.a_uint = ui;
return &tmp_anything;
}
anything *
long_to_any(lng)
long lng;
{
tmp_anything = zeroany;
tmp_anything.a_long = lng;
return &tmp_anything;
}
anything *
monst_to_any(mtmp)
struct monst *mtmp;
{
tmp_anything = zeroany;
tmp_anything.a_monst = mtmp;
return &tmp_anything;
}
anything *
obj_to_any(obj)
struct obj *obj;
{
tmp_anything = zeroany;
tmp_anything.a_obj = obj;
return &tmp_anything;
}
boolean
revive_nasty(x, y, msg)
int x, y;
const char *msg;
{
register struct obj *otmp, *otmp2;
struct monst *mtmp;
coord cc;
boolean revived = FALSE;
for (otmp = 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_OVL int
moverock()
{
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 != 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(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 && ((ttmp->ttyp == PIT)
|| (ttmp->ttyp == SPIKED_PIT))))) {
if (Blind)
feel_location(sx, sy);
if (canspotmon(mtmp)) {
pline("There's %s on the other side.", a_monnam(mtmp));
} else {
You_hear("a monster behind %s.", the(xname(otmp)));
map_invisible(rx, ry);
}
if (flags.verbose)
pline("Perhaps that's why %s cannot move it.",
u.usteed ? y_monnam(u.usteed) : "you");
goto cannot_push;
}
if (ttmp) {
/* 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)
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:
case TELEP_TRAP: {
int newlev = 0; /* lint suppression */
d_level dest;
if (ttmp->ttyp == LEVEL_TELEP) {
newlev = random_teleport_level();
if (newlev == depth(&u.uz) || In_endgame(&u.uz))
/* trap didn't work; skip "disappears" message */
goto dopush;
}
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 (moves > lastmovetime + 2 || moves < lastmovetime)
pline("With %s effort you move %s.",
throws_rocks(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 = 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(youmonst.data)) {
if (u.usteed && P_SKILL(P_RIDING) < P_BASIC) {
You("aren't skilled enough to %s %s from %s.",
(flags.pickup && !Sokoban) ? "pick up" : "push aside",
the(xname(otmp)), y_monnam(u.usteed));
} else {
pline("However, you can easily %s.",
(flags.pickup && !Sokoban) ? "pick it up"
: "push it aside");
sokoban_guilt();
break;
}
break;
}
if (!u.usteed
&& (((!invent || inv_weight() <= -850)
&& (!u.dx || !u.dy || (IS_ROCK(levl[u.ux][sy].typ)
&& IS_ROCK(levl[sx][u.uy].typ))))
|| verysmall(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. Returns TRUE if still eating, FALSE
* when done.
*/
STATIC_OVL int
still_chewing(x, y)
xchar x, y;
{
struct rm *lev = &levl[x][y];
struct obj *boulder = sobj_at(BOULDER, x, y);
const char *digtxt = (char *) 0, *dmgtxt = (char *) 0;
if (context.digging.down) /* not continuing previous dig (w/ pick-axe) */
(void) memset((genericptr_t) &context.digging, 0,
sizeof (struct dig_info));
if (!boulder && IS_ROCK(lev->typ) && !may_dig(x, y)) {
You("hurt your teeth on the %s.",
(lev->typ == IRONBARS)
? "bars"
: IS_TREE(lev->typ)
? "tree"
: "hard stone");
nomul(0);
return 1;
} else if (context.digging.pos.x != x || context.digging.pos.y != y
|| !on_level(&context.digging.level, &u.uz)) {
context.digging.down = FALSE;
context.digging.chew = TRUE;
context.digging.warned = FALSE;
context.digging.pos.x = x;
context.digging.pos.y = y;
assign_level(&context.digging.level, &u.uz);
/* solid rock takes more work & time to dig through */
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 ((context.digging.effort += (30 + u.udaminc)) <= 100) {
if (flags.verbose)
You("%s chewing on the %s.",
context.digging.chew ? "continue" : "begin",
boulder
? "boulder"
: IS_TREE(lev->typ)
? "tree"
: IS_ROCK(lev->typ)
? "rock"
: (lev->typ == IRONBARS)
? "bars"
: "door");
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) &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, 10L * ACURRSTR);
dmgtxt = "damage";
}
digtxt = "chew a hole in the wall.";
if (level.flags.is_maze_lev) {
lev->typ = ROOM;
} else if (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) {
digtxt = "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, 400L);
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) &context.digging, 0,
sizeof (struct dig_info));
return 0;
}
void
movobj(obj, ox, oy)
register struct obj *obj;
register xchar ox, oy;
{
/* optimize by leaving on the fobj chain? */
remove_object(obj);
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_OVL void
dosinkfall()
{
register struct obj *obj;
int dmg;
boolean lev_boots = (uarmf && uarmf->otyp == LEVITATION_BOOTS),
innate_lev = ((HLevitation & (FROMOUTSIDE | FROMFORM)) != 0L),
ufall = (!innate_lev && !(HFlying || EFlying)); /* BFlying */
if (!ufall) {
You(innate_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 = 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 (*aftermv)() 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 'aftermv' 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(x, y)
register xchar x, 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(x, y)
register xchar x, y;
{
return (boolean) !(IS_STWALL(levl[x][y].typ)
&& (levl[x][y].wall_info & W_NONPASSWALL));
}
boolean
bad_rock(mdat, x, y)
struct permonst *mdat;
register xchar x, 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(mon)
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 == &youmonst) ? inv_weight() + weight_cap()
: curr_mon_load(mon);
if (amt > 600)
return 2;
/* Sokoban restriction applies to hero only */
if (mon == &youmonst && Sokoban)
return 3;
/* can squeeze through */
return 0;
}
boolean
invocation_pos(x, y)
xchar x, y;
{
return (boolean) (Invocation_lev(&u.uz)
&& x == inv_pos.x && y == 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(ux, uy, dx, dy, mode)
int ux, uy, dx, dy;
int mode;
{
int x = ux + dx;
int y = uy + dy;
register struct rm *tmpr = &levl[x][y];
register struct rm *ust;
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 ((dmgtype(youmonst.data, AD_RUST)
|| dmgtype(youmonst.data, AD_CORR)) && mode == DO_MOVE
&& still_chewing(x, y)) {
return FALSE;
}
if (!(Passes_walls || passes_bars(youmonst.data))) {
if (mode == DO_MOVE && iflags.mention_walls)
You("cannot pass through the bars.");
return FALSE;
}
} else if (tunnels(youmonst.data) && !needspick(youmonst.data)) {
/* Eat the rock. */
if (mode == DO_MOVE && still_chewing(x, y))
return FALSE;
} else if (flags.autodig && !context.run && !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 (iflags.mention_walls)
pline("It's %s.", IS_WALL(tmpr->typ) ? "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(&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(youmonst.data) && !needspick(youmonst.data)) {
/* Eat the door. */
if (mode == DO_MOVE && still_chewing(x, y))
return FALSE;
} else {
if (mode == DO_MOVE) {
if (amorphous(youmonst.data))
You(
"try to ooze under the door, but can't squeeze your possessions through.");
if (flags.autoopen && !context.run && !Confusion
&& !Stunned && !Fumbling) {
context.door_opened = 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);
}
} 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 || iflags.mention_walls)
You_cant("move diagonally into an intact doorway.");
}
return FALSE;
}
}
}
if (dx && dy && bad_rock(youmonst.data, ux, y)
&& bad_rock(youmonst.data, x, uy)) {
/* Move at a diagonal. */
switch (cant_squeeze_thru(&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 (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(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 && iflags.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) && (context.run >= 2)
&& mode != TEST_TRAV)
return FALSE;
if (mode == DO_MOVE) {
/* tunneling monsters will chew before pushing */
if (tunnels(youmonst.data) && !needspick(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(youmonst.data) && !needspick(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;
}
#ifdef DEBUG
static boolean trav_debug = FALSE;
/* in this case, toggle display of travel debug info */
int wiz_debug_cmd_traveldisplay()
{
trav_debug = !trav_debug;
return 0;
}
#endif /* DEBUG */
/*
* 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_OVL boolean
findtravelpath(guess)
boolean guess;
{
/* if travel to adjacent, reachable location, use normal movement rules */
if (!guess && context.travel1 && distmin(u.ux, u.uy, u.tx, u.ty) == 1
&& !(u.ux != u.tx && u.uy != u.ty && NODIAG(u.umonnum))) {
context.run = 0;
if (test_move(u.ux, u.uy, u.tx - u.ux, u.ty - u.uy, TEST_MOVE)) {
u.dx = u.tx - u.ux;
u.dy = u.ty - u.uy;
nomul(0);
iflags.travelcc.x = iflags.travelcc.y = -1;
return TRUE;
}
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 (guess) {
tx = u.ux;
ty = u.uy;
ux = u.tx;
uy = u.ty;
} else {
tx = u.tx;
ty = u.ty;
ux = u.ux;
uy = u.uy;
}
noguess:
(void) memset((genericptr_t) travel, 0, sizeof(travel));
travelstepx[0][0] = tx;
travelstepy[0][0] = ty;
while (n != 0) {
int nn = 0;
for (i = 0; i < n; i++) {
int dir;
int x = travelstepx[set][i];
int y = travelstepy[set][i];
static int ordered[] = { 0, 2, 4, 6, 1, 3, 5, 7 };
/* no diagonal movement for grid bugs */
int dirmax = NODIAG(u.umonnum) ? 4 : 8;
boolean alreadyrepeated = FALSE;
for (dir = 0; dir < dirmax; ++dir) {
int nx = x + xdir[ordered[dir]];
int ny = y + ydir[ordered[dir]];
/*
* When guessing and trying to travel as close as possible
* to an unreachable target space, don't include spaces
* that would never be picked as a guessed target in the
* travel matrix describing hero-reachable spaces.
* This stops travel from getting confused and moving
* the hero back and forth in certain degenerate
* configurations of sight-blocking obstacles, e.g.
*
* T 1. Dig this out and carry enough to not be
* #### able to squeeze through diagonal gaps.
* #--.--- Stand at @ and target travel at space T.
* @.....
* |.....
*
* T 2. couldsee() marks spaces marked a and x
* #### as eligible guess spaces to move the hero
* a--.--- towards. Space a is closest to T, so it
* @xxxxx gets chosen. Travel system moves @ right
* |xxxxx to travel to space a.
*
* T 3. couldsee() marks spaces marked b, c and x
* #### as eligible guess spaces to move the hero
* a--c--- towards. Since findtravelpath() is called
* b@xxxx repeatedly during travel, it doesn't
* |xxxxx remember that it wanted to go to space a,
* so in comparing spaces b and c, b is
* chosen, since it seems like the closest
* eligible space to T. Travel system moves @
* left to go to space b.
*
* 4. Go to 2.
*
* By limiting the travel matrix here, space a in the
* example above is never included in it, preventing
* the cycle.
*/
if (!isok(nx, ny) || (guess && !couldsee(nx, ny)))
continue;
if ((!Passes_walls && !can_ooze(&youmonst)
&& closed_door(x, y)) || sobj_at(BOULDER, x, y)
|| test_move(x, y, nx-x, ny-y, TEST_TRAP)) {
/* closed doors and boulders usually
* cause a delay, so prefer another path */
if (travel[x][y] > radius - 3) {
if (!alreadyrepeated) {
travelstepx[1 - set][nn] = x;
travelstepy[1 - set][nn] = y;