27 template<
typename Context>
35 if constexpr (has_OnNodeStartTick<Context, void()>)
37 context.OnNodeStartTick();
42 if constexpr (has_OnNodeEndTick<Context, void(
Status)>)
44 context.OnNodeEndTick(result);
55 template<
typename Context>
58 using Node<Context>::Node;
87 throw std::runtime_error(std::string(
"Out of bounds child ticking in ") + this->
GetFullDescriptor() +
" at index " + std::to_string(index));
92 throw std::runtime_error(std::string(
"Nullptr child in ") + this->
GetFullDescriptor() +
" at index " + std::to_string(index));
95 if constexpr (has_OnNodeTickChild<Context, void(
size_t)>)
97 context.OnNodeTickChild(index);
102 return children[index]->Tick(context);
104 catch (
const std::exception& ex)
106 throw std::runtime_error(std::string(
"In ") + this->
GetFullDescriptor() +
" while Ticking child " + std::to_string(index) +
"\n" +
112 std::vector<std::shared_ptr<Node<Context>>>
children;
115 template<
typename Context>
118 using Node<Context>::Node;
134 return child ==
nullptr ? 0 : 1;
145 if (
child ==
nullptr)
147 throw std::runtime_error(
"Nullptr child in decorator " + this->
GetFullDescriptor());
150 if constexpr (has_OnNodeTickChild<Context, void(
size_t)>)
152 context.OnNodeTickChild(0);
157 return child->Tick(context);
159 catch (
const std::exception& ex)
167 std::shared_ptr<Node<Context>>
child;
170 template<
typename Context>
176 template<
typename FuncType>
177 Leaf(
const std::string& s, FuncType func_) :
Node<Context>(s),
func(func_) {}
179 template<
typename FuncType,
typename... Args>
180 Leaf(
const std::string& s, FuncType func_, Args&&... args) :
Node<Context>(s)
182 func = [=](Context& c) ->
Status {
return func_(c, (args)...); };
207 return func(context);
209 catch (
const std::exception& ex)
211 if (this->
name.empty())
215 throw std::runtime_error(std::string(
"In leaf \"") + this->
name +
"\"\n" +
224 template<
typename Context>
227 using Node<Context>::Node;
240 return root ==
nullptr ? 0 : 1;
253 throw std::runtime_error(std::string(
"Nullptr tree when trying to tick tree ") + this->
GetFullDescriptor());
256 if constexpr (has_OnNodeTickChild<Context, void(
size_t)>)
258 context.OnNodeTickChild(0);
263 return root->Tick(context);
265 catch (
const std::exception& ex)
267 if (this->
name.empty())
271 throw std::runtime_error(std::string(
"In tree \"") + this->
name +
"\"\n" +
277 std::shared_ptr<Node<Context>>
root;
284 template<
typename Context>
307 template<
typename Context>
333 template<
typename Context>
348 template<
typename Context>
364 template<
typename Context>
392 template<
typename Parent,
typename Context>
395 template<
typename Context>
398 template<
typename Parent,
typename Context>
414 typename =
typename std::enable_if_t<std::is_convertible_v<S, std::string>>
418 auto child = std::make_shared<Leaf<Context> >(s, std::forward<Args>(args)...);
419 node->AddChild(child);
427 template<
typename... Args>
430 auto child = std::make_shared<Leaf<Context> >(
"", std::forward<Args>(args)...);
431 node->AddChild(child);
447 auto child = std::make_shared<Sequence<Context>>(s);
448 node->AddChild(child);
455 auto child = std::make_shared<Selector<Context>>(s);
456 node->AddChild(child);
462 typename CompositeType,
465 typename =
typename std::enable_if_t<std::is_convertible_v<S, std::string>>
469 auto child = std::make_shared<CompositeType>(s, std::forward<Args>(args)...);
470 node->AddChild(child);
475 typename CompositeType,
480 auto child = std::make_shared<CompositeType>(
"", std::forward<Args>(args)...);
481 node->AddChild(child);
489 auto child = std::make_shared<Inverter<Context>>(s);
490 node->AddChild(child);
497 auto child = std::make_shared<Succeeder<Context>>(s);
498 node->AddChild(child);
505 auto child = std::make_shared<Repeater<Context>>(
"", n);
506 node->AddChild(child);
513 auto child = std::make_shared<Repeater<Context>>(s, n);
514 node->AddChild(child);
520 typename DecoratorType,
523 typename =
typename std::enable_if_t<std::is_convertible_v<S, std::string>>
527 auto child = std::make_shared<DecoratorType>(s, std::forward<Args>(args)...);
528 node->AddChild(child);
532 typename DecoratorType,
537 auto child = std::make_shared<DecoratorType>(
"", std::forward<Args>(args)...);
538 node->AddChild(child);
543 std::is_same_v<Parent, Builder<Context>>,
544 std::shared_ptr<BehaviourTree<Context>>,
548 if constexpr (std::is_same_v<Parent, Builder<Context>>)
563 template<
typename Parent,
typename Context>
573 typename =
typename std::enable_if_t<std::is_convertible_v<S, std::string>>
576 std::is_same_v<Parent, Builder<Context>>,
577 std::shared_ptr<BehaviourTree<Context>>,
579 >
leaf(
const S& s, Args&&... args)
581 auto child = std::make_shared<Leaf<Context> >(s, std::forward<Args>(args)...);
582 node->SetChild(child);
583 if constexpr (std::is_same_v<Parent, Builder<Context>>)
592 template<
typename... Args>
594 std::is_same_v<Parent, Builder<Context>>,
595 std::shared_ptr<BehaviourTree<Context>>,
599 auto child = std::make_shared<Leaf<Context> >(
"", std::forward<Args>(args)...);
600 node->SetChild(child);
601 if constexpr (std::is_same_v<Parent, Builder<Context>>)
613 std::is_same_v<Parent, Builder<Context>>,
614 std::shared_ptr<BehaviourTree<Context>>,
619 if constexpr (std::is_same_v<Parent, Builder<Context>>)
634 auto child = std::make_shared<Sequence<Context>>(s);
635 node->SetChild(child);
642 auto child = std::make_shared<Selector<Context>>(s);
643 node->SetChild(child);
649 typename CompositeType,
652 typename =
typename std::enable_if_t<std::is_convertible_v<S, std::string>>
656 auto child = std::make_shared<CompositeType>(s, std::forward<Args>(args)...);
657 node->SetChild(child);
661 typename CompositeType,
666 auto child = std::make_shared<CompositeType>(
"", std::forward<Args>(args)...);
667 node->SetChild(child);
675 auto child = std::make_shared<Inverter<Context>>(s);
676 node->SetChild(child);
683 auto child = std::make_shared<Succeeder<Context>>(s);
684 node->SetChild(child);
691 auto child = std::make_shared<Repeater<Context>>(
"", n);
692 node->SetChild(child);
699 auto child = std::make_shared<Repeater<Context>>(s, n);
700 node->SetChild(child);
706 typename DecoratorType,
709 typename =
typename std::enable_if_t<std::is_convertible_v<S, std::string>>
713 auto child = std::make_shared<DecoratorType>(s, std::forward<Args>(args)...);
714 node->SetChild(child);
718 typename DecoratorType,
723 auto child = std::make_shared<DecoratorType>(
"", std::forward<Args>(args)...);
724 node->SetChild(child);
735 template<
typename Context>
744 typename =
typename std::enable_if_t<std::is_convertible_v<S, std::string>>
746 std::shared_ptr<BehaviourTree<Context>>
leaf(
const S& s, Args&&... args)
748 auto tree = std::make_shared<BehaviourTree<Context> >(
root_name);
749 tree->SetRoot(std::make_shared<
Leaf<Context> >(s, std::forward<Args>(args)...));
752 template<
typename... Args>
753 std::shared_ptr<BehaviourTree<Context>>
leaf(Args&&... args)
755 auto tree = std::make_shared<BehaviourTree<Context> >(
root_name);
756 tree->SetRoot(std::make_shared<
Leaf<Context> >(
"", std::forward<Args>(args)...));
765 root = std::make_shared<Sequence<Context>>(s);
772 root = std::make_shared<Selector<Context>>(s);
778 typename CompositeType,
781 typename =
typename std::enable_if_t<std::is_convertible_v<S, std::string>>
785 root = std::make_shared<CompositeType>(s, std::forward<Args>(args)...);
789 typename CompositeType,
794 root = std::make_shared<CompositeType>(
"", std::forward<Args>(args)...);
802 root = std::make_shared<Inverter<Context>>(s);
809 root = std::make_shared<Succeeder<Context>>(s);
816 root = std::make_shared<Repeater<Context>>(
"", n);
823 root = std::make_shared<Repeater<Context>>(s, n);
829 typename DecoratorType,
832 typename =
typename std::enable_if_t<std::is_convertible_v<S, std::string>>
836 root = std::make_shared<DecoratorType>(s, std::forward<Args>(args)...);
840 typename DecoratorType,
845 root = std::make_shared<DecoratorType>(
"", std::forward<Args>(args)...);
850 std::shared_ptr<BehaviourTree<Context> >
build()
852 auto tree = std::make_shared<BehaviourTree<Context> >(
root_name);
861 std::shared_ptr<Node<Context> >
root;
#define GENERATE_CHECK_HAS_FUNC(FuncName)
std::string GetFullDescriptor() const
BaseNode(const std::string &name_)
virtual BehaviourNodeType GetNodeType() const override
std::shared_ptr< Node< Context > > root
virtual size_t GetNumChildren() const override
virtual const BaseNode * GetChild(const size_t index) const override
void SetRoot(const std::shared_ptr< Node< Context > > node)
virtual Status TickImpl(Context &context) const override
CompositeBuilder< Builder, Context > sequence(const std::string &s="")
DecoratorBuilder< Builder, Context > repeater(const size_t n)
std::shared_ptr< Node< Context > > root
DecoratorBuilder< Builder, Context > decorator(Args &&... args)
Builder(const std::string &name="")
DecoratorBuilder< Builder, Context > decorator(const S &s, Args &&... args)
std::shared_ptr< BehaviourTree< Context > > build()
CompositeBuilder< Builder, Context > composite(Args &&... args)
std::shared_ptr< BehaviourTree< Context > > leaf(Args &&... args)
DecoratorBuilder< Builder, Context > repeater(const std::string &s, const size_t n)
const std::string root_name
CompositeBuilder< Builder, Context > composite(const S &s, Args &&... args)
DecoratorBuilder< Builder, Context > succeeder(const std::string &s="")
DecoratorBuilder< Builder, Context > inverter(const std::string &s="")
std::shared_ptr< BehaviourTree< Context > > leaf(const S &s, Args &&... args)
CompositeBuilder< Builder, Context > selector(const std::string &s="")
friend DecoratorBuilder< Builder, Context >
friend CompositeBuilder< Builder, Context >
DecoratorBuilder< CompositeBuilder, Context > inverter(const std::string &s="")
DecoratorBuilder< CompositeBuilder, Context > succeeder(const std::string &s="")
CompositeBuilder leaf(const S &s, Args &&... args)
To add a named leaf.
DecoratorBuilder< CompositeBuilder, Context > decorator(Args &&... args)
Composite< Context > * node
CompositeBuilder(Parent *parent, Composite< Context > *node)
CompositeBuilder< CompositeBuilder, Context > sequence(const std::string &s="")
CompositeBuilder tree(std::shared_ptr< BehaviourTree< Context > > arg)
std::conditional_t< std::is_same_v< Parent, Builder< Context > >, std::shared_ptr< BehaviourTree< Context > >, Parent & > end()
CompositeBuilder< CompositeBuilder, Context > composite(const S &s, Args &&... args)
CompositeBuilder< CompositeBuilder, Context > composite(Args &&... args)
DecoratorBuilder< CompositeBuilder, Context > repeater(const size_t n)
DecoratorBuilder< CompositeBuilder, Context > decorator(const S &s, Args &&... args)
CompositeBuilder leaf(Args &&... args)
To add an anonymous leaf.
CompositeBuilder< CompositeBuilder, Context > selector(const std::string &s="")
DecoratorBuilder< CompositeBuilder, Context > repeater(const std::string &s, const size_t n)
virtual size_t GetNumChildren() const override
virtual const BaseNode * GetChild(const size_t index) const override
Status TickChild(Context &context, const size_t index) const
std::vector< std::shared_ptr< Node< Context > > > children
virtual BehaviourNodeType GetNodeType() const override
void AddChild(const std::shared_ptr< Node< Context > > &child)
CompositeBuilder< Parent, Context > composite(Args &&... args)
CompositeBuilder< Parent, Context > selector(const std::string &s="")
DecoratorBuilder< Parent, Context > repeater(const std::string &s, const size_t n)
CompositeBuilder< Parent, Context > sequence(const std::string &s="")
Decorator< Context > * node
CompositeBuilder< Parent, Context > composite(const S &s, Args &&... args)
DecoratorBuilder< Parent, Context > decorator(const S &s, Args &&... args)
DecoratorBuilder< Parent, Context > repeater(const size_t n)
DecoratorBuilder< Parent, Context > inverter(const std::string &s="")
DecoratorBuilder< Parent, Context > decorator(Args &&... args)
DecoratorBuilder< Parent, Context > succeeder(const std::string &s="")
std::conditional_t< std::is_same_v< Parent, Builder< Context > >, std::shared_ptr< BehaviourTree< Context > >, Parent & > leaf(const S &s, Args &&... args)
std::conditional_t< std::is_same_v< Parent, Builder< Context > >, std::shared_ptr< BehaviourTree< Context > >, Parent & > leaf(Args &&... args)
std::conditional_t< std::is_same_v< Parent, Builder< Context > >, std::shared_ptr< BehaviourTree< Context > >, Parent & > tree(std::shared_ptr< BehaviourTree< Context > > arg)
DecoratorBuilder(Parent *parent, Decorator< Context > *node)
virtual const BaseNode * GetChild(const size_t index) const override
virtual size_t GetNumChildren() const override
std::shared_ptr< Node< Context > > child
virtual BehaviourNodeType GetNodeType() const override
Status TickChild(Context &context) const
void SetChild(std::shared_ptr< Node< Context > > child_)
A Decorator that inverts the result of its child.
virtual Status TickImpl(Context &context) const override
Leaf(const std::string &s, FuncType func_, Args &&... args)
virtual BehaviourNodeType GetNodeType() const override
std::function< Status(Context &)> func
virtual const BaseNode * GetChild(const size_t index) const override
virtual size_t GetNumChildren() const override
Leaf(const std::string &s, FuncType func_)
virtual Status TickImpl(Context &context) const override
virtual Status TickImpl(Context &context) const =0
Status Tick(Context &context) const
A Decorator that ticks its child n times (repeat until first success if n == 0).
Repeater(const std::string &s, const size_t n_)
virtual Status TickImpl(Context &context) const override
virtual Status TickImpl(Context &context) const override
virtual Status TickImpl(Context &context) const override
A Decorator that always return success, independently of the result of its child.
virtual Status TickImpl(Context &context) const override