#include "Layer.h"



//// Layer construters
Layer::Layer()
{
	WORLD_X=100; 
	WORLD_Y=100;
 filename = "map1.txt";//defalse
masked= false;
Layergrid = create_bitmap(SCREEN_W,SCREEN_H);

int j,i,p;
for (i=0; i<WORLD_X; i++)
	{
		for (j=0; j<WORLD_Y; j++)
		{

			
			
			cells[j][i].nuberofleve=4;
			for( p = 0; p<leves; p++)
			{
				
				if(p==0)
				{
				cells[j][i].set_value(34,p);


				cells[j][i].set_tileset(0,p);

				cells[j][i].set_passable(0,p);

				cells[j][i].set_layer(1,p);
				}
				else
				{
			cells[j][i].set_value(247,p);


				cells[j][i].set_tileset(0,p);

				cells[j][i].set_passable(0,p);

				cells[j][i].set_layer(p+1,p);
				}
			}
		}
}

}




void Layer::load_layer()
{
PACKFILE *pfile;
pfile=pack_fopen(filename.c_str(), "rp");
int i,j,p;
	for (i=0; i<WORLD_X; i++)
	{
		for (j=0; j<WORLD_Y; j++)
		{

			cells[j][i].nuberofleve=pack_igetl(pfile);
			
			//cells[j][i].nuberofleve=4;
			for( p = 0; p<leves; p++)
			{
			/*	
				if(p==0)
				{
				cells[j][i].set_value(34,p);


				cells[j][i].set_tileset(0,p);

				cells[j][i].set_passable(0,p);

				cells[j][i].set_layer(1,p);
				}
				else
				{
			cells[j][i].set_value(247,p);


				cells[j][i].set_tileset(0,p);

				cells[j][i].set_passable(0,p);

				cells[j][i].set_layer(p+1,p);
				}
				*/
				cells[j][i].set_value(pack_igetl(pfile),p);


				cells[j][i].set_tileset(pack_igetl(pfile),p);

				cells[j][i].set_passable(pack_igetl(pfile),p);

				cells[j][i].set_layer(pack_igetl(pfile),p);
				
			}
		}
	}
	pack_fclose(pfile);

}


void Layer::save_layer()
{
PACKFILE *pfile;
	
	pfile=pack_fopen(filename.c_str(), "wp");

	int i,j,p;
	for (i=0; i<WORLD_X; i++)
	{
		for (j=0; j<WORLD_Y; j++)
		{
			/*
pack_iputl(0, pfile);

pack_iputl(cells[j][i].get_value(0), pfile);
pack_iputl(cells[j][i].get_tileset(0), pfile);
pack_iputl(cells[j][i].get_passable(0), pfile);

pack_iputl(0, pfile);



for( p = 1; p<leves; p++)
{
pack_iputl(0, pfile);

pack_iputl(247, pfile);
pack_iputl(0, pfile);
pack_iputl(0, pfile);

pack_iputl(p, pfile);


}

*/

			pack_iputl(cells[j][i].nuberofleve, pfile);
			for( p = 0; p<leves; p++)
			{
				pack_iputl(cells[j][i].get_value(p), pfile);
			
				pack_iputl(cells[j][i].get_tileset(p), pfile);
			
				pack_iputl(cells[j][i].get_passable(p), pfile);
				pack_iputl(cells[j][i].get_layer(p), pfile);
			}

			

		}
	}
	pack_fclose(pfile);
textprintf(screen, font, 100, 10, makecol(255, 255, 255),"Saved");
}
//Layer sets

