
function UpdateEntityAi(num)
{
	if (entities[num].type == 7 && entities[num].dir2 == "rise")
		return;
	var max_vx = 4;
	if (entities[num].type == 12)
		var jump_vy = -6;
	else if (entities[num].type == 9)
		var jump_vy = -7;
	else
		var jump_vy = -8;
	var ent = entities[num];
	var sx = ent.x;
	var sy = ent.y;
	if (entities[num].type == 1)
		max_vx = 3.5;
	else if (entities[num].type == 3)
		max_vx = 5;
	else if (entities[num].type == 7)
		max_vx = 4;
	else if (entities[num].type == 8)
		max_vx = 3;
	else if (entities[num].type == 9)
		max_vx = 3;
	else if (entities[num].type == 12)
		max_vx = 5;
	var amt_vx=1;

	var g_layer = 0;

	var jump_tiles = 5;
	if (entities[num].type == 9)
		jump_tiles = 4;
	if (entities[num].type == 12)
		jump_tiles = 3;

	function MoveTowardsPlayer(numy)
	{
		if (ent.type == 12)
		{
			if (ent.frame2 <5 && ent.frame > 1)
				amt_vx=1/2;
			else
				amt_vx=max_vx/2;
		}
		else
			amt_vx=1;

		if (ent.hp3 > 0 && ent.type != 9)
		{
/*			if (!IsEntityOnGround(numy))
				amt_vx = 1;
			else
				amt_vx = 0.25;
*/
			SetPersonIDLayer(ent.id1, 0)
			
			if (entities[0].x < ent.x-5*ent.w && !IsPersonIDObstructed(ent.id1, ent.x-1, ent.y))
			  ent.vx-=amt_vx;
			else if (entities[0].x > ent.x+5*ent.w && !IsPersonIDObstructed(ent.id1, ent.x+1, ent.y))
			  ent.vx+=amt_vx;
			else
			{
			  ent.vx*=0.5;
			  if (Math.abs(ent.vx)<0.5)
					ent.vx = 0;
			}
			
			if (!IsEntityOnGround(numy))
			{
				if (Math.abs(entities[0].x-ent.x) < 4*ent.w)
				{
					ent.vx*=0.5;
					if (Math.abs(ent.vx)<0.5)
						ent.vx = 0;
				}
			}
			
			
			
			
			SetPersonIDLayer(ent.id1, ent.layer);

			if ((entities[0].dir2).substr(0,6) != "attack")
			{
				if (entities[0].vx == 0)
					entities[0].dir2 = "idle";
				else
					entities[0].dir2 = "walk";
			}

			if (ent.vx < -max_vx)
			  ent.vx = -max_vx;
			if (ent.vx > max_vx)
			  ent.vx = max_vx;
			  
			var tlayer = GetPersonIDLayer(ent.id1);
			
			SetPersonIDLayer(ent.id1, 0);
			  
			//jump here
			if (IsPersonIDObstructed(ent.id1, ent.x-ent.w-2, ent.y) && IsPersonIDObstructed(ent.id1, ent.x, ent.y+1) && entities[0].x < ent.x-16)
			{
				var flagyy = false;
				
				for (fi=1;fi<jump_tiles;fi++)
				{
					if (IsPersonIDObstructed(ent.id1, ent.x, ent.y-16*fi))
					  break;
					else if (!IsPersonIDObstructed(ent.id1, ent.x-ent.w-2, ent.y-16*fi))
					{
						flagyy = true;
						break;
					}
				}
				//jump left
				if (flagyy)
				{
					ent.vy = jump_vy;
				}
			}
			else if (IsPersonIDObstructed(ent.id1, ent.x+ent.w+2, ent.y)&& IsPersonIDObstructed(ent.id1, ent.x, ent.y+1) && entities[0].x > ent.x+16)
			{
				//jump left
				var flagyy = false;
				
				for (fi=1;fi<jump_tiles;fi++)
				{
					if (IsPersonIDObstructed(ent.id1, ent.x, ent.y-16*fi))
					  break;
					else if (!IsPersonIDObstructed(ent.id1, ent.x+ent.w+2, ent.y-16*fi))
					{
						flagyy = true;
						break;
					}
				}
				//jump left
				if (flagyy)
				{
					ent.vy = jump_vy;
				}
			}

			if (ent.type == 12)
			{
				if (IsPersonIDObstructed(ent.id1, ent.x, ent.y+1))
				{
					if (ent.frame2 > 1 && ent.frame2 < 5)
					{
					 ent.vx = ent.vx/4;
					}
					else if (ent.frame2 == 1)
					{
					 ent.vx =2*ent.vx/3;
					}


					if (ent.vy != jump_vy &&IsPersonIDObstructed(ent.id1, ent.x, ent.y+1)== true)
					{
						if ((ent.vx == 0 && entities[0].x<ent.x&& IsPersonIDObstructed(ent.id1, ent.x-32, ent.y))
						||(ent.vx == 0 && entities[0].x>ent.x && IsPersonIDObstructed(ent.id1, ent.x+32, ent.y)))
						{
							var flagg = false;
						
							if (entities[0].x < ent.x && ent.x > 32)
							{
								if (GetTile(Math.floor(ent.x/16-2), Math.floor(ent.y/16), 0) == 0)
									flagg = true;
							}
							if (entities[0].x > ent.x && ent.x < GetLayerWidth(0)*16-32)
							{
								if (GetTile(Math.floor(ent.x/16+2), Math.floor(ent.y/16), 0) == 0)
									flagg = true;
							}
							//guard is "stuck" and can't move into the tiny passage
							//randomly swing at the player anyway!
							
							if (g_frames%30 == 0 && Math.random() < 0.66 && flagg == true)
							{
								var randy = Math.random();
								
								if (randy < 0.5)
									ent.dir2 = "attack2";
								else 
									ent.dir2 = "attack1";
							}
						
						}
					
					}
				}
			}


			SetPersonIDLayer(ent.id1, tlayer);

			//set frames according;
		}
		else if (ent.type == 9)
		{
/*			if (!IsEntityOnGround(numy))
				amt_vx = 1;
			else
				amt_vx = 0.25;
*/
			SetPersonIDLayer(ent.id1, 0)
			
			if (entities[0].x < ent.x-1*ent.w && !IsPersonIDObstructed(ent.id1, ent.x-ent.w, ent.y))
			  ent.vx-=amt_vx;
			else if (entities[0].x > ent.x+1*ent.w && !IsPersonIDObstructed(ent.id1, ent.x+ent.w, ent.y))
			  ent.vx+=amt_vx;
			else
			{
			  ent.vx = 0;
			}
			SetPersonIDLayer(ent.id1, ent.layer);

			if ((entities[0].dir2).substr(0,6) != "attack")
			{
				if (entities[0].vx == 0)
					entities[0].dir2 = "idle";
				else
					entities[0].dir2 = "walk";
			}


			if (ent.vx < -max_vx)
			  ent.vx = -max_vx;
			if (ent.vx > max_vx)
			  ent.vx = max_vx;
			  
			var tlayer = GetPersonIDLayer(ent.id1);
			
			SetPersonIDLayer(ent.id1, 0);
			  
			//jump here
			if (IsPersonIDObstructed(ent.id1, ent.x-ent.w-2, ent.y) && IsPersonIDObstructed(ent.id1, ent.x, ent.y+1) && entities[0].x < ent.x-16)
			{
				var flagyy = false;
				
				for (fi=1;fi<jump_tiles;fi++)
				{
					if (IsPersonIDObstructed(ent.id1, ent.x, ent.y-16*fi))
					  break;
					else if (!IsPersonIDObstructed(ent.id1, ent.x-ent.w-2, ent.y-16*fi))
					{
						flagyy = true;
						break;
					}
				}
				//jump left
				if (flagyy)
				{
					ent.vy = jump_vy;
				}
			}
			else if (IsPersonIDObstructed(ent.id1, ent.x+ent.w+2, ent.y)&& IsPersonIDObstructed(ent.id1, ent.x, ent.y+1) && entities[0].x > ent.x+16)
			{
				//jump left
				var flagyy = false;
				
				for (fi=1;fi<jump_tiles;fi++)
				{
					if (IsPersonIDObstructed(ent.id1, ent.x, ent.y-16*fi))
					  break;
					else if (!IsPersonIDObstructed(ent.id1, ent.x+ent.w+2, ent.y-16*fi))
					{
						flagyy = true;
						break;
					}
				}
				//jump left
				if (flagyy)
				{
					ent.vy = jump_vy;
				}
			}
			SetPersonIDLayer(ent.id1, tlayer);
			
			//set frames according;
		}
		else
		{
			ent.vx = 0;

			if ((ent.dir2).substr(0,9) != "legattack")
			{
				if ((ent.dir2).substr(0,7) != "legfall")
				{
					if (IsPersonOnGround(num))
					{
						if ((IsPersonIDObstructed(entities[num].id1, entities[num].x+5, entities[num].y) && entities[0].x > ent.x) || (IsPersonIDObstructed(entities[num].id1, entities[num].x-5, entities[num].y) && entities[0].x < ent.x))
						{
							ent.dir2 = "legidle1";
						}
						else if ((ent.dir2).substr(0,7) != "legwalk")
						{
							if (ent.hp1 > 0)
								ent.dir2 = "legwalk1";
							else
								ent.dir2 = "legwalk2";
						}
					}
				}
				
				SetPersonIDLayer(ent.id1, 0);
				
				if (entities[0].x < ent.x-4*ent.w  && !IsPersonIDObstructed(ent.id1, ent.x-2, ent.y))
				{
					if (ent.frame2 ==3)
						ent.vx=-2;
					else if (ent.frame2 ==4)
						ent.vx=-1;
				}
				else if (entities[0].x > ent.x+4*ent.w&& !IsPersonIDObstructed(ent.id1, ent.x+2, ent.y))
				{
					if (ent.frame2 ==3)
						ent.vx=2;
					else if (ent.frame2 ==4)
						ent.vx=1;
				}
				else if ((ent.dir2).substr(0,9) != "legattack")
				{
					ent.dir2 = "legidle1";
				}
				SetPersonIDLayer(ent.id1, ent.layer);

				if (ent.vx < -max_vx)
					ent.vx = -max_vx;
				if (ent.vx > max_vx)
					ent.vx = max_vx;
			}
			  
			//set frames according;
		}
	}
	
	function Attack()
	{
		if (Math.abs(entities[0].x - ent.x) < entity_misc[entities[num].type].atkw*ent.w+ent.w +entity_misc[entities[num].type].atkw3 && Math.abs(entities[0].y - ent.y) < ent.h*entity_misc[entities[num].type].atkh /*&& entities[0].hp > 0 */&& entities[num].dir2 !="legfall1")
		{
			if ((ent.hp3 > 0&& ent.dir2.substr(0,6) != "attack"))
			{
				var randy = Math.random();
				
				if (randy < 0.3333)
					ent.dir2 = "attack3";
				else if (randy < 0.6666)
					ent.dir2 = "attack2";
				else 
					ent.dir2 = "attack1";

				if (ent.type != 12)
				{
					if (ent.dir2 == "attack1" && ent.hp1 <=0)
					{
						if (ent.hp2 > 0)
							ent.dir2 = "attack2";
						else
							ent.dir2 = "attack3";
					}
					else if (ent.dir2 == "attack2" && ent.hp2 <=0)
					{
						if (ent.hp1 > 0)
							ent.dir2 = "attack1";
						else
							ent.dir2 = "attack3";
					}
				}
			}
			else if (ent.hp3 <=0 && ent.dir2.substr(0,9) != "legattack")
			{
			  ent.frame2 = 0;
			  
				if ((Math.random() < 0.5 && ent.hp1 > 0) || ent.hp2 <=0)
					ent.dir2 = "legattack1";
				else 
					ent.dir2 = "legattack2";
			}
		}
	}
	
	if (ent.type == 1||ent.type == 3||ent.type == 7)
	{
		if ((ent.dir2).substr(0,7) != "legfall")
		{
			if ((ent.dir2).substr(0,6)!="attack" && (ent.dir2).substr(0,9)!="legattack")
			{
					MoveTowardsPlayer(num);
			}
			if (IsPersonOnGround(num)||ent.hp3 > 0)
			{
				Attack();
			}
			if ((ent.dir2).substr(0,6)!="attack" && (ent.dir2).substr(0,9)!="legattack" && sx==ent.x && sy==ent.y)
			{
				if (sounds[44].isPlaying()==false && g_frames%32==0 && Math.random()<0.45 && Math.abs(idle_last_call - g_frames)>64 && entities[0].hp > 0)
				{
					idle_last_call = g_frames;
					PlaySound(44,true);
				}
			}
		}
	}
	else if (ent.type == 9)
	{
		if ((ent.dir2).substr(0,6)!="attack")
		{
			MoveTowardsPlayer(num);
		}
		if (IsPersonOnGround(num))
		{
			Attack();
		}
		if ((ent.dir2).substr(0,6)!="attack")// && sx==ent.x && sy==ent.y)
		{
			if (sounds[44].isPlaying()==false && g_frames%32==0 && Math.random()<0.45 && Math.abs(idle_last_call - g_frames)>64 && entities[0].hp > 0)
			{
				idle_last_call = g_frames;
				PlaySound(44,true);
			}
		}
	}
	else if (ent.type == 12)
	{

		
	
		if ((ent.dir2).substr(0,6)!="attack")
		{
			MoveTowardsPlayer(num);

			if (entities[0].x < ent.x)
				ent.dir = -1;
			else
				ent.dir = 1;
		}
		if (IsPersonOnGround(num))
		{
			Attack();
			if (ent.dir2 == "attack3")
			{
				if (ent.frame2 == 1 || ent.frame2 == 2)
				{
					ent.vx+=ent.dir*3;

					if (ent.vx < -max_vx)
						ent.vx = -max_vx;
					if (ent.vx > max_vx)
						ent.vx = max_vx;
				}
			}
		}
		if ((ent.dir2).substr(0,6)!="attack")// && sx==ent.x && sy==ent.y)
		{
			if (sounds[44].isPlaying()==false && g_frames%32==0 && Math.random()<0.45 && Math.abs(idle_last_call - g_frames)>64 && entities[0].hp > 0)
			{
				idle_last_call = g_frames;
				PlaySound(44,true);
			}
		}
	}
	else if (ent.type == 2)
	{
		//do nothing.
	}
	else if (ent.type == 5)
	{
		ent.vx = 0;
		if (entities[0].x < ent.x)
			ent.dir = -1;
		else if (entities[0].x > ent.x)
			ent.dir = 1;

		if ((ent.dir2).substr(0,7) != "legfall")
		{
			if (IsPersonOnGround(num)||ent.hp3 > 0)
			{
				Attack();
			}
			if ((ent.dir2).substr(0,6)!="attack" && (ent.dir2).substr(0,9)!="legattack" && sx==ent.x && sy==ent.y)
			{
				if (sounds[44].isPlaying()==false && g_frames%32==0 && Math.random()<0.45 && Math.abs(idle_last_call - g_frames)>64 && entities[0].hp > 0)
				{
					idle_last_call = g_frames;
					PlaySound(44,true);
				}
			}
		}
	}
	else if (ent.type == 8)
	{	
		if (((Math.abs(entities[0].y-entities[num].y)<30 && Math.abs(entities[0].x-entities[num].x)<60) || entities[num].hp2 <= 0)  && entities[num].attackcount < 1 && entities[num].nohit != true)
		{
			entities[num].attackcount = 20;
			entities[num].dir2 = "attack1";
			if (entities[num].hp2 > 0)
				PlaySound(60,true,0.90);
		}
		if (entities[0].x < entities[num].x)
			entities[num].dir = -1;
		else if (entities[0].x > entities[num].x)
			entities[num].dir = 1;
	
		if (entities[0].y < entities[num].y - 10)
			entities[num].wantup = 1;//want to go up;
		else if (entities[0].y > entities[num].y + 17)
			entities[num].wantup = -1;//want to go down;
		else
			entities[num].wantup = 0;//where I am is pretty good;
	
		if (entities[num].nohit != true)
		{
			if (entities[0].x < entities[num].x-20)
				entities[num].vx-=amt_vx/4;
			if (entities[0].x > entities[num].x+20)
				entities[num].vx+=amt_vx/4;

			if (entities[num].vx > max_vx)
				entities[num].vx = max_vx;
			else if (entities[num].vx < -max_vx)
				entities[num].vx = -max_vx;
		
			g_layer = GetPersonIDLayer(entities[i].id1);
			SetPersonIDLayer(entities[i].id1, 0);
	
			if (entities[num].vx < 0 && IsPersonIDObstructed(entities[num].id1, entities[num].x-1, entities[num].y) == true)
				entities[num].vx = 0;
			else if (entities[num].vx > 0 && IsPersonIDObstructed(entities[num].id1, entities[num].x+1, entities[num].y) == true)
				entities[num].vx = 0;
	
			SetPersonIDLayer(entities[i].id1, g_layer);
		}
		else
		{
			entities[num].vx = entities[num].avx;
			entities[num].wantup = entities[num].avy;
		
			if (GetTile(Math.floor(entities[num].x/GetTileWidth()), Math.floor(entities[num].y/GetTileHeight()), 1) == 0 &&
					GetTile(Math.floor(entities[num].x/GetTileWidth()), Math.floor(entities[num].y/GetTileHeight())+1, 1) == 0 &&
					GetTile(Math.floor(entities[num].x/GetTileWidth()), Math.floor(entities[num].y/GetTileHeight())-1, 1) == 0 &&
					GetTile(Math.floor(entities[num].x/GetTileWidth())+1, Math.floor(entities[num].y/GetTileHeight()), 1) == 0 &&
					GetTile(Math.floor(entities[num].x/GetTileWidth())-1, Math.floor(entities[num].y/GetTileHeight()), 1) == 0 &&
					GetTile(Math.floor(entities[num].x/GetTileWidth())+1, Math.floor(entities[num].y/GetTileHeight())+1, 1) == 0 &&
					GetTile(Math.floor(entities[num].x/GetTileWidth())-1, Math.floor(entities[num].y/GetTileHeight())+1, 1) == 0 &&
					GetTile(Math.floor(entities[num].x/GetTileWidth())+1, Math.floor(entities[num].y/GetTileHeight())-1, 1) == 0 &&
					GetTile(Math.floor(entities[num].x/GetTileWidth())-1, Math.floor(entities[num].y/GetTileHeight())-1, 1) == 0 
			)
			{
				entities[num].nohit = false;
			}
		}
		
		if (entities[num].frame2 > 1 && entities[num].frame2 < 6)
		{
			if (g_frames%frameupdate == 0 && entities[num].frame2 == 2)
			{
				PlaySound(113,true);
			}
			if (entities[num].frame2 > 2 && entities[num].frame2 < 5)
				entities[num].vy = -2.5-entities[num].wantup/2;
			else
				entities[num].vy = -1.5-entities[num].wantup/2;

		}
	}
	else if (ent.type == 10)
	{	
		if (((Math.abs(entities[0].y-entities[num].y)<30 && Math.abs(entities[0].x-entities[num].x)<60) || entities[num].hp2 <= 0)  && entities[num].attackcount < 1 && entities[num].nohit != true)
		{
			entities[num].attackcount = 10;
			entities[num].dir2 = "attack1";
		}
		if (entities[0].x < entities[num].x)
			entities[num].dir = -1;
		else if (entities[0].x > entities[num].x)
			entities[num].dir = 1;
	
		if (entities[0].y < entities[num].y - 10)
			entities[num].wantup = 1;//want to go up;
		else if (entities[0].y > entities[num].y + 17)
			entities[num].wantup = -1;//want to go down;
		else
			entities[num].wantup = 0;//where I am is pretty good;
	
		if (entities[num].nohit != true)
		{
			if (entities[0].x < entities[num].x-20)
				entities[num].vx-=amt_vx/4;
			if (entities[0].x > entities[num].x+20)
				entities[num].vx+=amt_vx/4;

			if (entities[num].vx > max_vx)
				entities[num].vx = max_vx;
			else if (entities[num].vx < -max_vx)
				entities[num].vx = -max_vx;
		
			g_layer = GetPersonIDLayer(entities[i].id1);
			SetPersonIDLayer(entities[i].id1, 0);
	
			if (entities[num].vx < 0 && IsPersonIDObstructed(entities[num].id1, entities[num].x-1, entities[num].y) == true)
				entities[num].vx = 0;
			else if (entities[num].vx > 0 && IsPersonIDObstructed(entities[num].id1, entities[num].x+1, entities[num].y) == true)
				entities[num].vx = 0;
	
			SetPersonIDLayer(entities[i].id1, g_layer);
		}
		else
		{
			entities[num].vx = entities[num].avx;
			entities[num].wantup = entities[num].avy;
		
			if (GetTile(Math.floor(entities[num].x/GetTileWidth()), Math.floor(entities[num].y/GetTileHeight()), 1) == 0 &&
					GetTile(Math.floor(entities[num].x/GetTileWidth()), Math.floor(entities[num].y/GetTileHeight())+1, 1) == 0 &&
					GetTile(Math.floor(entities[num].x/GetTileWidth()), Math.floor(entities[num].y/GetTileHeight())-1, 1) == 0 &&
					GetTile(Math.floor(entities[num].x/GetTileWidth())+1, Math.floor(entities[num].y/GetTileHeight()), 1) == 0 &&
					GetTile(Math.floor(entities[num].x/GetTileWidth())-1, Math.floor(entities[num].y/GetTileHeight()), 1) == 0 &&
					GetTile(Math.floor(entities[num].x/GetTileWidth())+1, Math.floor(entities[num].y/GetTileHeight())+1, 1) == 0 &&
					GetTile(Math.floor(entities[num].x/GetTileWidth())-1, Math.floor(entities[num].y/GetTileHeight())+1, 1) == 0 &&
					GetTile(Math.floor(entities[num].x/GetTileWidth())+1, Math.floor(entities[num].y/GetTileHeight())-1, 1) == 0 &&
					GetTile(Math.floor(entities[num].x/GetTileWidth())-1, Math.floor(entities[num].y/GetTileHeight())-1, 1) == 0 
			)
			{
				entities[num].nohit = false;
			}
		}
		
		if (entities[num].frame2 > 1 && entities[num].frame2 < 6)
		{
			if (entities[num].frame2 > 2 && entities[num].frame2 < 5)
				entities[num].vy = -2.5-entities[num].wantup/2;
			else
				entities[num].vy = -1.5-entities[num].wantup/2;

		}
	}
}

