function WipeIntro()
{
	var images = new Array(9);
	images[0] = LoadImage("wipe_intro/Liegh_console1b_1.png");
	images[1] = LoadImage("wipe_intro/Liegh_console1b_2.png");
	images[2] = LoadImage("wipe_intro/Liegh_console1b_3.png");
	images[3] = LoadImage("wipe_intro/Liegh_console1b_4.png");

	images[4] = LoadImage("wipe_intro/Liegh_console1_glass1.png");
	images[5] = LoadImage("wipe_intro/Liegh_console1_glass2.png");
	images[6] = LoadImage("wipe_intro/Liegh_console1_glass3.png");
	images[7] = LoadImage("wipe_intro/Liegh_console1_glass4.png");

	images[8] = LoadImage("wipe_intro/liegh_eye1_1.png");
	images[9] = LoadImage("wipe_intro/liegh_eye1_2.png");
	images[10] = LoadImage("wipe_intro/liegh_eye1_3.png");
	images[11] = LoadImage("wipe_intro/liegh_eye1_4.png");
	images[12] = LoadImage("wipe_intro/liegh_eye1_5.png");

	images[13] = LoadImage("wipe_intro/mid_liegh_back1_1.png");
	images[14] = LoadImage("wipe_intro/screen_broke1.png");

	images[15] = LoadImage("wipe_intro/wipe_1_1.png");
	images[16] = LoadImage("wipe_intro/wipe_1_2.png");
	images[17] = LoadImage("wipe_intro/wipe_1_3.png");
	images[18] = LoadImage("wipe_intro/wipe_1_4.png");
	images[19] = LoadImage("wipe_intro/wipe_1_5.png");
	images[20] = LoadImage("wipe_intro/wipe_1_6.png");
	images[21] = LoadImage("wipe_intro/wipe_1_7.png");
	images[22] = LoadImage("wipe_intro/wipe_1_8.png");
	images[23] = LoadImage("wipe_intro/wipe_1_9.png");

	var lasttime = GetTime();

	for (wi=0;wi<=20;wi++)
	{
		//draw back stuff;
		RenderBackground();
		Rectangle(0,0,320,240, CreateColor(0,0,0,255*wi/20));
		FlipScreen();
		while (GetTime()-lasttime < 1000/30);
		lasttime = GetTime();
	}
	lasttime = GetTime();
	PauseHeart();
	PauseAllMusic();
	
	var fratey = 1000/12;
	var quity = false;
	var db_message = "";

	function DrawBars()
	{
		Rectangle(0,0,320,20, CreateColor(0,0,0,255));
		Rectangle(0,240-20,320,20, CreateColor(0,0,0,255));
		font2.drawText(5,224, db_message);
	}

	function CheckExit()
	{
		while(AreKeysLeft())
		{
			switch(GetKey())
			{
				case KEY_ENTER:
				{
					quity = true;
				
				}
				case KEY_ESCAPE:
				{
					quity = true;
				
				}
				case KEY_SPACE:
				{
					quity = true;
				
				}
			}
		}
	}
	
	for (wi=0;wi<20;wi++)
	{
		while(GetTime()-lasttime < 1000/30)
		{
			images[0].blit(0,0);
			DrawBars();
			Rectangle(0,0,320,240,CreateColor(0,0,0,255-wi*255/20));
			FlipScreen();
		}
		lasttime = GetTime();
	}


	while(GetTime()-lasttime < 2000 && !quity)
	{
		images[0].blit(0,0);
		DrawBars();
		FlipScreen();
	}
	lasttime = GetTime();

	while(GetTime()-lasttime < fratey*1 && !quity)
	{
		images[0].blit(0,0);
		images[4].blit(0,0);
		DrawBars();
		FlipScreen();
	}
	lasttime = GetTime();

	while(AreKeysLeft())
		GetKey();

	while(GetTime()-lasttime < fratey*1 && !quity)
	{
		images[0].blit(0,0);
		images[5].blit(0,0);
		DrawBars();
		FlipScreen();
		CheckExit();
	}
	lasttime = GetTime();

	while(GetTime()-lasttime < fratey*1 && !quity)
	{
		images[0].blit(0,0);
		images[6].blit(0,0);
		DrawBars();
		FlipScreen();
		CheckExit();
	}
	lasttime = GetTime();

	while(GetTime()-lasttime < fratey*1 && !quity)
	{
		images[0].blit(0,0);
		images[7].blit(0,0);
		DrawBars();
		FlipScreen();
		CheckExit();
	}
	lasttime = GetTime();

	while(GetTime()-lasttime < fratey*2 && !quity)
	{
		images[1].blit(0,0);
		DrawBars();
		FlipScreen();
		CheckExit();
	}
	lasttime = GetTime();
	
	while(GetTime()-lasttime < fratey*2 && !quity)
	{
		images[2].blit(0,0);
		DrawBars();
		FlipScreen();
		CheckExit();
	}
	lasttime = GetTime();

	while(GetTime()-lasttime < fratey*20 && !quity)
	{
		images[3].blit(0,0);
		DrawBars();
		FlipScreen();
		CheckExit();
	}
	lasttime = GetTime();

	//eye now

	var eye_skip = 1;
	
	if (eye_skip == 0)
	{
		while(GetTime()-lasttime < fratey*3 && !quity)
		{
			images[8].blit(0,0);
			DrawBars();
			FlipScreen();
			CheckExit();
		}
		lasttime = GetTime();
		
		var wmessagey = "Leigh: What?";
		var wcount = 0;
	

		for (wj=0;wj<4;wj++)
		{
	
			while(GetTime()-lasttime < fratey*4 && !quity)
			{
				if (GetTime()-lasttime < fratey*4)
				{
					images[8+wj].blit(0,0);
					images[9+wj].blitMask(0,0, CreateColor(255,255,255,255*(GetTime()-lasttime)/(fratey*6)));
				}
				else
					images[9+wj].blit(0,0);
				DrawBars();
				FlipScreen();
				CheckExit();
				wcount++;
				db_message = wmessagey.substr(0,wcount/4);
			}
			lasttime = GetTime();
		}
		
		while(GetTime()-lasttime < fratey*6 && !quity)
		{
			images[12].blit(0,0);
			DrawBars();
			FlipScreen();
			CheckExit();
		}
		lasttime = GetTime();
	
		db_message = "";
	}

	var wx = 0;

	if (eye_skip == 1)
	{
		var wmessagey = "Leigh: What?";
		var wcount = 0;
	}

	wx = 0;
	while(wx<80 && !quity)
	{
		while(GetTime()-lasttime < fratey*3/2)
		{
			images[13].blit(-80+wx,0);
			DrawBars();
			FlipScreen();
			CheckExit();
			if (eye_skip == 1)
			{
				wcount++;
				db_message = wmessagey.substr(0,wcount/4);
			}
		}
		lasttime = GetTime();
		wx++;
	}
	if (eye_skip== 1)
		db_message = "";

	wx = 0;

	var img = 15;
	while(wx<8*9 && !quity)
	{
		while(GetTime()-lasttime < fratey)
		{
			if (wx < 8)
				img = 15;
			else if (wx < 8*2)
				img = 16;
			else if (wx < 8*3)
				img = 17;
			else if (wx < 8*4)
				img = 18;
			else if (wx < 8*5)
				img = 19;
			else if (wx < 8*6)
				img = 20;
			else if (wx < 8*7)
				img = 21;
			else if (wx < 8*8)
				img = 22;
			else //if (wx < *9)
				img = 23;
			images[img].transformBlit(-wx/6,-wx/6, 320+wx/6, -wx/6, 320+wx/6, 240+wx/6, -wx/6, 240+wx/6);

			if (wx > 8*7)
			{
				Rectangle(0,0,320,240, CreateColor(0,0,0,255*(wx-8*7)/16));
			}
			DrawBars();
			FlipScreen();
			CheckExit();
		}
		lasttime = GetTime();
		wx+=3;
	}
	StartHeart();
	UnPauseAllMusic();
	PlayMusic(11);

}

