forked from OldSkoolCode/Sega-Nuclear-Rush
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMAIN.C68
executable file
·613 lines (533 loc) · 12 KB
/
MAIN.C68
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
/***
* Main file for Nuclear Rush
* Copyright 1993, Futurescape Productions
* All Rights Reserved
***/
#include <stdio.h>
#include <stdlib.h>
#include "game.h"
#include "gems.h"
#define NOASCII TRUE
#define DEBUG FALSE
#define HUD TRUE
#define CENTERED TRUE
#define MAPSHIFT 5 /* 32x32 HUD map displays 1024x1024 */
#define SOUNDS TRUE
#define FPS FALSE
extern void Set_HScroll();
extern void UI_ClearScreen(UWORD);
/* extern void Update_Cockpit(); */
extern UWORD SYS_BASECHR;
extern UWORD SYS_BASECHR1;
extern UWORD SYS_LASTCHR;
extern WORD SYS_LASTSPR;
extern UWORD SYS_CHRATTR; /* Character Attributes */
extern UWORD SYS_MINFDIST;
extern UWORD SYS_MAXFDIST;
extern UWORD SYS_DISTDIV;
extern UWORD SYS_AVGFDIST;
extern FRAC SYS_BORDERSIZE;
extern int NumCracks; /* Number of cracks in window */
extern int NumNukes; /* Number of nuclear pools in the world */
extern int FreeNukes; /* Number of free nuclear pools in the world */
extern NUKE_POOL NukePools[30]; /* Nuke pool information */
extern UWORD SYS_HORZCHRS;
extern WORD SYS_SCRLXB;
extern WORD SYS_DOCYCLE;
extern WORD UI_DemoMode;
extern UWORD STATS_SHOTS;
extern UWORD STATS_HITS;
extern void SendAnimPal(UWORD *anim_file);
extern void DispFrame(UWORD *anim_file,WORD anim,WORD frame,WORD xp,WORD yp);
extern PLAYER Player;
UWORD Game_Frame; /* Game Frame counter */
char OutBuf[80];
/***
* Debugging display
***/
#include <stdarg.h>
#include <ctype.h>
#include <errno.h>
void
Bomb(char *fmt, ...)
{
va_list args;
char OutBuf[128];
extern void DisnableInts();
extern void EHInts();
extern WORD ScreenCol;
extern WORD ScreenRow;
ScreenRow = 2;
ScreenCol = 1;
va_start(args,fmt);
DisableInts();
vsprintf(OutBuf,fmt,args);
puts(OutBuf);
perror(" Error");
va_end(args);
EnableInts();
while (1)
;
}
/***
*
* Vertical Blank queing code. Build a list of commands that VBlank
* will do when it has time.
*
***/
WORD SYS_DCLISTLEN; /* List length */
WORD SYS_DCLISTAVL; /* List ready to send */
extern BYTE SYS_DCLIST[2048]; /* Data or Ctrl flags */
extern UWORD SYS_DCDATA[2048]; /* WORDs to send */
void
SYS_DCListHold()
{
SYS_DCLISTAVL = FALSE;
}
void
SYS_DCListGo()
{
SYS_DCLISTAVL = TRUE;
}
void
SYS_DCAddData(UWORD Value)
{
SYS_DCLIST[SYS_DCLISTLEN] = TRUE;
SYS_DCDATA[SYS_DCLISTLEN] = Value;
SYS_DCLISTLEN++;
}
/***
* WaitUntilSafe: Wait until we know VBlank will not trash VDP
***/
void
WaitUntilSafe()
{
UWORD cnt;
cnt = VCNTR;
if (cnt < 1 || cnt > 200)
SYS_WaitFrame();
}
/***
* VDP_puts: Print text to the VDP
***/
VDP_puts(char *text)
{
char *ptr;
UWORD Addr;
extern WORD ScreenCol;
extern WORD ScreenRow;
SYS_DCListHold();
Addr = (ScreenRow * (64 * 2)) + (ScreenCol * 2) + 0xE000;
SYS_DCAddVAddr(Addr);
for (ptr=text; *ptr; ptr++)
SYS_DCAddData(*ptr + 17 | 0xE000);
SYS_DCListGo();
ScreenRow++;
ScreenCol = 0;
}
/***
* Display debugging information on the screen
***/
void
Debug(char *fmt, ...)
{
va_list args;
char OutBuf[128];
extern WORD ScreenCol;
extern WORD ScreenRow;
va_start(args,fmt);
vsprintf(OutBuf,fmt,args);
#if 0
DisableInts();
puts(OutBuf);
EnableInts();
#else
VDP_puts(OutBuf);
#endif
va_end(args);
}
/****** Globals *******/
WORD ScreenCol;
WORD ScreenRow; /* Where all Console I/O takes place from */
WORD SYS_SCROLLX;
extern WORD SYS_NEWXA;
extern WORD SYS_NEWXB;
UWORD SYS_GAMEDONE;
UWORD WorldNum;
BYTE SEND_LEFT; /* True if currently sending left data */
/****
Background Maps:
UWORD Offset to Palette
UWORD Offset to Map
UWORD Offset to Character Definitions
???
Palette:
WORD Color Register index
UWORD Number of colors
UWORD RGB Value (Sega format)
...
...Repeat...
-1 End of list
Map:
UWORD Width of Map
UWORD Height of Map
UWORD Map[][]
??? Insert new blocks here.
Character Definitions:
BYTE 32 Bytes per character
***/
extern UWORD SYS_LASTCHR;
#define DECOMPRESS TRUE
void
DispMap(UWORD *map_file)
{
UWORD *pal;
UWORD *map;
UWORD *chrs;
UWORD *wptr;
ULONG num_bytes;
int i,x,y;
int w,h;
int numchrs;
extern OBJECT3D World_Objs[];
wptr = map_file;
num_bytes = *(ULONG *)wptr;
LZ_Open((BYTE *)wptr,(BYTE *)World_Objs + 40000);
wptr = (UWORD *)World_Objs;
LZ_Read((BYTE *)wptr,num_bytes);
pal = (UWORD *)((BYTE *)World_Objs + *wptr++);
map = (UWORD *)((BYTE *)World_Objs + *wptr++);
chrs = (UWORD *)((BYTE *)World_Objs + *wptr);
SYS_SetPalette(pal);
/* Dump characters */
SYS_SetVAddr(SYS_LASTCHR*32);
numchrs = *chrs++;
for (i=0; i<numchrs; i++)
for (y=0; y<16; y++)
VDATA = *chrs++;
/* Dump map */
SYS_SetVAddr(SYS_LASTCHR*32);
w = *map++;
h = *map++;
if (h > 64)
h = 64;
i = 0;
for (y=0; y<h; y++) {
SYS_SetVAddr(0xC000+i);
for (x=0; x<w; x++)
VDATA = *map++ + SYS_LASTCHR;
i += 128;
}
SYS_LASTCHR += numchrs;
}
void
SetMapPal(UWORD *map_file)
{
UWORD *pal;
UWORD *wptr;
ULONG num_bytes;
extern OBJECT3D World_Objs[];
wptr = map_file;
num_bytes = *(ULONG *)wptr;
LZ_Open((BYTE *)wptr,(BYTE *)World_Objs + 40000);
wptr = (UWORD *)World_Objs;
LZ_Read((BYTE *)wptr,num_bytes);
pal = (UWORD *)((BYTE *)World_Objs + *wptr);
SYS_SetPalette(pal);
}
/***
* Display a MAP file on Screen B
***/
void
DispBMap(UWORD *map_file)
{
UWORD *pal;
UWORD *map;
UWORD *chrs;
UWORD *wptr;
int i,x,y;
int w,h;
int numchrs;
ULONG num_bytes;
extern OBJECT3D World_Objs[];
wptr = map_file;
num_bytes = *(ULONG *)wptr;
LZ_Open((BYTE *)wptr,(BYTE *)World_Objs + 40000);
wptr = (UWORD *)World_Objs;
LZ_Read((BYTE *)wptr,num_bytes);
pal = (UWORD *)((BYTE *)World_Objs + *wptr++);
map = (UWORD *)((BYTE *)World_Objs + *wptr++);
chrs = (UWORD *)((BYTE *)World_Objs + *wptr);
SYS_SetPalette(pal);
/* Dump characters */
SYS_SetVAddr(SYS_LASTCHR*32);
numchrs = *chrs++;
for (i=0; i<numchrs; i++)
for (y=0; y<16; y++)
VDATA = *chrs++;
/* Dump map */
SYS_SetVAddr(SYS_LASTCHR*32);
w = *map++;
h = *map++;
if (h > 64)
h = 64;
i = 0;
for (y=0; y<h; y++) {
SYS_SetVAddr(0xE000+i);
for (x=0; x<w; x++)
VDATA = *map++ + SYS_LASTCHR | 0x8000;
i += 128;
}
SYS_LASTCHR += numchrs;
}
volatile LONG SYS_VCNT;
volatile LONG SYS_OLDVCNT;
LONG FPS_List[10];
WORD FPS_LPtr;
WORD
Compute_FPS()
{
WORD i;
LONG avg;
FPS_List[FPS_LPtr] = SYS_VCNT - SYS_OLDVCNT;
avg = 0;
for (i=0; i<10; i++)
avg += FPS_List[i];
if (FPS_LPtr < 9)
FPS_LPtr++;
else
FPS_LPtr = 0;
return ((WORD)(6000/avg));
}
#define TEST 0
extern WORD Ship_Rotation;
extern int Ship_Turn_Speed;
extern BYTE SYS_FLICKER;
int Old_Alt;
int Alt;
extern int Visable_Quad1; /* View angle quadrant left side */
extern int Visable_Quad2; /* View angle quadrant right side */
extern short MinAngle;
extern short MaxAngle;
WORD Player_Fuel;
void
MainGameLoop(void)
{
int i,j,k,x,y;
extern FRAC Cx,Cy,Cz;
extern int Headset_Tilt;
WORD *wptr;
UWORD *uwptr;
UWORD chrtmp;
BYTE moved;
BYTE oldjoy;
extern int Num_Visable_Objs;
extern int Num_Clipped;
extern void Do_UI();
extern void Generate_World(short num);
extern void Get_World_Info(short num);
extern WORD PalBuffer[64];
extern OBJECT3D World_Objs[MAX3DOBJS]; /* Total object list */
/* extern int NumNukes; */
extern WORD SYS_SAFEDMA;
Player_Fuel = 31; /* Enough for eight minutes of play */
UI_ClearScreen(0xC000);
UI_ClearScreen(0xE000);
/***
* Main Game Loop
***/
while (1) {
Get_World_Info(WorldNum);
UI_WorldIntro();
Game_Frame = 0;
SYS_OLDVCNT = SYS_VCNT = 0;
Player.Won = FALSE;
if (Player.Items[3])
Player.Damage = 200;
else
Player.Damage = 100;
NumCracks = 0;
#if NOASCII
SYS_LASTCHR = 1;
#else
SYS_LASTCHR = 148;
#endif
DisableInts();
#if HUD
DispBMap(&cockpit_map);
#endif
EnableInts();
View_World();
STATS_HITS = STATS_SHOTS = 0;
InitSprites();
Init3D();
Move3D(180,10,0,0,0);
NumNukes = 0;
FreeNukes = 0;
Generate_World(WorldNum);
ScreenRow = ScreenCol = 0;
Init_Player();
Init_Horizon();
Set_HScroll();
/* Clear all the background colors */
PalBuffer[0] = 0;
PalBuffer[16] = 0;
PalBuffer[32] = 0;
PalBuffer[48] = 0;
SYS_DOCYCLE = TRUE;
SYS_FadeIn(10); /* Fade the new world in */
SYS_VCNT = 1l;
SYS_GAMEDONE = FALSE;
/* Init the scaler for a new world */
PDQS_New();
while (!SYS_GAMEDONE) {
#if DEBUG
ScreenCol = 0;
ScreenRow = 2;
#if 0
Debug(" TILT %4d Z %4d THR %4d",
Player.Tilt, FRAC2INT (Player.Z), FRAC2INT (Player.Thrust));
/* 60/(SYS_VCNT-SYS_OLDVCNT)); */
#else
#if 1
{
WORD fps;
fps = Compute_FPS();
#if 0
Debug(" FPS=%2d.%1d Vis=%2d Clip=%2d rot=%3d",
fps / 10,
fps % 10,
Num_Visable_Objs,Num_Clipped, Player.Rotation);
#else
Debug(" FPS=%2d.%1d ",
fps / 10,
fps % 10);
#endif
}
#else
{
extern UWORD HeadX;
extern WORD HeadY;
Debug(" FPS=%2ld HeadX=%3d HeadY=%3d",
60/(SYS_VCNT-SYS_OLDVCNT),HeadX,HeadY);
}
#endif
#endif
#endif
#if FPS
{
WORD fps;
ScreenCol = 2;
ScreenRow = 2;
fps = Compute_FPS();
if (fps < 999)
UI_PrintString("%2d.%1d",fps / 10,fps % 10);
}
#endif
SYS_OLDVCNT = SYS_VCNT;
/* Move Camera */
Update_Player();
#if 0
/* Does user want SYS_FLICKER? */
if (SYS_JoyTable[JOY_START] && !oldjoy)
SYS_FLICKER = ~SYS_FLICKER;
#else
/* Does user want to pause? */
if (SYS_JoyTable[JOY_START] && !oldjoy) {
WORD Fuel_Save;
if (UI_DemoMode) {
UI_DemoMode = FALSE;
return;
}
Fuel_Save = Player_Fuel;
gemspauseall();
ScreenCol = 12;
ScreenRow = 12;
UI_PrintString("PAUSE");
if (SYS_JoyTable[JOY_START])
while (SYS_JoyTable[JOY_START])
;
while (!SYS_JoyTable[JOY_START])
;
ScreenCol = 12;
ScreenRow = 12;
UI_PrintString(" ");
gemsresumeall();
Player_Fuel = Fuel_Save;
}
#endif
oldjoy = SYS_JoyTable[JOY_START]; /* For debouncing */
if (SYS_FLICKER) {
/* Show left sprites */
SEND_LEFT = TRUE;
Move3D(Player.Rotation,Player.Tilt,Player.X,Player.Y,Player.Z);
View3DObjects(TRUE);
Set_HScroll();
Update_Cockpit();
Send3DObjects(TRUE);
/* Show right sprites */
SEND_LEFT = FALSE;
SetSendRight();
View3DObjects(FALSE);
Update_Cockpit();
Send3DObjects(FALSE);
} else {
/* Move without SYS_FLICKER */
SEND_LEFT = TRUE;
Move3D(Player.Rotation,Player.Tilt,Player.X,Player.Y,Player.Z);
View3DObjects(TRUE);
Set_HScroll();
Update_Cockpit();
Send3DObjects(TRUE);
}
/* Do a maximum of 15 frames per second! */
while (SYS_VCNT-SYS_OLDVCNT < 4)
SYS_WaitFrame();
SetupDMA(); /* Prepare to DMA sprite data */
Game_Frame++;
}
if (!UI_DemoMode) {
if (WorldNum == 31 && Player.Won) {
UI_GameWon();
return;
} else
UI_NextLevel();
if (Player_Fuel && Player.Damage)
UI_DroneEnhance();
else
return;
} else {
return;
}
}
}
void
main(void)
{
UWORD *uwptr;
extern void Do_UI();
extern WORD SYS_SAFEDMA;
extern UWORD RLEScaleCode;
SYS_SAFEDMA = 0xFFFF;
/* Init scale code */
uwptr = &RLEScaleCode;
uwptr[0] = 0x207C;
uwptr[3] = 0x227C;
uwptr[6] = 0x4EF9;
/* Set up default focus distances for Kevin Standard Eyes */
#if 0
SYS_MINFDIST = 43;
SYS_MAXFDIST = 66;
#else
SYS_MINFDIST = 0;
SYS_MAXFDIST = 23;
#endif
SYS_DISTDIV = 400 / (SYS_MAXFDIST - SYS_MINFDIST);
SYS_AVGFDIST = ((SYS_MAXFDIST - SYS_MINFDIST) / 2) + SYS_MINFDIST;
SYS_NEWXA = SYS_NEWXB = SYS_SCROLLX = 0;
Do_UI();
restart:
Main_UI();
MainGameLoop();
goto restart;
}