//=============================================================================
// sxMotionAlarm.
// Written By WILDCARD
//=============================================================================
class sxMotionAlarm expands sgBuilding;

var bool bOwnsCrossHair;
var sgHUD AlarHud;
var() texture TextureRed;
var() texture TextureBlue;
var String AlarmLocation;
var() int DetectionRange;
var int ShortestRange;
var bool StopSpamming;
var bool EnemyIsStillPresent;

function AnnounceTeam(string sMessage, int iTeam)
{
    local Pawn p;

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

simulated event PostBeginPlay()
{
	local Pawn p;

	ShortestRange = DetectionRange;
	StopSpamming = false;
	EnemyIsStillPresent = false;
	
	
	if( Pawn(owner) != None && Pawn(owner).PlayerReplicationInfo != None )
	{
	 if ( Pawn(owner).PlayerReplicationInfo.PlayerLocation != None )
	 {
	  AlarmLocation = PlayerReplicationInfo.PlayerLocation.LocationName;
	 }
	 else
	 {
	  if ( Pawn(owner).PlayerReplicationInfo.PlayerZone != None )
	  {
	   AlarmLocation = Pawn(owner).PlayerReplicationInfo.PlayerZone.ZoneName;
	  }
	  if ( AlarmLocation != "" && AlarmLocation != " ")
	  {
	   AlarmLocation = "at the "@AlarmLocation;
	  }
	 }
	}
	Super.PostBeginPlay();

}

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

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

			foreach RadiusActors(class'Pawn', p,DetectionRange)
				if ( p.bIsPlayer && p.Health > 0 &&
					p.PlayerReplicationInfo != None &&
					p.PlayerReplicationInfo.Team != Team && !p.PlayerReplicationInfo.bIsSpectator)
						{
							if (p != None) // if an enemy is present.
								{
									EnemyIsStillPresent = true;
									if ( StopSpamming == false )
										{
											StopSpamming = true;
											TeamAlarm();
										}
								}

						}

			if ( p == none )
				{
					if ( EnemyIsStillPresent == false )
						{
							StopSpamming = false;
							AlertLight(false);
						}
				}

	EnemyIsStillPresent = false ;
}

function TeamAlarm()
{

local pawn p;

if (bDisabledByEMP){return;}

	AnnounceTeam("ENEMY INTRUSION DETECTED!!"@AlarmLocation, Team);
	AlertLight(true);
	Self.PlaySound(Sound'IntrusionAlarm',, 4.0);
	for ( p = Level.PawnList; p != None; p = p.nextPawn )
		if ( p.IsA('TournamentPlayer' ) && p.PlayerReplicationInfo.Team == Team && !p.PlayerReplicationInfo.bIsSpectator )
	        TournamentPlayer(P).ReceiveLocalizedMessage(Class'TeamMotionAlarmAlert');
}

function AlertLight(bool on)
{
	if ( on == true && !bDisabledByEMP)
		{
			LightEffect=LE_SearchLight;
			LightBrightness=255;
			LightPeriod=1;

			if ( Team == 0 )
				LightHue=0;
			else
				LightHue=170;

			LightRadius=32;
			LightSaturation=0;
			LightType=LT_Steady;
		}
    else
		LightType=LT_None;
}

function Upgraded()
{
    if ( SiegeGI(Level.Game) != None )
		DetectionRange = (ShortestRange*(1+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;

	AnnounceTeam("Your team has setup a motion alarm "@AlarmLocation, Team);

    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.RotationRate.Pitch = MFXrotX.Pitch*FRand();
            myFX.RotationRate.Roll = MFXrotX.Roll*FRand();
            myFX.RotationRate.Yaw = MFXrotX.Yaw*FRand();

        }
}

defaultproperties
{
    TextureRed=Texture'SiegeUltimateRC7UB01Media.U.BuildingMaps.MotionAlarmSkinT0'
    TextureBlue=Texture'SiegeUltimateRC7UB01Media.U.BuildingMaps.MotionAlarmSkinT1'
    DetectionRange=64
    BuildingName="Motion Alarm"
    BuildCost=400
    UpgradeCost=30
    BuildTime=20.00
    MaxEnergy=8000.00
    Energy=8000.00
    SpriteScale=1.00
    Model=LodMesh'UnrealShare.UrnM'
    SpriteRedTeam=Texture'SiegeUltimateRC7UB01Media.U.BuildingMaps.MotionAlarmSpriteT0'
    SpriteBlueTeam=Texture'SiegeUltimateRC7UB01Media.U.BuildingMaps.MotionAlarmSpriteT1'
    SpriteGreenTeam=Texture'SiegeUltimateRC7UB01Media.U.BuildingMaps.MotionAlarmSpriteT2'
    SpriteYellowTeam=Texture'SiegeUltimateRC7UB01Media.U.BuildingMaps.MotionAlarmSpriteT3'
    DSofMFX=2.20
    NumOfMFX=3
    MFXrotX=(Pitch=9000,Yaw=9000,Roll=9000),
    Fatness=183
    MultiSkins(0)=Texture'SiegeUltimateRC7UB01Media.U.BuildingMaps.MotionAlarmSpriteT0'
    MultiSkins(1)=Texture'SiegeUltimateRC7UB01Media.U.BuildingMaps.MotionAlarmSpriteT1'
    MultiSkins(2)=Texture'SiegeUltimateRC7UB01Media.GFX.sgContSpriteT2'
    MultiSkins(3)=Texture'SiegeUltimateRC7UB01Media.GFX.sgContSpriteT3'
    CollisionRadius=32.00
    CollisionHeight=32.00
}
