/
ball.c
1114 lines (1013 loc) · 36.7 KB
/
ball.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.6 ball.c $NHDT-Date: 1573940835 2019/11/16 21:47:15 $ $NHDT-Branch: NetHack-3.6 $:$NHDT-Revision: 1.44 $ */
/* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
/*-Copyright (c) David Cohrs, 2006. */
/* NetHack may be freely redistributed. See license for details. */
/* Ball & Chain
* =============================================================*/
#include "hack.h"
STATIC_DCL int NDECL(bc_order);
STATIC_DCL void NDECL(litter);
STATIC_OVL void NDECL(placebc_core);
STATIC_OVL void NDECL(unplacebc_core);
STATIC_DCL boolean FDECL(check_restriction, (int));
static int bcrestriction = 0;
#ifdef BREADCRUMBS
static struct breadcrumbs bcpbreadcrumbs = {0}, bcubreadcrumbs = {0};
#endif
void
ballrelease(showmsg)
boolean showmsg;
{
if (carried(uball)) {
if (showmsg)
pline("Startled, you drop the iron ball.");
if (uwep == uball)
setuwep((struct obj *) 0);
if (uswapwep == uball)
setuswapwep((struct obj *) 0);
if (uquiver == uball)
setuqwep((struct obj *) 0);
/* [this used to test 'if (uwep != uball)' but that always passes
after the setuwep() above] */
freeinv(uball); /* remove from inventory but don't place on floor */
encumber_msg();
}
}
/* ball&chain might hit hero when falling through a trap door */
void
ballfall()
{
boolean gets_hit;
gets_hit = (((uball->ox != u.ux) || (uball->oy != u.uy))
&& ((uwep == uball) ? FALSE : (boolean) rn2(5)));
ballrelease(TRUE);
if (gets_hit) {
int dmg = rn1(7, 25);
pline_The("iron ball falls on your %s.", body_part(HEAD));
if (uarmh) {
if (is_metallic(uarmh)) {
pline("Fortunately, you are wearing a hard helmet.");
dmg = 3;
} else if (flags.verbose)
pline("%s does not protect you.", Yname2(uarmh));
}
losehp(Maybe_Half_Phys(dmg), "crunched in the head by an iron ball",
NO_KILLER_PREFIX);
}
}
/*
* To make this work, we have to mess with the hero's mind. The rules for
* ball&chain are:
*
* 1. If the hero can see them, fine.
* 2. If the hero can't see either, it isn't seen.
* 3. If either is felt it is seen.
* 4. If either is felt and moved, it disappears.
*
* If the hero can see, then when a move is done, the ball and chain are
* first picked up, the positions under them are corrected, then they
* are moved after the hero moves. Not too bad.
*
* If the hero is blind, then she can "feel" the ball and/or chain at any
* time. However, when the hero moves, the felt ball and/or chain become
* unfelt and whatever was felt "under" the ball&chain appears. Pretty
* nifty, but it requires that the ball&chain "remember" what was under
* them --- i.e. they pick-up glyphs when they are felt and drop them when
* moved (and felt). When swallowed, the ball&chain are pulled completely
* off of the dungeon, but are still on the object chain. They are placed
* under the hero when she is expelled.
*/
/*
* from you.h
* int u.bglyph glyph under the ball
* int u.cglyph glyph under the chain
* int u.bc_felt mask for ball/chain being felt
* #define BC_BALL 0x01 bit mask in u.bc_felt for ball
* #define BC_CHAIN 0x02 bit mask in u.bc_felt for chain
* int u.bc_order ball & chain order
*
* u.bc_felt is also manipulated in display.c and read.c, the others only
* in this file. None of these variables are valid unless the player is
* Blind.
*/
/* values for u.bc_order */
#define BCPOS_DIFFER 0 /* ball & chain at different positions */
#define BCPOS_CHAIN 1 /* chain on top of ball */
#define BCPOS_BALL 2 /* ball on top of chain */
/*
* Place the ball & chain under the hero. Make sure that the ball & chain
* variables are set (actually only needed when blind, but what the heck).
* It is assumed that when this is called, the ball and chain are NOT
* attached to the object list.
*
* Should not be called while swallowed except on waterlevel.
*/
STATIC_OVL void
placebc_core()
{
if (!uchain || !uball) {
impossible("Where are your ball and chain?");
return;
}
(void) flooreffects(uchain, u.ux, u.uy, ""); /* chain might rust */
if (carried(uball)) { /* the ball is carried */
u.bc_order = BCPOS_DIFFER;
} else {
/* ball might rust -- already checked when carried */
(void) flooreffects(uball, u.ux, u.uy, "");
place_object(uball, u.ux, u.uy);
u.bc_order = BCPOS_CHAIN;
}
place_object(uchain, u.ux, u.uy);
u.bglyph = u.cglyph = levl[u.ux][u.uy].glyph; /* pick up glyph */
newsym(u.ux, u.uy);
bcrestriction = 0;
}
STATIC_OVL void
unplacebc_core()
{
if (u.uswallow) {
if (Is_waterlevel(&u.uz)) {
/* we need to proceed with the removal from the floor
* so that movebubbles() processing will disregard it as
* intended. Ignore all the vision stuff.
*/
if (!carried(uball))
obj_extract_self(uball);
obj_extract_self(uchain);
}
/* ball&chain not unplaced while swallowed */
return;
}
if (!carried(uball)) {
obj_extract_self(uball);
if (Blind && (u.bc_felt & BC_BALL)) /* drop glyph */
levl[uball->ox][uball->oy].glyph = u.bglyph;
newsym(uball->ox, uball->oy);
}
obj_extract_self(uchain);
if (Blind && (u.bc_felt & BC_CHAIN)) /* drop glyph */
levl[uchain->ox][uchain->oy].glyph = u.cglyph;
newsym(uchain->ox, uchain->oy);
u.bc_felt = 0; /* feel nothing */
}
STATIC_OVL boolean
check_restriction(restriction)
int restriction;
{
boolean ret = FALSE;
if (!bcrestriction || (restriction == override_restriction))
ret = TRUE;
else
ret = (bcrestriction == restriction) ? TRUE : FALSE;
return ret;
}
#ifndef BREADCRUMBS
void
placebc()
{
if (!check_restriction(0)) {
#if (NH_DEVEL_STATUS != NH_STATUS_RELEASED)
char panicbuf[BUFSZ];
Sprintf(panicbuf, "placebc denied, restriction in effect");
paniclog("placebc", panicbuf);
#endif
return;
}
if (uchain && uchain->where != OBJ_FREE) {
impossible("bc already placed?");
return;
}
placebc_core();
}
void
unplacebc()
{
if (bcrestriction) {
impossible("unplacebc denied, restriction in place");
return;
}
unplacebc_core();
}
int
unplacebc_and_covet_placebc()
{
int restriction = 0;
if (bcrestriction) {
impossible("unplacebc_and_covet_placebc denied, already restricted");
} else {
restriction = bcrestriction = rnd(400);
unplacebc_core();
}
return restriction;
}
void
lift_covet_and_placebc(pin)
int pin;
{
if (!check_restriction(pin)) {
#if (NH_DEVEL_STATUS != NH_STATUS_RELEASED)
char panicbuf[BUFSZ];
Sprintf(panicbuf, "lift_covet_and_placebc denied, %s",
(pin != bcrestriction) ? "pin mismatch"
: "restriction in effect");
paniclog("placebc", panicbuf);
#endif
return;
}
if (uchain && uchain->where != OBJ_FREE) {
impossible("bc already placed?");
return;
}
placebc_core();
}
#else /* BREADCRUMBS */
void
Placebc(funcnm, linenum)
const char *funcnm;
int linenum;
{
if (!check_restriction(0)) {
#if (NH_DEVEL_STATUS != NH_STATUS_RELEASED)
char panicbuf[BUFSZ];
Sprintf(panicbuf, "Placebc denied to %s:%d, restricted by %s:%d",
funcnm, linenum,
bcpbreadcrumbs.funcnm, bcpbreadcrumbs.linenum);
paniclog("Placebc", panicbuf);
#endif
return;
}
if ((uchain && uchain->where != OBJ_FREE)
&& bcpbreadcrumbs.in_effect) {
impossible("Placebc collision at %s:%d, already placed by %s:%d",
funcnm, linenum,
bcpbreadcrumbs.funcnm, bcpbreadcrumbs.linenum);
return;
}
bcpbreadcrumbs.in_effect = TRUE;
bcubreadcrumbs.in_effect = FALSE;
bcpbreadcrumbs.funcnm = funcnm;
bcpbreadcrumbs.linenum = linenum;
placebc_core();
}
void
Unplacebc(funcnm, linenum)
const char *funcnm;
int linenum;
{
if (bcrestriction) {
char panicbuf[BUFSZ];
Sprintf(panicbuf, "Unplacebc from %s:%d, when restricted to %s:%d",
funcnm, linenum,
bcubreadcrumbs.funcnm, bcubreadcrumbs.linenum);
paniclog("Unplacebc", panicbuf);
}
bcpbreadcrumbs.in_effect = FALSE;
bcubreadcrumbs.in_effect = TRUE;
bcubreadcrumbs.funcnm = funcnm;
bcubreadcrumbs.linenum = linenum;
unplacebc_core();
}
int
Unplacebc_and_covet_placebc(funcnm, linenum)
const char *funcnm;
int linenum;
{
int restriction = 0;
if (bcrestriction) {
impossible(
"Unplacebc_and_covet_placebc denied to %s:%d, restricted by %s:%d",
funcnm, linenum,
bcubreadcrumbs.funcnm, bcubreadcrumbs.linenum);
} else {
restriction = bcrestriction = rnd(400);
bcpbreadcrumbs.in_effect = FALSE;
bcubreadcrumbs.in_effect = TRUE;
bcubreadcrumbs.funcnm = funcnm;
bcubreadcrumbs.linenum = linenum;
unplacebc_core();
}
return restriction;
}
void
Lift_covet_and_placebc(pin, funcnm, linenum)
int pin;
char *funcnm;
int linenum;
{
if (!check_restriction(pin)) {
#if (NH_DEVEL_STATUS != NH_STATUS_RELEASED)
char panicbuf[BUFSZ];
Sprintf(panicbuf,
"Lift_covet_and_placebc denied to %s:%d, restricted by %s:%d",
funcnm, linenum,
bcpbreadcrumbs.funcnm, bcpbreadcrumbs.linenum);
paniclog("Lift_covet_and_placebc", panicbuf);
#endif
return;
}
if (uchain && uchain->where != OBJ_FREE) {
impossible("bc already placed?");
return;
}
placebc_core();
}
#endif /* BREADCRUMBS */
/*
* Return the stacking of the hero's ball & chain. This assumes that the
* hero is being punished.
*/
STATIC_OVL int
bc_order()
{
struct obj *obj;
if (uchain->ox != uball->ox || uchain->oy != uball->oy || carried(uball)
|| u.uswallow)
return BCPOS_DIFFER;
for (obj = level.objects[uball->ox][uball->oy]; obj;
obj = obj->nexthere) {
if (obj == uchain)
return BCPOS_CHAIN;
if (obj == uball)
return BCPOS_BALL;
}
impossible("bc_order: ball&chain not in same location!");
return BCPOS_DIFFER;
}
/*
* set_bc()
*
* The hero is either about to go blind or already blind and just punished.
* Set up the ball and chain variables so that the ball and chain are "felt".
*/
void
set_bc(already_blind)
int already_blind;
{
int ball_on_floor = !carried(uball);
u.bc_order = bc_order(); /* get the order */
u.bc_felt = ball_on_floor ? BC_BALL | BC_CHAIN : BC_CHAIN; /* felt */
if (already_blind || u.uswallow) {
u.cglyph = u.bglyph = levl[u.ux][u.uy].glyph;
return;
}
/*
* Since we can still see, remove the ball&chain and get the glyph that
* would be beneath them. Then put the ball&chain back. This is pretty
* disgusting, but it will work.
*/
remove_object(uchain);
if (ball_on_floor)
remove_object(uball);
newsym(uchain->ox, uchain->oy);
u.cglyph = levl[uchain->ox][uchain->oy].glyph;
if (u.bc_order == BCPOS_DIFFER) { /* different locations */
place_object(uchain, uchain->ox, uchain->oy);
newsym(uchain->ox, uchain->oy);
if (ball_on_floor) {
newsym(uball->ox, uball->oy); /* see under ball */
u.bglyph = levl[uball->ox][uball->oy].glyph;
place_object(uball, uball->ox, uball->oy);
newsym(uball->ox, uball->oy); /* restore ball */
}
} else {
u.bglyph = u.cglyph;
if (u.bc_order == BCPOS_CHAIN) {
place_object(uball, uball->ox, uball->oy);
place_object(uchain, uchain->ox, uchain->oy);
} else {
place_object(uchain, uchain->ox, uchain->oy);
place_object(uball, uball->ox, uball->oy);
}
newsym(uball->ox, uball->oy);
}
}
/*
* move_bc()
*
* Move the ball and chain. This is called twice for every move. The first
* time to pick up the ball and chain before the move, the second time to
* place the ball and chain after the move. If the ball is carried, this
* function should never have BC_BALL as part of its control.
*
* Should not be called while swallowed.
*/
void
move_bc(before, control, ballx, bally, chainx, chainy)
int before, control;
xchar ballx, bally, chainx, chainy; /* only matter !before */
{
if (Blind) {
/*
* The hero is blind. Time to work hard. The ball and chain that
* are attached to the hero are very special. The hero knows that
* they are attached, so when they move, the hero knows that they
* aren't at the last position remembered. This is complicated
* by the fact that the hero can "feel" the surrounding locations
* at any time, hence, making one or both of them show up again.
* So, we have to keep track of which is felt at any one time and
* act accordingly.
*/
if (!before) {
if ((control & BC_CHAIN) && (control & BC_BALL)) {
/*
* Both ball and chain moved. If felt, drop glyph.
*/
if (u.bc_felt & BC_BALL)
levl[uball->ox][uball->oy].glyph = u.bglyph;
if (u.bc_felt & BC_CHAIN)
levl[uchain->ox][uchain->oy].glyph = u.cglyph;
u.bc_felt = 0;
/* Pick up glyph at new location. */
u.bglyph = levl[ballx][bally].glyph;
u.cglyph = levl[chainx][chainy].glyph;
movobj(uball, ballx, bally);
movobj(uchain, chainx, chainy);
} else if (control & BC_BALL) {
if (u.bc_felt & BC_BALL) {
if (u.bc_order == BCPOS_DIFFER) { /* ball by itself */
levl[uball->ox][uball->oy].glyph = u.bglyph;
} else if (u.bc_order == BCPOS_BALL) {
if (u.bc_felt & BC_CHAIN) { /* know chain is there */
map_object(uchain, 0);
} else {
levl[uball->ox][uball->oy].glyph = u.bglyph;
}
}
u.bc_felt &= ~BC_BALL; /* no longer feel the ball */
}
/* Pick up glyph at new position. */
u.bglyph = (ballx != chainx || bally != chainy)
? levl[ballx][bally].glyph
: u.cglyph;
movobj(uball, ballx, bally);
} else if (control & BC_CHAIN) {
if (u.bc_felt & BC_CHAIN) {
if (u.bc_order == BCPOS_DIFFER) {
levl[uchain->ox][uchain->oy].glyph = u.cglyph;
} else if (u.bc_order == BCPOS_CHAIN) {
if (u.bc_felt & BC_BALL) {
map_object(uball, 0);
} else {
levl[uchain->ox][uchain->oy].glyph = u.cglyph;
}
}
u.bc_felt &= ~BC_CHAIN;
}
/* Pick up glyph at new position. */
u.cglyph = (ballx != chainx || bally != chainy)
? levl[chainx][chainy].glyph
: u.bglyph;
movobj(uchain, chainx, chainy);
}
u.bc_order = bc_order(); /* reset the order */
}
} else {
/*
* The hero is not blind. To make this work correctly, we need to
* pick up the ball and chain before the hero moves, then put them
* in their new positions after the hero moves.
*/
if (before) {
if (!control) {
/*
* Neither ball nor chain is moving, so remember which was
* on top until !before. Use the variable u.bc_order
* since it is only valid when blind.
*/
u.bc_order = bc_order();
}
remove_object(uchain);
newsym(uchain->ox, uchain->oy);
if (!carried(uball)) {
remove_object(uball);
newsym(uball->ox, uball->oy);
}
} else {
int on_floor = !carried(uball);
if ((control & BC_CHAIN)
|| (!control && u.bc_order == BCPOS_CHAIN)) {
/* If the chain moved or nothing moved & chain on top. */
if (on_floor)
place_object(uball, ballx, bally);
place_object(uchain, chainx, chainy); /* chain on top */
} else {
place_object(uchain, chainx, chainy);
if (on_floor)
place_object(uball, ballx, bally);
/* ball on top */
}
newsym(chainx, chainy);
if (on_floor)
newsym(ballx, bally);
}
}
}
/* return TRUE if the caller needs to place the ball and chain down again */
boolean
drag_ball(x, y, bc_control, ballx, bally, chainx, chainy, cause_delay,
allow_drag)
xchar x, y;
int *bc_control;
xchar *ballx, *bally, *chainx, *chainy;
boolean *cause_delay;
boolean allow_drag;
{
struct trap *t = (struct trap *) 0;
boolean already_in_rock;
/*
* Should not be called while swallowed. Should be called before
* movement, because we might want to move the ball or chain to the
* hero's old position.
*
* It is called if we are moving. It is also called if we are
* teleporting *if* the ball doesn't move and we thus must drag the
* chain. It is not called for ordinary teleportation.
*
* 'allow_drag' is only used in the ugly special case where teleporting
* must drag the chain, while an identical-looking movement must drag
* both the ball and chain.
*/
*ballx = uball->ox;
*bally = uball->oy;
*chainx = uchain->ox;
*chainy = uchain->oy;
*bc_control = 0;
*cause_delay = FALSE;
if (dist2(x, y, uchain->ox, uchain->oy) <= 2) { /* nothing moved */
move_bc(1, *bc_control, *ballx, *bally, *chainx, *chainy);
return TRUE;
}
/* only need to move the chain? */
if (carried(uball) || distmin(x, y, uball->ox, uball->oy) <= 2) {
xchar oldchainx = uchain->ox, oldchainy = uchain->oy;
*bc_control = BC_CHAIN;
move_bc(1, *bc_control, *ballx, *bally, *chainx, *chainy);
if (carried(uball)) {
/* move chain only if necessary */
if (distmin(x, y, uchain->ox, uchain->oy) > 1) {
*chainx = u.ux;
*chainy = u.uy;
}
return TRUE;
}
#define CHAIN_IN_MIDDLE(chx, chy) \
(distmin(x, y, chx, chy) <= 1 \
&& distmin(chx, chy, uball->ox, uball->oy) <= 1)
#define IS_CHAIN_ROCK(x, y) \
(IS_ROCK(levl[x][y].typ) \
|| (IS_DOOR(levl[x][y].typ) \
&& (levl[x][y].doormask & (D_CLOSED | D_LOCKED))))
/*
* Don't ever move the chain into solid rock. If we have to, then
* instead undo the move_bc() and jump to the drag ball code. Note
* that this also means the "cannot carry and drag" message will not
* appear, since unless we moved at least two squares there is no
* possibility of the chain position being in solid rock.
*/
#define SKIP_TO_DRAG \
do { \
*chainx = oldchainx; \
*chainy = oldchainy; \
move_bc(0, *bc_control, *ballx, *bally, *chainx, *chainy); \
goto drag; \
} while (0)
if (IS_CHAIN_ROCK(u.ux, u.uy) || IS_CHAIN_ROCK(*chainx, *chainy)
|| IS_CHAIN_ROCK(uball->ox, uball->oy))
already_in_rock = TRUE;
else
already_in_rock = FALSE;
switch (dist2(x, y, uball->ox, uball->oy)) {
/* two spaces diagonal from ball, move chain inbetween */
case 8:
*chainx = (uball->ox + x) / 2;
*chainy = (uball->oy + y) / 2;
if (IS_CHAIN_ROCK(*chainx, *chainy) && !already_in_rock)
SKIP_TO_DRAG;
break;
/* player is distance 2/1 from ball; move chain to one of the
* two spaces between
* @
* __
* 0
*/
case 5: {
xchar tempx, tempy, tempx2, tempy2;
/* find position closest to current position of chain;
no effect if current position is already OK */
if (abs(x - uball->ox) == 1) {
tempx = x;
tempx2 = uball->ox;
tempy = tempy2 = (uball->oy + y) / 2;
} else {
tempx = tempx2 = (uball->ox + x) / 2;
tempy = y;
tempy2 = uball->oy;
}
if (IS_CHAIN_ROCK(tempx, tempy) && !IS_CHAIN_ROCK(tempx2, tempy2)
&& !already_in_rock) {
if (allow_drag) {
/* Avoid pathological case *if* not teleporting:
* 0 0_
* _X move northeast -----> X@
* @
*/
if (dist2(u.ux, u.uy, uball->ox, uball->oy) == 5
&& dist2(x, y, tempx, tempy) == 1)
SKIP_TO_DRAG;
/* Avoid pathological case *if* not teleporting:
* 0 0
* _X move east -----> X_
* @ @
*/
if (dist2(u.ux, u.uy, uball->ox, uball->oy) == 4
&& dist2(x, y, tempx, tempy) == 2)
SKIP_TO_DRAG;
}
*chainx = tempx2;
*chainy = tempy2;
} else if (!IS_CHAIN_ROCK(tempx, tempy)
&& IS_CHAIN_ROCK(tempx2, tempy2) && !already_in_rock) {
if (allow_drag) {
if (dist2(u.ux, u.uy, uball->ox, uball->oy) == 5
&& dist2(x, y, tempx2, tempy2) == 1)
SKIP_TO_DRAG;
if (dist2(u.ux, u.uy, uball->ox, uball->oy) == 4
&& dist2(x, y, tempx2, tempy2) == 2)
SKIP_TO_DRAG;
}
*chainx = tempx;
*chainy = tempy;
} else if (IS_CHAIN_ROCK(tempx, tempy)
&& IS_CHAIN_ROCK(tempx2, tempy2) && !already_in_rock) {
SKIP_TO_DRAG;
} else if (dist2(tempx, tempy, uchain->ox, uchain->oy)
< dist2(tempx2, tempy2, uchain->ox, uchain->oy)
|| ((dist2(tempx, tempy, uchain->ox, uchain->oy)
== dist2(tempx2, tempy2, uchain->ox, uchain->oy))
&& rn2(2))) {
*chainx = tempx;
*chainy = tempy;
} else {
*chainx = tempx2;
*chainy = tempy2;
}
break;
}
/* ball is two spaces horizontal or vertical from player; move*/
/* chain inbetween *unless* current chain position is OK */
case 4:
if (CHAIN_IN_MIDDLE(uchain->ox, uchain->oy))
break;
*chainx = (x + uball->ox) / 2;
*chainy = (y + uball->oy) / 2;
if (IS_CHAIN_ROCK(*chainx, *chainy) && !already_in_rock)
SKIP_TO_DRAG;
break;
/* ball is one space diagonal from player. Check for the
* following special case:
* @
* _ moving southwest becomes @_
* 0 0
* (This will also catch teleporting that happens to resemble
* this case, but oh well.) Otherwise fall through.
*/
case 2:
if (dist2(x, y, uball->ox, uball->oy) == 2
&& dist2(x, y, uchain->ox, uchain->oy) == 4) {
if (uchain->oy == y)
*chainx = uball->ox;
else
*chainy = uball->oy;
if (IS_CHAIN_ROCK(*chainx, *chainy) && !already_in_rock)
SKIP_TO_DRAG;
break;
}
/* fall through */
case 1:
case 0:
/* do nothing if possible */
if (CHAIN_IN_MIDDLE(uchain->ox, uchain->oy))
break;
/* otherwise try to drag chain to player's old position */
if (CHAIN_IN_MIDDLE(u.ux, u.uy)) {
*chainx = u.ux;
*chainy = u.uy;
break;
}
/* otherwise use player's new position (they must have
teleported, for this to happen) */
*chainx = x;
*chainy = y;
break;
default:
impossible("bad chain movement");
break;
}
#undef SKIP_TO_DRAG
#undef CHAIN_IN_MIDDLE
return TRUE;
}
drag:
if (near_capacity() > SLT_ENCUMBER && dist2(x, y, u.ux, u.uy) <= 2) {
You("cannot %sdrag the heavy iron ball.",
invent ? "carry all that and also " : "");
nomul(0);
return FALSE;
}
if ((is_pool(uchain->ox, uchain->oy)
/* water not mere continuation of previous water */
&& (levl[uchain->ox][uchain->oy].typ == POOL
|| !is_pool(uball->ox, uball->oy)
|| levl[uball->ox][uball->oy].typ == POOL))
|| ((t = t_at(uchain->ox, uchain->oy))
&& (is_pit(t->ttyp) || is_hole(t->ttyp)))) {
if (Levitation) {
You_feel("a tug from the iron ball.");
if (t)
t->tseen = 1;
} else {
struct monst *victim;
You("are jerked back by the iron ball!");
if ((victim = m_at(uchain->ox, uchain->oy)) != 0) {
int tmp;
int dieroll = rnd(20);
tmp = -2 + Luck + find_mac(victim);
tmp += omon_adj(victim, uball, TRUE);
if (tmp >= dieroll)
(void) hmon(victim, uball, HMON_DRAGGED, dieroll);
else
miss(xname(uball), victim);
} /* now check again in case mon died */
if (!m_at(uchain->ox, uchain->oy)) {
u.ux = uchain->ox;
u.uy = uchain->oy;
newsym(u.ux0, u.uy0);
}
nomul(0);
*bc_control = BC_BALL;
move_bc(1, *bc_control, *ballx, *bally, *chainx, *chainy);
*ballx = uchain->ox;
*bally = uchain->oy;
move_bc(0, *bc_control, *ballx, *bally, *chainx, *chainy);
spoteffects(TRUE);
return FALSE;
}
}
*bc_control = BC_BALL | BC_CHAIN;
move_bc(1, *bc_control, *ballx, *bally, *chainx, *chainy);
if (dist2(x, y, u.ux, u.uy) > 2) {
/* Awful case: we're still in range of the ball, so we thought we
* could only move the chain, but it turned out that the target
* square for the chain was rock, so we had to drag it instead.
* But we can't drag it either, because we teleported and are more
* than one square from our old position. Revert to the teleport
* behavior.
*/
*ballx = *chainx = x;
*bally = *chainy = y;
} else {
xchar newchainx = u.ux, newchainy = u.uy;
/*
* Generally, chain moves to hero's previous location and ball
* moves to chain's previous location, except that we try to
* keep the chain directly between the hero and the ball. But,
* take the simple approach if the hero's previous location or
* the potential between location is inaccessible.
*/
if (dist2(x, y, uchain->ox, uchain->oy) == 4
&& !IS_CHAIN_ROCK(newchainx, newchainy)) {
newchainx = (x + uchain->ox) / 2;
newchainy = (y + uchain->oy) / 2;
if (IS_CHAIN_ROCK(newchainx, newchainy)) {
/* don't let chain move to inaccessible location */
newchainx = u.ux;
newchainy = u.uy;
}
}
*ballx = uchain->ox;
*bally = uchain->oy;
*chainx = newchainx;
*chainy = newchainy;
}
#undef IS_CHAIN_ROCK
*cause_delay = TRUE;
return TRUE;
}
/*
* drop_ball()
*
* The punished hero drops or throws her iron ball. If the hero is
* blind, we must reset the order and glyph. Check for side effects.
* This routine expects the ball to be already placed.
*
* Should not be called while swallowed.
*/
void
drop_ball(x, y)
xchar x, y;
{
if (Blind) {
/* get the order */
u.bc_order = bc_order();
/* pick up glyph */
u.bglyph = (u.bc_order) ? u.cglyph : levl[x][y].glyph;
}
if (x != u.ux || y != u.uy) {
static const char *pullmsg = "The ball pulls you out of the %s!";
struct trap *t;
long side;
if (u.utrap
&& u.utraptype != TT_INFLOOR && u.utraptype != TT_BURIEDBALL) {
switch (u.utraptype) {
case TT_PIT:
pline(pullmsg, "pit");
break;
case TT_WEB:
pline(pullmsg, "web");
pline_The("web is destroyed!");
deltrap(t_at(u.ux, u.uy));
break;
case TT_LAVA:
pline(pullmsg, hliquid("lava"));
break;
case TT_BEARTRAP:
side = rn2(3) ? LEFT_SIDE : RIGHT_SIDE;
pline(pullmsg, "bear trap");
set_wounded_legs(side, rn1(1000, 500));
if (!u.usteed) {
Your("%s %s is severely damaged.",
(side == LEFT_SIDE) ? "left" : "right",
body_part(LEG));
losehp(Maybe_Half_Phys(2),
"leg damage from being pulled out of a bear trap",
KILLED_BY);
}
break;
}
reset_utrap(TRUE);
fill_pit(u.ux, u.uy);
}
u.ux0 = u.ux;
u.uy0 = u.uy;
if (!Levitation && !MON_AT(x, y) && !u.utrap
&& (is_pool(x, y)
|| ((t = t_at(x, y))
&& (is_pit(t->ttyp)
|| is_hole(t->ttyp))))) {
u.ux = x;
u.uy = y;
} else {
u.ux = x - u.dx;
u.uy = y - u.dy;
}
vision_full_recalc = 1; /* hero has moved, recalculate vision later */
if (Blind) {
/* drop glyph under the chain */
if (u.bc_felt & BC_CHAIN)
levl[uchain->ox][uchain->oy].glyph = u.cglyph;
u.bc_felt = 0; /* feel nothing */
/* pick up new glyph */
u.cglyph = (u.bc_order) ? u.bglyph : levl[u.ux][u.uy].glyph;
}
movobj(uchain, u.ux, u.uy); /* has a newsym */
if (Blind) {
u.bc_order = bc_order();
}
newsym(u.ux0, u.uy0); /* clean up old position */
if (u.ux0 != u.ux || u.uy0 != u.uy) {
spoteffects(TRUE);
sokoban_guilt();
}
}
}
/* ball&chain cause hero to randomly lose stuff from inventory */
STATIC_OVL void
litter()
{
struct obj *otmp, *nextobj = 0;
int capacity = weight_cap();
for (otmp = invent; otmp; otmp = nextobj) {
nextobj = otmp->nobj;
if ((otmp != uball) && (rnd(capacity) <= (int) otmp->owt)) {
if (canletgo(otmp, "")) {
You("drop %s and %s %s down the stairs with you.",
yname(otmp), (otmp->quan == 1L) ? "it" : "they",
otense(otmp, "fall"));
dropx(otmp);
encumber_msg(); /* drop[xyz]() probably ought to to this... */
}
}
}
}
void
drag_down()
{
boolean forward;
uchar dragchance = 3;