var g_fall_counter = 0;
var adren_run_start = 1;

var g_firedamage = 0.5

var g_resting_timer = 0;
var g_resting_timer_max = 90;
function PlayerResting()
{
	if (entities[0].vx == 0 && entities[0].vy == 0 && entities[0].invince<=0 && !IsEntityInWater(0))
	{
		g_resting_timer++;
		
		if (g_resting_timer > g_resting_timer_max && adren_timer >= 0)
		{
			g_resting_timer = g_resting_timer_max;
			return true;
		}
	
	}
	else
		g_resting_timer = 0;
	return false;
}




function UpdatePlayer()
{
	var keyy;
	
	if (key_play == false)
	{
		while(AreKeysLeft())
		{
			keyy = GetKey();
			
			if (keyy == KEY_F9)
			{
				if (key_record == false)
				{
					key_record = true;
				}
				else if (key_record == true)
				{
					key_record = false;
					KeySaveRecord();
				}
			}
			else if (keyy == KEY_P)
			{
				LoadPlayKey();
				key_play = true;
				KeyPlayNow();
				g_frames = key_array[0].frame_st;
				//play recording:
					
				
				UpdatePlayerFrame(0);
				return;
			}
		}
	
	}
	if (key_play == false)
	{
		var flaggy = false;
		i=0;
		
			if (g_supernaut_active == 1)
			{
				var ntx = naut1.bx+48
				if (entities[0].x > ntx)
				{
//					entities[0].x = naut1.bx+48;
//					entities[0].vx = 0;
		
					entities[0].vx-= (entities[0].x - ntx)/4;
	
				}
//				FlipScreen();
			}
			if (g_supernaut_active == 2)
			{
				var ntx = naut2.bx+128+32;
				if (entities[0].x < ntx)
				{
//					entities[0].x = naut1.bx+48;
//					entities[0].vx = 0;
		
					entities[0].vx+= (ntx-entities[0].x)/4;
	
				}
//				FlipScreen();
			}
			var friction = 2.00;

			if (entities[0].invince > 0)
			  entities[0].invince--;
 			//add gravity;
			if (entities[i].vy < grav_max && !IsPersonOnGround(i))
			{
				entities[i].vy+=grav_amt;

				if (IsEntityInWater(i))
				{
					if (g_fall_counter>0)
					{
						g_fall_counter-=2;
						
						if (g_fall_counter < 0)
							g_fall_counter = 0;
					}
				
				}

			}
			else if (!IsPersonOnGround(i))
			{
				g_fall_counter++;
				if (g_current_level > 16)
					g_fall_counter = 0;
				
				
			}
			else
			{
				if (g_fall_counter > 0)
				{
				  RemovePlayerHP(g_fall_counter/5, false);
				  
				  if (g_fall_counter < 3)
						PlaySound(47, true);
					else if (g_fall_counter < 6)
						PlaySound(119, true);
					else if (g_fall_counter < 9)
						PlaySound(120, true);
					else
						PlaySound(118, true);
				}
				g_fall_counter=0;
			}

				

			sx = entities[i].x;
			sy = entities[i].y;
			
			var watag = -1;

			if (IsEntityInWater(i))
			{
				InWater(i);
				PlayAWaterSound();
			}
			else
				watag++;
			
			
			entities[i].x += entities[i].vx;
			entities[i].y += entities[i].vy;
			
			t_layer = GetPersonLayer(entities[i].name);
			SetPersonLayer(entities[i].name, 0);
			
			flaggy = true;
			if (IsPersonObstructed(entities[i].name, entities[i].x, entities[i].y))
			{
				flaggy = false;
				speed = Math.sqrt(entities[i].vx*entities[i].vx + entities[i].vy*entities[i].vy);
				
				for (k=speed-1;k>=0;k--)
				{
					entities[i].x = sx+entities[i].vx*(k/speed);
					entities[i].y = sy+entities[i].vy*(k/speed);
				
					if (!IsPersonObstructed(entities[i].name, entities[i].x, entities[i].y))
						break;
				}
				if (IsPersonObstructed(entities[i].name, entities[i].x, entities[i].y))
				{
					entities[i].x = sx;
					entities[i].y = sy;
					g_fall_counter=0;
				}
			}

			if (IsEntityInWater(i) && watag == 0 && entities[i].vy > 3)
			{
				PlaySound(128,true);
			}

			if (IsPersonObstructed(entities[i].name, entities[i].x, entities[i].y+1) ||
					IsPersonObstructed(entities[i].name, entities[i].x, entities[i].y-1))
			{
				if (entities[0].vy > 3)
					PlaySound(33);

			  entities[i].vy = 0;
				flaggy = true;
			}
			if (IsPersonObstructed(entities[i].name, entities[i].x+1, entities[i].y) ||
				  IsPersonObstructed(entities[i].name, entities[i].x-1, entities[i].y))
			{
			  entities[i].vx = 0;
				flaggy = true;
			}

			if ((IsPersonObstructed(entities[i].name, entities[i].x+1, entities[i].y+1)||
				  IsPersonObstructed(entities[i].name, entities[i].x+1, entities[i].y-1)||
				  IsPersonObstructed(entities[i].name, entities[i].x-1, entities[i].y-1)||
				  IsPersonObstructed(entities[i].name, entities[i].x-1, entities[i].y+1)
				  )&&flaggy == false)
			{
			  entities[i].vx = 0;
			  if (!IsPersonObstructed(entities[i].name, entities[i].x, entities[i].y+1))
					entities[i].y++;
				
			}
			  
			  
			  
			  
			if (entities[i].vx < 0 && IsEntityOnGround(i))
			{
				entities[i].vx+=friction/4;
				if (entities[i].vx > 0)
					entities[i].vx = 0;
			}
			else if (entities[i].vx > 0 && IsEntityOnGround(i))
			{
				entities[i].vx-=friction/4;
				if (entities[i].vx < 0)
					entities[i].vx = 0;
			}
	

	  	SetPersonLayer(entities[i].name, t_layer);
	}	
	else
	{
		KeyPlayNow();
		if (entities[0].invince > 0)
		  entities[0].invince--;

		//play recording:
	}
	i=0;
			//update character info in the map
			SetPersonX(entities[i].name, entities[i].x);
			SetPersonY(entities[i].name, entities[i].y);

			//update the legs
			t_string = (entities[i].name);
			t_string = t_string.substr(0,t_string.length-5)+"legs";
	
			SetPersonX(t_string, entities[i].x);
			SetPersonY(t_string, entities[i].y);

			//check to see if the player is on a fire tile
			var pfirex = Math.floor(entities[i].x/GetTileWidth());
			var pfirex2 = Math.floor((entities[i].x-8)/GetTileWidth());
			var pfirey = Math.floor(entities[i].y/GetTileHeight());
		
		
			var firetile = GetTile(pfirex2, pfirey, GetNumLayers()-1);
			if (firetile > 690 && firetile < 734)
				SpriteAttack(0, true, g_firedamage, true);
			firetile = GetTile(pfirex, pfirey, GetNumLayers()-2);
			if (firetile > 690 && firetile < 734)
				SpriteAttack(0, true, g_firedamage, true);


//			font.drawText(0,0, pfirex+" "+pfirey+" "+firetile);
	//		FlipScreen();

	if (key_record == true)
	{
			KeyRecordNow();
	}


			UpdatePlayerFrame(0);


}

var g_f9_pressy = false;

var chaingun_timer = 0;
var chaingun_timer_max = 15;

var railgun_alt_charge = 9;
var railgun_alt_charge_max =9;