var invince_max = 25;

function SpriteAttack(num, bypass, damage, noblood, stam_damage)
{
	if ((Math.abs(entities[0].x - entities[num].x) < entities[num].w*entity_misc[entities[num].type].atkw2*1.5 && Math.abs(entities[0].y - entities[num].y) < entities[num].h*entity_misc[entities[num].type].atkh)||bypass == true)
	{
		if (stam_damage == true)
		{
			RemovePlayerStam(damage);
			entities[0].invince = Math.floor(invince_max/2);
		}
		else if (!(entities[0].invince > 0 || entities[0].hp <= 0 || oblit_shield > 0))
		{
			if (bypass == true)
			{
				RemovePlayerHP(damage);
			}
			else
			{
				if (entities[num].type == 1)
				{
					RemovePlayerHP(0.75);
				}
				if (entities[num].type == 3)
				{
					RemovePlayerHP(1.5);
				}
				if (entities[num].type == 5)
				{
					RemovePlayerHP(2.5);
				}
				if (entities[num].type == 6)
				{
					RemovePlayerHP(1.5);
				}
				if (entities[num].type == 7)
				{
					RemovePlayerHP(1.5);
				}
				if (entities[num].type == 12)
				{
					RemovePlayerHP(2);
				}
			}
	
			entities[0].invince = invince_max;

			if (noblood != true)
			{
				if (entities[0].x < entities[num].x)
					SpawnBlood(entities[0].x,entities[0].y,-3*b_slowx+Math.random()*2-1,-4*b_slowy-Math.random()-1,3, "w");		
				else
					SpawnBlood(entities[0].x,entities[0].y,3*b_slowx+Math.random()*2-1,-4*b_slowy-Math.random()-1,3, "w");		
			}
			if (entities[num].hp < 0)
			{
				//do something
			} 
		}
		
	}
}

