class InjectLookatTargetCommandTask extends AIbehaviortaskScript
{
	protected editable inlined var m_inCommand : AIArgumentMapping;
	protected var m_currentCommand : weak< AIInjectLookatTargetCommand >;
	protected var m_activationTimeStamp : Float;
	protected var m_commandDuration : Float;

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

	protected override function Update( context : ScriptExecutionContext ) : AIbehaviorUpdateOutcome
	{
		var rawCommand : IScriptable;
		var typedCommand : AIInjectLookatTargetCommand;
		var globalRef : GlobalNodeRef;
		var target : weak< GameObject >;
		rawCommand = ScriptExecutionContext.GetScriptableMappingValue( context, m_inCommand );
		typedCommand = ( ( AIInjectLookatTargetCommand )( rawCommand ) );
		if( m_currentCommand && !( typedCommand ) )
		{
			CancelCommand( context );
			return AIbehaviorUpdateOutcome.IN_PROGRESS;
		}
		if( typedCommand == m_currentCommand )
		{
			if( ( m_commandDuration > 0.0 ) && ( EngineTime.ToFloat( ScriptExecutionContext.GetAITime( context ) ) > ( m_activationTimeStamp + m_commandDuration ) ) )
			{
				CancelCommand( context );
				if( typedCommand && typedCommand.state == AICommandState.Executing )
				{
					GetPuppet( context ).GetAIControllerComponent().StopExecutingCommand( typedCommand, true );
				}
			}
			return AIbehaviorUpdateOutcome.IN_PROGRESS;
		}
		m_currentCommand = typedCommand;
		m_commandDuration = typedCommand.duration;
		m_activationTimeStamp = EngineTime.ToFloat( ScriptExecutionContext.GetAITime( context ) );
		if( !( GetGameObjectFromEntityReference( typedCommand.targetPuppetRef, ScriptExecutionContext.GetOwner( context ).GetGame(), target ) ) )
		{
			globalRef = ResolveNodeRef( typedCommand.targetNodeRef, GlobalNodeID.GetRoot() );
			target = ( ( GameObject )( GameInstance.FindEntityByID( GetGame( context ), ( ( EntityID )( globalRef ) ) ) ) );
		}
		if( !( target ) )
		{
			CancelCommand( context );
			if( typedCommand && typedCommand.state == AICommandState.Executing )
			{
				GetPuppet( context ).GetAIControllerComponent().StopExecutingCommand( typedCommand, true );
			}
		}
		else
		{
			ScriptExecutionContext.SetArgumentObject( context, 'CommandAimTarget', target );
		}
		return AIbehaviorUpdateOutcome.IN_PROGRESS;
	}

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

	protected final function CancelCommand( context : ScriptExecutionContext )
	{
		ScriptExecutionContext.SetArgumentObject( context, 'CommandAimTarget', NULL );
		ScriptExecutionContext.SetMappingValue( context, m_inCommand, NULL );
		m_activationTimeStamp = 0.0;
		m_commandDuration = 0.0;
		m_currentCommand = NULL;
	}

}

