//=============================================================================
// WildcardsForceField.
//=============================================================================
class WildcardsForceField expands sgBuilding;

var() float         SupplyRadius;
var Pawn            Target;
var() texture TextureRed;
var() texture TextureBlue;
var() byte MFXFatness;
var Projectile BlockedProjectile;
var Actor ProjectileOwner;
var WildcardsFX FX;
var WildcardsForceField NewField;
var WildcardsMeshFX newFX;
var sgMeshFX MeshFX;
var int RealTeam;

replication
{
	unreliable if( Role==ROLE_Authority )
		TextureRed, TextureBlue, NewField, newFX, MeshFX, RealTeam;
}

simulated function Upgraded()
{	
	AmbientGlow=255/(6-Grade);
	Enlarge();
}

simulated function Enlarge()
{
	local float Percent;
	local int IntGrade;

	IntGrade = Grade;
    if ( Role == ROLE_Authority )
		{
			switch ( IntGrade )
				{
					case 1:
						NewField = Spawn(class'WildcardsForceFieldLevel1',Self);
						break;
					case 2:
						NewField = Spawn(class'WildcardsForceFieldLevel2',Self);
						break;
					case 3:
						NewField = Spawn(class'WildcardsForceFieldLevel3',Self);
						break;
					case 4:
						NewField = Spawn(class'WildcardsForceFieldLevel4',Self);
						break;
					case 5:
						NewField = Spawn(class'WildcardsForceFieldLevel5',Self);
						break;
				}
			if ( NewField != none )
				{
					NewField.Team = Team;
					NewField.RealTeam = Team;
					NewField.sPlayerIP = sPlayerIP;
					NewField.Grade = Grade;
					NewField.UpgradeCost = UpgradeCost;
					Percent = Energy/MaxEnergy;
					NewField.default.MaxEnergy = default.MaxEnergy;
					NewField.MaxEnergy = default.MaxEnergy * (Grade+1);
					NewField.Energy = Percent * NewField.MaxEnergy;
					NewField.Texture = NewField.MultiSkins[Team];
					
					
					NewField.SetOwner(Owner);
					
					if ( grade == 0 || Team == 0)
						FinishBuilding();;
					SilentDestruct();
				}
			else
				{
					Grade = Grade;
					Percent = Energy/MaxEnergy;
					MaxEnergy = MaxEnergy * (Grade+1);
					Energy = Percent * MaxEnergy;
				}
		}
}

function SilentDestruct()
{
    bHidden = true;
	Destroy();
}

simulated function FinishBuilding()
{
	MultiSkins[4] = TextureRed;
	MultiSkins[5] = TextureBlue;
	MultiSkins[6] = texture'SiegeUltimateRC7UB01Media.sgEnvSkinT3';
	MultiSkins[7] = texture'SiegeUltimateRC7UB01Media.sgEnvSkinT0';
    JustFinishBuilding();
}

simulated function JustFinishBuilding()
{
    local int i;

    DrawScale = SpriteScale;

    if ( Role == ROLE_Authority && Grade == 0 )
		Spawn(class'sgFlash');

    if ( Level.NetMode == NM_DedicatedServer )
        return;

    if ( MeshFX == None && Model != None )
        for ( i = 0; i < numOfMFX; i++ )
        {
            newFX = Spawn(class'WildcardsMeshFX', Self,,,rotator(vect(0,0,0)));
            MeshFX = newFX;
            MeshFX.Mesh = Model;
            MeshFX.DrawScale = DSofMFX;
            MeshFX.Fatness = MFXFatness;
            MeshFX.RotationRate.Pitch = MFXrotX.Pitch*FRand();
            MeshFX.RotationRate.Roll = MFXrotX.Roll*FRand();
            MeshFX.RotationRate.Yaw = MFXrotX.Yaw*FRand();
        }
}

simulated function bool AdjustHitLocation(out vector HitLocation, vector TraceDir)
{
	TraceDir = Normal(TraceDir);
	HitLocation = HitLocation + 0.4 * CollisionRadius * TraceDir;
	return true;
}

