Giter VIP home page Giter VIP logo

behaviortrees's Introduction

Behavior Trees

A simple C# example of Behavior Trees + Editor.

Introduction

Behavior Trees (BT) is a simple, scalable, modular solution that represents complex AI-behaviors and provides easy-to-maintain and configure logic.

They have been widely used in robotics and gaming since mid 2000, in particular, such game engines as Unreal, Unity, CryEngine use BT. You can learn more about Behavior Trees at the following links:

About project

This project demonstrates the concept and working principle of the Behavior Trees. Therefore, I tried to make it as simple and laconic as possible. You can fork, adapt and extend the project to suit your needs.

  • The project includes BehaviorTrees library with the main types of nodes: actions, conditions, composites and decorators (20 in total) as well as auxiliary classes. You can add your nodes by inheriting from existing ones. You can use ActionBase base class to create custom actions and use BaseEvent base class to create custom events. Trees can be serialized in json.

  • BehaviorTreesEditor allows you to edit trees with a simple TreeList control, to save, to load and to run trees.

Behavior Trees

Example 1

  • BehaviorTrees.Example1 contains simple example of the Behavior Tree with custom node Move:
[DataContract]
[BTNode("Move", "Example")]
public class Move : Node
{
    Point _position;
    bool _completed;

    [DataMember]
    public Point Position
    {
        get { return _position; }
        set {
            _position = value;
            Root.SendValueChanged(this);
        }
    }

    public Move(Point pos)
    {
        Position = pos;
    }

    public override string NodeParameters => $"Move To ({Position.X}, {Position.Y})";

    protected override void OnActivated()
    {
        base.OnActivated();
        Log.Write($"{Owner.Name} moving to {Position}");
        Task.Delay(1000).ContinueWith(_ => _completed = true);
    }

    protected override void OnDeactivated()
    {
        base.OnDeactivated();
        Log.Write($"{Owner.Name} moving completed ");
    }

    protected override ExecutingStatus OnExecuted()
    {
        return _completed ? ExecutingStatus.Success : ExecutingStatus.Running;
    }
}

BehaviorTrees.Example1 demonstrates Behavior Tree creation by using TreeBuilder:

var exampleBT = new TreeBuilder<Node>(new Sequence())
    .AddWithChild(new Loop(3))
        .AddWithChild(new Sequence())
            .Add(new Move(new Point(0, 0)))
            .Add(new Move(new Point(20, 0)))
            .AddWithChild(new Delay(2))
                .Add(new Move(new Point(0, 20)))
            .Up()
        .Up()
    .Up()
.ToTree();

Example 2 - IronPython nodes

The library BehaviorTrees.IronPython is an example of scripting language integration into the Behavior Tree. As an example two nodes are added: ScriptedAction and ScriptedCondition. The script can be edited both with help of the PropertyGrid and using the syntax highlighting editor implemented in BehaviorTrees.IronPython.Editor project.

Behavior Trees

You can extend the example and add your own API, import your types, both from the host application and from other IronPython scripts. Based on this example you can integrate another scripting language you need.

Build

  • net6.0 TFM is used for libraries and net6.0-windows for editors.
  • Please rebuild the solution to execute post-build scripts that will copy the example libraries to the output directory.

Third party in this project

Other Behavior Trees on Github

License

Copyright (c) 2015 Eugeny Novikov. Code under MIT license.

behaviortrees's People

Contributors

eugenyn avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  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.