Category : A Collection of Games for DOS and Windows
Archive   : SPCWAR.ZIP
Filename : SWRMAIN.C

 
Output of file : SWRMAIN.C contained in archive : SPCWAR.ZIP

/************************************
SPACEWAR -- by Dan Moore
compile under TC2.0 ... BC++3.1
*************************************/

#include
#include
#include
#include
#include

#define _F1 (0x3b)
#define _F2 (0x3c)
#define _F3 (0x3d)
#define _F4 (0x3e)
#define _F5 (0x3f)

#include "swr.h"
#define MAX_HIT 25
#define MAX_VEL 7000

extern char K_mtx[128];
extern void kb_setup(),kb_retn();

extern int Tsin[MAX_ANG+1];
extern int Tcos[MAX_ANG+1];

int p1d[9][2] =
{ {144,0}, {48,0}, {-64,0}, {-16,80}, {-16,-80},
{16,80}, {-160,80}, {16,-80}, {-160,-80}
};
int p2d[5][2] =
{{-192,96},{240,0},{-192,-96},{-144,-48},{-144,48}};
/*****
int p1d[9][2] =
{{9,0},{3,0},{-4,0},{-1,5},{-1,-5},
{1,5},{-10,5},{1,-5},{-10,-5}};
int p2d[5][2] =
{{-12,6},{15,0},{-12,-6},{-9,-3},{-9,3}};
*****/

int p1r[9][2],p2r[5][2];
int p1s[9][2],p2s[5][2];
int p1L=9, p2L=5;

enum SW {off, on} ;
typedef enum SW SW;

long int d_lay=10000;
int gv_on=0;

struct vector {
int x;
int y;
} ;

typedef struct vector vector;

struct ship {
int x,y;
int xv,yv;
int rot,rotv;
int thrust;
int hit;
int hctr,pm_cnt;
int toast;
int fdel;
}
ship1,ship2;

typedef struct ship ship;

struct projc {
int count;
int x,y;
int xn,yn;
int xv,yv;
}
pj[10];

graph G;

typedef struct projc projc;

static int changes=1;
static int visible=0;
static int newvisi=1;
int exitloop,gravtogl;

int c_break(void)
{
changes=1;
return (1);
/*** closegraph();
kb_retn();
return 0;***/

}


help()
{

static int hidetogl=0;
static char s[90];
int oldvis=visible;

if (hidetogl) visible=!visible;
hidetogl=0;

if (changes) visible=1;

if (changes || (visible && !oldvis)) {

setfillstyle(SOLID_FILL,G.yellow);
bar(0,0,400,25);
setcolor (G.black);
sprintf(s,"F1 - [ delay = %05ld ] + F2 ",d_lay);
outtextxy(5,2,s);
sprintf(s,"F3 Gravity (%s) / F4 Hide / F5 Exit", gv_on?"ON":"OFF");
outtextxy(5,12,s);
}

changes=0;

if (!visible && oldvis)
{
setfillstyle(SOLID_FILL,G.black);
bar(0,0,400,25);
}
if (K_mtx[_F2]) { d_lay = d_lay *97/100+1; changes=1; }
if (K_mtx[_F1]) { d_lay = d_lay *100/97+1; changes=1; }
if (K_mtx[_F5]) exitloop=1; else exitloop=0;

gravtogl=0;


if (kbhit()) {
int j,i=getch();
if (kbhit()) j=getch();
if (i==0 && j==62) /*F4*/ {
hidetogl=1;
}
if (i==0 && j==61) {
gravtogl=1;
changes=1;
}
}
}

void setshipcolor(ship *s,SW sw)
{
int *hctr;
setcolor(G.black);
if (s->pm_cnt) {
if (s->pm_cnt<40) {
if (sw==on) setcolor(G.red);
if ((s->pm_cnt==3&&sw==on)||(s->pm_cnt==40&&sw==off))
setlinestyle(SOLID_LINE,0,THICK_WIDTH);
}
}
else {
hctr=&(s->hctr);
if (*hctr) {
if (sw==off) {
(*hctr)--;
setcolor(G.black);
}
else setcolor(G.red);
}
else {
if (sw) setcolor(G.green); else setcolor(G.black);
}
}
}

