//=============================================================================
// Mine 
// Written by nOs*Badger
// Revised for Monster Madness by nOs*Wildcard
// Revised by SK
//=============================================================================
class Mine extends sgBuilding;

function PostBeginPlay()
{

Super.PostBeginPlay();

Texture = Texture'SKFlare';

}

simulated event Timer()
{
    	local Pawn p;
	Super.Timer();
	

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

		foreach RadiusActors(class'Pawn', p, 75)
			if ( p.bIsPlayer && p.Health > 0 &&
        			p.PlayerReplicationInfo != None &&
          			p.PlayerReplicationInfo.Team != Team && !p.PlayerReplicationInfo.bIsSpectator || ScriptedPawn(p) != None )
				if (p != None && !bDisabledByEMP) 
					Damage();
}

simulated function string KillMessage( name damageType, pawn Other )
{
    local sgWarhead sgNuke;
    local string s;
    local string sWarhead;
    local pawn p;

    p = GetAwardedPlayer();

    s=p.PlayerReplicationInfo.PlayerName;

    if (s!="" && s!=" ")
         s=" built by"@s;

    if (sgNukeLauncher(Other.FindInventoryType(class'sgNukeLauncher')).AmmoType.AmmoAmount == 1)
	{
        sWarhead = ". "$Other.PlayerReplicationInfo.PlayerName@"was carrying a WARHEAD!!!";
	sgPRI(p.PlayerReplicationInfo).AddRU(500);
	sgPRI(p.PlayerReplicationInfo).sgInfoWarheadKiller++;
	}
	
	else if (sgNukeLauncher(Other.FindInventoryType(class'sgNukeLauncher')).AmmoType.AmmoAmount > 1)
	{
        sWarhead = ". "$Other.PlayerReplicationInfo.PlayerName@"was carrying TWO WARHEADS!!!";
	sgPRI(p.PlayerReplicationInfo).AddRU(1000);
	sgPRI(p.PlayerReplicationInfo).sgInfoWarheadKiller++;
	sgPRI(p.PlayerReplicationInfo).sgInfoWarheadKiller++;
	}

    else
        sWarhead = ".";
    return (""@Other.PlayerReplicationInfo.PlayerName@"was killed by a"@BuildingName$s$sWarhead);
}

function bool ShouldAttack(Pawn enemy)
{
    if ( ScriptedPawn(enemy) != None )
		return true;
	if ( enemy == None )
        return false;
    if ( !FastTrace(enemy.Location) )
        return false;
    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.bProjTarget )
        return false;
    return true;
}


function Damage()
{
	local int i;
    	local Pawn p;
	local Pawn pAward;

    	local vector dir;
	local int x;
	local int y;
	local int z;
	local string sMessage;
	local string sName;

	Self.PlaySound(Sound'SharpExplosion',, 4.0);

	pAward = GetAwardedPlayer();

        for ( i = 0; i <= Grade; i++ )
	{
		dir.x = Rand(80)-40;
		dir.y = Rand(80)-40;
		dir.z = Rand(80)-40;
		spawn(class'MineExplosion',,,Location + dir); 
		foreach RadiusActors(class'Pawn', p, 75+(i*25))
			if (p != None) 
				if (ShouldAttack(p))
				{
        				dir = Location - p.Location;
        				dir = normal(dir); 
					if (p.Health>0)
					{
        					p.TakeDamage((Grade+1) * 15, instigator, 0.5 * (p.CollisionHeight + p.CollisionRadius)*dir, vect(0,0,0), 'mine');
						sgPRI(pAward.PlayerReplicationInfo).AddRU((Grade+1) * 4);
					}
				}
	}

	sgPRI(pAward.PlayerReplicationInfo).AddRU(50);

        Destroy();

}



function AnnounceAll(string sMessage)
{
    local Pawn p;

    for ( p = Level.PawnList; p != None; p = p.nextPawn )
	    if ( (p.bIsPlayer || p.IsA('MessagingSpectator')) &&
          p.PlayerReplicationInfo != None  )
		    p.ClientMessage(sMessage);
}

function Pawn GetAwardedPlayer()
{
    local Pawn p;

    if (Pawn(Owner) != None) return Pawn(Owner);

    for ( p = Level.PawnList; p != None; p = p.nextPawn )
	    if ( (p.bIsPlayer || p.IsA('MessagingSpectator')) &&
          p.PlayerReplicationInfo != None  )
		if (GetPlayerNetworkAddres() == sPlayerIp)
			return p;

}

simulated function CompleteBuilding()
{
    	
}

simulated function FinishBuilding()
{
    local int i;
    local sgMeshFX newFX;
    local vector spawnLocation;
	
	Texture=Texture'Botpack.FLAKAMMOLED';

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

    if ( Level.NetMode == NM_DedicatedServer )
        return;

    spawnLocation = Location;
    spawnLocation.Z -= 10;
    if ( myFX == None && Model != None )
        for ( i = 0; i < numOfMFX; i++ )
        {
            newFX = Spawn(class'WildcardsMeshFX', Self,,,
              rotator(vect(0,0,0)));
            newFX.NextFX = myFX;
            myFX = newFX;
            myFX.Mesh = Model;
            myFX.DrawScale = DSofMFX;
            myFX.RotationRate.Pitch = MFXrotX.Pitch*FRand();
            myFX.RotationRate.Roll = MFXrotX.Roll*FRand();
            myFX.RotationRate.Yaw = MFXrotX.Yaw*FRand();
	    myFX.AmbientGlow=1;
	    myFX.Style = STY_Translucent;
	    myFX.ScaleGlow = 0.2;
	    //myFX.LODBias=0.200000;
        }
}

defaultproperties
{
    BuildingName="Mine"
    BuildCost=125
    UpgradeCost=15
    BuildTime=1.00
    MaxEnergy=2000.00
    SpriteScale=0.25
    Model=LodMesh'Botpack.DiscStud'
    SpriteRedTeam=ScriptedTexture'Botpack.Ammocount.FlakAmmoLED'
    SpriteBlueTeam=ScriptedTexture'Botpack.Ammocount.FlakAmmoLED'
    SpriteGreenTeam=ScriptedTexture'Botpack.Ammocount.FlakAmmoLED'
    SpriteYellowTeam=ScriptedTexture'Botpack.Ammocount.FlakAmmoLED'
    MFXrotX=(Pitch=0,Yaw=10000,Roll=0),
    AmbientGlow=0
    MultiSkins(0)=Texture'SiegeUltimateRC7UB01Media.GFX.sgContSpriteT0'
    MultiSkins(1)=Texture'SiegeUltimateRC7UB01Media.GFX.sgContSpriteT1'
    MultiSkins(2)=Texture'SiegeUltimateRC7UB01Media.GFX.sgContSpriteT2'
    MultiSkins(3)=Texture'SiegeUltimateRC7UB01Media.GFX.sgContSpriteT3'
    CollisionHeight=10.00
    LightBrightness=1
}
