//=============================================================================
// sxDashPad.
// Written By WILDCARD
//=============================================================================
class sxDashPad extends sgBuilding config(SiegeXtreme);

var float           RepairTimer;
var DashPlayer Dash;
var bool InUse;

// For Fine Tuning
var() config float DashTime;
var() config float DashFactor;
var() config float DoubleDashFactor;
var() config float DashChargeRate;
var() config int   DashMultiDashLimit;

replication
{
	reliable if ( Role==ROLE_Authority )
		DoubleDashClient, Dash;
}

/*
simulated function CompleteBuilding()
{
	SaveConfig();
}
*/

simulated event Touch(Actor other)
{
	local int Matches;
	
	log("--- Dash Pad: Touch()");
	
	Enable('UnTouch');

	if ( Role == ROLE_Authority )
		{
			if ( DoneBuilding == false || InUse == true || SCount > 0 || Pawn(other) == None || !Pawn(other).bIsPlayer ||
				Pawn(other).PlayerReplicationInfo == None || 
				Pawn(other).PlayerReplicationInfo.Team != Team )
				return;

			Matches = 0;
				
			foreach AllActors(class'DashPlayer',Dash)
				{
					if ( Dash.DashPawn == Pawn(other) )
						{
							Matches++;
							// log("Match #"@Matches@"Player:"@Dash.DashPawn.PlayerReplicationInfo.Playername);
							if ( Dash.UsedPad(Self) == false )
								{
									// log("DashPad: DOUBLE DASH!");
									DoubleDashClient(Dash.DashPawn);
									Dash.DashChargeRate = DashChargeRate;
									Dash.Charge = Dash.LifeSpan+DashTime;
									Dash.DashFactor *= DoubleDashFactor;
									Dash.PadHistory[Dash.PadIndex] = self;
									Dash.PadIndex++;
									return;
								}
							else
								{
									// log("DashPad: WARNING! This player is already DASHING! What shall we do now?");
									Disable('UnTouch');
									return;
								}
						}
				}
				
			
			
			// log("Dash Pad: Chargeing...");
			Dash = Spawn(Class'DashPlayer',Owner,,Location);
			// Dash.log("< < < DASH ACTOR CREATED > > >");
			Dash.DashPawn = Pawn(other);
			Dash.Chargeing = True;
			Dash.DashChargeRate = DashChargeRate;
			Dash.MaxCharge = DashTime;
			Dash.DashFactor = (Grade+1)*DashFactor;
			Dash.PadHistory[Dash.PadIndex] = self;
			Dash.PadIndex++;
			// log("Dash ="@Dash);
			
			InUse = true;
			
		}
	else
		{
			if ( DoneBuilding == false || InUse == true || SCount > 0 || Pawn(other) == None || !Pawn(other).bIsPlayer ||
			Pawn(other).PlayerReplicationInfo == None || 
			Pawn(other).PlayerReplicationInfo.Team != Team )
				return;
			
			foreach AllActors(class'DashPlayer',Dash)
				{
					// log("Found A Dash Actor");
					if ( Dash.DashPawn == Pawn(other) )
						{
							// log("This Dash Actor Controls A Player!");
							if ( Dash.UsedPad(Self) == true )
								{
									// log("No sound for yuu! you already visited this Dash PAD!!!");
									return;
								}
							else
								// log("This Dash Actor SAIS the dashpad has never been used :S");
						}
				}
				
			// log("Dash Pad: Dash ="@Dash);
			// log("Dash Pad: Chargeing ="@Dash.Chargeing);
			//log("===============================================================================");
			//log("============ SOUND PLAYED OMFG LOOK!!!! =======================================");
			//log("===============================================================================");
			PlaySound(Sound'DashCharge');
		}
}

simulated function DoubleDashClient(actor other)
{
	//log("DoubleDashClient() SOUNDS PLAYED!!!!");
	Pawn(other).PlaySound(Sound'DashBoost');
	PlaySound(Sound'DashBoost');
}

simulated event UnTouch(Actor other)
{
	Disable('UnTouch');
	log("Dash Pad: UnTouch()");
	
	if ( DoneBuilding == false || Dash.Chargeing == False )
		return;
		
    if ( SCount <= 0 && Pawn(other) != None && Pawn(other).bIsPlayer &&
      Pawn(other).PlayerReplicationInfo != None &&
      Pawn(other).PlayerReplicationInfo.Team == Team )
    {
		if ( Role != ROLE_Authority )
			PlaySound(Sound'DashOut');
		else
			{
				Dash.Chargeing = False;
				InUse = false;
			}
    }
}

simulated function Upgraded()
{
	local float Percent;
	
    if ( Role == ROLE_Authority )
		{
			Percent = Energy/MaxEnergy;
			MaxEnergy = default.MaxEnergy * (1 + Grade/2);
			Energy = Percent * MaxEnergy;
		}
}

function Destruct()
{
	Dash.Destroy();
	super.Destruct();
}

defaultproperties
{
     BuildingName="Dash Pad"
     BuildCost=600
     UpgradeCost=90
     BuildTime=10.000000
     MaxEnergy=3500.000000
     Model=LodMesh'UnrealI.CryopodM'
     SkinRedTeam=Texture'SuperProtectorSkinT0'
     SkinBlueTeam=Texture'SuperProtectorSkinT1'
     SpriteRedTeam=Texture'ProtectorSpriteTeam0'
     SpriteBlueTeam=Texture'ProtectorSpriteTeam1'
     DSofMFX=0.65
     MFXrotX=(Yaw=20000)
     CollisionHeight=30.000000
	 DashTime=2.5
	 DashFactor=1
	 DoubleDashFactor=1.25
	 DashChargeRate=0.10
}
