Git Product home page Git Product logo

braintree's Introduction

BrainTree

A C++ behavior tree header-only library.

Features

  • behavior tree implementation
  • predefined composites
  • predefined decorators
  • (optional) rudimentary blackboard
  • (optional) behavior tree builders

Install

Include BrainTree.h in your project.

Example

// this example should print out "Hello, World!", four times
#include <iostream>
#include "BrainTree.h"

class Action : public BrainTree::Node
{
public:
    Status update() override
    {
        std::cout << "Hello, World!" << std::endl;
        return Node::Status::Success;
    }
};

void CreatingBehaviorTreeManually()
{
    BrainTree::BehaviorTree tree;
    auto sequence = std::make_shared<BrainTree::Sequence>();
    auto sayHello = std::make_shared<Action>();
    auto sayHelloAgain = std::make_shared<Action>();
    sequence->addChild(sayHello);
    sequence->addChild(sayHelloAgain);
    tree.setRoot(sequence);
    tree.update();
}

void CreatingBehaviorTreeUsingBuilders()
{
    auto tree = BrainTree::Builder()
        .composite<BrainTree::Sequence>()
            .leaf<Action>()
            .leaf<Action>()
        .end()
        .build();
    tree->update();
}

int main()
{
    CreatingBehaviorTreeManually();
    CreatingBehaviorTreeUsingBuilders();
    return 0;
}

Composites

Selector

  • The Selector composite ticks each child node in order.
  • If a child succeeds or runs, the selector returns the same status.
  • In the next tick, it will try to run each child in order again.
  • If all children fails, only then does the selector fail.

Sequence

  • The Sequence composite ticks each child node in order.
  • If a child fails or runs, the sequence returns the same status.
  • In the next tick, it will try to run each child in order again.
  • If all children succeeds, only then does the sequence succeed.

StatefulSelector

  • The StatefulSelector composite ticks each child node in order, and remembers what child it prevously tried to tick.
  • If a child succeeds or runs, the stateful selector returns the same status.
  • In the next tick, it will try to run the next child or start from the beginning again.
  • If all children fails, only then does the stateful selector fail.

StatefulSequence

  • The StatefulSequence composite ticks each child node in order, and remembers what child it prevously tried to tick.
  • If a child fails or runs, the stateful sequence returns the same status.
  • In the next tick, it will try to run the next child or start from the beginning again.
  • If all children succeeds, only then does the stateful sequence succeed.

Builder

The Builder class simplifies the process of creating a behavior tree. You use three methods to build your tree:

  • leaf<NodeType>()
  • composite<CompositeType>()
  • decorator<DecoratorType>()

Both composite() and decorator() require a corresponding call to end(), this marks where you are done adding children to a composite or a child to a decorator. At the very end you call build() which will then give you the finished behavior tree.

auto tree = Builder()
    .decorator<Repeater>()
        .composite<Sequence>()
            .leaf<SayHello>("Foo")
            .leaf<SayHello>("Bar")
        .end()
    .end()
    .build();

License

MIT (c) arvidsson 2015-2018

braintree's People

Contributors

arvidsson avatar s7jones avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.