1 /*
2 Copyright notice:
3 
4 This is mine.  I'm only letting you use it.  Period.  Feel free to rip off
5 any of the code you see fit, but have the courtesy to give me credit.
6 Otherwise great hairy beasties will rip your eyes out and eat your flesh
7 when you least expect it.
8 
9 Jonny Goldman <jonathan@think.com>
10 
11 Wed May  8 1991
12 */
13 
14 /* shot.c - handle movement, etc. of the shots. */
15 
16 #ifdef HAVE_CONFIG_H
17 #include <config.h>
18 #endif
19 
20 #include "vaders.h"
21 
22 extern int paused;
23 
24 #define MAXSHOTS	1
25 #define MAXVSHOTS	6
26 #define SHOTSIZE	(8*scale)
27 
28 typedef struct _ShotRec {
29     int x, y;		/* Location of this shot. */
30 } ShotRec, *Shot;
31 
32 ShotRec shots[MAXSHOTS], vshots[MAXVSHOTS];
33 
34 XImage *vshot_image[2];
35 
36 static int tick = 0;
37 
38 int numshots;		/* Number of shots currently flying. */
39 int numvshots;		/* Number of shots currently flying. */
40 
PaintShot(shot,gc)41 static void PaintShot(shot, gc)
42 Shot shot;
43 GC gc;
44 {
45     AddLine(shot->x, shot->y,
46 	    shot->x, shot->y + SHOTSIZE, gc);
47 }
48 
PaintVshot(vshot,gc)49 static void PaintVshot(vshot, gc)
50 Shot vshot;
51 GC gc;
52 {
53   XPutImage(dpy, gamewindow, gc, vshot_image[tick],
54 	    0, 0, vshot->x, vshot->y, vshot_image[tick]->width, vshot_image[tick]->height);
55 }
56 
57 
PaintAllShots()58 void PaintAllShots()
59 {
60     int i;
61     for (i=0 ; i<numshots ; i++)
62 	PaintShot(shots + i, shotgc);
63     for (i=0 ; i<numvshots ; i++)
64 	PaintVshot(vshots + i, vshotgc);
65 }
66 
67 
DestroyShot(i)68 static void DestroyShot(i)
69 int i;
70 {
71     PaintShot(shots + i, backgc);
72     numshots--;
73     shots[i] = shots[numshots];
74 }
75 
76 
DestroyVshot(i)77 static void DestroyVshot(i)
78 int i;
79 {
80     PaintVshot(vshots + i, backgc);
81     numvshots--;
82     vshots[i] = vshots[numvshots];
83 }
84 
85 /*ARGSUSED*/
MoveShots(closure,id)86 void MoveShots(closure, id)
87 Opaque closure;
88 XtIntervalId id;
89 {
90   int i, x, y, newy;
91   Shot shot;
92   if (closure != (Opaque) MoveShots) return;
93   if (!paused) {
94     if (numshots > 0)
95       shottimerid = XtAddTimeOut(shotwait, MoveShots, (Opaque) MoveShots);
96     else
97       shottimerid = 0;
98     for (i=0 ; i<numshots ; i++) {
99       shot = shots + i;
100       newy = shot->y - SHOTSIZE/2;
101       x = shot->x;
102       y = shot->y;
103       if (ShotHitsVader(x, y)
104 	  || ShotHitsSpacer(x, y)
105 	  || ShotHitsBuilding(x, y)
106 	  || y < 0) {
107 	DestroyShot(i);
108 	i--;			/* Ensures we don't skip moving a shot. */
109       } else {
110 	PaintShot(shot, backgc);
111 	shot->y = newy;
112 	PaintShot(shot, shotgc);
113       }
114     }
115   }
116 }
117 
VshotHitsShot(x,y)118 Boolean VshotHitsShot(x, y)
119 int x, y;
120 {
121   int i, dx, dy;
122   Shot shot;
123 
124   for (i=0; i<numshots; i++) {
125     shot = shots + i;
126     dx = shot->x;
127     dy = shot->y;
128     if(dx >= x && dx < x+vshot_image[tick]->width
129        && dy >= y && dy < y+vshot_image[tick]->height) {
130       DestroyShot(i);
131       return TRUE;
132     }
133   }
134   return FALSE;
135 }
136 
137 /*ARGSUSED*/
MoveVshots(closure,id)138 void MoveVshots(closure, id)
139 Opaque closure;
140 XtIntervalId id;
141 {
142   int i, x, y, newy;
143   Shot vshot;
144 
145   if (closure != (Opaque) MoveVshots) return;
146   if (!paused) {
147     if (numvshots > 0)
148       vshottimerid = XtAddTimeOut(vshotwait, MoveVshots, (Opaque) MoveVshots);
149     else
150       vshottimerid = 0;
151     for (i=0 ; i<numvshots ; i++) {
152       vshot = vshots + i;
153       newy = vshot->y + 2*scale;
154       x = vshot->x;
155       y = vshot->y;
156       if (y>gameheight ||
157 	  VshotHitsShot(x, y) ||
158 	  ShotHitsBase(x,y) ||
159 	  ShotHitsBuilding(x,y)) {
160 	DestroyVshot(i);
161 	i--;			/* Ensures we don't skip moving a shot. */
162       } else {
163 	PaintVshot(vshot, backgc);
164 	tick = tick ? 0 : 1;
165 	vshot->y = newy;
166 	PaintVshot(vshot, vshotgc);
167 	tick = tick ? 0 : 1;
168       }
169     }
170     tick = tick ? 0 : 1;
171   }
172 }
173 
174 
AddShot(x,y)175 void AddShot(x, y)
176 int x, y;
177 {
178     Shot shot;
179     if (numshots >= maxshots) return;
180     shot = shots + numshots;
181     numshots++;
182     shot->x = x;
183     shot->y = y-SHOTSIZE;
184     PaintShot(shot, shotgc);
185     if (shottimerid == 0)
186         shottimerid = XtAddTimeOut(shotwait, MoveShots, (Opaque) MoveShots);
187 }
188 
AddVshot(x,y)189 void AddVshot(x, y)
190 int x, y;
191 {
192     Shot shot;
193     if (numvshots >= maxvshots) return;
194     shot = vshots + numvshots;
195     numvshots++;
196     shot->x = x;
197     shot->y = y;
198     PaintVshot(shot, vshotgc);
199     if (vshottimerid == 0)
200       vshottimerid = XtAddTimeOut(shotwait, MoveVshots, (Opaque) MoveVshots);
201 }
202 
203 #include "sperma1.bit"
204 #include "sperma2.bit"
205 #include "spermb1.bit"
206 #include "spermb2.bit"
207 
ReadVshotImages()208 int ReadVshotImages()
209 {
210   vshot_image[0] = XCreateImage(dpy,
211 				DefaultVisual(dpy, DefaultScreen(dpy)),
212 				1,
213 				XYBitmap,
214 				0,
215 				(scale == 1) ? sperma1_bits : sperma2_bits,
216 				(scale == 1) ? sperma1_width : sperma2_width,
217 				(scale == 1) ? sperma1_height : sperma2_height,
218 				8, 0);
219   vshot_image[0]->bitmap_bit_order = LSBFirst;
220   vshot_image[0]->byte_order = LSBFirst;
221 
222   vshot_image[1] = XCreateImage(dpy,
223 				DefaultVisual(dpy, DefaultScreen(dpy)),
224 				1,
225 				XYBitmap,
226 				0,
227 				(scale == 1) ? spermb1_bits : spermb2_bits,
228 				(scale == 1) ? spermb1_width : spermb2_width,
229 				(scale == 1) ? spermb1_height : spermb2_height,
230 				8, 0);
231   vshot_image[1]->bitmap_bit_order = LSBFirst;
232   vshot_image[1]->byte_order = LSBFirst;
233 
234   return BitmapSuccess;
235 }
236 
InitShot()237 void InitShot()
238 {
239     shottimerid = 0;
240     numshots = 0;
241     vshottimerid = 0;
242     numvshots = 0;
243     if( ReadVshotImages() != BitmapSuccess) {
244       fprintf(stderr, _("Error reading vshot images.\n"));
245       exit(20);
246     }
247 }
248 
AddLine(fromx,fromy,tox,toy,gc)249 void AddLine(fromx, fromy, tox, toy, gc)
250 int fromx, fromy, tox, toy;
251 GC gc;
252 {
253     XDrawLine(dpy, gamewindow, gc, fromx, fromy, tox, toy);
254 }
255