//=============================================================================
// Grenade.
//=============================================================================
class Grenade expands Projectile;

#exec MESH IMPORT MESH=Grenade ANIVFILE=MODELS\rocket_a.3D DATAFILE=MODELS\rocket_d.3D X=0 Y=0 Z=0
#exec MESH ORIGIN MESH=Grenade X=0 Y=0 Z=0 YAW=-64

#exec MESH SEQUENCE MESH=Grenade SEQ=All       STARTFRAME=0   NUMFRAMES=6
#exec MESH SEQUENCE MESH=Grenade SEQ=Still     STARTFRAME=0   NUMFRAMES=1
#exec MESH SEQUENCE MESH=Grenade SEQ=WingIn    STARTFRAME=1   NUMFRAMES=1
#exec MESH SEQUENCE MESH=Grenade SEQ=Armed     STARTFRAME=1   NUMFRAMES=3
#exec MESH SEQUENCE MESH=Grenade SEQ=SpinCCW   STARTFRAME=4   NUMFRAMES=1
#exec MESH SEQUENCE MESH=Grenade SEQ=SpinCW    STARTFRAME=5   NUMFRAMES=1

#exec TEXTURE IMPORT NAME=JRocket1 FILE=MODELS\Rocket.PCX
#exec MESHMAP SCALE MESHMAP=Grenade X=0.025 Y=0.025 Z=0.05 YAW=128
#exec MESHMAP SETTEXTURE MESHMAP=Grenade NUM=1 TEXTURE=JRocket1

#exec AUDIO IMPORT FILE="Sounds\EightBal\grenflor.UFX" NAME="GrenadeFloor" GROUP="Eightball"

var bool bCanHitOwner;
var float Count;
var ScriptedPawn WarnTarget;	// warn this pawn away

function PreBeginPlay()
{
	local vector X,Y,Z;
	local rotation RandRot;

	CallParent.PreBeginPlay();
	PlayAnim('WingIn');
	GetAxes(Instigator.ViewRotation,X,Y,Z);	
	Velocity = X * (Instigator.Velocity Dot X)*0.4 + Vector(Rotation) * Speed +
		FRand() * 100 * Vector(Rotation);
	Velocity.z += 210;
	SetTimer(2.75+FRand()*0.5,false);                  //Grenade begins unarmed
	RandRot.Pitch = FRand() * 1400 - 700;
	RandRot.Yaw = FRand() * 1400 - 700;
	RandRot.Roll = FRand() * 1400 - 700;
	MaxSpeed = 1000;
	Velocity = Velocity >> RandRot;
	RandSpin(50000);	
	bCanHitOwner = False;
}

	function Timer()
	{
		Explosion(Location+Vect(0,0,1)*16);
	}

	simulated function Tick(float DeltaTime)
	{
		Count += DeltaTime;
		if (Count>(Frand()*0.15+0.1) && Level.NetMode!=NM_Server) 
		{
			Spawn(class'BlackSmoke');		
			Count=0;
		}
		if ( (Physics == PHYS_None) && (WarnTarget != None) 
			&& (WarnTarget.Physics == PHYS_Walking) && (WarnTarget.Acceleration != vect(0,0,0)) )
			WarnTarget.Velocity = WarnTarget.Velocity + DeltaTime * WarnTarget.GroundSpeed * Normal(WarnTarget.Location - Location);
	}

	simulated function Landed( vector HitNormal )
	{
		HitWall( HitNormal, None );
	}

	function ProcessTouch( actor Other, vector HitLocation )
	{
		if ( (Other!=instigator) || bCanHitOwner )
			Explosion(HitLocation);
	}

	simulated function HitWall( vector HitNormal, actor Wall )
	{
		bCanHitOwner = True;
		Velocity = 0.8*(( Velocity dot HitNormal ) * HitNormal * (-2.0) + Velocity);   // Reflect off Wall w/damping
		RandSpin(100000);
		if ( Level.NetMode != NM_Server )
			PlaySound(ImpactSound, SLOT_Misc, FMax(0.5, Size(Velocity)/800) );	
		If (Size(Velocity) < 20) 
		{
			bBounce = False;
			SetPhysics(PHYS_None);
		}
	}

	///////////////////////////////////////////////////////
	function Explosion(vector HitLocation)
	{
		PlaySound(SpawnSound);
		HurtRadius(damage, 200, 'exploded', MomentumTransfer, HitLocation);
  		spawn(class'SpriteBallExplosion',,,HitLocation);
 		Destroy();
	}

