class FollowVehicleTask extends AIbehaviortaskScript
{
}

class GetFollowTarget extends FollowVehicleTask
{
	private var m_blackboard : IBlackboard;
	private var m_vehicle : weak< VehicleObject >;

	protected override function Activate( context : ScriptExecutionContext )
	{
		var slotRequest : RequestSlotEvent;
		VehicleComponent.GetVehicle( GetGame( context ), GameInstance.GetPlayerSystem( GetGame( context ) ).GetLocalPlayerMainGameObject(), m_vehicle );
		m_blackboard = IBlackboard.Create( GetAllBlackboardDefs().AIFollowSlot );
		ScriptExecutionContext.SetArgumentVector( context, 'MovementTarget', m_vehicle.GetWorldPosition() );
		slotRequest = new RequestSlotEvent;
		slotRequest.blackboard = m_blackboard;
		slotRequest.requester = ScriptExecutionContext.GetOwner( context );
		m_vehicle.QueueEvent( slotRequest );
		SetUpdateInterval( context, 0.1 );
	}

	protected override function Update( context : ScriptExecutionContext ) : AIbehaviorUpdateOutcome
	{
		var followSlot : Transform;
		var followPointWorldOffset : Vector4;
		var target : Vector4;
		var changeDestinationEvent : gameChangeDestination;
		var actionEvent : ActionEvent;
		followSlot = ( ( Transform )( m_blackboard.GetVariant( GetAllBlackboardDefs().AIFollowSlot.slotTransform ) ) );
		if( !( Vector4.IsZero( Transform.GetPosition( followSlot ) ) ) )
		{
			followPointWorldOffset = Vector4.RotByAngleXY( Transform.GetPosition( followSlot ), -1.0 * Vector4.Heading( m_vehicle.GetWorldForward() ) );
			target = m_vehicle.GetWorldPosition() + followPointWorldOffset;
			changeDestinationEvent = new gameChangeDestination;
			changeDestinationEvent.destination = target;
			actionEvent = new ActionEvent;
			actionEvent.name = 'actionEvent';
			actionEvent.internalEvent = ( ( ActionInternalEvent )( changeDestinationEvent ) );
			ScriptExecutionContext.GetOwner( context ).QueueEvent( actionEvent );
			ScriptExecutionContext.SetArgumentVector( context, 'MovementTarget', target );
			return AIbehaviorUpdateOutcome.SUCCESS;
		}
		return AIbehaviorUpdateOutcome.IN_PROGRESS;
	}

	protected override function Deactivate( context : ScriptExecutionContext )
	{
		var slotReleaseRequest : ReleaseSlotEvent;
		slotReleaseRequest = new ReleaseSlotEvent;
		slotReleaseRequest.slotID = m_blackboard.GetInt( GetAllBlackboardDefs().AIFollowSlot.slotID );
		m_vehicle.QueueEvent( slotReleaseRequest );
	}

}

class CheckFollowTarget extends AIbehaviorconditionScript
{

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

	protected override function Check( context : ScriptExecutionContext ) : AIbehaviorConditionOutcomes
	{
		var nodeRef : Vector4;
		nodeRef = ScriptExecutionContext.GetArgumentVector( context, 'MovementTarget' );
		if( !( Vector4.IsZero( nodeRef ) ) )
		{
			return true;
		}
		return false;
	}

}

class CheckTargetInVehicle extends AIbehaviorconditionScript
{

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

	protected override function Check( context : ScriptExecutionContext ) : AIbehaviorConditionOutcomes
	{
		var combatTarget : GameObject;
		combatTarget = ScriptExecutionContext.GetArgumentObject( context, 'CombatTarget' );
		if( !( combatTarget ) || !( VehicleComponent.IsDriver( combatTarget.GetGame(), combatTarget ) ) )
		{
			return false;
		}
		return true;
	}

}