function UpdatePlayerFrame(num)
{
	var tempytemptemp;
	SetPersonIDDirection(entities[num].id12, "blade1"+entities[num].bdir);


	var objy;
	
	if (IsMouseButtonPressed(CMOUSE_LEFT) == false)
	{
		chaingun_timer = 0;
		sounds[62].stop();

	}
	
	

	//whatever
	if (entities[num].vx > 0)
		entities[num].dir = 1;
	if (entities[num].vx < 0)
		entities[num].dir = -1;
		
		var cx = MapToScreenX(0, entities[0].x);
		var cy = MapToScreenY(0, entities[0].y);

		if (entities[0].dir == -1)
		{
			angle = Math.atan2(GetMouseY()-cy,GetMouseX()-cx)+3.14159;
			
			angle/=(2*3.14159);
			
			angle*=16;
			angle+=0.5;
			angle = Math.floor(angle);
		}
		else
		{
			angle = Math.atan2(GetMouseY()-cy,cx-GetMouseX())+3.14159;
			
			angle/=(2*3.14159);
			
			angle*=16;
			angle+=0.5;
			angle = Math.floor(angle);
		}

	  entities[0].frame1 = angle;
	  
		
		
		
		
		
	var face = "east"
	var facedir = (-1*entities[num].dir+1)/2;
	
	SetPersonIDHorizontalFlip(entities[num].id1, facedir);
	SetPersonIDHorizontalFlip(entities[num].id2, facedir);
	SetPersonIDHorizontalFlip(entities[num].id3, facedir);
	SetPersonIDHorizontalFlip(entities[num].id4, facedir);
	SetPersonIDHorizontalFlip(entities[num].id5, facedir);
	SetPersonIDHorizontalFlip(entities[num].id6, facedir);
	SetPersonIDHorizontalFlip(entities[num].id7, facedir);
	SetPersonIDHorizontalFlip(entities[num].id8, facedir);
	SetPersonIDHorizontalFlip(entities[num].id9, facedir);
	SetPersonIDHorizontalFlip(entities[num].id10, facedir);
	if (GetMouseX()< MapToScreenX(0, entities[0].x))
	{
		SetPersonIDHorizontalFlip(entities[num].id11, 1);
		SetPersonIDHorizontalFlip(entities[num].id12, 1);
	}
	else 
	{
		SetPersonIDHorizontalFlip(entities[num].id11, 0);
		SetPersonIDHorizontalFlip(entities[num].id12, 0);
	}
	
	if (entities[num].hp<=0 || g_isrunning ==true)
	{
		SetPersonIDHorizontalFlip(entities[num].id11, facedir);
		SetPersonIDHorizontalFlip(entities[num].id12, facedir);
	
	}

//	if (entities[num].dir == -1)
//		SetPersonIDHorizontalFlip(entities[num].id1, 1);

	if (IsPersonOnGround(num) == true )
	{
		if (entities[0].dir2 == "punch"||entities[0].dir2 == "grenade")
		{
			/*
			if (g_frames % frameupdate == 0)
			{
				entities[0].frame2++;
				if (entities[0].frame2 == 3)
				{
				//fire off grenade
				}
				else if (entities[0].frame2 == 9)
				{
					entities[num].dir2 = "idle";
					entities[num].frame2 = 0;
				}
			}
			*/
		}
		else if (entities[num].vx != 0)
		{
			if (entities[num].dir2 != "walk")
			{
				if (entities[num].dir2 == "idle")
					entities[num].frame2 = entities[num].frame2*2;
				else
					entities[num].frame2 = 0;
				
				entities[num].dir2 = "walk";
			}
			else
			{
				if (g_frames % frameupdate == 0)
					entities[num].frame2++;
			}
		}
		else if (entities[num].dir2 != "idle")
		{
			if (entities[num].dir2 == "walk")
				entities[num].frame2 = Math.ceil(entities[num].frame2/2);
			else
				entities[num].frame2 = 0;

			entities[num].dir2 = "idle";
		}
		else if (entities[num].dir2 == "idle" && g_frames%2 == 0 && entities[num].frame2 != 0)
		{
				if (entities[num].frame2 >4)
				entities[num].frame2++;
				else
				entities[num].frame2--;
		
		}
	}
	else
	{
			entities[num].dir2 = "jump";
			if (entities[0].vy < 0)
				entities[num].frame2 = 0;
			else
				entities[num].frame2 = 1;
	}
	SetPersonIDFrame(entities[num].id12, 0);
	SetPersonIDX(entities[num].id12, entities[num].x);
	SetPersonIDY(entities[num].id12, entities[num].y);


	if (entities[0].hp <= 0)
	{
	  entities[num].dir2 = "die1";
	  entities[num].bdir = "";
	  entities[num].dir1 = "blank";
	  
		SetPersonIDDirection(entities[num].id1, "blank"+face);
	}
	else if ((entities[0].dir1).substr(0,6) == "reload")
	{
		if (entities[0].dir1 == "reload1")
		{
			SetPersonIDDirection(entities[num].id1, "reload1"+face);
			if (g_frames % (frameupdate+2) == 0)
			{
				if (cur_weap == 0)
				{
					PlaySound(12);
				}
				else if (cur_weap == 2)
				{
					PlaySound(14);
					while(magnum_shot > 0)
					{
						magnum_shot--;
						MakeCasing();
					}
				}
				else if (cur_weap == 3)
				{
					PlaySound(131);
					while(shell_shot > 0)
					{
						shell_shot--;
						MakeCasing("shell1");
					}
				}
				if (cur_weap == 4)
				{
					PlaySound(13);
				}
				else if (cur_weap == 7)
				{
					PlaySound(3);
				  while (rail_shot > 0)
				  {
						rail_shot--;
						MakeCasing("rail1");
					}
				}
				else if (cur_weap == 8)
				{
					while(shrap_shot > 0)
					{
						PlaySound(16);
						shrap_shot--;
						MakeCasing("shrap1");
					}
				}
				else if (cur_weap == 9)
				{
						PlaySound(18);
				}
				else if (cur_weap == 12)
				{
						PlaySound(16);
				}
				
				entities[0].dir1 = "reload2";
			}
		}
		else if (entities[0].dir1 == "reload2")
		{
			SetPersonIDDirection(entities[num].id1, "reload2"+face);
			if (g_frames % (frameupdate+2) == 0)
				entities[0].dir1 = "reload3";
		}
		else if (entities[0].dir1 == "reload3")
		{
			SetPersonIDDirection(entities[num].id1, "reload3"+face);
			if (g_frames % (frameupdate+2) == 0)
			{
				if (cur_weap == 3)
				{
				  if (ammo_array[cur_weap].curclip < ammo_array[cur_weap].maxclip && ammo_array[cur_weap].curammo>0)
				  {
					ammo_array[cur_weap].curclip++;
					ammo_array[cur_weap].curammo--;
					}
				}
				else if (cur_weap == 9)
				{
				  if (ammo_array[cur_weap].curammo>0)
				  {
					ammo_array[cur_weap].curclip++;
					ammo_array[cur_weap].curammo--;
					}
				}
				else if (ammo_array[cur_weap].curammo+ammo_array[cur_weap].curclip>=ammo_array[cur_weap].maxclip)
				{
					ammo_array[cur_weap].curammo-= ammo_array[cur_weap].maxclip-ammo_array[cur_weap].curclip;
					ammo_array[cur_weap].curclip+= (ammo_array[cur_weap].maxclip-ammo_array[cur_weap].curclip);
				}
				else
				{
					ammo_array[cur_weap].curclip+=ammo_array[cur_weap].curammo;
					ammo_array[cur_weap].curammo= 0;
				}

				if (cur_weap == 3)
					entities[0].dir1 = "aim1";
				else
					entities[0].dir1 = "reload4";

			}
		}
		else if (entities[0].dir1 == "reload4")
		{
			SetPersonIDDirection(entities[num].id1, "reload4"+face);
			if (g_frames % (frameupdate+2) == 0)
				entities[0].dir1 = "aim1";
		}
	}
	else if ((entities[0].dir1).substr(0,3) == "alt")
	{
		if (entities[0].dir1 == "alt1")
		{
			if (cur_weap == 7)
			{
				if (sounds[22].isPlaying() != true && sounds[91].isPlaying() == false && railgun_alt_charge >= railgun_alt_charge_max)
				{
					PlaySound(22);
					railgun_alt_charge = 0;
				}
				if (sounds[23].isPlaying() == true)
					sounds[23].stop();
				railgun_alt_charge++;
			}

			SetPersonIDDirection(entities[num].id1, "alt1"+face);
			if (g_frames % (frameupdate+ammo_array[cur_weap].aw1) == 0)
			{
				if (cur_weap == 0 || cur_weap == 2)
				{
						PlaySound(29);
				}
				else if (cur_weap == 4)
				{
					if (ammo_array[5].curammo > 0)
						ammo_array[5].curammo--;
					else
					{
						ammo_array[5].curammo=0;
						ammo_array[5].curclip=0;
					}
					
					var cx = MapToScreenX(0, entities[0].x);
					var cy = MapToScreenY(0, entities[0].y);
				
					if (entities[0].dir == 1)
						var	angle = Math.atan2(GetMouseY()-cy,cx-GetMouseX())+3.14159;
					else
						var	angle = Math.atan2(GetMouseY()-cy,GetMouseX()-cx)+3.14159;
									
					angle/=(2*3.14159);
									
					angle*=16;
					angle+=0.5;
					angle = Math.floor(angle);
				
					if (angle > 15)
						angle = 0;
				
					//determine line of fire:
					if (entities[0].dir == -1)
					{
						var sx = entities[0].x-gunpos[5][angle].x;
						var sy = entities[0].y+gunpos[5][angle].y-4;
					}
					else
					{
						var sx = entities[0].x+gunpos[5][angle].x;
						var sy = entities[0].y+gunpos[5][angle].y-4;
					}
	

					objy = GetGrenadeVelo(2);
					SpawnGrenade(sx,sy,objy.vx,objy.vy, 15, 2, 0);
				}
				else if (cur_weap == 6)
				{
					if (ammo_array[cur_weap].curclip > 0)
					{
						ammo_array[cur_weap].curclip--;
						if (ammo_array[cur_weap].curammo > 0)
						{
							ammo_array[cur_weap].curclip++;
							ammo_array[cur_weap].curammo--;
						}
					}

					var cx = MapToScreenX(0, entities[0].x);
					var cy = MapToScreenY(0, entities[0].y);
				
					if (entities[0].dir == 1)
						var	angle = Math.atan2(GetMouseY()-cy,cx-GetMouseX())+3.14159;
					else
						var	angle = Math.atan2(GetMouseY()-cy,GetMouseX()-cx)+3.14159;
									
					angle/=(2*3.14159);
									
					angle*=16;
					angle+=0.5;
					angle = Math.floor(angle);
				
					if (angle > 15)
						angle = 0;
				
					//determine line of fire:
					if (entities[0].dir == -1)
					{
						var sx = entities[0].x-gunpos[6][angle].x;
						var sy = entities[0].y+gunpos[6][angle].y;
					}
					else
					{
						var sx = entities[0].x+gunpos[6][angle].x;
						var sy = entities[0].y+gunpos[6][angle].y;
					}

					PlaySound(26);

				  for (i=0;i<1;i++)
				  {
						objy = GetGrenadeVelo(3, true);
						SpawnGrenade(sx,sy,objy.vx,objy.vy, 5, 4, 0, 0.75);
					}
				}
				else if (cur_weap == 8)
				{
					var cx = MapToScreenX(0, entities[0].x);
					var cy = MapToScreenY(0, entities[0].y);
				
					if (entities[0].dir == 1)
						var	angle = Math.atan2(GetMouseY()-cy,cx-GetMouseX())+3.14159;
					else
						var	angle = Math.atan2(GetMouseY()-cy,GetMouseX()-cx)+3.14159;
									
					angle/=(2*3.14159);
									
					angle*=16;
					angle+=0.5;
					angle = Math.floor(angle);
				
					if (angle > 15)
						angle = 0;
				
					//determine line of fire:
					if (entities[0].dir == -1)
					{
						var sx = entities[0].x-gunpos[8][angle].x;
						var sy = entities[0].y+gunpos[8][angle].y;
					}
					else
					{
						var sx = entities[0].x+gunpos[8][angle].x;
						var sy = entities[0].y+gunpos[8][angle].y;
					}
				
				
				  for (i=0;i<shrap_spawn_number;i++)
				  {
						objy = GetGrenadeVelo(4);
						SpawnShrapnel(sx,sy,objy.vx,objy.vy, 0);
					}
					
					shrap_shot=1;
					ammo_array[cur_weap].curclip = 0;
				}
				else if (cur_weap == 7 && railgun_alt_charge >= railgun_alt_charge_max)
				{
					angle = Math.atan2(GetMouseY()-cy,cx-GetMouseX())+3.14159;
					
					angle/=(2*3.14159);
					
					angle*=16;
					angle+=0.5;
					angle = Math.floor(angle);
				

//					if (ammo_array[cur_weap].curclip == 5)
	//					PlaySound(23);
		//			else if (ammo_array[cur_weap].curclip > 2)
			//			PlaySound(22);
				//	else
						PlaySound(21);
					
				if (ammo_array[7].curclip > 0)
				{
					ammo_array[7].curclip--;
					rail_shot++;
					FireRailGun(0, undefined, undefined, angle);
				}
				}
				else if (cur_weap == 13)
				{	
					if (adren_timer > 0)
					{
						if (Math.random()<0.5)
							PlaySound(82, true);
						else
							PlaySound(83, true);				
					}
					else
					{
						if (Math.random()<0.5)
							PlaySound(79, true);
						else
							PlaySound(80, true);				
					}
				}
				entities[0].dir1 = "alt2";

				if (cur_weap == 7)
				{
					if (railgun_alt_charge < railgun_alt_charge_max)
					{
						entities[0].dir1 = "alt1";
					}
					else if (sounds[91].isPlaying()==false)
					{
						sounds[22].stop();
						PlaySound(91, undefined,undefined, true);					
					}
				}
				

				if (cur_weap == 3)
				{
					if (ammo_array[cur_weap].curclip == 1)
						PlaySound(0);
					else if (ammo_array[cur_weap].curclip == 2)
						PlaySound(1);
					else
						PlaySound(2);
				}
				else if (cur_weap == 6)
				{
						PlaySound(26);
				}
				else if (cur_weap == 8)
				{
						PlaySound(87);
				}
				else if (cur_weap == 9)
				{
						PlaySound(17);
				}
				else if (cur_weap == 12)
				{
						PlaySound(64);
				}

			}
		}
		else if (entities[0].dir1 == "alt2")
		{
			SetPersonIDDirection(entities[num].id1, "alt2"+face);
			if (g_frames % (frameupdate+ammo_array[cur_weap].aw2) == 0)
			{
				if (cur_weap == 0 || cur_weap == 2)
				{

					if (ammo_array[1].curammo > 0)
						ammo_array[1].curammo--;
					else
					{
						ammo_array[1].curammo=0;
						ammo_array[1].curclip=0;
					}

					var cx = MapToScreenX(0, entities[0].x);
					var cy = MapToScreenY(0, entities[0].y);
				
					if (entities[0].dir == 1)
						var	angle = Math.atan2(GetMouseY()-cy,cx-GetMouseX())+3.14159;
					else
						var	angle = Math.atan2(GetMouseY()-cy,GetMouseX()-cx)+3.14159;
									
					angle/=(2*3.14159);
									
					angle*=16;
					angle+=0.5;
					angle = Math.floor(angle);
				
					if (angle > 15)
						angle = 0;
				
					//determine line of fire:
					if (entities[0].dir == -1)
					{
						var sx = entities[0].x-gunpos[1][angle].x;
						var sy = entities[0].y+gunpos[1][angle].y;
					}
					else
					{
						var sx = entities[0].x+gunpos[1][angle].x;
						var sy = entities[0].y+gunpos[1][angle].y;
					}

					
					objy = GetGrenadeVelo(1);
					SpawnGrenade(entities[0].x,entities[0].y,objy.vx,objy.vy, 10, 1, 0);
				}
				else if (cur_weap == 6)
				{
					var cx = MapToScreenX(0, entities[0].x);
					var cy = MapToScreenY(0, entities[0].y);
				
					if (entities[0].dir == 1)
						var	angle = Math.atan2(GetMouseY()-cy,cx-GetMouseX())+3.14159;
					else
						var	angle = Math.atan2(GetMouseY()-cy,GetMouseX()-cx)+3.14159;
									
					angle/=(2*3.14159);
									
					angle*=16;
					angle+=0.5;
					angle = Math.floor(angle);
				
					if (angle > 15)
						angle = 0;
				
					//determine line of fire:
					if (entities[0].dir == -1)
					{
						var sx = entities[0].x-gunpos[6][angle].x;
						var sy = entities[0].y+gunpos[6][angle].y;
					}
					else
					{
						var sx = entities[0].x+gunpos[6][angle].x;
						var sy = entities[0].y+gunpos[6][angle].y;
					}

					PlaySound(26);

				  for (i=0;i<1;i++)
				  {
						objy = GetGrenadeVelo(3, true);
						SpawnGrenade(sx,sy,objy.vx,objy.vy, 5, 4, 0, 0.75);
					}
				}
				else if (cur_weap == 13)
				{
					if (GetMouseX()<MapToScreenX(0,entities[0].x))
						tempytemptemp = -1;
					else
						tempytemptemp = 1;
					if (adren_timer > 0)
						SpawnBeam(entities[0].x,entities[0].y, tempytemptemp, adren_sword_bonus*ammo_array[13].damage/2, 0, 0, 1, 6);
					else
						SpawnBeam(entities[0].x,entities[0].y, tempytemptemp, ammo_array[13].damage/2, 0, 0, 1, 6);
				}
				
				entities[0].dir1 = "alt3";
				if (cur_weap == 7 && ammo_array[7].curclip > 0)
					entities[0].dir1 = "alt1";


			}
		}
		else if (entities[0].dir1 == "alt3")
		{
			if (cur_weap == 7)
			{
				if (sounds[91].isPlaying() == true)
					sounds[91].stop();
					
			}
			SetPersonIDDirection(entities[num].id1, "alt3"+face);
			if (g_frames % (frameupdate+ammo_array[cur_weap].aw3) == 0)
			{
				if (cur_weap == 3)
				{
					
				  while(ammo_array[cur_weap].curclip > 0)
				  {
						FirePlayerGun(cur_weap);
						ammo_array[cur_weap].curclip--;
						shell_shot++;
				  }
					ammo_array[cur_weap].curclip = 0;
				}
				else if (cur_weap == 6)
				{
					var cx = MapToScreenX(0, entities[0].x);
					var cy = MapToScreenY(0, entities[0].y);
				
					if (entities[0].dir == 1)
						var	angle = Math.atan2(GetMouseY()-cy,cx-GetMouseX())+3.14159;
					else
						var	angle = Math.atan2(GetMouseY()-cy,GetMouseX()-cx)+3.14159;
									
					angle/=(2*3.14159);
									
					angle*=16;
					angle+=0.5;
					angle = Math.floor(angle);
				
					if (angle > 15)
						angle = 0;
				
					//determine line of fire:
					if (entities[0].dir == -1)
					{
						var sx = entities[0].x-gunpos[6][angle].x;
						var sy = entities[0].y+gunpos[6][angle].y;
					}
					else
					{
						var sx = entities[0].x+gunpos[6][angle].x;
						var sy = entities[0].y+gunpos[6][angle].y;
					}

					PlaySound(26);

				  for (i=0;i<1;i++)
				  {
						objy = GetGrenadeVelo(3, true);
						SpawnGrenade(sx,sy,objy.vx,objy.vy, 5, 4, 0, 0.75);
					}
				}
				else if (cur_weap == 7)
				{
				  
					ammo_array[cur_weap].curclip = 0;
				}
				else if (cur_weap == 9)
				{
					ammo_array[cur_weap].curclip = 0;
					FireObliterator(1);
				}
				else if (cur_weap == 12)
				{
					for (l=0;l<6;l++)
					{	
						if (ammo_array[cur_weap].curclip>0)
						{
							FirePlayerGun(cur_weap);
							ammo_array[cur_weap].curclip--;
							MakeCasing();
						}
					}
				}
			
				entities[0].dir1 = "alt4";
			}
		}
		else if (entities[0].dir1 == "alt4")
		{
			SetPersonIDDirection(entities[num].id1, "alt4"+face);
			if (g_frames % (frameupdate+ammo_array[cur_weap].aw4) == 0)
			{
				entities[0].dir1 = "aim1";
				if (cur_weap == 7)
					PlaySound(23);
			}
				if (cur_weap == 6 && g_frames % (frameupdate+ammo_array[cur_weap].aw4) == 0)
				{
					var cx = MapToScreenX(0, entities[0].x);
					var cy = MapToScreenY(0, entities[0].y);
				
					if (entities[0].dir == 1)
						var	angle = Math.atan2(GetMouseY()-cy,cx-GetMouseX())+3.14159;
					else
						var	angle = Math.atan2(GetMouseY()-cy,GetMouseX()-cx)+3.14159;
									
					angle/=(2*3.14159);
									
					angle*=16;
					angle+=0.5;
					angle = Math.floor(angle);
				
					if (angle > 15)
						angle = 0;
				
					//determine line of fire:
					if (entities[0].dir == -1)
					{
						var sx = entities[0].x-gunpos[6][angle].x;
						var sy = entities[0].y+gunpos[6][angle].y;
					}
					else
					{
						var sx = entities[0].x+gunpos[6][angle].x;
						var sy = entities[0].y+gunpos[6][angle].y;
					}

					PlaySound(26);

				  for (i=0;i<1;i++)
				  {
						objy = GetGrenadeVelo(3, true);
						SpawnGrenade(sx,sy,objy.vx,objy.vy, 5, 4, 0, 0.75);
					}
			}
			
		}
	}
	else if (entities[0].dir1 == "aim2")
	{
		if (cur_weap == 7)
		{
			if (sounds[23].isPlaying() == true)
			{
				sounds[23].stop();
			}
		}
	
		if (cur_weap == 13)
		{
			SetPersonIDFrame(entities[num].id12, 1);
		}
//		if (cur_weap == 0 || cur_weap == 2)
//		{
//		}

		SetPersonIDDirection(entities[num].id1, "aim2"+face);
		
	  if (g_frames % (frameupdate+ammo_array[cur_weap].fw1) == 0 || cur_weap == 12)
	  {
			entities[0].dir1 = "aim3";
			if (ammo_array[cur_weap].useammo != 0)
				ammo_array[cur_weap].curclip--;
			
			//fire the weapon
			if (cur_weap == 6)
			{
				var cx = MapToScreenX(0, entities[0].x);
				var cy = MapToScreenY(0, entities[0].y);
			
				if (entities[0].dir == 1)
					var	angle = Math.atan2(GetMouseY()-cy,cx-GetMouseX())+3.14159;
				else
					var	angle = Math.atan2(GetMouseY()-cy,GetMouseX()-cx)+3.14159;
								
				angle/=(2*3.14159);
								
				angle*=16;
				angle+=0.5;
				angle = Math.floor(angle);
			
				if (angle > 15)
					angle = 0;
			
				//determine line of fire:
				if (entities[0].dir == -1)
				{
					var sx = entities[0].x-gunpos[6][angle].x;
					var sy = entities[0].y+gunpos[6][angle].y;
				}
				else
				{
					var sx = entities[0].x+gunpos[6][angle].x;
					var sy = entities[0].y+gunpos[6][angle].y;
				}

			  objy = GetGrenadeVelo(3);
				SpawnGrenade(sx,sy,objy.vx,objy.vy, 20, 3, 0);
			}
			else if (cur_weap == 13)
			{
					if (adren_timer > 0)
					{
						if (Math.random()<0.5)
							PlaySound(82, true);
						else
							PlaySound(83, true);				
					}
					else
					{
						if (Math.random()<0.5)
							PlaySound(79, true);
						else
							PlaySound(80, true);				
					}
			}
			else
			{
			  if (cur_weap == 7)
			  {
					angle = Math.atan2(GetMouseY()-cy,cx-GetMouseX())+3.14159;
					
					angle/=(2*3.14159);
					
					angle*=16;
					angle+=0.5;
					angle = Math.floor(angle);

					rail_shot++;
					FireRailGun(0, undefined, undefined, angle);
				}
				else if (cur_weap == 9)
				{
					ammo_array[cur_weap].curclip = 0;
					FireObliterator(0);
				}
				else
				{
					if (cur_weap == 0|| cur_weap == 4)
					{
					  MakeCasing();
					}
					else if (cur_weap == 2)
					{
						magnum_shot++;
					}
					else if (cur_weap == 3)
					{
					
						shell_shot++;
					}
					else if (cur_weap == 8)
					{
						shrap_shot=1;
					}

					if (cur_weap != 12)
					{
						FirePlayerGun(cur_weap);
						if (cur_weap ==8)
						{
							FirePlayerGun(cur_weap);
							FirePlayerGun(cur_weap);
						}
					}
					else
					{
						ammo_array[cur_weap].curclip++;
						
						if (chaingun_timer >= chaingun_timer_max)
						{
							ammo_array[cur_weap].curclip--;
							PlayChainGun();
							MakeCasing();
							FirePlayerGun(cur_weap);
							if (ammo_array[cur_weap].curclip > 0)
							{
								ammo_array[cur_weap].curclip--;
								MakeCasing();
								FirePlayerGun(cur_weap);
							}
						}
						else
						{
							chaingun_timer++;
							entities[0].dir1 = "aim2";
						}
					}
				}
			}
		}
	}
	else if (entities[0].dir1 == "aim3")
	{
		if (cur_weap == 13)
		{
			SetPersonIDFrame(entities[num].id12, 2);
			if (g_frames % (frameupdate+ammo_array[cur_weap].aw2) == 0)
			{
				if (adren_timer>0)
				{
					if (GetMouseX()<MapToScreenX(0,entities[0].x))
						DoSwingAttack(entities[0].x,entities[0].y,-1,adren_sword_bonus*ammo_array[13].damage, 0);
					else
						DoSwingAttack(entities[0].x,entities[0].y,1,adren_sword_bonus*ammo_array[13].damage, 0);
				}
				else
				{
					if (GetMouseX()<MapToScreenX(0,entities[0].x))
						DoSwingAttack(entities[0].x,entities[0].y,-1,ammo_array[13].damage, 0);
					else
						DoSwingAttack(entities[0].x,entities[0].y,1,ammo_array[13].damage, 0);
				}
			}
		}
		
		if (cur_weap == 12)
		{
			if (ammo_array[cur_weap].curclip > 0)
			{
				if (chaingun_timer >= chaingun_timer_max)
				{
					ammo_array[cur_weap].curclip--;
					MakeCasing();
					PlayChainGun();
					FirePlayerGun(cur_weap);
					if (ammo_array[cur_weap].curclip > 0)
					{
						ammo_array[cur_weap].curclip--;
						MakeCasing();
						FirePlayerGun(cur_weap);
					}
				}
			}
		}
		
		SetPersonIDDirection(entities[num].id1, "aim3"+face);
	  if (g_frames % (frameupdate+ammo_array[cur_weap].fw2) == 0)
			entities[0].dir1 = "aim4";
	}
	else if (entities[0].dir1 == "aim4")
	{
		if (chaingun_timer >= chaingun_timer_max)
		{
			PlayChainGun();
		}

		SetPersonIDDirection(entities[num].id1, "aim4"+face);
//	  if (g_frames % frameupdate == 0)
			entities[0].dir1 = "aim1";

			if (cur_weap == 7 )
			{
				PlaySound(23);
			}
	
	}
	else
		SetPersonIDDirection(entities[num].id1, "aim1"+face);

	if ((entities[0].dir1).substr(0,3) != "alt" || cur_weap != 7)
	{
		if (sounds[91].isPlaying() == true)
			sounds[91].stop();	
	}

	SetPersonIDFrame(entities[num].id1, entities[num].frame1);

	var t_string = (entities[num].name);
	t_string = t_string.substr(0,t_string.length-5)+"legs";

	if (entities[0].hp <=0)
	{
		SetPersonDirection(t_string, "die1"+face);
	}
	else if (g_isrunning)
	{
	  entities[num].bdir = "";

		if (entities[num].dir2 == "jump")
			SetPersonDirection(t_string, "runjump"+face);
		else
			SetPersonDirection(t_string, "run"+face);

		SetPersonDirection(entities[num].name, "blank"+face);
		if (cur_weap == 13)
		{
			SetPersonIDHorizontalFlip(entities[num].id11, (-1*entities[num].dir+1)/2);
		}
	}
	else
		SetPersonDirection(t_string, entities[num].dir2+face);

	if (entities[0].dir2 == "punch" || entities[0].dir2 == "grenade")
	{
	//	SetPersonDirection(entities[num].name, "blank"+face);
	}
	


		SetPersonIDX(entities[0].id1, entities[0].x);


	if (cur_weap == 0)
	{
	/*
		SetPersonDirection("main_gun2", GetPersonDirection(entities[num].name));
		SetPersonFrame("main_gun2", GetPersonFrame(entities[num].name));
		SetPersonX("main_gun2", GetPersonX(entities[num].name));
		SetPersonY("main_gun2", GetPersonY(entities[num].name));
		SetPersonLayer("main_gun2", GetPersonLayer(entities[num].name));
	*/	
	}
	else if (cur_weap != 2)
	{
		SetPersonX(entities[num].name, -100);
	}

	if (cur_weap == 2)
	{
		SetPersonDirection("main_gun2", GetPersonDirection(entities[num].name));
		SetPersonFrame("main_gun2", GetPersonFrame(entities[num].name));
		SetPersonX("main_gun2", entities[num].x);
		SetPersonY("main_gun2", GetPersonY(entities[num].name));
		SetPersonLayer("main_gun2", GetPersonLayer(entities[num].name));
	}
	else
	{
		SetPersonX("main_gun2", -100);
	}

	if (cur_weap == 3)
	{
		SetPersonDirection("main_gun3", GetPersonDirection(entities[num].name));
		SetPersonFrame("main_gun3", GetPersonFrame(entities[num].name));
		SetPersonX("main_gun3", entities[num].x);
		SetPersonY("main_gun3", GetPersonY(entities[num].name));
		SetPersonLayer("main_gun3", GetPersonLayer(entities[num].name));
	}
	else
	{
		SetPersonX("main_gun3", -100);
//		SetPersonX("main_torso", -100);
	}

	if (cur_weap == 4)
	{
		SetPersonDirection("main_gun4", GetPersonDirection(entities[num].name));
		SetPersonFrame("main_gun4", GetPersonFrame(entities[num].name));
		SetPersonX("main_gun4", entities[num].x);
		SetPersonY("main_gun4", GetPersonY(entities[num].name));
		SetPersonLayer("main_gun4", GetPersonLayer(entities[num].name));
	}
	else
	{
		SetPersonX("main_gun4", -100);
//		SetPersonX("main_torso", -100);
	}

	if (cur_weap == 6)
	{
		SetPersonDirection("main_gun5", GetPersonDirection(entities[num].name));
		SetPersonFrame("main_gun5", GetPersonFrame(entities[num].name));
		SetPersonX("main_gun5", entities[num].x);
		SetPersonY("main_gun5", GetPersonY(entities[num].name));
		SetPersonLayer("main_gun5", GetPersonLayer(entities[num].name));
	}
	else
	{
		SetPersonX("main_gun5", -100);
//		SetPersonX("main_torso", -100);
	}

	if (cur_weap == 7)
	{
		SetPersonDirection("main_gun6", GetPersonDirection(entities[num].name));
		SetPersonFrame("main_gun6", GetPersonFrame(entities[num].name));
		SetPersonX("main_gun6", entities[num].x);
		SetPersonY("main_gun6", GetPersonY(entities[num].name));
		SetPersonLayer("main_gun6", GetPersonLayer(entities[num].name));
	}
	else
	{
		SetPersonX("main_gun6", -100);
//		SetPersonX("main_torso", -100);
	}

	if (cur_weap == 8)
	{
		SetPersonDirection("main_gun7", GetPersonDirection(entities[num].name));
		SetPersonFrame("main_gun7", GetPersonFrame(entities[num].name));
		SetPersonX("main_gun7", entities[num].x);
		SetPersonY("main_gun7", GetPersonY(entities[num].name));
		SetPersonLayer("main_gun7", GetPersonLayer(entities[num].name));
	}
	else
	{
		SetPersonX("main_gun7", -100);
//		SetPersonX("main_torso", -100);
	}

	if (cur_weap == 9)
	{
		SetPersonDirection("main_gun8", GetPersonDirection(entities[num].name));
		SetPersonFrame("main_gun8", GetPersonFrame(entities[num].name));
		SetPersonX("main_gun8", entities[num].x);
		SetPersonY("main_gun8", GetPersonY(entities[num].name));
		SetPersonLayer("main_gun8", GetPersonLayer(entities[num].name));
	}
	else
	{
		SetPersonX("main_gun8", -100);
//		SetPersonX("main_torso", -100);
	}
	if (cur_weap == 12)
	{
		SetPersonDirection("main_gun7b", GetPersonDirection(entities[num].name));
		SetPersonFrame("main_gun7b", GetPersonFrame(entities[num].name));
		SetPersonX("main_gun7b", entities[num].x);
		SetPersonY("main_gun7b", GetPersonY(entities[num].name));
		SetPersonLayer("main_gun7b", GetPersonLayer(entities[num].name));
	}
	else
	{
		SetPersonX("main_gun7b", -100);
//		SetPersonX("main_torso", -100);
	}
	if (cur_weap == 13)
	{
		SetPersonDirection("main_sword2", GetPersonDirection(entities[num].name)+entities[0].bdir);
		SetPersonFrame("main_sword2", GetPersonFrame(entities[num].name));

		if (((GetMouseX()< MapToScreenX(0, entities[0].x) && entities[0].dir != -1)||
				(GetMouseX()> MapToScreenX(0, entities[0].x) && entities[0].dir != 1)) && (entities[0].hp > 0 && g_isrunning == false))
		{
			SetPersonX("main_sword2", entities[num].x-entities[0].dir);
		}
		else
			SetPersonX("main_sword2", entities[num].x);
			
		if (g_isrunning ==true && entities[0].dir == -1)
			SetPersonX("main_sword2", GetPersonX("main_sword2")-2);
			
			
		SetPersonY("main_sword2", GetPersonY(entities[num].name));
		SetPersonLayer("main_sword2", GetPersonLayer(entities[num].name));
	}
	else
	{
		SetPersonX("main_sword2", -100);
//		SetPersonX("main_torso", -100);
	}

	var old_frame = GetPersonFrame(t_string);
	if (key_play == true)
	{
		var fra = g_frames-key_array[0].frame_st;
		entities[num].frame2 = key_array[fra].frame2;
		SetPersonFrame("main_legs", entities[0].frame2);
	}
	else
		SetPersonFrame(t_string, entities[num].frame2);

	if (old_frame != entities[num].frame2)
	{
		if (GetPersonDirection(t_string).substr(0,3) == "run" && IsPersonOnGround(0))
		{
			if (entities[num].frame2 == 1 || entities[num].frame2 == 5)
			{
				PlaySound(31, true);
			}
	
		}
		else if (GetPersonDirection(t_string).substr(0,4) == "walk" && IsPersonOnGround(0))
		{
			if (entities[num].frame2 == 3 || entities[num].frame2 == 9)
			{
				PlaySound(30, true);
			}
		}
	}

	entities[num].frame1 = GetPersonFrame(entities[num].name);
	entities[num].frame2 = GetPersonFrame(t_string);

	if (key_play == true)
	{
		var fra = g_frames-key_array[0].frame_st;
		entities[num].frame2 = key_array[fra].frame2;
	}

}

