function UpdateBarrel(i)
{
	var speed;
	var sx,sy;
	var t_string;
	var t_layer = 0;
	var boooo = false;
	
	var wspeed = 4;
	var rspeed = 7;
	
	var xbounce = 0.7;
	var ybounce = 0.5;
	
	var rollo = 0;
	var flaggy = false;

	var friction = 0.33;


	if (Math.abs(entities[i].x -entities[0].x)<16 && Math.abs(entities[i].y - entities[0].y) < 16)
	{
		rollo = wspeed;
		
		if (entities[0].vx == 0)
			rollo = 0;
		
		if (g_isrunning)
			rollo = rspeed;
	
		if (entities[i].x < entities[0].x)
			entities[i].vx = -rollo;
		else
			entities[i].vx = rollo;
			
		if (entities[i].dir2 == "idle" && entities[i].frame2 == 0)
		{
			entities[i].frame2++;
			PlaySound(103,false);
			
			if (entities[0].dir2 == "aim1")
			{
				entities[0].dir2 = "barrel_punch";
				entities[0].frame = 4;
			}
			//set liegh to "punch"
		}
		else 
		{
			t_layer = GetPersonIDLayer(entities[i].id1);
			SetPersonIDLayer(entities[i].id1, 0);

			if (!IsPersonIDObstructed(entities[i].id1, entities[i].x+entities[i].vx/2, entities[i].y) )
			{
				if (sounds[102].isPlaying() == false)
				{
					if (rollo == 4)
						PlaySound(102,false, 0.6);
					else if (rollo == 7)
						PlaySound(102,false);
				}
			}
			SetPersonIDLayer(entities[i].id1, t_layer);
		}
	}

	//update it's physics:
		//add gravity;
		if (entities[i].vy < grav_max && !IsPersonOnGround(i))
		{
			if (entities[i].type == 8 && entities[i].hp > 0)
			{
				if (entities[i].vy < grav_max)
					entities[i].vy+=grav_amt;
			}
			else
				entities[i].vy+=grav_amt;
		}
	
		if (IsEntityInWater(i))
		{
			InWater(i);
		}
	
		sx = entities[i].x;
		sy = entities[i].y;
		
		t_layer = GetPersonIDLayer(entities[i].id1);
		SetPersonIDLayer(entities[i].id1, 0);

			entities[i].x += entities[i].vx;
			entities[i].y += entities[i].vy;
					
			
		btest_id = entities[i].id1;
//		if (entities[i].dir2 == "walk")
	//		btest_id = g_widthy_id;
			
					
			if (IsPersonIDObstructed(btest_id, entities[i].x, entities[i].y) || (entities[i].type == 8 && IsEntityInWater(i))|| (entities[i].type == 10 && !IsEntityInWater(i)))
			{
				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 (!IsPersonIDObstructed(btest_id, entities[i].x, entities[i].y) && (entities[i].type != 8 || !IsEntityInWater(i))&& (entities[i].type != 10 || IsEntityInWater(i)))
						break;
				}
				if (k< 0)
				{
				entities[i].x = sx;
				entities[i].y = sy;
				}
	
				if (!IsPersonIDObstructed(btest_id, entities[i].x, entities[i].y) && (entities[i].type != 8 || !IsEntityInWater(i))&& (entities[i].type != 10 || IsEntityInWater(i)))
				{
					flaggy = false;
	
					if (IsPersonIDObstructed(btest_id, entities[i].x+1, entities[i].y))
					{
						entities[i].vx*=-xbounce;
						flaggy = true;
					}
					else if (IsPersonIDObstructed(btest_id, entities[i].x-1, entities[i].y))
					{
						entities[i].vx*=-xbounce;
						flaggy = true;
					}
	
					else if (IsPersonIDObstructed(btest_id, entities[i].x, entities[i].y-1))
					{
						entities[i].vy*=-ybounce;
						flaggy = true;
					}
					else if (IsPersonIDObstructed(btest_id, entities[i].x, entities[i].y+1))
					{
						entities[i].vy*=-ybounce;
						flaggy = true;
					}
					if (flaggy == false)
					{
						entities[i].vx*=-xbounce;
					}
					
					if (entities[i].vy < 0.5)
						entities[i].vy = 0;

					if (Math.sqrt(entities[i].vx*entities[i].vx+entities[i].vy*entities[i].vy) > 2)
					{
//						if (sounds[102].isPlaying() == false)
							PlaySound(105,false, Math.sqrt(entities[i].vx*entities[i].vx+entities[i].vy*entities[i].vy)/7);
					}
				}
			}
		
	
			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;
			}
					
					
		SetPersonIDLayer(entities[i].id1, t_layer);
	
	
	//check for nearby enemies:
	
	if (entities[i].dir2 == "walk")
	{
		
		for (ub=1;ub<max_entities; ub++)
		{
			if (entities[ub].active == true && entities[ub].type != 2&& entities[ub].hp > 0&& i!= ub&& 0!= ub&&entities[ub].stop == false && entities[ub].ambush == false)
			{
				if (Math.abs(entities[ub].x-entities[i].x) < 16 && Math.abs(entities[ub].y-entities[i].y) < 24)
				{
					//explode!
					entities[i].hp = 0;
				}
			}
		}
	}
					
	//Barrel type
	if (entities[i].hp <=0)
	{
		SpawnExplosion(entities[i].x,entities[i].y, 20, 0, 2);
		entities[i].stop = true;
		entities[i].x = blood_dx;
		SetPersonIDX(entities[i].id1, entities[i].x);
		
		if (sounds[104].isPlaying() == true)
		{
			sounds[104].stop();
			sounds[104].setPosition(0);
		}
	}
}