function SpriteShoveAttack(num, bypass, damage, noblood, stam_damage)
{
	if ((Math.abs(entities[0].x - entities[num].x) < entities[num].w*entity_misc[entities[num].type].atkw2*1.5 && Math.abs(entities[0].y - entities[num].y) < entities[num].h*entity_misc[entities[num].type].atkh)||bypass == true)
	{
		if (stam_damage == true)
		{
			RemovePlayerStam(damage);
			entities[0].invince = Math.floor(invince_max/2);
		}
		else if (!(entities[0].invince > 0 || entities[0].hp <= 0 || oblit_shield > 0))
		{
			if (bypass == true)
			{
				RemovePlayerHP(damage);
			}
			else
			{
				if (entities[num].type == 1)
				{
					RemovePlayerHP(0.75);
				}
				if (entities[num].type == 3)
				{
					RemovePlayerHP(1.5);
				}
				if (entities[num].type == 5)
				{
					RemovePlayerHP(2.5);
				}
				if (entities[num].type == 6)
				{
					RemovePlayerHP(1.5);
				}
				if (entities[num].type == 7)
				{
					RemovePlayerHP(1.5);
				}
				if (entities[num].type == 12)
				{
					RemovePlayerHP(2);
				}
				
//			RemovePlayerHP(1.5);
				entities[0].vx = entities[num].dir*4;
				entities[0].vx_bypass = true;
				entities[0].vy = -3;
				if (IsPersonOnGround(0) == true)
					entities[0].y--;	
			}
	
			entities[0].invince = invince_max;

			if (noblood != true)
			{
				if (entities[0].x < entities[num].x)
					SpawnBlood(entities[0].x,entities[0].y,-3*b_slowx+Math.random()*2-1,-4*b_slowy-Math.random()-1,3, "w");		
				else
					SpawnBlood(entities[0].x,entities[0].y,3*b_slowx+Math.random()*2-1,-4*b_slowy-Math.random()-1,3, "w");		
			}
			if (entities[num].hp < 0)
			{
				//do something
			} 
		}
		
	}
}


