NoJobEngine
2.0.7
Toggle main menu visibility
Loading...
Searching...
No Matches
BTSelector.h
Go to the documentation of this file.
1
6
#pragma once
7
#include "
BTNode.h
"
8
#include <memory>
9
#include <utility>
10
#include <vector>
11
12
namespace
NoJob
{
13
class
BTSelector
final :
public
BTNode
{
14
public
:
15
explicit
BTSelector
(
BTNodeID
id
) :
BTNode
(id) {}
16
void
AddChild
(std::unique_ptr<BTNode> child) {
17
if
(child) m_Children.push_back(std::move(child));
18
}
19
std::string
DebugLabel
()
const override
{
return
"Selector"
; }
20
BTStatus
Tick
(
BTContext
& context,
float
dt)
const override
{
21
// A running fallback (e.g. Patrol) must not lock out higher-priority
22
// branches. Probe them on every tick and preempt when one is ready.
23
// This also supports ordinary Condition nodes, without a decorator
24
// Blackboard observer being required.
25
if
(context.
ConsumeReevaluation
())
26
context.
Emit
(
GetID
(),
"Reevaluation"
,
"Selector checking priorities"
);
27
28
const
auto
runningIndex = context.
GetNodeState
(
GetID
()).
CurrentChild
;
29
if
(runningIndex > 0 && runningIndex < m_Children.size()) {
30
for
(std::size_t i = 0; i < runningIndex; ++i) {
31
const
auto
status = m_Children[i]->Tick(context, dt);
32
if
(status ==
BTStatus::Failure
) {
33
m_Children[i]->Reset(context);
34
continue
;
35
}
36
context.
Emit
(
GetID
(),
"PriorityChanged"
,
"Higher priority branch preempted running fallback"
);
37
m_Children[runningIndex]->Reset(context);
38
context.
GetNodeState
(
GetID
()).
CurrentChild
= i;
39
if
(status ==
BTStatus::Success
) {
40
Reset
(context);
41
return
BTStatus::Success
;
42
}
43
return
BTStatus::Running
;
44
}
45
}
46
while
(
true
) {
47
const
auto
index = context.
GetNodeState
(
GetID
()).
CurrentChild
;
48
if
(index >= m_Children.size()) {
49
Reset
(context);
50
return
BTStatus::Failure
;
51
}
52
const
auto
status = m_Children[index]->Tick(context, dt);
53
if
(status ==
BTStatus::Running
)
return
BTStatus::Running
;
54
if
(status ==
BTStatus::Success
) {
55
Reset
(context);
56
return
BTStatus::Success
;
57
}
58
context.
GetNodeState
(
GetID
()).
CurrentChild
= index + 1;
59
}
60
}
61
void
Reset
(
BTContext
& context)
const override
{
62
for
(
const
auto
& child : m_Children) child->Reset(context);
63
BTNode::Reset
(context);
64
}
65
void
CollectChildren
(std::vector<const BTNode*>& out)
const override
{
66
for
(
const
auto
& child : m_Children) out.push_back(child.get());
67
}
68
private
:
69
std::vector<std::unique_ptr<BTNode>> m_Children;
70
};
71
}
BTNode.h
Base interface for behavior tree nodes.
NoJob::BTContext
Definition
BTContext.h:24
NoJob::BTContext::Emit
void Emit(BTNodeID node, std::string kind, std::string detail={})
Definition
BTContext.h:36
NoJob::BTContext::GetNodeState
BTNodeRuntimeState & GetNodeState(BTNodeID id)
Definition
BTContext.h:44
NoJob::BTContext::ConsumeReevaluation
bool ConsumeReevaluation() noexcept
Definition
BTContext.h:76
NoJob::BTNode::Reset
virtual void Reset(BTContext &context) const
Definition
BTNode.h:28
NoJob::BTNode::BTNode
BTNode(BTNodeID id)
Definition
BTNode.h:17
NoJob::BTNode::GetID
BTNodeID GetID() const
Definition
BTNode.h:21
NoJob::BTSelector::DebugLabel
std::string DebugLabel() const override
Definition
BTSelector.h:19
NoJob::BTSelector::CollectChildren
void CollectChildren(std::vector< const BTNode * > &out) const override
Definition
BTSelector.h:65
NoJob::BTSelector::AddChild
void AddChild(std::unique_ptr< BTNode > child)
Definition
BTSelector.h:16
NoJob::BTSelector::Reset
void Reset(BTContext &context) const override
Definition
BTSelector.h:61
NoJob::BTSelector::BTSelector
BTSelector(BTNodeID id)
Definition
BTSelector.h:15
NoJob::BTSelector::Tick
BTStatus Tick(BTContext &context, float dt) const override
Evaluates this node for one update.
Definition
BTSelector.h:20
NoJob
Definition
BTActionNode.h:13
NoJob::BTNodeID
std::uint64_t BTNodeID
Definition
BTContext.h:19
NoJob::BTStatus
BTStatus
Result of a behavior tree node tick.
Definition
BTNode.h:13
NoJob::BTStatus::Success
@ Success
Definition
BTNode.h:13
NoJob::BTStatus::Running
@ Running
Definition
BTNode.h:13
NoJob::BTStatus::Failure
@ Failure
Definition
BTNode.h:13
NoJob::BTNodeRuntimeState::CurrentChild
std::size_t CurrentChild
Definition
BTContext.h:21
Engine
AI
BehaviorTree
BTSelector.h
Generated by
1.18.0