simulated event TakeDamage( int damage, Pawn instigatedBy, Vector hitLocation, 
  Vector momentum, name damageType )
{
	local int actualDamage;
	local float tempScore;

	if ( Role < ROLE_Authority || Level.Game == None )
        return;

	if ( damage < 0 )
		damage = damage-(damage*2);

	actualDamage = Level.Game.ReduceDamage(Damage, DamageType, Self,
      instigatedBy);

    if( instigatedBy != None &&
      instigatedBy.PlayerReplicationInfo != None )
    {
        if ( TeamGamePlus(Level.Game) != None &&
          instigatedBy.PlayerReplicationInfo.Team == Team )
        {
            actualDamage *= TeamGamePlus(Level.Game).FriendlyFireScale;
	        tempScore = -1 * FMin(Energy, actualDamage);
        }
        else
            tempScore = FMin(Energy, actualDamage);

        tempScore *= 1 + Grade/10;

        instigatedBy.PlayerReplicationInfo.Score += tempScore/1000;

        if ( sgPRI(instigatedBy.PlayerReplicationInfo) != None )
	        sgPRI(instigatedBy.PlayerReplicationInfo).AddRU((tempScore/30)*RuRewardScale);
    }
    if ( sgBuilding(instigatedBy) != None &&
      TeamGamePlus(Level.Game) != None &&
      sgBuilding(instigatedBy).Team == Team )
        actualDamage *= TeamGamePlus(Level.Game).FriendlyFireScale;

	Spawn(class'ForceFieldFlash',,,hitlocation);
	Self.PlaySound(Sound'UnrealShare.General.Expla02',,7.0);
	Energy -= actualDamage;
	if ( Energy <= 0 )
        Destruct();
}

defaultproperties
{
    TextureRed=WetTexture'SiegeUltimateRC7UB01Media.U.BuildingMaps.ForceFieldT0'
    TextureBlue=WetTexture'SiegeUltimateRC7UB01Media.U.BuildingMaps.ForceFieldT1'
    MFXFatness=125
    BuildingName="Forcefield"
    BuildCost=1200
    UpgradeCost=30
    BuildTime=15.00
    MaxEnergy=4000.00
    SpriteScale=0.50
    Model=LodMesh'Botpack.earth1'
    SkinRedTeam=WetTexture'SiegeUltimateRC7UB01Media.U.BuildingMaps.ForceFieldT0'
    SkinBlueTeam=WetTexture'SiegeUltimateRC7UB01Media.U.BuildingMaps.ForceFieldT1'
    SkinGreenTeam=WetTexture'SiegeUltimateRC7UB01Media.U.BuildingMaps.ForceFieldT2'
    SkinYellowTeam=WetTexture'SiegeUltimateRC7UB01Media.U.BuildingMaps.ForceFieldT3'
    SpriteRedTeam=Texture'SiegeUltimateRC7UB01Media.U.BuildingMaps.ForceFieldAuraT0'
    SpriteBlueTeam=Texture'SiegeUltimateRC7UB01Media.U.BuildingMaps.ForceFieldAuraT1'
    SpriteGreenTeam=Texture'SiegeUltimateRC7UB01Media.U.BuildingMaps.ForceFieldAuraT2'
    SpriteYellowTeam=Texture'SiegeUltimateRC7UB01Media.U.BuildingMaps.ForceFieldAuraT3'
    MFXrotX=(Pitch=10000,Yaw=10000,Roll=10000),
    MultiSkins(0)=Texture'SiegeUltimateRC7UB01Media.U.BuildingMaps.ForceFieldAuraT0'
    MultiSkins(1)=Texture'SiegeUltimateRC7UB01Media.U.BuildingMaps.ForceFieldAuraT1'
    MultiSkins(2)=Texture'SiegeUltimateRC7UB01Media.U.BuildingMaps.ForceFieldAuraT2'
    MultiSkins(3)=Texture'SiegeUltimateRC7UB01Media.U.BuildingMaps.ForceFieldAuraT3'
    CollisionRadius=40.00
    CollisionHeight=40.00
}
