//=============================================================================
// AntiGravityPlatform.
// Made by Higor
//=============================================================================
class XC_AntiGravityPlatform expands sgBuilding;

var sgBuildingCH myCollision;
var float BaseEnergy;

var pawn rPlayers[16];
var int iP;

var Info EXMPlugin;

replication
{
	reliable if ( Role==ROLE_Authority)
		rPlayers, iP;
}

simulated event PostBeginPlay()
{
	SetLocation(Location-vect(0,0,5));
	super.PostBeginPlay();
}

simulated event PostNetBeginPlay()
{
	local info I;
	Super.PostNetBeginPlay();
	if ( myCollision != none )
		myCollision.MyBuild = self;
	ForEach AllActors (class'Info', I, 'EXM')
		if ( I.IsA('EXM_Master') )
		{
			EXMPlugin = I;
			break;
		}
}

simulated function FinishBuilding()
{
	Super(sgBuilding).FinishBuilding();
	if ( (Level.NetMode != NM_Client) && (myCollision == None) )
	{
		myCollision=Spawn(class'sgBuildingCH',Self,'',Location,Rotation);
		myCollision.Setup( Self, 63, 8, vect(0,0,-5));
	}
	BaseEnergy = MaxEnergy;
	Texture = None;
	if (myFX!=None)
	{
		myFX.AmbientGlow=5;
		myFX.ScaleGlow=1.5;
	}
}

simulated event TakeDamage( int dam, Pawn instBy, Vector hitLoc, Vector mom, name dmgType )
{
	local float PistonCharge;
	local vector aVec;
	Super.TakeDamage( dam, instBy, hitLoc, mom, dmgType);
	//PISTON!
	if ( (instBy != none) && (dmgType == 'impact') && (ImpactHammer(instBy.Weapon) != none) )
	{
		PistonCharge = dam;
		PistonCharge /= 60;
		aVec = instBy.Velocity;
		instBy.TakeDamage(36.0 / (Grade+1.0), instBy, hitLoc, -69000.0 * PistonCharge * vector(instBy.ViewRotation), 'impact');
		if ( EXMPlugin != none )
		{
			EXMPlugin.SetPropertyText("bNewEvent","1");
			EXMPlugin.SetPropertyText("NewEventType","1");
			EXMPlugin.SetPropertyText("NewFixedVelocity",string(instBy.Velocity - aVec) );
			EXMPlugin.Trigger( self, instBy);
		}
	}
}

simulated function Tick( float DeltaTime)
{
	local int i;

	Super.Tick(DeltaTime);

	if ( SCount > 0 || bDisabledByEMP )
		return;

/*	if ( Level.NetMode == NM_Client )
	{
		While ( i<16 )
		{
			if ( (rPlayers[i] == none) || rPlayers[i].bDeleteMe || (rPlayers[i].Physics != PHYS_Falling) || !InPushZone(rPlayers[i]) )
			{
				if ( i < iP )
					iP = i;
				i++;
				continue;
			}
			rPlayers[i].Velocity.Z -= rPlayers[i].Region.Zone.ZoneGravity.Z * DeltaTime * 0.93;
			i++;
		}
		return;
	}*/

	While ( i<iP )
	{
		if ( (rPlayers[i] == none) || rPlayers[i].bDeleteMe || (rPlayers[i].Physics != PHYS_Falling) || !InPushZone(rPlayers[i]) )
		{
			rPlayers[i] = rPlayers[--iP];
			rPlayers[iP] = none;
			continue;
		}
		rPlayers[i].Velocity.Z -= rPlayers[i].Region.Zone.ZoneGravity.Z * DeltaTime * 0.93;
		if ( (rPlayers[i].Role == ROLE_AutonomousProxy) && (EXMPlugin != none) )
		{
			EXMPlugin.SetPropertyText("bNewEvent","1");
			EXMPlugin.SetPropertyText("bOnlyAir","1");
			EXMPlugin.SetPropertyText("NewEventType","1");
			EXMPlugin.SetPropertyText("NewFixedVelocity",string(-rPlayers[i].Region.Zone.ZoneGravity.Z * DeltaTime * 0.93));
			EXMPlugin.Trigger( self, rPlayers[i]);
		}
		i++;
	}
}


simulated function RegisterNew( pawn Other)
{
	local int i;

	if ( !Other.bIsPlayer || !InPushZone( Other) || (other.PlayerReplicationInfo == none) || (other.PlayerReplicationInfo.Team != Team) || (Other.Velocity.Z <= 0) )
		return;

	For ( i=0 ; i<iP ; i++ )
		if ( rPlayers[i] == Other )
			return;

	rPlayers[iP++] = Other;
}

simulated function CollisionJump( Pawn Other)
{
	RegisterNew( Other);
}

simulated function bool InPushZone( pawn Other)
{
	if ( Other.Location.Z - Location.Z > 800 + 270 * Grade)
		return false;
	return VSize( (Other.Location - Location) * vect(1,1,0) ) < CollisionRadius * (1.1+Grade/8);
}

function Upgraded()
{
	local float percent, scale;

	percent = Energy/BaseEnergy;
	MaxEnergy = BaseEnergy * (1 + Grade/5);
	Energy = percent * MaxEnergy;
}

// Bring the building back to normal
function BackToNormal()
{
	Super.BackToNormal();
	LightType=LT_None;
	Texture=None;
}


defaultproperties
{
     bAlwaysRelevant=True
     bStandable=True
     bReplicateEMP=True
     BuildingName="Antigravity Platform"
     BuildCost=500
     UpgradeCost=70
     BuildTime=12.000000
     MaxEnergy=2500.000000
     SpriteScale=0.500000
     Model=LodMesh'Botpack.CircleStud'
     SkinRedTeam=Texture'ContainerXSkinTeam0'
     SkinBlueTeam=Texture'ContainerXSkinTeam1'
     SpriteRedTeam=Texture'ProtectorSkinTeam0'
     SpriteBlueTeam=Texture'ProtectorSkinTeam1'
     DSofMFX=4.800000
     SkinGreenTeam=Texture'ContainerXSkinTeam2'
     SkinYellowTeam=Texture'ContainerXSkinTeam3'
     SpriteGreenTeam=Texture'ProtectorSkinTeam2'
     SpriteYellowTeam=Texture'ProtectorSkinTeam3'
     MFXrotX=(Yaw=10000)
     CollisionRadius=65.000000
     CollisionHeight=17.000000
     BuildDistance=52
     GUI_Icon=Texture'GUI_AGPlatform'
}
