File: GravityWars101.c

package info (click to toggle)
gravitywars 1.102-34
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid, stretch
  • size: 2,468 kB
  • ctags: 553
  • sloc: ansic: 3,938; makefile: 52; sh: 52
file content (454 lines) | stat: -rw-r--r-- 9,740 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
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
/* -------------------------------------------------------------------------
   Gravity Wars v1.1, (C) Sami Niemi 1995, 1996

   NOTES: [960824] [RELEASE NOTES 961101]

   The game started as a bunch of fun routines that made the ship fly.
   Later on I added collition control, and after P�r got involved with
   the graphics I just couldn't stop... So, the structure of the game is
   just terrible, with lots of unecessary variables , flags that
   are used as variables etc.. I should've written the program in c++, and
   for X! Svgalib just contains too many bugs.. 
   
   I'd be fun to see this game ported to different operating systems, though
   the structure of the game might make it hard, especially since I used 
   the SVGALIB.

   You're free to modify the code and the graphics as long as you write
   that the original code was written by Sami Niemi, and the original
   graphics were drawn by P�r Johannesson (If you're using any parts of
   the original graphics)
   
   I'd love to hear from any changes that you've made to the game!

   / Sami

   sami@niemi.com
   saminiemi@usa.net
   http://www.kuai.se/~sniemi
   http://www.efd.lth.se/~e96sn


   REMEMBER:

   getbox(x,y,*ptr) Gets the original graphics based on the block[]&level[]
   getbox2(x,y,*ptr Gets the graphics directly from the screen



   BUGS (i.e. I don't have the time to fix'em):

   - consoleswitching takes away the splitscreen (real easy to fix)

   ------------------------------------------------------------------------- */
#include <unistd.h>
#include "includes.h"
#include "memory.h"

#if defined (USE_SDL) && defined (USE_JOYSTICK)
#include <stdlib.h>
#include <errno.h>
extern SDL_Joystick * joy;
extern int JOY_INDEX;
extern int JOY_XAXIS;
extern int JOY_YAXIS;
extern int JOY_BUTTON_UP;
extern int JOY_BUTTON_SHOOT;
extern int JOY_BUTTON_TOGGLE_FULLSCREEN;
extern int JOY_BUTTON_ESCAPE;
extern Sint16 JOY_XAXIS_CENTER;
extern Sint16 JOY_XAXIS_MINIMUM;
extern Sint16 JOY_XAXIS_MAXIMUM;
extern Sint16 JOY_XAXIS_NULLZONE;
extern Sint16 JOY_YAXIS_CENTER;
extern Sint16 JOY_YAXIS_MINIMUM;
extern Sint16 JOY_YAXIS_MAXIMUM;
extern Sint16 JOY_YAXIS_NULLZONE;

int get_int_env_var (char * env_name,int * i)
{
	char * env_val = NULL;
	int tmp = 0;

	if ((env_val = getenv (env_name)) != NULL) {
#ifdef DEBUG
		printf ("env_var : %s=%s\n", env_name, env_val);
#endif
		errno = 0;
		tmp = (int) strtol (env_val, NULL, 10);
		if (!errno) {
			*i = tmp;
			return 0;
		} else {
			return (1);
		}
	}
	return (2);
}

int get_Sint16_env_var (char * env_name,Sint16 * i)
{
	char * env_val = NULL;
	Sint16 tmp = 0;

	if ((env_val = getenv (env_name)) != NULL) {
#ifdef DEBUG
		printf ("env_var : %s=%s\n", env_name, env_val);
#endif
		errno = 0;
		tmp = (Sint16) strtol (env_val, NULL, 10);
		if (!errno) {
			*i = tmp;
			return (0);
		} else {
			return (1);
		}
	}
	return (2);
}
#endif