void wrap(int *x, int *y)
{
if (*y>=7680) *y-=7680;
if (*y<0) *y+= 7680;
if (*x<0) *x+=10240;
if (*x>=10240) *x-=10240;
}

void limitvel(int *x, int *y)
{
if (*x>MAX_VEL) *x=MAX_VEL;
if (*y>MAX_VEL) *y=MAX_VEL;
if (*x<-MAX_VEL) *x=-MAX_VEL;
if (*y<-MAX_VEL) *y=-MAX_VEL;
}

void init_players(void)
{
int i;
ship1.hctr=ship2.hctr=ship1.fdel=ship2.fdel=0;
ship1.pm_cnt = ship2.pm_cnt = 0;
ship1.hit = ship2.hit = ship1.toast = ship2.toast = 0;
ship1.thrust = ship2.thrust = 0;
ship1.x=0; ship1.y=120<<4;
ship2.x=640<<4; ship2.y=360<<4;
ship1.xv=100; ship2.xv=-100;
ship1.yv = ship2.yv = 0;
for (i=0; i<10; i++) pj[i].count=0;
ship1.rot = ship1.rotv = ship2.rotv = 0;
ship2.rot = MAX_ANG >>1;
ship1.thrust=ship2.thrust=0;
rotate (&p1d,&p1r,p1L,ship1.x,ship1.y,ship1.rot);
rotate (&p2d,&p2r,p2L,ship2.x,ship2.y,ship2.rot);
}

void DrawSun (void)
{
setcolor(G.yellow);
ellipse(Xscale(320*16),Yscale(240*16),0,360,Xscale(7*16),Yscale(7*16));
/* circle(320,240,7); */
}

vector grav(int x, int y)
{
int magn;
struct uvector v;
struct vector w;
int angle;
int dx=(320*16)-x;
int dy=(240*16)-y;
v=magphz(dx,dy);
magn=invsqr(v.mag*3,0xffff);
angle=mul(MAX_ANG,v.phz>>1);
w.x = mul(magn,Tcos[angle]);
w.y = mul(magn,Tsin[angle]);
return w;
}

void put_projc(struct projc *a,int color)
{
int x=Xscale(a->x);
int y=Yscale(a->y);
putpixel(x,y-1,color);
putpixel(x-1,y,color);
putpixel(x,y+1,color);
putpixel(x+1,y,color);
putpixel(x,y,color);
}

/* show projectiles, detect a hit */

void show_projs(int color)
{
int i;
ship *Sptr;
uvector uv;
projc *PJ=&(pj[0]);
for (i=0; i<10; i++,PJ++) {
if (!PJ->count) continue;
put_projc(PJ,color);
if (color==G.black) {
PJ->count--;
if (i<5) Sptr=&ship2; else Sptr=&ship1;
uv=magphz(PJ->x - Sptr->x, PJ->y - Sptr->y);
if (uv.mag < 200) { Sptr->hctr=10; Sptr->hit++; PJ->count=0;}
}
}
}

void new_proj(int which) /* which = 0 (ship1) or 5 (ship2) */
{
int i,j;
int Sne,Csn;
projc *PJ;
ship *Sptr;
int *p;

/* firing delay between projectiles */

if (which) p=&(ship2.fdel); else p=&(ship1.fdel);
if (*p) { (*p)--; return; }

PJ=&pj[which];
for (j=0; (PJ->count > 0) && (j < 5); PJ++,j++) ;
if (j>=5) return;

PJ->count=70;
if (which) Sptr=&ship2; else Sptr=&ship1;
PJ->xn = Sptr->x + mul(256,Csn=Tcos[Sptr->rot]);
PJ->yn = Sptr->y + mul(256,Sne=Tsin[Sptr->rot]);
wrap(&(PJ->xn),&(PJ->yn));
PJ->x = PJ->xn;
PJ->y = PJ->yn;
PJ->xv=mul(2000,Csn)+(Sptr->xv>>1);
PJ->yv=mul(2000,Sne)+(Sptr->yv>>1);

*p = 3; /* delay to next fire */

}

void proj_motn(void)
{
vector gvect;
projc *PJ=&(pj[0]);
int i;

for (i=0; i<10; i++,PJ++) {
if (!PJ->count) continue;
PJ->xn = PJ->x + (PJ->xv >> 5);
PJ->yn = PJ->y + (PJ->yv >> 5);
wrap(&PJ->xn,&PJ->yn);
}
}

