//=============================================================================
// FortStandard.
//=============================================================================
class FortStandard expands StationaryPawn;

#exec AUDIO IMPORT FILE="Sounds\Domination\teleprt28.WAV" NAME="ControlSound" GROUP="Domination"
#exec AUDIO IMPORT FILE="Sounds\Domination\uLNitro1.WAV" NAME="WarningSound" GROUP="Domination"

var() bool bSelfDisplayed;
var() bool bTriggerOnly;
var() byte DefensePriority;
var() float ScorePerSecond; 
var Pawn Defender;
var float LastHelpMessage;
var() name NearestPathNodeTag;
var NavigationPoint NearestPath;
  
function PostBeginPlay()
{
	local NavigationPoint N;

	if ( !Level.Game.IsA('Assault') )
	{
		Destroy();
		return;
	}

	if ( NearestPathNodeTag != '' )
		for ( N=Level.NavigationPointList; N!=None; N=N.NextNavigationPoint )
			if ( N.Tag == NearestPathNodeTag )
			{
				NearestPath = N;
				break;
			}

	Super.PostBeginPlay();
	bHidden = !bSelfDisplayed;
	LoopAnim('Wave', 0.5);


}

function Destroyed()
{
	Super.Destroyed();
	log(self$" destroyed");
	if ( Level.Game.IsA('Assault') )
		Assault(Level.Game).RemoveFort(self, instigator);
}

function Touch( Actor Other )
{
	if ( bTriggerOnly && Other.IsA('Pawn') && Pawn(Other).bIsPlayer )
		DestroyFort(Pawn(Other));
}

function Trigger( actor Other, pawn EventInstigator )
{
	// TakeDamage(Health+10, EventInstigator, Location. vect(0,0,0);, ''); //FIXME - TIM causes crash
	DestroyFort(EventInstigator);
}

function DestroyFort(pawn InstigatedBy)
{
	local Actor A;

	SetTimer(0.0, false);
	Health = Default.Health;
	AmbientSound = None;
	if ( Event != '' )
		ForEach AllActors(class'Actor', A, Event)
			A.Trigger( self, instigatedBy );
	instigator = instigatedBy;
	Destroy();
}

function TakeDamage( int Damage, Pawn instigatedBy, Vector hitlocation, 
						Vector momentum, name damageType)
{
	local Pawn P;

	if ( bTriggerOnly || (instigatedBy == None)
		 || (instigatedBy.bIsPlayer && (instigatedBy.PlayerReplicationInfo.Team == Assault(Level.Game).Defender.TeamIndex)) )
		return;
	Health -= Damage;
	if ( (Defender != None) && (Defender.Health > 0)
		&& (Level.TimeSeconds - LastHelpMessage > 15)
		&& (VSize(Location - Defender.Location) < 1000)
		&& Defender.LineOfSightTo(self) )
	{
		if ( Defender.IsA('Bot') )
			Bot(Defender).SendTeamMessage(None, 'OTHER', 13, 15);
		LastHelpMessage = Level.TimeSeconds;
	}

	for ( P=Level.PawnList; P!=None; P=P.NextPawn )
        if ( P.IsA('Bot') && (P.PlayerReplicationInfo.Team == PlayerReplicationInfo.Team) && (Bot(P).Orders == 'FreeLance') )
		{
			//FIXME p should say this fort is under attack
			Bot(P).HandleHelpMessageFrom(self);
		}

	// log("Fort health now "$health$" after damage by "$instigatedBy$" ("$instigatedBy.PlayerReplicationInfo.team$")");
	if ( Health < 0 )
		DestroyFort(instigatedBy);
	else
	{
		AmbientSound = sound'WarningSound'; 
		if ( (TimerRate == 0) || (TimerRate - TimerCounter > 0.01 * Health) )
			SetTimer(FClamp(0.006 * Health,0.2,1.5), true);
	}
}

function Timer()
{
	if ( LightType == LT_Steady )
		LightType = LT_None;
	else
	{
		LightType = LT_Steady;
		PlaySound(sound'ControlSound');
	}
}

defaultproperties
{
	Health=200
    ScorePerSecond=0.100000
    Mesh=Flag1M
	Skin=JFlag11
    SoundRadius=255
    SoundVolume=255
	 LightEffect=LE_NonIncidence
	 LightBrightness=255
	 LightHue=170
	 LightRadius=12
	 LightSaturation=96
	 TransientSoundVolume=+00002.000000
	bSelfDisplayed=true
	bUnlit=true
	bCollideActors=true
	bCollideWorld=true
	bBlockActors=false
	bBlockPlayers=false
	bProjTarget=true

}