function UpdateBarrelFrame(num)
{
	//whatever
	if (entities[num].vx > 0)
		entities[num].dir = 1;
	if (entities[num].vx < 0)
		entities[num].dir = -1;
		
	var face = "east"
	var facedir = (-1*entities[num].dir+1)/2;
	
	SetPersonIDHorizontalFlip(entities[num].id1, facedir);

	SetPersonIDLayer(entities[num].id1, gut_layer);
		
	
	if (entities[num].dir2 == "idle")
	{
		if (g_frames % (frameupdate*2) ==0 && entities[num].frame2 != 0)
			entities[num].frame2++;
			
		if (entities[num].frame2 > 3)
		{
			entities[num].frame2 = 0;
			entities[num].dir2 = "walk";
		}
		SetPersonIDDirection(entities[num].id1, entities[num].dir2+face);
		SetPersonIDFrame(entities[num].id1, entities[num].frame2);
		
	}
	else if (entities[num].dir2 == "walk")
	{
		var t_layer = GetPersonIDLayer(entities[num].id1);
		SetPersonIDLayer(entities[num].id1, 0);

		if (Math.abs(entities[num].vx) > 1 && IsEntityOnGround(num))
		{
			if (sounds[104].isPlaying() == false)
			{
				sounds[104].setPosition(0);
				sounds[104].play(true);
			}

			if (barrel_up == -1)
			{
				if (sounds[102].isPlaying() || sounds[103].isPlaying())
					sounds[104].setVolume(225*sound_level*((Math.abs(entities[num].vx)-1)/7));
				else
					sounds[104].setVolume(255*sound_level*((Math.abs(entities[num].vx)-1)/7));
			}
			else
			{
				if (sounds[102].isPlaying() || sounds[103].isPlaying())
				{
					if (sounds[104].getVolume() < 225*sound_level*((Math.abs(entities[num].vx)-1)/7))
						sounds[104].setVolume(225*sound_level*((Math.abs(entities[num].vx)-1)/7));
				}
				else
				{
					if (sounds[104].getVolume() < 255*sound_level*((Math.abs(entities[num].vx)-1)/7))
						sounds[104].setVolume(255*sound_level*((Math.abs(entities[num].vx)-1)/7));
				}
			}
			barrel_up = 1;
//			FlipScreen();
		}
		SetPersonIDLayer(entities[num].id1, t_layer);
	
		if (g_frames % frameupdate ==0)
		{
			entities[num].frame2+=entities[num].vx;
		}
		if (entities[num].frame2 < 0)
			entities[num].frame2+=7*4;

		SetPersonIDDirection(entities[num].id1, entities[num].dir2+face);
		SetPersonIDFrame(entities[num].id1, Math.floor(entities[num].frame2/7)%4);
	}
	
	SetPersonIDX(entities[num].id1, entities[num].x);
	SetPersonIDY(entities[num].id1, entities[num].y);
}


function PostBarrelUpdate()
{
	if (barrel_up == -1)
	{
		if (sounds[104].isPlaying() == true)
		{
			sounds[104].stop();
			sounds[104].setPosition(0);
		}
	}
}