#include "game.h"
#include "string.h"
#include "water.h"
#include <string.h>

#define MAXX 300
#define MAXY 300

void write_file();

int main()
{
	init(); 

	logo_screen(true);	
	play_game();
	shutdown();
	
	return 0;
}
END_OF_MAIN();

// Used back when I had a remote file for online high scores
void write_file()
{
	int i;
	PACKFILE *f = pack_fopen("ohsc", "w");
	float version = fVERSION;
	char cTemp[CHAR_SIZE];
	int iScore;
	float Version = fVERSION;
	int iLevel = 1;

	// Version is only for online files...
	pack_fwrite(&version, sizeof(float), f);

	if(f)
	{
	
		for(i = 0; i < 100; i++)
		{
			iScore = rand() % 20000;
			if(rand() % 100 > 75)
				strcpy(cTemp, "Steven");
			else
				strcpy(cTemp, "NPC");
	
			pack_fwrite(&cTemp, sizeof(char) * CHAR_SIZE, f);
			pack_fwrite(&iScore, sizeof(int), f);
			pack_fwrite(&Version, sizeof(float), f);
			pack_fwrite(&iLevel, sizeof(int), f);
				
		}
		pack_fclose(f);
	}

}















//  *********************************
//  This was the original code used to test the water effects.  It's pretty sloppy thoughs...
//  *********************************