function BossSpriteShoveAttack(bypass, damage, noblood, stam_damage)
{
	if ((Math.abs(entities[0].x - boss2.x-boss2.axoff) < boss2.attackw && Math.abs(entities[0].y - boss2.y-boss2.ayoff) < boss2.attackh ) ||bypass == true)
	{
		if (stam_damage == true)
		{
			RemovePlayerStam(damage);
			entities[0].invince = Math.floor(invince_max/2);
		}
		else if (!(entities[0].invince > 0 || entities[0].hp <= 0 || oblit_shield > 0))
		{
			if (bypass == true)
			{
				RemovePlayerHP(damage);
			}
			else
			{
				RemovePlayerHP(damage);

				entities[0].vx = -4;
				entities[0].vx_bypass = true;
				entities[0].vy = -3;
				if (IsPersonOnGround(0) == true)
					entities[0].y--;	
			}
	
			entities[0].invince = invince_max;

			if (noblood != true)
			{
				if (entities[0].x < boss2.x+boss2.axoff)
					SpawnBlood(entities[0].x,entities[0].y,-3*b_slowx+Math.random()*2-1,-4*b_slowy-Math.random()-1,3, "w");		
				else
					SpawnBlood(entities[0].x,entities[0].y,3*b_slowx+Math.random()*2-1,-4*b_slowy-Math.random()-1,3, "w");		
			}
			if (entities[0].hp < 0)
			{
				//do something
			} 
		}
		
	}
}

