1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
use crate::prelude::*;
pub struct Node<T> {
pub name: Option<String>,
pub behavior: Behavior<T>,
pub status: Status,
pub collapse_as: Option<String>,
}
impl<T> Node<T> {
fn new(behavior: Behavior<T>) -> Node<T> {
Node {
name: None,
behavior,
status: Status::Running,
collapse_as: None,
}
}
pub fn new_named(name: String, behavior: Behavior<T>) -> Node<T> {
Node {
name: Some(name),
behavior,
status: Status::Running,
collapse_as: None,
}
}
pub fn action(name: &str, func: fn(&mut T) -> Status) -> Node<T> {
Self::new_named(name.to_owned(), Behavior::Action(name.to_owned(), func))
}
pub fn action_success(name: &str, func: fn(&mut T) -> ()) -> Node<T> {
Self::new_named(
name.to_owned(),
Behavior::ActionSuccess(name.to_owned(), func),
)
}
pub fn stateful_action(name: &str, func: Box<dyn StatefulAction<T>>) -> Node<T> {
Self::new_named(
name.to_owned(),
Behavior::StatefulAction(name.to_owned(), func),
)
}
pub fn sequence(nodes: Vec<Node<T>>) -> Node<T> {
Self::new(Behavior::Sequence(0, nodes))
}
pub fn named_sequence(name: &str, nodes: Vec<Node<T>>) -> Node<T> {
Self::new_named(name.to_owned(), Behavior::Sequence(0, nodes))
}
pub fn select(nodes: Vec<Node<T>>) -> Node<T> {
Self::new(Behavior::Select(0, nodes))
}
pub fn named_select(name: &str, nodes: Vec<Node<T>>) -> Node<T> {
Self::new_named(name.to_owned(), Behavior::Select(0, nodes))
}
pub fn cond(name: &str, cond: fn(&T) -> bool, success: Node<T>, failure: Node<T>) -> Node<T> {
Self::new_named(
name.to_owned(),
Behavior::Cond(name.to_owned(), cond, Box::new(success), Box::new(failure)),
)
}
pub fn wait(time: f64) -> Node<T> {
Self::new(Behavior::Wait {
curr: time,
max: time,
})
}
pub fn named_while_single(name: &str, cond: fn(&T) -> bool, child: Node<T>) -> Node<T> {
Self::new_named(name.to_owned(), Behavior::While(cond, Box::new(child)))
}
pub fn while_single(cond: fn(&T) -> bool, child: Node<T>) -> Node<T> {
Self::new(Behavior::While(cond, Box::new(child)))
}
pub fn collapse(self, desc: &str) -> Node<T> {
Self {
collapse_as: Some(desc.to_owned()),
..self
}
}
pub fn tick(&mut self, delta: f64, context: &mut T) -> (Status, DebugRepr) {
if self.status == Status::Success || self.status == Status::Failure {
self.behavior.reset();
}
let (status, repr) = self.behavior.tick(delta, context);
self.status = status;
(status, repr)
}
pub fn to_debug(&self) -> TreeRepr {
match &self.collapse_as {
Some(collapse_text) => TreeRepr::new(collapse_text, vec![]),
None => {
match &self.behavior {
Behavior::Wait { curr, max } => TreeRepr::new("Wait", vec![])
.with_detail(format!("curr={}, max={}", curr, max)),
Behavior::Cond(name, _cond, a, b) => {
TreeRepr::new("Cond", vec![a.to_debug(), b.to_debug()])
.with_detail(name.clone())
}
Behavior::Sequence(_, seq) => TreeRepr::new(
if let Some(ref name) = self.name {
format!("Sequence {}", name)
} else {
"Sequence".to_string()
},
seq.iter().map(|x| x.to_debug()).collect(),
),
Behavior::Select(_, seq) => TreeRepr::new(
if let Some(ref name) = self.name {
format!("Select {}", name)
} else {
"Select".to_string()
},
seq.iter().map(|x| x.to_debug()).collect(),
),
Behavior::Action(name, _) => {
TreeRepr::new("Action", vec![]).with_detail(name.clone())
}
Behavior::ActionSuccess(name, _) => {
TreeRepr::new("ActionSuccess", vec![]).with_detail(name.clone())
}
Behavior::StatefulAction(name, _) => {
TreeRepr::new("StatefulAction", vec![]).with_detail(name.clone())
}
Behavior::While(_, x) => x.to_debug(),
}
}
}
}
}