void proj_mvps(void)
{
int i;
projc *PJ=&(pj[0]);
for (i=0; i<10; i++, PJ++) {
PJ->x = PJ->xn;
PJ->y = PJ->yn;
}
}

show_damage()
{
int a1,a2,b;
b=Xscale(4000);
a1=Xscale(160*ship1.hit);
a2=Xscale(160*ship2.hit);

setlinestyle(DOTTED_LINE,0,NORM_WIDTH);
setcolor(G.yellow);
line(b,G.my,a1,G.my);
line(G.mx-b,G.my,G.mx-a2,G.my);
setlinestyle(SOLID_LINE,0,NORM_WIDTH);
setcolor(G.red);
line(0,G.my,a1,G.my);
line(G.mx,G.my,G.mx-a2,G.my);
}

/******************************************************/



main()
{
int gmode;
int i;
long int li;
char YNchar;
unsigned c;
vector gvect;
uvector check;
int collide;

ctrlbrk(c_break);
Play_Again:
gv_on=0;
clrscr();

gotoxy(0,0);
printf("\n\n\n\t\tS P A C E W A R by Dan W. Moore\n");
printf("\n\n\n\t\t-----------------------------------------\n");

printf(" During the Game press: \n\n");
printf(", to slow down/speed up game or stop annoying blinking\n");
printf(" to toggle gravity on/off \n");
printf(" to exit to DOS \n\n");
printf(" \n");
printf(" Play | ROTATE THRUST ROTATE FIRE \n");
printf(" Keys | RIGHT LEFT \n");
printf(" -----------+--------------------------------- \n");
printf(" Lplayer(1) | Z X C LeftSHIFT \n");
printf(" Rplayer(2) | , . / ENTER \n");
printf(" -----------+--------------------------------- \n");

printf(" \n\n ");

getch();

Init_Graph();

gmode = getgraphmode();

cleardevice();


collide=0;

init_tables();

init_players();

kb_setup();


while (1) {
help();
if (d_lay<100) d_lay=100;
if (d_lay>100001L) d_lay=100001;
if (exitloop) break;
if (gravtogl) gv_on=!gv_on;

show_damage();
setshipcolor(&ship1,on);
draw_p1(p1r);
setlinestyle(SOLID_LINE,0,NORM_WIDTH);

setshipcolor(&ship2,on);
draw_p2(p2r);
setlinestyle(SOLID_LINE,0,NORM_WIDTH);

DrawSun();

show_projs(G.pcolor);

memcpy (&p1s,&p1r,sizeof(p1r));
memcpy (&p2s,&p2r,sizeof(p2r));

/*update velocities */

gvect=grav(ship1.x,ship1.y);
if (!gv_on) gvect.x=gvect.y=0;
ship1.xv += Rshift(gvect.x,2);
ship1.yv += Rshift(gvect.y,2);

if (ship1.thrust) {
ship1.xv += mul(200,Tcos[ship1.rot]);
ship1.yv += mul(200,Tsin[ship1.rot]);
}

limitvel(&ship1.xv,&ship1.yv);

gvect=grav(ship2.x,ship2.y);
if (!gv_on) gvect.x=gvect.y=0;
ship2.xv += Rshift(gvect.x,2);
ship2.yv += Rshift(gvect.y,2);

if (ship2.thrust) {
ship2.xv += mul(200,Tcos[ship2.rot]);
ship2.yv += mul(200,Tsin[ship2.rot]);
}

limitvel(&ship2.xv,&ship2.yv);

if (!ship1.pm_cnt) {
ship1.x += Rshift(ship1.xv,6);
ship1.y += Rshift(ship1.yv,6);
wrap(&ship1.x,&ship1.y);
ship1.rot += ship1.rotv;
if (ship1.rot<0) ship1.rot+=MAX_ANG;
if (ship1.rot>=MAX_ANG) ship1.rot-=MAX_ANG;
rotate (&p1d,&p1r,p1L,ship1.x,ship1.y,ship1.rot);

ship1.rotv=0;
if (K_mtx[0x2C]) ship1.rotv = -1;
else if (K_mtx[0x2E]) ship1.rotv = +1;
if (K_mtx[0x2a]) new_proj(0);
ship1.thrust=0;
if (K_mtx[0x2d]) ship1.thrust=2;
if (ship1.hit>=MAX_HIT) ship1.pm_cnt=1;
}

if (!ship2.pm_cnt) {
ship2.x += Rshift(ship2.xv,6);
ship2.y += Rshift(ship2.yv,6);
wrap(&ship2.x,&ship2.y);
ship2.rot += ship2.rotv;
if (ship2.rot<0) ship2.rot+=MAX_ANG;
if (ship2.rot>=MAX_ANG) ship2.rot-=MAX_ANG;
rotate (&p2d,&p2r,p2L,ship2.x,ship2.y,ship2.rot);

ship2.rotv=0;
if (K_mtx[0x33]) ship2.rotv = -1;
else if (K_mtx[0x35]) ship2.rotv = +1;
if (K_mtx[0x1c]) new_proj(5);
ship2.thrust=0;
if (K_mtx[0x34]) ship2.thrust=2;
if (ship2.hit>=MAX_HIT) ship2.pm_cnt=1;
}
/*
if (K_mtx[0x3e]) goto cont_debug;
if (K_mtx[0x3f]) { gv_on=!gv_on; while(K_mtx[0x3f]) ; delay(50);}
*/
proj_motn();

/****
if (K_mtx[0x3b]) {
d_lay=d_lay*11L/10L;
}

if (K_mtx[0x3c]) {
d_lay=d_lay*10L/11L;
}
****/

if (ship1.pm_cnt) ship1.pm_cnt++;
if (ship2.pm_cnt) ship2.pm_cnt++;

if ((ship1.pm_cnt>45)&&(ship2.pm_cnt==0)) break;
if ((ship2.pm_cnt>45)&&(ship1.pm_cnt==0)) break;
if ((ship2.pm_cnt>=45)&&(ship1.pm_cnt>=45)) break;

/* check for collisions */
check=magphz(ship1.x-320*16,ship1.y-240*16);
if (!ship1.toast&&(check.mag<250))
{ ship1.toast=1; ship1.hit=MAX_HIT; ship1.pm_cnt=1;}

check=magphz(ship2.x-320*16,ship2.y-240*16);
if (!ship2.toast&&(check.mag<250))
{ ship2.toast=1; ship2.hit=MAX_HIT; ship2.pm_cnt=1; }

check=magphz(ship1.x-ship2.x,ship1.y-ship2.y);
if (!collide&&(check.mag<250)) {
collide=1;
ship2.hit=ship1.hit=MAX_HIT;
ship1.pm_cnt=ship2.pm_cnt=1;
}

/* delay, then erase screen figures */

for (li=0; li
show_projs(G.black);
proj_mvps();

setshipcolor(&ship1,off);
draw_p1(p1s);
setlinestyle(SOLID_LINE,0,NORM_WIDTH);

setshipcolor(&ship2,off);
draw_p2(p2s);
setlinestyle(SOLID_LINE,0,NORM_WIDTH);
}
cont_debug: ;
kb_retn();
closegraph();
restorecrtmode();
clrscr();
if (ship1.toast) printf("Left Player burned up!!\n");
if (ship2.toast) printf("Right Player burned up!!\n");
if (collide) printf("Ships collided!\n");

printf("Left Player [1] had %3d damage points\n",ship1.hit);
printf("Right Player [2] had %3d damage points\n",ship2.hit);
printf("\nPlay again (Y/N): ");

GetKey:
switch(getch()) {
case 'Y': case 'y':{
restorecrtmode();
goto Play_Again;
}
case 'N': case 'n': break;
default: goto GetKey;
}

} /* end main */


  3 Responses to “Category : A Collection of Games for DOS and Windows
Archive   : SPCWAR.ZIP
Filename : SWRMAIN.C

  1. Very nice! Thank you for this wonderful archive. I wonder why I found it only now. Long live the BBS file archives!

  2. This is so awesome! 😀 I’d be cool if you could download an entire archive of this at once, though.

  3. But one thing that puzzles me is the “mtswslnkmcjklsdlsbdmMICROSOFT” string. There is an article about it here. It is definitely worth a read: http://www.os2museum.com/wp/mtswslnk/