var gunpos = new Array(16);
for (j=0;j<16;j++)
{
  gunpos[j] = new Array(16);
	for (i=0;i<16;i++)
	{
		gunpos[j][i] = new Object();
		gunpos[j][i].x = 0;
		gunpos[j][i].y = 0;
	}
}

gunpos[0][0].x = 41-26;
gunpos[0][0].y = 20-22;
gunpos[0][1].x = 38-26;
gunpos[0][1].y = 16-22;
gunpos[0][2].x = 36-26;
gunpos[0][2].y = 11-22;
gunpos[0][3].x = 31-26;
gunpos[0][3].y =  8-22;
gunpos[0][4].x = 25-26;
gunpos[0][4].y =  9-22;
gunpos[0][5].x = 18-26;
gunpos[0][5].y =  7-22;
gunpos[0][6].x = 12-26;
gunpos[0][6].y = 10-22;
gunpos[0][7].x =  6-26;
gunpos[0][7].y = 14-22;
gunpos[0][8].x =  5-26;
gunpos[0][8].y = 18-22;
gunpos[0][9].x =  7-26;
gunpos[0][9].y = 27-22;
gunpos[0][10].x =  7-26;
gunpos[0][10].y = 33-22;
gunpos[0][11].x = 13-26;
gunpos[0][11].y = 34-22;
gunpos[0][12].x = 26-26;
gunpos[0][12].y = 35-22;
gunpos[0][13].x = 34-26;
gunpos[0][13].y = 35-22;
gunpos[0][14].x = 38-26;
gunpos[0][14].y = 32-22;
gunpos[0][15].x = 40-26;
gunpos[0][15].y = 29-22;

for (i=0;i<16;i++)
  gunpos[2][i] = gunpos[0][i];

gunpos[3][0].x = 36-26;
gunpos[3][0].y = 24-22;
gunpos[3][1].x = 35-26;
gunpos[3][1].y = 18-22;
gunpos[3][2].x = 32-26;
gunpos[3][2].y = 16-22;
gunpos[3][3].x = 26-26;
gunpos[3][3].y = 10-22;
gunpos[3][4].x = 22-26;
gunpos[3][4].y = 11-22;
gunpos[3][5].x = 19-26;
gunpos[3][5].y = 14-22;
gunpos[3][6].x = 16-26;
gunpos[3][6].y = 15-22;
gunpos[3][7].x = 13-26;
gunpos[3][7].y = 17-22;
gunpos[3][8].x = 15-26;
gunpos[3][8].y = 22-22;
gunpos[3][9].x = 15-26;
gunpos[3][9].y = 25-22;
gunpos[3][10].x = 14-26;
gunpos[3][10].y = 27-22;
gunpos[3][11].x = 16-26;
gunpos[3][11].y = 29-22;
gunpos[3][12].x = 22-26;
gunpos[3][12].y = 31-22;
gunpos[3][13].x = 27-26;
gunpos[3][13].y = 31-22;
gunpos[3][14].x = 33-26;
gunpos[3][14].y = 30-22;
gunpos[3][15].x = 34-26;
gunpos[3][15].y = 29-22;


gunpos[4][0].x = 30-26;
gunpos[4][0].y = 24-22;
gunpos[4][1].x = 29-26;
gunpos[4][1].y = 21-22;
gunpos[4][2].x = 29-26;
gunpos[4][2].y = 19-22;
gunpos[4][3].x = 24-26;
gunpos[4][3].y = 15-22;
gunpos[4][4].x = 23-26;
gunpos[4][4].y = 12-22;
gunpos[4][5].x = 20-26;
gunpos[4][5].y = 16-22;
gunpos[4][6].x = 19-26;
gunpos[4][6].y = 18-22;
gunpos[4][7].x = 17-26;
gunpos[4][7].y = 19-22;
gunpos[4][8].x = 17-26;
gunpos[4][8].y = 22-22;
gunpos[4][9].x = 17-26;
gunpos[4][9].y = 24-22;
gunpos[4][10].x = 18-26;
gunpos[4][10].y = 24-22;
gunpos[4][11].x = 19-26;
gunpos[4][11].y = 25-22;
gunpos[4][12].x = 23-26;
gunpos[4][12].y = 28-22;
gunpos[4][13].x = 26-26;
gunpos[4][13].y = 29-22;
gunpos[4][14].x = 29-26;
gunpos[4][14].y = 27-22;
gunpos[4][15].x = 30-26;
gunpos[4][15].y = 27-22; 