var g_jump_pressed = false;
var g_gun_click = false;
var g_rgun_click = false;
var g_fire_pressed = false;
var g_punch_pressed = false;
var g_open_pressed = false;
var g_isrunning = false;
var g_grenade_fired = false;
var g_reload_fired = false;
var g_adren_fired = false;
var g_z_fired = false;
var g_x_fired = false;

var g_x_pressed = false;
var g_z_pressed = false;


var adren_timer = 0;
var adren_max = 200;

var Key7Pressed = false;
var Key8Pressed = false;
var g_upate_input_suspend = false;


function UpdateInput()
{
	var pre_weap = cur_weap;
	var pre_run = g_isrunning;

	if (entities[0].vx_bypass == true)
	{
		if (IsPersonOnGround(0) == true)
		{
			entities[0].vx_bypass = false;
		}
	}
	if (g_upate_input_suspend == true)
		return 0;
	var in_water = IsXYInWater(entities[0].x,entities[0].y-8);//IsXYInWater(entities[0].x,entities[0].y-16);

	if (IsKeyPressed(CKEY_7) == false)
		Key7Pressed = false;
	if (IsKeyPressed(CKEY_8) == false)
		Key8Pressed = false;
	if (g_cancel_fade == true)
		return 0;

  if (g_isrunning == false && entities[0].vx_bypass != true)
		entities[0].vx = 0;
	
	if (entities[0].vx == 0)
	  g_isrunning = false;

	var t_layer = GetPersonLayer(entities[0].name)
	SetPersonLayer(entities[0].name, 0);

  if (!IsKeyPressed(CKEY_W))
			g_jump_pressed = false;

  if (!IsKeyPressed(CKEY_Q))
			g_adren_fired = false;



  if (!IsMouseButtonPressed(CMOUSE_LEFT))
  {
			g_fire_pressed = false;
			g_gun_click = false;
			
			if (cur_weap == 12 && (entities[0].dir1 == "aim2"||entities[0].dir1 == "aim3"||entities[0].dir1 == "aim4"))
			{
				entities[0].dir1 = "aim1";
			}
  }
  if (!IsMouseButtonPressed(CMOUSE_RIGHT))
  {
			g_rgun_click = false;
  }
  if (!IsMouseButtonPressed(CMOUSE_RIGHT) && (entities[0].dir1).substr(0,3) != "alt")
  {
			g_punch_pressed = false;
  }

	if ((entities[0].dir1).substr(0,3) == "alt" && cur_weap == 6)
	{
		g_punch_pressed = false;
	}

  if (!IsKeyPressed(CKEY_E)&& (entities[0].dir1).substr(0,6) != "reload")
  {
			g_reload_pressed = false;
  }


  if (!IsKeyPressed(CKEY_Z))
			g_z_pressed = false;
  if (!IsKeyPressed(CKEY_X))
			g_x_pressed = false;
			

	var wheely = -1;
	if (GetNumMouseWheelEvents() > 0)
	{
		wheely = GetMouseWheelEvent();
	}
			
	if ((g_z_pressed == false && IsKeyPressed(CKEY_Z)) || wheely == MOUSE_WHEEL_DOWN)
	{
		entities[0].dir1 = "aim1";
		if (IsKeyPressed(CKEY_Z))
			g_z_pressed = true;

		var gx;
		if (cur_weap == 0)
			gx = 0;
		else if (cur_weap < 5)
		  gx = cur_weap-1;
		else if (cur_weap == 12)
			gx = 10;
		else if (cur_weap == 13)
			gx = 11;
		else
		  gx = cur_weap-2;
		
		for (gz=0;gz<11;gz++)
		{
			if (gx != 10)
				gx=gx-1;
			else
				gx = 7;
				
		  if (gx < 0)
		    gx = 11;
		  if (weap_possess[gx] == 1)
		  {
				if (gx == 0)
				  cur_weap = 0;
				else if (gx<4)
				  cur_weap = gx+1;
				else if (gx == 10)
					cur_weap = 12;
				else if (gx == 11)
					cur_weap = 13;
				else
				  cur_weap = gx+2;
				break;
		  }
		}
	}

	if ((g_x_pressed == false && IsKeyPressed(CKEY_X)) || wheely == MOUSE_WHEEL_UP)
	{
		entities[0].dir1 = "aim1";
		if (IsKeyPressed(CKEY_X))
			g_x_pressed = true;

		var gx;
		if (cur_weap == 0)
			gx = 0;
		else if (cur_weap < 5)
		  gx = cur_weap-1;
		else if (cur_weap == 12)
			gx = 10;
		else if (cur_weap == 13)
			gx = 11;
		else
		  gx = cur_weap-2;
		
		for (gz=0;gz<9;gz++)
		{
			if (gx == 7)
				gx = 10;
			else if (gx == 10)
				gx = 11
			else if (gx == 11)
				gx = 0;
			else
				gx=(gx+1)%8;
				
		  if (weap_possess[gx] == 1)
		  {
				if (gx == 0)
				  cur_weap = 0;
				else if (gx<4)
				  cur_weap = gx+1;
				else if (gx == 10)
					cur_weap = 12;
				else if (gx == 11)
					cur_weap = 13;
				else
				  cur_weap = gx+2;
				break;
		  }
		}
	}



	if (IsPersonOnGround(0)&&IsKeyPressed(CKEY_SHIFT) && (IsKeyPressed(CKEY_D) || IsKeyPressed(CKEY_A))
		  && entities[0].dir2 != "punch" && entities[0].dir2 != "grenade")
	{
		if (entities[0].stam > adren_run_start)
			g_isrunning = true;

		if (IsKeyPressed(CKEY_D) && IsPersonObstructed(entities[0].name, entities[0].x+1, entities[0].y))
			g_isrunning = false;
		if (IsKeyPressed(CKEY_A) && IsPersonObstructed(entities[0].name, entities[0].x-1, entities[0].y))
			g_isrunning = false;

	}
	else if (IsPersonOnGround(0))
	{
		g_isrunning = false;
	}


	var tempytemptemp;


	if ((IsKeyPressed(CKEY_D) && IsKeyPressed(CKEY_A) != true) && !IsPersonObstructed(entities[0].name, entities[0].x+1, entities[0].y)
		  && entities[0].dir2 != "punch" && entities[0].dir2 != "grenade")
	{
		if (g_isrunning == false&& entities[0].vx_bypass != true)
			entities[0].vx=3;
		else
		{
			if (IsPersonOnGround(0))
				entities[0].vx=6;
		}
		
	}
	if (IsKeyPressed(CKEY_A) && !IsPersonObstructed(entities[0].name, entities[0].x-1, entities[0].y)
		  && entities[0].dir2 != "punch" && entities[0].dir2 != "grenade")
	{
		if (g_isrunning == false&& entities[0].vx_bypass != true)
			entities[0].vx=-3;
		else
		{
			if (IsPersonOnGround(0))
				entities[0].vx=-6;
		}
	}
	if (IsKeyPressed(CKEY_W) && IsPersonOnGround(0)==true && g_jump_pressed == false
		  && entities[0].dir2 != "punch" && entities[0].dir2 != "grenade")
	{
		entities[0].vy=-8;
		g_jump_pressed = true;
		if (IsXYInWater(entities[0].x,entities[0].y-8) == false)
			PlaySound(99+Math.floor(Math.random()*3));
	}
	
	if (IsMouseButtonPressed(CMOUSE_LEFT) && g_fire_pressed== false && g_isrunning == false)
	{
		if (entities[0].dir1 != "aim2" && entities[0].dir1 != "aim3" && entities[0].dir1 != "aim4")
		{
//			if (entities[0].dir2 != "punch" && entities[0].dir2 != "grenade")
			if ((entities[0].dir1).substr(0,3) != "alt" && (entities[0].dir1).substr(0,6) != "reload")
			{
				if (g_frames % frameupdate == 0)
				{
					if ((ammo_array[cur_weap].curclip > 0 || ammo_array[cur_weap].useammo == 0) && (in_water == false || ammo_array[cur_weap].waterfirable == 1))
					{
						entities[0].bdir = "";
						entities[0].dir1 = "aim2";
						if (cur_weap == 0)
						{
							PlaySound(9);
						}
						else if (cur_weap == 2)
						{
							PlaySound(10);
						}
						else if (cur_weap == 3)
						{
							PlaySound(0);
						}
						else if (cur_weap == 4)
						{
							PlaySound(11);
						}
						else if (cur_weap == 6)
						{
							PlaySound(25);
						}
						else if (cur_weap == 7)
						{
							PlaySound(21);
						}
						else if (cur_weap == 8)
						{
							PlaySound(15);
						}
						else if (cur_weap == 9)
						{
							PlaySound(17);
						}
						else if (cur_weap == 12)
						{
							if (chaingun_timer == 0)
								PlaySound(61);
							if (chaingun_timer < chaingun_timer_max)
							{	
								chaingun_timer++;
								entities[0].dir1 = "aim1";
							}
							else if (g_frames%8 == 0)
							{
								entities[0].dir1 = "aim2";
							}
						}			
						else if (cur_weap == 13)
						{
							if (Math.random()<0.5)
								entities[0].bdir = "";
							else
								entities[0].bdir = "b";
							
							//play sword sound
						}
					}
					else if ((ammo_array[cur_weap].curammo <= 0 && g_gun_click == false)||(in_water == true && ammo_array[cur_weap].waterfirable == 0 && g_gun_click == false))
					{
						//clicking sound 
						g_gun_click = true;
						if (cur_weap > 4)
							PlaySound(19);
						else
							PlaySound(20);
						
					}
				}
			}
		}
	}

	if (IsMouseButtonPressed(CMOUSE_RIGHT) && g_punch_pressed== false /*&& IsPersonOnGround(0)*/ && g_isrunning == false)
	{
		if (entities[0].dir1 != "aim2" && entities[0].dir1 != "aim3" && entities[0].dir1 != "aim4")
		{
			if ((entities[0].dir1).substr(0,3) != "alt" && g_grenade_fired == false && (entities[0].dir1).substr(0,6) != "reload")
			{
				entities[0].bdir = "";
			  if (cur_weap == 0 || cur_weap == 2)
			  {
					if (ammo_array[1].curammo > 0)
						g_grenade_fired = true;
					else
					;
			  }
			  else if (cur_weap == 4)
			  {
					if (ammo_array[5].curammo > 0)
					{
						PlaySound(28);
						g_grenade_fired = true;
					}
					else
					;
			  }
			  else if (cur_weap == 13)
			  {
					PlaySound(78, true);
					g_grenade_fired = true;
			  }
			  else 
			  {
					
					if ((ammo_array[cur_weap].curclip > 0 || ammo_array[cur_weap].useammo == 0) && (in_water == false || ammo_array[cur_weap].waterfirable == 1))
						g_grenade_fired = true;
					else if (g_rgun_click == false)
					{
					//clicking sound
						g_rgun_click = true;
						if (cur_weap > 4)
							PlaySound(19);
						else
							PlaySound(20);					
					}
			  }
			}
		}
	}

	if (g_frames % (frameupdate+2) == 0 && g_grenade_fired == true && g_isrunning == false)
	{
	  g_grenade_fired = false;
		entities[0].dir1 = "alt1";
//		entities[0].frame2 = 0;
		g_punch_pressed=true;
	}


	if (IsKeyPressed(CKEY_E) && g_reload_pressed== false /*&& IsPersonOnGround(0)*/&& g_isrunning == false)
	{
		if (entities[0].dir1 != "aim2" && entities[0].dir1 != "aim3" && entities[0].dir1 != "aim4")
		{
			if ((entities[0].dir1).substr(0,3) != "alt" && (entities[0].dir1).substr(0,6) != "reload")
			{
			  if (ammo_array[cur_weap].curclip >= ammo_array[cur_weap].maxclip);
				else if (ammo_array[cur_weap].curammo>0)
					g_reload_fired = true;
				else
				//clicking sound
				;
			}
		}
	}
	else if (ammo_array[cur_weap].curclip<=0 && ammo_array[cur_weap].curammo>0&& g_isrunning == false)
	{
		if (entities[0].dir1 != "aim2" && entities[0].dir1 != "aim3" && entities[0].dir1 != "aim4")
		{
			if ((entities[0].dir1).substr(0,3) != "alt" && (entities[0].dir1).substr(0,6) != "reload")
			{
					g_reload_fired = true;
			}
		}
	}

	if (g_frames % (frameupdate+2) == 0 && g_reload_fired == true&& g_isrunning == false)
	{
	  g_reload_fired = false;
		entities[0].dir1 = "reload1";
//		entities[0].frame2 = 0;
//		g_reload_pressed=true;
	}

  if (g_isrunning)
  {
		//CANCEL RELOADING
  	entities[0].dir1 = "aim1";
  }



	if (IsKeyPressed(CKEY_1) && cur_weap != 0 && weap_possess[0] == 1)
	{
		cur_weap = 0;
		entities[0].dir1 = "aim1";
//		SetPersonSpriteset("main_torso", weapon_sets[0]);
	}
	else if (IsKeyPressed(CKEY_2) && cur_weap != 2&& weap_possess[1] == 1)
	{
		cur_weap = 2;
		entities[0].dir1 = "aim1";
	//	SetPersonSpriteset("main_torso", weapon_sets[1]);
	}
	else if (IsKeyPressed(CKEY_3) && cur_weap != 3&& weap_possess[2] == 1)
	{
		cur_weap = 3;
		entities[0].dir1 = "aim1";
//		SetPersonSpriteset("main_torso", weapon_sets[0]);
	}
	else if (IsKeyPressed(CKEY_4) && cur_weap != 4&& weap_possess[3] == 1)
	{
		cur_weap = 4;
		entities[0].dir1 = "aim1";
//		SetPersonSpriteset("main_torso", weapon_sets[0]);
	}
	else if (IsKeyPressed(CKEY_5) && cur_weap != 6&& weap_possess[4] == 1)
	{
		cur_weap = 6;
		entities[0].dir1 = "aim1";
//		SetPersonSpriteset("main_torso", weapon_sets[0]);
	}
	else if (IsKeyPressed(CKEY_6) && cur_weap != 7&& weap_possess[5] == 1)
	{
		cur_weap = 7;
		entities[0].dir1 = "aim1";
//		SetPersonSpriteset("main_torso", weapon_sets[0]);
	}
	else if (IsKeyPressed(CKEY_7) && cur_weap != 8&& weap_possess[6] == 1 && Key7Pressed == false)
	{
		cur_weap = 8;
		entities[0].dir1 = "aim1";
		Key7Pressed = true;
//		SetPersonSpriteset("main_torso", weapon_sets[0]);
	}
	else if (IsKeyPressed(CKEY_7) && cur_weap != 12&& weap_possess[10] == 1 && Key7Pressed == false)
	{
		//chaingun
		cur_weap = 12;
		Key7Pressed = true;
		entities[0].dir1 = "aim1";
//		SetPersonSpriteset("main_torso", weapon_sets[0]);
	}	
	else if (IsKeyPressed(CKEY_8) && cur_weap != 9&& weap_possess[7] == 1 && Key8Pressed == false)
	{
		cur_weap = 9;
		entities[0].dir1 = "aim1";
		Key8Pressed = true;
//		SetPersonSpriteset("main_torso", weapon_sets[0]);
	}
	else if (IsKeyPressed(CKEY_8) && cur_weap != 13&& weap_possess[11] == 1 && Key8Pressed == false)
	{
		cur_weap = 13;
		entities[0].dir1 = "aim1";
		Key8Pressed = true;
//		SetPersonSpriteset("main_torso", weapon_sets[0]);
	}
	else if (IsKeyPressed(CKEY_9) && cur_weap != 12&& weap_possess[10] == 1)
	{
		cur_weap = 12;
		entities[0].dir1 = "aim1";
//		SetPersonSpriteset("main_torso", weapon_sets[0]);
	}
	else if (IsKeyPressed(CKEY_0) && cur_weap != 13&& weap_possess[11] == 1)
	{
		cur_weap = 13;
		entities[0].dir1 = "aim1";
//		SetPersonSpriteset("main_torso", weapon_sets[0]);
	}
/*
	if (IsKeyPressed(CKEY_SPACE))
	{
	  ClearAllBlood();
	}
*/

	if (g_isrunning)
	{
	  if (adren_timer <= 0)
			entities[0].stam-=g_stam_use;
		
		if (entities[0].stam < 0)
		{
		  entities[0].stam = 0;
		  if (IsPersonOnGround(0))
				g_isrunning = false;
		}
	}
	
	if (IsKeyPressed(CKEY_Q) && g_adren_fired == false && entities[0].hp > 0 && ammo_array[10].curammo > 0 && adren_timer == 0)
	{
		ammo_array[10].curammo--;
		adren_timer = adren_max;	
		g_adren_fired = true;
		if (Math.random()<0.5)
			PlaySound(134);
		else
			PlaySound(135);
		
		sounds[32].setPosition(0);
		sounds[32].setVolume(255);
		sounds[32].stop();
		sounds[32].play(true);
	}
	
	SetPersonLayer(entities[0].name, t_layer);
	
	if (!IsKeyPressed(CKEY_ESCAPE))
		ckey_esc_pressed = false;
	
	if ((IsKeyPressed(CKEY_ESCAPE) && ckey_esc_pressed == false)||!IsWindowActive())
	{
		//pause all music here:
		
		PauseAllMusic();
		
	
		var temmpy = MainMenu(0);
		if (temmpy != 0)
		{
			//do not return to this game
			return temmpy; 
		}
		else
		{
			UnPauseAllMusic();
			//continue music here:
		}
		
		ckey_esc_pressed = true;
	}
	
	
	if (pre_weap != cur_weap || g_isrunning == true)
	{
		StopReloadSounds();
	
	}
	
	return 0;
		
}
var ckey_esc_pressed = true;