void Layer::load_layer2()
{
	int numb;
	char trash;
		ifstream inData;

	inData.open(filename.c_str() );
	if(inData.is_open())
	{

		int i,j,p;
		inData>>WORLD_X;
		inData>>WORLD_Y;

		for (i=0; i<WORLD_X; i++)
		{
			for (j=0; j<WORLD_Y; j++)
			{
				inData>>trash;
				inData>>cells[j][i].nuberofleve;
				for( p = 0; p<leves; p++)
				{
					inData>>trash;
					inData>>numb;
					cells[j][i].set_value(numb,p);
					inData>>trash;
					inData>>numb;
					cells[j][i].set_tileset(numb,p);
					inData>>trash;
					inData>>numb;
					cells[j][i].set_passable(numb,p);
					inData>>trash;
					inData>>numb;
					cells[j][i].set_layer(numb,p);
					
				}
				inData>>trash;
			}

		}
		inData.close();
	}
	else
	{
		allegro_message("cant not find input text file map1.txt\n");
	}

}
void Layer::drawRectangle(BITMAP *bmp,int x, int y, int w,int h,int color, int style, int bgcolor)
{
	w--;
	h--;
	if (style==0)
	{
		vline(bmp,x,y,y+h,color);
		vline(bmp,x+w,y,y+h,color);
		hline(bmp,x,y,x+w,color);
		hline(bmp,x,y+h,x+w,color);
	}
	if (style==1)
	{
		rectfill(bmp,x,y,x+w,y+h,bgcolor);
		vline(bmp,x,y,y+h,color);
		vline(bmp,x+w,y,y+h,color);
		hline(bmp,x,y,x+w,color);
		hline(bmp,x,y+h,x+w,color);
	}


	// this is for our dialog. This draws a box with rounded corners.
	if (style==2)
	{									
		arc(bmp,x+10,y+10,itofix(64),itofix(128),10,255);
		arc(bmp,x+10,y+h-10,itofix(128),itofix(192),10,255);
		arc(bmp,x+w-10,y+10,itofix(0),itofix(64),10,255);
		arc(bmp,x+w-10,y+h-10,itofix(192),itofix(256),10,255);


		hline(bmp,x+10,y,x+w-10,255);
		hline(bmp,x+10,y+h,x+w-10,255);
		vline(bmp,x,y+10,y+h-10,255);
		vline(bmp,x+w,y+10,y+h-10,255);
	
		floodfill(bmp,x+w/2,y+h/2,255);

		arc(bmp,x+10,y+10,itofix(64),itofix(128),8,bgcolor);
		arc(bmp,x+10,y+h-10,itofix(128),itofix(192),8,bgcolor);
		arc(bmp,x+w-10,y+10,itofix(0),itofix(64),8,bgcolor);
		arc(bmp,x+w-10,y+h-10,itofix(192),itofix(256),8,bgcolor);


		hline(bmp,x+10,y+2,x+w-10,bgcolor);
		hline(bmp,x+10,y+h-2,x+w-10,bgcolor);
		vline(bmp,x+2,y+10,y+h-10,bgcolor);
		vline(bmp,x+w-2,y+10,y+h-10,bgcolor);

		floodfill(bmp,x+w/2,y+h/2,bgcolor);
	}
}















void Layer::set_masked(bool newmasked)
{
masked = newmasked;
}
void Layer::set_filename(string newname)
{
filename = newname;
}

	//Layer gets
BITMAP * Layer::get_layerBitmap()
{
return Layergrid;
}
bool Layer::get_masked()
{
	return masked;
}
string Layer::get_filename()
{
return filename;
}