gunpos[5][0].x = 42-26;
gunpos[5][0].y = 24-22;
gunpos[5][1].x = 41-26;
gunpos[5][1].y = 15-22;
gunpos[5][2].x = 37-26;
gunpos[5][2].y = 11-22;
gunpos[5][3].x = 29-26;
gunpos[5][3].y =  5-22;
gunpos[5][4].x = 23-26;
gunpos[5][4].y =  3-22;
gunpos[5][5].x = 15-26;
gunpos[5][5].y =  7-22;
gunpos[5][6].x = 11-26;
gunpos[5][6].y = 10-22;
gunpos[5][7].x =  7-26;
gunpos[5][7].y = 14-22;
gunpos[5][8].x =  7-26;
gunpos[5][8].y = 22-22;
gunpos[5][9].x =  7-26;
gunpos[5][9].y = 29-22;
gunpos[5][10].x =  9-26;
gunpos[5][10].y = 32-22;
gunpos[5][11].x = 12-26;
gunpos[5][11].y = 37-22;
gunpos[5][12].x = 23-26;
gunpos[5][12].y = 37-22;
gunpos[5][13].x = 30-26;
gunpos[5][13].y = 37-22;
gunpos[5][14].x = 37-26;
gunpos[5][14].y = 35-22;
gunpos[5][15].x = 39-26;
gunpos[5][15].y = 32-22; 


gunpos[6][0].x = 42-26;
gunpos[6][0].y = 21-22;
gunpos[6][1].x = 38-26;
gunpos[6][1].y = 10-22;
gunpos[6][2].x = 35-26;
gunpos[6][2].y =  7-22;
gunpos[6][3].x = 26-26;
gunpos[6][3].y =  6-22;
gunpos[6][4].x = 20-26;
gunpos[6][4].y =  5-22;
gunpos[6][5].x = 16-26;
gunpos[6][5].y =  8-22;
gunpos[6][6].x = 12-26;
gunpos[6][6].y = 10-22;
gunpos[6][7].x =  8-26;
gunpos[6][7].y = 14-22;
gunpos[6][8].x =  7-26;
gunpos[6][8].y = 19-22;
gunpos[6][9].x =  9-26;
gunpos[6][9].y = 26-22;
gunpos[6][10].x = 10-26;
gunpos[6][10].y = 31-22;
gunpos[6][11].x = 12-26;
gunpos[6][11].y = 33-22;
gunpos[6][12].x = 18-26;
gunpos[6][12].y = 34-22;
gunpos[6][13].x = 27-26;
gunpos[6][13].y = 34-22;
gunpos[6][14].x = 35-26;
gunpos[6][14].y = 33-22;
gunpos[6][15].x = 40-26;
gunpos[6][15].y = 30-22;


