//=============================================================================
// TranslocatorTarget.
//=============================================================================
class TranslocatorTarget expands Translocator;

#exec MESH  IMPORT MESH=Module ANIVFILE=MODELS\Module_a.3D DATAFILE=MODELS\Module_d.3D X=0 Y=0 Z=0 UNMIRROR=1
#exec MESH ORIGIN MESH=Module X=0 Y=0 Z=0 YAW=64 PITCH=-10 ROLL=-10
#exec MESH SEQUENCE MESH=Module SEQ=All		STARTFRAME=0  NUMFRAMES=14
#exec MESH SEQUENCE MESH=Module SEQ=Open	STARTFRAME=0  NUMFRAMES=3
#exec MESH SEQUENCE MESH=Module SEQ=Spin	STARTFRAME=3  NUMFRAMES=11
#exec TEXTURE IMPORT NAME=tloc FILE=MODELS\transloc.PCX GROUP="Skins"
#exec MESHMAP SCALE MESHMAP=Module X=0.01 Y=0.01 Z=0.02
#exec MESHMAP SETTEXTURE MESHMAP=Module NUM=1 TEXTURE=tloc

#exec AUDIO IMPORT FILE="..\unrealshare\Sounds\EightBal\grenflor.wav" NAME="GrenadeFloor" GROUP="Eightball"
#exec AUDIO IMPORT FILE="..\unreali\Sounds\Krall\krasht2.wav" NAME="Krasht2" GROUP="Krall"
#exec AUDIO IMPORT FILE="Sounds\Pickups\ambhum3.wav" NAME="targethum" GROUP="translocator"

var float Disruption;
var pawn Disruptor;
var pawn Firer;
var translocator Master;

function float RateSelf( out int bUseAltMode )
{
	return 1000; // want to get rid of it fast
}

function float SuggestAttackStyle()
{
	return -0.5;
}

function float SuggestDefenseStyle();


function DropFrom(vector StartLocation)
{
	if ( Master == None )
		return;
	else
		Super.DropFrom(StartLocation);
}

singular function ZoneChange( ZoneInfo NewZone )
{
	local float splashsize;
	local actor splash;

	if( NewZone.bWaterZone )
	{
		if( !Region.Zone.bWaterZone && (Velocity.Z < -200) )
		{
			// Else play a splash.
			splashSize = FClamp(0.0001 * Mass * (250 - 0.5 * FMax(-600,Velocity.Z)), 1.0, 3.0 );
			if( NewZone.EntrySound != None )
				PlaySound(NewZone.EntrySound, SLOT_Interact, splashSize);
			if( NewZone.EntryActor != None )
			{
				splash = Spawn(NewZone.EntryActor); 
				if ( splash != None )
					splash.DrawScale = splashSize;
			}
		}
	}
}


// Compare self to current weapon.  If better than current weapon, then switch
// Targets are special in that they make the other weapon go down instantly (need fast reaction)
function bool WeaponSet(Pawn Other)
{
	local bool bSwitch,bHaveAmmo;
	local Inventory Inv;
	
	if ( Other.Weapon == self)
		return false;

	Other.PendingWeapon = self;
	Other.ChangedWeapon();
	return true;	
}

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

function Fire( float Value )
{
	bPointing=True;
	GoToState('Tossing');
}

function Throw(Pawn Thrower, float force)
{
	local vector dir;

	Firer = Thrower;
	dir = vector(Thrower.ViewRotation);
	Velocity = FMin(force, 650) * dir + vect(0,0,180);
	bBounce = true;
	DropFrom(Thrower.Location + 0.3 * Thrower.CollisionRadius * dir + Thrower.CollisionHeight * vect(0,0,0.3));
	SetCollision(true,true,true);
}

function PlaySelect()
{
	TweenAnim('Spin',0.03);
	PlaySound(SelectSound, SLOT_Misc,Pawn(Owner).SoundDampening);		
}

////////////////////////////////////////////////////////
state Tossing
{
	function Timer()
	{
		Bot(Owner).bFire = 0;
		Disable('Tick');
		TossForce += 100;
		Gotostate ('Tossing', 'Throwing');
	}

	function BeginState()
	{
		Super.BeginState();
		if ( Owner.IsA('Bot') && (Owner != Master.Owner) )
			SetTimer(0.05 + FRand() * (0.3 - 0.1 * Bot(Owner).skill), false);
	}

Throwing:
	PlaySound(AltFireSound, SLOT_Misc, Pawn(Owner).SoundDampening);	
	PlayAnim('Throw', 1.5, 0.03);
	FinishAnim();
	Throw(Pawn(Owner), TossForce);
Begin:
	PlayAnim('PreThrow',0.6);
}

