arora_behavior/lib.rs
1//! Behavior interpreters: the executors the Arora runtime ticks each step.
2//!
3//! Two things share the word "behavior", and this crate is about only one of
4//! them:
5//!
6//! - A **behavior** (the noun) is an *authored, editable representation* of what
7//! a device should do — a behavior tree, a node graph — produced in a visual
8//! editor (Studio, the Vizij Workspace) and shipped as data.
9//! - A [`BehaviorInterpreter`] is the *runtime-level executor* that runs one of
10//! those: a behavior-tree interpreter, a node-graph interpreter. It is the
11//! thing the runtime actually ticks.
12//!
13//! The runtime holds one `Box<dyn BehaviorInterpreter>` and ticks it — swapping
14//! the interpreter replaces the behavior. The behavior tree is one interpreter
15//! (`arora-behavior-tree`'s [`BehaviorTreeInterpreter`]); a Vizij node graph is
16//! another. Adding a new *kind of authored behavior* means adding a new
17//! interpreter here. Hand-implementing the trait to hard-code a single behavior
18//! in Rust is possible, but it is a corner case — the promoted path is to author
19//! a behavior in an editor and let an interpreter run it.
20//!
21//! An authored behavior is a [`graph::Graph`] — nodes bound to functions, with
22//! typed I/Os and links — and interpreters are edited by
23//! [`apply`](BehaviorInterpreter::apply)ing a [`graph::GraphDiff`]. Loading a
24//! behavior is applying a diff onto an empty graph. See [`graph`] for the model.
25//!
26//! Each tick an interpreter gets a [`BehaviorContext`]: the shared
27//! [`DataStore`](arora_types::data::DataStore) (read inputs, write intent /
28//! outputs) and a [`CallBridge`](arora_types::call::CallBridge) (so a
29//! module-calling interpreter like the behavior tree can reach the engine). An
30//! interpreter uses whichever it needs — a graph reads/writes the store; the
31//! tree drives the caller.
32//!
33//! Timing is not a tick argument. The runtime publishes the frame's clock into
34//! the store under the [`golden`] keys before it ticks, so an interpreter that
35//! needs `dt` or elapsed time reads it from the store like any other slot.
36
37use arora_types::call::CallBridge;
38use arora_types::data::DataStore;
39
40pub mod golden;
41pub mod graph;
42
43pub use graph::{Graph, GraphDiff};
44
45/// Whether an interpreter wants to be ticked again.
46#[derive(Debug, Clone, Copy, PartialEq, Eq)]
47pub enum BehaviorStatus {
48 /// Tick me again next step (a node graph; a tree still running).
49 Running,
50 /// I reached a terminal state; the runtime may drop me.
51 Done,
52}
53
54/// What a [`BehaviorInterpreter`] receives each
55/// [`tick`](BehaviorInterpreter::tick).
56pub struct BehaviorContext<'a> {
57 /// The shared blackboard: read inputs, write intent / outputs.
58 pub store: &'a dyn DataStore,
59 /// The module-call bridge (the engine), for interpreters that call modules.
60 pub call_bridge: &'a mut dyn CallBridge,
61}
62
63/// An interpreter failed to tick.
64#[derive(Debug, Clone, PartialEq, Eq)]
65pub struct BehaviorError {
66 /// Human-readable description.
67 pub message: String,
68}
69
70impl std::fmt::Display for BehaviorError {
71 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
72 f.write_str(&self.message)
73 }
74}
75
76impl std::error::Error for BehaviorError {}
77
78/// A behavior *executor*: something the Arora runtime ticks once per step to run
79/// an authored behavior.
80///
81/// Implemented by the behavior tree (`arora-behavior-tree`'s
82/// [`BehaviorTreeInterpreter`]) and by other interpreters such as a Vizij
83/// node-graph interpreter. The runtime holds one `Box<dyn BehaviorInterpreter>`
84/// and ticks it without knowing which is which.
85///
86/// Implement this to add a new *kind of executor* (a new authored-behavior
87/// representation the runtime can run), not to hand-code one particular
88/// behavior — authored behaviors come from the visual editors, run by an
89/// existing interpreter.
90///
91/// Not `Send`: the runtime is a single-owner, single-thread step loop.
92pub trait BehaviorInterpreter {
93 /// Advance one step. Return [`BehaviorStatus::Running`] to be ticked again,
94 /// or [`BehaviorStatus::Done`] to be dropped.
95 fn tick(&mut self, ctx: &mut BehaviorContext) -> Result<BehaviorStatus, BehaviorError>;
96
97 /// Edit the running behavior by applying a [`GraphDiff`](graph::GraphDiff):
98 /// add/remove nodes and links, set/override predetermined keys. **Loading** a
99 /// behavior is applying a diff onto an empty graph.
100 ///
101 /// The default rejects edition — override it in interpreters that carry an
102 /// editable [`graph::Graph`] (the behavior tree does). A hand-coded,
103 /// non-graph interpreter (the corner case above) can leave this as-is.
104 ///
105 /// `apply` validates and stores the edit; an implementation may defer
106 /// re-lowering its runtime form to the next [`tick`](Self::tick) (which
107 /// carries the store in its context), so a lowering problem can surface
108 /// there rather than here. This keeps `apply` callable from anywhere a
109 /// [`GraphDiff`](graph::GraphDiff) arrives — notably an engine-registered
110 /// edit function, which holds no store.
111 fn apply(&mut self, diff: graph::GraphDiff) -> Result<(), BehaviorError> {
112 let _ = diff;
113 Err(BehaviorError {
114 message: "this interpreter does not support graph edition".to_string(),
115 })
116 }
117}