18 typedef std::shared_ptr<TreeNode> TreeNodePtr;
19 typedef std::vector<TreeNodePtr> TreeNodes;
54 #define NODE_FACTORY(NodeName) \
55 class Factory: public ::ai::ITreeNodeFactory { \
57 ::ai::TreeNodePtr create (const ::ai::TreeNodeFactoryContext *ctx) const override { \
58 return std::make_shared<NodeName>(ctx->name, ctx->parameters, ctx->condition); \
61 static const Factory& getFactory() { \
62 static Factory FACTORY; \
69 #define NODE_CLASS(NodeName) \
70 NodeName(const std::string& name, const std::string& parameters, const ::ai::ConditionPtr& condition) : \
71 ::ai::TreeNode(name, parameters, condition) { \
72 _type = AI_STRINGIFY(NodeName); \
74 virtual ~NodeName() { \
77 NODE_FACTORY(NodeName)
90 static int getNextId() {
92 const int nextId = _nextId++;
102 std::string _parameters;
103 ConditionPtr _condition;
105 TreeNodeStatus state(
const AIPtr& entity, TreeNodeStatus treeNodeState);
106 int getSelectorState(
const AIPtr& entity)
const;
107 void setSelectorState(
const AIPtr& entity,
int selected);
108 int getLimitState(
const AIPtr& entity)
const;
109 void setLimitState(
const AIPtr& entity,
int amount);
110 void setLastExecMillis(
const AIPtr& entity);
112 TreeNodePtr getParent_r(
const TreeNodePtr& parent,
int id)
const;
121 TreeNode(
const std::string& name,
const std::string& parameters,
const ConditionPtr& condition) :
122 _id(getNextId()), _name(name), _parameters(parameters), _condition(condition) {
135 const std::string&
getName()
const;
147 void setName(
const std::string& name);
151 const std::string&
getType()
const;
152 const ConditionPtr& getCondition()
const;
153 void setCondition(
const ConditionPtr& condition);
154 const TreeNodes& getChildren()
const;
155 TreeNodes& getChildren();
161 virtual void getRunningChildren(
const AIPtr& entity, std::vector<bool>& active)
const;
166 TreeNodeStatus getLastStatus(
const AIPtr& ai)
const;
173 virtual TreeNodeStatus
execute(
const AIPtr& entity, int64_t deltaMillis);
180 virtual bool addChild(
const TreeNodePtr& child);
181 TreeNodePtr getChild(
int id)
const;
198 TreeNodePtr
getParent(
const TreeNodePtr&
self,
int id)
const;
TreeNode(const std::string &name, const std::string ¶meters, const ConditionPtr &condition)
Definition: TreeNode.h:121
void setName(const std::string &name)
Updates the custom name of this TreeNode.
Definition: TreeNodeImpl.h:15
int64_t getLastExecMillis(const AIPtr &ai) const
Returns the time in milliseconds when this node was last run. This is only updated if execute() was c...
Definition: TreeNodeImpl.h:107
virtual TreeNodeStatus execute(const AIPtr &entity, int64_t deltaMillis)
Definition: TreeNodeImpl.h:184
Definition: MemoryAllocator.h:26
int getId() const
Return the unique id for this node.
Definition: TreeNodeImpl.h:11
const std::string & getType() const
The node type - this usually matches the class name of the TreeNode.
Definition: TreeNodeImpl.h:22
The base class for all behaviour tree actions.
Definition: TreeNode.h:88
virtual void getRunningChildren(const AIPtr &entity, std::vector< bool > &active) const
Get the state of all child nodes for the given entity.
Definition: TreeNodeImpl.h:61
const std::string & getName() const
Each node can have a user defines name that can be retrieved with this method.
Definition: TreeNodeImpl.h:26
bool replaceChild(int id, const TreeNodePtr &newNode)
Replace the given child node with a new one (or removes it)
Definition: TreeNodeImpl.h:142
TreeNodePtr getParent(const TreeNodePtr &self, int id) const
Get the parent node for a given TreeNode id - This should only be called on the root node of the beha...
Definition: TreeNodeImpl.h:170
virtual void resetState(const AIPtr &entity)
Reset the states in the node and also in the entity.
Definition: TreeNodeImpl.h:55
const std::string & getParameters() const
Return the raw parameters for this node.
Definition: TreeNodeImpl.h:38
int _id
Every node has an id to identify it. It's unique per type.
Definition: TreeNode.h:98