var id_mask = LoadImage("id_mask.png");


function IdIntro()
{
	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 images = new Array(9);
	images[0] = LoadImage("id_intro/stand1_01.png");
	images[1] = LoadImage("id_intro/stand1_02.png");
	images[2] = LoadImage("id_intro/stand1_03.png");
	images[3] = LoadImage("id_intro/stand1_04.png");
	images[4] = LoadImage("id_intro/stand_close1.png");

	while(AreKeysLeft())GetKey();

	var fratey = 1000/12;
	var quity = false;

	function DrawBars()
	{
		Rectangle(0,0,320,20, CreateColor(0,0,0,255));
		Rectangle(0,240-20,320,20, CreateColor(0,0,0,255));
	}

	function CheckExit()
	{
		//not used for this intro
		while(AreKeysLeft())
		{
			switch(GetKey())
			{
				case KEY_ENTER:
				{
					quity = true;
				
				}
				case KEY_ESCAPE:
				{
					quity = true;
				
				}
				case KEY_SPACE:
				{
					quity = true;
				
				}
			}
		}
	}
	
	var imnum = 0;
	
	for (wi=0;wi<55 && quity == false;wi++)
	{
		if (wi == 20+10)
			imnum++;
		if (wi == 20+12)
			imnum++;
		if (wi == 20+14)
			imnum++;

		while(GetTime()-lasttime < 1000/20)
		{
			images[imnum].blit(5,-20);
			DrawBars();
			Rectangle(0,0,320,240,CreateColor(0,0,0,255-wi*255/20));
			FlipScreen();
			//CheckExit();
		}
		lasttime = GetTime();
	}
	
	while(AreKeysLeft())
		GetKey();

	for (wi=0;wi<70&& quity == false;wi++)
	{
		while(GetTime()-lasttime < 1000/15)
		{
			images[4].blit(5,240-images[4].height-20+wi);
			DrawBars();
			if (wi > 40)
				Rectangle(0,0,320,240,CreateColor(0,0,0,(wi-40)*255/30));
			FlipScreen();
			CheckExit();
		}
		lasttime = GetTime();
	}

	var text = "";

	for (wi=0;wi<20&& quity == false;wi++)
	{
		while(GetTime()-lasttime < 1000/15)
		{
			FlipScreen();
		}
		lasttime = GetTime();
	}

	StartHeart();
	UnPauseAllMusic();

	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 id_id;

var id_regencount = -1;
var id_regenmax = 10;

var id_dash_bounderline = 110;
var id_direction = -1;
var id_dash_attack = 32;

var id_walk_counter = 0;
var id_walk_counter_max = 30;

var id_walk_back = -1;
var id_stage = 1;
var id_death_counter = 0;

function IdStart()
{
	//show the cutscene
	g_id_trigger = 3;
	IdIntro();
	MakeTransition(1);
	entities[0].vy = 0;
	g_fall_counter = 0;
	for (enti=0;enti<max_entities;enti++)
	{
		if (entities[enti].name == "id_legs_1")
		{
			break;
		}
	}
	
	id_direction = -1;
	id_walk_counter = 0;
	id_stage = 1;
	id_walk_back = -1;

	if (enti < max_entities)
	{
		id_id = enti;
		//activate id'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 = "idle";

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

function IdStart2()
{
  var mask = CreateColor(255,255,255,255);
	
	if (music[14].isPlaying() == true)
		music[14].stop();
	MakeTransition(1);

	for (enti=0;enti<max_entities;enti++)
	{
		if (entities[enti].name == "id_legs_1")
		{
			break;
		}
	}
	
	id_direction = 1;
	id_walk_counter = 0;
	id_stage = 2;
	id_walk_back = -1;

	if (enti < max_entities)
	{
	
	
		id_id = enti;
		//activate id's ai
		entities[enti].active = true;
		PlaySound(41,true);

		entities[id_id].x = 147*16;
		entities[id_id].y = 104*16+8;

		for (entj=0;entj<2;entj++)
		{
			SpawnGuts(-2,entities[enti].x,entities[enti].y,-1+2*Math.random(),-7+2*Math.random(), "guts"+(2+Math.floor(3*Math.random())), 1, gut_layer+Math.floor(2*Math.random()), mask);
			SpawnGuts(-2,entities[enti].x,entities[enti].y,-1+2*Math.random(),-7+2*Math.random(), "guts"+(2+Math.floor(3*Math.random())), 1, gut_layer+Math.floor(2*Math.random()), mask);
			SpawnGuts(-2,entities[enti].x,entities[enti].y,-1+2*Math.random(),-7+2*Math.random(), "guts"+(2+Math.floor(3*Math.random())), 1, gut_layer+Math.floor(2*Math.random()), mask);
			SpawnGuts(-1,entities[enti].x,entities[enti].y,-1+2*Math.random(),-7+2*Math.random(), "guts"+(2+Math.floor(3*Math.random())), 1, gut_layer+Math.floor(2*Math.random()), mask);
			SpawnGuts(-1,entities[enti].x,entities[enti].y,-1+2*Math.random(),-7+2*Math.random(), "guts"+(2+Math.floor(3*Math.random())), 1, gut_layer+Math.floor(2*Math.random()), mask);
		}

//		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 = "idle";

		SetPersonDirection(entities[enti].name, entities[enti].dir1+"east");
		SetPersonFrame(entities[enti].name, entities[enti].frame1);
		//
	}
	
	
	SetTile(146,103, 2, 735);
	SetTile(147,103, 2, 736);
	SetTile(146,104, 2, 740);
	SetTile(147,104, 2, 741);
	SetTile(146,105, 2, 745);
	SetTile(147,105, 2, 746);
	
}


function IdStart3()
{
	if (music[14].isPlaying() == true)
		music[14].stop();

  var mask = CreateColor(255,255,255,255);

	for (enti=0;enti<max_entities;enti++)
	{
		if (entities[enti].name == "id_legs_1")
		{
			break;
		}
	}
	MakeTransition(3);
	
	id_direction = 1;
	id_walk_counter = 0;
	id_stage = 3;
	id_walk_back = -1;

	if (enti < max_entities)
	{
	
	
		id_id = enti;
		//activate id's ai
		entities[enti].active = true;
		PlaySound(41,true);

		entities[id_id].x = 373*16;
		entities[id_id].y = 142*16+8;

		for (entj=0;entj<2;entj++)
		{
			SpawnGuts(-2,entities[enti].x,entities[enti].y,-1+2*Math.random(),-7+2*Math.random(), "guts"+(2+Math.floor(3*Math.random())), 1, gut_layer+Math.floor(2*Math.random()), mask);
			SpawnGuts(-2,entities[enti].x,entities[enti].y,-1+2*Math.random(),-7+2*Math.random(), "guts"+(2+Math.floor(3*Math.random())), 1, gut_layer+Math.floor(2*Math.random()), mask);
			SpawnGuts(-2,entities[enti].x,entities[enti].y,-1+2*Math.random(),-7+2*Math.random(), "guts"+(2+Math.floor(3*Math.random())), 1, gut_layer+Math.floor(2*Math.random()), mask);
			SpawnGuts(-1,entities[enti].x,entities[enti].y,-1+2*Math.random(),-7+2*Math.random(), "guts"+(2+Math.floor(3*Math.random())), 1, gut_layer+Math.floor(2*Math.random()), mask);
			SpawnGuts(-1,entities[enti].x,entities[enti].y,-1+2*Math.random(),-7+2*Math.random(), "guts"+(2+Math.floor(3*Math.random())), 1, gut_layer+Math.floor(2*Math.random()), mask);
		}

//		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 = "idle";

		SetPersonDirection(entities[enti].name, entities[enti].dir1+"east");
		SetPersonFrame(entities[enti].name, entities[enti].frame1);
		//
	}
	
	
	SetTile(372,141, 2, 735);
	SetTile(373,141, 2, 736);
	SetTile(372,142, 2, 740);
	SetTile(373,142, 2, 741);
	SetTile(372,143, 2, 745);
	SetTile(373,143, 2, 746);
	
}



function UpdateIdAI()
{
//update physics
var friction = 0.1;
	g_id_active = id_id;

	//add gravity;
	if (entities[id_id].hp1<=0)
		entities[id_id].vx = 0;

	
				if (entities[id_id].vy < grav_max && !IsPersonOnGround(id_id))
					entities[id_id].vy+=grav_amt;
	
				sx = entities[id_id].x;
				sy = entities[id_id].y;
				
				entities[id_id].x += entities[id_id].vx;
				entities[id_id].y += entities[id_id].vy;
				
				t_layer = GetPersonIDLayer(entities[id_id].id1);
				SetPersonIDLayer(entities[id_id].id1, 0);
				
				if (IsPersonIDObstructed(entities[id_id].id1, entities[id_id].x, entities[id_id].y))
				{
					speed = Math.sqrt(entities[id_id].vx*entities[id_id].vx + entities[id_id].vy*entities[id_id].vy);
					
					for (k=speed-1;k>=0;k--)
					{
						entities[id_id].x = sx+entities[id_id].vx*(k/speed);
						entities[id_id].y = sy+entities[id_id].vy*(k/speed);
					
						if (!IsPersonIDObstructed(entities[id_id].id1, entities[id_id].x, entities[id_id].y))
							break;
					}
					if (IsPersonIDObstructed(entities[id_id].id1, entities[id_id].x, entities[id_id].y))
					{
						entities[id_id].x = sx;
						entities[id_id].y = sy;
					}
				}
	
				if (entities[id_id].vx < 0 && IsEntityOnGround(id_id))
				{
					entities[id_id].vx+=friction/4;
					if (entities[id_id].vx > 0)
						entities[id_id].vx = 0;
				}
				else if (entities[id_id].vx > 0 && IsEntityOnGround(id_id))
				{
					entities[id_id].vx-=friction/4;
					if (entities[id_id].vx < 0)
						entities[id_id].vx = 0;
				}
				
	
				if (IsPersonIDObstructed(entities[id_id].id1, entities[id_id].x, entities[id_id].y+1) ||
						IsPersonIDObstructed(entities[id_id].id1, entities[id_id].x, entities[id_id].y-1))
				{
					if (entities[id_id].vy > 3)
						PlaySound(33);
					
					entities[id_id].vy = 0;
				}
				if (IsPersonIDObstructed(entities[id_id].id1, entities[id_id].x+1, entities[id_id].y) ||
						IsPersonIDObstructed(entities[id_id].id1, entities[id_id].x-1, entities[id_id].y))
					entities[id_id].vx = 0;
					
				SetPersonIDLayer(entities[id_id].id1, t_layer);




//update frames





var amb_flag = false;
var num = id_id;

	//whatever
	if (entities[num].vx > 0)
		entities[num].dir = 1;
	if (entities[num].vx < 0)
		entities[num].dir = -1;
		
	var face = "east"
	var facedir = (-1*entities[num].dir+1)/2;
	/*
	font.drawText(0,0, num);
	font.drawText(0,20, entities[num].dir2);
	FlipScreen();
	GetKey();
	*/
	SetPersonIDHorizontalFlip(entities[num].id1, facedir);
	SetPersonIDHorizontalFlip(entities[num].id2, facedir);
	
	if (entities[num].type == 2)
	{
		if (entities[num].hp <= 0)
		{
			SetPersonIDDirection(entities[num].id1, "die1east");
			SetPersonIDFrame(entities[num].id1, 1);
			entities[num].frame2 = 1;
			entities[num].dir2 = "die1";
		}
	}
	
	if ((entities[num].dir2).substr(0,3) != "leg")
	{

		//change dir2 based on current action
		//eg: should be walking, should be dead, etc.
	
		if (entities[id_id].hp1 <=0)
		{

			if (id_walk_back >= 7 && id_stage == 3 && entities[num].dir2 != "die1")
			{
				entities[num].dir2 = "die1";
				entities[id_id].vx = 0;
				entities[id_id].frame2 = 0;
				id_death_counter = 0;
			}
			else if (entities[id_id].dir2 != "armregen" && entities[num].dir2 != "die1")
			{
				entities[id_id].dir2 = "armregen";
				entities[id_id].vx = 0;
				entities[id_id].frame2 = 0;
			}
		}
		else if (entities[num].dir2 == "idle" && entities[num].vx != 0 && IsPersonOnGround(num) == true)
		{
			entities[num].dir2 = "walk";
			entities[num].frame2 = 0;
		}
		
		if (entities[num].dir2 != "walk" && IsPersonOnGround(num) && (entities[num].dir2).substr(0,6) != "attack" && entities[num].hp > 0 && entities[num].hp1 > 0&& entities[num].vx != 0 && entities[num].dir2 != "dash1")
		{
			entities[num].dir2 = "walk";
		}
		else if (entities[num].dir2 != "idle" && IsPersonOnGround(num) && (entities[num].dir2).substr(0,6) != "attack" && entities[num].hp > 0 && entities[num].hp1 > 0&& entities[num].vx == 0 && entities[num].dir2 != "dash1")
		{
			entities[num].dir2 = "idle";
		}


		if (id_walk_back == -1 || id_stage == 3)
		{
			//Try doing a dash
			if ((entities[0].x - entities[num].x)*id_direction > id_dash_bounderline)
			{
				//go into dash mode if id is walking
				if (entities[num].dir2 == "walk")
				{
					//try attack 4 first
					if (entities[0].stam > 1)
					{
						entities[num].dir2 = "attack4";
						entities[num].frame2 = 0;
					}
					else
					{
						entities[num].dir2 = "dash1";
						entities[num].frame2 = 0;
					}
				}
			}
		}
		
		if ((entities[num].dir2).substr(0,3) == "die")
		{
			//update the death frames.
	
			if (g_frames % frameupdate == 0)
				entities[num].frame2++;
				
			if (entities[num].frame2 >= entity_frames[entities[num].type].die)
			{
				entities[num].frame2 = entity_frames[entities[num].type].die
			}
			//if on last frame, do not update the frames again.
		}
		else if (entities[num].dir2 == "dash1")
		{
			if (g_frames % frameupdate == 0 && entities[num].frame2 < 4)
				entities[num].frame2++;
			else if (entities[num].frame2 == 4)
			{
				if ((entities[0].x - entities[num].x)*id_direction < id_dash_attack)
				{
					entities[num].dir2 = "attack2";
					entities[num].frame2 = 3;
					PlaySound(45,true);
				}
			}
		}
		else if ((entities[num].dir2).substr(0,6) == "injury")
		{
			if (g_frames % frameupdate == 0)
				entities[num].frame2++;
				
			if (entities[num].frame2 > entity_frames[entities[num].type].injury)
			{
				entities[num].dir2 = "idle";
				entities[num].frame2 = 0;
			}
		}
		else if ((entities[num].dir2).substr(0,8) == "armregen")
		{
			if (g_frames % (frameupdate*3) == 0)
			{
				if (id_regencount < id_regenmax)
				{
					id_regencount++;
					if (id_regencount == id_regenmax)
						entities[num].frame2 = 0;
				}
				else
					entities[num].frame2++;
					
			}
				
			if (entities[num].frame2 > entity_frames[entities[num].type].armregen)
			{
				id_regencount = -1;
				if (id_stage == 3)
				{
					id_walk_back++;
				}
				entities[num].dir2 = "idle";
				entities[num].frame2 = 0;
				entities[num].hp1 = entities[num].maxhp;
			}
		}
		else if (IsPersonOnGround(num) == true)
		{
			if ((entities[num].dir2).substr(0,6) == "attack")
			{
				//update attack frames
				if (g_frames % frameupdate == 0)
				{
					entities[num].frame2++;
					if (entities[num].frame2 == 1)
					{
						if (entities[num].type == 5 && entities[num].dir2 == "attack2")
						{
						}
						else if (entities[num].dir2 == "attack2")
							PlaySound(45,true);
					}
					if (entities[num].dir2 == "attack3")
					{
						if (entities[num].frame2 == 6)
							SpawnBeam(entities[num].x,entities[num].y, entities[num].dir, 3, num);
						if (entities[num].frame2 == 8)
							PlaySound(75);

					}
					if (entities[num].dir2 == "attack4")
					{
						if (entities[num].frame2 == 2)
							SpawnUpBeam(entities[0].x-160-16,entities[0].y-160+32, 1, num);
						if (entities[num].frame2 == 1)
						{
							if (Math.random() < 0.5)
								PlaySound(106);
							else
								PlaySound(107);
						}

					}
				}
					
				if (entities[num].frame2 == 4)
				{
					if (entities[num].type == 5 && entities[num].dir2 == "attack2")
					{
					}
					else if (entities[num].dir2 == "attack2")
					  SpriteAttack(num);
				}

				if (entities[num].dir2 == "attack2")
				{
					if (entities[num].frame2 > entity_frames[entities[num].type].attack2)
					{
						entities[num].dir2 = "idle";
						entities[num].frame2 = 0;
					}
				}
				else if (entities[num].dir2 == "attack1")
				{
					if (entities[num].frame2 > entity_frames[entities[num].type].attack)
					{
						entities[num].dir2 = "idle";
						entities[num].frame2 = 0;
					}
				}
				else if (entities[num].dir2 == "attack3")
				{
					if (entities[num].frame2 > entity_frames[entities[num].type].attack3)
					{
						entities[num].dir2 = "idle";
						entities[num].frame2 = 0;
					}
				}
				else if (entities[num].dir2 == "attack4")
				{
					if (entities[num].frame2 > entity_frames[entities[num].type].attack4)
					{
						entities[num].dir2 = "idle";
						entities[num].frame2 = 0;
					}
				}
			}
			else if (entities[num].dir2 == "walk")
			{
				//it's walking.
				if (g_frames % frameupdate == 0)
				{
					entities[num].frame2++;
					if (entities[num].frame2 == 1||entities[num].frame2 == 4)
						PlayEnemyWalk(64*Math.random());
					id_walk_counter++;
				}
			}
			else if (entities[num].dir2 == "idle")
			{
				if (g_frames % frameupdate == 0)
					entities[num].frame2++;
			}
		}
		else
		{
			//enemy is in the air, default is the jump frames
			entities[num].dir2 = "jump";
		
			if (entities[num].vy < 0)
				entities[num].frame2 = 0;
			else
				entities[num].frame2 = 1;
		}
	}


	SetPersonIDFrame(entities[num].id1, entities[num].frame2);
	SetPersonIDFrame(entities[num].id2, entities[num].frame2);

	SetPersonIDDirection(entities[num].id1, entities[num].dir2+"east");
	SetPersonIDDirection(entities[num].id2, entities[num].dir2+"east");

	entities[num].frame2 = GetPersonIDFrame(entities[num].id1);

	if (entities[num].dir2 == "armregen")
	{
		if (id_regencount < id_regenmax && id_regencount > -1)
		{
			SetPersonIDDirection(entities[num].id1, "idle"+"east");
			SetPersonIDDirection(entities[num].id2, "idle"+"east");

			SetPersonIDFrame(entities[num].id1, id_regencount);
			SetPersonIDFrame(entities[num].id2, id_regencount);
		}
	}



	//update character info in the map
	SetPersonIDX(entities[id_id].id1, entities[id_id].x);
	SetPersonIDY(entities[id_id].id1, entities[id_id].y);

	if (g_frames % (1*frameupdate)== 0 && IsPersonOnGround(id_id)==true && id_stage != 3)
	{
		var firex = Math.floor(entities[id_id].x/GetTileWidth())-entities[id_id].dir;
		var firex2 = Math.floor((entities[id_id].x-8)/GetTileWidth())-entities[id_id].dir;
		var firey = Math.floor(entities[id_id].y/GetTileHeight()+1);

		//determine whether or not to ignite a fire.
		if (GetTile(firex2, firey, GetNumLayers()-1) == 0)
			SetTile(firex2, firey, GetNumLayers()-1, 691); 
		if (GetTile(firex, firey, GetNumLayers()-2) == 0)
			SetTile(firex, firey, GetNumLayers()-2, 691); 

	}

	if (entities[id_id].hp1 > 0)
	{
		SetPersonIDX(entities[id_id].id2, entities[id_id].x);
		SetPersonIDY(entities[id_id].id2, entities[id_id].y);

		if (entities[id_id].dir2 == "dash1" && entities[id_id].frame2 == 4 && Math.random() < 0.5)
		{
			SetPersonIDX(entities[id_id].id2, entities[id_id].x-entities[id_id].dir*16);
		}

	}
	else
		SetPersonIDX(entities[id_id].id2, -100);

//update ai

	//move left and attack
	if (id_walk_back >=64)
	{
		if (id_stage == 1)
		{
			if (entities[id_id].x < 143*16)
				entities[id_id].vx = 2;
			else
				entities[id_id].active = false;
		}
		if (id_stage == 2)
		{
			if (entities[id_id].x > 376*16)
				entities[id_id].vx = -2;
			else
				entities[id_id].active = false;
		}

	}
	else if ((entities[id_id].dir2).substr(0,8) == "armregen")
		entities[id_id].vx = 0;
	else if ((entities[id_id].dir2).substr(0,3) == "die")
	{
		//apply a counter
		if (g_frames % (1*frameupdate)== 0)
		{
			if (id_death_counter == 0)
				PlaySound(114);

			id_death_counter++;
		}
		if (id_death_counter > 64)
		{
			g_exit_level = 10;
			IdEnd();

			//end the level
		}
	
	}
	else if (entities[0].hp <=0)
	{
		entities[id_id].vx = 0;
	}
	else if (entities[id_id].dir2 == "dash1" && entities[id_id].frame2 == 4)
	{
		entities[id_id].vx = 16*id_direction;
	}
	else if (((entities[id_id].dir2).substr(0,4) == "walk" || (entities[id_id].dir2).substr(0,4) == "idle")&& (entities[0].x - entities[num].x)*id_direction < id_dash_attack)
	{
		if ((entities[0].x - entities[num].x)*id_direction < -id_dash_attack)
		{
			entities[id_id].dir=-1*id_direction;
			entities[id_id].vx = 0;
			if ((entities[id_id].dir2).substr(0,4) != "idle")
			{
				entities[id_id].dir2 = "idle1";
				entities[id_id].frame = 0;
			}
		}
		else
		{
			entities[id_id].vx = 0;
			entities[id_id].dir2 = "attack2";
			entities[id_id].frame = 0;
		}
	}
	else if ((entities[num].dir2).substr(0,6) != "attack" && (entities[num].dir2).substr(0,4) != "dash")
	{
//		FlipScreen();
		entities[id_id].vx = 2*id_direction;
		if (id_walk_counter >= id_walk_counter_max*(Math.random()+1))
		{
			if (Math.random()<0.5)
			{
				amb_flag = false;
				//search for nearby ambushing enemies
				for (aii=0;aii<max_entities;aii++)
				{
					if (entities[aii].ambush == true)
					{
						if (Math.abs(entities[aii].x-entities[0].x) < 160 && Math.abs(entities[aii].y-entities[0].y)< 120)
						{
							amb_flag = true;
							EnemyAmbush(undefined, aii);
						}
					}
				}
				if (amb_flag == true)
					entities[id_id].dir2 = "attack1";
				else
					entities[id_id].dir2 = "attack3";
			}
			else if (Math.random()<0.33)
			{
				entities[id_id].dir2 = "attack4";
			}
			else
			{
				entities[id_id].dir2 = "attack3";
			}
					
			entities[id_id].frame2 = 0;
			entities[id_id].vx = 0;
			id_walk_counter = 0;
		}
	}
	else
	{
		entities[id_id].vx = 0;
	}

	if (id_stage == 1)
	{
		if (Math.abs(entities[id_id].x-133*16+4)<16 && Math.abs(entities[id_id].y-63*16-8)<16 && IsIdCloseDoor("door1b_1c"))
		{
			entities[id_id].dir2 = "walk";
			if (g_frames % frameupdate == 0 && id_walk_back < 64)
			{
				entities[id_id].vx = 0;
				id_walk_back+=2;
			}
			if (id_walk_back >=64)
			{
				entities[id_id].vx = 2;
				entities[id_id].dir = 1;
			}
		}
		else if (Math.abs(entities[id_id].x-133*16+4)<16 && Math.abs(entities[id_id].y-63*16-8)<16)
		{
			if (entities[id_id].dir2 == "dash1")
				entities[id_id].dir2 = "walk";
		}
	}
	else if (id_stage == 2)
	{
		if (Math.abs(entities[id_id].x-386*16-4)<16 && Math.abs(entities[id_id].y-105*16-8)<16 && IsIdCloseDoor("door1b_2c"))
		{
			entities[id_id].dir2 = "walk";
			if (g_frames % frameupdate == 0 && id_walk_back < 64)
			{
//				FlipScreen();
//				entities[id_id].vx = 0;
				id_walk_back+=2;
			}
			if (id_walk_back >=64)
			{
				entities[id_id].vx = -2;
				entities[id_id].dir = -1;
			}
		}
		else if (Math.abs(entities[id_id].x-386*16-4)<16 && Math.abs(entities[id_id].y-105*16-8)<16)
		{
			if (entities[id_id].dir2 == "dash1")
				entities[id_id].dir2 = "walk";
		}
	}
	else if (id_stage == 3 && entities[id_id].hp1>0)
	{
		if (entities[0].x < entities[id_id].x)
			id_direction = -1;
		else
			id_direction = 1;
	}

}

function IdCloseDoor(name)
{
//	StopAllMusics();
	MakeTransition(8)

	for (hs=0;hs<max_doors;hs++)
	{
		if (doors[hs].name == name)
		{
			doors[hs].active = (doors[hs].active+1)%2;
				
			doors[hs].dir = "closed1";
			doors[hs].frame = 0;
			PlaySound(39);

			SetPersonIDDirection(doors[hs].id1, doors[hs].dir);
			SetPersonIDFrame(doors[hs].id1, doors[hs].frame);
			break;
		}
	}
}

function IsIdCloseDoor(name)
{
	for (hs=0;hs<max_doors;hs++)
	{
		if (doors[hs].name == name)
		{
			if (doors[hs].dir == "open1")
				return true;
			else
				return false;
		}
	}
	return true;
}

var g_id_trigger = -1;

function IdSounds1()
{
	g_id_trigger = 0;
	IdSounds1a(53);
	Queue_Command("IdSounds1b(54)", 8);
}

function IdSounds1c()
{
	IdSounds1a(97);
}


function IdSounds1d()
{
	IdSounds1b(98);
}


var id_s_time = 8;
function IdSounds1a(nummy)
{
	if (g_id_trigger < 1)
	{
		var s = Math.floor((Math.random()*8))+4;
		var vol = Math.pow(((entities[0].x-150*16)/(300*16)),1);
		if (entities[0].x < 175*16)
			id_s_stime = 12;
		else if (entities[0].x < 200*16)
			id_s_stime = 10;
		else if (entities[0].x < 225*16)
			id_s_stime = 7;
		else if (entities[0].x < 250*16)
			id_s_stime = 4;
		else if (entities[0].x < 275*16)
			id_s_stime = 2;
		else if (entities[0].x < 300*16)
			id_s_stime = 1;
	
	
		PlaySound(nummy, true, vol, false);
		Queue_Command("IdSounds1a("+nummy+");", 1+id_s_time*(s));
	}
}

function IdSounds1b(nummy)
{
	if (g_id_trigger < 1)
	{
		var s = Math.floor((Math.random()*8))+4;
		var vol = Math.pow(((entities[0].x-150*16)/(300*16)),1);
	
		if (entities[0].x < 175*16)
			id_s_stime = 12;
		else if (entities[0].x < 200*16)
			id_s_stime = 10;
		else if (entities[0].x < 225*16)
			id_s_stime = 7;
		else if (entities[0].x < 250*16)
			id_s_stime = 4;
		else if (entities[0].x < 275*16)
			id_s_stime = 2;
		else if (entities[0].x < 300*16)
			id_s_stime = 1;


		PlaySound(nummy, true, vol, false);
		Queue_Command("IdSounds1b("+nummy+");", 1+id_s_time*(s));
	}
}

function IdSounds2()
{
	g_id_trigger = 2;
	PlaySound(94, false, 1);
	//if (music[curr_music].isPlaying() == true)
		//music[curr_music].stop();
//	Queue_Command("IdSounds2a();", 364);
	//var s = Math.floor((Math.random()*16));
	//Queue_Command("IdSounds2b();", 4*s+200);
}

function IdSounds2a()
{
	if (g_id_trigger < 3)
	{
		//set static variable
		g_statics=100
	
		Queue_Command("IdSounds2a();", 264+Math.floor(Math.random()*200));
	}
}

function IdSounds2b()
{
	if (g_id_trigger < 3)
	{
		var s = Math.floor((Math.random()*32));
		PlaySound(95, true);
		Queue_Command("IdSounds2b();", 4*s+96);
	}
}