function HitThePlayer()
{

}

function FlameHurtPlayer(damage)
{
	if (!(entities[0].invince > 0 || entities[0].hp <= 0 || oblit_shield > 0))
	{
		RemovePlayerHP(damage);
		entities[0].invince = invince_max;
	}
}

function RailyHurtPlayer(damage)
{
	if (!(entities[0].invince > 0 || entities[0].hp <= 0 || oblit_shield > 0))
	{
		RemovePlayerHP(damage);
		entities[0].invince = 1;//invince_max;
	}
}


function RemovePlayerStam(amt)
{
	entities[0].stam-=amt;
	
	if (entities[0].stam < 0)
	{
		entities[0].stam = 0;
		g_isrunning = false;
	}
}

function RemovePlayerHP(amt, armour)
{
  var dam_res = 1;
	  
	if (g_chosen_diff == 0)
		amt = amt*0.66;
	else if (g_chosen_diff == 2)
		amt = amt*1.33;
	  
	//Let armour take some of it:
	if (ammo_array[11].curammo > 0 && armour != false)
	{
		dam_res *=(1-0.75*ammo_array[11].curammo/ammo_array[11].maxammo);
		ammo_array[11].curammo -= amt*dam_res;
		if (ammo_array[11].curammo < 0)
			ammo_array[11].curammo = 0;
	}
	  
	if (adren_timer > 0)
	  dam_res*=0.5;

	entities[0].hp -= amt*dam_res;
	
	if (g_isrunning)
	{
		entities[0].stam-=amt*dam_res*3;
		if (entities[0].stam <= 0)
		{
			entities[0].stam = 0;
			g_isrunning = false;
		}
	}

	//remove armour 
}