gunpos[7][0].x = 36-26;
gunpos[7][0].y = 24-22;
gunpos[7][1].x = 35-26;
gunpos[7][1].y = 18-22;
gunpos[7][2].x = 33-26;
gunpos[7][2].y = 16-22;
gunpos[7][3].x = 29-26;
gunpos[7][3].y = 10-22;
gunpos[7][4].x = 24-26;
gunpos[7][4].y = 11-22;
gunpos[7][5].x = 20-26;
gunpos[7][5].y = 14-22;
gunpos[7][6].x = 16-26;
gunpos[7][6].y = 15-22;
gunpos[7][7].x = 13-26;
gunpos[7][7].y = 17-22;
gunpos[7][8].x = 15-26;
gunpos[7][8].y = 22-22;
gunpos[7][9].x = 15-26;
gunpos[7][9].y = 25-22;
gunpos[7][10].x = 14-26;
gunpos[7][10].y = 27-22;
gunpos[7][11].x = 16-26;
gunpos[7][11].y = 29-22;
gunpos[7][12].x = 22-26;
gunpos[7][12].y = 31-22;
gunpos[7][13].x = 27-26;
gunpos[7][13].y = 31-22;
gunpos[7][14].x = 33-26;
gunpos[7][14].y = 30-22;
gunpos[7][15].x = 34-26;
gunpos[7][15].y = 29-22;

gunpos[8][0].x = 39-26;
gunpos[8][0].y = 24-22;
gunpos[8][1].x = 38-26;
gunpos[8][1].y = 17-22;
gunpos[8][2].x = 35-26;
gunpos[8][2].y = 14-22;
gunpos[8][3].x = 28-26;
gunpos[8][3].y = 19-22;
gunpos[8][4].x = 22-26;
gunpos[8][4].y =  8-22;
gunpos[8][5].x = 17-26;
gunpos[8][5].y = 11-22;
gunpos[8][6].x = 13-26;
gunpos[8][6].y = 13-22;
gunpos[8][7].x = 11-26;
gunpos[8][7].y = 16-22;
gunpos[8][8].x = 11-26;
gunpos[8][8].y = 22-22;
gunpos[8][9].x = 11-26;
gunpos[8][9].y = 27-22;
gunpos[8][10].x = 12-26;
gunpos[8][10].y = 30-22;
gunpos[8][11].x = 16-26;
gunpos[8][11].y = 32-22;
gunpos[8][12].x = 23-26;
gunpos[8][12].y = 33-22;
gunpos[8][13].x = 28-26;
gunpos[8][13].y = 34-22;
gunpos[8][14].x = 34-26;
gunpos[8][14].y = 33-22;
gunpos[8][15].x = 35-26;
gunpos[8][15].y = 30-22;

gunpos[12][0].x = 36-26;
gunpos[12][0].y = 24-22;
gunpos[12][1].x = 35-26;
gunpos[12][1].y = 18-22;
gunpos[12][2].x = 33-26;
gunpos[12][2].y = 16-22;
gunpos[12][3].x = 29-26;
gunpos[12][3].y = 10-22;
gunpos[12][4].x = 24-26;
gunpos[12][4].y = 11-22;
gunpos[12][5].x = 20-26;
gunpos[12][5].y = 14-22;
gunpos[12][6].x = 16-26;
gunpos[12][6].y = 15-22;
gunpos[12][7].x = 13-26;
gunpos[12][7].y = 17-22;
gunpos[12][8].x = 15-26;
gunpos[12][8].y = 22-22;
gunpos[12][9].x = 15-26;
gunpos[12][9].y = 25-22;
gunpos[12][10].x = 14-26;
gunpos[12][10].y = 27-22;
gunpos[12][11].x = 16-26;
gunpos[12][11].y = 29-22;
gunpos[12][12].x = 22-26;
gunpos[12][12].y = 31-22;
gunpos[12][13].x = 27-26;
gunpos[12][13].y = 31-22;
gunpos[12][14].x = 33-26;
gunpos[12][14].y = 30-22;
gunpos[12][15].x = 34-26;
gunpos[12][15].y = 29-22;



var magnum_shot = 0;
var shell_shot = 0;
var rail_shot = 0;
var shrap_shot = 0;

