dora_ssr/dora/platformer/behavior/tree.rs
1/* Copyright (c) 2016-2025 Li Jin <dragon-fly@qq.com>
2
3Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
5The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
7THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
8
9extern "C" {
10 fn platformer_behavior_tree_type() -> i32;
11 fn platformer_behavior_leaf_seq(nodes: i64) -> i64;
12 fn platformer_behavior_leaf_sel(nodes: i64) -> i64;
13 fn platformer_behavior_leaf_con(name: i64, func0: i32, stack0: i64) -> i64;
14 fn platformer_behavior_leaf_act(action_name: i64) -> i64;
15 fn platformer_behavior_leaf_command(action_name: i64) -> i64;
16 fn platformer_behavior_leaf_wait(duration: f64) -> i64;
17 fn platformer_behavior_leaf_countdown(time: f64, node: i64) -> i64;
18 fn platformer_behavior_leaf_timeout(time: f64, node: i64) -> i64;
19 fn platformer_behavior_leaf_repeat(times: i32, node: i64) -> i64;
20 fn platformer_behavior_leaf_repeat_forever(node: i64) -> i64;
21 fn platformer_behavior_leaf_retry(times: i32, node: i64) -> i64;
22 fn platformer_behavior_leaf_retry_until_pass(node: i64) -> i64;
23}
24use crate::dora::IObject;
25/// A behavior tree framework for creating game AI structures.
26pub struct Tree { raw: i64 }
27crate::dora_object!(Tree);
28impl Tree {
29 pub(crate) fn type_info() -> (i32, fn(i64) -> Option<Box<dyn IObject>>) {
30 (unsafe { platformer_behavior_tree_type() }, |raw: i64| -> Option<Box<dyn IObject>> {
31 match raw {
32 0 => None,
33 _ => Some(Box::new(Tree { raw: raw }))
34 }
35 })
36 }
37 /// Creates a new sequence node that executes an array of child nodes in order.
38 ///
39 /// # Arguments
40 ///
41 /// * `nodes` - A vector of child nodes.
42 ///
43 /// # Returns
44 ///
45 /// * `Leaf` - A new sequence node.
46 pub fn seq(nodes: &Vec<crate::dora::platformer::behavior::Tree>) -> crate::dora::platformer::behavior::Tree {
47 unsafe { return crate::dora::platformer::behavior::Tree::from(platformer_behavior_leaf_seq(crate::dora::Vector::from_btree(nodes))).unwrap(); }
48 }
49 /// Creates a new selector node that selects and executes one of its child nodes that will succeed.
50 ///
51 /// # Arguments
52 ///
53 /// * `nodes` - A vector of child nodes.
54 ///
55 /// # Returns
56 ///
57 /// * `Leaf` - A new selector node.
58 pub fn sel(nodes: &Vec<crate::dora::platformer::behavior::Tree>) -> crate::dora::platformer::behavior::Tree {
59 unsafe { return crate::dora::platformer::behavior::Tree::from(platformer_behavior_leaf_sel(crate::dora::Vector::from_btree(nodes))).unwrap(); }
60 }
61 /// Creates a new condition node that executes a check handler function when executed.
62 ///
63 /// # Arguments
64 ///
65 /// * `name` - The name of the condition.
66 /// * `check` - A function that takes a blackboard object and returns a boolean value.
67 ///
68 /// # Returns
69 ///
70 /// * `Leaf` - A new condition node.
71 pub fn con(name: &str, mut handler: Box<dyn FnMut(&crate::dora::platformer::behavior::Blackboard) -> bool>) -> crate::dora::platformer::behavior::Tree {
72 let mut stack0 = crate::dora::CallStack::new();
73 let stack_raw0 = stack0.raw();
74 let func_id0 = crate::dora::push_function(Box::new(move || {
75 let result = handler(&crate::dora::platformer::behavior::Blackboard::from(stack0.pop_i64().unwrap()).unwrap());
76 stack0.push_bool(result);
77 }));
78 unsafe { return crate::dora::platformer::behavior::Tree::from(platformer_behavior_leaf_con(crate::dora::from_string(name), func_id0, stack_raw0)).unwrap(); }
79 }
80 /// Creates a new action node that executes an action when executed.
81 /// This node will block the execution until the action finishes.
82 ///
83 /// # Arguments
84 ///
85 /// * `action_name` - The name of the action to execute.
86 ///
87 /// # Returns
88 ///
89 /// * `Leaf` - A new action node.
90 pub fn act(action_name: &str) -> crate::dora::platformer::behavior::Tree {
91 unsafe { return crate::dora::platformer::behavior::Tree::from(platformer_behavior_leaf_act(crate::dora::from_string(action_name))).unwrap(); }
92 }
93 /// Creates a new command node that executes a command when executed.
94 /// This node will return right after the action starts.
95 ///
96 /// # Arguments
97 ///
98 /// * `action_name` - The name of the command to execute.
99 ///
100 /// # Returns
101 ///
102 /// * `Leaf` - A new command node.
103 pub fn command(action_name: &str) -> crate::dora::platformer::behavior::Tree {
104 unsafe { return crate::dora::platformer::behavior::Tree::from(platformer_behavior_leaf_command(crate::dora::from_string(action_name))).unwrap(); }
105 }
106 /// Creates a new wait node that waits for a specified duration when executed.
107 ///
108 /// # Arguments
109 ///
110 /// * `duration` - The duration to wait in seconds.
111 ///
112 /// # Returns
113 ///
114 /// * A new wait node of type `Leaf`.
115 pub fn wait(duration: f64) -> crate::dora::platformer::behavior::Tree {
116 unsafe { return crate::dora::platformer::behavior::Tree::from(platformer_behavior_leaf_wait(duration)).unwrap(); }
117 }
118 /// Creates a new countdown node that executes a child node continuously until a timer runs out.
119 ///
120 /// # Arguments
121 ///
122 /// * `time` - The time limit in seconds.
123 /// * `node` - The child node to execute.
124 ///
125 /// # Returns
126 ///
127 /// * A new countdown node of type `Leaf`.
128 pub fn countdown(time: f64, node: &crate::dora::platformer::behavior::Tree) -> crate::dora::platformer::behavior::Tree {
129 unsafe { return crate::dora::platformer::behavior::Tree::from(platformer_behavior_leaf_countdown(time, node.raw())).unwrap(); }
130 }
131 /// Creates a new timeout node that executes a child node until a timer runs out.
132 ///
133 /// # Arguments
134 ///
135 /// * `time` - The time limit in seconds.
136 /// * `node` - The child node to execute.
137 ///
138 /// # Returns
139 ///
140 /// * A new timeout node of type `Leaf`.
141 pub fn timeout(time: f64, node: &crate::dora::platformer::behavior::Tree) -> crate::dora::platformer::behavior::Tree {
142 unsafe { return crate::dora::platformer::behavior::Tree::from(platformer_behavior_leaf_timeout(time, node.raw())).unwrap(); }
143 }
144 /// Creates a new repeat node that executes a child node a specified number of times.
145 ///
146 /// # Arguments
147 ///
148 /// * `times` - The number of times to execute the child node.
149 /// * `node` - The child node to execute.
150 ///
151 /// # Returns
152 ///
153 /// * A new repeat node of type `Leaf`.
154 pub fn repeat(times: i32, node: &crate::dora::platformer::behavior::Tree) -> crate::dora::platformer::behavior::Tree {
155 unsafe { return crate::dora::platformer::behavior::Tree::from(platformer_behavior_leaf_repeat(times, node.raw())).unwrap(); }
156 }
157 /// Creates a new repeat node that executes a child node repeatedly.
158 ///
159 /// # Arguments
160 ///
161 /// * `node` - The child node to execute.
162 ///
163 /// # Returns
164 ///
165 /// * A new repeat node of type `Leaf`.
166 pub fn repeat_forever(node: &crate::dora::platformer::behavior::Tree) -> crate::dora::platformer::behavior::Tree {
167 unsafe { return crate::dora::platformer::behavior::Tree::from(platformer_behavior_leaf_repeat_forever(node.raw())).unwrap(); }
168 }
169 /// Creates a new retry node that executes a child node repeatedly until it succeeds or a maximum number of retries is reached.
170 ///
171 /// # Arguments
172 ///
173 /// * `times` - The maximum number of retries.
174 /// * `node` - The child node to execute.
175 ///
176 /// # Returns
177 ///
178 /// * A new retry node of type `Leaf`.
179 pub fn retry(times: i32, node: &crate::dora::platformer::behavior::Tree) -> crate::dora::platformer::behavior::Tree {
180 unsafe { return crate::dora::platformer::behavior::Tree::from(platformer_behavior_leaf_retry(times, node.raw())).unwrap(); }
181 }
182 /// Creates a new retry node that executes a child node repeatedly until it succeeds.
183 ///
184 /// # Arguments
185 ///
186 /// * `node` - The child node to execute.
187 ///
188 /// # Returns
189 ///
190 /// * A new retry node of type `Leaf`.
191 pub fn retry_until_pass(node: &crate::dora::platformer::behavior::Tree) -> crate::dora::platformer::behavior::Tree {
192 unsafe { return crate::dora::platformer::behavior::Tree::from(platformer_behavior_leaf_retry_until_pass(node.raw())).unwrap(); }
193 }
194}