function FirePlayerGun(num)
{
  if (num == undefined)
    num = 0;
  //determine line of fire:
  var sx = entities[0].x;
  var sy = entities[0].y-4;
  
	var tx = GetMouseX()-MapToScreenX(0, entities[0].x);
	var ty = GetMouseY()-MapToScreenY(0, entities[0].y);
  
  var entx, enty;
  var entw, enth;

  var hentx, henty;
  var hentw, henth;

  var dentx, denty;
  var dentw, denth;

  var a,b;
  var ha,hb;
  var px,py;
  var hpx,hpy;
  var dpx,dpy;
  
  var dammulti = 1;
  if (adren_timer > 0)
		dammulti = 1.25;

  if (tx==0 && ty==0)
    tx = 1;
    
    
  var ent_hit = -1;
  var head_hit = -1;
  var door_hit = -1;
  var min_const = 1000000;
  var hmin_const = 1000000;
  var dmin_const = 1000000;
  
  var hit_type = 0;
  
  if (GetTile(Math.floor(ScreenToMapX(0,GetMouseX())/GetTileWidth()), Math.floor(ScreenToMapY(0,GetMouseY())/GetTileWidth()), 0) == 0)
  {
//		SpawnBlood(ScreenToMapX(0,GetMouseX()), ScreenToMapY(0,GetMouseY()), 8*Math.random()-4,4*Math.random()-8,Math.floor(Math.random()*3+1));
//			SpawnGuts(0,ScreenToMapX(0,GetMouseX()), ScreenToMapY(0,GetMouseY()), 8*Math.random()-4,4*Math.random()-8, "guts"+Math.floor(2+Math.random()*3), 0, blood_layer);
//			AddBackBlood(Math.floor(ScreenToMapX(0,GetMouseX())/GetTileWidth()), Math.floor(ScreenToMapY(0,GetMouseY())/GetTileHeight()),1);
//			SpawnExplosion(ScreenToMapX(0,GetMouseX()), ScreenToMapY(0,GetMouseY()), 5, 0);
  }

  //go through entities to see if we have an intersection
  if (tx!=0 || ty!=0)
  {
		for (i=1;i<max_entities;i++)
		{
			if ((entities[i].active == true || 1==1) && entities[i].type > 0&& entities[i].hp > 0&& entities[i].ambush == false && entities[i].nohit != true)
			{
				if (entities[i].hp3 > 0 || entity_misc[entities[i].type].legs == 0)
				{
					entx = entities[i].x;
					enty = entities[i].y;
					
					entw = entities[i].w;
					enth = entities[i].h;
				}
				else
				{
					entw = entities[i].w;
					enth = entities[i].h/2;
	
					entx = entities[i].x;
					enty = entities[i].y+enth;
				}
			  
			  if (tx!=0)
			  {
					if (entities[0].x < entities[i].x)
						a = (entx-entw-sx)/tx;
					else
						a = (entx+entw-sx)/tx;
				}
				else
					a=-1;
					
			  if (ty!=0)
			  {
					if (entities[0].y < entities[i].y)
						b = (enty-enth-sy)/ty;
					else
						b = (enty+enth-sy)/ty;
				}
				else
					b=-1;
					
				px = sx+a*tx;
				py = sy+a*ty;
				
				if (!(Math.abs(px-entx) <= entw && Math.abs(py-enty) <= enth))
				  a = -1;
	
				px = sx+b*tx;
				py = sy+b*ty;
				if (!(Math.abs(px-entx) <= entw && Math.abs(py-enty) <= enth))
				  b = -1;
					
					
				if ((a < b && a >= 0) || (b <0 && a >= 0))
				{
					px = sx+a*tx;
					py = sy+a*ty;
					
					if (a<min_const && (Math.abs(px-entx) <= entw && Math.abs(py-enty) <= enth))
					{
						if (!(Math.abs(px-sx) <= entw && Math.abs(py-sy) <= enth))
						{
							min_const = a;
							ent_hit = i;
						}
					}

				}
				else if (b >= 0)
				{
					px = sx+b*tx;
					py = sy+b*ty;

					if (b<min_const && (Math.abs(px-entx) <= entw && Math.abs(py-enty) <= enth))
					{
						if (!(Math.abs(px-sx) <= entw && Math.abs(py-sy) <= enth))
						{
							min_const = b;
							ent_hit = i;
						}
					}
				}
			}
		}


		for (i=1;i<max_heads;i++)
		{
			if (((heads[i].x != blood_dx) && entities[i].type > 0) || heads[i].name == "head_shred1head")
			{
			  hentx = heads[i].x;
			  henty = heads[i].y;
			  
			  hentw = 4;
			  henth = 4;
			  
			  if (tx!=0)
			  {
					if (entities[0].x < heads[i].x)
						ha = (hentx-hentw-sx)/tx;
					else
						ha = (hentx+hentw-sx)/tx;
				}
				else
					ha=-1;
					
			  if (ty!=0)
			  {
					if (entities[0].y < heads[i].y)
						hb = (henty-henth-sy)/ty;
					else
						hb = (henty+henth-sy)/ty;
				}
				else
					hb=-1;
					
				hpx = sx+ha*tx;
				hpy = sy+ha*ty;
				
				if (!(Math.abs(hpx-hentx) <= hentw && Math.abs(hpy-henty) <= henth))
				  ha = -1;
	
				hpx = sx+hb*tx;
				hpy = sy+hb*ty;
				if (!(Math.abs(hpx-hentx) <= hentw && Math.abs(hpy-henty) <= henth))
				  hb = -1;
					
					
				if ((ha < hb && ha >= 0) || (hb <0 && ha >= 0))
				{
					hpx = sx+ha*tx;
					hpy = sy+ha*ty;
					
					if (ha<hmin_const && (Math.abs(hpx-hentx) <= hentw && Math.abs(hpy-henty) <= henth))
					{
						if (!(Math.abs(hpx-sx) <= hentw && Math.abs(hpy-sy) <= henth))
						{
							hmin_const = ha;
							head_hit = i;
						}
					}

				}
				else if (hb >= 0)
				{
					hpx = sx+hb*tx;
					hpy = sy+hb*ty;

					if (hb<hmin_const && (Math.abs(hpx-hentx) <= hentw && Math.abs(hpy-henty) <= henth))
					{
						if (!(Math.abs(hpx-sx) <= hentw && Math.abs(hpy-sy) <= henth))
						{
							hmin_const = hb;
							head_hit = i;
						}
					}
				}
			}
		}

		for (i=1;i<max_doors;i++)
		{
			if ((doors[i].x != blood_dx) && doors[i].type == 2)
			{
				if (doors[i].active == 0 && Math.abs(entities[0].x - doors[i].x) < GetScreenWidth()/2+60 && Math.abs(entities[0].y - doors[i].y)<GetScreenHeight()/2+40)
				{
					dentx = doors[i].x;
					denty = doors[i].y;
						
					dentw = 8;
					denth = 8;
						
					if (tx!=0)
					{
						if (entities[0].x < doors[i].x)
							da = (dentx-dentw-sx)/tx;
						else
							da = (dentx+dentw-sx)/tx;
					}
					else
						da=-1;
							
					if (ty!=0)
					{
						if (entities[0].y < doors[i].y)
							db = (denty-denth-sy)/ty;
						else
							db = (denty+denth-sy)/ty;
					}
					else
						db=-1;
						
					dpx = sx+da*tx;
					dpy = sy+da*ty;
						
					if (!(Math.abs(dpx-dentx) <= dentw && Math.abs(dpy-denty) <= denth))
						ha = -1;
			
					dpx = sx+db*tx;
					dpy = sy+db*ty;
					if (!(Math.abs(dpx-dentx) <= dentw && Math.abs(dpy-denty) <= denth))
						db = -1;
							
							
					if ((da < db && da >= 0) || (db <0 && da >= 0))
					{
						dpx = sx+da*tx;
						dpy = sy+da*ty;
							
						if (da<dmin_const && (Math.abs(dpx-dentx) <= dentw && Math.abs(dpy-denty) <= denth))
						{
							if (!(Math.abs(dpx-sx) <= dentw && Math.abs(dpy-sy) <= denth))
							{
								dmin_const = da;
								door_hit = i;
							}
						}
		
					}
					else if (db >= 0)
					{
						dpx = sx+db*tx;
						dpy = sy+db*ty;
		
						if (db<dmin_const && (Math.abs(dpx-dentx) <= dentw && Math.abs(dpy-denty) <= denth))
						{
							if (!(Math.abs(dpx-sx) <= dentw && Math.abs(dpy-sy) <= denth))
							{
								dmin_const = db;
								door_hit = i;
							}
						}
					}
				}
			}
		}


  }
  
	var tile,hx,hy;
	var wipe_temp_hp;
		
	var length = Math.sqrt(tx*tx+ty*ty);

	var fx = tx*3.0/length;
	var fy = ty*3.0/length;
	var nauty = naut1;
	if (g_supernaut_active > 0)
	{
		nauty = ReturnActiveSupernaut();
	}
	var superflag = false;
	var tempcolor;
	var tx1, tx2;
	var ty1, ty2;

	if (ent_hit > 0)
	{
		for (j=0;j<min_const*length/3.0;j++)
		{
			hx=fx*j+sx;
			hy=fy*j+sy;
			if (CheckObstructTile(hx,hy))
			{
				px = hx;
				py = hy;
				
				SpawnSparkSplat(px,py, ammo_array[num].damage*dammulti);

				if (j<hmin_const)
					head_hit = -1;

				return;
			}
		}
	}
	else if (head_hit > 0)
	{
		for (j=0;j<hmin_const*length/3.0;j++)
		{
			hx=fx*j+sx;
			hy=fy*j+sy;
			if (CheckObstructTile(hx,hy))
			{
				px = hx;
				py = hy;
				
				SpawnSparkSplat(px,py, ammo_array[num].damage*dammulti);

				head_hit = -1;

				return;
			}
		}
	}
	else if (door_hit > -1)
	{
		//dmin_const = 1;
		for (j=0;j<dmin_const*length/3.0;j++)
		{
			dx=fx*j+sx;
			dy=fy*j+sy;
			if (CheckObstructTile(dx,dy) || Math.abs(dx-sx)> 170 || Math.abs(dy-sy) > 130)
			{
				px = dx;
				py = dy;
				
				SpawnSparkSplat(px,py, ammo_array[num].damage*dammulti);

				door_hit = -1;

				return;
			}
		}
	}
	else
	{
		for (j=0;j<90;j++)
		{
			hx=fx*j+sx;
			hy=fy*j+sy;
			if (CheckObstructTile(hx,hy))
			{
				px = hx;
				py = hy;
				SpawnSparkSplat(px,py, ammo_array[num].damage*dammulti);
				return;
			}

			if (g_supernaut_active == 1)
			{
				//we have check if we hit the supernaut
					//GetKey();
				if (hx-nauty.x-nauty.hx > 0 && hx-nauty.x-nauty.hx < nauty.width-1 && 
						hy-nauty.y-nauty.hy > 0 && hy-nauty.y-nauty.hy < nauty.height-1)
				{
					//check a more precise hit...
					if (naut_head.getPixel(hx-nauty.x-nauty.hx, hy-nauty.y-nauty.hy).alpha > 0 && nauty.hp1 > 0)
					{
						tempcolor = naut_head_mask.getPixel(hx-nauty.x-nauty.hx, hy-nauty.y-nauty.hy);
						DoSupernautDamage(ammo_array[num].damage*dammulti, "head", tempcolor);
						//it's a hit;
						superflag = true;
					} 
				}
				if (hx-nauty.x-nauty.nx > 0 && hx-nauty.x-nauty.nx < nauty.width-1 && 
						hy-nauty.y-nauty.ny > 0 && hy-nauty.y-nauty.ny < nauty.height-1)
				{
					if (superflag == false && naut_neck_bottom.getPixel(hx-nauty.x-nauty.nx, hy-nauty.y-nauty.ny).alpha > 0 && nauty.hp2 > 0)
					{
						tempcolor = naut_neck_bottom_mask.getPixel(hx-nauty.x-nauty.nx, hy-nauty.y-nauty.ny);
						DoSupernautDamage(ammo_array[num].damage*dammulti, "neck", tempcolor);
						//it's a hit;
						superflag = true;
					} 
				}
				if (hx-nauty.x-nauty.mx > 0 && hx-nauty.x-nauty.mx < nauty.width-1 && 
						hy-nauty.y-nauty.my > 0 && hy-nauty.y-nauty.my < nauty.height-1)
				{
					if (superflag == false && nauty.openmouth == 0)
					{
						if (naut_mouth_1.getPixel(hx-nauty.x-nauty.mx, hy-nauty.y-nauty.my).alpha > 0 && nauty.hp3 > 0)
						{
							if (naut_mouth_mask_1.getPixel(hx-nauty.x-nauty.mx, hy-nauty.y-nauty.my).red > 0 ||
							naut_mouth_mask_1.getPixel(hx-nauty.x-nauty.mx, hy-nauty.y-nauty.my).green > 0 ||
							naut_mouth_mask_1.getPixel(hx-nauty.x-nauty.mx, hy-nauty.y-nauty.my).blue > 0)
							{
								tempcolor = naut_mouth_mask_1.getPixel(hx-nauty.x-nauty.mx, hy-nauty.y-nauty.my);
								DoSupernautDamage(ammo_array[num].damage*dammulti, "mouth_1", tempcolor);
								//it's a hit;
								superflag = true;
							}
						} 					
					}
					if (superflag == false && nauty.openmouth == 1)
					{
						if (naut_mouth_2.getPixel(hx-nauty.x-nauty.mx, hy-nauty.y-nauty.my).alpha > 0 && nauty.hp3 > 0)
						{
							if (naut_mouth_mask_2.getPixel(hx-nauty.x-nauty.mx, hy-nauty.y-nauty.my).red > 0 ||
							naut_mouth_mask_2.getPixel(hx-nauty.x-nauty.mx, hy-nauty.y-nauty.my).green > 0 ||
							naut_mouth_mask_2.getPixel(hx-nauty.x-nauty.mx, hy-nauty.y-nauty.my).blue > 0)
							{
								tempcolor = naut_mouth_mask_2.getPixel(hx-nauty.x-nauty.mx, hy-nauty.y-nauty.my);
								DoSupernautDamage(ammo_array[num].damage*dammulti, "mouth_2", tempcolor);
								//it's a hit;
								superflag = true;
							}
						} 					
					}
					if (superflag == true)
					{
						SpawnBloodSplat(hx,hy,4*Math.random()-1,-4-2*Math.random(), ammo_array[num].damage*dammulti);
						return;
					}
				}
			}
			else if (g_supernaut_active == 2)
			{
				//we have check if we hit the supernaut
					//GetKey();
				if (hx-nauty.x-nauty.hx > 0 && hx-nauty.x-nauty.hx < nauty.width-1 && 
						hy-nauty.y-nauty.hy > 0 && hy-nauty.y-nauty.hy < nauty.height-1)
				{
					//check a more precise hit...
					if (naut2_head.getPixel(hx-nauty.x-nauty.hx, hy-nauty.y-nauty.hy).alpha > 0 && nauty.hp1 > 0)
					{
						tempcolor = naut2_head_mask.getPixel(hx-nauty.x-nauty.hx, hy-nauty.y-nauty.hy);
						DoSupernautDamage(ammo_array[num].damage*dammulti, "head", tempcolor);
						//it's a hit;
						superflag = true;
					} 
				}
/*				if (hx-nauty.x-nauty.nx > 0 && hx-nauty.x-nauty.nx < nauty.width-1 && 
						hy-nauty.y-nauty.ny > 0 && hy-nauty.y-nauty.ny < nauty.height-1)
				{
					if (superflag == false && naut2_neck_top.getPixel(hx-nauty.x-nauty.nx, hy-nauty.y-nauty.ny).alpha > 0 && nauty.hp2 > 0)
					{
						tempcolor = naut2_neck_top_mask.getPixel(hx-nauty.x-nauty.nx, hy-nauty.y-nauty.ny);
						DoSupernautDamage(ammo_array[num].damage*dammulti, "neck", tempcolor);
						//it's a hit;
						superflag = true;
					} 
				}
*/
				if (hx-nauty.x-nauty.mx > 0 && hx-nauty.x-nauty.mx < nauty.width-1 && 
						hy-nauty.y-nauty.my > 0 && hy-nauty.y-nauty.my < nauty.height-1)
				{
					if (superflag == false && nauty.openmouth == 0)
					{
						if (naut2_mouth_1.getPixel(hx-nauty.x-nauty.mx, hy-nauty.y-nauty.my).alpha > 0 && nauty.hp3 > 0)
						{
							if (naut2_mouth_mask_1.getPixel(hx-nauty.x-nauty.mx, hy-nauty.y-nauty.my).red > 0 ||
							naut2_mouth_mask_1.getPixel(hx-nauty.x-nauty.mx, hy-nauty.y-nauty.my).green > 0 ||
							naut2_mouth_mask_1.getPixel(hx-nauty.x-nauty.mx, hy-nauty.y-nauty.my).blue > 0)
							{
								tempcolor = naut2_mouth_mask_1.getPixel(hx-nauty.x-nauty.mx, hy-nauty.y-nauty.my);
								DoSupernautDamage(ammo_array[num].damage*dammulti, "mouth_1", tempcolor);
								//it's a hit;
								superflag = true;
							}
						} 					
					}
					if (superflag == false && nauty.openmouth == 1)
					{
						if (naut2_mouth_2.getPixel(hx-nauty.x-nauty.mx, hy-nauty.y-nauty.my).alpha > 0 && nauty.hp3 > 0)
						{
							if (naut2_mouth_mask_2.getPixel(hx-nauty.x-nauty.mx, hy-nauty.y-nauty.my).red > 0 ||
							naut2_mouth_mask_2.getPixel(hx-nauty.x-nauty.mx, hy-nauty.y-nauty.my).green > 0 ||
							naut2_mouth_mask_2.getPixel(hx-nauty.x-nauty.mx, hy-nauty.y-nauty.my).blue > 0)
							{
								tempcolor = naut2_mouth_mask_2.getPixel(hx-nauty.x-nauty.mx, hy-nauty.y-nauty.my);
								DoSupernautDamage(ammo_array[num].damage*dammulti, "mouth_2", tempcolor);
								//it's a hit;
								superflag = true;
							}
						} 					
					}
					if (superflag == true)
					{
						SpawnBloodSplat(hx,hy,4*Math.random()-1,-4-2*Math.random(), ammo_array[num].damage*dammulti);
						return;
					}
				}
			}
			
			
		}
	}
	
  if (ent_hit > 0)
  {
		px = sx+min_const*tx;
		py = sy+min_const*ty;
		
		//check to see if the bullet hit an obstruction

		//passed the obstruction test.
		//compute area hit;
		
		if (entities[ent_hit].hp3 > 0 || entity_misc[entities[ent_hit].type].damage == 0)
		{
			//head
			if (py <= entities[ent_hit].y-2)
				hit_type = 1;
			//legs
			else if (py >= entities[ent_hit].y+6)
				hit_type = 2;
			//an arm
			else 
				hit_type = 3;

	
			if (entity_misc[entities[ent_hit].type].damage == 0 || (entities[ent_hit].type == 8 && entities[ent_hit].hp2 <=0))
				hit_type = 1;
			else if (entity_misc[entities[ent_hit].type].damage == 2)
				hit_type = 3;
			else if (entity_misc[entities[ent_hit].type].damage == 3)
			{
				if (hit_type == 2)
					hit_type = 3;
			}
			//deal damage
			//remove limb if needed.
			
			if (hit_type == 1)
			{
				if (num == 12)
					entities[ent_hit].hp-=ammo_array[num].damage*dammulti*(2/3);
				else
					entities[ent_hit].hp-=ammo_array[num].damage*dammulti;
			}
			else if (hit_type == 2)
			{
				entities[ent_hit].hp3-=ammo_array[num].damage*dammulti;
				if (entities[ent_hit].hp3 <= 0)
				{
					if (entities[ent_hit].hp1 <=0 && entities[ent_hit].hp2 <=0)
					{
					  entities[ent_hit].hp = 0;
					}
					else
					{
						entities[ent_hit].hp3 = 0;
						
						var tsx,tsy;
						var randy= Math.random();
						var tlength = Math.sqrt(tx*tx+ty*ty);
						tsx = 3*Math.log(ammo_array[num].damage*dammulti+3*randy+1)/Math.LN10*tx/tlength;
						tsy = 3*Math.log(ammo_array[num].damage*dammulti+3*randy+1)/Math.LN10*ty/tlength-5;
					
						SpawnGuts(ent_hit, entities[ent_hit].x,entities[ent_hit].y,tsx-1+2*Math.random(),tsy-7+2*Math.random(), "enemy1_leg", 0, gut_layer+Math.floor(2*Math.random()), entities[ent_hit].mask);
						SpawnGuts(ent_hit, entities[ent_hit].x,entities[ent_hit].y,tsx-1+2*Math.random(),tsy-7+2*Math.random(), "enemy1_leg", 0, gut_layer+Math.floor(2*Math.random()), entities[ent_hit].mask);
						//remove this arm;
						entities[ent_hit].dir2 = "legfall1";
						entities[ent_hit].frame2 = 0;
					}
				}
			}
			else if (hit_type == 3)
			{
				if (entities[ent_hit]["hp"+entities[ent_hit].farm] > 0 || entities[ent_hit].type == 4 || entities[ent_hit].type == 6)
				{
					if (entities[ent_hit]["hp"+entities[ent_hit].farm] <=0 && entities[ent_hit].type == 6)
					{
						//do nothing

					}
					else
					{
						wipe_temp_hp = entities[ent_hit]["hp"+entities[ent_hit].farm];
						entities[ent_hit]["hp"+entities[ent_hit].farm]-=ammo_array[num].damage*dammulti;
	
						if (entities[ent_hit]["hp"+entities[ent_hit].farm] <=0)
						{
							if (entities[ent_hit].type != 4)
							{
								var tsx,tsy;
								var randy= Math.random();
								var tlength = Math.sqrt(tx*tx+ty*ty);
								
								tsx = 3*Math.log(ammo_array[num].damage*dammulti+3*randy+1)/Math.LN10*tx/tlength;
								tsy = 3*Math.log(ammo_array[num].damage*dammulti+3*randy+1)/Math.LN10*ty/tlength-5;
								SpawnArm(ent_hit, px,py,tsx,tsy, gut_layer+1*((entities[ent_hit].farm+1)%2), entities[ent_hit].mask);
								if (entities[ent_hit].type == 8)
								{
									for (d=0;d<2;d++)
									{
										randy= Math.random();
										tsx = 3*Math.log(ammo_array[num].damage*dammulti+3*randy+1)/Math.LN10*tx/tlength;
										tsy = 3*Math.log(ammo_array[num].damage*dammulti+3*randy+1)/Math.LN10*ty/tlength-5;
										SpawnArm(ent_hit, px,py,tsx,tsy, gut_layer+1*((entities[ent_hit].farm+d)%2), entities[ent_hit].mask);
									}
								}
	
	
								if (entities[ent_hit].type == 6)
								{
									randy = Math.random();
									tsx = 3*Math.log(ammo_array[num].damage*dammulti+3*randy+1)/Math.LN10*tx/tlength;
									tsy = 3*Math.log(ammo_array[num].damage*dammulti+3*randy+1)/Math.LN10*ty/tlength-5;
									SpawnArm(ent_hit, px,py,tsx,tsy, gut_layer+1*((entities[ent_hit].farm+1)%2), entities[ent_hit].mask);
								}
								
								//remove this arm;
							}
							else if (wipe_temp_hp > 0)
							{
								StunWipe(ent_hit);
							}
						}
					}
				}
				else if (entities[ent_hit]["hp"+(entities[ent_hit].farm%2+1)] > 0)
				{
					entities[ent_hit]["hp"+(entities[ent_hit].farm%2+1)]-=ammo_array[num].damage*dammulti;
		
					if (entities[ent_hit]["hp"+(entities[ent_hit].farm%2+1)] <=0)
					{
						var tsx,tsy;
						var randy= Math.random();
						var tlength = Math.sqrt(tx*tx+ty*ty);
						tsx = 3*Math.log(ammo_array[num].damage*dammulti+3*randy+1)/Math.LN10*tx/tlength;
						tsy = 3*Math.log(ammo_array[num].damage*dammulti+3*randy+1)/Math.LN10*ty/tlength-5;
						
						
						SpawnArm(ent_hit, px,py,tsx,tsy,gut_layer+1*((entities[ent_hit].farm)%2), entities[ent_hit].mask);
						//remove this arm;
					}
				}
				else
					entities[ent_hit].hp-=ammo_array[num].damage*dammulti/3;
			}
			
			if (entities[ent_hit].hp <= 0)
			{
				var hsx,hsy;
				var hrandy= Math.random();
				var hlength = Math.sqrt(tx*tx+ty*ty);
				hsx = 3*Math.log(ammo_array[num].damage*dammulti+3*hrandy+1)/Math.LN10*tx/hlength;
				hsy = 3*Math.log(ammo_array[num].damage*dammulti+3*hrandy+1)/Math.LN10*ty/hlength-5;
			
			
				(Math.random()*5+ammo_array[num].damage*dammulti)/10
			
				entities[ent_hit].hp = 0;
				if (ammo_array[num].damage*dammulti >= 5 && ammo_array[num].damage*dammulti <10)
					SpawnHead(ent_hit,px,py, hsx,hsy, Math.floor((Math.random()*10+ammo_array[num].damage*dammulti)/10));//Math.sqrt(Math.random()*2)));
				else if (ammo_array[num].damage*dammulti >=10)
					SpawnHead(ent_hit,px,py, hsx,hsy, 1);
				else 
					SpawnHead(ent_hit,px,py, hsx,hsy, 0);
			}
			
			if (entity_misc[entities[ent_hit].type].blood == 1)
			{
				if (entities[ent_hit].type == 6 && entities[ent_hit].hp1 <=0)
					SpawnSparkSplat(px,py, ammo_array[num].damage*dammulti);
				else if (num == 0)
					SpawnBloodSplat(px,py,tx,ty, 1);
				else if (num == 2|| num == 4)
					SpawnBloodSplat(px,py,tx,ty, 2.9);
				else if (num == 3 ||num == 7)
					SpawnBloodSplat(px,py,tx,ty, 5);
				else if (num == 6 || num == 8)
					SpawnBloodSplat(px,py,tx,ty, 8);
				else if (num == 9 || num == 11 || num == 12)
					SpawnBloodSplat(px,py,tx,ty, 8);
			}
			else
			{
				SpawnSparkSplat(px,py, ammo_array[num].damage*dammulti);
			}
			//spawn blood here;
		}
		else
		{
		  var army = 1;
			//legless hit.
			if (entities[ent_hit].hp2 > 0)
				army = 2;
			else
				army = 1;

			entities[ent_hit]["hp"+army]-=ammo_array[num].damage*dammulti/3;
			
			if (entities[ent_hit]["hp"+army] <= 0)
			{
				entities[ent_hit]["hp"+army] = 0;

				var hsx,hsy;
				var hrandy= Math.random();
				var hlength = Math.sqrt(tx*tx+ty*ty);
				hsx = 3*Math.log(ammo_array[num].damage*dammulti+3*hrandy+1)/Math.LN10*tx/hlength;
				hsy = 3*Math.log(ammo_array[num].damage*dammulti+3*hrandy+1)/Math.LN10*ty/hlength-5;
			
				SpawnArm(ent_hit, px,py,hsx,hsy,gut_layer+1*((entities[ent_hit].farm)%2), entities[ent_hit].mask);
				
				if (entities[ent_hit]["hp"+((army)%2+1)] <= 0)
				{
					entities[ent_hit].hp = 0;
					if (ammo_array[num].damage*dammulti >= 5 && ammo_array[num].damage*dammulti <10)
						SpawnHead(ent_hit,px,py, hsx,hsy, Math.floor((Math.random()*10+ammo_array[num].damage*dammulti)/10));//Math.sqrt(Math.random()*2)));
					else if (ammo_array[num].damage*dammulti >=10)
						SpawnHead(ent_hit,px,py, hsx,hsy, 1);
					else 
						SpawnHead(ent_hit,px,py, hsx,hsy, 0);
				}
			}
			
			if (num == 0)
				SpawnBloodSplat(px,py,tx,ty, 1);
			else if (num == 2|| num == 4)
				SpawnBloodSplat(px,py,tx,ty, 2.9);
			else if (num == 3 ||num == 7)
				SpawnBloodSplat(px,py,tx,ty, 5);
			else if (num == 6 || num == 8)
				SpawnBloodSplat(px,py,tx,ty, 8);
			else if (num == 9 || num == 11)
				SpawnBloodSplat(px,py,tx,ty, 8);
			//spawn blood here;
		}
  }
  
  if (head_hit > 0)
  {
		heads[head_hit].stop = false;
		heads[head_hit].vx += fx;
		if (heads[head_hit].vx > max_headvx)
			heads[head_hit].vx = max_headvx;
		if (heads[head_hit].vx < -max_headvx)
			heads[head_hit].vx = -max_headvx;

		heads[head_hit].vy += fy-6;
		if (heads[head_hit].vy > max_headvy)
			heads[head_hit].vy = max_headvy;
		if (heads[head_hit].vy < -max_headvy)
			heads[head_hit].vy = -max_headvy;
  }
  
  if (door_hit > -1)
  {
		HitSwitch(doors[door_hit].x, doors[door_hit].y);  
		SpawnSparkSplat(doors[door_hit].x, doors[door_hit].y, ammo_array[num].damage*dammulti);
  }
}


