//=============================================================================
// sgGuardian
// Written by nOs*Badger
// Higor: optimizing this and bringing it back
//=============================================================================
class sgGuardian extends sgBuilding;

var float ShockSize;

simulated event Timer()
{

	Super.Timer();

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

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


}

function bool CanAttackPlayer( pawn P)
{
	if (p.bIsPlayer && p.Health > 0 && (p.PlayerReplicationInfo != None) && (p.PlayerReplicationInfo.Team != Team) && !p.PlayerReplicationInfo.bIsSpectator
		|| (ScriptedPawn(p) != None) )
		return FastTrace(p.Location);
	return false;
}

function Damage()
{
    local float     dist,
                    moScale;
    local vector    dir;
    local Pawn p;
    local effects e;

	foreach RadiusActors(class 'Pawn', p, ShockSize )
	{
		if ( CanAttackPlayer(p) )
		{
			dir = Normal(Location - p.Location);
			dist = VSize(Location - p.Location); 
			MoScale = (((ShockSize)-dist)/(ShockSize))+0.1;
			p.TakeDamage(moScale*10, Instigator, 0.5 * (p.CollisionHeight + p.CollisionRadius)*dir, dir * sqrt(dist) * 250, 'sgSpecial');
			if ( FRand() < 0.25 && (e == none) )
			{
				PlaySound(sound'UnrealShare.Skrjshot',, 7.0);
				e = Spawn(Class'UnrealShare.ParticleBurst', Owner, , Location);
			}
		}
  		else if ( (sgBuilding(p) != none) && (sgBuilding(p).Team != Team) && (p.Health > 0) && FastTrace(p.Location) )
		{
			dir = Normal(Location - p.Location);
			dist = VSize(Location - p.Location); 
			MoScale = (((ShockSize)-dist)/(ShockSize))+0.1;
			p.TakeDamage(moScale*10, Instigator, 0.5 * (p.CollisionHeight + p.CollisionRadius)*dir,vect(0,0,0), 'sgSpecial');
			if ( FRand() < 0.15 && (e == none) ) 
			{
				PlaySound(sound'UnrealShare.Skrjshot',, 7.0);
				e = Spawn(Class'UnrealShare.ParticleBurst', Owner, , Location);
			}
		}
 	}
}

function Upgraded()
{
	AmbientGlow=255/(6-Grade);
	ShockSize = ((Grade+1)* 20)+100;
}


defaultproperties
{
     ShockSize=120
     BuildingName="Guardian"
     BuildCost=400
     BuildTime=15.000000
     MaxEnergy=8500.000000
     SpriteScale=0.400000
     Model=LodMesh'Botpack.Module'
     SkinRedTeam=None
     SkinBlueTeam=None
     DSofMFX=3.800000
     NumOfMFX=3
     MFXrotX=(Pitch=20000,Yaw=20000,Roll=20000)
     AmbientGlow=40
     SkinRedTeam=Texture'ContainerSkinTeam0'
     SkinBlueTeam=Texture'ContainerSkinTeam1'
     SpriteRedTeam=Texture'CoreSpriteTeam0'
     SpriteBlueTeam=Texture'CoreSpriteTeam1'
     SkinGreenTeam=Texture'ContainerSkinTeam2'
     SkinYellowTeam=Texture'ContainerSkinTeam3'
     SpriteGreenTeam=Texture'CoreSpriteTeam2'
     SpriteYellowTeam=Texture'CoreSpriteTeam3'
     CollisionHeight=32.000000
     GUI_Icon=Texture'GUI_Guardian'
}
