//=============================================================================
// WarHeadLauncher
//=============================================================================
class WarHeadLauncher expands Weapon;

#exec MESH IMPORT  MESH=WarHead ANIVFILE=MODELS\war_a.3D DATAFILE=MODELS\war_d.3D X=0 Y=0 Z=0 
#exec MESH ORIGIN MESH=WarHead X=0 Y=-210 Z=-50 YAW=64 PITCH=16  ROLL=-62
#exec MESH SEQUENCE MESH=WarHead SEQ=All         STARTFRAME=0   NUMFRAMES=50
#exec MESH SEQUENCE MESH=WarHead SEQ=Select      STARTFRAME=0   NUMFRAMES=15
#exec MESH SEQUENCE MESH=WarHead SEQ=Still       STARTFRAME=15  NUMFRAMES=1
#exec MESH SEQUENCE MESH=WarHead SEQ=Idle        STARTFRAME=15  NUMFRAMES=5
#exec MESH SEQUENCE MESH=WarHead SEQ=Down        STARTFRAME=20  NUMFRAMES=7
#exec MESH SEQUENCE MESH=WarHead SEQ=Fire        STARTFRAME=28  NUMFRAMES=7
#exec TEXTURE IMPORT NAME=Jwarhead1 FILE=MODELS\warh1.PCX GROUP=Skins
#exec TEXTURE IMPORT NAME=Jwarhead2 FILE=MODELS\warh2.PCX GROUP=Skins
#exec TEXTURE IMPORT NAME=Jwarhead3 FILE=MODELS\warh3.PCX GROUP=Skins
#exec TEXTURE IMPORT NAME=Jwarhead4 FILE=MODELS\warh4.PCX GROUP=Skins
#exec MESHMAP SCALE MESHMAP=WarHead X=0.006 Y=0.006 Z=0.012
#exec MESHMAP SETTEXTURE MESHMAP=WarHead NUM=0 TEXTURE=Jwarhead1
#exec MESHMAP SETTEXTURE MESHMAP=WarHead NUM=1 TEXTURE=Jwarhead2
#exec MESHMAP SETTEXTURE MESHMAP=WarHead NUM=2 TEXTURE=Jwarhead3
#exec MESHMAP SETTEXTURE MESHMAP=WarHead NUM=3 TEXTURE=Jwarhead4

#exec MESH IMPORT MESH=WHPick ANIVFILE=MODELS\WHPick_a.3D DATAFILE=MODELS\WHPick_d.3D X=0 Y=0 Z=0
#exec MESH ORIGIN MESH=WHPick X=150 Y=-10 Z=0 YAW=0 ROLL=-64
#exec MESH SEQUENCE MESH=WHPick SEQ=All         STARTFRAME=0   NUMFRAMES=1
#exec MESH SEQUENCE MESH=WHPick SEQ=Still       STARTFRAME=0   NUMFRAMES=1
#exec TEXTURE IMPORT NAME=Jwhpick1 FILE=MODELS\whpick.PCX GROUP=Skins
#exec MESHMAP SCALE MESHMAP=WHPick X=0.1 Y=0.1 Z=0.2
#exec MESHMAP SETTEXTURE MESHMAP=WHPick NUM=1 TEXTURE=Jwhpick1

#exec MESH IMPORT MESH=WHHand ANIVFILE=MODELS\WHpick_a.3D DATAFILE=MODELS\WHpick_d.3D X=0 Y=0 Z=0
#exec MESH ORIGIN MESH=WHHand X=-150 Y=-110 Z=0 YAW=0 ROLL=-64
#exec MESH SEQUENCE MESH=WHHand SEQ=All         STARTFRAME=0   NUMFRAMES=1
#exec MESH SEQUENCE MESH=WHHand SEQ=Still       STARTFRAME=0   NUMFRAMES=1
#exec MESHMAP SCALE MESHMAP=WHHand X=0.07 Y=0.07 Z=0.14
#exec MESHMAP SETTEXTURE MESHMAP=WHHand NUM=1 TEXTURE=JWHPick1

#exec TEXTURE IMPORT NAME=Readout FILE=MODELS\data.PCX GROUP="Icons" FLAGS=2 MIPS=OFF
#exec TEXTURE IMPORT NAME=GuidedX FILE=MODELS\crshair.PCX GROUP="Icons" FLAGS=2 MIPS=OFF

#exec AUDIO IMPORT FILE="Sounds\Warhead\warheadshot.wav" NAME="WarheadShot" GROUP=WarHead
#exec AUDIO IMPORT FILE="Sounds\Warhead\warheadpickup.wav" NAME="WarheadPickup" GROUP=WarHead
#exec AUDIO IMPORT FILE="Sounds\Warhead\warheadaltshot.wav" NAME="WarheadAltShot" GROUP=WarHead

var GuidedWarShell GuidedShell;
var int Scroll;
var PlayerPawn GuidingPawn;
var bool	bGuiding;
var() sound AltFireSound;

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

simulated function PostRender( canvas Canvas )
{
	local int i, numReadouts, OldClipX, OldClipY;

	bOwnsCrossHair = bGuiding;

	if ( !bGuiding )
		return;

	GuidedShell.PostRender(Canvas);
	OldClipX = Canvas.ClipX;
	OldClipY = Canvas.ClipY;
	Canvas.SetPos( 0.5 * OldClipX - 128, 0.5 * OldClipY - 128 );
	Canvas.Style = 2;
	Canvas.DrawIcon(Texture'GuidedX', 1.0);

	numReadouts = OldClipY/128 + 2;
	for ( i = 0; i < numReadouts; i++ )
	{ 
		Canvas.SetPos(1,Scroll + i * 128);
		Scroll--;
		if ( Scroll < -128 )
			Scroll = 0;
		Canvas.DrawIcon(Texture'Readout', 1.0);
	}
}	