function WipeStart()
{
	SetTile(30,5, 1, 403);
	SetTile(31,5, 1, 404);
	SetTile(30,6, 1, 408);
	SetTile(31,6, 1, 409);
	

	for (enti=0;enti<max_entities;enti++)
	{
		if (entities[enti].name == "wipe_1")
		{
			break;
		}
	}
	
	if (enti < max_entities)
	{
		//active wipe's ai
		entities[enti].active = true;
		entities[enti].startcount = 100;
		entities[enti].x = 30*16+16;
		entities[enti].y = 5*16+16;
		
		entities[enti].nohit = true;
			entities[enti].frame1 = 0;
			entities[enti].dir1 = "fadein";

	SetPersonDirection(entities[enti].name, entities[enti].dir1+"east");
	SetPersonFrame(entities[enti].name, entities[enti].frame1);
		//
		
	}

	if (g_current_level < 13)
	{
		var lasttime = GetTime();
		for (wi=0;wi<20;wi++)
		{
			//draw back stuff;
			RenderBackground();
			Rectangle(0,0,320,240, CreateColor(0,0,0,255-255*wi/20));
			FlipScreen();
			while (GetTime()-lasttime < 1000/30);
			lasttime = GetTime();
		}
	}

}

var wipe_pos = new Array(7);