var drone_damage = 1;

function DoDroneAttack(x, y, dir, ent, umber)
{
	if (umber == true)
		SpawnExplosion(x+dir*16,y, drone_damage, ent, 0.5, 2,dir*7,Math.random()*3-1.5);
	else
		SpawnExplosion(x,y, drone_damage, ent, 0.5, 2,dir*7,0);
}


function DoJellyAttack(x, y, dir, ent, umber)
{
	if (g_frames % (2*frameupdate) == 0)
		PlaySound(92,true,0.40);

	var sangle = Math.random()*3.14159*2;
	var currx = x;
	var curry = y;
	var nx = x;
	var ny = y;
	var lx = x;
	var ly = y;

	var tile = GetTile(Math.floor(x/GetTileWidth()),Math.floor(y/GetTileWidth()), 0);
	var angle = sangle;
	var speed = 5;
	
	if (tile == 1 || tile == 2)
		return;
	for (d=0;d<4;d++)
	{
		angle = sangle+Math.random()*3.14159-3.14159/2;
		lx = nx;
		ly = ny;
		
		for (k=0;k<4;k++)
		{
			nx = currx+speed*Math.cos(angle);
			ny = curry+speed*Math.sin(angle);
			tile = GetTile(Math.floor(nx/GetTileWidth()),Math.floor(ny/GetTileWidth()), 0);
			currx = nx;
			curry = ny;
			if (tile == 1 || tile == 2)
			{
				break;
			}
		}
		SpawnRailgun(lx,ly,nx, ny,0,0.2,true);

		if (tile == 1 || tile == 2)
		{
			break;
		}
	}

}