/*--------------------------------------------------------------------- main */
int main(int n_args, char *arg[]) {
#if defined (USE_SDL) && defined (USE_JOYSTICK)
	int c, index, calibrate_joystick = 0;
	opterr = 0;
#endif

  int n,ch,x,y;
  
  long tmp_long;
  
  long  pal_m;
  long pal_n;
  
  short baselevel;
  char filename[128];
  
  char firstlevel[80]="levels/level01";
  char buf[128];

  levelnum=1;
  
  if (readlink(arg[0],buf,128)==-1) 
    strcpy(buf,arg[0]);

#if defined (USE_SDL) && defined (USE_JOYSTICK)
	while ((c = getopt (n_args, arg, "j")) != -1) {
		switch (c) {
			case 'j':
				calibrate_joystick = 1;
				break;
			case '?':
				fprintf (stderr, "Unknown option. Use '-j' to configure joystick.\n\n");
				break;
			default:
				break;
		}
	}
#endif
/*  
  n=strlen(buf);
  while(n>=0)                       
    if (buf[n--]=='/') break;
  if (n<0) {
    strcpy(gamename,"/");
    gamenamelen=1;
  }  
  else {
    strcpy(gamename,buf);
    gamename[n+2]=0;
    gamenamelen=strlen(gamename);
  }
*/

	strcpy(gamename,"/usr/share/games/gravitywars/");
	gamenamelen=strlen(gamename);

#if defined (USE_SDL) && (USE_JOYSTICK)
	if (optind < n_args) {
#else
  if (n_args>1) {
#endif
    n=0;
    do {
      n++;
#if defined (USE_SDL) && (USE_JOYSTICK)
    } while ( (strncasecmp(arg[optind],codes[n],6)) && (n<99));
#else
    } while ( (strncasecmp(arg[1],codes[n],6)) && (n<99));
#endif

    if (n!=99) {
      levelnum=n+1;
#ifndef USE_SDL
      if (n_args>2)
	delay_len=atoi(arg[2]);
#endif
    } else {
#ifdef USE_SDL
#ifndef USE_JOYSTICK
      printf("\nUsage: %s [code-to-a-given-level]\n"
#else
      printf("\nUsage: %s [-j] [code-to-a-given-level]\n"
#endif
	     "(Sure you've got the right code?)\n\n",arg[0]);
      exit(0);
#else
      if ((arg[1][0]<'0') || (arg[1][0]>'9')) {
	printf("\nUsage: %s [code-to-a-given-level] [beam-adjustment]\n"
	       "(Sure you've got the right code?)\n\n",arg[0]);
	exit(0);
      }
      else
	delay_len=atoi(arg[1]);
#endif
    } 
  }    
  
  baselevel=levelnum;

  strcpy(firstlevel,nextlevel);


  printf(
"Gravity Wars v1.1 (November 1995), a Gravity Force clone for Unix\n"
"Coding by Sami Niemi, graphics by P�r Johannesson, SDL port by Sam Hocevar\n"
);

  initParams();
  loadGfx();
  rotGfx();

  initScreen();

  keyboard_init();

#ifdef USE_SDL
#ifdef USE_JOYSTICK
	if (calibrate_joystick == 1)
		set_joystick ();
	else
		if (load_joystick_settings () == 0)
			printf ("Joystick configuration loaded from file.\n");
		else
			printf ("Can not load joystick configuration file. Using defaults.\n");
/*Parsing environment variables*/
	get_int_env_var ("GW_JOY_INDEX", &JOY_INDEX);
	get_int_env_var ("GW_JOY_XAXIS", &JOY_XAXIS);
	get_int_env_var ("GW_JOY_YAXIS", &JOY_YAXIS);
	get_int_env_var ("GW_JOY_BUTTON_UP", &JOY_BUTTON_UP);
	get_int_env_var ("GW_JOY_BUTTON_SHOOT", &JOY_BUTTON_SHOOT);
	get_int_env_var ("GW_JOY_BUTTON_TOGGLE_FULLSCREEN", &JOY_BUTTON_TOGGLE_FULLSCREEN);
	get_int_env_var ("GW_JOY_BUTTON_ESCAPE", &JOY_BUTTON_ESCAPE);

	get_Sint16_env_var ("GW_JOY_XAXIS_CENTER", &JOY_XAXIS_CENTER);
	get_Sint16_env_var ("GW_JOY_XAXIS_MINIMUM", &JOY_XAXIS_MINIMUM);
	get_Sint16_env_var ("GW_JOY_XAXIS_MAXIMUM", &JOY_XAXIS_MAXIMUM);
	get_Sint16_env_var ("GW_JOY_XAXIS_NULLZONE", &JOY_XAXIS_NULLZONE);
	get_Sint16_env_var ("GW_JOY_YAXIS_CENTER", &JOY_YAXIS_CENTER);
	get_Sint16_env_var ("GW_JOY_YAXIS_MINIMUM", &JOY_YAXIS_MINIMUM);
	get_Sint16_env_var ("GW_JOY_YAXIS_MAXIMUM", &JOY_YAXIS_MAXIMUM);
	get_Sint16_env_var ("GW_JOY_YAXIS_NULLZONE", &JOY_YAXIS_NULLZONE);

	joystick_init ();
#endif
#endif

  mouse_init("/dev/mouse", MOUSE_MICROSOFT, MOUSE_DEFAULTSAMPLERATE);
  scans=keyboard_getstate();

  ending=TRUE;

  while(ending) {

    intro();
    if (escape) 
      goto escaped;
    loaddata();
    drawScreen();

/*
    old_x=0;
    old_y=0;
    getbox(old_x, old_y, shipback[0]);
    putbox(old_x, old_y, shipback[0]);
    moveShip(0,0);
*/
    old_x=sx[0] >> STEP;
    old_y=sy[0] >> STEP;
    moveShip(0,0);

    updatescore();
    win_y=(sy[0] >> STEP)-232;
    if (win_y<8)
      win_y=8;
    else
      if (win_y>968)
	win_y=968;
    putscore(0,win_y); 


    /*CenterShip*/
    y=(sy[0] >> STEP)-240;
    if (y<16)
      y=16;
    else
      if (y>960) 
	y=960;
    gl_setdisplaystart(0,y);

    stampnum(levelnum);
    putstamp(272,y+184,68,69,70);

    if (levelnum&1) {
      for(pal_n=0; pal_n<=1024; pal_n+=16) {
	for (pal_m=0; pal_m<=767; pal_m++)
	  p1[pal_m]=(((long)pal[pal_m])*pal_n)>>10;
	vga_waitretrace();
	gl_setpalette(p1);
      }
      gl_setpalette(pal);
    }
    else {
      for(pal_n=0; pal_n<=1024; pal_n+=16) {
        for (pal_m=0; pal_m<=767; pal_m++)
          p1[pal_m]=(((long)palB[pal_m])*pal_n)>>10;
        vga_waitretrace();
        gl_setpalette(p1);
      }
      gl_setpalette(palB);
    }

    waitanykey();

    killstamp(272,y+184);
 
    anim_frame=0;
    ShipScore=0;
  
    control(0); 

    /*------ The End --------*/
    if (ending) {

      for(n=0; n<=4095; n++) {
	tmpscore[n]=score[n];
	score[n]=highscore[n];
      }

      tmp_long=Dec2BCD[ShipScore];

      putdigit(1375,tmp_long>>16);
      putdigit(1383,(tmp_long>>12)&15);
      putdigit(1391,(tmp_long>>8)&15);
      putdigit(1399,(tmp_long>>4)&15);
      putdigit(1407,tmp_long&15);

      if (ShipScore>HighScore) {
	HighScore=ShipScore;

	strcpy(filename,"/var/games/gravitywars/hscore.gw");
	if ( ( fileptr=fopen(filename,"w") ) != NULL) {
	  fprintf(fileptr,"%ld\n",ShipScore);
	  fclose(fileptr);
	}
	else {
	  printf("Can't write the HighScore!!!!\n");
	  doPanic();
	}
      }
 
      tmp_long=Dec2BCD[HighScore];
 
      putdigit(1474,tmp_long>>16);
      putdigit(1482,(tmp_long>>12)&15);
      putdigit(1490,(tmp_long>>8)&15);
      putdigit(1498,(tmp_long>>4)&15);
      putdigit(1506,tmp_long&15);

      putscoreOnly(0,win_y);

      putstamp(272,win_y+192,54,60,61);
      waitanykey();
      killstamp(272,win_y+192);
      if (*(scans+SCANCODE_ESCAPE))
	goto escaped;

      for(n=0; n<=4095; n++) {
	score[n]=tmpscore[n];
      }

      if (levelnum&1) {
	for(pal_n=1024; pal_n>=0; pal_n-=16) {
	  for (pal_m=0; pal_m<=767; pal_m++)
	    p1[pal_m]=(((long)pal[pal_m])*pal_n)>>10;
	  vga_waitretrace();
	  gl_setpalette(p1);
	}
	gl_setpalette(p0);
      }
      else {
        for(pal_n=1024; pal_n>=0; pal_n-=16) {
          for (pal_m=0; pal_m<=767; pal_m++)
            p1[pal_m]=(((long)palB[pal_m])*pal_n)>>10;
          vga_waitretrace();
          gl_setpalette(p1);
        }
        gl_setpalette(p0);
      }

      strcpy(nextlevel,firstlevel);
      levelnum=baselevel;
      ShipLife=3;
    }

  }
 escaped:    

  mouse_close();
  keyboard_close();

#ifdef USE_SDL
#ifdef USE_JOYSTICK
	if (joy != NULL)
		joystick_close ();
#endif
#endif

  vga_setmode(TEXT);
  printf("\n");
  exit(0);

}