Files
UnrealEngineUWP/Engine/Source/Runtime/AIModule/Private/BehaviorTree/BTAuxiliaryNode.cpp
Marc Audy 11f5b21210 Merging //UE5/Release-Engine-Staging @ 13752110 to Main (//UE5/Main)
#rnx

[CL 13753156 by Marc Audy in ue5-main branch]
2020-06-23 18:40:00 -04:00

170 lines
4.9 KiB
C++

// Copyright Epic Games, Inc. All Rights Reserved.
#include "BehaviorTree/BTAuxiliaryNode.h"
#include "BehaviorTree/BTCompositeNode.h"
UBTAuxiliaryNode::UBTAuxiliaryNode(const FObjectInitializer& ObjectInitializer) : Super(ObjectInitializer)
{
bNotifyBecomeRelevant = false;
bNotifyCeaseRelevant = false;
bNotifyTick = false;
bTickIntervals = false;
ChildIndex = BTSpecialChild::OwnedByComposite;
}
void UBTAuxiliaryNode::WrappedOnBecomeRelevant(UBehaviorTreeComponent& OwnerComp, uint8* NodeMemory) const
{
if (bNotifyBecomeRelevant)
{
const UBTNode* NodeOb = HasInstance() ? GetNodeInstance(OwnerComp, NodeMemory) : this;
if (NodeOb)
{
((UBTAuxiliaryNode*)NodeOb)->OnBecomeRelevant(OwnerComp, NodeMemory);
}
}
}
void UBTAuxiliaryNode::WrappedOnCeaseRelevant(UBehaviorTreeComponent& OwnerComp, uint8* NodeMemory) const
{
if (bNotifyCeaseRelevant)
{
const UBTNode* NodeOb = HasInstance() ? GetNodeInstance(OwnerComp, NodeMemory) : this;
if (NodeOb)
{
((UBTAuxiliaryNode*)NodeOb)->OnCeaseRelevant(OwnerComp, NodeMemory);
}
}
}
bool UBTAuxiliaryNode::WrappedTickNode(UBehaviorTreeComponent& OwnerComp, uint8* NodeMemory, float DeltaSeconds, float& NextNeededDeltaTime) const
{
if (bNotifyTick || HasInstance())
{
const UBTAuxiliaryNode* NodeOb = HasInstance() ? static_cast<UBTAuxiliaryNode*>(GetNodeInstance(OwnerComp, NodeMemory)) : this;
ensure(NodeOb);
if (NodeOb != nullptr && NodeOb->bNotifyTick)
{
float UseDeltaTime = DeltaSeconds;
if (NodeOb->bTickIntervals)
{
FBTAuxiliaryMemory* AuxMemory = GetSpecialNodeMemory<FBTAuxiliaryMemory>(NodeMemory);
AuxMemory->NextTickRemainingTime -= DeltaSeconds;
AuxMemory->AccumulatedDeltaTime += DeltaSeconds;
const bool bTick = AuxMemory->NextTickRemainingTime <= 0.0f;
if (bTick)
{
UseDeltaTime = AuxMemory->AccumulatedDeltaTime;
AuxMemory->AccumulatedDeltaTime = 0.0f;
const_cast<UBTAuxiliaryNode*>(NodeOb)->TickNode(OwnerComp, NodeMemory, UseDeltaTime);
}
if (AuxMemory->NextTickRemainingTime < NextNeededDeltaTime)
{
NextNeededDeltaTime = AuxMemory->NextTickRemainingTime;
}
return bTick;
}
else
{
const_cast<UBTAuxiliaryNode*>(NodeOb)->TickNode(OwnerComp, NodeMemory, UseDeltaTime);
NextNeededDeltaTime = 0.0f;
return true;
}
}
}
return false;
}
void UBTAuxiliaryNode::SetNextTickTime(uint8* NodeMemory, float RemainingTime) const
{
if (bTickIntervals)
{
FBTAuxiliaryMemory* AuxMemory = GetSpecialNodeMemory<FBTAuxiliaryMemory>(NodeMemory);
AuxMemory->NextTickRemainingTime = RemainingTime;
}
}
float UBTAuxiliaryNode::GetNextTickRemainingTime(uint8* NodeMemory) const
{
if (bTickIntervals)
{
FBTAuxiliaryMemory* AuxMemory = GetSpecialNodeMemory<FBTAuxiliaryMemory>(NodeMemory);
return FMath::Max(0.0f, AuxMemory->NextTickRemainingTime);
}
return 0.0f;
}
void UBTAuxiliaryNode::DescribeRuntimeValues(const UBehaviorTreeComponent& OwnerComp, uint8* NodeMemory, EBTDescriptionVerbosity::Type Verbosity, TArray<FString>& Values) const
{
Super::DescribeRuntimeValues(OwnerComp, NodeMemory, Verbosity, Values);
if (Verbosity == EBTDescriptionVerbosity::Detailed && bTickIntervals)
{
FBTAuxiliaryMemory* AuxMemory = GetSpecialNodeMemory<FBTAuxiliaryMemory>(NodeMemory);
Values.Add(FString::Printf(TEXT("next tick: %ss"), *FString::SanitizeFloat(AuxMemory->NextTickRemainingTime)));
}
}
void UBTAuxiliaryNode::OnBecomeRelevant(UBehaviorTreeComponent& OwnerComp, uint8* NodeMemory)
{
// empty in base class
}
void UBTAuxiliaryNode::OnCeaseRelevant(UBehaviorTreeComponent& OwnerComp, uint8* NodeMemory)
{
// empty in base class
}
void UBTAuxiliaryNode::TickNode(UBehaviorTreeComponent& OwnerComp, uint8* NodeMemory, float DeltaSeconds)
{
// empty in base class
}
uint16 UBTAuxiliaryNode::GetSpecialMemorySize() const
{
return bTickIntervals ? sizeof(FBTAuxiliaryMemory) : Super::GetSpecialMemorySize();
}
void UBTAuxiliaryNode::InitializeParentLink(uint8 MyChildIndex)
{
ChildIndex = MyChildIndex;
}
const UBTNode* UBTAuxiliaryNode::GetMyNode() const
{
return (ChildIndex == BTSpecialChild::OwnedByComposite) ? GetParentNode() : (GetParentNode() ? GetParentNode()->GetChildNode(ChildIndex) : nullptr);
}
float UBTAuxiliaryNode::GetNextNeededDeltaTime(UBehaviorTreeComponent& OwnerComp, uint8* NodeMemory) const
{
if (bNotifyTick || HasInstance())
{
const UBTAuxiliaryNode* NodeOb = HasInstance() ? static_cast<UBTAuxiliaryNode*>(GetNodeInstance(OwnerComp, NodeMemory)) : this;
if (NodeOb != nullptr && NodeOb->bNotifyTick)
{
if (bTickIntervals)
{
FBTAuxiliaryMemory* AuxMemory = GetSpecialNodeMemory<FBTAuxiliaryMemory>(NodeMemory);
return AuxMemory->NextTickRemainingTime;
}
else
{
return 0.0f;
}
}
}
return FLT_MAX;
}
//----------------------------------------------------------------------//
// DEPRECATED
//----------------------------------------------------------------------//