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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
|
/* $OpenBSD: shots.c,v 1.14 2017/01/21 08:22:57 krw Exp $ */
/* $NetBSD: shots.c,v 1.3 1997/10/11 08:13:50 lukem Exp $ */
/*
* Copyright (c) 1983-2003, Regents of the University of California.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* + Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* + Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* + Neither the name of the University of California, San Francisco nor
* the names of its contributors may be used to endorse or promote
* products derived from this software without specific prior written
* permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <sys/select.h>
#include <stdlib.h>
#include <syslog.h>
#include "conf.h"
#include "hunt.h"
#include "server.h"
#define PLUS_DELTA(x, max) if (x < max) x++; else x--
#define MINUS_DELTA(x, min) if (x > min) x--; else x++
static void chkshot(BULLET *, BULLET *);
static void chkslime(BULLET *, BULLET *);
static void explshot(BULLET *, int, int);
static void find_under(BULLET *, BULLET *);
static int iswall(int, int);
static void mark_boot(BULLET *);
static void mark_player(BULLET *);
static int move_drone(BULLET *);
static void move_flyer(PLAYER *);
static int move_normal_shot(BULLET *);
static void move_slime(BULLET *, int, BULLET *);
static void save_bullet(BULLET *);
static void zapshot(BULLET *, BULLET *);
/* Return true if there is pending activity */
int
can_moveshots(void)
{
PLAYER *pp;
/* Bullets are moving? */
if (Bullets)
return 1;
/* Explosions are happening? */
if (can_rollexpl())
return 1;
/* Things are flying? */
for (pp = Boot; pp < &Boot[NBOOTS]; pp++)
if (pp->p_flying >= 0)
return 1;
for (pp = Player; pp < End_player; pp++)
if (pp->p_flying >= 0)
return 1;
/* Everything is quiet: */
return 0;
}
/*
* moveshots:
* Move the shots already in the air, taking explosions into account
*/
void
moveshots(void)
{
BULLET *bp, *next;
PLAYER *pp;
int x, y;
BULLET *blist;
rollexpl();
if (Bullets == NULL)
goto no_bullets;
/*
* First we move through the bullet list conf_bulspd times, looking
* for things we may have run into. If we do run into
* something, we set up the explosion and disappear, checking
* for damage to any player who got in the way.
*/
/* Move the list to a working list */
blist = Bullets;
Bullets = NULL;
/* Work with bullets on the working list (blist) */
for (bp = blist; bp != NULL; bp = next) {
next = bp->b_next;
x = bp->b_x;
y = bp->b_y;
/* Un-draw the bullet on all screens: */
Maze[y][x] = bp->b_over;
check(ALL_PLAYERS, y, x);
/* Decide how to move the bullet: */
switch (bp->b_type) {
/* Normal, atomic bullets: */
case SHOT:
case GRENADE:
case SATCHEL:
case BOMB:
if (move_normal_shot(bp)) {
/* Still there: put back on the active list */
bp->b_next = Bullets;
Bullets = bp;
}
break;
/* Slime bullets that explode into slime on impact: */
case SLIME:
if (bp->b_expl || move_normal_shot(bp)) {
/* Still there: put back on the active list */
bp->b_next = Bullets;
Bullets = bp;
}
break;
/* Drones that wander about: */
case DSHOT:
if (move_drone(bp)) {
/* Still there: put back on the active list */
bp->b_next = Bullets;
Bullets = bp;
}
break;
/* Other/unknown: */
default:
/* Place it back on the active list: */
bp->b_next = Bullets;
Bullets = bp;
break;
}
}
/* Again, hang the Bullets list off `blist' and work with that: */
blist = Bullets;
Bullets = NULL;
for (bp = blist; bp != NULL; bp = next) {
next = bp->b_next;
/* Is the bullet exploding? */
if (!bp->b_expl) {
/*
* Its still flying through the air.
* Put it back on the bullet list.
*/
save_bullet(bp);
/* All the monitors can see the bullet: */
for (pp = Monitor; pp < End_monitor; pp++)
check(pp, bp->b_y, bp->b_x);
/* All the scanning players can see the drone: */
if (bp->b_type == DSHOT)
for (pp = Player; pp < End_player; pp++)
if (pp->p_scan >= 0)
check(pp, bp->b_y, bp->b_x);
} else {
/* It is exploding. Check what we hit: */
chkshot(bp, next);
/* Release storage for the destroyed bullet: */
free(bp);
}
}
/* Re-draw all the players: (in case a bullet wiped them out) */
for (pp = Player; pp < End_player; pp++)
Maze[pp->p_y][pp->p_x] = pp->p_face;
no_bullets:
/* Move flying boots through the air: */
for (pp = Boot; pp < &Boot[NBOOTS]; pp++)
if (pp->p_flying >= 0)
move_flyer(pp);
/* Move flying players through the air: */
for (pp = Player; pp < End_player; pp++) {
if (pp->p_flying >= 0)
move_flyer(pp);
/* Flush out the explosions: */
sendcom(pp, REFRESH);
look(pp);
}
/* Flush out and synchronise all the displays: */
sendcom(ALL_PLAYERS, REFRESH);
}
/*
* move_normal_shot:
* Move a normal shot along its trajectory.
* Returns false if the bullet no longer needs tracking.
*/
static int
move_normal_shot(BULLET *bp)
{
int i, x, y;
PLAYER *pp;
/*
* Walk an unexploded bullet along conf_bulspd times, moving it
* one unit along each step. We flag it as exploding if it
* meets something.
*/
for (i = 0; i < conf_bulspd; i++) {
/* Stop if the bullet has already exploded: */
if (bp->b_expl)
break;
/* Adjust the bullet's co-ordinates: */
x = bp->b_x;
y = bp->b_y;
switch (bp->b_face) {
case LEFTS:
x--;
break;
case RIGHT:
x++;
break;
case ABOVE:
y--;
break;
case BELOW:
y++;
break;
}
/* Look at what the bullet is colliding with : */
switch (Maze[y][x]) {
/* Gun shots have a chance of collision: */
case SHOT:
if (rand_num(100) < conf_pshot_coll) {
zapshot(Bullets, bp);
zapshot(bp->b_next, bp);
}
break;
/* Grenades only have a chance of collision: */
case GRENADE:
if (rand_num(100) < conf_pgren_coll) {
zapshot(Bullets, bp);
zapshot(bp->b_next, bp);
}
break;
/* Reflecting walls richochet the bullet: */
case WALL4:
switch (bp->b_face) {
case LEFTS:
bp->b_face = BELOW;
break;
case RIGHT:
bp->b_face = ABOVE;
break;
case ABOVE:
bp->b_face = RIGHT;
break;
case BELOW:
bp->b_face = LEFTS;
break;
}
Maze[y][x] = WALL5;
for (pp = Monitor; pp < End_monitor; pp++)
check(pp, y, x);
break;
case WALL5:
switch (bp->b_face) {
case LEFTS:
bp->b_face = ABOVE;
break;
case RIGHT:
bp->b_face = BELOW;
break;
case ABOVE:
bp->b_face = LEFTS;
break;
case BELOW:
bp->b_face = RIGHT;
break;
}
Maze[y][x] = WALL4;
for (pp = Monitor; pp < End_monitor; pp++)
check(pp, y, x);
break;
/* Dispersion doors randomly disperse bullets: */
case DOOR:
switch (rand_num(4)) {
case 0:
bp->b_face = ABOVE;
break;
case 1:
bp->b_face = BELOW;
break;
case 2:
bp->b_face = LEFTS;
break;
case 3:
bp->b_face = RIGHT;
break;
}
break;
/* Bullets zing past fliers: */
case FLYER:
pp = play_at(y, x);
message(pp, "Zing!");
break;
/* Bullets encountering a player: */
case LEFTS:
case RIGHT:
case BELOW:
case ABOVE:
/*
* Give the person a chance to catch a
* grenade if s/he is facing it:
*/
pp = play_at(y, x);
pp->p_ident->i_shot += bp->b_charge;
if (opposite(bp->b_face, Maze[y][x])) {
/* Give them a 10% chance: */
if (rand_num(100) < conf_pgren_catch) {
/* They caught it! */
if (bp->b_owner != NULL)
message(bp->b_owner,
"Your charge was absorbed!");
/*
* The target player stole from the bullet's
* owner. Charge stolen statistics:
*/
if (bp->b_score != NULL)
bp->b_score->i_robbed += bp->b_charge;
/* They acquire more ammo: */
pp->p_ammo += bp->b_charge;
/* Check if it would have destroyed them: */
if (pp->p_damage + bp->b_size * conf_mindam
> pp->p_damcap)
/* Lucky escape statistics: */
pp->p_ident->i_saved++;
/* Tell them: */
message(pp, "Absorbed charge (good shield!)");
/* Absorbtion statistics: */
pp->p_ident->i_absorbed += bp->b_charge;
/* Deallocate storage: */
free(bp);
/* Update ammo display: */
ammo_update(pp);
/* No need for caller to keep tracking it: */
return FALSE;
}
/* Bullets faced head-on (statistics): */
pp->p_ident->i_faced += bp->b_charge;
}
/*
* Small chance that the bullet just misses the
* person. If so, the bullet just goes on its
* merry way without exploding. (5% chance)
*/
if (rand_num(100) < conf_pmiss) {
/* Ducked statistics: */
pp->p_ident->i_ducked += bp->b_charge;
/* Check if it would have killed them: */
if (pp->p_damage + bp->b_size * conf_mindam
> pp->p_damcap)
/* Lucky escape statistics: */
pp->p_ident->i_saved++;
/* Shooter missed statistics: */
if (bp->b_score != NULL)
bp->b_score->i_missed += bp->b_charge;
/* Tell target that they were missed: */
message(pp, "Zing!");
/* Tell the bullet owner they missed: */
if (bp->b_owner != NULL)
message(bp->b_owner,
((bp->b_score->i_missed & 0x7) == 0x7) ?
"My! What a bad shot you are!" :
"Missed him");
/* Don't fall through */
break;
} else {
/* The player is to be blown up: */
bp->b_expl = TRUE;
}
break;
/* Bullet hits a wall, and always explodes: */
case WALL1:
case WALL2:
case WALL3:
bp->b_expl = TRUE;
break;
}
/* Update the bullet's new position: */
bp->b_x = x;
bp->b_y = y;
}
/* Caller should keep tracking the bullet: */
return TRUE;
}
/*
* move_drone:
* Move the drone to the next square
* Returns FALSE if the drone need no longer be tracked.
*/
static int
move_drone(BULLET *bp)
{
int mask, count;
int n, dir = -1;
PLAYER *pp;
/* See if we can give someone a blast: */
if (is_player(Maze[bp->b_y][bp->b_x - 1])) {
dir = WEST;
goto drone_move;
}
if (is_player(Maze[bp->b_y - 1][bp->b_x])) {
dir = NORTH;
goto drone_move;
}
if (is_player(Maze[bp->b_y + 1][bp->b_x])) {
dir = SOUTH;
goto drone_move;
}
if (is_player(Maze[bp->b_y][bp->b_x + 1])) {
dir = EAST;
goto drone_move;
}
/* Find out what directions are clear and move that way: */
mask = count = 0;
if (!iswall(bp->b_y, bp->b_x - 1))
mask |= WEST, count++;
if (!iswall(bp->b_y - 1, bp->b_x))
mask |= NORTH, count++;
if (!iswall(bp->b_y + 1, bp->b_x))
mask |= SOUTH, count++;
if (!iswall(bp->b_y, bp->b_x + 1))
mask |= EAST, count++;
/* All blocked up, just wait: */
if (count == 0)
return TRUE;
/* Only one way to go: */
if (count == 1) {
dir = mask;
goto drone_move;
}
/* Avoid backtracking, and remove the direction we came from: */
switch (bp->b_face) {
case LEFTS:
if (mask & EAST)
mask &= ~EAST, count--;
break;
case RIGHT:
if (mask & WEST)
mask &= ~WEST, count--;
break;
case ABOVE:
if (mask & SOUTH)
mask &= ~SOUTH, count--;
break;
case BELOW:
if (mask & NORTH)
mask &= ~NORTH, count--;
break;
}
/* Pick one of the remaining directions: */
n = rand_num(count);
if (n >= 0 && mask & NORTH)
dir = NORTH, n--;
if (n >= 0 && mask & SOUTH)
dir = SOUTH, n--;
if (n >= 0 && mask & EAST)
dir = EAST, n--;
if (n >= 0 && mask & WEST)
dir = WEST, n--;
drone_move:
/* Move the drone: */
switch (dir) {
case -1:
/* no move */
case WEST:
bp->b_x--;
bp->b_face = LEFTS;
break;
case EAST:
bp->b_x++;
bp->b_face = RIGHT;
break;
case NORTH:
bp->b_y--;
bp->b_face = ABOVE;
break;
case SOUTH:
bp->b_y++;
bp->b_face = BELOW;
break;
}
/* Look at what the drone moved onto: */
switch (Maze[bp->b_y][bp->b_x]) {
case LEFTS:
case RIGHT:
case BELOW:
case ABOVE:
/*
* Players have a 1% chance of absorbing a drone,
* if they are facing it.
*/
if (rand_num(100) < conf_pdroneabsorb && opposite(bp->b_face,
Maze[bp->b_y][bp->b_x])) {
/* Feel the power: */
pp = play_at(bp->b_y, bp->b_x);
pp->p_ammo += bp->b_charge;
message(pp, "**** Absorbed drone ****");
/* Release drone storage: */
free(bp);
/* Update ammo: */
ammo_update(pp);
/* No need for caller to keep tracking drone: */
return FALSE;
}
/* Detonate the drone: */
bp->b_expl = TRUE;
break;
}
/* Keep tracking the drone. */
return TRUE;
}
/*
* save_bullet:
* Put a bullet back onto the bullet list
*/
static void
save_bullet(BULLET *bp)
{
/* Save what the bullet will be flying over: */
bp->b_over = Maze[bp->b_y][bp->b_x];
switch (bp->b_over) {
/* Bullets that can pass through each other: */
case SHOT:
case GRENADE:
case SATCHEL:
case BOMB:
case SLIME:
case LAVA:
case DSHOT:
find_under(Bullets, bp);
break;
}
switch (bp->b_over) {
/* A bullet hits a player: */
case LEFTS:
case RIGHT:
case ABOVE:
case BELOW:
case FLYER:
mark_player(bp);
break;
/* A bullet passes a boot: */
case BOOT:
case BOOT_PAIR:
mark_boot(bp);
/* FALLTHROUGH */
/* The bullet flies over everything else: */
default:
Maze[bp->b_y][bp->b_x] = bp->b_type;
break;
}
/* Insert the bullet into the Bullets list: */
bp->b_next = Bullets;
Bullets = bp;
}
/*
* move_flyer:
* Update the position of a player in flight
*/
static void
move_flyer(PLAYER *pp)
{
int x, y;
if (pp->p_undershot) {
fixshots(pp->p_y, pp->p_x, pp->p_over);
pp->p_undershot = FALSE;
}
/* Restore what the flier was flying over */
Maze[pp->p_y][pp->p_x] = pp->p_over;
/* Fly: */
x = pp->p_x + pp->p_flyx;
y = pp->p_y + pp->p_flyy;
/* Bouncing off the edges of the maze: */
if (x < 1) {
x = 1 - x;
pp->p_flyx = -pp->p_flyx;
}
else if (x > WIDTH - 2) {
x = (WIDTH - 2) - (x - (WIDTH - 2));
pp->p_flyx = -pp->p_flyx;
}
if (y < 1) {
y = 1 - y;
pp->p_flyy = -pp->p_flyy;
}
else if (y > HEIGHT - 2) {
y = (HEIGHT - 2) - (y - (HEIGHT - 2));
pp->p_flyy = -pp->p_flyy;
}
/* Make sure we don't land on something we can't: */
again:
switch (Maze[y][x]) {
default:
/*
* Flier is over something other than space, a wall
* or a door. Randomly move (drift) the flier a little bit
* and then try again:
*/
switch (rand_num(4)) {
case 0:
PLUS_DELTA(x, WIDTH - 2);
break;
case 1:
MINUS_DELTA(x, 1);
break;
case 2:
PLUS_DELTA(y, HEIGHT - 2);
break;
case 3:
MINUS_DELTA(y, 1);
break;
}
goto again;
/* Give a little boost when about to land on a wall or door: */
case WALL1:
case WALL2:
case WALL3:
case WALL4:
case WALL5:
case DOOR:
if (pp->p_flying == 0)
pp->p_flying++;
break;
/* Spaces are okay: */
case SPACE:
break;
}
/* Update flier's coordinates: */
pp->p_y = y;
pp->p_x = x;
/* Consume 'flying' time: */
if (pp->p_flying-- == 0) {
/* Land: */
if (pp->p_face != BOOT && pp->p_face != BOOT_PAIR) {
/* Land a player - they stustain a fall: */
checkdam(pp, (PLAYER *) NULL, (IDENT *) NULL,
rand_num(pp->p_damage / conf_fall_frac), FALL);
pp->p_face = rand_dir();
showstat(pp);
} else {
/* Land boots: */
if (Maze[y][x] == BOOT)
pp->p_face = BOOT_PAIR;
Maze[y][x] = SPACE;
}
}
/* Save under the flier: */
pp->p_over = Maze[y][x];
/* Draw in the flier: */
Maze[y][x] = pp->p_face;
showexpl(y, x, pp->p_face);
}
/*
* chkshot
* Handle explosions
*/
static void
chkshot(BULLET *bp, BULLET *next)
{
int y, x;
int dy, dx, absdy;
int delta, damage;
char expl;
PLAYER *pp;
delta = 0;
switch (bp->b_type) {
case SHOT:
case MINE:
case GRENADE:
case GMINE:
case SATCHEL:
case BOMB:
delta = bp->b_size - 1;
break;
case SLIME:
case LAVA:
chkslime(bp, next);
return;
case DSHOT:
bp->b_type = SLIME;
chkslime(bp, next);
return;
}
/* Draw the explosion square: */
for (y = bp->b_y - delta; y <= bp->b_y + delta; y++) {
if (y < 0 || y >= HEIGHT)
continue;
dy = y - bp->b_y;
absdy = (dy < 0) ? -dy : dy;
for (x = bp->b_x - delta; x <= bp->b_x + delta; x++) {
/* Draw a part of the explosion cloud: */
if (x < 0 || x >= WIDTH)
continue;
dx = x - bp->b_x;
if (dx == 0)
expl = (dy == 0) ? '*' : '|';
else if (dy == 0)
expl = '-';
else if (dx == dy)
expl = '\\';
else if (dx == -dy)
expl = '/';
else
expl = '*';
showexpl(y, x, expl);
/* Check what poor bastard was in the explosion: */
switch (Maze[y][x]) {
case LEFTS:
case RIGHT:
case ABOVE:
case BELOW:
case FLYER:
if (dx < 0)
dx = -dx;
if (absdy > dx)
damage = bp->b_size - absdy;
else
damage = bp->b_size - dx;
/* Everybody hurts, sometimes. */
pp = play_at(y, x);
checkdam(pp, bp->b_owner, bp->b_score,
damage * conf_mindam, bp->b_type);
break;
case GMINE:
case MINE:
/* Mines detonate in a chain reaction: */
add_shot((Maze[y][x] == GMINE) ?
GRENADE : SHOT,
y, x, LEFTS,
(Maze[y][x] == GMINE) ?
GRENREQ : BULREQ,
(PLAYER *) NULL, TRUE, SPACE);
Maze[y][x] = SPACE;
break;
}
}
}
}
/*
* chkslime:
* handle slime shot exploding
*/
static void
chkslime(BULLET *bp, BULLET *next)
{
BULLET *nbp;
switch (Maze[bp->b_y][bp->b_x]) {
/* Slime explodes on walls and doors: */
case WALL1:
case WALL2:
case WALL3:
case WALL4:
case WALL5:
case DOOR:
switch (bp->b_face) {
case LEFTS:
bp->b_x++;
break;
case RIGHT:
bp->b_x--;
break;
case ABOVE:
bp->b_y++;
break;
case BELOW:
bp->b_y--;
break;
}
break;
}
/* Duplicate the unit of slime: */
nbp = malloc(sizeof (BULLET));
if (nbp == NULL) {
logit(LOG_ERR, "malloc");
return;
}
*nbp = *bp;
/* Move it around: */
move_slime(nbp, nbp->b_type == SLIME ? conf_slimespeed :
conf_lavaspeed, next);
}
/*
* move_slime:
* move the given slime shot speed times and add it back if
* it hasn't fizzled yet
*/
static void
move_slime(BULLET *bp, int speed, BULLET *next)
{
int i, j, dirmask, count;
PLAYER *pp;
BULLET *nbp;
if (speed == 0) {
if (bp->b_charge <= 0)
free(bp);
else
save_bullet(bp);
return;
}
/* Draw it: */
showexpl(bp->b_y, bp->b_x, bp->b_type == LAVA ? LAVA : '*');
switch (Maze[bp->b_y][bp->b_x]) {
/* Someone got hit by slime or lava: */
case LEFTS:
case RIGHT:
case ABOVE:
case BELOW:
case FLYER:
pp = play_at(bp->b_y, bp->b_x);
message(pp, "You've been slimed.");
checkdam(pp, bp->b_owner, bp->b_score, conf_mindam, bp->b_type);
break;
/* Bullets detonate in slime and lava: */
case SHOT:
case GRENADE:
case SATCHEL:
case BOMB:
case DSHOT:
explshot(next, bp->b_y, bp->b_x);
explshot(Bullets, bp->b_y, bp->b_x);
break;
}
/* Drain the slime/lava of some energy: */
if (--bp->b_charge <= 0) {
/* It fizzled: */
free(bp);
return;
}
/* Figure out which way the slime should flow: */
dirmask = 0;
count = 0;
switch (bp->b_face) {
case LEFTS:
if (!iswall(bp->b_y, bp->b_x - 1))
dirmask |= WEST, count++;
if (!iswall(bp->b_y - 1, bp->b_x))
dirmask |= NORTH, count++;
if (!iswall(bp->b_y + 1, bp->b_x))
dirmask |= SOUTH, count++;
if (dirmask == 0)
if (!iswall(bp->b_y, bp->b_x + 1))
dirmask |= EAST, count++;
break;
case RIGHT:
if (!iswall(bp->b_y, bp->b_x + 1))
dirmask |= EAST, count++;
if (!iswall(bp->b_y - 1, bp->b_x))
dirmask |= NORTH, count++;
if (!iswall(bp->b_y + 1, bp->b_x))
dirmask |= SOUTH, count++;
if (dirmask == 0)
if (!iswall(bp->b_y, bp->b_x - 1))
dirmask |= WEST, count++;
break;
case ABOVE:
if (!iswall(bp->b_y - 1, bp->b_x))
dirmask |= NORTH, count++;
if (!iswall(bp->b_y, bp->b_x - 1))
dirmask |= WEST, count++;
if (!iswall(bp->b_y, bp->b_x + 1))
dirmask |= EAST, count++;
if (dirmask == 0)
if (!iswall(bp->b_y + 1, bp->b_x))
dirmask |= SOUTH, count++;
break;
case BELOW:
if (!iswall(bp->b_y + 1, bp->b_x))
dirmask |= SOUTH, count++;
if (!iswall(bp->b_y, bp->b_x - 1))
dirmask |= WEST, count++;
if (!iswall(bp->b_y, bp->b_x + 1))
dirmask |= EAST, count++;
if (dirmask == 0)
if (!iswall(bp->b_y - 1, bp->b_x))
dirmask |= NORTH, count++;
break;
}
if (count == 0) {
/*
* No place to go. Just sit here for a while and wait
* for adjacent squares to clear out.
*/
save_bullet(bp);
return;
}
if (bp->b_charge < count) {
/* Only bp->b_charge paths may be taken */
while (count > bp->b_charge) {
if (dirmask & WEST)
dirmask &= ~WEST;
else if (dirmask & EAST)
dirmask &= ~EAST;
else if (dirmask & NORTH)
dirmask &= ~NORTH;
else if (dirmask & SOUTH)
dirmask &= ~SOUTH;
count--;
}
}
/* Spawn little slimes off in every possible direction: */
i = bp->b_charge / count;
j = bp->b_charge % count;
if (dirmask & WEST) {
count--;
nbp = create_shot(bp->b_type, bp->b_y, bp->b_x - 1, LEFTS,
i, bp->b_size, bp->b_owner, bp->b_score, TRUE, SPACE);
move_slime(nbp, speed - 1, next);
}
if (dirmask & EAST) {
count--;
nbp = create_shot(bp->b_type, bp->b_y, bp->b_x + 1, RIGHT,
(count < j) ? i + 1 : i, bp->b_size, bp->b_owner,
bp->b_score, TRUE, SPACE);
move_slime(nbp, speed - 1, next);
}
if (dirmask & NORTH) {
count--;
nbp = create_shot(bp->b_type, bp->b_y - 1, bp->b_x, ABOVE,
(count < j) ? i + 1 : i, bp->b_size, bp->b_owner,
bp->b_score, TRUE, SPACE);
move_slime(nbp, speed - 1, next);
}
if (dirmask & SOUTH) {
count--;
nbp = create_shot(bp->b_type, bp->b_y + 1, bp->b_x, BELOW,
(count < j) ? i + 1 : i, bp->b_size, bp->b_owner,
bp->b_score, TRUE, SPACE);
move_slime(nbp, speed - 1, next);
}
free(bp);
}
/*
* iswall:
* returns whether the given location is a wall
*/
static int
iswall(int y, int x)
{
if (y < 0 || x < 0 || y >= HEIGHT || x >= WIDTH)
return TRUE;
switch (Maze[y][x]) {
case WALL1:
case WALL2:
case WALL3:
case WALL4:
case WALL5:
case DOOR:
case SLIME:
case LAVA:
return TRUE;
}
return FALSE;
}
/*
* zapshot:
* Take a shot out of the air.
*/
static void
zapshot(BULLET *blist, BULLET *obp)
{
BULLET *bp;
for (bp = blist; bp != NULL; bp = bp->b_next) {
/* Find co-located bullets not facing the same way: */
if (bp->b_face != obp->b_face
&& bp->b_x == obp->b_x && bp->b_y == obp->b_y)
{
/* Bullet collision: */
explshot(blist, obp->b_y, obp->b_x);
return;
}
}
}
/*
* explshot -
* Make all shots at this location blow up
*/
static void
explshot(BULLET *blist, int y, int x)
{
BULLET *bp;
for (bp = blist; bp != NULL; bp = bp->b_next)
if (bp->b_x == x && bp->b_y == y) {
bp->b_expl = TRUE;
if (bp->b_owner != NULL)
message(bp->b_owner, "Shot intercepted.");
}
}
/*
* play_at:
* Return a pointer to the player at the given location
*/
PLAYER *
play_at(int y, int x)
{
PLAYER *pp;
for (pp = Player; pp < End_player; pp++)
if (pp->p_x == x && pp->p_y == y)
return pp;
/* Internal fault: */
logx(LOG_ERR, "play_at: not a player");
abort();
}
/*
* opposite:
* Return TRUE if the bullet direction faces the opposite direction
* of the player in the maze
*/
int
opposite(int face, char dir)
{
switch (face) {
case LEFTS:
return (dir == RIGHT);
case RIGHT:
return (dir == LEFTS);
case ABOVE:
return (dir == BELOW);
case BELOW:
return (dir == ABOVE);
default:
return FALSE;
}
}
/*
* is_bullet:
* Is there a bullet at the given coordinates? If so, return
* a pointer to the bullet, otherwise return NULL
*/
BULLET *
is_bullet(int y, int x)
{
BULLET *bp;
for (bp = Bullets; bp != NULL; bp = bp->b_next)
if (bp->b_y == y && bp->b_x == x)
return bp;
return NULL;
}
/*
* fixshots:
* change the underlying character of the shots at a location
* to the given character.
*/
void
fixshots(int y, int x, char over)
{
BULLET *bp;
for (bp = Bullets; bp != NULL; bp = bp->b_next)
if (bp->b_y == y && bp->b_x == x)
bp->b_over = over;
}
/*
* find_under:
* find the underlying character for a bullet when it lands
* on another bullet.
*/
static void
find_under(BULLET *blist, BULLET *bp)
{
BULLET *nbp;
for (nbp = blist; nbp != NULL; nbp = nbp->b_next)
if (bp->b_y == nbp->b_y && bp->b_x == nbp->b_x) {
bp->b_over = nbp->b_over;
break;
}
}
/*
* mark_player:
* mark a player as under a shot
*/
static void
mark_player(BULLET *bp)
{
PLAYER *pp;
for (pp = Player; pp < End_player; pp++)
if (pp->p_y == bp->b_y && pp->p_x == bp->b_x) {
pp->p_undershot = TRUE;
break;
}
}
/*
* mark_boot:
* mark a boot as under a shot
*/
static void
mark_boot(BULLET *bp)
{
PLAYER *pp;
for (pp = Boot; pp < &Boot[NBOOTS]; pp++)
if (pp->p_y == bp->b_y && pp->p_x == bp->b_x) {
pp->p_undershot = TRUE;
break;
}
}
|