//=============================================================================
// Translocator.
//=============================================================================
class Translocator expands Weapon;

#exec MESH  IMPORT MESH=Transloc ANIVFILE=MODELS\Translocator_a.3D DATAFILE=MODELS\Translocator_d.3D X=0 Y=0 Z=0 UNMIRROR=1
#exec MESH ORIGIN MESH=Transloc X=0 Y=0 Z=0 YAW=64 PITCH=-10 ROLL=-10
#exec MESH SEQUENCE MESH=Transloc SEQ=All		STARTFRAME=0  NUMFRAMES=146
#exec MESH SEQUENCE MESH=Transloc SEQ=Twiddle1  STARTFRAME=0 NUMFRAMES=30
#exec MESH SEQUENCE MESH=Transloc SEQ=Twiddle2  STARTFRAME=30  NUMFRAMES=30
#exec MESH SEQUENCE MESH=Transloc SEQ=PreThrow  STARTFRAME=60 NUMFRAMES=20
#exec MESH SEQUENCE MESH=Transloc SEQ=Throw     STARTFRAME=60 NUMFRAMES=20
#exec MESH SEQUENCE MESH=Transloc SEQ=Thrown    STARTFRAME=79 NUMFRAMES=1
#exec MESH SEQUENCE MESH=Transloc SEQ=Down		STARTFRAME=80 NUMFRAMES=11
#exec MESH SEQUENCE MESH=Transloc SEQ=Select	STARTFRAME=91 NUMFRAMES=15
#exec MESH SEQUENCE MESH=Transloc SEQ=Reset		STARTFRAME=26 NUMFRAMES=20
#exec MESH SEQUENCE MESH=Transloc SEQ=Transport STARTFRAME=34 NUMFRAMES=20
#exec TEXTURE IMPORT NAME=tloc FILE=MODELS\transloc.PCX GROUP="Skins"
#exec MESHMAP SCALE MESHMAP=Transloc X=0.005 Y=0.005 Z=0.01
#exec MESHMAP SETTEXTURE MESHMAP=Transloc NUM=1 TEXTURE=tloc

var TranslocatorTarget TTarget;
var float TossForce;
var Weapon PreviousWeapon;
var Actor DesiredTarget;
var float MaxTossForce;
var bool bUnFired, bBotMoveFire;

function float RateSelf( out int bUseAltMode )
{
	return -2; //FIXME - until bots understand translocator as weapon
}

function BringUp()
{
	PreviousWeapon = None;
	Super.BringUp();
}

function RaiseUp(Weapon OldWeapon)
{
	if ( OldWeapon == self )
		PreviousWeapon = None;
	else
		PreviousWeapon = OldWeapon;
	Super.BringUp();
}

// return delta to combat style
function float SuggestAttackStyle()
{
	local float EnemyDist;

	if ( TTarget != None )
		return -0.6;

	EnemyDist = VSize(Pawn(Owner).Enemy.Location - Owner.Location);
	if ( EnemyDist < 700 )
		return 1.0;
	else
		return -0.2;
}

function float SuggestDefenseStyle()
{	
	if ( TTarget != None )
		return 0;

	return -0.6;
}
	
function bool HandlePickupQuery( inventory Item )
{
	if ( Item.IsA('TranslocatorTarget') && (item == TTarget) )
	{
		TTarget.Destroy();
		TTarget = None;
		return true;
	}
	else
		Super.HandlePickupQuery(Item);
}

function Destroyed()
{
	Super.Destroyed();
	if ( TTarget != None )
		TTarget.Destroy();
}

function SetSwitchPriority(pawn Other)
{
	AutoSwitchPriority = 0;
}

function float SwitchPriority() 
{
	local int bTemp;
	
	if ( !Owner.IsA('PlayerPawn') )
		return RateSelf(bTemp);
	else 
		return -2;
}

function Fire( float Value )
{
	local vector Dest, Start, HitLocation, HitNormal;
	local Actor HitActor;
	
	if ( bBotMoveFire )
		return;

	if ( TTarget != None )
	{
		if ( TTarget.Disruption > 80 )
		{
			Pawn(Owner).gibbedBy(TTarget.disruptor);
			return;
		}
		TweenAnim('Twiddle1', 0.01);
		if ( TTarget.Owner != None )
			Dest = TTarget.Owner.Location;
		else
			Dest = TTarget.Location + vect(0,0,40);
		
		if ( Level.Game.IsA('DeathMatchPlus') 
			&& !DeathMatchPlus(Level.Game).AllowTranslocation(Pawn(Owner), Dest) )
			return;

		Start = Pawn(Owner).Location;
		if ( Pawn(Owner).SetLocation(Dest) )
		{
			HitActor = Trace(HitLocation, HitNormal, Pawn(Owner).Location, Dest, false);
			if (HitActor != None )
				Pawn(Owner).SetLocation(Start);
			else  
				Level.Game.PlayTeleportEffect(Owner, true, true);
		}

		if ( TTarget != None )
		{
			TTarget.Destroy();
			TTarget = None;
		}
		bPointing=True;
		if ( Owner != None )
 			GotoState('NormalFire');
	}
	else
	{
		bPointing=True;
		GoToState('Tossing');
	}

}