function MakeCasing(type)
{
	var cx = MapToScreenX(0,entities[0].x);
	var cy = MapToScreenY(0,entities[0].y);
	
	if (type == undefined)
		type = "case1";
	
	if (entities[0].dir == 1)
		var angle = Math.atan2(GetMouseY()-cy,cx-GetMouseX())+3.14159;
	else
		var angle = Math.atan2(GetMouseY()-cy,-cx+GetMouseX())+3.14159;
		
	angle/=(2*3.14159);
							
	angle*=16;
	angle+=0.5;
	angle = Math.floor(angle);
	
	if (angle > 15) angle = 0;
					
	if (GetMouseX() < MapToScreenX(0,entities[0].x))
		SpawnCasing(entities[0].x+gunpos[cur_weap][angle].x*entities[0].dir, entities[0].y+gunpos[cur_weap][angle].y-2,2+2*Math.random()-1, -4-Math.random(), type);
	else
		SpawnCasing(entities[0].x+gunpos[cur_weap][angle].x*entities[0].dir, entities[0].y+gunpos[cur_weap][angle].y-2,-2+2*Math.random()-1, -4-Math.random(), type);
}

var max_case_time = 50;
var max_casings = 55;
var casings = new Array(max_casings);

for (i=0;i<max_casings;i++)
{
	casings[i] = new Object();
	casings[i].x = blood_dx;
	casings[i].y = blood_dy;
	
	casings[i].vx = 0;
	casings[i].vy = 0;

	casings[i].name = "c"+i;
	casings[i].dir = "case1";
	casings[i].id1 = 0;

	casings[i].timer = max_case_time;
	casings[i].layer = gut_layer+1;
}

function SetupCasings()
{
	for (i=0;i<max_casings;i++)
	{
		casings[i].id1 = GetPersonID("c"+i);
		casings[i].x = blood_dx;
		casings[i].y = blood_dy;
		
		casings[i].vx = 0;
		casings[i].vy = 0;
	
		casings[i].name = "c"+i;
		casings[i].dir = "case1";
		casings[i].id1 = 0;
	
		casings[i].timer = max_case_time;
	}
}

