void UAblRayCastQueryTask::OnTaskTick(const TWeakObjectPtr<const UAblAbilityContext>& Context, float deltaTime) const
{
	Super::OnTaskTick(Context, deltaTime);

	if (IsAsyncFriendly() && UAbleSettings::IsAsyncEnabled())
	{
		UAblRayCastQueryTaskScratchPad* ScratchPad = Cast<UAblRayCastQueryTaskScratchPad>(Context->GetScratchPadForTask(this));
		check(ScratchPad);
		if (!ScratchPad->AsyncProcessed && ScratchPad->AsyncHandle._Handle != 0)
		{
			AActor* SourceActor = m_QueryLocation.GetSourceActor(*Context.Get());
			check(SourceActor);

			UWorld* World = SourceActor->GetWorld();

			FTraceDatum Datum;
			if (World->QueryTraceData(ScratchPad->AsyncHandle, Datum))
			{
				if (m_CopyResultsToContext)
				{
					CopyResultsToContext(Datum.OutHits, Context);
				}

				if (m_FireEvent)
				{
					Context->GetAbility()->OnRaycastEvent(Context.Get(), m_Name, Datum.OutHits);
				}

				ScratchPad->AsyncProcessed = true;
			}

		}
	}
}
void UAblAbilityTaskDependencyValidator::Validate(const TWeakObjectPtr<UAblAbilityTaskValidatorContext>& Context, const TWeakObjectPtr<const UAblAbilityTask>& Task) const
{
	const TArray<UAblAbilityTask*>& AllTasks = Context->GetAbility()->GetTasks();
	for (const UAblAbilityTask* CurrentTask : AllTasks)
	{
		HasCircularDependency(Context, CurrentTask, CurrentTask);
	}
}
void UAblAbilityTaskOrderValidator::Validate(const TWeakObjectPtr<UAblAbilityTaskValidatorContext>& Context, const TWeakObjectPtr<const UAblAbilityTask>& Task) const
{
	const float AbilityLength = Context->GetAbility()->GetLength();
	const TArray<UAblAbilityTask*>& AllTasks = Context->GetAbility()->GetTasks();
	int j = 0;
	float TimeDifference = 0.0f;
	for (int i = 0; i < AllTasks.Num() - 1; ++i)
	{
		for (j = i + 1; j < AllTasks.Num(); ++j)
		{
			TimeDifference = AllTasks[j]->GetStartTime() - AllTasks[i]->GetStartTime();
			// The 0.0 check is just there to catch any constants people may be using.
			if (TimeDifference != 0.0f && TimeDifference < KINDA_SMALL_NUMBER)
			{
				Context->AddError(LOCTEXT("AbilityValidatorTasksOutOfOrder", "Tasks aren't in sequential order. Please resave the ability."));
				return; // We can early out.
			}
		}
	}
}
void UAblAbilityLengthValidator::Validate(const TWeakObjectPtr<UAblAbilityTaskValidatorContext>& Context, const TWeakObjectPtr<const UAblAbilityTask>& Task) const
{
	const float AbilityLength = Context->GetAbility()->GetLength();
	const TArray<UAblAbilityTask*>& AllTasks = Context->GetAbility()->GetTasks();

	float LastTaskEndTime = 0.0f;
	for (const UAblAbilityTask* AbilityTask : AllTasks)
	{
		if (AbilityTask->GetEndTime() > AbilityLength)
		{
			FText WarningMessage = FText::Format(LOCTEXT("AbilityValidatorTaskTooLong", "Task {0} extends past the length of the ability. Is this intended?"), AbilityTask->GetTaskName());
			Context->AddWarning(WarningMessage);
		}

		LastTaskEndTime = FMath::Max(AbilityTask->GetEndTime(), LastTaskEndTime);
	}

	if (AbilityLength - LastTaskEndTime > KINDA_SMALL_NUMBER)
	{
		FText WarningMessage = FText::FormatOrdered(LOCTEXT("AbilityValidatorEmptySpace", "There is empty space in the timeline. The last task finishes at {0} but the ability has a length of {1}. Is this intended?"), LastTaskEndTime, AbilityLength);
		Context->AddWarning(WarningMessage);
	}
}
void UAblSetShaderParameterTask::OnTaskStart(const TWeakObjectPtr<const UAblAbilityContext>& Context) const
{
	Super::OnTaskStart(Context);

	if (!m_Value)
	{
		UE_LOG(LogAble, Warning, TEXT("No Value set for SetShaderParameter in Ability [%s]"), *Context->GetAbility()->GetDisplayName());
		return;
	}

	// We need to convert our Actors to primitive components.
	TArray<TWeakObjectPtr<AActor>> TargetArray;
	GetActorsForTask(Context, TargetArray);

	TArray<TWeakObjectPtr<UPrimitiveComponent>> PrimitiveComponents;

	for (TWeakObjectPtr<AActor>& Target : TargetArray)
	{
		for (UActorComponent* Component : Target->GetComponentsByClass(UPrimitiveComponent::StaticClass()))
		{
			PrimitiveComponents.Add(Cast<UPrimitiveComponent>(Component));
		}
	}

	UAblSetShaderParameterTaskScratchPad* ScratchPad = Cast<UAblSetShaderParameterTaskScratchPad>(Context->GetScratchPadForTask(this));
	check(ScratchPad);

	ScratchPad->BlendIn = m_BlendIn;
	ScratchPad->BlendIn.Reset();

	ScratchPad->BlendOut = m_BlendOut;
	ScratchPad->BlendOut.Reset();

	UAblSetParameterValue* CachedValue = nullptr;
	for (TWeakObjectPtr<UPrimitiveComponent>& PrimitiveComponent : PrimitiveComponents)
	{
		if (PrimitiveComponent.IsValid())
		{
			for (int32 i = 0; i < PrimitiveComponent->GetNumMaterials(); ++i)
			{
				UMaterialInterface* MaterialInterface = PrimitiveComponent->GetMaterial(i);
				UMaterialInstanceDynamic* DynamicMaterial = Cast<UMaterialInstanceDynamic>(MaterialInterface);

				// If our material currently isn't dynamic, but we have the parameter we're looking for - instantiate a dynamic version.
				if (!DynamicMaterial && CheckMaterialHasParameter(MaterialInterface))
				{
					DynamicMaterial = PrimitiveComponent->CreateDynamicMaterialInstance(i, MaterialInterface);
				}

				if (DynamicMaterial)
				{
					CachedValue = CacheShaderValue(ScratchPad, DynamicMaterial);
					if (CachedValue)
					{
						ScratchPad->DynamicMaterials.Add(DynamicMaterial);
						ScratchPad->PreviousValues.Add(CachedValue);

						if (ScratchPad->BlendIn.IsComplete())
						{
							// If there isn't any blend. Just set it here since we won't be ticking.
							InternalSetShaderValue(DynamicMaterial, m_Value, CachedValue, ScratchPad->BlendIn.GetAlpha());
						}
					}
				}
			}
		}
	}

}
void UAblRayCastQueryTask::OnTaskStart(const TWeakObjectPtr<const UAblAbilityContext>& Context) const
{
	Super::OnTaskStart(Context);

	AActor* SourceActor = m_QueryLocation.GetSourceActor(*Context.Get());
	check(SourceActor);

	UWorld* World = SourceActor->GetWorld();

	FTransform QueryTransform;
	m_QueryLocation.GetTransform(*Context.Get(), QueryTransform);

	const FVector RayStart = QueryTransform.GetLocation();
	const FVector RayEnd = RayStart + QueryTransform.GetRotation().GetForwardVector() * m_Length;

	if (m_UseAsyncQuery && UAbleSettings::IsAsyncEnabled())
	{
		UAblRayCastQueryTaskScratchPad* ScratchPad = Cast<UAblRayCastQueryTaskScratchPad>(Context->GetScratchPadForTask(this));
		check(ScratchPad);
		if (m_OnlyReturnBlockingHit)
		{
			ScratchPad->AsyncHandle = World->AsyncLineTraceByChannel(EAsyncTraceType::Single, RayStart, RayEnd, m_CollisionChannel);
		}
		else
		{
			ScratchPad->AsyncHandle = World->AsyncLineTraceByChannel(EAsyncTraceType::Multi, RayStart, RayEnd, m_CollisionChannel);
		}
	}
	else
	{
		TArray<FHitResult> HitResults;
		FHitResult TraceResult;
		if (m_OnlyReturnBlockingHit)
		{
			if (World->LineTraceSingleByChannel(TraceResult, RayStart, RayEnd, m_CollisionChannel))
			{
				HitResults.Add(TraceResult);
			}
		}
		else
		{
			World->LineTraceMultiByChannel(HitResults, RayStart, RayEnd, m_CollisionChannel);		
		}

#if !(UE_BUILD_SHIPPING)
		if (IsVerbose())
		{
			PrintVerbose(FString::Printf(TEXT("Raycast found %d results."), HitResults.Num()));
		}
#endif

		if (HitResults.Num())
		{
#if !(UE_BUILD_SHIPPING)
			if (IsVerbose())
			{
				// Quick distance print help to see if we hit ourselves.
				float DistanceToBlocker = HitResults[HitResults.Num() - 1].Distance;
				PrintVerbose(FString::Printf(TEXT("Raycast blocking hit distance: %4.2f."), DistanceToBlocker));
			}
#endif

			if (m_CopyResultsToContext)
			{
#if !(UE_BUILD_SHIPPING)
				if (IsVerbose())
				{
					PrintVerbose(FString::Printf(TEXT("Copying %d results into Context."), HitResults.Num()));
				}
#endif
				CopyResultsToContext(HitResults, Context);
			}

			if (m_FireEvent)
			{
#if !(UE_BUILD_SHIPPING)
				if (IsVerbose())
				{
					PrintVerbose(FString::Printf(TEXT("Firing Raycast Event %s with %d results."), *m_Name.ToString(), HitResults.Num()));
				}
#endif
				Context->GetAbility()->OnRaycastEvent(Context.Get(), m_Name, HitResults);
			}
		}
	}

#if !UE_BUILD_SHIPPING
	if (FAblAbilityDebug::ShouldDrawQueries())
	{
		FAblAbilityDebug::DrawRaycastQuery(World, QueryTransform, m_Length);
	}
#endif
}