function CheckObstructTile(x,y)
{
	var tilew = GetTileWidth();
	var tileh = GetTileHeight();

	if (x<0 || y<0|| x > tilew*GetLayerWidth(0)|| y > tileh*GetLayerHeight(0))
		return true;
		
	var tile = GetTile(Math.floor(x/tilew),Math.floor(y/tileh), 0);
	if (tile == 1||tile == 4)
	  return true;
	else if (tile == 2 && sy -Math.floor(sy/tileh)>= 5)
	  return true;
	return false;
}


function GetGrenadeVelo(type, rand)
{
	var speed = 6;
	
	var cx = MapToScreenX(0,entities[0].x);
	var cy = MapToScreenY(0,entities[0].y);
	var length = Math.sqrt((GetMouseX()-cx)*(GetMouseX()-cx)+(GetMouseY()-cy+4)*(GetMouseY()-cy+4));

	var rval = new Object();
	if (type == 1)
	{
		rval.vx = speed*(GetMouseX()-cx)/length;
		rval.vy = speed*(GetMouseY()-cy+4)/length-Math.abs(length)/25-1;
	}
	else if (type == 2)
	{
	  speed = 8;
		length = Math.sqrt((GetMouseX()-cx)*(GetMouseX()-cx)+(GetMouseY()-cy)*(GetMouseY()-cy));
		rval.vx = speed*(GetMouseX()-cx)/length;
		rval.vy = speed*(GetMouseY()-cy)/length-Math.abs(length)/40-1;
	}
	else if (type == 3)
	{
	  speed = 8;
		rval.vx = speed*(GetMouseX()-cx)/length;
		rval.vy = speed*(GetMouseY()-cy+4)/length;
		
		
		
		if (rand==true)
		{
			speed = 7+Math.random()*2;
			
			var theta = (3.1459/20)*(Math.random()*2-1);
			
			rval.vx = speed*(GetMouseX()-cx)/length;
			rval.vy = speed*(GetMouseY()-cy+4)/length;
	
			rval.vx = rval.vx*Math.cos(theta)-rval.vy*Math.sin(theta);
			rval.vy = rval.vx*Math.sin(theta)+rval.vy*Math.cos(theta);
		
		}
	}
	else if (type == 4)
	{
	  speed = 4;
	  
	  var theta = (3.1459/20)*(Math.random()*2-1);
	  
		rval.vx = speed*(GetMouseX()-cx)/length;
		rval.vy = speed*(GetMouseY()-cy+4)/length-Math.abs(length)/160;

		rval.vx = rval.vx*Math.cos(theta)-rval.vy*Math.sin(theta);
		rval.vy = rval.vx*Math.sin(theta)+rval.vy*Math.cos(theta);
		
	}
	return rval;
}