for (i=0;i<wipe_pos.length;i++)
{
	wipe_pos[i] = new Object();
}

wipe_pos[0].x = 28;
wipe_pos[0].y = 4;

wipe_pos[1].x = 33;
wipe_pos[1].y = 4;

wipe_pos[2].x = 26;
wipe_pos[2].y = 5;

wipe_pos[3].x = 30;
wipe_pos[3].y = 5;

wipe_pos[4].x = 32;
wipe_pos[4].y = 5;

wipe_pos[5].x = 27;
wipe_pos[5].y = 6;
wipe_pos[6].x = 34;
wipe_pos[6].y = 6;

var wipe_hp1_max = 35;
var wipe_hp1_maxr = 15;

function UpdateWipeAI(num)
{
	g_wipe_active = num;
	var tempw=0;
	var tscaley = 1;
	//ps: don't use i;
	//Go through activation, etc.
	//SetLayerVisible(2, true);
	//...
	
	//beginning intro...

	//restock the ammo lockers if the player is low on ammo

	
	if (entities[num].hp<=0)
		entities[num].dir1 = "die1";
	if (entities[num].hp1 > 0)
	{
		SetPersonIDX(entities[num].sid1, -200);
		SetPersonIDX(entities[num].sid2, -200);
		SetPersonIDX(entities[num].sid3, -200);
	}
		
	if (entities[num].startcount >= 0)
	{
		g_rumble = 2;
		entities[num].startcount--;
		if (entities[num].startcount%3==0)
		{
			SpawnExplosion((25*16+11*16*Math.random()),3*16+5*16*Math.random(), 0, 0, Math.random()*0.5+1)
			SpawnExplosion((25*16+11*16*Math.random()),3*16+5*16*Math.random(), 0, 0, Math.random()*0.5+1)
			SpawnExplosion((25*16+11*16*Math.random()),3*16+5*16*Math.random(), 0, 0, Math.random()*0.5+1)
		}

		if (entities[num].startcount < 0)
		{
			for (wi=0;wi<10;wi++)
			{
				SpawnGuts(-1, (25*16+11*16*Math.random()),3*16+5*16*Math.random(), 6*Math.random()-3,-4*Math.random()-2, "barrel1_1", false, blood_layer, undefined)			
			}
			SetLayerVisible(2, true);
			g_rumble = -1;
			entities[num].frame1 = 0;
			entities[num].dir1 = "fadein";
		}
	}
	else if (entities[num].dir1 == "die1")
	{
		SetPersonIDX(entities[num].sid1, -200);
		SetPersonIDX(entities[num].sid2, -200);
		SetPersonIDX(entities[num].sid3, -200);
	
		if (g_frames%(frameupdate*3) == 0)
		{

//			SpawnExplosion((entities[num].x-16+32*Math.random()),entities[num].y-16+32*Math.random(), 0, 0, Math.random()*0.5+1)
//			SpawnExplosion((entities[num].x-16+32*Math.random()),entities[num].y-16+32*Math.random(), 0, 0, Math.random()*0.5+1)
			if (entities[num].frame1<10)
				SpawnExplosion((entities[num].x-16+32*Math.random()),entities[num].y-16+32*Math.random(), 0, 0, Math.random()*0.5+1)

			entities[num].frame1++;
		}
		if (entities[num].frame1 > 20)
		{
			entities[num].frame1 = 20;
			WipeEnd();
			return;
		}
	}
	else if (entities[num].hp1 <= 0)
	{
		entities[num].dir1 = "injury1";
		
		for (ts=1;ts<4;ts++)
		{
			tscaley = Math.cos((g_frames+3*ts)/9)*Math.cos((g_frames+3*ts)/9)/4+0.25;
			
			SetPersonIDX(entities[num]["sid"+ts], entities[num].x+Math.cos(g_frames/5+3.14159*2*ts/3)*16-tscaley*8+8);
			SetPersonIDY(entities[num]["sid"+ts], entities[num].y+Math.sin(g_frames/5+3.14159*2*ts/3)*4-16-tscaley*8);
			SetPersonIDDirection(entities[num]["sid"+ts], "wipe_star");
			SetPersonIDScaleFactor(entities[num]["sid"+ts], tscaley, tscaley);
			SetPersonIDFrame(entities[num]["sid"+ts], 0);
		}

		if (g_frames%(frameupdate*2) == 0)
			entities[num].frame1++;

		if (entities[num].frame1 > 8)
		{
				entities[num].frame1 = 0;
				entities[num].hp1 = wipe_hp1_max+Math.floor(Math.random()*wipe_hp1_maxr);
				entities[num].dir1 = "fadeout";
				entities[num].nohit = true;
		}
	}
	else if (entities[num].dir1 == "fadein")
	{
		if (g_frames%(frameupdate*2) == 0)
			entities[num].frame1++;
		if (entities[num].frame1 > 4)
		{
			entities[num].frame1 = 0;
			entities[num].dir1 = "idle1";
			entities[num].nohit = false;
		}
	}
	else if (entities[num].dir1 == "fadeout")
	{
		if (g_frames%(frameupdate*2) == 0)
			entities[num].frame1++;
		if (entities[num].frame1 > 4)
		{
			entities[num].frame1 = 0;
			entities[num].dir1 = "fadein";
			tempw = Math.floor(7*Math.random());
			entities[num].x = wipe_pos[tempw].x*16+16; 
			entities[num].y = wipe_pos[tempw].y*16+16; 
		}
	}
	else if (entities[num].dir1 == "idle1")
	{
		if (g_frames%(frameupdate*1) == 0)
			entities[num].frame1++;
		if (entities[num].frame1 > 3)
		{
			entities[num].frame1 = 0;
			//chance to attack:
			if (Math.random()<0.5)
			{
				entities[num].frame1=0;
				entities[num].dir1="attack1";
			}
		}
	}
	else if (entities[num].dir1 == "attack1")
	{
		if (g_frames%(frameupdate*2) == 0)
		{
			if (entities[num].frame1 == 0)
			{
				PlaySound(55, true);
			}
			entities[num].frame1++;
		}

		if ((entities[0].x < 22*16 || entities[0].x > 39*16) && Math.random()<0.5)
			SpawnDeadlyFlame2(entities[num].x,entities[num].y+8,Math.random()*6-3,Math.random()*-4-1,num);
		else
			SpawnDeadlyFlame(entities[num].x,entities[num].y+8,Math.random()*6-3,Math.random()*-4-1,num);

		if (entities[num].frame1 > 3)
		{
			entities[num].frame1 = 0;
//			entities[num].hp1 = wipe_hp1_max;
			entities[num].dir1="fadeout";
			entities[num].nohit = true;
		}
	}
	
	
	var wi_width = 32;
	var wi_height = 32;
	
	//otherwise check for grenades nearby or the player
	if (entities[num].nohit !=true && entities[num].dir1!="injury1" && entities[num].hp > 0)
	{
		if (Math.abs(entities[num].x-entities[0].x)<wi_width*1 && Math.abs(entities[num].y-entities[0].y)<wi_height*1)
		{
			entities[num].frame1 = 0;
//			entities[num].hp1 = wipe_hp1_max;
			entities[num].dir1 = "fadeout";
			entities[num].nohit = true;	
		}
	}
	if (entities[num].nohit !=true && entities[num].dir1!="injury1" && entities[num].hp > 0)
	{
		for (wi=0;wi<max_grenades;wi++)
		{
			if (Math.abs(entities[num].x-grenades[wi].x)<wi_width && Math.abs(entities[num].y-grenades[wi].y)<wi_height)
			{
				entities[num].frame1 = 0;
//				entities[num].hp1 = wipe_hp1_max;
				entities[num].dir1 = "fadeout";
				entities[num].nohit = true;	
			}
		}
	}

	//Attack... maybe change position
	SetPersonDirection(entities[num].name, entities[num].dir1+"east");
	SetPersonFrame(entities[num].name, entities[num].frame1);
	SetPersonLayer(entities[num].name, blood_layer-1);
	SetPersonX(entities[num].name, entities[num].x);
	SetPersonY(entities[num].name, entities[num].y);
}

function StunWipe(num)
{
	entities[num].frame1 = 0;
	entities[num].dir1 = "injury1";

	//start timer;
}

function WipeEnd()
{
	//fade to cutscene
	//cutscene
	//fade to usual screen.
	WipeOutro();
	ClearGameInfo(-1);
	GoToLevel(5);
	g_cancel_fade = true;
}