void Layer::update_layer(View view,TileAnimeList tiles_a_list,int lay,DATAFILE *data)
{

	int i,j,x,y;
	short p,t;
		
	for (i=-1; i<view.get_GRID_WIDTH()+1; i++)
	{
		
		for (j=-1; j<view.get_GRID_HEIGHT()+1; j++)
		{
			
			if(cells[j+view.get_screen_pos_y()][i+view.get_screen_pos_x()].get_layer(lay-1)==lay)
			{
			//cells[j+view.get_screen_pos_y()][i+view.get_screen_pos_x()].speed = 100;
			cells[j+view.get_screen_pos_y()][i+view.get_screen_pos_x()].animate(tiles_a_list);

			p=cells[j+view.get_screen_pos_y()][i+view.get_screen_pos_x()].get_value(lay-1);
			t=cells[j+view.get_screen_pos_y()][i+view.get_screen_pos_x()].get_tileset(lay-1);

			x=32*(p/32); 
			y=32*(p%32);

			/*
			our tile bitmap is 8x32 tiles
			if p=155 then x coordinate of our tile is 128,
						  y coordinate of our tile is 864;
			Also because we are scrolling from 0..16 pixels int the x
			and y direction, we need to draw an extra tile in each 
			direction  i=-1 to GRID_WIDTH+1
			*/
		//	p=getpixel(world,i+view.screen_pos_x,j+view.screen_pos_y);
		//	if (p>0)
		//	{
			//	stretch_blit((BITMAP*)data[TILES1+t].dat, Layergrid, x,y, 32, 32, i*32-view.get_scroll_x(), j*32-view.get_scroll_y(), 32, 32);
				if(p!=247)
				{
				masked_blit((BITMAP*)data[TILES1+t].dat,Layergrid,x,y,i*32-view.get_scroll_x(),j*32-view.get_scroll_y(),32,32);
			
				}
				//	if (p==2)
			//	{
					// draw the tile then darken it fog of war
					//draw_sprite(grid,(BITMAP*)data[GRID].dat,i*32-scroll_x,j*32-scroll_y);
			//	}
		//	}
		//	else
		//	{
				// draw a black square
			//	rectfill(Layergrid,i*32-view.scroll_x,j*32-view.scroll_y,i*32-view.scroll_x+31,j*32-view.scroll_y+31,0);
			//}

			}
		}
	}

//if (play_2.walking)
//	{
//		draw_sprite(grid,(BITMAP*)data[XMARK].dat,pointx-screen_pos_x*32-scroll_x-10,pointy-screen_pos_y*32-scroll_y-7);
//	}
 
	 	
}
void Layer::draw_layer(View view)
{
	if(masked == false)
	{
	blit(Layergrid,view.get_buffer(),4,4,4,4,view.get_GRID_WIDTH()*TILESIZE-8,view.get_GRID_HEIGHT()*TILESIZE-8);
	}
	else
	{
	masked_blit(Layergrid,view.get_buffer(),4,4,4,4,view.get_GRID_WIDTH()*TILESIZE-8,view.get_GRID_HEIGHT()*TILESIZE-8);
	}
}
void Layer::draw_layer2(View view,BITMAP*BUF)
{
	if(masked == false)
	{
	blit(Layergrid,BUF,4,4,4,4,view.get_GRID_WIDTH()*TILESIZE-8,view.get_GRID_HEIGHT()*TILESIZE-8);
	}
	else
	{
	masked_blit(Layergrid,BUF,4,4,4,4,view.get_GRID_WIDTH()*TILESIZE-8,view.get_GRID_HEIGHT()*TILESIZE-8);
	}
}