function float RateSelf( out int bUseAltMode )
{
	if ( (AmmoType.AmmoAmount <=0) || (Pawn(Owner).Enemy == None) )
		return -2;

	bUseAltMode = 0;

	if ( Owner.IsInState('Charging') 
		|| ((VSize(Pawn(Owner).Enemy.Location - Owner.Location) < 1500) 
		&& (Pawn(Owner).Health > 0.3 * Pawn(Owner).Enemy.Health)) )
		return -0.1;

	if ( Owner.IsA('Bot') && (Bot(Owner).OldEnemy == None) )
		return 0.8 * AIRating;
		 
	return AIRating;
}

// return delta to combat style
function float SuggestAttackStyle()
{
	return -1.0;
}

function PlayFiring()
{
	if ( PlayerPawn(Owner) != None )
	{
		PlayerPawn(Owner).ClientInstantFlash( -0.4, vect(950, 650, 290));
		PlayerPawn(Owner).ShakeView(ShakeTime, ShakeMag, ShakeVert);
	}
	PlayAnim( 'Fire', 0.3 );		
	Owner.PlaySound(FireSound, SLOT_None,2.0*Pawn(Owner).SoundDampening);
}

function setHand(float Hand)
{
	local Rotator newRot;

	PlayerViewOffset.Y = Default.PlayerViewOffset.Y;
	PlayerViewOffset.X = Default.PlayerViewOffset.X;
	PlayerViewOffset.Z = Default.PlayerViewOffset.Z;
	
	PlayerViewOffset *= 100; //scale since network passes vector components as ints
	newRot = Rotation;
	setRotation(newRot);
}

function AltFire( float Value )
{
/*	local actor Victims;

	foreach RadiusActors( class 'Actor', Victims, 4000, Location )
	{
		if (WarShell(Victims) != None) Warshell(Victims).Explode(Victims.Location,Vect(0,0,1));
	}
*/
	if ( !Owner.IsA('PlayerPawn') )
	{
		// FIXME - bots should do guided attacks, but specially when pick up weapon
		Fire(Value);
		return;
	}

	if (AmmoType.UseAmmo(1))
	{
		PlayerPawn(Owner).ShakeView(ShakeTime, ShakeMag, ShakeVert);
		bPointing=True;
		PlayFiring();
		GuidedShell = GuidedWarShell(ProjectileFire(AltProjectileClass, ProjectileSpeed, bWarnTarget));
		PlayerPawn(Owner).ViewTarget = GuidedShell;
		GuidedShell.Guider = PlayerPawn(Owner);
		if ( Owner.bHidden )
			CheckVisibility();
		GotoState('Guiding');
		Owner.PlaySound(FireSound, SLOT_None,2.0*Pawn(Owner).SoundDampening);
	}
}

State Guiding
{
	function Fire ( float Value )
	{
		if ( GuidedShell != None )
			GuidedShell.Explode(GuidedShell.Location,Vect(0,0,1));

		GotoState('Finishing');
	}

	function AltFire ( float Value )
	{
		Fire(Value);
	}

	function BeginState()
	{
		Scroll = 0;
		bGuiding = true;
		Disable('Fire');
		Disable('AltFire');
		if ( Owner.IsA('PlayerPawn') )
		{
			GuidingPawn = PlayerPawn(Owner);
			PlayerPawn(Owner).ClientAdjustGlow(-0.2,vect(200,0,0));
		}
	}

	function EndState()
	{
		bGuiding = false;
		if ( GuidingPawn != None )
		{
			GuidingPawn.ClientAdjustGlow(0.2,vect(-200,0,0));
			GuidingPawn = None;
		}
	}


Begin:
	Sleep(1.0);
	Enable('Fire');
	Enable('AltFire');
}

State Finishing
{
	ignores Fire, AltFire;

Begin:
	Sleep(1.0);
	GotoState('Idle');
}

defaultproperties
{
     AmmoName=Class'Botpack.WarheadAmmo'
     ReloadCount=1
     PickupAmmoCount=3
     bWarnTarget=True
     bAltWarnTarget=True
     bSplashDamage=True
     FireOffset=(X=80.000000,Z=-10.000000)
     ProjectileClass=Class'BotPack.WarShell'
     AltProjectileClass=Class'BotPack.GuidedWarShell'
     shakemag=350.000000
     shaketime=0.200000
     shakevert=7.500000
     AIRating=1.000000
     RefireRate=0.250000
     AltRefireRate=0.250000
     AutoSwitchPriority=10
     InventoryGroup=5
     PickupMessage="You got the Redeemer"
     ItemName="Redeemer"
     RespawnTime=60.000000
     PlayerViewOffset=(X=1.800000,Y=1.000000,Z=-1.890000)
     PlayerViewMesh=Mesh'BotPack.WarHead'
     BobDamping=0.985000
     PickupViewMesh=Mesh'BotPack.WHPick'
     ThirdPersonMesh=Mesh'BotPack.WHHand'
     Mesh=Mesh'BotPack.WHPick'
     bNoSmooth=False
     bMeshCurvy=False
     CollisionRadius=40.000000
     CollisionHeight=23.000000
	 DeathMessage="%o was vaporized by %k's %w!!"
     PickupSound=Sound'WarheadPickup'
	 FireSound=Sound'WarheadShot'
	 AltFireSound=Sound'WarheadAltShot'
}
