File: base.c

package info (click to toggle)
xinvaders 2.0-12
  • links: PTS
  • area: non-free
  • in suites: potato
  • size: 312 kB
  • ctags: 443
  • sloc: ansic: 2,364; makefile: 256; sh: 6
file content (347 lines) | stat: -rw-r--r-- 7,135 bytes parent folder | download
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
/* 
Copyright notice:

This is mine.  I'm only letting you use it.  Period.  Feel free to rip off
any of the code you see fit, but have the courtesy to give me credit.
Otherwise great hairy beasties will rip your eyes out and eat your flesh
when you least expect it.

Jonny Goldman <jonathan@think.com>

Wed May  8 1991
*/

/* base.c - handle movement, etc. of the base. */

#include "vaders.h"

extern int paused;
Boolean basedestroyed;

static Boolean showingexplosion = FALSE;

void DrawBuildings();

#define BASEY (gameheight-base->height)

typedef struct _BaseRec {
  int x;			/* Location. */
  int v;			/* velocity */
  int width, height;		/* box of this base. */
  XImage *shape_image;		/* an XImage for the spaceship */
} BaseRec, *Base;

BaseRec baserec;

Base base = &baserec;

XImage *explosion;

#define BaseNearPoint(base, x, y)	\
  ((base)->x <= (x) && (x) < (base)->x + (base)->width  && \
   y <= BASEY + (base)->height && y > BASEY)

#include "base1.bit"
#include "base2.bit"
#include "explode1.bit"
#include "explode2.bit"

int ReadBaseImage()
{
/*
  unsigned int width, height;
  int x_hot, y_hot;
  char *data, filename[255];
  int status;
*/

  base->width = (scale == 1) ? base1_width : base2_width;
  base->height = (scale == 1) ? base1_height : base2_height;

  base->shape_image = XCreateImage(dpy,
				   DefaultVisual(dpy, DefaultScreen(dpy)),
				   1,
				   XYBitmap,
				   0,
				   (scale == 1) ? base1_bits : base2_bits,
				   base->width, base->height,
				   8, 0);

  base->shape_image->bitmap_bit_order = LSBFirst;
  base->shape_image->byte_order = LSBFirst;

  explosion = XCreateImage(dpy,
			   DefaultVisual(dpy, DefaultScreen(dpy)),
			   1,
			   XYBitmap,
			   0,
			   (scale == 1) ? explode1_bits : explode2_bits,
			   (scale == 1) ? explode1_width : explode2_width,
			   (scale == 1) ? explode1_height : explode2_height,
			   8, 0);
  explosion->bitmap_bit_order = LSBFirst;
  explosion->byte_order = LSBFirst;
  
  return BitmapSuccess;
}

void InitBase()
{
    if( ReadBaseImage() != BitmapSuccess) {
      fprintf(stderr, "Error reading base image.\n");
      exit(20);
    }
    basedestroyed = TRUE;
    showingexplosion = FALSE;
    basetimerid = 0;
    base->v = 0;
}



void PaintBase(GC gc)
{
  XPutImage(dpy, gamewindow, gc, base->shape_image,
	    0, 0, base->x, gameheight-base->height, base->width, base->height);
}


void ShowBase(int i, GC gc)
{
  XPutImage(dpy, labelwindow, gc, base->shape_image,
	    0, 0, i*(base->width+2), gameheight/2-(3*base->height),
	    base->width, base->height);
}

void PaintBasesLeft()
{
  int i;
  XDrawString(dpy, labelwindow, scoregc,
	      0, gameheight-(4*base->height),
	      "Bases", 5);
  for(i = 0; i < basesleft; i++) {
    ShowBase(i, basegc);
  }
}


void ShowExplosion(gc)
GC gc;
{
  XPutImage(dpy, gamewindow, gc, explosion,
	      0, 0, base->x, gameheight-base->height, explosion->width, explosion->height);
}

void DestroyBase()
{
  if(!paused) {
    PaintBase(backgc);
    basedestroyed = TRUE;
    showingexplosion = TRUE;
    ShowExplosion(basegc);
    if (basetimerid) XtRemoveTimeOut(basetimerid);
    basetimerid = XtAddTimeOut(1000, MoveBase, (Opaque) MoveBase);
  }
}


Boolean ShotHitsBase(x,y)
int x,y;
{
  if(!basedestroyed && BaseNearPoint(base, x, y)) {
    DestroyBase();
    return TRUE;
  }
  return FALSE;
}

void ResetGame()
{
  static Arg args[1];

  spacer_shown = 0;
  SuspendTimers();
  XClearWindow(dpy, gamewindow);
  paused = 1;
  InitScore();
  basesleft--;
  level = 1;
  CreateVaders(level);
  spacer_counter = 1000;
  numshots = 0;
  numvshots = 0;
  PaintAllVaders();
  PaintBasesLeft();
  InitBuildings();
  DrawBuildings();
  lastscore = 0;
  PaintScore();
  XSync(dpy, 0);
  basedestroyed = FALSE;
  base->x = base->v = 0;
  showingexplosion = FALSE;
  PaintBase(basegc);
  XtSetArg(args[0], XtNlabel, _(" Start"));
  XtSetValues(pausebutton, args, 1);
}