function FireRailGun(num,tx,ty, angle)
{
	var cx = MapToScreenX(0, entities[0].x);
	var cy = MapToScreenY(0, entities[0].y);

	var nauty = naut1;
	if (g_supernaut_active > 0)
	{
		nauty = ReturnActiveSupernaut();
	}


	if (entities[0].dir == 1)
		var	angle = Math.atan2(GetMouseY()-cy,cx-GetMouseX())+3.14159;
	else
		var	angle = Math.atan2(GetMouseY()-cy,GetMouseX()-cx)+3.14159;
					
	angle/=(2*3.14159);
					
	angle*=16;
	angle+=0.5;
	angle = Math.floor(angle);

	if (angle > 15)
		angle = 0;

  if (num == undefined)
    num = 0;

  //determine line of fire:
  if (angle == undefined)
  {
		var sx = entities[num].x;
		var sy = entities[num].y-4;
  }
  else
  {

		if (entities[0].dir == -1)
		{
			var sx = entities[num].x-gunpos[7][angle].x+3;
			var sy = entities[num].y+gunpos[7][angle].y-4;
		}
		else
		{
			var sx = entities[num].x+gunpos[7][angle].x;
			var sy = entities[num].y+gunpos[7][angle].y-4;
		}
  }
  
  var sx2 = sx;
  var sy2 = sy;
  
  
  if (tx == undefined)
  {
		tx = GetMouseX()-MapToScreenX(0, entities[0].x);
		ty = GetMouseY()-MapToScreenY(0, entities[0].y-4);
	}
		
  var entx, enty;
  var entw, enth;

  var hentx, henty;
  var hentw, henth;

  var dentx, denty;
  var dentw, denth;

  var a,b;
  var ha,hb;
  var px,py;
  var hpx,hpy;
  var dpx,dpy;

  if (tx==0 && ty==0)
    tx = 1;
    
  var dammulti = 1;
  if (adren_timer > 0)
		dammulti = 1.25;

    
  var ent_hit = -1;
  var head_hit = -1;
  var door_hit = -1;
  var min_const = 1000000;
  var hmin_const = 1000000;
  var dmin_const = 1000000;
  
  var hit_type = 0;
  
  var overridey = 0;
  
  var mmi= -4/(Math.sqrt(tx*tx+ty*ty));

  //go through entities to see if we have an intersection
  if (tx!=0 || ty!=0)
  {
		for (i=0;i<max_entities;i++)
		{
			overridey = 0;
			if ((entities[i].active == true || 1==1) && entities[i].type > 0&& entities[i].hp > 0 && entities[i].ambush == false&& i != num && entities[i].nohit != true)
			{
				if (entities[i].hp3 > 0 || entity_misc[entities[i].type].legs == 0)
				{
					entx = entities[i].x;
					enty = entities[i].y;
					
					entw = entities[i].w;
					enth = entities[i].h;
				}
				else
				{
					entw = entities[i].w;
					enth = entities[i].h/2;
	
					entx = entities[i].x;
					enty = entities[i].y+enth;
				}

			  
			  if (tx!=0)
			  {
					if (entities[0].x < entities[i].x-entw)
						a = (entx-entw-sx)/tx;
					else if (entities[0].x > entities[i].x+entw)
						a = (entx+entw-sx)/tx;
					else
						a = (entx-sx)/tx;
				}
				else
					a=mmi;
					
			  if (ty!=0)
			  {
					if (entities[0].y < entities[i].y-enth)
						b = (enty-enth-sy)/ty;
					else if (entities[0].y > entities[i].y+enth)
						b = (enty+enth-sy)/ty;
					else
						b = (enty-sy)/ty;
				}
				else
					b=mmi;
					
				px = sx+a*tx;
				py = sy+a*ty;
				
				if (!(Math.abs(px-entx) <= entw && Math.abs(py-enty) <= enth))
				  a = mmi;
	
				px = sx+b*tx;
				py = sy+b*ty;
				if (!(Math.abs(px-entx) <= entw && Math.abs(py-enty) <= enth))
				  b = mmi;
				  
				if (Math.abs(sx-entx) <= entw+16 && Math.abs(sy-enty) <= enth+12)
				{
					if (tx!=0)
						a=0;
					else
						b=0;
				
					overridey=1
				}
					
					
				if ((a < b && a >mmi) || (b <=mmi&& a >mmi))
				{
					px = sx+a*tx;
					py = sy+a*ty;
					
					if (a<min_const && ((Math.abs(px-entx) <= entw && Math.abs(py-enty) <= enth)||overridey))
					{
						if (!(Math.abs(px-sx) <= entw && Math.abs(py-sy) <= enth)||overridey)
						{
						  entities[i].min_const = a;
						}
					}

				}
				else if (b >mmi)
				{
					px = sx+b*tx;
					py = sy+b*ty;

					if (b<min_const && ((Math.abs(px-entx) <= entw && Math.abs(py-enty) <= enth)||overridey))
					{
						if (!(Math.abs(px-sx) <= entw && Math.abs(py-sy) <= enth)||overridey)
						{
						  entities[i].min_const = b;
						}
					}
				}
			}
		}


		for (i=1;i<max_heads;i++)
		{
			if (((heads[i].x != blood_dx) && entities[i].type > 0) || heads[i].name == "head_shred1head")
			{
			  hentx = heads[i].x;
			  henty = heads[i].y;
			  
			  hentw = 4;
			  henth = 4;
			  
			  if (tx!=0)
			  {
					if (entities[0].x < heads[i].x)
						ha = (hentx-hentw-sx)/tx;
					else
						ha = (hentx+hentw-sx)/tx;
				}
				else
					ha=-1;
					
			  if (ty!=0)
			  {
					if (entities[0].y < heads[i].y)
						hb = (henty-henth-sy)/ty;
					else
						hb = (henty+henth-sy)/ty;
				}
				else
					hb=-1;
					
				hpx = sx+ha*tx;
				hpy = sy+ha*ty;
				
				if (!(Math.abs(hpx-hentx) <= hentw && Math.abs(hpy-henty) <= henth))
				  ha = -1;
	
				hpx = sx+hb*tx;
				hpy = sy+hb*ty;
				if (!(Math.abs(hpx-hentx) <= hentw && Math.abs(hpy-henty) <= henth))
				  hb = -1;
					
					
				if ((ha < hb && ha >= 0) || (hb <0 && ha >= 0))
				{
					hpx = sx+ha*tx;
					hpy = sy+ha*ty;
					
					if (ha<hmin_const && (Math.abs(hpx-hentx) <= hentw && Math.abs(hpy-henty) <= henth))
					{
						if (!(Math.abs(hpx-sx) <= hentw && Math.abs(hpy-sy) <= henth))
						{
						  heads[i].min_const = ha;
						}
					}

				}
				else if (hb >= 0)
				{
					hpx = sx+hb*tx;
					hpy = sy+hb*ty;

					if (hb<hmin_const && (Math.abs(hpx-hentx) <= hentw && Math.abs(hpy-henty) <= henth))
					{
						if (!(Math.abs(hpx-sx) <= hentw && Math.abs(hpy-sy) <= henth))
						{
						  heads[i].min_const = hb;
						}
					}
				}
			}
		}

		for (i=1;i<max_doors;i++)
		{
			doors[i].min_const = -1;

			if ((doors[i].x != blood_dx) && doors[i].type == 2)
			{
				if (doors[i].active == 0)
				{
					doors[i].min_const = dmin_const;
					dentx = doors[i].x;
					denty = doors[i].y;
						
					dentw = 8;
					denth = 8;
						
					if (tx!=0)
					{
						if (entities[0].x < doors[i].x)
							da = (dentx-dentw-sx)/tx;
						else
							da = (dentx+dentw-sx)/tx;
					}
					else
						da=-1;
							
					if (ty!=0)
					{
						if (entities[0].y < doors[i].y)
							db = (denty-denth-sy)/ty;
						else
							db = (denty+denth-sy)/ty;
					}
					else
						db=-1;
						
					dpx = sx+da*tx;
					dpy = sy+da*ty;
						
					if (!(Math.abs(dpx-dentx) <= dentw && Math.abs(dpy-denty) <= denth))
						ha = -1;
			
					dpx = sx+db*tx;
					dpy = sy+db*ty;
					if (!(Math.abs(dpx-dentx) <= dentw && Math.abs(dpy-denty) <= denth))
						db = -1;
							
							
					if ((da < db && da >= 0) || (db <0 && da >= 0))
					{
						dpx = sx+da*tx;
						dpy = sy+da*ty;
							
						if (da<dmin_const && (Math.abs(dpx-dentx) <= dentw && Math.abs(dpy-denty) <= denth))
						{
							if (!(Math.abs(dpx-sx) <= dentw && Math.abs(dpy-sy) <= denth))
							{
								doors[i].min_const = da;
								door_hit = i;
							}
						}
		
					}
					else if (db >= 0)
					{
						dpx = sx+db*tx;
						dpy = sy+db*ty;
		
						if (db<dmin_const && (Math.abs(dpx-dentx) <= dentw && Math.abs(dpy-denty) <= denth))
						{
							if (!(Math.abs(dpx-sx) <= dentw && Math.abs(dpy-sy) <= denth))
							{
								doors[i].min_const = db;
								door_hit = i;
							}
						}
					}
				}
			}
		}


  }
  
	var tile,hx,hy;
		
	var length = Math.sqrt(tx*tx+ty*ty);

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

	var superflag = false;
	for (aj=0;aj<90;aj++)
	{
		if (superflag == true)
		{
			px = hx;
			py = hy;
			SpawnSparkSplat(px,py);
			break;
		}

		hx=fx*aj+sx;
		hy=fy*aj+sy;

		if (CheckObstructTile(hx,hy))
		{
			px = hx;
			py = hy;
			SpawnSparkSplat(px,py);
			break;
		}


			if (g_supernaut_active == 1)
			{
				//we have check if we hit the supernaut
					//GetKey();
				if (hx-nauty.x-nauty.hx > 0 && hx-nauty.x-nauty.hx < nauty.width-1 && 
						hy-nauty.y-nauty.hy > 0 && hy-nauty.y-nauty.hy < nauty.height-1)
				{
					//check a more precise hit...
					if (naut_head.getPixel(hx-nauty.x-nauty.hx, hy-nauty.y-nauty.hy).alpha > 0 && nauty.hp1 > 0)
					{
						tempcolor = naut_head_mask.getPixel(hx-nauty.x-nauty.hx, hy-nauty.y-nauty.hy);
						DoSupernautDamage(ammo_array[num].damage*dammulti, "head", tempcolor);
						//it's a hit;
						superflag = true;
					} 
				}
				if (hx-nauty.x-nauty.nx > 0 && hx-nauty.x-nauty.nx < nauty.width-1 && 
						hy-nauty.y-nauty.ny > 0 && hy-nauty.y-nauty.ny < nauty.height-1)
				{
					if (superflag == false && naut_neck_bottom.getPixel(hx-nauty.x-nauty.nx, hy-nauty.y-nauty.ny).alpha > 0 && nauty.hp2 > 0)
					{
						tempcolor = naut_neck_bottom_mask.getPixel(hx-nauty.x-nauty.nx, hy-nauty.y-nauty.ny);
						DoSupernautDamage(ammo_array[num].damage*dammulti, "neck", tempcolor);
						//it's a hit;
						superflag = true;
					} 
				}
				if (hx-nauty.x-nauty.mx > 0 && hx-nauty.x-nauty.mx < nauty.width-1 && 
						hy-nauty.y-nauty.my > 0 && hy-nauty.y-nauty.my < nauty.height-1)
				{
					if (superflag == false && nauty.openmouth == 0)
					{
						if (naut_mouth_1.getPixel(hx-nauty.x-nauty.mx, hy-nauty.y-nauty.my).alpha > 0 && nauty.hp3 > 0)
						{
							if (naut_mouth_mask_1.getPixel(hx-nauty.x-nauty.mx, hy-nauty.y-nauty.my).red > 0 ||
							naut_mouth_mask_1.getPixel(hx-nauty.x-nauty.mx, hy-nauty.y-nauty.my).green > 0 ||
							naut_mouth_mask_1.getPixel(hx-nauty.x-nauty.mx, hy-nauty.y-nauty.my).blue > 0)
							{
								tempcolor = naut_mouth_mask_1.getPixel(hx-nauty.x-nauty.mx, hy-nauty.y-nauty.my);
								DoSupernautDamage(ammo_array[num].damage*dammulti, "mouth_1", tempcolor);
								//it's a hit;
								superflag = true;
							}
						} 					
					}
					if (superflag == false && nauty.openmouth == 1)
					{
						if (naut_mouth_2.getPixel(hx-nauty.x-nauty.mx, hy-nauty.y-nauty.my).alpha > 0 && nauty.hp3 > 0)
						{
							if (naut_mouth_mask_2.getPixel(hx-nauty.x-nauty.mx, hy-nauty.y-nauty.my).red > 0 ||
							naut_mouth_mask_2.getPixel(hx-nauty.x-nauty.mx, hy-nauty.y-nauty.my).green > 0 ||
							naut_mouth_mask_2.getPixel(hx-nauty.x-nauty.mx, hy-nauty.y-nauty.my).blue > 0)
							{
								tempcolor = naut_mouth_mask_2.getPixel(hx-nauty.x-nauty.mx, hy-nauty.y-nauty.my);
								DoSupernautDamage(ammo_array[num].damage*dammulti, "mouth_2", tempcolor);
								//it's a hit;
								superflag = true;
							}
						} 					
					}
					if (superflag == true)
					{
						SpawnBloodSplat(hx,hy,4*Math.random()-1,-4-2*Math.random(), ammo_array[num].damage*dammulti);
//						return;
					}
				}
			}
			else if (g_supernaut_active == 2)
			{
				//we have check if we hit the supernaut
					//GetKey();
				if (hx-nauty.x-nauty.hx > 0 && hx-nauty.x-nauty.hx < nauty.width-1 && 
						hy-nauty.y-nauty.hy > 0 && hy-nauty.y-nauty.hy < nauty.height-1)
				{
					//check a more precise hit...
					if (naut2_head.getPixel(hx-nauty.x-nauty.hx, hy-nauty.y-nauty.hy).alpha > 0 && nauty.hp1 > 0)
					{
						tempcolor = naut2_head_mask.getPixel(hx-nauty.x-nauty.hx, hy-nauty.y-nauty.hy);
						DoSupernautDamage(ammo_array[num].damage*dammulti, "head", tempcolor);
						//it's a hit;
						superflag = true;
					} 
				}
				if (hx-nauty.x-nauty.nx > 0 && hx-nauty.x-nauty.nx < nauty.width-1 && 
						hy-nauty.y-nauty.ny > 0 && hy-nauty.y-nauty.ny < nauty.height-1)
				{
					if (superflag == false && naut2_neck_top.getPixel(hx-nauty.x-nauty.nx, hy-nauty.y-nauty.ny).alpha > 0 && nauty.hp2 > 0)
					{
						tempcolor = naut2_neck_top_mask.getPixel(hx-nauty.x-nauty.nx, hy-nauty.y-nauty.ny);
						DoSupernautDamage(ammo_array[num].damage*dammulti, "neck", tempcolor);
						//it's a hit;
						superflag = true;
					} 
				}
				if (hx-nauty.x-nauty.mx > 0 && hx-nauty.x-nauty.mx < nauty.width-1 && 
						hy-nauty.y-nauty.my > 0 && hy-nauty.y-nauty.my < nauty.height-1)
				{
					if (superflag == false && nauty.openmouth == 0)
					{
						if (naut2_mouth_1.getPixel(hx-nauty.x-nauty.mx, hy-nauty.y-nauty.my).alpha > 0 && nauty.hp3 > 0)
						{
							if (naut2_mouth_mask_1.getPixel(hx-nauty.x-nauty.mx, hy-nauty.y-nauty.my).red > 0 ||
							naut2_mouth_mask_1.getPixel(hx-nauty.x-nauty.mx, hy-nauty.y-nauty.my).green > 0 ||
							naut2_mouth_mask_1.getPixel(hx-nauty.x-nauty.mx, hy-nauty.y-nauty.my).blue > 0)
							{
								tempcolor = naut2_mouth_mask_1.getPixel(hx-nauty.x-nauty.mx, hy-nauty.y-nauty.my);
								DoSupernautDamage(ammo_array[num].damage*dammulti, "mouth_1", tempcolor);
								//it's a hit;
								superflag = true;
							}
						} 					
					}
					if (superflag == false && nauty.openmouth == 1)
					{
						if (naut2_mouth_2.getPixel(hx-nauty.x-nauty.mx, hy-nauty.y-nauty.my).alpha > 0 && nauty.hp3 > 0)
						{
							if (naut2_mouth_mask_2.getPixel(hx-nauty.x-nauty.mx, hy-nauty.y-nauty.my).red > 0 ||
							naut2_mouth_mask_2.getPixel(hx-nauty.x-nauty.mx, hy-nauty.y-nauty.my).green > 0 ||
							naut2_mouth_mask_2.getPixel(hx-nauty.x-nauty.mx, hy-nauty.y-nauty.my).blue > 0)
							{
								tempcolor = naut2_mouth_mask_2.getPixel(hx-nauty.x-nauty.mx, hy-nauty.y-nauty.my);
								DoSupernautDamage(ammo_array[num].damage*dammulti, "mouth_2", tempcolor);
								//it's a hit;
								superflag = true;
							}
						} 					
					}
					if (superflag == true)
					{
						SpawnBloodSplat(hx,hy,4*Math.random()-1,-4-2*Math.random(), ammo_array[num].damage*dammulti);
//						return;
					}
				}
			}
			

	}
	for (i=0;i<2;i++)
	{
		SpawnRailgun(sx2,sy2,hx-3+6*Math.random(),hy-3+6*Math.random());
	}

	aj = 3.0*aj/length;
	
	for (i=0;i<max_entities;i++)
	{
		if (entities[i].min_const<aj && entities[i].hp > 0&& entities[i].ambush == false)
		{	
			px = sx+entities[i].min_const*tx;
			py = sy+entities[i].min_const*ty;
			
			//check to see if the bullet hit an obstruction
	
			//passed the obstruction test.
			//compute area hit;

	
			if (entities[i].hp3 > 0 || entity_misc[entities[i].type].damage == 0)
			{
				//head
				if (py <= entities[i].y-2)
					hit_type = 1;
				//legs
				else if (py >= entities[i].y+6)
					hit_type = 2;
				//an arm
				else 
					hit_type = 3;
				
				if (entity_misc[entities[i].type].damage == 0 || (entities[i].type == 8 && entities[i].hp2 <=0))
				  hit_type = 1;
				if (entity_misc[entities[i].type].damage == 2)
				  hit_type = 3;
				else if (entity_misc[entities[i].type].damage == 3)
				{
					if (hit_type == 2)
						hit_type = 3;
				}
		
				//deal damage
				//remove limb if needed.
				
				if (hit_type == 1)
					entities[i].hp-=ammo_array[7].damage*dammulti;
				else if (hit_type == 2)
				{
					entities[i].hp3-=ammo_array[7].damage*dammulti;
					if (entities[i].hp3<=0)
					{
						if (entities[i].hp1 <=0 && entities[i].hp2 <=0)
						{
							entities[i].hp = 0;
						}
						else
						{
							entities[i].hp3 = 0;
							
							var tsx,tsy;
							var randy= Math.random();
							var tlength = Math.sqrt(tx*tx+ty*ty);
							tsx = 3*Math.log(ammo_array[7].damage*dammulti+3*randy+1)/Math.LN10*tx/tlength;
							tsy = 3*Math.log(ammo_array[7].damage*dammulti+3*randy+1)/Math.LN10*ty/tlength-5;
						
							SpawnGuts(i,entities[i].x,entities[i].y,tsx-1+2*Math.random(),tsy-7+2*Math.random(), "enemy1_leg", 0, gut_layer+Math.floor(2*Math.random()), entities[i].mask);
							SpawnGuts(i,entities[i].x,entities[i].y,tsx-1+2*Math.random(),tsy-7+2*Math.random(), "enemy1_leg", 0, gut_layer+Math.floor(2*Math.random()), entities[i].mask);
							//remove this arm;
							entities[i].dir2 = "legfall1";
							entities[i].frame2 = 0;
						}
					}
				}
				else if (hit_type == 3)
				{
					if (entities[i]["hp"+entities[i].farm] > 0 || entity_misc[entities[i].type].damage == 2)
					{
						if (entities[i].hp1 <= 0 && entities[i].type == 6)
						{
							//do nothing
						}
						else
						{
							wipe_temp_hp = entities[i]["hp"+entities[i].farm];
							entities[i]["hp"+entities[i].farm]-=ammo_array[7].damage*dammulti;
							if (entities[i].type != 4)
							{
								if (entities[i]["hp"+entities[i].farm] <=0)
								{
									var tsx,tsy;
									var randy= Math.random();
									var tlength = Math.sqrt(tx*tx+ty*ty);
									tsx = 3*Math.log(ammo_array[7].damage*dammulti+3*randy+1)/Math.LN10*tx/tlength;
									tsy = 3*Math.log(ammo_array[7].damage*dammulti+3*randy+1)/Math.LN10*ty/tlength-5;
								
									SpawnArm(i, px,py,tsx,tsy, gut_layer+1*((entities[i].farm+1)%2), entities[i].mask);
								
									if (entities[i].type == 8)
									{
										for (d=0;d<2;d++)
										{
											randy= Math.random();
											tsx = 3*Math.log(ammo_array[7].damage*dammulti+3*randy+1)/Math.LN10*tx/tlength;
											tsy = 3*Math.log(ammo_array[7].damage*dammulti+3*randy+1)/Math.LN10*ty/tlength-5;
											SpawnArm(i, px,py,tsx,tsy, gut_layer+1*((entities[i].farm+d)%2), entities[i].mask);
										}
									}

									if (entities[i].type == 6)
									{
										randy= Math.random();
										tsx = 3*Math.log(ammo_array[7].damage*dammulti+3*randy+1)/Math.LN10*tx/tlength;
										tsy = 3*Math.log(ammo_array[7].damage*dammulti+3*randy+1)/Math.LN10*ty/tlength-5;
										SpawnArm(i, px,py,tsx,tsy, gut_layer+1*((entities[i].farm+1)%2), entities[i].mask);
									}
									//remove this arm;
								}
							}
							else if (wipe_temp_hp > 0)
							{
								StunWipe(i);
							}
						}
					}
					else if (entities[i]["hp"+(entities[i].farm%2+1)] > 0)
					{
						entities[i]["hp"+(entities[i].farm%2+1)]-=ammo_array[7].damage*dammulti;
			
						if (entities[i]["hp"+(entities[i].farm%2+1)] <=0)
						{
							var tsx,tsy;
							var randy= Math.random();
							var tlength = Math.sqrt(tx*tx+ty*ty);
							tsx = 3*Math.log(ammo_array[7].damage*dammulti+3*randy+1)/Math.LN10*tx/tlength;
							tsy = 3*Math.log(ammo_array[7].damage*dammulti+3*randy+1)/Math.LN10*ty/tlength-5;
							
							
							SpawnArm(i, px,py,tsx,tsy,gut_layer+1*((entities[i].farm)%2), entities[i].mask);
							//remove this arm;
						}
					}
					else
						entities[i].hp-=ammo_array[7].damage*dammulti/3;
				}
				
				if (entities[i].hp <= 0)
				{

					var hsx,hsy;
					var hrandy= Math.random();
					var hlength = Math.sqrt(tx*tx+ty*ty);
					hsx = 3*Math.log(ammo_array[7].damage*dammulti+3*hrandy+1)/Math.LN10*tx/hlength;
					hsy = 3*Math.log(ammo_array[7].damage*dammulti+3*hrandy+1)/Math.LN10*ty/hlength-5;
				
				
					(Math.random()*5+ammo_array[7].damage*dammulti)/10
				
					entities[i].hp = 0;
					if (ammo_array[7].damage*dammulti >= 5 && ammo_array[7].damage*dammulti <10)
						SpawnHead(i,px,py, hsx,hsy, Math.floor((Math.random()*10+ammo_array[7].damage*dammulti)/10));//Math.sqrt(Math.random()*2)));
					else if (ammo_array[7].damage*dammulti >=10)
						SpawnHead(i,px,py, hsx,hsy, 1);
					else 
						SpawnHead(i,px,py, hsx,hsy, 0);

				}
			}
			else
			{
				var army = 1;
				//legless hit.
				if (entities[i].hp2 > 0)
					army = 2;
				else
					army = 1;
	
				entities[i]["hp"+army]-=ammo_array[7].damage*dammulti/3;
				
				if (entities[i]["hp"+army] <= 0)
				{
					entities[i]["hp"+army] = 0;
	
					var hsx,hsy;
					var hrandy= Math.random();
					var hlength = Math.sqrt(tx*tx+ty*ty);
					hsx = 3*Math.log(ammo_array[7].damage*dammulti+3*hrandy+1)/Math.LN10*tx/hlength;
					hsy = 3*Math.log(ammo_array[7].damage*dammulti+3*hrandy+1)/Math.LN10*ty/hlength-5;
				
					SpawnArm(i, px,py,hsx,hsy,gut_layer+1*((entities[i].farm)%2), entities[i].mask);
					
					if (entities[i]["hp"+((army)%2+1)] <= 0)
					{
						entities[i].hp = 0;
						if (ammo_array[7].damage*dammulti >= 5 && ammo_array[7].damage*dammulti <10)
							SpawnHead(i,px,py, hsx,hsy, Math.floor((Math.random()*10+ammo_array[7].damage*dammulti)/10));//Math.sqrt(Math.random()*2)));
						else if (ammo_array[7].damage*dammulti >=10)
							SpawnHead(i,px,py, hsx,hsy, 1);
						else 
							SpawnHead(i,px,py, hsx,hsy, 0);
					}
				}
			}
			if (entity_misc[entities[i].type].blood == 1)
			{
				if (entities[i].type == 6 && entities[i].hp1 <=0)
					SpawnSparkSplat(px,py);
				else
					SpawnBloodSplat(px,py,tx,ty, 5);
			}
			else
			{
				SpawnSparkSplat(px,py);
			}
			//spawn blood here;
		}
		entities[i].min_const = 1000000;
	}
  
	for (i=0;i<max_heads;i++)
	{
		if (heads[i].min_const<aj)
		{
			heads[i].stop = false;
			heads[i].vx += fx;
			if (heads[i].vx > max_headvx)
				heads[i].vx = max_headvx;
			if (heads[i].vx < -max_headvx)
				heads[i].vx = -max_headvx;
	
			heads[i].vy += fy-6;
			if (heads[i].vy > max_headvy)
				heads[i].vy = max_headvy;
			if (heads[i].vy < -max_headvy)
				heads[i].vy = -max_headvy;
		}
		heads[i].min_const = 1000000;
  }

	for (j=0;j<max_doors;j++)
	{
		if (doors[j].min_const<aj)
		{
			if (doors[j].type == 2 && doors[j].active == 0)
			{
				HitSwitch(px,py);
				SpawnSparkSplat(px,py);
			}
		}
	}
}