function UpdateCasings()
{
  var sx, sy;
  var gravity_const = 0.5;
  var max_fall = 7;
  var speed = 10;
  var friction = 1.25;
  var bool = false;
  var bounce = -0.3;
  
  var gx,gy;
  
  var casey;
  var sboo;
  var smag = 0;
  
	for (uci=0;uci<max_casings;uci++)
	{
		if (casings[uci].x != blood_dx && casings[uci].timer >0)
		{
			casey = casings[uci];

			SetPersonIDLayer(casey.id1, 0);
					
			if (casey.vy < max_fall && !IsPersonIDObstructed(casey.id1, casey.x, casey.y+1))
				casey.vy+=gravity_const;

			if (IsXYInWater(casey.x,casey.y))
			{
				if (casey.vy > 1)
					casey.vy-=1;
				if (casey.vy <-1)
					casey.vy+=1;
				if (casey.vx > 1)
					casey.vx-=1;
				if (casey.vx <-1)
					casey.vx+=1;
			}
					
			casey.x+=casey.vx;
			casey.y+=casey.vy;
				
			gx = casey.x;
			gy = casey.y;
					
			if (IsPersonIDObstructed(casey.id1, casey.x, casey.y))
			{
				for (ucj=0;ucj<speed;ucj++)
				{
					casey.x -=casey.vx/speed;
					casey.y -=casey.vy/speed;
					if (!IsPersonIDObstructed(casey.id1, casey.x, casey.y))
						break;
				}
		
		
				sx = casey.vx;
				sy = casey.vy;
		
				sboo = false;
				smag = 0;

				if (IsPersonIDObstructed(casey.id1, casey.x, casey.y+1))
				{
					casey.vy*=bounce;
					sboo = true;
					if (Math.abs(casey.vy) > smag);
						smag = Math.abs(casey.vy);
				}
				if (IsPersonIDObstructed(casey.id1, casey.x+1, casey.y))
				{
					casey.vx*=bounce;
					sboo = true;
					if (Math.abs(casey.vx) > smag);
						smag = Math.abs(casey.vx);
				}
		
		
				if (IsPersonIDObstructed(casey.id1, casey.x, casey.y-1))
				{
					casey.vy*=bounce;
					sboo = true;
					if (Math.abs(casey.vy) > smag);
						smag = Math.abs(casey.vy);
				}
				if (IsPersonIDObstructed(casey.id1, casey.x-1, casey.y))
				{
					casey.vx*=bounce;
					sboo = true;
					if (Math.abs(casey.vx) > smag);
						smag = Math.abs(casey.vx);
				}
							
				if (sx==casey.vx && sy == casey.vy)
					casey.vx*=bounce;
					
				if (sboo)
				{
					if (smag > 8)
						smag = 8;
					PlayCasing(smag);
				}	
							
				if (sx != casey.vx)
				{
				  //make sound
				}
				if (sy != casey.vy)
				{
				  //make sound
				}
			}
				
				
			if (casey.vx < 0 && IsCasingOnGround(uci))
			{
				casey.vx+=friction/4;
				if (casey.vx > 0)
					casey.vx = 0;
			}
			else if (casey.vx > 0 && IsCasingOnGround(uci))
			{
				casey.vx-=friction/4;
				if (casey.vx < 0)
					casey.vx = 0;
			}

			if (IsCasingOnGround(uci) && Math.abs(casey.vy) < 1.5)
			  casey.vy = 0;

			casey.timer--
			if (casey.timer <=0)
			{
				casey.x=blood_dx;
				casey.timer = 0;
				
			}	
		
			SetPersonIDX(casey.id1, casey.x);
			if (g_current_level != 9)
				SetPersonIDY(casey.id1, casey.y+1);
			else if (g_current_level == 9)
				SetPersonIDY(casey.id1, casey.y+1-6);
					
			if (IsCasingOnGround(uci))
				SetPersonIDFrame(casey.id1, 0);
			else
				SetPersonIDFrame(casey.id1, 0);// Math.floor(g_frames/4)%2);

			SetPersonIDLayer(casey.id1, casey.layer);
		}
	}
}


function IsCasingOnGround(i)
{
	if (!IsPersonIDObstructed(casings[i].id1, casings[i].x, casings[i].y) && IsPersonIDObstructed(casings[i].id1, casings[i].x, casings[i].y+1))
	  return true;
	return false;
}


function SpawnCasing(x,y,vx,vy, type)
{
	for (sci = 0; sci<max_casings;sci++)
	{
	  if (casings[sci].x == blood_dx)
	  {
			casings[sci].x = x+vx;
			casings[sci].y = y+vy;
	    SetPersonIDLayer(casings[sci].id1, 0);
			if (IsPersonIDObstructed(casings[sci].id1, x+vx, y+vy))
			{
				if (IsPersonIDObstructed(casings[sci].id1, x, y))
				{
					if (IsPersonIDObstructed(casings[sci].id1, x-vx, y-vy))
					{
//						casings[sci].x = blood_dx;
//						return;
					  casings[sci].x = entities[0].x;
					  casings[sci].y = entities[0].y;
					}
					else
					{
						casings[sci].x = x-vx;
						casings[sci].y = y-vy;
					}
				}
				else
				{
					casings[sci].x = x;
					casings[sci].y = y;
				}
			}

			casings[sci].vx = vx;
			casings[sci].vy = vy;

			casings[sci].dir = type;
			casings[sci].timer = max_case_time;
			
			SetPersonIDDirection(casings[sci].id1, type);
	    SetPersonIDLayer(casings[sci].id1, casings[sci].layer);
	    if (vx < 0)
	      SetPersonIDHorizontalFlip(casings[sci].id1, -1);
	    else
	      SetPersonIDHorizontalFlip(casings[sci].id1, 1);
			break;
	  }
	}
}