state Idle
{
Begin:
	if ( Owner.IsA('Bot') && (Master.Owner != Owner) )
	{
		Pawn(Owner).bFire = 1;
		Fire(0.0);
	}
	bPointing=False;
	if ( (AmmoType != None) && (AmmoType.AmmoAmount<=0) ) 
		Pawn(Owner).SwitchToBestWeapon();  //Goto Weapon that has Ammo
	if ( Pawn(Owner).bFire!=0 ) Fire(0.0);
	if ( Pawn(Owner).bAltFire!=0 ) AltFire(0.0);	
	Disable('AnimEnd');
	PlayIdleAnim();
}
state Active
{
Begin:
	FinishAnim();
	if ( Owner.IsA('Bot') && (Master.Owner != Owner) )
	{
		Pawn(Owner).bFire = 1;
		Global.Fire(0.0);
	}
	if ( bChangeWeapon )
		GotoState('DownWeapon');
	bWeaponUp = True;
	PlayPostSelect();
	FinishAnim();
	Finish();
}

////////////////////////////////////////////////////////
auto state Pickup
{
	function Timer()
	{
		//disruption effect
		Spawn(class'Electricity');
		PlaySound(sound'Krasht2', SLOT_None, 4.0);
		if ( Disruption > 120 )
			SetTimer(0.5 + 3 * FRand(), false);
	}

	event Landed( vector HitNormal )
	{
		if ( Master.Owner.IsA('Bot') )
		{
			if ( Pawn(Master.Owner).Weapon == Master )
				Bot(Master.Owner).SwitchToBestWeapon();
			LifeSpan = 10;
		}
		Firer = None;
		Disable('Tick');
		SetCollision(true,false,false);
		PlayAnim('Open');
	}		

	function Bump ( Actor Other )
	{
		Touch(Other);
	}

	event TakeDamage( int Damage, Pawn EventInstigator, vector HitLocation, vector Momentum, name DamageType)
	{

		Disruption += Damage;
		Disruptor = EventInstigator;
		SetTimer(0.3, false);
	
		SetCollision(true,true,true);
		SetPhysics(PHYS_Falling);
		Velocity = Momentum;
		Velocity.Z = FMax(Velocity.Z, 0.7 * VSize(Velocity));
	}

	function Touch( Actor Other )
	{
		local bool bMasterTouch;

		if ( !Other.IsA('Pawn') )
			return;

		bMasterTouch = ( Other == Master.Owner );
		
		if ( !bMasterTouch && (Physics == PHYS_None) )
			return;
		if ( bBounce && (bMasterTouch || (Other == Firer)) ) 
			return;
		if ( Level.Game.bTeamGame && !bMasterTouch
			&& (Pawn(Master.Owner).PlayerReplicationInfo.Team == Pawn(Other).PlayerReplicationInfo.Team) )
			return;

		if ( Master.Owner.IsA('Bot') && !bMasterTouch )
		{
			Master.bBotMoveFire = false;
			Master.Fire(0.0);
		}
		SetCollision(true,false,false);
		Super.Touch(Other);
	}

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

		Firer = None;
		PlaySound(Misc2Sound, SLOT_Misc);	  // hit wall sound
		Velocity = 0.4*(( Velocity dot HitNormal ) * HitNormal * (-2.0) + Velocity);   // Reflect off Wall w/damping
		speed = VSize(Velocity);
		if ( speed > 400 )
			Speed *= 0.8;
		else if (speed < 100) 
			bBounce = False;
	}

	function Tick(float DeltaTime)
	{
		local actor HitActor;
		local vector HitLocation, HitNormal;

		if ( (DesiredTarget == None) || (Master == None) )
		{
			Disable('Tick');
			if ( Master.Owner.IsA('Bot') && (Pawn(Master.Owner).Weapon == Master) )
				Bot(Master.Owner).SwitchToBestWeapon();
			return;
		}

		if ( (Abs(Location.X - DesiredTarget.Location.X) < 18)
			&& (Abs(Location.Y - DesiredTarget.Location.Y) < 18) )
		{
			HitActor = Trace(HitLocation, HitNormal, DesiredTarget.Location, Location, false);
			if ( HitActor != None )
				return;	

			Pawn(Master.Owner).StopWaiting();
			Master.bBotMoveFire = false;
			Master.Fire(0.0);
			if ( Master.Owner.IsA('Bot') && (Pawn(Master.Owner).Weapon == Master) )
				Bot(Master.Owner).SwitchToBestWeapon();
			Disable('Tick');
		}
	}

	function BeginState()
	{
		LoopAnim('Spin');
	}

	function EndState()
	{
		Super.EndState();
		DesiredTarget = None;
		if ( (Master != None) && (Master.Owner != None)
			&& Master.Owner.IsA('Bot') && (Pawn(Master.Owner).Weapon == Master) )
			Bot(Master.Owner).SwitchToBestWeapon();
	}
}

defaultproperties
{
	 Mass=20.0
     Misc2Sound=Sound'UnrealI.Eightball.GrenadeFloor'
	 AutoSwitchPriority=255
     PickupMessage="You got the Translocator Target Module"
	 MaxDesireability=+0.0
     bCollideWorld=True
	 bBlockActors=True
	 bBlockPlayers=True
	 AmbientSound=Sound'targethum'
	 SoundRadius=20
	 SoundVolume=128
     PlayerViewMesh=Module
	 Mesh=Module
}
