//=============================================================================
// UnrealIPlayer.
//=============================================================================
class UnrealIPlayer expands PlayerPawn
	abstract;

#exec AUDIO IMPORT FILE="Sounds\Generic\land1.WAV" NAME="Land1" GROUP="Generic"
#exec AUDIO IMPORT FILE="Sounds\Generic\lsplash.WAV" NAME="LSplash" GROUP="Generic"

var(Sounds) sound 	drown;
var(Sounds) sound	breathagain;
var(Sounds) sound	Footstep1;
var(Sounds) sound	Footstep2;
var(Sounds) sound	Footstep3;
var(Sounds) sound	HitSound3;
var(Sounds) sound	HitSound4;
var(Sounds) sound	Die2;
var(Sounds) sound	Die3;
var(Sounds) sound	Die4;
var(Sounds) sound	Gasp;
var(Sounds) sound	UWHit1;
var(Sounds) sound	UWHit2;

replication
{
	// Functions server can call.
	reliable if( Role==ROLE_Authority )
		ClientPlayTakeHit;
}

function Possess()
{
	log(Self$" being possessed");
	CallParent.Possess();
}

function PlayFootStep()
{
	local sound step;
	local float decision;

	if ( !bIsWalking && (Level.Game.Difficulty > 1) && ((Weapon == None) || !Weapon.bPointing) )
		MakeNoise(0.05 * Level.Game.Difficulty);
	if ( FootRegion.Zone.bWaterZone )
	{
		ServerPlaySound(sound 'LSplash', SLOT_Interact, 0.5, false, 1500.0, 1.0);
		return;
	}

	decision = FRand();
	if ( decision < 0.34 )
		step = Footstep1;
	else if (decision < 0.67 )
		step = Footstep2;
	else
		step = Footstep3;

	if ( bIsWalking )
		ServerPlaySound(step, SLOT_Interact, 0.1, false, 400.0, 1.0);
	else 
		ServerPlaySound(step, SLOT_Interact, 0.4, false, 1000.0, 1.0);
}

function PlayHit(float Damage, vector HitLocation, name damageType, float MomentumZ)
{
	local float rnd;
	local Bubble bub;
	local bool bServerGuessWeapon;

	if (Damage > 1) //spawn some blood
	{
		if (damageType == 'Drowned')
		{
			rnd = FRand();
			if (rnd < 0.2)
				bub = spawn(class 'Bubble1',,, Location 
					+ 0.7 * CollisionRadius * vector(ViewRotation) + 0.3 * EyeHeight * vect(0,0,1));
			else if (rnd < 0.4)
				bub = spawn(class 'Bubble2',,, Location 
					+ 0.6 * CollisionRadius * vector(ViewRotation) + 0.3 * EyeHeight * vect(0,0,1));
			else if (rnd < 0.65)
				bub = spawn(class 'Bubble3',,, Location 
					+ 0.7 * CollisionRadius * vector(ViewRotation) + 0.3 * EyeHeight * vect(0,0,1));
			else if (rnd < 0.9)
				bub = spawn(class 'Bubble4',,, Location 
					+ 0.6 * CollisionRadius * vector(ViewRotation) + 0.5 * EyeHeight * vect(0,0,1));
			if (bub != None)
				bub.DrawScale = FRand()*0.06+0.04; 
		}
		else if ( (damageType != 'Burned') && (damageType != 'Corroded') 
					&& (damageType != 'Fell') )
				spawn(class 'BloodSpurt',,, hitLocation);
	}	

	if ( (damage > 0) || (ReducedDamageType == 'All') )
	{
		rnd = FClamp(Damage, 20, 40);
		if ( damageType == 'Burned' )
			ClientFlash( rnd * vect(0.009375, 0.009375, 0.009375), rnd * vect(0.01641, 0.011719, 0.0046875));
		else if ( damageType == 'corroded' )
			ClientFlash( rnd * vect(0.01171875,0.01171875,0.01171875), rnd * vect(0.009375, 0.0140625, 0.0046875));
		else if ( damageType == 'Drowned' )
			ClientFlash(vect(0.390625,0.390625,0.390625), vect(0.3125,0.46875,0.46875));
		else 
			ClientFlash( rnd * vect(0.01171875,0.01171875,0.01171875), rnd * vect(0.017578, 0.0046875, 0.0046875));

		ShakeView(0.15 + 0.005 * Damage, Damage * 30, 0.3 * Damage); 
		PlayTakeHitSound(Damage, damageType);
		bServerGuessWeapon = ( ((Weapon != None) && Weapon.bPointing) || (GetAnimGroup(AnimSequence) != 'Dodge') );
		ClientPlayTakeHit(0.1, hitLocation, Damage, bServerGuessWeapon ); 
		if ( !bServerGuessWeapon && (Level.NetMode == NM_Server) )
		{
			Enable('AnimEnd');
			BaseEyeHeight = Default.BaseEyeHeight;
			bAnimTransition = true;
			PlayTakeHit(0.1, hitLocation, Damage);
		}
	}
}