var max_railguns = 200;
var railgun_timer = 50;
var railguns = new Array(max_railguns);

for (i=0;i<max_railguns;i++)
{
	railguns[i] = new Object();
	railguns[i].sx = 0;
	railguns[i].sy = 0;
	railguns[i].x = 0;
	railguns[i].y = 0;
	railguns[i].timer = 0;
	railguns[i].cr = 0;
	railguns[i].cg = 0;
	railguns[i].cb = 0;
}

function SpawnRailgun(sx,sy,x,y,oby, hurtp,colors)
{
  for (sri=0;sri<max_railguns;sri++)
  {
		if (railguns[sri].timer <= 0)
		{
			railguns[sri].sx = sx;
			railguns[sri].sy = sy;
			railguns[sri].x = x;
			railguns[sri].y = y;
			railguns[sri].railgun_timer = railgun_timer;
			railguns[sri].scolors=false;
			if (colors == true)
			{
				railguns[sri].scolors = true;
				railguns[sri].railgun_timer = railgun_timer/4;
			}
			
			if (oby == 1)
			  railguns[sri].oby = 1;
			else
			  railguns[sri].oby = 0;

			railguns[sri].timer = railguns[sri].railgun_timer;

			railguns[sri].cr = 105+40*Math.random();
			railguns[sri].cg = 146+40*Math.random();
			railguns[sri].cb = 165+40*Math.random();
			
			if (hurtp != undefined)
			{
				//railgun will hurt the player if it hits him/her
				if (Math.abs(entities[0].x - x) < Math.abs(sx-x)+16 && Math.abs(entities[0].x - sx) < Math.abs(sx-x) &&
						Math.abs(entities[0].y - y) < Math.abs(sy-y)+16 && Math.abs(entities[0].y - sy) < Math.abs(sy-y) +16)
				{
						//player is in the bounding rectangle.
						if (DistToLine(entities[0].x - x, entities[0].y-y, sx-x, sy-y) < 20)
						{
							//hit...
							RailyHurtPlayer(hurtp);
						}
						
				}
			}
			break;
		}
  }
}

