2021-11-24 04:26:12 -05:00
|
|
|
// Copyright Epic Games, Inc. All Rights Reserved.
|
|
|
|
|
|
2022-05-02 09:15:14 -04:00
|
|
|
#include "Components/StateTreeComponent.h"
|
2021-11-24 04:26:12 -05:00
|
|
|
#include "GameFramework/Actor.h"
|
|
|
|
|
#include "GameplayTasksComponent.h"
|
|
|
|
|
#include "VisualLogger/VisualLogger.h"
|
|
|
|
|
#include "StateTree.h"
|
|
|
|
|
#include "StateTreeEvaluatorBase.h"
|
|
|
|
|
#include "Conditions/StateTreeCondition_Common.h"
|
|
|
|
|
#include "AIController.h"
|
|
|
|
|
#include "GameFramework/CharacterMovementComponent.h"
|
|
|
|
|
|
|
|
|
|
#define STATETREE_LOG(Verbosity, Format, ...) UE_VLOG(GetOwner(), LogStateTree, Verbosity, Format, ##__VA_ARGS__)
|
|
|
|
|
#define STATETREE_CLOG(Condition, Verbosity, Format, ...) UE_CVLOG((Condition), GetOwner(), LogStateTree, Verbosity, Format, ##__VA_ARGS__)
|
|
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////
|
2022-05-02 09:15:14 -04:00
|
|
|
// UStateTreeComponent
|
2021-11-24 04:26:12 -05:00
|
|
|
|
2022-05-02 09:15:14 -04:00
|
|
|
UStateTreeComponent::UStateTreeComponent(const FObjectInitializer& ObjectInitializer)
|
2021-11-24 04:26:12 -05:00
|
|
|
: Super(ObjectInitializer)
|
|
|
|
|
{
|
|
|
|
|
bWantsInitializeComponent = true;
|
|
|
|
|
bIsRunning = false;
|
|
|
|
|
bIsPaused = false;
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-02 09:15:14 -04:00
|
|
|
void UStateTreeComponent::InitializeComponent()
|
2021-11-24 04:26:12 -05:00
|
|
|
{
|
|
|
|
|
if (StateTree == nullptr)
|
|
|
|
|
{
|
|
|
|
|
STATETREE_LOG(Error, TEXT("%s: StateTree asset is not set, cannot initialize."), ANSI_TO_TCHAR(__FUNCTION__));
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!StateTreeContext.Init(*GetOwner(), *StateTree, EStateTreeStorage::Internal))
|
|
|
|
|
{
|
|
|
|
|
STATETREE_LOG(Error, TEXT("%s: Failed to init StateTreeContext."), ANSI_TO_TCHAR(__FUNCTION__));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-02 09:15:14 -04:00
|
|
|
void UStateTreeComponent::UninitializeComponent()
|
2021-11-24 04:26:12 -05:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-02 09:15:14 -04:00
|
|
|
bool UStateTreeComponent::SetContextRequirements()
|
2021-11-24 04:26:12 -05:00
|
|
|
{
|
|
|
|
|
if (!StateTreeContext.IsValid())
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2022-04-06 10:04:05 -04:00
|
|
|
|
|
|
|
|
const UWorld* World = GetWorld();
|
2021-11-24 04:26:12 -05:00
|
|
|
if (World == nullptr)
|
|
|
|
|
{
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (const FStateTreeExternalDataDesc& ItemDesc : StateTreeContext.GetExternalDataDescs())
|
|
|
|
|
{
|
|
|
|
|
if (ItemDesc.Struct != nullptr)
|
|
|
|
|
{
|
|
|
|
|
if (ItemDesc.Struct->IsChildOf(UWorldSubsystem::StaticClass()))
|
|
|
|
|
{
|
|
|
|
|
UWorldSubsystem* Subsystem = World->GetSubsystemBase(Cast<UClass>(const_cast<UStruct*>(ItemDesc.Struct)));
|
|
|
|
|
StateTreeContext.SetExternalData(ItemDesc.Handle, FStateTreeDataView(Subsystem));
|
|
|
|
|
}
|
|
|
|
|
else if (ItemDesc.Struct->IsChildOf(UActorComponent::StaticClass()))
|
|
|
|
|
{
|
|
|
|
|
UActorComponent* Component = GetOwner()->FindComponentByClass(Cast<UClass>(const_cast<UStruct*>(ItemDesc.Struct)));
|
|
|
|
|
StateTreeContext.SetExternalData(ItemDesc.Handle, FStateTreeDataView(Component));
|
|
|
|
|
}
|
|
|
|
|
else if (ItemDesc.Struct->IsChildOf(APawn::StaticClass()))
|
|
|
|
|
{
|
2022-04-06 10:04:05 -04:00
|
|
|
APawn* OwnerPawn = (AIOwner != nullptr) ? AIOwner->GetPawn() : Cast<APawn>(GetOwner());
|
|
|
|
|
StateTreeContext.SetExternalData(ItemDesc.Handle, FStateTreeDataView(OwnerPawn));
|
2021-11-24 04:26:12 -05:00
|
|
|
}
|
|
|
|
|
else if (ItemDesc.Struct->IsChildOf(AAIController::StaticClass()))
|
|
|
|
|
{
|
2022-04-06 10:04:05 -04:00
|
|
|
AAIController* OwnerController = (AIOwner != nullptr) ? AIOwner.Get() : Cast<AAIController>(GetOwner());
|
|
|
|
|
StateTreeContext.SetExternalData(ItemDesc.Handle, FStateTreeDataView(OwnerController));
|
|
|
|
|
}
|
|
|
|
|
else if (ItemDesc.Struct->IsChildOf(AActor::StaticClass()))
|
|
|
|
|
{
|
|
|
|
|
AActor* OwnerActor = (AIOwner != nullptr) ? AIOwner->GetPawn() : GetOwner();
|
|
|
|
|
StateTreeContext.SetExternalData(ItemDesc.Handle, FStateTreeDataView(OwnerActor));
|
2021-11-24 04:26:12 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return StateTreeContext.AreExternalDataViewsValid();
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-02 09:15:14 -04:00
|
|
|
void UStateTreeComponent::TickComponent(float DeltaTime, enum ELevelTick TickType, FActorComponentTickFunction *ThisTickFunction)
|
2021-11-24 04:26:12 -05:00
|
|
|
{
|
|
|
|
|
Super::TickComponent(DeltaTime, TickType, ThisTickFunction);
|
|
|
|
|
|
|
|
|
|
if (!bIsRunning || bIsPaused)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (SetContextRequirements())
|
|
|
|
|
{
|
|
|
|
|
StateTreeContext.Tick(DeltaTime);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-02 09:15:14 -04:00
|
|
|
void UStateTreeComponent::StartLogic()
|
2021-11-24 04:26:12 -05:00
|
|
|
{
|
|
|
|
|
STATETREE_LOG(Log, TEXT("%s: Start Logic"), ANSI_TO_TCHAR(__FUNCTION__));
|
|
|
|
|
|
|
|
|
|
if (SetContextRequirements())
|
|
|
|
|
{
|
|
|
|
|
StateTreeContext.Start();
|
|
|
|
|
bIsRunning = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-02 09:15:14 -04:00
|
|
|
void UStateTreeComponent::RestartLogic()
|
2021-11-24 04:26:12 -05:00
|
|
|
{
|
|
|
|
|
STATETREE_LOG(Log, TEXT("%s: Restart Logic"), ANSI_TO_TCHAR(__FUNCTION__));
|
|
|
|
|
|
|
|
|
|
if (SetContextRequirements())
|
|
|
|
|
{
|
|
|
|
|
StateTreeContext.Start();
|
|
|
|
|
bIsRunning = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-02 09:15:14 -04:00
|
|
|
void UStateTreeComponent::StopLogic(const FString& Reason)
|
2021-11-24 04:26:12 -05:00
|
|
|
{
|
|
|
|
|
STATETREE_LOG(Log, TEXT("%s: Stopping, reason: \'%s\'"), ANSI_TO_TCHAR(__FUNCTION__), *Reason);
|
|
|
|
|
|
|
|
|
|
if (SetContextRequirements())
|
|
|
|
|
{
|
|
|
|
|
StateTreeContext.Stop();
|
|
|
|
|
bIsRunning = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-02 09:15:14 -04:00
|
|
|
void UStateTreeComponent::Cleanup()
|
2021-11-24 04:26:12 -05:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-02 09:15:14 -04:00
|
|
|
void UStateTreeComponent::PauseLogic(const FString& Reason)
|
2021-11-24 04:26:12 -05:00
|
|
|
{
|
|
|
|
|
STATETREE_LOG(Log, TEXT("%s: Execution updates: PAUSED (%s)"), ANSI_TO_TCHAR(__FUNCTION__), *Reason);
|
|
|
|
|
bIsPaused = true;
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-02 09:15:14 -04:00
|
|
|
EAILogicResuming::Type UStateTreeComponent::ResumeLogic(const FString& Reason)
|
2021-11-24 04:26:12 -05:00
|
|
|
{
|
|
|
|
|
STATETREE_LOG(Log, TEXT("%s: Execution updates: RESUMED (%s)"), ANSI_TO_TCHAR(__FUNCTION__), *Reason);
|
|
|
|
|
|
|
|
|
|
const EAILogicResuming::Type SuperResumeResult = Super::ResumeLogic(Reason);
|
|
|
|
|
|
|
|
|
|
if (!!bIsPaused)
|
|
|
|
|
{
|
|
|
|
|
bIsPaused = false;
|
|
|
|
|
|
|
|
|
|
if (SuperResumeResult == EAILogicResuming::Continue)
|
|
|
|
|
{
|
|
|
|
|
// Nop
|
|
|
|
|
}
|
|
|
|
|
else if (SuperResumeResult == EAILogicResuming::RestartedInstead)
|
|
|
|
|
{
|
|
|
|
|
RestartLogic();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return SuperResumeResult;
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-02 09:15:14 -04:00
|
|
|
bool UStateTreeComponent::IsRunning() const
|
2021-11-24 04:26:12 -05:00
|
|
|
{
|
|
|
|
|
return bIsRunning;
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-02 09:15:14 -04:00
|
|
|
bool UStateTreeComponent::IsPaused() const
|
2021-11-24 04:26:12 -05:00
|
|
|
{
|
|
|
|
|
return bIsPaused;
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-02 09:15:14 -04:00
|
|
|
UGameplayTasksComponent* UStateTreeComponent::GetGameplayTasksComponent(const UGameplayTask& Task) const
|
2021-11-24 04:26:12 -05:00
|
|
|
{
|
|
|
|
|
const UAITask* AITask = Cast<const UAITask>(&Task);
|
|
|
|
|
return (AITask && AITask->GetAIController()) ? AITask->GetAIController()->GetGameplayTasksComponent(Task) : Task.GetGameplayTasksComponent();
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-02 09:15:14 -04:00
|
|
|
AActor* UStateTreeComponent::GetGameplayTaskOwner(const UGameplayTask* Task) const
|
2021-11-24 04:26:12 -05:00
|
|
|
{
|
|
|
|
|
if (Task == nullptr)
|
|
|
|
|
{
|
|
|
|
|
return GetAIOwner();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const UAITask* AITask = Cast<const UAITask>(Task);
|
|
|
|
|
if (AITask)
|
|
|
|
|
{
|
|
|
|
|
return AITask->GetAIController();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const UGameplayTasksComponent* TasksComponent = Task->GetGameplayTasksComponent();
|
|
|
|
|
return TasksComponent ? TasksComponent->GetGameplayTaskOwner(Task) : nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-02 09:15:14 -04:00
|
|
|
AActor* UStateTreeComponent::GetGameplayTaskAvatar(const UGameplayTask* Task) const
|
2021-11-24 04:26:12 -05:00
|
|
|
{
|
|
|
|
|
if (Task == nullptr)
|
|
|
|
|
{
|
|
|
|
|
return GetAIOwner() ? GetAIOwner()->GetPawn() : nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const UAITask* AITask = Cast<const UAITask>(Task);
|
|
|
|
|
if (AITask)
|
|
|
|
|
{
|
|
|
|
|
return AITask->GetAIController() ? AITask->GetAIController()->GetPawn() : nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const UGameplayTasksComponent* TasksComponent = Task->GetGameplayTasksComponent();
|
|
|
|
|
return TasksComponent ? TasksComponent->GetGameplayTaskAvatar(Task) : nullptr;
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-02 09:15:14 -04:00
|
|
|
uint8 UStateTreeComponent::GetGameplayTaskDefaultPriority() const
|
2021-11-24 04:26:12 -05:00
|
|
|
{
|
|
|
|
|
return static_cast<uint8>(EAITaskPriority::AutonomousAI);
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-02 09:15:14 -04:00
|
|
|
void UStateTreeComponent::OnGameplayTaskInitialized(UGameplayTask& Task)
|
2021-11-24 04:26:12 -05:00
|
|
|
{
|
|
|
|
|
const UAITask* AITask = Cast<const UAITask>(&Task);
|
|
|
|
|
if (AITask && (AITask->GetAIController() == nullptr))
|
|
|
|
|
{
|
|
|
|
|
// this means that the task has either been created without specifying
|
|
|
|
|
// UAITAsk::OwnerController's value (like via BP's Construct Object node)
|
|
|
|
|
// or it has been created in C++ with inappropriate function
|
|
|
|
|
UE_LOG(LogStateTree, Error, TEXT("Missing AIController in AITask %s"), *AITask->GetName());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#if WITH_GAMEPLAY_DEBUGGER
|
2022-05-02 09:15:14 -04:00
|
|
|
FString UStateTreeComponent::GetDebugInfoString() const
|
2021-11-24 04:26:12 -05:00
|
|
|
{
|
|
|
|
|
return StateTreeContext.GetDebugInfoString();
|
|
|
|
|
}
|
|
|
|
|
#endif // WITH_GAMEPLAY_DEBUGGER
|
|
|
|
|
|
|
|
|
|
#undef STATETREE_LOG
|
|
|
|
|
#undef STATETREE_CLOG
|