function AltFire( float Value )
{
	if ( (PreviousWeapon == None)
		|| ((PreviousWeapon.AmmoType != None) && (PreviousWeapon.AmmoType.AmmoAmount <=0)) )
		Pawn(Owner).SwitchToBestWeapon();
	else
	{
		Pawn(Owner).PendingWeapon = PreviousWeapon;
		PutDown();
	}	
}

function ThrowTarget()
{
	PlaySound(AltFireSound, SLOT_Misc, Pawn(Owner).SoundDampening);	
	TweenAnim('Thrown',0.01);
	if ( Owner.IsA('Bot') )
		bBotMoveFire = true;
	TTarget = Spawn(class'TranslocatorTarget',,, Owner.Location);
	TTarget.Master = self;
	if ( Owner.IsA('Bot') )
		TTarget.SetCollisionSize(0,0); 
	TTarget.Throw(Pawn(Owner), TossForce);
}

////////////////////////////////////////////////////////
//Tossing = throw the target out
state Tossing
{
	function Tick(float DeltaTime)
	{
		if ( (pawn(Owner).bFire == 0) || (TossForce > MaxTossForce) )
		{
			Disable('Tick');
			Gotostate ('Tossing', 'Throwing');
		}
		else
			TossForce += DeltaTime * 400;
	}

	function BeginState()
	{
		TossForce = 250;
	}

Throwing:
	ThrowTarget();
	FinishAnim();
	pawn(Owner).SwitchToBestWeapon();
	GotoState('Idle');
Begin:
	PlayAnim('PreThrow',0.6);

}

///////////////////////////////////////////////////////
// Teleport to target (or if fails, get target back)
state NormalFire
{
	function bool PutDown()
	{
		GotoState('DownWeapon');
		return True;
	}
	
	function BeginState()
	{
		bUnFired = false;
		Super.BeginState();
	}

Begin:
	FinishAnim();
	PlayAnim('Twiddle1');
	FinishAnim();
	if ( Owner.IsA('Bot') )
		Bot(Owner).SwitchToBestWeapon();
	if ( Pawn(Owner).bFire == 0 )
	{
		if ( bUnFired )
			GotoState('Idle');
		else
			bUnFired = true;
	}
	Goto('Begin');
}

///////////////////////////////////////////////////////////
function PlayIdleAnim()
{
	local Float decision;
	if ( TTarget != None )
		TweenAnim('Thrown', 1.0);
	else if ( VSize(Pawn(Owner).Velocity)>20 ) {
		PlayAnim('Transport',0.1);
	}
	else  
	{
		decision = FRand();
		if ( decision<0.5 ) 
			PlayAnim('Twiddle1',0.5);
			PlayAnim('Twiddle2',0.5);			
	}
	Enable('AnimEnd');
}

function PlaySelect()
{
	Pawn(Owner).ClientMessage("The fact that the translocator is fucked up is not Steve's fault!");
	if ( TTarget != None )
		TweenAnim('Thrown', 1.0);
	else
		PlayAnim('Select',0.9, 0.0);
	PlaySound(SelectSound, SLOT_Misc,Pawn(Owner).SoundDampening);		
}

function PlayPostSelect()
{
	local actor RealTarget;

	// If Bot is wanting a specific target fired at, do it
	if ( DesiredTarget != None )
	{
		TossForce = MaxTossForce;
		RealTarget = Owner.Target;
		Owner.Target = DesiredTarget;
		Pawn(Owner).ViewRotation = Pawn(Owner).AdjustToss(TossForce, Owner.Location, 0, true, true); 
		Owner.Target = RealTarget;
		ThrowTarget();
		TTarget.DesiredTarget = DesiredTarget;
		DesiredTarget = None;
	}	
}

defaultproperties
{
	 bCanThrow=false
	 MaxTossForce=+650.0
	 AIRating=-1
     AmmoName=UnrealI.NullAmmo
	 AutoSwitchPriority=0
     PickupAmmoCount=1
     InventoryGroup=1
     PickupMessage="You got the Translocator Source Module"
     RespawnTime=+00000.000000
     PlayerViewOffset=(X=8.000000,Y=-2.500000,Z=-4.700000)
	 Mesh=Transloc
     PickupViewMesh=Module
     PlayerViewMesh=Transloc
	 ThirdPersonMesh=Module
     bMeshCurvy=False
     CollisionRadius=+00008.000000
     CollisionHeight=+00003.000000
}
