// Starfield by PEK '2000

#include <dos.h>
#include <conio.h>
#include <stdlib.h>


#define N_STARS 700


void setmode(int mode);
void setpal(void);


void main(void)
{
	int i, x_ch=0, y_ch=0;
	unsigned char *screen = (unsigned char *)MK_FP(0xA000, 0);
	char key = 0;

	int *x = new int[N_STARS];
	int *y = new int[N_STARS];
	int *z = new int[N_STARS];
	int *sx = new int[N_STARS];
	int *sy = new int[N_STARS];
	int *sp = new int[N_STARS];
	int *x_par = new int[N_STARS];
	int *y_par = new int[N_STARS];

	setmode(0x13);
	setpal();

	randomize();

	for(i = 0; i < N_STARS; i++)
	{
		x[i] = random(321)-160;
		y[i] = random(201)-100;
		z[i] = 256;
		x_par[i] = 160;
		y_par[i] = 100;
		sp[i] = 0;
	}

	while(key != 0x1B)
	{
		while(!kbhit())
		{
			for(i = 0; i < N_STARS; i++)
			{
				x_par[i] += x_ch;
				y_par[i] += y_ch;

				sx[i] = x[i]*256/z[i] + x_par[i];
				sy[i] = y[i]*256/z[i] + y_par[i];

				if(z[i] <= 1 || sx[i] > 319 || sx[i] < 0 || sy[i] > 199 || sy[i] < 0)
				{
					x[i] = random(321)-160;
					y[i] = random(201)-100;
					z[i] = 256;
					x_par[i] = 160;
					y_par[i] = 100;

					sx[i] = x[i]*256/z[i] + x_par[i];
					sy[i] = y[i]*256/z[i] + y_par[i];
				}

				screen[sp[i]] = 0;
				sp[i] = sx[i] + sy[i]*320;
				screen[sp[i]] = (256-z[i])/4;

				z[i]--;
			}

			delay(5);

			x_ch = 0;
			y_ch = 0;
		}

		key = getch();

		if(key == 0)
		{
			key = getch();

			if(key == 0x4B)
				x_ch = 2;
			if(key == 0x4D)
				x_ch = -2;
			if(key == 0x48)
				y_ch = -2;
			if(key == 0x50)
				y_ch = 2;
		}
	}

	delete[] x;
	delete[] y;
	delete[] z;
	delete[] sx;
	delete[] sy;
	delete[] sp;
	delete[] x_par;
	delete[] y_par;

	setmode(0x03);
}


void setmode(int mode)
{
	asm	mov ax,mode
	asm	int 10h
}


void setpal(void)
{
	int c;

	outp(0x3C8, 0);

	for(c = 0; c<64; c++)
	{
		outp(0x3C9, c);
		outp(0x3C9, c);
		outp(0x3C9, c);
	}
}
