ai_behavior/
lib.rs

1#![deny(missing_docs)]
2#![deny(missing_copy_implementations)]
3
4//! AI behavior tree
5//!
6//! You can serialize the
7//! behavior tree using [Serde](https://crates.io/crates/serde) and
8//! e.g. [Ron](https://crates.io/crates/ron).
9//!
10//! ### What is an AI behavior tree?
11//!
12//! An AI behavior tree is a kind of state machine logic for processes.
13//!
14//! Many things that a game logic does, e.g. controlling AI characters,
15//! fits the pattern of AI behavior trees.
16//!
17//! An AI behavior tree is a very generic way of organizing interactive logic.
18//! It has built-in semantics for processes that signals `Running`, `Success` or
19//! `Failure`.
20//!
21//! For example, if you have a state `A` and a state `B`:
22//!
23//! - Move from state `A` to state `B` if `A` succeeds: `Sequence([A, B])`
24//! - Try `A` first and then try `B` if `A` fails: `Select([A, B])`
25//! - Do `B` repeatedly while `A` runs: `While(A, [B])`
26//! - Do `A`, `B` forever: `While(WaitForever, [A, B])`
27//! - Wait for both `A` and `B` to complete: `WhenAll([A, B])`
28//! - Wait for either `A` or `B` to complete: `WhenAny([A, B])`
29//!
30//! See the `Behavior` enum for more information.
31//!
32//! ### Parallel semantics
33//!
34//! This library has parallel semantics for AI behavior trees.
35//! It means that multiple processes can happen at the same time
36//! and the logic can be constructed around how these processes runs or terminate.
37//!
38//! For example, `While(A, [B])` runs both `A` and `B` at the same time.
39//! If either `A` or `B` fails, then the whole while-behavior fails.
40//!
41//! A property of AI behavior trees with parallel semantics is that you can
42//! control termination conditions externally, as opposed to most
43//! programming languages where termination condition is controlled internally:
44//!
45//! ```text
46//! while A() {
47//!     // This inner loop will never terminate unless `B` fails.
48//!     while true {
49//!       B();  // Runs `B` forever.
50//!     }
51//! }
52//! ```
53//!
54//! ```text
55//! // This will terminate if `A` stops running, which also stops `B`.
56//! WhenAny([A,
57//!   While(WaitForever, [
58//!     B
59//!   ])
60//! ])
61//! ```
62
63extern crate input;
64#[macro_use]
65extern crate serde_derive;
66extern crate serde;
67
68pub use behavior::Behavior::{
69    self, Action, After, AlwaysSucceed, Fail, If, Select, Sequence, Wait, WaitForPressed,
70    WaitForReleased, WaitForever, WhenAll, WhenAny, While,
71};
72pub use state::{ActionArgs, State, RUNNING};
73pub use status::Status::{self, Failure, Running, Success};
74
75mod behavior;
76mod state;
77mod status;