//=============================================================================
// CreatureChunks.
//=============================================================================
class CreatureChunks expands Carcass;

#exec MESH IMPORT MESH=CowBody1 ANIVFILE=MODELS\g_cow2_a.3D DATAFILE=MODELS\g_cow2_d.3D X=0 Y=0 Z=0
#exec MESH ORIGIN MESH=CowBody1 X=0 Y=0 Z=0 YAW=64
#exec MESH SEQUENCE MESH=CowBody1 SEQ=All    STARTFRAME=0   NUMFRAMES=1
#exec MESH SEQUENCE MESH=CowBody1 SEQ=Still  STARTFRAME=0   NUMFRAMES=1
#exec TEXTURE IMPORT NAME=JGCow1  FILE=MODELS\Nc_1.PCX GROUP=Skins 
#exec MESHMAP SCALE MESHMAP=CowBody1 X=0.06 Y=0.06 Z=0.12
#exec MESHMAP SETTEXTURE MESHMAP=CowBody1 NUM=1 TEXTURE=JGCow1

#exec AUDIO IMPORT FILE="Sounds\Gibs\gibP1.WAV" NAME="gibP1" GROUP="Gibs"
#exec AUDIO IMPORT FILE="Sounds\Gibs\gibP3.WAV" NAME="gibP3" GROUP="Gibs"
#exec AUDIO IMPORT FILE="Sounds\Gibs\gibP4.WAV" NAME="gibP4" GROUP="Gibs"
#exec AUDIO IMPORT FILE="Sounds\Gibs\gibP5.WAV" NAME="gibP5" GROUP="Gibs"
#exec AUDIO IMPORT FILE="Sounds\Gibs\gibP6.WAV" NAME="gibP6" GROUP="Gibs"

var	  Bloodtrail	trail;
var	  texture		BloodColor;
var	  float			TrailSize;

function PostBeginPlay()
{
	if ( Region.Zone.bDestructive )
		Destroy();
	else
		CallParent.PostBeginPlay();
}

simulated function ZoneChange( ZoneInfo NewZone )
{
	if ( NewZone.bWaterZone )
	{
		if ( bSplash && !Region.Zone.bWaterZone && (Abs(Velocity.Z) < 80) )
		{
			RotationRate *= 0.6;
			if ( trail != None )
				trail.Destroy();
			if ( Mass <= Buoyancy )
				SetCollisionSize(0,0);
		}
		// else play a splash
		bSplash = true;
	}

	if ( NewZone.bDestructive || (NewZone.bPainZone  && (NewZone.DamagePerSec > 0)) )
		Destroy();
}
	
function Destroyed()
{
	if ( trail != None )
		trail.Destroy();
	CallParent.Destroyed();
}

function Initfor(actor Other)
{
	local vector RandDir;

	bDecorative = false;
	DrawScale = Other.DrawScale;
	RotationRate.Yaw = Rand(200000) - 100000;
	RotationRate.Pitch = Rand(200000 - Abs(RotationRate.Yaw)) - 0.5 * (200000 - Abs(RotationRate.Yaw));
	RandDir = 700 * FRand() * VRand();
	RandDir.Z = 500 * FRand() - 250;
	Velocity = (0.2 + FRand()) * (other.Velocity + RandDir);
	If (Region.Zone.bWaterZone)
		Velocity *= 0.5;
	if ( TrailSize > 0 )
	{
		if ( CreatureCarcass(Other) != None )
			BloodColor = CreatureCarcass(Other).BloodColor;
		else if ( (CreatureChunks(Other) != None) && (CreatureChunks(Other).trail.skin != None) )
			BloodColor = CreatureChunks(Other).trail.skin;
	}
			
	if ( FRand() < 0.3 )
		Buoyancy = 1.06 * Mass; // float corpse
	else
		Buoyancy = 0.94 * Mass;
}

function ChunkUp(int Damage)
{
	if (bHidden)
		return;
	Spawn(class 'BloodSpurt2');
	if (bPlayerCarcass)
	{
		bHidden = true;
		SetPhysics(PHYS_None);
		SetCollision(false,false,false);
		bProjTarget = false;
	}
	else
		destroy();
}

function ClientExtraChunks()
{
	If ( Level.NetMode == NM_Server )
		return;
	Spawn(class 'Bloodspurt2');
}

simulated function Landed(vector HitNormal)
{
	local rotation finalRot;

	if ( trail != None )
	{
		trail.Destroy();
		trail = None;
	}
	finalRot = Rotation;
	finalRot.Roll = 0;
	finalRot.Pitch = 0;
	setRotation(finalRot);
	if ( Level.NetMode != NM_Server )
		Spawn(class 'Bloodspurt2');
	SetPhysics(PHYS_None);
	SetCollision(true, false, false);
}

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

	Velocity = 0.8 * (Velocity - 2 * HitNormal * (Velocity Dot HitNormal));
	Velocity.Z = FMin(Velocity.Z * 0.8, 700);
	speed = Size(Velocity);
	if ( (speed < 250) && (trail != None) )
	{
		trail.Destroy();
		trail = None;
	}
	else if ( speed > 350 )
	{
		decision = FRand();
		if ( decision < 0.2 )
			PlaySound(sound 'gibP1');
		else if ( decision < 0.4 )
			PlaySound(sound 'gibP3');
		else if ( decision < 0.6 )
			PlaySound(sound 'gibP4');
		else if ( decision < 0.8 )
			PlaySound(sound 'gibP5');
		else 
			PlaySound(sound 'gibP6');
	}
	if ( (trail == None) && (Level.NetMode != NM_Server) )
		Spawn(class 'Bloodspurt2');

	if ( speed < 120 )
	{
		bBounce = false;
		Disable('HitWall');
	}
}

function Damage( int Damage, Pawn instigatedBy, Vector hitlocation, 
						Vector momentum, name damageType)
{
	SetPhysics(PHYS_Falling);
	bBobbing = false;
	Velocity += momentum/Mass;
	CumulativeDamage += Damage;
	If ( Damage > FMin(15, Mass) || (CumulativeDamage > Mass) )
		ChunkUp(Damage);
}

auto state Dying
{
	ignores Damage;

Begin:
	if ( bDecorative )
		SetPhysics(PHYS_None);
	else if ( TrailSize > 0 )
	{
		trail = Spawn(class'BloodTrail',self);
		trail.DrawScale = TrailSize;
		trail.skin = BloodColor;
	}
	Sleep(0.2);
	GotoState('Dead');
}	

state Dead 
{
	function BeginState()
	{
		if ( bDecorative )
			lifespan = 0.0;
		else
			SetTimer(5.0, false);
	}
}

