//=============================================================================
// GuidedWarShell.
//=============================================================================
class GuidedWarShell expands WarShell;

var Pawn Guider;
var vector GuidedVelocity;
var rotator OldGuiderRotation, GuidedRotation;

replication
{
	// Things the server should send to the client.
	unreliable if( Role==ROLE_Authority )
		GuidedVelocity;
}

function Destroyed()
{
	local WarheadLauncher W;

	if ( (PlayerPawn(Guider) != None) )
		PlayerPawn(Guider).ViewTarget = None;

	if ( Guider != None )
	{
		W = WarheadLauncher(Guider.FindInventoryType(class'WarheadLauncher'));
		if ( W != None )
		{
			W.GuidedShell = None;
			W.GotoState('Finishing');
		}
	}
	Super.Destroyed();
}

function PostBeginPlay()
{
	Super.PostBeginPlay();
	GuidedRotation = Rotation;
	OldGuiderRotation = Rotation;
}

simulated function Tick(float DeltaTime)
{
	local int DeltaYaw, DeltaPitch;
	local int YawDiff, PitchDiff;

	if ( Level.NetMode == NM_Client )
	{
		if ( GuidedVelocity != vect(0,0,0) )
			Velocity = GuidedVelocity;
	}
	else
	{
		if ( Guider.Health <= 0 )
			Explode(Location,Vect(0,0,1));
		else
		{
			DeltaYaw = (Guider.ViewRotation.Yaw & 65535) - (OldGuiderRotation.Yaw & 65535);
			DeltaPitch = (Guider.ViewRotation.Pitch & 65535) - (OldGuiderRotation.Pitch & 65535);
			if ( DeltaPitch < -32768 )
				DeltaPitch += 65536;
			else if ( DeltaPitch > 32768 )
				DeltaPitch -= 65536;
			if ( DeltaYaw < -32768 )
				DeltaYaw += 65536;
			else if ( DeltaYaw > 32768 )
				DeltaYaw -= 65536;

			YawDiff = (Rotation.Yaw & 65535) - (GuidedRotation.Yaw & 65535) - DeltaYaw;
			if ( DeltaYaw < 0 )
			{
				if ( ((YawDiff > 0) && (YawDiff < 16384)) || (YawDiff < -49152) )
					GuidedRotation.Yaw += DeltaYaw;
			}	
			else if ( ((YawDiff < 0) && (YawDiff > -16384)) || (YawDiff > 49152) )
				GuidedRotation.Yaw += DeltaYaw;

			GuidedRotation.Pitch += DeltaPitch;

			Velocity += Vector(GuidedRotation) * 1500 * DeltaTime;
			Velocity = Normal(Velocity) * 500;
			GuidedVelocity = Velocity;
			OldGuiderRotation = Guider.ViewRotation;
		}
	}
	SetRotation(Rotator(Velocity));
}

simulated function PostRender( canvas Canvas )
{
	local float Dist;
	local Actor A;
	local int XPos, YPos;
	local Vector X,Y,Z, Dir;

	GetAxes(Rotation, X,Y,Z);
	Canvas.Font = Font'TinyRedFont';
	Canvas.Style = 2;
	foreach visiblecollidingactors(class'Actor', A, 1500)
	{
		Dir = A.Location - Location;
		Dist = VSize(Dir);
		Dir = Dir/Dist;
		if ( (Dir Dot X) > 0.7 )
		{
			XPos = 0.5 * Canvas.ClipX * (1 + 1.4 * (Dir Dot Y));
			YPos = 0.5 * Canvas.ClipY * (1 - 1.4 * (Dir Dot Z));
			Canvas.SetPos(XPos - 8, YPos - 8);
			Canvas.DrawIcon(texture'CrossHair6', 1.0);
			Canvas.SetPos(Xpos - 12, YPos + 8);
			Canvas.DrawText(Dist, true);
		}
	}	
}		

defaultproperties
{
}
