forked from pngwen/xboing
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgun.c
657 lines (544 loc) · 16.7 KB
/
gun.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
/*
* 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: gun.c,v 1.1.1.1 1994/12/16 01:36:44 jck Exp $
* $Source: /usr5/legends/jck/xb/master/xboing/gun.c,v $
* $Revision: 1.1.1.1 $
* $Date: 1994/12/16 01:36:44 $
*
* $Log: gun.c,v $
* Revision 1.1.1.1 1994/12/16 01:36:44 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 <xpm.h>
#include <stdio.h>
#include <math.h>
#include <assert.h>
//#include <X11/Xos.h>
//#include "bitmaps/guns/bullet.xpm"
//#include "bitmaps/guns/tink.xpm"
#include "include/faketypes.h"
#include "include/error.h"
#include "include/audio.h"
#include "include/score.h"
#include "include/init.h"
#include "include/mess.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/ball.h"
#include "include/special.h"
#include "include/eyedude.h"
#include "include/gun.h"
/*
* Internal macro definitions:
*/
#define BULLET_DY -7
#define BULLET_WIDTH 7
#define BULLET_HEIGHT 16
#define BULLET_START_Y (PLAY_HEIGHT - 40)
#define BULLET_WC (BULLET_WIDTH / 2)
#define BULLET_HC (BULLET_HEIGHT / 2)
#define TINK_WIDTH 10
#define TINK_HEIGHT 5
#define TINK_WC (TINK_WIDTH / 2)
#define TINK_HC (TINK_HEIGHT / 2)
#define X2COL(col, x) (col = x / colWidth)
#define Y2ROW(row, y) (row = y / rowHeight)
#define BULLET_FRAME_RATE 3
/* Should be the same */
#define MAX_MOVING_BULLETS 40
#define MAX_TINKS 40
#define TINK_DELAY 100
/*
* Internal type declarations:
*/
static int ResetBulletStart(Display *display, Window window);
static void CheckTinks(Display *display, Window window);
static void AddTink(Display *display, Window window, int xpos);
static void ClearTinks(void);
static void UpdateBullet(Display *display, Window window);
static int StartABullet(Display *display, Window window, int xpos);
static void ClearBullet(int i);
static void DrawTheTink(Display *display, Window window, int x, int y);
static void EraseTheTink(Display *display, Window window, int x, int y);
static void DrawBullet(Display *display, Window window, int i);
static int CheckForBulletCollision(Display *display, Window window,
int x, int y);
static int CheckBallBulletCollision(Display *display, Window window,
int bx, int by, int j);
static int CheckEyeDudeBulletCollision(Display *display, Window window,
int bx, int by);
static struct
{
int xpos; /* x position of tink centre */
int clearFrame; /* Last frame to clear it */
} tinks[MAX_TINKS];
static struct
{
int xpos; /* x position of bullet */
int ypos; /* y position of bullet */
int oldypos; /* previous y position */
int dy; /* Change in y positoon */
} bullets[MAX_MOVING_BULLETS];
/*
* Internal variable declarations:
*/
static Pixmap bulletPixmap, bulletMask;
static Pixmap tinkPixmap, tinkMask;
static int numBullets, unlimitedBullets;
void InitialiseBullet(Display *display, Window window, Colormap colormap)
{
XpmAttributes attributes;
int XpmErrorStatus;
attributes.valuemask = XpmColormap;
attributes.colormap = colormap;
/* Create the xpm pixmap bullet */
XpmErrorStatus = XpmCreatePixmapFromData(display, window, bullet_xpm,
&bulletPixmap, &bulletMask, &attributes);
HandleXPMError(display, XpmErrorStatus, "InitialiseBullet(bullet)");
/* Create the xpm pixmap tink for bullet */
XpmErrorStatus = XpmCreatePixmapFromData(display, window, tink_xpm,
&tinkPixmap, &tinkMask, &attributes);
HandleXPMError(display, XpmErrorStatus, "InitialiseBullet(tink)");
/* Free the xpm pixmap attributes */
XpmFreeAttributes(&attributes);
SetNumberBullets(4);
ClearTinks();
ClearBullets();
}
static void CheckTinks(Display *display, Window window)
{
int i;
/* Clear and tinks that need to be cleared */
for (i = 0; i < MAX_TINKS; i++)
{
/* Is this tink active */
if (tinks[i].xpos != -1)
{
/* Time to clear tink? */
if (frame >= tinks[i].clearFrame)
{
/* Clear the tink! */
EraseTheTink(display, window, tinks[i].xpos, 2);
/* Free the tink up for another */
tinks[i].xpos = -1;
tinks[i].clearFrame = 0;
}
}
}
}
static void AddTink(Display *display, Window window, int xpos)
{
int i;
/* Cycle through tinks and try to add one */
for (i = 0; i < MAX_TINKS; i++)
{
/* Is this tink free? */
if (tinks[i].xpos == -1)
{
/* Set the tink array position */
tinks[i].xpos = xpos;
tinks[i].clearFrame = frame + TINK_DELAY;
/* Draw the new found tink! */
DrawTheTink(display, window, xpos, 2);
if (noSound == False) playSoundFile("shoot", 80);
return;
}
}
/* Full tink array - lots of shooting? */
WarningMessage("Cannot draw tink - tink array full.");
}
static void ClearTinks(void)
{
int i;
/* Initialise tinks array to empty */
for (i = 0; i < MAX_TINKS; i++)
{
tinks[i].xpos = -1;
tinks[i].clearFrame = 0;
}
}
static void UpdateBullet(Display *display, Window window)
{
int i, j;
int row, col;
int ballX, ballY;
struct aBlock *blockP;
/* Obtain the position of the ball */
GetBallPosition(&ballX, &ballY, 0);
/* Draw all bullets that need updating */
for (i = 0; i < MAX_MOVING_BULLETS; i++)
{
/* Is this bullet active */
if (bullets[i].xpos != -1)
{
/* Update bullet position using dy value */
bullets[i].ypos = bullets[i].oldypos + bullets[i].dy;
/* Has the bullet gone off the top edge */
if (bullets[i].ypos < -BULLET_HC)
{
/* Clear the bullet from the screen */
EraseTheBullet(display, window,
bullets[i].xpos, bullets[i].oldypos);
/* Draw a tink on the top edge */
AddTink(display, window, bullets[i].xpos);
/* Free the bullet up for another */
ClearBullet(i);
continue;
}
for (j = 0; j < MAX_BALLS; j++)
{
if (balls[j].active == True)
{
/* Has the bullet killed the ball */
if (CheckBallBulletCollision(display, window,
bullets[i].xpos, bullets[i].ypos, j))
{
/* Clear the bullet from the screen */
EraseTheBullet(display, window,
bullets[i].xpos, bullets[i].oldypos);
ClearBullet(i);
/* Kill the ball off */
ClearBallNow(display, window, j);
/* Play the lovel ahhh pop sound for ball shot */
if (noSound == False) playSoundFile("ballshot", 50);
break;
}
}
}
/* Convert the new bullet pos to rows and cols for collision */
X2COL(col, bullets[i].xpos);
Y2ROW(row, bullets[i].ypos);
/* Pointer to the correct block we need - speed things up */
blockP = &blocks[row][col];
if (getEyeDudeMode() == EYEDUDE_WALK)
{
/* See if the bullet has hit the active eyedude */
if (CheckEyeDudeBulletCollision(display, window,
bullets[i].xpos, bullets[i].ypos) == True)
{
/* Clear the bullet from the screen */
EraseTheBullet(display, window,
bullets[i].xpos, bullets[i].oldypos);
/* Ok so the eyedude has been hit - arrggh */
ChangeEyeDudeMode(EYEDUDE_DIE);
/* Free the bullet up for another */
ClearBullet(i);
}
}
/* Check if the bullet has hit a brick or something */
if (CheckForBulletCollision(display, window,
bullets[i].xpos, bullets[i].ypos) == True)
{
/* Clear the bullet from the screen */
EraseTheBullet(display, window,
bullets[i].xpos, bullets[i].oldypos);
/* Switch on the type of block hit */
switch (blockP->blockType)
{
case COUNTER_BLK:
if (blockP->counterSlide == 0)
{
/* Counter has counted down to 0 so kill off */
DrawBlock(display, window, row, col, KILL_BLK);
}
else
{
/* Decrement counter block and draw new one */
blockP->counterSlide--;
DrawBlock(display, window, row, col,
COUNTER_BLK);
}
break;
case HYPERSPACE_BLK:
/* Do nothing - redraw the block */
DrawBlock(display, window, row, col, HYPERSPACE_BLK);
break;
case BLACK_BLK:
/* Do nothing - redraw the block */
DrawBlock(display, window, row, col, BLACK_BLK);
break;
case REVERSE_BLK:
case MGUN_BLK:
case STICKY_BLK:
case WALLOFF_BLK:
case MULTIBALL_BLK:
case PAD_EXPAND_BLK:
case PAD_SHRINK_BLK:
case DEATH_BLK:
/* Shoot the block times to kill it */
blockP->counterSlide--;
if (blockP->counterSlide == 0)
{
/* Ok then a hit, explode that block */
DrawBlock(display, window, row, col, KILL_BLK);
}
break;
default:
/* Ok then a hit, explode that block */
DrawBlock(display, window, row, col, KILL_BLK);
break;
}
/* Free the bullet up for another */
ClearBullet(i);
}
else
DrawBullet(display, window, i);
/* Keep track of old position */
bullets[i].oldypos = bullets[i].ypos;
} /* Bullet active? */
} /* For loop */
}
static int StartABullet(Display *display, Window window, int xpos)
{
int i;
/* Cycle through bullets and try to add one */
for (i = 0; i < MAX_MOVING_BULLETS; i++)
{
/* Is this bullet free? */
if (bullets[i].xpos == -1)
{
/* Set the bullet array position */
bullets[i].xpos = xpos;
/* Get out of here */
return True;
}
/* Break out as the machine gun is not active */
if (fastGun == False) return False;
}
/* Full moving bullet array - lots of shooting? */
WarningMessage("Cannot draw bullet - bullet array full.");
return False;
}
static void ClearBullet(int i)
{
/* Setup the bullet entry */
bullets[i].xpos = -1;
bullets[i].ypos = BULLET_START_Y;
bullets[i].oldypos = BULLET_START_Y;
bullets[i].dy = BULLET_DY;
}
void ClearBullets(void)
{
int i;
/* Initialise bullets array to empty */
for (i = 0; i < MAX_MOVING_BULLETS; i++)
ClearBullet(i);
}
void FreeBullet(Display *display)
{
if (bulletPixmap) XFreePixmap(display, bulletPixmap);
if (bulletMask) XFreePixmap(display, bulletMask);
if (tinkPixmap) XFreePixmap(display, tinkPixmap);
if (tinkMask) XFreePixmap(display, tinkMask);
}
void SetNumberBullets(int num)
{
/* Set the number of bullets available */
numBullets = num;
}
void IncNumberBullets(void)
{
/* Increment the number of bullets */
numBullets++;
/* But don't give to many */
if (numBullets > MAX_BULLETS)
numBullets = MAX_BULLETS;
}
void SetUnlimitedBullets(int state)
{
/* Set the unlimit bullets state */
unlimitedBullets = state;
}
void DecNumberBullets(void)
{
/* Only decrement number of bullets if the unlimited ammo is off */
if (unlimitedBullets == False)
{
/* Decrement the number of bullets */
numBullets--;
/* But not to far */
if (numBullets < 0)
numBullets = 0;
}
}
int GetNumberBullets(void)
{
assert(numBullets >= 0);
/* How many bullets do I have */
return numBullets;
}
void shootBullet(Display *display, Window window)
{
/* Only shoot if no bullet is active and bullets and ball active */
if ((GetNumberBullets() > 0) && (IsBallWaiting() == False))
{
/* Reset the bullet to the starting possy to go forward */
if (ResetBulletStart(display, window) == True)
{
/* Remove a bullet from the ammunition */
DeleteABullet(display);
/* Play a shooting sound */
if (noSound == False) playSoundFile("shotgun", 50);
}
}
else if (GetNumberBullets() == 0)
{
/* Play an trigger clicking sound */
if (noSound == False) playSoundFile("click", 99);
}
}
void EraseTheBullet(Display *display, Window window, int x, int y)
{
/* Erase the bullet pixmap from the window */
XClearArea(display, window, x - BULLET_WC, y - BULLET_HC,
BULLET_WIDTH, BULLET_HEIGHT, False);
}
static void DrawTheTink(Display *display, Window window, int x, int y)
{
/* Draw the tink pixmap into the window */
RenderShape(display, window, tinkPixmap, tinkMask,
x - TINK_WC, y - TINK_HC, TINK_WIDTH, TINK_HEIGHT, False);
}
static void EraseTheTink(Display *display, Window window, int x, int y)
{
/* Erase the tink pixmap from the window */
XClearArea(display, window, x - TINK_WC, y - TINK_HC,
TINK_WIDTH, TINK_HEIGHT, False);
}
void DrawTheBullet(Display *display, Window window, int x, int y)
{
/* Draw the bullet pixmap into the window */
RenderShape(display, window, bulletPixmap, bulletMask,
x - BULLET_WC, y - BULLET_HC, BULLET_WIDTH, BULLET_HEIGHT, False);
}
static void DrawBullet(Display *display, Window window, int i)
{
/* Clear the window of the bullet in the old position */
XClearArea(display, window, bullets[i].xpos - BULLET_WC,
bullets[i].oldypos - BULLET_HC, BULLET_WIDTH, BULLET_HEIGHT, False);
/* Now draw the new bullet in the new position */
DrawTheBullet(display, window, bullets[i].xpos, bullets[i].ypos);
}
static int CheckEyeDudeBulletCollision(Display *display, Window window,
int bx, int by)
{
/*
* Check if the bullet has hit an eye dude.
*/
int eyeX, eyeY;
GetEyeDudePosition(&eyeX, &eyeY);
/* Check if any part of the bullets coords is inside the eyedudes box */
if (((bx + BULLET_WC) >= (eyeX - EYEDUDE_WC)) &&
((bx - BULLET_WC) <= (eyeX + EYEDUDE_WC)) &&
((by + BULLET_HC) >= (eyeY - EYEDUDE_WC)) &&
((by - BULLET_HC) <= (eyeY + EYEDUDE_WC)))
return True;
else
return False;
}
static int CheckBallBulletCollision(Display *display, Window window,
int bx, int by, int j)
{
int ballX, ballY;
GetBallPosition(&ballX, &ballY, j);
/* Check if any part of the bullets coords is inside the balls box */
if (((bx + BULLET_WC) >= (ballX - BALL_WC)) &&
((bx - BULLET_WC) <= (ballX + BALL_WC)) &&
((by + BULLET_HC) >= (ballY - BALL_HC)) &&
((by - BULLET_HC) <= (ballY + BALL_HC)))
return True;
else
return False;
}
static int CheckForBulletCollision(Display *display, Window window,
int x, int y)
{
/* Check for bullet to block collision */
int row, col;
struct aBlock *blockP;
/* Get the row and col for block where bullet is */
X2COL(col, x);
Y2ROW(row, y);
blockP = &blocks[row][col];
/* If blocks is occupied then check for collision */
if (blockP->occupied == 1 && blockP->exploding == False)
{
/* Check if x adjusted for bullet width is in block region */
if (((x + BULLET_WC) > blockP->x) &&
((x - BULLET_WC) < (blockP->x + blockP->width)))
{
if (((y + BULLET_HC) > blockP->y) &&
((y - BULLET_HC) < (blockP->y + blockP->height)))
{
/* Collision */
return True;
}
}
}
/* No collision if reached here */
return False;
}
static int ResetBulletStart(Display *display, Window window)
{
int status, size;
/* Start a bullet on the way if possible */
if (fastGun == True)
{
/* Obtain the size of the paddle */
size = GetPaddleSize();
/* Shoot 2 bullets - dual fire!!!!! Power */
status = StartABullet(display, window, paddlePos - (size / 3));
status = StartABullet(display, window, paddlePos + (size / 3));
}
else
status = StartABullet(display, window, paddlePos);
/* Return status of bullet */
return status;
}
void HandleBulletMode(Display *display, Window window)
{
/* Update all the bullets that may be moving */
if ((frame % BULLET_FRAME_RATE) == 0)
UpdateBullet(display, window);
/* Clear any tinks that are due to be cleared */
CheckTinks(display, window);
}