////////////////////////////////////////////////////////////
void Layer::edit(View view,TileAnimeList animations,DATAFILE *data)
{




	
int lly=0;
int i,j,x,y,sy,sx,x1,x2,y1,y2,cell,tile,set;
	short p,t,mt,l;

	BITMAP *buf;
	buf=create_bitmap(SCREEN_W,SCREEN_H); // double buffering

	set=0;
	mt=0; // which tileset the mouse is using
	cell=0; // which tile the mouse is using
	tile=0; // which tileset is being displayed
	sx=view.get_screen_pos_x(); //location of grid
	sy=view.get_screen_pos_y();

load_layer2();



/////////////////////////////////////////////////////
while (!key[KEY_Q])////////////////////////////////////////////loop
	{
/*
clear_bitmap(view.get_buffer());
clear_bitmap(buf);
for (int j2=0; j2<layer_num-1; j2++)
{
	//	other_layers[j2].draw_layer(view);
		//other_layers[j2].update_layer(view, animations);
	//	other_layers[j2].draw_layer2(view,buf);
}
*/

if (key[KEY_F1]) 
			{
				lly=0;
			}
			if (key[KEY_F2]) 
			{
				lly=1;
			}
			if (key[KEY_F3]) 
			{
				lly=2;
			}
			if (key[KEY_F4]) 
			{
				lly=3;
			}
for (i=0; i<view.get_GRID_WIDTH(); i++)
		{
			for (j=0; j<view.get_GRID_HEIGHT(); j++)
			{
				p=cells[j+sy][i+sx].get_value(lly);
				l=cells[j+sy][i+sx].get_passable(lly);
				t=cells[j+sy][i+sx].get_tileset(lly);
				x=32*(p/32); 
				y=32*(p%32);
				blit((BITMAP*)data[TILES1+t].dat,Layergrid,x,y,i*32,j*32,32,32);
				
				x1=i*32;
				x2=x1+31;
				y1=j*32;
				y2=y1+31;

				// draw a dark grey rectangle around everytile
				drawRectangle(Layergrid,x1,y1,32,32,0,0,162);

				if (l==1)
				{
					//put an x through each tile that isn't passable
					line(Layergrid,x1,y1,x2,y2,255);
					//line(grid,x1,y2,x2,y1,255);
				}
			}
		}
		blit(Layergrid,buf,0,0,0,0,view.get_GRID_WIDTH()*TILESIZE,view.get_GRID_HEIGHT()*TILESIZE);

		if (mouse_x<view.get_GRID_WIDTH()*TILESIZE)
			blit((BITMAP*)data[TILES1+mt].dat,buf,32*(cell/32),32*(cell%32),32*(mouse_x/32),32*(mouse_y/32),32,32);

		stretch_blit((BITMAP*)data[TILES1+tile].dat,buf,0,0,256,1024,view.get_GRID_WIDTH()*TILESIZE,0,SCREEN_W-view.get_GRID_WIDTH()*TILESIZE,SCREEN_H);
		
		if (mouse_x<view.get_GRID_WIDTH()*TILESIZE)
		{
			x1=TILESIZE*(mouse_x/TILESIZE);
			y1=TILESIZE*(mouse_y/TILESIZE);
			x2=x1+TILESIZE-1;
			y2=y1+TILESIZE-1;
			
			drawRectangle(buf,x1,y1,32,32,255,0,0);

			textprintf_centre(buf, font, 50, 10, makecol(255, 255, 255),"layer:  %d",cells[y1/TILESIZE+sy][x1/TILESIZE+sx].get_layer(lly));
			textprintf_centre(buf, font, 50, 20, makecol(255, 255, 255),"Value:  %d", cells[y1/TILESIZE+sy][x1/TILESIZE+sx].get_value(lly));
			textprintf_centre(buf, font, 50, 30, makecol(255, 255, 255),"Tileset:  %d", cells[y1/TILESIZE+sy][x1/TILESIZE+sx].get_tileset(lly));

			textprintf_centre(buf, font, 50, 40, makecol(255, 255, 255),"Passable:  %d", cells[y1/TILESIZE+sy][x1/TILESIZE+sx].get_passable(lly));
			textprintf_centre(buf, font, 50, 50, makecol(255, 255, 255),"Animated:  %d", cells[y1/TILESIZE+sy][x1/TILESIZE+sx].animated[lly]);
			textprintf_centre(buf, font, 50, 60, makecol(255, 255, 255),"Speed:  %d", cells[y1/TILESIZE+sy][x1/TILESIZE+sx].speed[lly]);
			textprintf_centre(buf, font, 50, 70, makecol(255, 255, 255),"Leve:  %d", lly);
			if (key[KEY_C] || ((mouse_b & 1) && set==1)) cells[y1/TILESIZE+sy][x1/TILESIZE+sx].set_passable(1,lly);
			if (key[KEY_V] || ((mouse_b & 1) && set==2)) cells[y1/TILESIZE+sy][x1/TILESIZE+sx].set_passable(0,lly);
			if (key[KEY_Z] ) cells[y1/TILESIZE+sy][x1/TILESIZE+sx].set_layer(1,lly);
			if (key[KEY_X] ) cells[y1/TILESIZE+sy][x1/TILESIZE+sx].set_layer(2,lly);

			if (mouse_b & 1) 
			{
				cells[y1/TILESIZE+sy][x1/TILESIZE+sx].set_value(cell,lly);
				cells[y1/TILESIZE+sy][x1/TILESIZE+sx].set_tileset(mt,lly);

			}
		}
		else
		{
			x2=mouse_x-view.get_GRID_WIDTH()*TILESIZE;
			x1=(view.get_GRID_WIDTH()*TILESIZE)+20*(x2/20);
			y1=15*(mouse_y/15);
			x2=x1+19;
			y2=y1+14;

			drawRectangle(buf,x1,y1,20,15,255,0,0);

			if (mouse_b & 1)
			{
				x1-=view.get_GRID_WIDTH()*TILESIZE;
				x1=x1/20;
				y1=y1/15;
				cell=x1*32+y1;
				mt=tile;
			}
		}
//		blitBuffer(buf);

if(masked == false)
	{
	blit(buf,view.get_buffer(),0,0,0,0,SCREEN_W,SCREEN_H);
	}
	else
	{
	masked_blit(buf,view.get_buffer(),0,0,0,0,SCREEN_W,SCREEN_H);
	}
blit(view.get_buffer(),screen,0,0,0,0,SCREEN_W,SCREEN_H);
//blit(buf,view.get_buffer(),0,0,0,0,SCREEN_W,SCREEN_H);
		// move the grid in the direction of the keypressed
	//	if (walk_count==0)
	//	{
			if (key[KEY_LEFT]) 
			{
				if (sx>1)
				{
	
					sx--;
					view.set_screen_pos_x(sx);
				}
			}
			if (key[KEY_RIGHT]) 
			{
				if (sx<(WORLD_X-view.get_GRID_WIDTH()-1))
				{
					sx++;
					view.set_screen_pos_x(sx);
				}
			}
			if (key[KEY_UP]) 
			{
				if (sy>1)
				{
					sy--;
					view.set_screen_pos_y(sy);

				}
			}
			if (key[KEY_DOWN]) 
			{
				if (sy<(WORLD_Y-view.get_GRID_HEIGHT()-1))
				{
					sy++;
					view.set_screen_pos_y(sy);
				}
			}
		//}

		if (key[KEY_1]) // chooses tileset 1
		{
			tile=0;
		}

		if (key[KEY_2]) // chooses tileset 2
		{
			tile=1;
		}

		if (key[KEY_3]) // chooses tileset 3
		{
			tile=2;
		}

		if (key[KEY_4]) // chooses tileset 4
		{
			tile=3;
		}
		if (key[KEY_5]) // chooses tileset 5
		{

			tile=4;
		}

		if (key[KEY_P]) set=1;
		if (key[KEY_O]) set=2;
		if (key[KEY_I]) set=0;


		if (key[KEY_7_PAD]) // move the grid to the topleft corner
		{
			sx=0;
			view.set_screen_pos_x(sx);
			sy=0;
			view.set_screen_pos_y(sy);
		}

		

		if (key[KEY_1_PAD])// move the grid to the bottomleft corner
		{
			sx=0;
			view.set_screen_pos_x(sx);
			sy=WORLD_Y-view.get_GRID_HEIGHT();
			view.set_screen_pos_y(sy);
		}

		if (key[KEY_3_PAD]) // move the grid to the bottomright corner
		{
			sx=WORLD_X-view.get_GRID_WIDTH();
			view.set_screen_pos_x(sx);
			sy=WORLD_Y-view.get_GRID_HEIGHT();
			view.set_screen_pos_y(sy);
		}

		if (key[KEY_9_PAD]) // move the grid to the topright corner
		{
			sx=WORLD_X-view.get_GRID_WIDTH();
			view.set_screen_pos_x(sx);
			sy=0;
			view.set_screen_pos_y(sy);

		}
//}


if (key[KEY_S])
{
save_layer();
}


}
destroy_bitmap(buf);
}

//}

//world gets

//Layer *World::get_layers()
//{
//	return layers;
//}