//=============================================================================
// WildcardsPoisonGuardian.
//=============================================================================
class WildcardsPoisonGuardian expands sgGuardian;

var() float         SupplyRadius;
var Pawn            Target;
var() texture TextureRed;
var() texture TextureBlue;
var() byte MFXFatness;

simulated event Timer()
{

	Super.Timer();

	if ( SCount > 0 || Role != ROLE_Authority )
        	return;

	if ( FRand() < 1/(6/(Grade+1)) && !bDisabledByEMP)
		Damage();


}

simulated function Damage()
{
    local float     dist,
                    moScale;
    local vector    dir;
    local Pawn p;
    local sgBuilding s;
    local float ShockSize;
    local effects e;
    local WildcardsFX FX;
	local PoisonPlayer Poison;
	local bool AlreadyPoisoned;
	local vector loc;

    ShockSize = ((Grade+1)* 20)+100;

    foreach RadiusActors(class 'Pawn', p, ShockSize )
    {
        dir = Location - p.Location;
        dist = VSize(dir); 
        dir = normal(dir); 

        MoScale = (((ShockSize)-dist)/(ShockSize))+0.1;

    	if ( FastTrace(p.Location) )
		if (p.bIsPlayer && p.Health > 0 &&
        p.PlayerReplicationInfo != None && 
    	p.PlayerReplicationInfo.Team != Team && 
 		!p.PlayerReplicationInfo.bIsSpectator
		|| ScriptedPawn(p) != None )
		{

			AlreadyPoisoned = false;

    		foreach RadiusActors(class 'PoisonPlayer', Poison, ShockSize )
    			{
					if (Poison.PoisonedPlayer == p)
						AlreadyPoisoned = true;
				}

			if (AlreadyPoisoned == false)
				{
					Poison = Spawn(Class'PoisonPlayer', Owner, , Location);
					Poison.PoisonedPlayer = p;
					
					switch ( Grade )
					{
						case 0:
							Poison.Slowness	= 0.5;
							Poison.RecoverRate = 0.800;
							break;
						case 1:
							Poison.Slowness	= 1;
							Poison.RecoverRate = 0.600;
							break;
						case 2:
							Poison.Slowness	= 1.5;
							Poison.RecoverRate = 0.400;
							break;
						case 3:
							Poison.Slowness	= 2;
							Poison.RecoverRate = 0.200;
							break;
						case 4:
							Poison.Slowness	= 2.5;
							Poison.RecoverRate = 0.100;
							break;
						case 5:
							Poison.Slowness	= 3;
							Poison.RecoverRate = 0.050;
							break;
					}

				}

			p.TakeDamage(moScale*10, Instigator, 0.5 *
			(p.CollisionHeight + p.CollisionRadius)*dir,vect(0,0,0), 'sgSpecial');
			if ( FRand() < 0.25) 
				{
					loc=100*VRand();
					loc.z *= 0.4;
					loc += Location;
					Self.PlaySound(sound'PoisonGasHiss',,4.0);
					FX = Spawn( class 'PoisonCloud',, '', loc, RotRand());
					FX.DrawScale *= (0.5+2*frand());
				}
		}
    }
    foreach RadiusActors(class 'sgBuilding', s, ShockSize )
    {
        dir = Location - s.Location;
        dist = VSize(dir); 
        dir = normal(dir); 

        MoScale = (((ShockSize)-dist)/(ShockSize))+0.1;

    	if ( FastTrace(s.Location) )
		if (s.Health > 0 && s.Team != Team)
		{
            s.TakeDamage(moScale*10, Instigator, 0.5 * (s.CollisionHeight + s.CollisionRadius)*dir,vect(0,0,0), 'sgSpecial');
			
			if ( FRand() < 0.15) 
				{
					loc=100*VRand();
					loc.z *= 0.4;
					loc += Location;
					Self.PlaySound(sound'PoisonGasHiss',,4.0);
					FX = Spawn( class 'PoisonCloud',, '', loc, RotRand());
					FX.DrawScale *= (0.5+2*frand());
				}
		}
    }
}




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

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;
    local WildcardsMeshFX newFX;

    DrawScale = SpriteScale;

    if ( Role == ROLE_Authority )
        Spawn(class'sgFlash');

    if ( Level.NetMode == NM_DedicatedServer )
        return;

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

        }
}

defaultproperties
{
    TextureRed=Texture'SiegeUltimateRC7UB01Media.U.BuildingMaps.PoisonGuardianSkinT0'
    TextureBlue=Texture'SiegeUltimateRC7UB01Media.U.BuildingMaps.PoisonGuardianSkinT1'
    MFXFatness=255
    BuildingName="Poison Guardian"
    BuildCost=1500
    BuildTime=35.00
    Model=LodMesh'UnrealI.TracerM'
    SkinRedTeam=Texture'SiegeUltimateRC7UB01Media.U.BuildingMaps.PoisonGuardianSkinT0'
    SkinBlueTeam=Texture'SiegeUltimateRC7UB01Media.U.BuildingMaps.PoisonGuardianSkinT1'
    SkinGreenTeam=Texture'SiegeUltimateRC7UB01Media.U.BuildingMaps.PoisonGuardianSkinT2'
    SkinYellowTeam=Texture'SiegeUltimateRC7UB01Media.U.BuildingMaps.PoisonGuardianSkinT3'
    SpriteRedTeam=Texture'SiegeUltimateRC7UB01Media.U.BuildingMaps.PoisonGuardianSpriteT0'
    SpriteBlueTeam=Texture'SiegeUltimateRC7UB01Media.U.BuildingMaps.PoisonGuardianSpriteT1'
    SpriteGreenTeam=Texture'SiegeUltimateRC7UB01Media.U.BuildingMaps.PoisonGuardianSpriteT2'
    SpriteYellowTeam=Texture'SiegeUltimateRC7UB01Media.U.BuildingMaps.PoisonGuardianSpriteT3'
    DSofMFX=0.50
    NumOfMFX=8
    MultiSkins(0)=Texture'SiegeUltimateRC7UB01Media.U.BuildingMaps.PoisonGuardianSpriteT0'
    MultiSkins(1)=Texture'SiegeUltimateRC7UB01Media.U.BuildingMaps.PoisonGuardianSpriteT1'
}