/*WATER bWater("allegro.pcx", "vortec.pcx");
	bWater.Setup(300, 300, 8, 1, 80);
	
	set_trans_blender(0,0,0,0);
	bWater.mixture(255);

	while(!key[KEY_ESC])
	{
		while(system_time > 0)
		{
			ticks++;
			bWater.Update();
			if(ticks % 365 == 0)
				bWater.Drop(rand() % 300, rand() % 300, rand() % 5000 + 5000, 25, 0.95, false, true, 30);
				//bWater.Drop(100, 100, rand() % 5000 + 5000, 25, 0.95, true, true, 10);
			
			system_time--;
		}
	
		draw_ticks++;
		bWater.draw(buffer);
		draw_sprite(screen, buffer, 0, 0);
	}
//*/


	/*BITMAP *bBack;
	BITMAP *bText, *bTemp, *bTemp2;
	BITMAP *bTransfer;
	BITMAP *bWater;
	BITMAP *bImage;
	init();
	//short ***WaveMap;
	short WaveMap1[MAXX][MAXY];
	//short **WaveMap1;
	short WaveMap2[MAXX][MAXY];
	//short **WaveMap2;
	int ImageMap[MAXX][MAXY];
	int dispLut[512];
	int CT, NW, n, tWave;
	int xDiff, yDiff;
	float xAngle, xRefraction;
	float yAngle, yRefraction;
	int iRefrac = 0;
	int x, y, xDisplace, yDisplace, newcolor, nx, ny, color_shift;

	int spotx, spoty = 0;
	int wTransparency;
	
	for (int c=-256; c<256; c++) {
		double d = c/4.0;
		dispLut[c+256] = (unsigned short)((int)(tan(asin(sin(atan(d))/2.0))*d));
	}
		
	

	
	bTemp = load_bitmap_check("allegro.pcx", NULL);
	bTemp2 = load_bitmap_check("vortec.pcx", NULL);
	bBack = create_bitmap_check(MAXX, MAXY);
	bWater = create_bitmap_check(bBack->w, bBack->h);
	stretch_sprite(bBack, bTemp, 0, 0, bBack->w, bBack->h);
	stretch_sprite(bWater, bTemp2, 0, 0, bWater->w, bWater->h);
	//rectfill(bWater, 0, 0, bWater->w, bWater->h, rgb(0,0, 100));
	wTransparency = 125;
	set_trans_blender(0,0,0, wTransparency);
	draw_trans_sprite(bBack, bWater, 0, 0);

	
	for(x = 0; x < MAXX; x++)
	{
		for(y = 0; y < MAXY; y++)
		{
			ImageMap[x][y] = getpixel(bBack, x, y);
			WaveMap1[x][y] = 0;
			WaveMap2[x][y] = WaveMap1[x][y];
		}
	}
	//rectfill(bBack, 0, 0, bBack->w, bBack->h, BLUE);
	bTransfer = create_bitmap_check(buffer->w, buffer->h);
	clear(bTransfer);
	
	
	bText = create_bitmap_check(buffer->w, buffer->h);
	
	bImage = create_bitmap_check(bBack->w, bBack->h);
	clear(bImage);
	draw_sprite(bImage, bBack, 0, 0);
	
	CT = 0;
	NW = 1;

	draw_ticks = 0;
	float wsize = 5000.0;
	spotx = 100;
	while(!key[KEY_ESC])
	{
		while(system_time > 0)
		{
			ticks++;
			if(ticks % 200 == 0 && wTransparency < 0)
			{
				wTransparency += 10;
				
				stretch_sprite(bBack, bTemp, 0, 0, bBack->w, bBack->h);
				//stretch_sprite(bWater, bTemp2, 0, 0, bWater->w, bWater->h);
				//rectfill(bWater, 0, 0, bWater->w, bWater->h, rgb(0,0, 100));
				
				set_trans_blender(0,0,0, wTransparency);
				//draw_trans_sprite(bBack, bWater, 0, 0);
				iRefrac += 2;
				
				for(x = 0; x < MAXX; x++)
				{
					for(y = 0; y < MAXY; y++)
					{
						ImageMap[x][y] = getpixel(bBack, x, y);						
					}
				}
	
			}
			if(ticks % 30 == 0 && wsize > 2)
			{
				
			WaveMap1[spotx][spoty + 0] = wsize;
			WaveMap1[spotx][spoty + 1] = wsize;
			WaveMap1[spotx][spoty + 2] = wsize;
			WaveMap1[spotx][spoty + 3] = wsize;
			WaveMap1[spotx][spoty + 4] = wsize;
			WaveMap1[spotx][spoty + 5] = wsize;
			WaveMap1[spotx][spoty + 6] = wsize;
			WaveMap1[spotx][spoty + 7] = wsize;
			WaveMap1[spotx][spoty + 8] = wsize;
			WaveMap1[spotx][spoty + 9] = wsize;
			WaveMap1[spotx][spoty + 10] = wsize;
				wsize = wsize * 0.5;
			}
			if(ticks % 100 == 0)
			{
				spotx = rand() % MAXX;
				spoty = rand() % MAXY - 20;
				wsize = rand() % 5000 + 5000;
			}
			//WaveMap[0][int(rand() % MAXX)][int(rand() % MAXY)] = int(rand() % 1500 - rand() % 500);
			//WaveMap[0][int(rand() % MAXX)][int(rand() % MAXY)] = int(rand() % 500 - rand() % 2500);
			//WaveMap[0][int(rand() % MAXX)][int(rand() % MAXY)] = int(rand() % 1500 - rand() % 500);
			for(x = 1; x < MAXX - 1 ; x++)
			{
				for(y = 1; y < MAXY - 1; y++)
				{
					if(CT == 0)
					{
					n = (WaveMap1[x-1][y] +
						 WaveMap1[x+1][y] +
						 WaveMap1[x][y-1] +
						 WaveMap1[x][y+1]) / 2 - WaveMap2[x][y];
					//n -= (n / 64);
					n -= (n >> 8);
					WaveMap2[x][y] = n;					
					}
					else
					{
					n = (WaveMap2[x-1][y] +
						 WaveMap2[x+1][y] +
						 WaveMap2[x][y-1] +
						 WaveMap2[x][y+1]) / 2 - WaveMap1[x][y];
					//n -= (n / 64);
					n -= (n >> 8);
					WaveMap1[x][y] = n;					
					
					}
				}
			}
			tWave = CT;
			CT = NW;
			NW = tWave;
			system_time--;
		}

		draw_ticks++;
		
		
		draw_sprite(bImage, bBack, 0, 0);
		//draw_trans_sprite(bImage, bWater, 0, 0);

		for(y = 1; y < MAXY - 1; y++)
		{
			for(x = 1; x < MAXX -1 ; x++)
			{
				if(CT == 0)
				{
					xDiff = WaveMap1[x+1][y] - WaveMap1[x][y];
					yDiff = WaveMap1[x][y+1] - WaveMap1[x][y];
				}
				else
				{
					xDiff = WaveMap2[x+1][y] - WaveMap2[x][y];
					yDiff = WaveMap2[x][y+1] - WaveMap2[x][y];
				}
				
				xDiff = MID(-256, xDiff, 255);
				yDiff = MID(-256, yDiff, 255);
			    xDisplace = dispLut[xDiff+256];
			    yDisplace = dispLut[yDiff+256];

				xDisplace >>= 1;
				yDisplace >>= 1;
			
				if (xDisplace == 0 || yDisplace == 0) {
					continue;
				}

				if(xDiff < 0)
				{
					nx = MID(0, x - xDisplace + iRefrac, MAXX - 1);
					if(yDiff < 0)
						ny = MID(0, y - yDisplace + iRefrac, MAXY - 1);						
					else
						ny = MID(0, y + yDisplace + iRefrac, MAXY - 1);
					
				}
				else
				{
					nx = MID(0, x + xDisplace + iRefrac, MAXX - 1);
					if(yDiff < 0)
						ny = MID(0, y - yDisplace + iRefrac, MAXY - 1);						
					else
						ny = MID(0, y + yDisplace + iRefrac, MAXY - 1);
						
				}
				newcolor = ImageMap[nx][ny];
				color_shift = xDisplace;
				putpixel(bImage, x, y, makecol(MID(0, getr(newcolor) + color_shift, 255), MID(0, getg(newcolor) + color_shift, 255), MID(0, getb(newcolor) + color_shift, 255)));
				
			}
		}
		

		stretch_sprite(bTransfer, bImage, 0, 0, buffer->w, buffer->h);
		rectfill(bText, 0, 0, bText->w, bText->h, MASK);
		textprintf(bText, font, 100, 100, WHITE, "%d", framerate);
		//draw_sprite(buffer, bWater, 0, 0);
		draw_trans_sprite(buffer, bTransfer, 0, 0);
		draw_sprite(buffer, bText, 0, 0);
		draw_sprite(screen, buffer, 0, 0);
	}

	destroy_bitmap(bBack);
	destroy_bitmap(bText);
	destroy_bitmap(bTemp);
	destroy_bitmap(bTransfer);
	destroy_bitmap(bWater);
	destroy_bitmap(bImage);
	destroy_bitmap(bTemp2);

	shutdown();
//*/

