Bonsai MDSL
A Rust library for parsing and executing behavior trees defined in MDSL (Mistreevous Domain Specific Language). Heavily inspired by Bonsai.
Features
- ๐ณ MDSL Parser: Parse behavior trees from MDSL strings
- โก Fast Execution: High-performance behavior tree execution engine
- ๐ Async Support: Built-in async/await support for long-running actions
- ๐ฏ Type Safety: Strongly typed tree nodes and states
- ๐งช Well Tested: Comprehensive test suite with benchmarks
Quick Start
Add this to your Cargo.toml:
[]
= "0.1"
Basic Usage
use ;
// Define your behavior tree in MDSL
let mdsl = r#"
root {
sequence {
action [check_health]
selector {
action [attack_enemy]
action [find_cover]
}
}
}
"#;
// Create a context with your action implementations
let mut context = new;
context.register_action;
context.register_action;
context.register_action;
// Parse and execute the tree
let mut tree = from_mdsl?;
let result = tree.tick?;
assert_eq!;
MDSL Syntax
Bonsai MDSL supports the same syntax as the original mistreevous library:
Composite Nodes
sequence { ... }- Execute children in order, fail on first failureselector { ... }- Execute children in order, succeed on first successparallel { ... }- Execute all children concurrentlyrace { ... }- Execute children concurrently, finish on first completionlotto { ... }- Randomly select one child to execute
Decorator Nodes
repeat [n] { ... }- Repeat child n timesretry [n] { ... }- Retry child up to n times on failureflip { ... }- Invert success/failure resultsucceed { ... }- Always return successfail { ... }- Always return failure
Leaf Nodes
action [name]- Execute an actioncondition [name]- Evaluate a conditionwait [ms]- Wait for specified milliseconds
Arguments and Guards
// Actions with arguments
action
// Guards
action while
action until
// Callbacks
sequence entry exit
Loop Constructs
The library supports two types of loop constructs for compatibility with bonsai-bt:
while - Standard While Loop
while [condition] {
action [some_action]
}
Checks the condition between child executions. This matches bonsai-bt's While behavior.
while_all - WhileAll Loop
while_all [condition] {
action [some_action]
}
Executes all children first as a sequence, then checks the condition. This matches bonsai-bt's WhileAll behavior.
until - Until Loop
until [condition] {
action [some_action]
}
Similar to while but continues until the condition becomes true (inverse of while).
Async Support
For long-running actions, use async support:
use ;
let mut context = new;
context.register_async_action;
let result = tree.tick_async.await?;
Integration with Existing Projects
This library is designed to be easily integrated into existing Rust projects. You can:
- Use as a dependency: Add to your
Cargo.tomland import - Local path dependency: Reference from a local workspace
- Git dependency: Reference directly from GitHub
# As a workspace member
[]
= ["bonsai-mdsl", "your-game"]
# Or as a git dependency
[]
= { = "https://github.com/bx2h/bonsai-mdsl" }
Examples
See the examples/ directory:
basic_tree- Simple behavior tree exampleasync_drone- Async action handlingboids- A simple boids implementationsimple_npc_ai- A simple NPC AI implementation
Contributing
Contributions are welcome! Please see CONTRIBUTING.md for guidelines.
License
MIT License (LICENSE)
Acknowledgments
This library is inspired by the excellent mistreevous TypeScript library and Bonsai.