Skip to content

About

A versatile Kotlin library for creating and managing behavior trees with an intuitive DSL.

Resources

Stars

1 star

Watchers

1 watching

Forks

Repository files navigation

KoBeTree: Kotlin Behavior Tree Library

Description

KoBeTree is a powerful and flexible Kotlin library designed specifically for building behavior trees. Behavior trees are widely used in AI and game development to create complex, decision-driven behaviors for characters, NPCs, and more.

Key Features

  • DSL API: KoBeTree offers an intuitive and expressive Domain-Specific Language (DSL) for constructing behavior trees with ease.
  • Composite Nodes: Implement complex decision-making logic using composite nodes like Selector and Sequence.
  • Decorator Nodes: Fine-tune behaviors with decorator nodes like Inverter and Repeater.
  • Task Nodes: Create custom task nodes using Kotlin lambdas for dynamic and specialized behaviors.
  • Blackboard: Share data and information across nodes using a centralized blackboard.

Installation

You can easily integrate KoBeTree into your Kotlin projects by adding the following dependency:

Add the repository to your project's settings.gradle.kts or settings.gradle file:

dependencyResolutionManagement {
    ...
    repositories {
        ...
        maven {
            url = uri("https://maven.pkg.github.com/pererikbergman/github-packages")
        }
    }
}

Gradle

dependencies {
    implementation("com.rakangsoftware.kobetree:kobetree:0.1.0") // Replace with the latest version
}

Maven

<dependencies>
    <dependency>
        <groupId>com.rakangsoftware.kobetree</groupId>
        <artifactId>kobetree</artifactId>
        <version>0.1.0</version> <!-- Replace with the latest version -->
    </dependency>
</dependencies>

Getting Started

Here's a simple example of how to create a behavior tree using KoBeTree:

import com.rakangsoftware.kobetree.core.BehaviorStatus
import com.rakangsoftware.kobetree.core.Blackboard
import com.rakangsoftware.kobetree.dsl.condition
import com.rakangsoftware.kobetree.dsl.selector
import com.rakangsoftware.kobetree.dsl.sequence
import com.rakangsoftware.kobetree.dsl.task

fun main() {
    // Create a blackboard to share data between nodes
    val blackboard = Blackboard().apply {
        put("health", 100)
        put("enemyInSight", true)
    }

    // Define a behavior tree using the DSL
    val tree = selector(blackboard) {
        sequence {
            condition { blackboard.get<Int>("health")!! > 50 }         // Healthy enough to fight?
            condition { blackboard.get<Boolean>("enemyInSight") == true } // Enemy in sight?
            task {
                println("Attack the enemy!")
                BehaviorStatus.SUCCESS
            }
        }

        sequence {
            condition { blackboard.get<Boolean>("enemyInSight") == true } // Enemy in sight?
            task {
                println("Run away!")
                BehaviorStatus.SUCCESS
            }
        }
    }

    // Execute it (call execute() again on every tick)
    val result = tree.execute()
    println("Behavior tree execution result: $result")
}

Every node's execute() returns a BehaviorStatus of SUCCESS, FAILURE, or RUNNING. task { } lambdas must return one of these, and condition { } lambdas return a Boolean.

Available DSL builders: selector, sequence, inverter, repeater(times), task, and condition. Each builder has a root form that takes a blackboard and a nested form for use inside another builder:

val patrol = repeater(blackboard, times = 3) {
    sequence {
        task { walkToNextWaypoint() }
        inverter { condition { blackboard.get<Boolean>("enemyInSight") == true } }
    }
}

Documentation

TBA

License

KoBeTree is released under the MIT License for maximum flexibility and compatibility with your projects.

Contribute

We welcome contributions from the community! If you have ideas for improvements, bug reports, or new features, please open an issue or submit a pull request.

Acknowledgments

KoBeTree is inspired by the work of many AI and game development enthusiasts who have contributed to the field of behavior trees.

About

A versatile Kotlin library for creating and managing behavior trees with an intuitive DSL.

Resources

Stars

1 star

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages