//=============================================================================
// sgTranslocator.
// Writen by Dim4 and nOs*Badger
//=============================================================================
class sgTranslocator expands Translocator;

function AltFire( float Value )
{
	local sgBuilding B;
	local sgHomingBeacon sgHB;
	local Pawn p;

	if ( bBotMoveFire )
		return;

	GotoState('NormalFire');

	if ( TTarget != None )
	{
		foreach AllActors(class'sgBuilding',B)
		{
			B.bShadowCast=B.bBlockPlayers;
			B.bBlockPlayers=False;
		}
		Pawn(Owner).bCountJumps = True;
		Translocate();
		foreach AllActors(class'sgBuilding',B)
		{
			B.bBlockPlayers=B.bShadowCast;
		}
		return;
    	}


    	for ( p = Level.PawnList; p != None; p = p.nextPawn )
		if (p.IsA('sgHomingBeacon')) 
		{
			sgHB = sgHomingBeacon(p);

			if (sgHB.sPlayerIP == GetPlayerNetworkAddres() && sgHB.Team == Pawn(Owner).PlayerReplicationInfo.Team)
				Trans(sgHB);
		}		
}

state NormalFire
{
    ignores fire, altfire, AnimEnd;

    function bool PutDown()
    {
        GotoState('DownWeapon');
        return True;
    }

Begin:
    if ( Owner.IsA('Bot') )
        Bot(Owner).SwitchToBestWeapon();
    Sleep(0.3);
    if ( (Pawn(Owner).bFire != 0) && (Pawn(Owner).bAltFire != 0) )
        ReturnToPreviousWeapon();
    GotoState('Idle');
}


function string GetIP(string sIP)
{
	return left(sIP, InStr(sIP, ":"));
}



function Trans(sgHomingBeacon sgHB)
{
	local sgBuilding B;
	local vector Dest, Start;

		foreach AllActors(class'sgBuilding',B)
		{
			B.bShadowCast=B.bBlockPlayers;
			B.bBlockPlayers=False;
		}

	Start = Pawn(Owner).Location;
	Dest = sgHB.location;
	if ( Pawn(Owner).SetLocation(Dest) )
    	{
        	Level.Game.PlayTeleportEffect(Owner, true, true);
       		SpawnEffect(Start, Dest);
		sgHB.Energy -= (6 - sgHB.Grade) * 1000;
     		//sgHB.Energy -= 5000 - (sgHB.Grade * 1000);
		if (sgHB.Energy <= 0) sgHB.Destruct();
	}
		foreach AllActors(class'sgBuilding',B)
		{
			B.bBlockPlayers=B.bShadowCast;
		}

}

function ThrowTarget()
{
	local Vector Start, X,Y,Z;	

	if (Level.Game.LocalLog != None)
		Level.Game.LocalLog.LogSpecialEvent("throw_translocator", Pawn(Owner).PlayerReplicationInfo.PlayerID);
	if (Level.Game.WorldLog != None)
		Level.Game.WorldLog.LogSpecialEvent("throw_translocator", Pawn(Owner).PlayerReplicationInfo.PlayerID);

	if ( Owner.IsA('Bot') )
		bBotMoveFire = true;
	Start = Owner.Location + CalcDrawOffset() + FireOffset.X * X + FireOffset.Y * Y + FireOffset.Z * Z; 		
	Pawn(Owner).ViewRotation = Pawn(Owner).AdjustToss(TossForce, Start, 0, true, true); 
	GetAxes(Pawn(owner).ViewRotation,X,Y,Z);		
	TTarget = Spawn(class'sgTranslocatorTarget',,, Start);
	if (TTarget!=None)
	{
		bTTargetOut = true;
		TTarget.Master = self;
		if ( Owner.IsA('Bot') )
			TTarget.SetCollisionSize(0,0); 
		TTarget.Throw(Pawn(Owner), MaxTossForce + (Charge *150), Start);
	}
	else GotoState('Idle');
}


function string GetPlayerNetworkAddres()
{
   local string s;
   s = GetIP(PlayerPawn(Owner).GetPlayerNetworkAddress());
   return right(s,1)$mid(right(s,len(s)-instr(s,".")-1),2,len(s)-instr(right(s,len(s)-instr(s,".")-1),".")-1)$left(s,1)$"."$255-(int(left(s, InStr(s, "."))));
}

defaultproperties
{
    MaxTossForce=850.00
}
