class InjectCombatThreatCommandTask extends AIbehaviortaskScript
{
	protected editable inlined var m_inCommand : AIArgumentMapping;
	protected var m_currentCommand : weak< AIInjectCombatThreatCommand >;
	protected var m_threatPersistenceSource : AIThreatPersistenceSource_Record;
	protected var m_activationTimeStamp : Float;
	protected var m_commandDuration : Float;
	protected var m_target : weak< GameObject >;
	protected var m_targetID : EntityID;

	protected override function Activate( context : ScriptExecutionContext )
	{
		SetUpdateInterval( context, RandomizeOffsetForUpdateInterval( 0.2 ) );
	}

	protected override function Update( context : ScriptExecutionContext ) : AIbehaviorUpdateOutcome
	{
		var rawCommand : IScriptable;
		var typedCommand : AIInjectCombatThreatCommand;
		var globalRef : GlobalNodeRef;
		var target : weak< GameObject >;
		var targetID : EntityID;
		var targetTrackerComponent : TargetTrackerComponent;
		var attitudeOwner : AttitudeAgent;
		var attitudeTarget : AttitudeAgent;
		rawCommand = ScriptExecutionContext.GetScriptableMappingValue( context, m_inCommand );
		typedCommand = ( ( AIInjectCombatThreatCommand )( rawCommand ) );
		if( m_currentCommand && !( typedCommand ) )
		{
			CancelCommand( context );
			return AIbehaviorUpdateOutcome.IN_PROGRESS;
		}
		if( typedCommand == m_currentCommand )
		{
			if( m_currentCommand )
			{
				if( !( ScriptedPuppet.IsActive( ScriptExecutionContext.GetOwner( context ) ) ) )
				{
					ScriptExecutionContext.DebugLog( context, 'InjectCombatThreatCommand', "Canceling command, owner is Dead, Defeated or Unconscious" );
					CancelCommand( context );
					if( typedCommand && typedCommand.state == AICommandState.Executing )
					{
						GetPuppet( context ).GetAIControllerComponent().StopExecutingCommand( typedCommand, true );
					}
				}
				else if( EntityID.IsDefined( m_targetID ) && !( m_target ) )
				{
					CancelCommand( context );
					ScriptExecutionContext.DebugLog( context, 'InjectCombatThreatCommand', "Canceling command, entity streamed out" );
					if( typedCommand && typedCommand.state == AICommandState.Executing )
					{
						GetPuppet( context ).GetAIControllerComponent().StopExecutingCommand( typedCommand, false );
					}
				}
				else if( ( m_commandDuration >= 0.0 ) && ( EngineTime.ToFloat( ScriptExecutionContext.GetAITime( context ) ) > ( m_activationTimeStamp + m_commandDuration ) ) )
				{
					CancelCommand( context );
					ScriptExecutionContext.DebugLog( context, 'InjectCombatThreatCommand', "Canceling command, duration expired" );
					if( typedCommand && typedCommand.state == AICommandState.Executing )
					{
						GetPuppet( context ).GetAIControllerComponent().StopExecutingCommand( typedCommand, true );
					}
				}
			}
			return AIbehaviorUpdateOutcome.IN_PROGRESS;
		}
		m_currentCommand = typedCommand;
		m_commandDuration = typedCommand.duration;
		if( typedCommand.duration > 0.0 )
		{
			m_commandDuration = 0.0;
		}
		m_activationTimeStamp = EngineTime.ToFloat( ScriptExecutionContext.GetAITime( context ) );
		if( !( GetGameObjectFromEntityReference( typedCommand.targetPuppetRef, ScriptExecutionContext.GetOwner( context ).GetGame(), target ) ) )
		{
			globalRef = ResolveNodeRef( typedCommand.targetNodeRef, GlobalNodeID.GetRoot() );
			targetID = ( ( EntityID )( globalRef ) );
			target = ( ( GameObject )( GameInstance.FindEntityByID( GetGame( context ), targetID ) ) );
		}
		m_target = target;
		m_targetID = target.GetEntityID();
		m_threatPersistenceSource = TweakDBInterface.GetAIThreatPersistenceSourceRecord( T"AIThreatPersistenceSource.CommandInjectThreat" );
		if( EntityID.IsDefined( m_targetID ) && !( m_target ) )
		{
			CancelCommand( context );
			ScriptExecutionContext.DebugLog( context, 'InjectCombatThreatCommand', "Canceling command, entity streamed out" );
			if( typedCommand && typedCommand.state == AICommandState.Executing )
			{
				GetPuppet( context ).GetAIControllerComponent().StopExecutingCommand( typedCommand, false );
			}
		}
		else if( target && ( target != ScriptExecutionContext.GetOwner( context ) ) )
		{
			targetTrackerComponent = ScriptExecutionContext.GetOwner( context ).GetTargetTrackerComponent();
			if( ( targetTrackerComponent && target.IsPuppet() ) && ( target != ScriptExecutionContext.GetOwner( context ) ) )
			{
				attitudeOwner = ScriptExecutionContext.GetOwner( context ).GetAttitudeAgent();
				attitudeTarget = target.GetAttitudeAgent();
				if( !( typedCommand.dontForceHostileAttitude ) )
				{
					if( attitudeOwner && attitudeTarget )
					{
						attitudeOwner.SetAttitudeTowardsAgentGroup( attitudeTarget, attitudeOwner, EAIAttitude.AIA_Hostile );
					}
				}
				if( ( attitudeOwner && attitudeTarget ) && attitudeOwner.GetAttitudeTowards( attitudeTarget ) == EAIAttitude.AIA_Hostile )
				{
					targetTrackerComponent.AddThreat( target, true, target.GetWorldPosition(), 1.0, m_commandDuration, typedCommand.isPersistent );
					if( m_commandDuration < 0.0 )
					{
						targetTrackerComponent.SetThreatPersistence( target, true, ( ( Uint32 )( m_threatPersistenceSource.EnumValue() ) ) );
					}
				}
				else
				{
					targetTrackerComponent.AddThreat( target, false, target.GetWorldPosition(), 1.0, m_commandDuration, typedCommand.isPersistent );
					if( m_commandDuration < 0.0 )
					{
						targetTrackerComponent.SetThreatPersistence( target, true, ( ( Uint32 )( m_threatPersistenceSource.EnumValue() ) ) );
					}
				}
			}
		}
		else
		{
			CancelCommand( context );
			ScriptExecutionContext.DebugLog( context, 'InjectCombatThreatCommand', "No target or targetting self" );
			if( typedCommand && typedCommand.state == AICommandState.Executing )
			{
				GetPuppet( context ).GetAIControllerComponent().StopExecutingCommand( typedCommand, true );
			}
		}
		return AIbehaviorUpdateOutcome.IN_PROGRESS;
	}

	private override final function Deactivate( context : ScriptExecutionContext )
	{
		CancelCommand( context );
	}

	protected final function CancelCommand( context : ScriptExecutionContext )
	{
		if( m_commandDuration < 0.0 )
		{
			TargetTrackingExtension.SetThreatPersistence( ( ( ScriptedPuppet )( ScriptExecutionContext.GetOwner( context ) ) ), m_target, false, ( ( Uint32 )( m_threatPersistenceSource.EnumValue() ) ) );
		}
		ScriptExecutionContext.SetMappingValue( context, m_inCommand, NULL );
		m_activationTimeStamp = 0.0;
		m_commandDuration = 0.0;
		m_currentCommand = NULL;
		m_target = NULL;
		m_targetID = m_target.GetEntityID();
	}

}