function DoJellyAttack2(x, y, tx,ty)
{
	PlaySound(92,true,0.40);

	var currx = x;
	var curry = y;
	var nx = x;
	var ny = y;
	
	var tangle;

	var tile = GetTile(Math.floor(x/GetTileWidth()),Math.floor(y/GetTileWidth()), 0);
	var angle = 0;
	var speed = 30;
	
	if (tile == 1 || tile == 2)
		return;
		
	for (k=0;k<10;k++)
	{
		tangle = Math.atan2(ty-curry, tx-currx);
		angle = tangle+Math.random()*3.14159/4-3.14159/8;

		for (d=0;d<5;d++)
		{
			nx = currx+speed*Math.cos(angle);
			ny = curry+speed*Math.sin(angle);

			tile = GetTile(Math.floor(nx/GetTileWidth()),Math.floor(ny/GetTileWidth()), 0);
			if (tile == 1 || tile == 2)
			{
				break;
			}
		}

		SpawnRailgun(currx,curry,nx, ny,0,0.4,true);

		if (tile == 1 || tile == 2)
		{
			return;
		}

		currx = nx;
		curry = ny;

		
		if (Math.abs(currx-tx)<speed && Math.abs(curry-ty)<speed)
		{
			SpawnRailgun(currx,curry,tx, ty,0,0.2,true);
			return;
		}
	}

}

function EntitySeesPlayer(num)
{
	var sx = entities[0].x;
	var sy = entities[0].y;

	var tx = entities[num].x - entities[0].x;
	var ty = entities[num].y - entities[0].y;

	var length = Math.sqrt(tx*tx+ty*ty);

	var fx = tx*3.0/length;
	var fy = ty*3.0/length;

	for (aj=0;aj<length/3.0;aj++)
	{
		hx=fx*aj+sx;
		hy=fy*aj+sy;

		if (CheckObstructTile(hx,hy))
		{
			break;
		}
	}
	if (aj < length/3.0)
	  return false;
	return true;
}


function DoWaveEnemyDamage(enemy, owner, damage)
{
	SpawnExplosion(entities[enemy].x,entities[enemy].y, damage, owner, 1, 1,0,0, enemy)

}

