forked from pngwen/xboing
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ball.c
2042 lines (1645 loc) · 54 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
/*
* XBoing - An X11 blockout style computer game
*
* (c) Copyright 1993, 1994, 1995, Justin C. Kibell, All Rights Reserved
*
* The X Consortium, and any party obtaining a copy of these files from
* the X Consortium, directly or indirectly, is granted, free of charge, a
* full and unrestricted irrevocable, world-wide, paid up, royalty-free,
* nonexclusive right and license to deal in this software and
* documentation files (the "Software"), including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons who receive
* copies from any such party to do so. This license includes without
* limitation a license to do the foregoing actions under any patents of
* the party supplying this software to the X Consortium.
*
* In no event shall the author be liable to any party for direct, indirect,
* special, incidental, or consequential damages arising out of the use of
* this software and its documentation, even if the author has been advised
* of the possibility of such damage.
*
* The author specifically disclaims any warranties, including, but not limited
* to, the implied warranties of merchantability and fitness for a particular
* purpose. The software provided hereunder is on an "AS IS" basis, and the
* author has no obligation to provide maintenance, support, updates,
* enhancements, or modifications.
*/
/*
* =========================================================================
*
* $Id: ball.c,v 1.1.1.1 1994/12/16 01:36:42 jck Exp $
* $Source: /usr5/legends/jck/xb/master/xboing/ball.c,v $
* $Revision: 1.1.1.1 $
* $Date: 1994/12/16 01:36:42 $
*
* $Log: ball.c,v $
* Revision 1.1.1.1 1994/12/16 01:36:42 jck
* The XBoing distribution requires configuration management. This is why the
* cvs utility is being used. This is the initial import of all source etc..
*
*
* =========================================================================
*/
/*
* Include file dependencies:
*/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <values.h>
/*
// TODO: Remove instances of X11-associated data types
#include <xpm.h>
#include <X11/Xos.h>
//TODO: Replace references to bitmaps with png textures
#include "bitmaps/balls/ball1.png"
#include "bitmaps/balls/ball2.png"
#include "bitmaps/balls/ball3.png"
#include "bitmaps/balls/ball4.png"
#include "bitmaps/balls/killer.png"
#include "bitmaps/guides/guide1.png"
#include "bitmaps/guides/guide2.png"
#include "bitmaps/guides/guide3.png"
#include "bitmaps/guides/guide4.png"
#include "bitmaps/guides/guide5.png"
#include "bitmaps/guides/guide6.png"
#include "bitmaps/guides/guide7.png"
#include "bitmaps/guides/guide8.png"
#include "bitmaps/guides/guide9.png"
#include "bitmaps/guides/guide10.png"
#include "bitmaps/guides/guide11.png"
#include "bitmaps/balls/bbirth1.png"
#include "bitmaps/balls/bbirth2.png"
#include "bitmaps/balls/bbirth3.png"
#include "bitmaps/balls/bbirth4.png"
#include "bitmaps/balls/bbirth5.png"
#include "bitmaps/balls/bbirth6.png"
#include "bitmaps/balls/bbirth7.png"
#include "bitmaps/balls/bbirth8.png"
*/
#include "include/audio.h"
#include "include/error.h"
#include "include/score.h"
#include "include/sfx.h"
#include "include/eyedude.h"
#include "include/init.h"
#include "include/main.h"
#include "include/stage.h"
#include "include/blocks.h"
#include "include/paddle.h"
#include "include/misc.h"
#include "include/level.h"
#include "include/mess.h"
#include "include/special.h"
#include "include/ball.h"
#include "include/faketypes.h"
/*
* Internal macro definitions:
*/
#define X2COL(col, x) (col = x / colWidth)
#define Y2ROW(row, y) (row = y / rowHeight)
#define COL2X(x, col) (x = col * colWidth)
#define ROW2Y(y, row) (y = row * rowHeight)
/* MIN returns the smallest at a and b */
#ifndef MIN
#define MIN(a,b) ((a)<(b) ? (a):(b))
#endif
/* MAX returns the largest of a and b */
#ifndef MAX
#define MAX(a,b) ((a)>(b) ? (a):(b))
#endif
/* SQR returns the square of x */
#ifndef SQR
#define SQR(x) ((x)*(x))
#endif
#ifndef MINFLOAT
#define MINFLOAT ((float)1.40129846432481707e-45)
#endif
/*
* Internal type declarations:
*/
static void MoveBall(Display *display, Window window, int x, int y, int replace,
int i);
static void MoveBallBirth(Display *display, Window window, int x, int y,
int slide, int replace, int i);
static void TeleportBall(Display *display, Window window, int i);
static int BallHitPaddle(Display *display, Window window, int *hit, int i,
int *x, int *y);
static void UpdateABall(Display *display, Window window, int i);
static int CheckRegions(Display *display, Window window, int row, int col,
int x, int y, int i);
static int CheckForCollision(Display *display, Window window, int x, int y,
int *r, int *c, int i);
static void updateBallVariables(int i);
static void SetBallWait(enum BallStates newMode, int waitFrame, int i);
static void DoBallWait(int i);
static void EraseTheBall(Display *display, Window window, int x, int y);
static void ChangeBallDirectionToGuide(int i);
static void Ball2BallCollision(BALL *ball1, BALL *ball2);
static int WhenBallsCollide(BALL *ball1, BALL *ball2, float *time);
typedef struct
{
float x, y;
} vector_t;
/*
* Internal variable declarations:
*/
static Pixmap ballsPixmap[BALL_SLIDES];
static Pixmap ballsMask[BALL_SLIDES];
static Pixmap ballBirthPixmap[BIRTH_SLIDES];
static Pixmap ballBirthMask[BIRTH_SLIDES];
static Pixmap guides[11];
static Pixmap guidesM[11];
BALL balls[MAX_BALLS];
static int guidePos = 6; /* Start in middle of guider */
/* global constant machine epsilon */
float MACHINE_EPS;
void InitialiseBall(Display *display, Window window, Colormap colormap)
{
/*
* Read and create all the animation frames for the balls and guides.
*/
XpmAttributes attributes;
int XpmErrorStatus;
attributes.valuemask = XpmColormap;
attributes.colormap = colormap;
/* Create the xpm pixmap ball frames */
XpmErrorStatus = XpmCreatePixmapFromData(display, window, ball1_xpm,
&ballsPixmap[0], &ballsMask[0], &attributes);
HandleXPMError(display, XpmErrorStatus, "InitialiseBall(ball1)");
XpmErrorStatus = XpmCreatePixmapFromData(display, window, ball2_xpm,
&ballsPixmap[1], &ballsMask[1], &attributes);
HandleXPMError(display, XpmErrorStatus, "InitialiseBall(ball2)");
XpmErrorStatus = XpmCreatePixmapFromData(display, window, ball3_xpm,
&ballsPixmap[2], &ballsMask[2], &attributes);
HandleXPMError(display, XpmErrorStatus, "InitialiseBall(ball3)");
XpmErrorStatus = XpmCreatePixmapFromData(display, window, ball4_xpm,
&ballsPixmap[3], &ballsMask[3], &attributes);
HandleXPMError(display, XpmErrorStatus, "InitialiseBall(ball4)");
XpmErrorStatus = XpmCreatePixmapFromData(display, window, killer_xpm,
&ballsPixmap[4], &ballsMask[4], &attributes);
HandleXPMError(display, XpmErrorStatus, "InitialiseBall(killer)");
/* Ball birth sequence */
/* Create the xpm pixmap ball birth frames */
XpmErrorStatus = XpmCreatePixmapFromData(display, window, ballbirth1_xpm,
&ballBirthPixmap[0], &ballBirthMask[0], &attributes);
HandleXPMError(display, XpmErrorStatus, "InitialiseBall(ballbirth1)");
XpmErrorStatus = XpmCreatePixmapFromData(display, window, ballbirth2_xpm,
&ballBirthPixmap[1], &ballBirthMask[1], &attributes);
HandleXPMError(display, XpmErrorStatus, "InitialiseBall(ballbirth2)");
XpmErrorStatus = XpmCreatePixmapFromData(display, window, ballbirth3_xpm,
&ballBirthPixmap[2], &ballBirthMask[2], &attributes);
HandleXPMError(display, XpmErrorStatus, "InitialiseBall(ballbirth3)");
XpmErrorStatus = XpmCreatePixmapFromData(display, window, ballbirth4_xpm,
&ballBirthPixmap[3], &ballBirthMask[3], &attributes);
HandleXPMError(display, XpmErrorStatus, "InitialiseBall(ballbirth4)");
XpmErrorStatus = XpmCreatePixmapFromData(display, window, ballbirth5_xpm,
&ballBirthPixmap[4], &ballBirthMask[4], &attributes);
HandleXPMError(display, XpmErrorStatus, "InitialiseBall(ballbirth5)");
XpmErrorStatus = XpmCreatePixmapFromData(display, window, ballbirth6_xpm,
&ballBirthPixmap[5], &ballBirthMask[5], &attributes);
HandleXPMError(display, XpmErrorStatus, "InitialiseBall(ballbirth6)");
XpmErrorStatus = XpmCreatePixmapFromData(display, window, ballbirth7_xpm,
&ballBirthPixmap[6], &ballBirthMask[6], &attributes);
HandleXPMError(display, XpmErrorStatus, "InitialiseBall(ballbirth7)");
XpmErrorStatus = XpmCreatePixmapFromData(display, window, ballbirth8_xpm,
&ballBirthPixmap[7], &ballBirthMask[7], &attributes);
HandleXPMError(display, XpmErrorStatus, "InitialiseBall(ballbirth8)");
/* Now load in the guide pixmaps */
XpmErrorStatus = XpmCreatePixmapFromData(display, window, guide1_xpm,
&guides[0], &guidesM[0], &attributes);
HandleXPMError(display, XpmErrorStatus, "InitialiseBall(guide1)");
XpmErrorStatus = XpmCreatePixmapFromData(display, window, guide2_xpm,
&guides[1], &guidesM[1], &attributes);
HandleXPMError(display, XpmErrorStatus, "InitialiseBall(guide2)");
XpmErrorStatus = XpmCreatePixmapFromData(display, window, guide3_xpm,
&guides[2], &guidesM[2], &attributes);
HandleXPMError(display, XpmErrorStatus, "InitialiseBall(guide3)");
XpmErrorStatus = XpmCreatePixmapFromData(display, window, guide4_xpm,
&guides[3], &guidesM[3], &attributes);
HandleXPMError(display, XpmErrorStatus, "InitialiseBall(guide4)");
XpmErrorStatus = XpmCreatePixmapFromData(display, window, guide5_xpm,
&guides[4], &guidesM[4], &attributes);
HandleXPMError(display, XpmErrorStatus, "InitialiseBall(guide5)");
XpmErrorStatus = XpmCreatePixmapFromData(display, window, guide6_xpm,
&guides[5], &guidesM[5], &attributes);
HandleXPMError(display, XpmErrorStatus, "InitialiseBall(guide6)");
XpmErrorStatus = XpmCreatePixmapFromData(display, window, guide7_xpm,
&guides[6], &guidesM[6], &attributes);
HandleXPMError(display, XpmErrorStatus, "InitialiseBall(guide7)");
XpmErrorStatus = XpmCreatePixmapFromData(display, window, guide8_xpm,
&guides[7], &guidesM[7], &attributes);
HandleXPMError(display, XpmErrorStatus, "InitialiseBall(guide8)");
XpmErrorStatus = XpmCreatePixmapFromData(display, window, guide9_xpm,
&guides[8], &guidesM[8], &attributes);
HandleXPMError(display, XpmErrorStatus, "InitialiseBall(guide9)");
XpmErrorStatus = XpmCreatePixmapFromData(display, window, guide10_xpm,
&guides[9], &guidesM[9], &attributes);
HandleXPMError(display, XpmErrorStatus, "InitialiseBall(guide10)");
XpmErrorStatus = XpmCreatePixmapFromData(display, window, guide11_xpm,
&guides[10], &guidesM[10], &attributes);
HandleXPMError(display, XpmErrorStatus, "InitialiseBall(guide11)");
/* Free the xpm pixmap attributes */
XpmFreeAttributes(&attributes);
MACHINE_EPS = sqrt(MINFLOAT);
/* Make sure that all the balls are initialised */
ClearAllBalls();
}
void FreeBall(Display *display)
{
/*
* Free all the animation frames for the balls and guides etc.
*/
int i;
/* Free all animation frames for the ball */
for (i = 0; i < BALL_SLIDES; i++)
{
if (ballsPixmap[i]) XFreePixmap(display, ballsPixmap[i]);
if (ballsMask[i]) XFreePixmap(display, ballsMask[i]);
}
/* Free all animation frames for the guides */
for (i = 0; i < 11; i++)
{
if (guides[i]) XFreePixmap(display, guides[i]);
if (guidesM[i]) XFreePixmap(display, guidesM[i]);
}
/* Free all animation frames for the ball birth */
for (i = 0; i < BIRTH_SLIDES; i++)
{
/* Free the ball birth animation pixmaps */
if (ballBirthPixmap[i]) XFreePixmap(display, ballBirthPixmap[i]);
if (ballBirthMask[i]) XFreePixmap(display, ballBirthMask[i]);
}
}
void RedrawBall(Display *display, Window window)
{
/* not hard - STILL TO BE IMPLEMENTED */
}
static void EraseTheBall(Display *display, Window window, int x, int y)
{
/*
* Clear the ball area! The x, y coordinates are the centre of ball
*/
XClearArea(display, window, x - BALL_WC, y - BALL_HC,
BALL_WIDTH, BALL_HEIGHT, False);
}
void DrawTheBall(Display *display, Window window, int x, int y, int slide)
{
/*
* Draw the ball using the slide variable as the index into the frames
* of the ball animation. The x,y are the centre of the ball.
*/
RenderShape(display, window, ballsPixmap[slide], ballsMask[slide],
x - BALL_WC, y - BALL_HC, BALL_WIDTH, BALL_HEIGHT, False);
}
void DrawTheBallBirth(Display *display, Window window, int x, int y, int slide)
{
/*
* Draw the ball using the slide variable as the index into the frames
* of the ball animation. The x,y are the centre of the ball birth anim.
*/
RenderShape(display, window, ballBirthPixmap[slide], ballBirthMask[slide],
x - BALL_WC, y - BALL_HC, BALL_WIDTH, BALL_HEIGHT, False);
}
static void MoveBallBirth(Display *display, Window window, int x, int y,
int slide, int replace, int i)
{
/*
* Remove any debris under ball first by clearing it
*/
if (replace)
{
XClearArea(display, window,
balls[i].oldx - BALL_WC, balls[i].oldy - BALL_HC,
BALL_WIDTH, BALL_HEIGHT, False);
}
balls[i].oldx = x;
balls[i].oldy = y;
/* If slide is -1 then clear the ball area */
if (slide == -1)
XClearArea(display, window,
x - BALL_WC, y - BALL_HC, BALL_WIDTH, BALL_HEIGHT, False);
else
DrawTheBallBirth(display, window, x, y, slide);
}
static void MoveBall(Display *display, Window window, int x, int y, int replace,
int i)
{
/*
* Move the ball from one position to the next and also update the
* balls old positions. Will only move if the frame is correct for
* this move, ie: framrate. Ball animates as well - hard to see though.
*/
if (replace)
EraseTheBall(display, window, balls[i].oldx, balls[i].oldy);
/* Update the old position of this ball */
balls[i].oldx = x;
balls[i].oldy = y;
if (Killer == True)
{
/* Render the killer ball now ie: red ball */
DrawTheBall(display, window, x, y, BALL_SLIDES-1);
}
else
{
/* Render the ball now */
DrawTheBall(display, window, x, y, balls[i].slide);
}
/* Change slide for ball every n frames of animation */
if ((frame % BALL_ANIM_RATE) == 0)
balls[i].slide++;
/* wrap around slides */
if (balls[i].slide == BALL_SLIDES-1) balls[i].slide = 0;
}
static void MoveGuides(Display *display, Window window, int i, int remove)
{
/*
* The guide hangs out above a READY ball when waiting to be sent into
* action. It has a rotating yellow dot that indicates which direction
* the ball will be heading off in. This code animates and also clears
* the guide.
*/
static int oldgx = 0;
static int oldgy = 0;
static int inc = 1;
/* Clear the old slide */
XClearArea(display, window, oldgx - 14, oldgy - 6, 29, 12, False);
if (remove == False)
{
/* Update its old positions */
oldgx = balls[i].oldx;
oldgy = balls[i].oldy - 16;
/* Check for any silly errors */
if (guidePos < 0 || guidePos > 10)
ErrorMessage("Guidepos out of range.");
/* draw the guide pixmap */
RenderShape(display, window, guides[guidePos], guidesM[guidePos],
oldgx - 14, oldgy - 6, 29, 12, False);
/* Don't draw it ever frame */
if ((frame % (BALL_FRAME_RATE*8)) == 0)
guidePos += inc;
/* wrap around slides */
if (guidePos == 10) inc = -1;
if (guidePos == 0) inc = 1;
}
else
guidePos = 6;
}
void RandomiseBallVelocity(int i)
{
balls[i].dx = balls[i].dy = 0;
/* Loop until values are random */
while (balls[i].dx == 0 || balls[i].dy == 0)
{
/* Randomise the ball btwn [3, MAX_VEL] */
balls[i].dx = (rand() % (MAX_X_VEL - 3)) + 3;
balls[i].dy = (rand() % (MAX_Y_VEL - 3)) + 3;
/* Make it possible for negative numbers */
if ((rand() % 10) < 5)
balls[i].dx *= -1;
if ((rand() % 10) < 5)
balls[i].dy *= -1;
balls[i].lastPaddleHitFrame = frame + PADDLE_BALL_FRAME_TILT;
}
}
void DoBoardTilt(Display *display, int i)
{
/*
* In the event of a ball loop bounce then this function will fiddle
* with the velocity and it may just jump out of the loop.
*/
/* Only worry about active balls */
if (balls[i].ballState == BALL_ACTIVE)
{
DEBUG("Auto Tilt activated.");
/* Tilt the board to remove any endless loops */
SetCurrentMessage(display, messWindow,
"Auto Tilt Activated", True);
RandomiseBallVelocity(i);
}
}
static void TeleportBall(Display *display, Window window, int i)
{
/*
* This function will teleport the ball to some other space not occupied
* and start off there.
*/
int r1, c1, s1, r2, c2, s2, r3, c3, s3, r4, c4, s4;
int r, c, x, y;
struct aBlock *blockP, *bP;
int done = False;
int count = 0;
/* Loop until we find a block to move to */
while (done == False && count <= 20)
{
/* Stop this going on forever */
count++;
/* Give me a new random block position */
r = (rand() % (MAX_ROW - 6)) + 1;
c = (rand() % MAX_COL) + 1;
if (r < 0 || r >= MAX_ROW) continue;
if (c < 0 || c >= MAX_COL) continue;
/* Pointer to the correct block we need - speed things up */
blockP = &blocks[r][c];
/* Check that the block is not occupied and not exploding */
if ((blockP->occupied == False) && (blockP->exploding == False))
{
/* Check that the block is not a closed in position */
r1 = r; c1 = c - 1; s1 = 0;
if (r1 < 0 || r1 >= MAX_ROW) s1 = 1;
if (c1 < 0 || c1 >= MAX_COL) s1 = 1;
if (s1 == 0)
{
bP = &blocks[r1][c1];
if (bP->blockType == BLACK_BLK)
s1 = 1;
}
r2 = r - 1; c2 = c; s2 = 0;
if (r2 < 0 || r2 >= MAX_ROW) s2 = 1;
if (c2 < 0 || c2 >= MAX_COL) s2 = 1;
if (s2 == 0)
{
bP = &blocks[r2][c2];
if (bP->blockType == BLACK_BLK)
s2 = 1;
}
r3 = r; c3 = c + 1; s3 = 0;
if (r3 < 0 || r3 >= MAX_ROW) s3 = 1;
if (c3 < 0 || c3 >= MAX_COL) s3 = 1;
if (s3 == 0)
{
bP = &blocks[r3][c3];
if (bP->blockType == BLACK_BLK)
s3 = 1;
}
r4 = r + 1; c4 = c; s4 = 0;
if (r4 < 0 || r4 >= MAX_ROW) s4 = 1;
if (c4 < 0 || c4 >= MAX_COL) s4 = 1;
if (s4 == 0)
{
bP = &blocks[r4][c4];
if (bP->blockType == BLACK_BLK)
s4 = 1;
}
/* If it isn't ok to go here then keep searching */
if ((s1 == 1) && (s2 == 1) && (s3 == 1) && (s4 == 1))
{
DEBUG("Trying new spot for teleport.");
continue;
}
/* Calculate the new ball coordinates */
COL2X(x, c);
ROW2Y(y, r);
balls[i].ballx = x;
balls[i].bally = y;
/* Move the ball to the new position */
MoveBall(display, window, x, y, True, i);
balls[i].lastPaddleHitFrame = frame + PADDLE_BALL_FRAME_TILT;
/* Ok jump out now thanks. */
done = True;
DEBUG("Ball was Teleported.");
return;
}
else
{
DEBUG("Looping in teleport function.");
}
}
/* Ok - give up add erase ball and start it from paddle!! */
EraseTheBall(display, window, balls[i].oldx, balls[i].oldy);
updateBallVariables(i);
DEBUG("Ball was NOT Teleported.");
}
void SplitBallInTwo(Display *display, Window window)
{
/*
* If a multiball block was hit then start another ball from somewhere.
* Start it somewhere random and also randomise the velocity.
*/
int j;
j = AddANewBall(display, 0, 0, 3, 3);
if (j > 0)
{
/* Make this new ball move straight away */
ChangeBallMode(BALL_ACTIVE, j);
TeleportBall(display, window, j);
RandomiseBallVelocity(j);
SetCurrentMessage(display, messWindow, "Another ball!", True);
}
else
SetCurrentMessage(display, messWindow,
"Cannot add ball!", True);
}
void ClearBallNow(Display *display, Window window, int i)
{
/*
* Terminate and clear this ball.
*/
EraseTheBall(display, window, balls[i].oldx, balls[i].oldy);
ClearBall(i);
DeadBall(display, window);
DEBUG("Clear ball now called.");
}
void KillBallNow(Display *display, Window window, int i)
{
/*
* Set the ball to die by popping.
*/
ChangeBallMode(BALL_POP, i);
DEBUG("Ball mode now BALL_POP.");
}
void GetBallPosition(int *ballX, int *ballY, int i)
{
/*
* Get the position of ball i.
*/
*ballX = balls[i].ballx;
*ballY = balls[i].bally;
}
static int BallHitPaddle(Display *display, Window window, int *hit, int i,
int *x, int *y)
{
/*
* Handle the ball hitting the paddle if it is. The bounce it back
* at an angle that is described below.
*/
float x1, x2, y1, y2, alpha, beta, xP1, xP2, xH, yH;
int paddleLine;
/***********************************************************************
A1 (x1,y1)
*
.
.
P1 =========.=========== P2 <---- paddle (x, y pos is known )
(xP1,yP1) . H (xH, yH) (xP2,yP2)
.
.
.
*
A2 (x2,y2)
Given the line A1A2, is the intersecting point H (xH, yH) in the paddle
segment ? (i.e xH in [xP1,xP2])
A1A2 is : y = alpha * x + beta
A1 and A2 are in A1A2 than beta = [(y1 + y2) - alpha*(x1+x2)] / 2
yH = yP1 = yP2
so xH = (yP1 - beta) / alpha
**********************************************************************/
paddleLine = (PLAY_HEIGHT - DIST_BASE - 2);
if (balls[i].bally + BALL_HC > paddleLine)
{
xP1 = (float)(paddlePos - (GetPaddleSize() / 2) - BALL_WC);
xP2 = (float)(paddlePos + (GetPaddleSize() / 2) + BALL_WC);
if (balls[i].dx == 0)
{
/* process the vertical moving balls */
if (((float)balls[i].ballx > xP1) && ((float)balls[i].ballx < xP2))
{
/* the ball hit the paddle */
*hit = balls[i].ballx - paddlePos;
*x = balls[i].ballx;
*y = paddleLine - BALL_HC;
return True;
}
else
return False;
}
else
{
/* compute the line coefficients of the ball */
alpha = (float) balls[i].dy;
x1 = (float) (balls[i].ballx - balls[i].dx);
y1 = (float) (balls[i].bally - balls[i].dy);
x2 = (float) (balls[i].ballx);
y2 = (float) (balls[i].bally);
beta = ((y1 + y2) - alpha * (x1 + x2)) / 2.0;
yH = (float) paddleLine;
xH = (yH - beta) / alpha;
if ((xH > xP1) && (xH < xP2))
{
/* the ball hit the paddle */
*hit = (int) (xH + 0.5) - paddlePos;
*x = (int) (xH + 0.5);
*y = paddleLine - BALL_HC;
return True;
}
else
return False;
}
}
/* We didn't hit the paddle */
return False;
}
static int HandleTheBlocks(Display *display, Window window, int row, int col,
int i)
{
/*
* When a ball hits a block it calls this routine and this routine
* will perform the function relevant to that block. eg: teleport
* will teleport the ball. "i" is the ball number.
*
* Will return TRUE if the ball is not to bounce off the block.
*/
struct aBlock *blockP;
/* Pointer to the block the ball is in */
blockP = &blocks[row][col];
/* There has been a collision so handle it */
if (blockP->exploding == False)
{
switch (blockP->blockType)
{
case COUNTER_BLK:
balls[i].lastPaddleHitFrame = frame + PADDLE_BALL_FRAME_TILT;
/* If in killer mode then don't bounce off block */
if (Killer == True || blockP->explodeAll == True)
{
/* Ok kill the block outright */
DrawBlock(display, window, row, col, KILL_BLK);
return True;
}
/* Special case for counter - reduce count on block */
if (blockP->counterSlide == 0)
DrawBlock(display, window, row, col, KILL_BLK);
else
{
/* Draw the counter block minus one number */
blockP->counterSlide--;
DrawBlock(display, window, row, col, COUNTER_BLK);
}
break;
case MGUN_BLK:
/* Turn the machine gun on */
ToggleFastGun(display, True);
DrawSpecials(display);
SetCurrentMessage(display, messWindow, "Machine Gun", True);
/* Not a wall so explode the block */
DrawBlock(display, window, row, col, KILL_BLK);
balls[i].lastPaddleHitFrame = frame + PADDLE_BALL_FRAME_TILT;
/* If in killer mode then don't bounce off block */
if (Killer == True) return True;
break;
case DEATH_BLK:
/* Ha ha - hit death block so die */
/* Kill the ball now */
ClearBallNow(display, window, i);
/* Not a wall so explode the block */
DrawBlock(display, window, row, col, KILL_BLK);
balls[i].lastPaddleHitFrame = frame + PADDLE_BALL_FRAME_TILT;
/* If in killer mode then don't bounce off block */
if (Killer == True) return True;
break;
case HYPERSPACE_BLK:
/* Teleport to somewhere else */
TeleportBall(display, window, i);
RandomiseBallVelocity(i);
/* Redraw it just in case */
DrawBlock(display, window, row, col, HYPERSPACE_BLK);
balls[i].lastPaddleHitFrame = frame + PADDLE_BALL_FRAME_TILT;
PlaySoundForBlock(HYPERSPACE_BLK);
return True;
break;
case WALLOFF_BLK:
/* Walls are now turned off */
ToggleWallsOn(display, True);
DrawSpecials(display);
SetCurrentMessage(display, messWindow,
"Walls off", True);
/* Not a wall so explode the block */
DrawBlock(display, window, row, col, KILL_BLK);
balls[i].lastPaddleHitFrame = frame + PADDLE_BALL_FRAME_TILT;
/* If in killer mode then don't bounce off block */
if (Killer == True) return True;
break;
case REVERSE_BLK:
/* Paddle control now reverse */
ToggleReverse(display);
SetCurrentMessage(display, messWindow,
"Paddle control reversed", True);
DrawSpecials(display);
/* Move the paddle to reflect reversed paddle */
handlePaddleMoving(display);
/* Not a wall so explode the block */
DrawBlock(display, window, row, col, KILL_BLK);
balls[i].lastPaddleHitFrame = frame + PADDLE_BALL_FRAME_TILT;
/* If in killer mode then don't bounce off block */
if (Killer == True) return True;
break;
case PAD_SHRINK_BLK:
/* Paddle shrinking block */
ChangePaddleSize(display, window, PAD_SHRINK_BLK);
SetCurrentMessage(display, messWindow,
"Shrink Paddle", True);
/* Not a wall so explode the block */
DrawBlock(display, window, row, col, KILL_BLK);
balls[i].lastPaddleHitFrame = frame + PADDLE_BALL_FRAME_TILT;
/* If in killer mode then don't bounce off block */
if (Killer == True) return True;
break;
case PAD_EXPAND_BLK:
/* Paddle expanding block */
ChangePaddleSize(display, window, PAD_EXPAND_BLK);
SetCurrentMessage(display, messWindow,
"Expand Paddle", True);
/* Not a wall so explode the block */
DrawBlock(display, window, row, col, KILL_BLK);
balls[i].lastPaddleHitFrame = frame + PADDLE_BALL_FRAME_TILT;
/* If in killer mode then don't bounce off block */
if (Killer == True) return True;
break;
case EXTRABALL_BLK:
/* Extra ball */
AddExtraLife(display);
SetCurrentMessage(display, messWindow,
"Extra ball", True);
/* Not a wall so explode the block */
DrawBlock(display, window, row, col, KILL_BLK);
balls[i].lastPaddleHitFrame = frame + PADDLE_BALL_FRAME_TILT;
/* If in killer mode then don't bounce off block */
if (Killer == True) return True;
break;
case STICKY_BLK:
ToggleStickyBat(display, True);
DrawSpecials(display);
SetCurrentMessage(display, messWindow,
"Sticky Bat", True);
/* Not a wall so explode the block */
DrawBlock(display, window, row, col, KILL_BLK);
balls[i].lastPaddleHitFrame = frame + PADDLE_BALL_FRAME_TILT;
/* If in killer mode then don't bounce off block */
if (Killer == True) return True;
break;
case MULTIBALL_BLK:
SplitBallInTwo(display, window);
/* Not a wall so explode the block */
DrawBlock(display, window, row, col, KILL_BLK);
balls[i].lastPaddleHitFrame = frame + PADDLE_BALL_FRAME_TILT;
/* If in killer mode then don't bounce off block */
if (Killer == True) return True;
break;
case BLACK_BLK:
if (frame <= blockP->nextFrame)
{
/* Set the block up to be blown away */
DrawBlock(display, window, row, col, KILL_BLK);
balls[i].lastPaddleHitFrame = frame +
PADDLE_BALL_FRAME_TILT;
/* If in killer mode then don't bounce off block */
if (Killer == True) return True;
}
else
{
/* Redraw the solid wall block to make sure */
DrawBlock(display, window, row, col, BLACKHIT_BLK);
blockP->nextFrame = frame + 30;
}
break;
default:
/* Not a wall so explode the block */
DrawBlock(display, window, row, col, KILL_BLK);
balls[i].lastPaddleHitFrame = frame + PADDLE_BALL_FRAME_TILT;
/* If in killer mode then don't bounce off block */