//=============================================================================
// Chunk.
//=============================================================================
class Chunk expands Projectile;

#exec AUDIO IMPORT FILE="Sounds\flak\flachit1.WAV" NAME="Hit1" GROUP="flak"
#exec AUDIO IMPORT FILE="Sounds\flak\flachit2.WAV" NAME="Hit2" GROUP="flak"
#exec AUDIO IMPORT FILE="Sounds\flak\flachit3.WAV" NAME="Hit3" GROUP="flak"
#exec AUDIO IMPORT FILE="Sounds\flak\flachit4.WAV" NAME="Hit4" GROUP="flak"
#exec AUDIO IMPORT FILE="Sounds\flak\flachit5.WAV" NAME="Hit5" GROUP="flak"
#exec AUDIO IMPORT FILE="Sounds\flak\flachit6.WAV" NAME="Hit6" GROUP="flak"
#exec AUDIO IMPORT FILE="Sounds\flak\chunkhit.WAV" NAME="ChunkHit" GROUP="flak"

var bool bDelayTime;

	function PreBeginPlay()
	{
		local rotation RandRot;

		RandRot = Rotation;
		RandRot.Pitch += FRand() * 2000 - 1000;
		RandRot.Yaw += FRand() * 2000 - 1000;
		RandRot.Roll += FRand() * 2000 - 1000;
		Velocity = Vector(RandRot) * (Speed + (FRand() * 200 - 100));
		if (Region.zone.bWaterZone)
			SetPhysics(PHYS_Falling);
		CallParent.PreBeginPlay();
	}

	function ProcessTouch (Actor Other, vector HitLocation)
	{
		if ( (Chunk(Other) == None) && (bDelayTime || (Other != Instigator)) )
		{
			speed = Size(Velocity);
			If ( speed > 200 )
			{
				Other.Damage(damage, instigator,HitLocation,
					(MomentumTransfer * Velocity/speed), 'shredded' );
				PlaySound(Sound 'ChunkHit');
			}
			Destroy();
		}
	}
	function Timer() 
	{
		Destroy();
	}

	simulated function Landed( Vector HitNormal )
	{
		SetPhysics(PHYS_None);
	}

	simulated function HitWall( vector HitNormal, actor Wall )
	{
		local float Rand;

		if ( (Mover(Wall) != None) && Mover(Wall).bDamageTriggered )
		{
			Wall.Damage( Damage, instigator, Location, MomentumTransfer * Normal(Velocity), '');
			Destroy();
			return;
		}
		if (!bDelayTime) 
		{
			bDelayTime=True;
			SetPhysics(PHYS_Falling);
			if ( (Level.Netmode != NM_Server) && (FRand()<0.2) ) 
				Spawn(Class'SmallSpark',,,Location+HitNormal*5,rotation(HitNormal));
		}
		Velocity = 0.8*(( Velocity dot HitNormal ) * HitNormal * (-1.8 + FRand()*0.8) + Velocity);   // Reflect off Wall w/damping
		SetRotation(Rotation(Velocity));
		speed = Size(Velocity);
		if ( speed > 100 ) 
		{
			if ( Role == ROLE_Authority )
				MakeNoise(0.3);
			if ( Level.Netmode != NM_Server )
			{
				Rand = FRand();
				if (Rand < 0.2)	PlaySound(sound 'Hit1', SLOT_Misc,0.5);	
				else if (Rand < 0.35) PlaySound(sound 'Hit2', SLOT_Misc,0.5);
				else if (Rand < 0.5) PlaySound(sound 'Hit3', SLOT_Misc,0.5);
				else if (Rand < 0.65) PlaySound(sound 'Hit4', SLOT_Misc,0.5);
				else if (Rand < 0.8) PlaySound(sound 'Hit5', SLOT_Misc,0.5);
				else  PlaySound(sound 'Hit6', SLOT_Misc,0.5);
			}				
		}
		else 
		{
			bBounce = False;
			SetTimer(1.0,False);
		}
	}

	function zonechange(Zoneinfo NewZone)
	{
		if (NewZone.bWaterZone)
			SetPhysics(PHYS_Falling);			
	}

