//------------------------------------------------------------------------------
// Purpose:
//------------------------------------------------------------------------------
bool CAI_ASW_RangedAttackBehavior::FindFiringLocation( )
{
    CBaseEntity *pBestEnt = NULL;
    float flBestDistSq = FLT_MAX;
    AIEnemiesIter_t iter;
    CBaseEntity *pEnemy;

    pEnemy = GetEnemy();

    if ( pEnemy && pEnemy->IsAlive() && GetOuter()->FVisible( pEnemy ) )
    {
        m_hTarget = pEnemy;
        UpdateTargetLocation();
        return true;
    }

    for( AI_EnemyInfo_t *pEMemory = GetEnemies()->GetFirst( &iter ); pEMemory != NULL; pEMemory = GetEnemies()->GetNext( &iter ) )
    {
        pEnemy = pEMemory->hEnemy;
        if ( !pEnemy || !pEnemy->IsAlive() || !GetOuter()->FVisible( pEnemy ) )
        {
            continue;
        }

        Vector vDelta = GetAbsOrigin() - pEnemy->GetAbsOrigin();

        float flLenSq = vDelta.LengthSqr();
        if ( flLenSq <= flBestDistSq )
        {
            pBestEnt = pEnemy;
            flBestDistSq = flLenSq;
        }
    }

    if ( !pBestEnt )
    {
        // just try shooting at our enemy for now
        if ( pEnemy && pEnemy->IsAlive() )
        {
            UpdateTargetLocation();
            return true;
        }
        return false;
    }

    m_hTarget = pBestEnt;
    GetOuter()->SetEnemy( pBestEnt );
    UpdateTargetLocation();
    return true;
}
void UAbilityTask_ApplyRootMotionMoveToActorForce::TickTask(float DeltaTime)
{
	if (bIsFinished)
	{
		return;
	}

	Super::TickTask(DeltaTime);

	AActor* MyActor = GetAvatarActor();
	if (MyActor)
	{
		float CurrentTime = GetWorld()->GetTimeSeconds();
		const bool bTimedOut = CurrentTime >= EndTime;

		// Update target location
		{
			const FVector PreviousTargetLocation = TargetLocation;
			if (UpdateTargetLocation(DeltaTime))
			{
				SetRootMotionTargetLocation(TargetLocation);
			}
			else
			{
				// TargetLocation not updated - TargetActor not around anymore, continue on to last set TargetLocation
			}
		}

#if !(UE_BUILD_SHIPPING || UE_BUILD_TEST)
		// Draw debug
		if (DebugMoveToActorForce > 0)
		{
			DrawDebugSphere(GetWorld(), TargetLocation, 50.f, 10, FColor::Green, false, 15.f);
		}
#endif // !(UE_BUILD_SHIPPING || UE_BUILD_TEST)

		const float ReachedDestinationDistanceSqr = 50.f * 50.f;
		const bool bReachedDestination = FVector::DistSquared(TargetLocation, MyActor->GetActorLocation()) < ReachedDestinationDistanceSqr;

		if (bTimedOut || (bReachedDestination && !bDisableDestinationReachedInterrupt))
		{
			// Task has finished
			bIsFinished = true;
			if (!bIsSimulating)
			{
				MyActor->ForceNetUpdate();
				OnFinished.Broadcast(bReachedDestination, bTimedOut, TargetLocation);
				EndTask();
			}
		}
	}
	else
	{
		bIsFinished = true;
		EndTask();
	}
}