function DrawRailguns()
{
	var tempdrg = 0;
	var camt = 0;
	for (i=0;i<max_railguns;i++)
	{

		if (railguns[i].timer > 0)
		{
			tempdrg = 0;
			if (railguns[i].scolors == true)
				tempdrg = railguns[i].timer*255/railguns[i].railgun_timer;
			if (g_current_level != 18)
			{
				if (railguns[i].oby == 1)
				{
					GradientLine(MapToScreenX(0,railguns[i].sx),MapToScreenY(0,railguns[i].sy), 
											 MapToScreenX(0,railguns[i].x),MapToScreenY(0,railguns[i].y),
											 CreateColor(railguns[i].cr,railguns[i].cg,railguns[i].cb,tempdrg), 
											 CreateColor(railguns[i].cr,railguns[i].cg,railguns[i].cb,railguns[i].timer*255/railguns[i].railgun_timer));
				}
				else
				{
					GradientLine(MapToScreenX(0,railguns[i].sx),MapToScreenY(0,railguns[i].sy), 
											 MapToScreenX(0,railguns[i].x),MapToScreenY(0,railguns[i].y),
											 CreateColor(railguns[i].cr,railguns[i].cg,railguns[i].cb,tempdrg), 
											 CreateColor(railguns[i].cr,railguns[i].cg,railguns[i].cb,railguns[i].timer*255/railguns[i].railgun_timer));
				}
			}
			else
			{
				camt = (railguns[i].cr+railguns[i].cg+railguns[i].cb)/3;
				if (railguns[i].oby == 1)
				{
					GradientLine(MapToScreenX(0,railguns[i].sx),MapToScreenY(0,railguns[i].sy), 
											 MapToScreenX(0,railguns[i].x),MapToScreenY(0,railguns[i].y),
											 CreateColor(camt,camt,camt,tempdrg), 
											 CreateColor(camt,camt,camt,railguns[i].timer*255/railguns[i].railgun_timer));
				}
				else
				{
					GradientLine(MapToScreenX(0,railguns[i].sx),MapToScreenY(0,railguns[i].sy), 
											 MapToScreenX(0,railguns[i].x),MapToScreenY(0,railguns[i].y),
											 CreateColor(camt,camt,camt,tempdrg), 
											 CreateColor(camt,camt,camt,railguns[i].timer*255/railguns[i].railgun_timer));
				}
			}
		}
	}
}

function UpdateRailguns()
{
	for (i=0;i<max_railguns;i++)
	{
		if (railguns[i].timer > 0)
		{
			railguns[i].timer--;
		}
	}
	if (oblit_shield > 0)
	  oblit_shield--;
}

var oblit_shield = 0;
var oblit_shield_length = 300;//10000000000;//250;

var shield_images = new Array(4);
shield_images[0] = LoadImage("shield1.png");
shield_images[1] = LoadImage("shield2.png");
shield_images[2] = LoadImage("shield3.png");
shield_images[3] = LoadImage("shield4.png");

function DrawOblitShield()
{
	shield_images[oblit_shield%4].blit(MapToScreenX(0, entities[0].x)-18, MapToScreenY(0, entities[0].y)-18);
}