var sup_sword_dam = 3;
function DoSwingAttack(x,y,dir,damage, owner, skiptosuper)
{
	var swing_w = 34;
	var swing_h = 30;
	
	var tw = 0;
	if (skiptosuper != true)
	{
		for (dsa = 0;dsa < max_entities; dsa++)
		{
			tw = entities[dsa].w;
			if (entities[dsa].type == 9)
				tw+=8;
			if (entities[dsa].hp > 0 && dsa != owner&& entities[dsa].ambush == false && entities[dsa].nohit != true)
			{
				if (Math.abs(entities[dsa].x - (x+swing_w*dir))<swing_w+tw/2 && Math.abs(entities[dsa].y- y)<swing_h)
				{
					SpawnExplosion(entities[dsa].x,entities[dsa].y, damage, owner, 1, 1,dir*3,-2, dsa);
//					break;
					//hit entity
				}
			}
		}
	}
	
	if (g_boss_active == 1)
	{
		//
		if (Math.abs(boss.hx-x)<24 && Math.abs(boss.hy-y)<24)
		{
			DoBossHp1Damage(damage);
		}

		for (k=0;k<max_boss_colours;k++)
		{
			if (Math.abs(boss.x+bcolours[k].cx-x-swing_w*dir)<swing_w+16 && Math.abs(boss.y+bcolours[k].cy-y) < swing_h+16 && bcolours[k].hp > 0 && boss.scream <= 0)
			{
				MakeBossBleed(k, damage);
				return;
			}
		}
	
	}
	
	if (g_boss_active == 2)
	{
/*
		font.drawText(0,0, 
		if (Math.abs(boss.y+boss2.ayoff-y)<boss2.ah)
			FlipScreen();*/
		if (Math.abs(boss2.x+boss2.axoff-(x+swing_w*dir))<boss2.aw+swing_w && Math.abs(boss2.y+boss2.ayoff-y)<boss2.ah)
		{
//			FlipScreen();
			
			DoBoss2Hp1Damage(damage,x,y);
		}
	}


	
	var px;
	var py;
	var tempcolor;
	var super_flag = false
	//check for supernaut
	if (g_supernaut_active == 2)
	{
		if (x+swing_w*dir>naut2.x+naut2.hx && x+swing_w*dir<naut2.x+naut2.hx+naut2.width-1)
		{
			if (y>naut2.y+naut2.hy+swing_h && y<naut2.y+naut2.hy+naut2.height+swing_h-1)
			{
				px = x+swing_w*dir*2-naut2.x-naut2.hx;
				py = y-naut2.y-naut2.hy-swing_h;
				
				if (px < naut2_head_blood.width && px > 0 && py < naut2_head_blood.height && py > 0)
				{
					if (naut2_head_blood.getPixel(px,py).alpha > 0)
					{
						tempcolor = naut2_head_mask.getPixel(px, py);
						DoSupernautDamage(damage*sup_sword_dam, "head", tempcolor);
						super_flag = true;
					}
				}
			}
			if (y>naut2.y+naut2.hy-swing_h && y<naut2.y+naut2.hy+naut2.height-swing_h-1 && super_flag == false)
			{
				px = x+swing_w*dir*2-naut2.x-naut2.hx;
				py = y-naut2.y-naut2.hy+swing_h;
				
				if (px < naut2_head_blood.width && px > 0 && py < naut2_head_blood.height && py > 0)
				{
					if (naut2_head_blood.getPixel(px,py).alpha > 0)
					{
						tempcolor = naut2_head_mask.getPixel(px, py);
						DoSupernautDamage(damage*sup_sword_dam, "head", tempcolor);
						super_flag = true;
					}
				}
			}
		}
		if (x+swing_w*dir>naut2.x+naut2.mx && x+swing_w*dir<naut2.x+naut2.mx+naut2.width-1)
		{
			if (y>naut2.y+naut2.my+swing_h && y<naut2.y+naut2.my+naut2.height+swing_h-1)
			{
				px = x+swing_w*dir*2-naut2.x-naut2.mx;
				py = y-naut2.y-naut2.my-swing_h;
				if (px < naut2_mouth_blood_1.width && px > 0 && py < naut2_mouth_blood_1.height && py > 0)
				{
					if (naut2.openmouth == 0)
					{
						if (px < naut2_head_blood.width && px > 0 && py < naut2_head_blood.height && py > 0)
						{
							if (naut2_mouth_blood_1.getPixel(px,py).alpha > 0 && super_flag == false)
							{
								tempcolor = naut2_mouth_mask_1.getPixel(px, py);
								DoSupernautDamage(damage*sup_sword_dam, "mouth_1", tempcolor);
								super_flag = true;
							}
						}
					}
					if (naut2.openmouth == 1)
					{
						if (naut2_mouth_blood_2.getPixel(px,py).alpha > 0 && super_flag == false)
						{
							tempcolor = naut2_mouth_mask_2.getPixel(px, py);
							DoSupernautDamage(damage*sup_sword_dam, "mouth_1", tempcolor);
							super_flag = true;
						}
					}
				}
			}
			if (y>naut2.y+naut2.my-swing_h && y<naut2.y+naut2.my+naut2.height-swing_h-1)
			{
				px = x+swing_w*dir*2-naut2.x-naut2.mx;
				py = y-naut2.y-naut2.my+swing_h;
				if (px < naut2_mouth_blood_1.width && px > 0 && py < naut2_mouth_blood_1.height && py > 0)
				{
					if (naut2.openmouth == 0)
					{
						if (naut2_mouth_blood_1.getPixel(px,py).alpha > 0 && super_flag == false)
						{
							tempcolor = naut2_mouth_mask_1.getPixel(px, py);
							DoSupernautDamage(damage*sup_sword_dam, "mouth_1", tempcolor);
							super_flag = true;
						}
					}
					if (naut2.openmouth == 1)
					{
						if (naut2_mouth_blood_2.getPixel(px,py).alpha > 0 && super_flag == false)
						{
							tempcolor = naut2_mouth_mask_2.getPixel(px, py);
							DoSupernautDamage(damage*sup_sword_dam, "mouth_1", tempcolor);
							super_flag = true;
						}
					}
				}
			}
		}
	}
	if (super_flag == true)
	{
		for (lp =0;lp<4;lp++)
			SpawnBloodSplat(x+swing_w*dir-5+10*Math.random(),y-5+10*Math.random(),4*Math.random()-1,-4-2*Math.random(), 10);
	}
		return true;
	return false;
}