10 #include <Atoms/Globals.h>
11 #include <Atoms/BehaviourTree/Behaviour.h>
12 #include <Atoms/BehaviourTree/Blackboard.h>
13 #include <Atoms/BehaviourTree/BlackboardValue.h>
14 #include <Atoms/AgentGroup.h>
15 #include <Atoms/Agent.h>
36 unsigned short childIndex;
37 unsigned short childState;
44 if (agent) { agent->lock(); }
48 if (agent) { agent->unlock(); }
53 const unsigned short nullIndex = 0;
65 bool start(
const std::string& name);
67 bool start(
unsigned short index);
69 void abort(
unsigned short index);
71 bool isRunning()
const;
75 Behaviour::Status tick();
86 void releaseMemory(T* data) {
90 std::string behaviourName(
unsigned short index)
const;
93 inline T& getBlackboardValue(BlackboardValue<T>& value);
96 inline std::vector<T>& getBlackboardValue(BlackboardValue<std::vector<T>>& value);
99 inline bool setBlackboardValue(
const BlackboardValue<T>& value);
102 inline bool setBlackboardValue(
const BlackboardValue<std::vector<T>>& value);
104 AtomsPtr<AtomsCore::Metadata> getBlackboardValueFromName(
const std::string& name)
const;
106 AtomsPtr<AtomsCore::Metadata> getBlackboardValueFromPosition(
const BlackboardEntryPosition& position)
const;
118 BlackboardInstance blackboardInstance;
122 AgentGroup* agentGroup;
128 std::deque<BehaviourStackState> m_activeBehaviours;
130 std::vector<Behaviour::State> m_activeStack;
132 std::vector<BehaviourStackState> m_aborted;
134 std::vector<BehaviourStackState> m_started;
136 std::vector<unsigned short> m_successBehaviours;
137 std::vector<unsigned short> m_failureBehaviours;
139 unsigned short tickCount;
152 inline T& BehaviourTreeContext::getBlackboardValue(BlackboardValue<T>& value)
154 if (value.position.valueIndex == -1)
159 auto* sourceboard = &blackboardInstance;
160 if (value.position.parentIndex > 0)
162 for (
short i = 0; i < value.position.parentIndex; ++i)
169 sourceboard = sourceboard->parent;
174 if (!sourceboard || sourceboard->size() <= value.position.valueIndex)
179 auto& entry = sourceboard->blackboard->entry(value.position.valueIndex);
180 auto& instanceEntry = sourceboard->entry(value.position.valueIndex);
181 if (entry.flags != Atoms::BlackboardEntry::Flags::LinkMetadata)
186 return metaPtr->
get();
201 return agentMeta->get();
205 inline AtomsPtr<AtomsCore::Metadata>& BehaviourTreeContext::getBlackboardValue<AtomsPtr<AtomsCore::Metadata>>(BlackboardMetadataValue& value)
207 if (value.position.valueIndex == -1)
212 auto* sourceboard = &blackboardInstance;
213 if (value.position.parentIndex > 0)
215 for (
short i = 0; i < value.position.parentIndex; ++i)
222 sourceboard = sourceboard->parent;
227 if (!sourceboard || sourceboard->size() <= value.position.valueIndex)
232 auto& entry = sourceboard->blackboard->entry(value.position.valueIndex);
233 auto& instanceEntry = sourceboard->entry(value.position.valueIndex);
234 if (entry.flags != Atoms::BlackboardEntry::Flags::LinkMetadata)
238 return instanceEntry;
249 auto agentMeta = agent->metadata().data().find(entry.name);
250 if (agentMeta == agent->metadata().data().end())
253 return agentMeta->second;
257 inline std::vector<T>& BehaviourTreeContext::getBlackboardValue(BlackboardValue<std::vector<T>>& value)
259 if (value.position.valueIndex == -1)
264 auto* sourceboard = &blackboardInstance;
265 if (value.position.parentIndex > 0)
267 for (
short i = 0; i < value.position.parentIndex; ++i)
274 sourceboard = sourceboard->parent;
279 if (!sourceboard || sourceboard->size() <= value.position.valueIndex)
284 auto& entry = sourceboard->blackboard->entry(value.position.valueIndex);
285 auto& instanceEntry = sourceboard->entry(value.position.valueIndex);
286 if (entry.flags != Atoms::BlackboardEntry::Flags::LinkMetadata)
291 return metaPtr->
get();
306 return agentMeta->get();
310 inline bool BehaviourTreeContext::setBlackboardValue(
const BlackboardValue<T>& value)
312 if (value.position.valueIndex == -1)
317 auto* sourceboard = &blackboardInstance;
318 if (value.position.parentIndex > 0)
320 for (
short i = 0; i < value.position.parentIndex; ++i)
327 sourceboard = sourceboard->parent;
332 if (!sourceboard || sourceboard->size() <= value.position.valueIndex)
337 auto& entry = sourceboard->blackboard->entry(value.position.valueIndex);
338 auto& instanceEntry = sourceboard->entry(value.position.valueIndex);
339 if (entry.flags != Atoms::BlackboardEntry::Flags::LinkMetadata)
362 inline bool BehaviourTreeContext::setBlackboardValue<AtomsPtr<AtomsCore::Metadata>>(
const BlackboardMetadataValue& value)
364 if (value.position.valueIndex == -1)
369 auto* sourceboard = &blackboardInstance;
370 if (value.position.parentIndex > 0)
372 for (
short i = 0; i < value.position.parentIndex; ++i)
379 sourceboard = sourceboard->parent;
384 if (!sourceboard || sourceboard->size() <= value.position.valueIndex)
389 auto& entry = sourceboard->blackboard->entry(value.position.valueIndex);
390 auto& instanceEntry = sourceboard->entry(value.position.valueIndex);
391 if (entry.flags != Atoms::BlackboardEntry::Flags::LinkMetadata)
393 if (instanceEntry && instanceEntry->typeId() == value.value->typeId())
395 instanceEntry->copy(value.value.get());
399 instanceEntry = value.value->clone();
407 auto agentMeta = agent->metadata().getEntry(entry.name);
408 if (agentMeta && agentMeta->typeId() == value.value->typeId())
410 agentMeta->copy(value.value.get());
417 inline bool BehaviourTreeContext::setBlackboardValue(
const BlackboardValue<std::vector<T>>& value)
419 if (value.position.valueIndex == -1)
424 auto* sourceboard = &blackboardInstance;
425 if (value.position.parentIndex > 0)
427 for (
short i = 0; i < value.position.parentIndex; ++i)
434 sourceboard = sourceboard->parent;
439 if (!sourceboard || sourceboard->size() <= value.position.valueIndex)
444 auto& entry = sourceboard->blackboard->entry(value.position.valueIndex);
445 auto& instanceEntry = sourceboard->entry(value.position.valueIndex);
446 if (entry.flags != Atoms::BlackboardEntry::Flags::LinkMetadata)
Agent.
Definition: Agent.h:44
AtomsCore::MapMetadata & metadata()
Gets the agent metadata map.
Definition: Agent.impl.h:32
Definition: Behaviour.h:21
Definition: BehaviourTreeContext.h:41
Definition: BehaviourTreeContext.h:24
Definition: BehaviourTree.h:27
Atoms namespace.
Definition: Agent.h:29
Definition: Behaviour.h:34
Definition: BehaviourTreeContext.h:34
Definition: BehaviourTreeContext.h:28
Definition: BlackboardValue.h:21