/*ARGSUSED*/
void MoveBase(closure, id)
Opaque closure;
XtIntervalId id;
{
  if (closure != (Opaque) MoveBase) return;
  if(!paused) {
    if (basedestroyed) {
      if (showingexplosion) {
	ShowExplosion(backgc);
	showingexplosion = FALSE;
	basetimerid = XtAddTimeOut(2000, MoveBase, (Opaque) MoveBase);
	return;
      }
      if (basesleft <= 0) {
	ResetGame();
	return;
      }
      base->x = 0;
      basesleft--;
      ShowBase(basesleft, backgc);
      PaintBase(basegc);
      PaintScore();
      basedestroyed = FALSE;
      base->v = 0;
    }

    if (!paused)
      basetimerid = XtAddTimeOut(basewait, MoveBase, (Opaque) MoveBase);
    if(base->v) {
      PaintBase(backgc);
      base->x += base->v;
      base->x = (base->x < 0) ? 0 :
      ((base->x > gamewidth-base->width) ? gamewidth-base->width : base->x);
      PaintBase(basegc);
    }
  }
}

void MoveLeft()
{
  if(!paused) base->v= -scale;
}


void MoveRight()
{
  if(!paused) base->v = scale;
}


void Stop()
{
  if(!paused)
    base->v = 0;
}


void Fire()
{
    if (!basedestroyed&&!paused) AddShot(base->x+base->width/2, gameheight-base->height);
}

/* this part is for the buildings */

#define NUMBUILDINGS 4
#define HUNKROWS 4
#define NUMHUNKS 10
#define HUNKWIDTH (2*scale)
#define HUNKHEIGHT (4*scale)
#define buildingwidth HUNKWIDTH*NUMHUNKS
#define buildingheight HUNKHEIGHT*HUNKROWS

typedef struct {
  int x,y;
  Boolean hunks[HUNKROWS][NUMHUNKS];
} BuildingRec, *Building;

BuildingRec buildings[NUMBUILDINGS];


void DrawBuildingHunk(building, r, c, gc)
Building building;
int r,c;
GC gc;
{
  int x, y;

  x = building->x+c*HUNKWIDTH;
  y = gameheight-scale*45+r*HUNKHEIGHT;

  XFillRectangle(dpy, gamewindow, gc, x, y, HUNKWIDTH, HUNKHEIGHT);
}

void ToastHunk(building,r,c)
Building building;
int r,c;
{
  building->hunks[r][c] = FALSE;
  DrawBuildingHunk(building, r, c, backgc);
}

Boolean ShotHitsBuilding(x, y)
int x,y;
{
  int i, r, c;
  Building building;

  for(i=0; i< NUMBUILDINGS; i++) {
    building = &buildings[i];
    if(x>=building->x && x<building->x+buildingwidth &&
       y>=gameheight-scale*45 && y<gameheight-scale*45+buildingheight) {
      r = (y-(gameheight-scale*45))/HUNKHEIGHT;
      c = (x-building->x)/HUNKWIDTH;
      if (r<0 || r>=HUNKROWS)
	printf(_("Error in row"));
      if (c<0 || c>=NUMHUNKS)
	printf(_("Error in column"));
      if(building->hunks[r][c]) {
	ToastHunk(building, r,c);
	return TRUE;
      }
      return FALSE;
    }
  }
  return FALSE;
}

void InitBuildings()
{
  int i, j, k;

  for(i=0; i< NUMBUILDINGS; i++) {
    buildings[i].x = i*((gamewidth ?
			 (scale*(VWIDTH-70)) :
			 (gamewidth-scale*70)))/4+scale*35+(HUNKWIDTH*NUMHUNKS)/2;
    for (j=0; j<HUNKROWS; j++)
      for (k = 0; k < NUMHUNKS; k++) 
	buildings[i].hunks[j][k] = TRUE;
  }
  j--;

  for(i=0; i< NUMBUILDINGS; i++) {
    buildings[i].hunks[0][0] = FALSE;
    buildings[i].hunks[0][NUMHUNKS-1] = FALSE;
    for (k = 3; k < NUMHUNKS-3; k++) 
	buildings[i].hunks[j][k] = FALSE;
  }
}

void DrawBuildings()
{
  int i, j, k;

  for(i=0; i< NUMBUILDINGS; i++) {
    for (j=0; j<HUNKROWS; j++)
      for (k = 0; k < NUMHUNKS; k++) 
	if(buildings[i].hunks[j][k]) DrawBuildingHunk(&buildings[i], j, k, buildinggc);
  }
}