function PlayDeathHit(float Damage, vector HitLocation, name damageType)
{
	local Bubble bub;

	if ( Region.Zone.bDestructive && (Region.Zone.ExitActor != None) )
		Spawn(Region.Zone.ExitActor);
	if (HeadRegion.Zone.bWaterZone)
	{
		bub = spawn(class 'Bubble1',,, Location 
			+ 0.3 * CollisionRadius * vector(Rotation) + 0.8 * EyeHeight * vect(0,0,1));
		if (bub != None)
			bub.DrawScale = FRand()*0.08+0.03; 
		bub = spawn(class 'Bubble3',,, Location 
			+ 0.2 * CollisionRadius * VRand() + 0.7 * EyeHeight * vect(0,0,1));
		if (bub != None)
			bub.DrawScale = FRand()*0.08+0.03; 
		bub = spawn(class 'Bubble4',,, Location 
			+ 0.3 * CollisionRadius * VRand() + 0.6 * EyeHeight * vect(0,0,1));
		if (bub != None)
			bub.DrawScale = FRand()*0.08+0.03; 
	}

	//if ( damageType == 'Burned' )
	//	skin = texture'Burnt';
	//else 
	if ( (damageType != 'Drowned') && (damageType != 'Corroded') )
		spawn(class 'BloodSpurt',self,'', hitLocation);
}

//-----------------------------------------------------------------------------
// Sound functions

function PlayDyingSound()
{
	local float rnd;

	if ( HeadRegion.Zone.bWaterZone )
	{
		if ( FRand() < 0.5 )
			PlaySound(UWHit1, SLOT_Pain,,,,Frand()*0.2+0.9);
		else
			PlaySound(UWHit2, SLOT_Pain,,,,Frand()*0.2+0.9);
		return;
	}

	rnd = FRand();
	if (rnd < 0.25)
		PlaySound(Die, SLOT_Talk);
	else if (rnd < 0.5)
		PlaySound(Die2, SLOT_Talk);
	else if (rnd < 0.75)
		PlaySound(Die3, SLOT_Talk);
	else 
		PlaySound(Die4, SLOT_Talk);
}

function PlayTakeHitSound(int damage, name damageType)
{
	if ( HeadRegion.Zone.bWaterZone )
	{
		if ( damageType == 'Drowned' )
			PlaySound(drown, SLOT_Pain, 1.5);
		else if ( FRand() < 0.5 )
			PlaySound(UWHit1, SLOT_Pain,2.0,,,Frand()*0.2+0.9);
		else
			PlaySound(UWHit2, SLOT_Pain,2.0,,,Frand()*0.2+0.9);
		return;
	}
	damage *= FRand();

	if (damage < 8) 
		PlaySound(HitSound1, SLOT_Pain,2.0,,,Frand()*0.2+0.9);
	else if (damage < 25)
	{
		if (FRand() < 0.5) PlaySound(HitSound2, SLOT_Pain,2.0,,,Frand()*0.2+0.9);			
		else PlaySound(HitSound3, SLOT_Pain,2.0,,,Frand()*0.2+0.9);
	}
	else
		PlaySound(HitSound4, SLOT_Pain,2.0,,,Frand()*0.2+0.9);			
}

function ClientPlayTakeHit(float tweentime, vector HitLoc, int Damage, bool bServerGuessWeapon)
{
		bServerGuessWeapon = ( ((Weapon != None) && Weapon.bPointing) || (GetAnimGroup(AnimSequence) != 'Dodge') );
	if ( bServerGuessWeapon && ((GetAnimGroup(AnimSequence) != 'Dodge') || ((Weapon != None) && Weapon.bPointing)) )
		return;
	Enable('AnimEnd');
	bAnimTransition = true;
	BaseEyeHeight = Default.BaseEyeHeight;
	PlayTakeHit(tweentime, HitLoc, Damage);
}	

function Gasp()
{
	if ( Role != ROLE_Authority )
		return;
	if ( PainTime < 2 )
		PlaySound(Gasp, SLOT_Talk, 2.0);
	else
		PlaySound(BreathAgain, SLOT_Talk, 2.0);
}

