//=============================================================================
// sgProtector.
// * Revised by 7DS'Lust
// Revised for Monster Madness by nOs*Wildcard
// Team-colored projectiles by SK
//=============================================================================
class sgProtector extends sgBuilding;

var() sound         FireSound;
var() class<Projectile>
                    ProjectileType;

simulated event Timer()
{
	Super.Timer();
	
	if ( SCount > 0 || Role != ROLE_Authority )
        return;
    
    if ( ShouldFire() && !bDisabledByEMP)
		Shoot(FindEnemy());
}

function PostBeginPlay()
{
	Super.PostBeginPlay();
	
	Switch(Team)
	{
	 case 0:
	 ProjectileType=Class'sgProtProjRed';
	 break;
	 
	 case 1:
	 ProjectileType=Class'sgProtProjBlue';
	 break;
	 
	 case 2:
	 ProjectileType=Class'sgProtProjGreen';
	 break;
	 
	 case 3:
	 ProjectileType=Class'sgProtProjYellow';
	 break;
	 
	 default:
	 ProjectileType=Class'sgProtProj';
	 break;
	
	}
}

function Shoot(Pawn target)
{
	local vector    fireSpot,
                    projStart;
	local Projectile
                    proj;
    local rotator   shootDirection;

	if ( target == None || VSize(target.Location - Location) > SightRadius )
        return;

    shootDirection = rotator(target.Location - Location);

	PlaySound(FireSound, SLOT_None, 0.75);

	if ( Grade > 2 )
	{
	    fireSpot = target.Location + FMin(1, 1.1 + 0.6 * FRand()) *
          target.Velocity * VSize(target.Location - Location) /
          ProjectileType.default.Speed;

		if ( !FastTrace(fireSpot, ProjStart) )
			fireSpot = 0.5 * (fireSpot + target.Location);

		shootDirection = Rotator(fireSpot - Location);
	}

	Spawn(ProjectileType, Self,, Location, shootDirection);
}

function bool ShouldFire()
{
    return ( FRand() < 0.05 + Grade/20 );
}

function bool ShouldAttack(Pawn enemy)
{
    local UT_Invisibility inv;
    local bool bInv;
	local Inventory RubberSuit;
	
	RubberSuit = enemy.FindInventoryType(class'WildcardsRubberSuit');

    if ( enemy == None )
        return false;

    if ( !FastTrace(enemy.Location) )
        return false;
		
    if ( ScriptedPawn(enemy) != None && enemy.Health > 10 )
        return true;
		
    if ( sgBuilding(enemy) != None )
    {
        if ( sgBuilding(enemy).Team == Team || sgBuilding(enemy).Energy < 0 )
            return false;
    }
    else if ( enemy.PlayerReplicationInfo == None ||
      enemy.PlayerReplicationInfo.Team == Team ||
      enemy.Health <= 10 || RubberSuit != none ||
      !enemy.bProjTarget)
        return false;
    return true;
}

function Pawn FindEnemy()
{
    local Pawn p, target;

    SightRadius = 1000 + 1750*Grade;

    foreach RadiusActors(class'Pawn', p, SightRadius)
        if ( ShouldAttack(p) && (target == None ||
          VSize(p.Location - Location) < VSize(target.Location - Location)) )
            target = p;

    return target;
}

defaultproperties
{
    FireSound=Sound'SiegeUltimateRC7UB01Media.SFX.sgProtShot'
    ProjectileType=Class'sgProtProj'
    BuildingName="Protector"
    BuildCost=100
    UpgradeCost=40
    MaxEnergy=1200.00
    SpriteScale=0.85
    Model=LodMesh'Botpack.Crystal'
    SkinRedTeam=Texture'SiegeUltimateRC7UB01Media.U.BuildingMaps.SuperProtectorSkinT0'
    SkinBlueTeam=Texture'SiegeUltimateRC7UB01Media.U.BuildingMaps.SuperProtectorSkinT1'
    SkinGreenTeam=Texture'SiegeUltimateRC7UB01Media.U.BuildingMaps.SuperProtectorSkinT2'
    SkinYellowTeam=Texture'SiegeUltimateRC7UB01Media.U.BuildingMaps.SuperProtectorSkinT3'
    SpriteRedTeam=Texture'SiegeUltimateRC7UB01Media.U.BuildingMaps.SuperProtectorSpriteT0'
    SpriteBlueTeam=Texture'SiegeUltimateRC7UB01Media.U.BuildingMaps.SuperProtectorSpriteT1'
    SpriteGreenTeam=Texture'SiegeUltimateRC7UB01Media.U.BuildingMaps.SuperProtectorSpriteT2'
    SpriteYellowTeam=Texture'SiegeUltimateRC7UB01Media.U.BuildingMaps.SuperProtectorSpriteT3'
    DSofMFX=1.20
    NumOfMFX=3
    MFXrotX=(Pitch=20000,Yaw=20000,Roll=20000),
    SightRadius=3000.00
    MultiSkins(0)=Texture'SiegeUltimateRC7UB01Media.GFX.sgProtSpriteT0'
    MultiSkins(1)=Texture'SiegeUltimateRC7UB01Media.GFX.sgProtSpriteT1'
    MultiSkins(2)=Texture'SiegeUltimateRC7UB01Media.GFX.sgProtSpriteT2'
    MultiSkins(3)=Texture'SiegeUltimateRC7UB01Media.GFX.sgProtSpriteT3'
}
