Debian Codegraph @

scene/3d/navigation/navigation_agent_3d.cpp

raw symbols SCIP
1 /**************************************************************************/
2 /* navigation_agent_3d.cpp */
3 /**************************************************************************/
4 /* This file is part of: */
5 /* GODOT ENGINE */
6 /* https://godotengine.org */
7 /**************************************************************************/
8 /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9 /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10 /* */
11 /* Permission is hereby granted, free of charge, to any person obtaining */
12 /* a copy of this software and associated documentation files (the */
13 /* "Software"), to deal in the Software without restriction, including */
14 /* without limitation the rights to use, copy, modify, merge, publish, */
15 /* distribute, sublicense, and/or sell copies of the Software, and to */
16 /* permit persons to whom the Software is furnished to do so, subject to */
17 /* the following conditions: */
18 /* */
19 /* The above copyright notice and this permission notice shall be */
20 /* included in all copies or substantial portions of the Software. */
21 /* */
22 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23 /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24 /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25 /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26 /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27 /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28 /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29 /**************************************************************************/
30
31 #include "navigation_agent_3d.h"
32
33 #include "scene/3d/navigation/navigation_link_3d.h"
34 #include "servers/navigation_3d/navigation_server_3d.h"
35
36 void NavigationAgent3D::_bind_methods() {
37 ClassDB::bind_method(D_METHOD("get_rid"), &NavigationAgent3D::get_rid);
38
39 ClassDB::bind_method(D_METHOD("set_avoidance_enabled", "enabled"), &NavigationAgent3D::set_avoidance_enabled);
40 ClassDB::bind_method(D_METHOD("get_avoidance_enabled"), &NavigationAgent3D::get_avoidance_enabled);
41
42 ClassDB::bind_method(D_METHOD("set_path_desired_distance", "desired_distance"), &NavigationAgent3D::set_path_desired_distance);
43 ClassDB::bind_method(D_METHOD("get_path_desired_distance"), &NavigationAgent3D::get_path_desired_distance);
44
45 ClassDB::bind_method(D_METHOD("set_target_desired_distance", "desired_distance"), &NavigationAgent3D::set_target_desired_distance);
46 ClassDB::bind_method(D_METHOD("get_target_desired_distance"), &NavigationAgent3D::get_target_desired_distance);
47
48 ClassDB::bind_method(D_METHOD("set_radius", "radius"), &NavigationAgent3D::set_radius);
49 ClassDB::bind_method(D_METHOD("get_radius"), &NavigationAgent3D::get_radius);
50
51 ClassDB::bind_method(D_METHOD("set_height", "height"), &NavigationAgent3D::set_height);
52 ClassDB::bind_method(D_METHOD("get_height"), &NavigationAgent3D::get_height);
53
54 ClassDB::bind_method(D_METHOD("set_path_height_offset", "path_height_offset"), &NavigationAgent3D::set_path_height_offset);
55 ClassDB::bind_method(D_METHOD("get_path_height_offset"), &NavigationAgent3D::get_path_height_offset);
56
57 ClassDB::bind_method(D_METHOD("set_use_3d_avoidance", "enabled"), &NavigationAgent3D::set_use_3d_avoidance);
58 ClassDB::bind_method(D_METHOD("get_use_3d_avoidance"), &NavigationAgent3D::get_use_3d_avoidance);
59
60 ClassDB::bind_method(D_METHOD("set_keep_y_velocity", "enabled"), &NavigationAgent3D::set_keep_y_velocity);
61 ClassDB::bind_method(D_METHOD("get_keep_y_velocity"), &NavigationAgent3D::get_keep_y_velocity);
62
63 ClassDB::bind_method(D_METHOD("set_neighbor_distance", "neighbor_distance"), &NavigationAgent3D::set_neighbor_distance);
64 ClassDB::bind_method(D_METHOD("get_neighbor_distance"), &NavigationAgent3D::get_neighbor_distance);
65
66 ClassDB::bind_method(D_METHOD("set_max_neighbors", "max_neighbors"), &NavigationAgent3D::set_max_neighbors);
67 ClassDB::bind_method(D_METHOD("get_max_neighbors"), &NavigationAgent3D::get_max_neighbors);
68
69 ClassDB::bind_method(D_METHOD("set_time_horizon_agents", "time_horizon"), &NavigationAgent3D::set_time_horizon_agents);
70 ClassDB::bind_method(D_METHOD("get_time_horizon_agents"), &NavigationAgent3D::get_time_horizon_agents);
71
72 ClassDB::bind_method(D_METHOD("set_time_horizon_obstacles", "time_horizon"), &NavigationAgent3D::set_time_horizon_obstacles);
73 ClassDB::bind_method(D_METHOD("get_time_horizon_obstacles"), &NavigationAgent3D::get_time_horizon_obstacles);
74
75 ClassDB::bind_method(D_METHOD("set_max_speed", "max_speed"), &NavigationAgent3D::set_max_speed);
76 ClassDB::bind_method(D_METHOD("get_max_speed"), &NavigationAgent3D::get_max_speed);
77
78 ClassDB::bind_method(D_METHOD("set_path_max_distance", "max_speed"), &NavigationAgent3D::set_path_max_distance);
79 ClassDB::bind_method(D_METHOD("get_path_max_distance"), &NavigationAgent3D::get_path_max_distance);
80
81 ClassDB::bind_method(D_METHOD("set_navigation_layers", "navigation_layers"), &NavigationAgent3D::set_navigation_layers);
82 ClassDB::bind_method(D_METHOD("get_navigation_layers"), &NavigationAgent3D::get_navigation_layers);
83
84 ClassDB::bind_method(D_METHOD("set_navigation_layer_value", "layer_number", "value"), &NavigationAgent3D::set_navigation_layer_value);
85 ClassDB::bind_method(D_METHOD("get_navigation_layer_value", "layer_number"), &NavigationAgent3D::get_navigation_layer_value);
86
87 ClassDB::bind_method(D_METHOD("set_pathfinding_algorithm", "pathfinding_algorithm"), &NavigationAgent3D::set_pathfinding_algorithm);
88 ClassDB::bind_method(D_METHOD("get_pathfinding_algorithm"), &NavigationAgent3D::get_pathfinding_algorithm);
89
90 ClassDB::bind_method(D_METHOD("set_path_postprocessing", "path_postprocessing"), &NavigationAgent3D::set_path_postprocessing);
91 ClassDB::bind_method(D_METHOD("get_path_postprocessing"), &NavigationAgent3D::get_path_postprocessing);
92
93 ClassDB::bind_method(D_METHOD("set_path_metadata_flags", "flags"), &NavigationAgent3D::set_path_metadata_flags);
94 ClassDB::bind_method(D_METHOD("get_path_metadata_flags"), &NavigationAgent3D::get_path_metadata_flags);
95
96 ClassDB::bind_method(D_METHOD("set_navigation_map", "navigation_map"), &NavigationAgent3D::set_navigation_map);
97 ClassDB::bind_method(D_METHOD("get_navigation_map"), &NavigationAgent3D::get_navigation_map);
98
99 ClassDB::bind_method(D_METHOD("set_target_position", "position"), &NavigationAgent3D::set_target_position);
100 ClassDB::bind_method(D_METHOD("get_target_position"), &NavigationAgent3D::get_target_position);
101
102 ClassDB::bind_method(D_METHOD("set_simplify_path", "enabled"), &NavigationAgent3D::set_simplify_path);
103 ClassDB::bind_method(D_METHOD("get_simplify_path"), &NavigationAgent3D::get_simplify_path);
104
105 ClassDB::bind_method(D_METHOD("set_simplify_epsilon", "epsilon"), &NavigationAgent3D::set_simplify_epsilon);
106 ClassDB::bind_method(D_METHOD("get_simplify_epsilon"), &NavigationAgent3D::get_simplify_epsilon);
107
108 ClassDB::bind_method(D_METHOD("set_path_return_max_length", "length"), &NavigationAgent3D::set_path_return_max_length);
109 ClassDB::bind_method(D_METHOD("get_path_return_max_length"), &NavigationAgent3D::get_path_return_max_length);
110
111 ClassDB::bind_method(D_METHOD("set_path_return_max_radius", "radius"), &NavigationAgent3D::set_path_return_max_radius);
112 ClassDB::bind_method(D_METHOD("get_path_return_max_radius"), &NavigationAgent3D::get_path_return_max_radius);
113
114 ClassDB::bind_method(D_METHOD("set_path_search_max_polygons", "max_polygons"), &NavigationAgent3D::set_path_search_max_polygons);
115 ClassDB::bind_method(D_METHOD("get_path_search_max_polygons"), &NavigationAgent3D::get_path_search_max_polygons);
116
117 ClassDB::bind_method(D_METHOD("set_path_search_max_distance", "distance"), &NavigationAgent3D::set_path_search_max_distance);
118 ClassDB::bind_method(D_METHOD("get_path_search_max_distance"), &NavigationAgent3D::get_path_search_max_distance);
119
120 ClassDB::bind_method(D_METHOD("get_path_length"), &NavigationAgent3D::get_path_length);
121
122 ClassDB::bind_method(D_METHOD("get_next_path_position"), &NavigationAgent3D::get_next_path_position);
123
124 ClassDB::bind_method(D_METHOD("set_velocity_forced", "velocity"), &NavigationAgent3D::set_velocity_forced);
125
126 ClassDB::bind_method(D_METHOD("set_velocity", "velocity"), &NavigationAgent3D::set_velocity);
127 ClassDB::bind_method(D_METHOD("get_velocity"), &NavigationAgent3D::get_velocity);
128
129 ClassDB::bind_method(D_METHOD("distance_to_target"), &NavigationAgent3D::distance_to_target);
130
131 ClassDB::bind_method(D_METHOD("get_current_navigation_result"), &NavigationAgent3D::get_current_navigation_result);
132
133 ClassDB::bind_method(D_METHOD("get_current_navigation_path"), &NavigationAgent3D::get_current_navigation_path);
134 ClassDB::bind_method(D_METHOD("get_current_navigation_path_index"), &NavigationAgent3D::get_current_navigation_path_index);
135 ClassDB::bind_method(D_METHOD("is_target_reached"), &NavigationAgent3D::is_target_reached);
136 ClassDB::bind_method(D_METHOD("is_target_reachable"), &NavigationAgent3D::is_target_reachable);
137 ClassDB::bind_method(D_METHOD("is_navigation_finished"), &NavigationAgent3D::is_navigation_finished);
138 ClassDB::bind_method(D_METHOD("get_final_position"), &NavigationAgent3D::get_final_position);
139
140 ClassDB::bind_method(D_METHOD("_avoidance_done", "new_velocity"), &NavigationAgent3D::_avoidance_done);
141
142 ClassDB::bind_method(D_METHOD("set_avoidance_layers", "layers"), &NavigationAgent3D::set_avoidance_layers);
143 ClassDB::bind_method(D_METHOD("get_avoidance_layers"), &NavigationAgent3D::get_avoidance_layers);
144 ClassDB::bind_method(D_METHOD("set_avoidance_mask", "mask"), &NavigationAgent3D::set_avoidance_mask);
145 ClassDB::bind_method(D_METHOD("get_avoidance_mask"), &NavigationAgent3D::get_avoidance_mask);
146 ClassDB::bind_method(D_METHOD("set_avoidance_layer_value", "layer_number", "value"), &NavigationAgent3D::set_avoidance_layer_value);
147 ClassDB::bind_method(D_METHOD("get_avoidance_layer_value", "layer_number"), &NavigationAgent3D::get_avoidance_layer_value);
148 ClassDB::bind_method(D_METHOD("set_avoidance_mask_value", "mask_number", "value"), &NavigationAgent3D::set_avoidance_mask_value);
149 ClassDB::bind_method(D_METHOD("get_avoidance_mask_value", "mask_number"), &NavigationAgent3D::get_avoidance_mask_value);
150 ClassDB::bind_method(D_METHOD("set_avoidance_priority", "priority"), &NavigationAgent3D::set_avoidance_priority);
151 ClassDB::bind_method(D_METHOD("get_avoidance_priority"), &NavigationAgent3D::get_avoidance_priority);
152
153 ADD_GROUP("Pathfinding", "");
154 ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "target_position", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR), "set_target_position", "get_target_position");
155 ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "path_desired_distance", PROPERTY_HINT_RANGE, "0.1,100,0.01,or_greater,suffix:m"), "set_path_desired_distance", "get_path_desired_distance");
156 ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "target_desired_distance", PROPERTY_HINT_RANGE, "0.1,100,0.01,or_greater,suffix:m"), "set_target_desired_distance", "get_target_desired_distance");
157 ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "path_height_offset", PROPERTY_HINT_RANGE, "-100.0,100,0.01,or_greater,suffix:m"), "set_path_height_offset", "get_path_height_offset");
158 ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "path_max_distance", PROPERTY_HINT_RANGE, "0.01,100,0.1,or_greater,suffix:m"), "set_path_max_distance", "get_path_max_distance");
159 ADD_PROPERTY(PropertyInfo(Variant::INT, "navigation_layers", PROPERTY_HINT_LAYERS_3D_NAVIGATION), "set_navigation_layers", "get_navigation_layers");
160 ADD_PROPERTY(PropertyInfo(Variant::INT, "pathfinding_algorithm", PROPERTY_HINT_ENUM, "AStar"), "set_pathfinding_algorithm", "get_pathfinding_algorithm");
161 ADD_PROPERTY(PropertyInfo(Variant::INT, "path_postprocessing", PROPERTY_HINT_ENUM, "Corridorfunnel,Edgecentered,None"), "set_path_postprocessing", "get_path_postprocessing");
162 ADD_PROPERTY(PropertyInfo(Variant::INT, "path_metadata_flags", PROPERTY_HINT_FLAGS, "Include Types,Include RIDs,Include Owners"), "set_path_metadata_flags", "get_path_metadata_flags");
163 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "simplify_path"), "set_simplify_path", "get_simplify_path");
164 ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "simplify_epsilon", PROPERTY_HINT_RANGE, "0.0,10.0,0.001,or_greater,suffix:m"), "set_simplify_epsilon", "get_simplify_epsilon");
165 ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "path_return_max_length", PROPERTY_HINT_RANGE, "0.0,100.0,0.001,or_greater,suffix:m"), "set_path_return_max_length", "get_path_return_max_length");
166 ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "path_return_max_radius", PROPERTY_HINT_RANGE, "0.0,100.0,0.001,or_greater,suffix:m"), "set_path_return_max_radius", "get_path_return_max_radius");
167 ADD_PROPERTY(PropertyInfo(Variant::INT, "path_search_max_polygons", PROPERTY_HINT_RANGE, "0,4096,1,or_greater"), "set_path_search_max_polygons", "get_path_search_max_polygons");
168 ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "path_search_max_distance"), "set_path_search_max_distance", "get_path_search_max_distance");
169
170 ADD_GROUP("Avoidance", "");
171 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "avoidance_enabled", PROPERTY_HINT_GROUP_ENABLE), "set_avoidance_enabled", "get_avoidance_enabled");
172 ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "velocity", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR), "set_velocity", "get_velocity");
173 ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "height", PROPERTY_HINT_RANGE, "0.01,100,0.01,or_greater,suffix:m"), "set_height", "get_height");
174 ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "radius", PROPERTY_HINT_RANGE, "0.01,100,0.01,or_greater,suffix:m"), "set_radius", "get_radius");
175 ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "neighbor_distance", PROPERTY_HINT_RANGE, "0.1,10000,0.01,or_greater,suffix:m"), "set_neighbor_distance", "get_neighbor_distance");
176 ADD_PROPERTY(PropertyInfo(Variant::INT, "max_neighbors", PROPERTY_HINT_RANGE, "1,10000,1,or_greater"), "set_max_neighbors", "get_max_neighbors");
177 ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "time_horizon_agents", PROPERTY_HINT_RANGE, "0.0,10,0.01,or_greater,suffix:s"), "set_time_horizon_agents", "get_time_horizon_agents");
178 ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "time_horizon_obstacles", PROPERTY_HINT_RANGE, "0.0,10,0.01,or_greater,suffix:s"), "set_time_horizon_obstacles", "get_time_horizon_obstacles");
179 ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "max_speed", PROPERTY_HINT_RANGE, "0.01,10000,0.01,or_greater,suffix:m/s"), "set_max_speed", "get_max_speed");
180 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "use_3d_avoidance"), "set_use_3d_avoidance", "get_use_3d_avoidance");
181 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "keep_y_velocity"), "set_keep_y_velocity", "get_keep_y_velocity");
182 ADD_PROPERTY(PropertyInfo(Variant::INT, "avoidance_layers", PROPERTY_HINT_LAYERS_AVOIDANCE), "set_avoidance_layers", "get_avoidance_layers");
183 ADD_PROPERTY(PropertyInfo(Variant::INT, "avoidance_mask", PROPERTY_HINT_LAYERS_AVOIDANCE), "set_avoidance_mask", "get_avoidance_mask");
184 ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "avoidance_priority", PROPERTY_HINT_RANGE, "0.0,1.0,0.01"), "set_avoidance_priority", "get_avoidance_priority");
185
186 ADD_SIGNAL(MethodInfo("path_changed"));
187 ADD_SIGNAL(MethodInfo("target_reached"));
188 ADD_SIGNAL(MethodInfo("waypoint_reached", PropertyInfo(Variant::DICTIONARY, "details")));
189 ADD_SIGNAL(MethodInfo("link_reached", PropertyInfo(Variant::DICTIONARY, "details")));
190 ADD_SIGNAL(MethodInfo("navigation_finished"));
191 ADD_SIGNAL(MethodInfo("velocity_computed", PropertyInfo(Variant::VECTOR3, "safe_velocity")));
192
193 ClassDB::bind_method(D_METHOD("set_debug_enabled", "enabled"), &NavigationAgent3D::set_debug_enabled);
194 ClassDB::bind_method(D_METHOD("get_debug_enabled"), &NavigationAgent3D::get_debug_enabled);
195 ClassDB::bind_method(D_METHOD("set_debug_use_custom", "enabled"), &NavigationAgent3D::set_debug_use_custom);
196 ClassDB::bind_method(D_METHOD("get_debug_use_custom"), &NavigationAgent3D::get_debug_use_custom);
197 ClassDB::bind_method(D_METHOD("set_debug_path_custom_color", "color"), &NavigationAgent3D::set_debug_path_custom_color);
198 ClassDB::bind_method(D_METHOD("get_debug_path_custom_color"), &NavigationAgent3D::get_debug_path_custom_color);
199 ClassDB::bind_method(D_METHOD("set_debug_path_custom_point_size", "point_size"), &NavigationAgent3D::set_debug_path_custom_point_size);
200 ClassDB::bind_method(D_METHOD("get_debug_path_custom_point_size"), &NavigationAgent3D::get_debug_path_custom_point_size);
201
202 ADD_GROUP("Debug", "debug_");
203 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "debug_enabled", PROPERTY_HINT_GROUP_ENABLE), "set_debug_enabled", "get_debug_enabled");
204 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "debug_use_custom"), "set_debug_use_custom", "get_debug_use_custom");
205 ADD_PROPERTY(PropertyInfo(Variant::COLOR, "debug_path_custom_color"), "set_debug_path_custom_color", "get_debug_path_custom_color");
206 ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "debug_path_custom_point_size", PROPERTY_HINT_RANGE, "0,50,0.01,or_greater,suffix:px"), "set_debug_path_custom_point_size", "get_debug_path_custom_point_size");
207 }
208
209 #ifndef DISABLE_DEPRECATED
210 // Compatibility with Godot 4.0 beta 10 or below.
211 // Functions in block below all renamed or replaced in 4.0 beta 1X avoidance rework.
212 bool NavigationAgent3D::_set(const StringName &p_name, const Variant &p_value) {
213 if (p_name == "time_horizon") {
214 set_time_horizon_agents(p_value);
215 return true;
216 }
217 if (p_name == "target_location") {
218 set_target_position(p_value);
219 return true;
220 }
221 if (p_name == "agent_height_offset") {
222 set_path_height_offset(p_value);
223 return true;
224 }
225 return false;
226 }
227
228 bool NavigationAgent3D::_get(const StringName &p_name, Variant &r_ret) const {
229 if (p_name == "time_horizon") {
230 r_ret = get_time_horizon_agents();
231 return true;
232 }
233 if (p_name == "target_location") {
234 r_ret = get_target_position();
235 return true;
236 }
237 if (p_name == "agent_height_offset") {
238 r_ret = get_path_height_offset();
239 return true;
240 }
241 return false;
242 }
243 #endif // DISABLE_DEPRECATED
244
245 void NavigationAgent3D::_notification(int p_what) {
246 switch (p_what) {
247 case NOTIFICATION_POST_ENTER_TREE: {
248 // need to use POST_ENTER_TREE cause with normal ENTER_TREE not all required Nodes are ready.
249 // cannot use READY as ready does not get called if Node is re-added to SceneTree
250 set_agent_parent(get_parent());
251 set_physics_process_internal(true);
252
253 if (agent_parent && avoidance_enabled) {
254 NavigationServer3D::get_singleton()->agent_set_position(agent, agent_parent->get_global_transform().origin);
255 }
256
257 #ifdef DEBUG_ENABLED
258 if (NavigationServer3D::get_singleton()->get_debug_enabled()) {
259 debug_path_dirty = true;
260 }
261 #endif // DEBUG_ENABLED
262
263 } break;
264
265 case NOTIFICATION_PARENTED: {
266 if (is_inside_tree() && (get_parent() != agent_parent)) {
267 // only react to PARENTED notifications when already inside_tree and parent changed, e.g. users switch nodes around
268 // PARENTED notification fires also when Node is added in scripts to a parent
269 // this would spam transforms fails and world fails while Node is outside SceneTree
270 // when node gets reparented when joining the tree POST_ENTER_TREE takes care of this
271 set_agent_parent(get_parent());
272 set_physics_process_internal(true);
273 }
274 } break;
275
276 case NOTIFICATION_UNPARENTED: {
277 // if agent has no parent no point in processing it until reparented
278 set_agent_parent(nullptr);
279 set_physics_process_internal(false);
280 } break;
281
282 case NOTIFICATION_EXIT_TREE: {
283 set_agent_parent(nullptr);
284 set_physics_process_internal(false);
285
286 #ifdef DEBUG_ENABLED
287 if (debug_path_instance.is_valid()) {
288 RS::get_singleton()->instance_set_visible(debug_path_instance, false);
289 }
290 #endif // DEBUG_ENABLED
291 } break;
292
293 case NOTIFICATION_SUSPENDED:
294 case NOTIFICATION_PAUSED: {
295 if (agent_parent) {
296 NavigationServer3D::get_singleton()->agent_set_paused(get_rid(), !agent_parent->can_process());
297 }
298 } break;
299
300 case NOTIFICATION_UNSUSPENDED: {
301 if (get_tree()->is_paused()) {
302 break;
303 }
304 [[fallthrough]];
305 }
306
307 case NOTIFICATION_UNPAUSED: {
308 if (agent_parent) {
309 NavigationServer3D::get_singleton()->agent_set_paused(get_rid(), !agent_parent->can_process());
310 }
311 } break;
312
313 case NOTIFICATION_INTERNAL_PHYSICS_PROCESS: {
314 if (agent_parent && avoidance_enabled) {
315 NavigationServer3D::get_singleton()->agent_set_position(agent, agent_parent->get_global_position());
316 }
317 if (agent_parent && target_position_submitted) {
318 if (velocity_submitted) {
319 velocity_submitted = false;
320 if (avoidance_enabled) {
321 if (!use_3d_avoidance) {
322 if (keep_y_velocity) {
323 stored_y_velocity = velocity.y;
324 }
325 velocity.y = 0.0;
326 }
327 NavigationServer3D::get_singleton()->agent_set_velocity(agent, velocity);
328 }
329 }
330 if (velocity_forced_submitted) {
331 velocity_forced_submitted = false;
332 if (avoidance_enabled) {
333 NavigationServer3D::get_singleton()->agent_set_velocity_forced(agent, velocity_forced);
334 }
335 }
336 }
337 #ifdef DEBUG_ENABLED
338 if (debug_path_dirty) {
339 _update_debug_path();
340 }
341 #endif // DEBUG_ENABLED
342 } break;
343 }
344 }
345
346 void NavigationAgent3D::_validate_property(PropertyInfo &p_property) const {
347 if (p_property.name == "keep_y_velocity" && use_3d_avoidance) {
348 p_property.usage = PROPERTY_USAGE_NONE;
349 return;
350 }
351 }
352
353 NavigationAgent3D::NavigationAgent3D() {
354 agent = NavigationServer3D::get_singleton()->agent_create();
355
356 NavigationServer3D::get_singleton()->agent_set_neighbor_distance(agent, neighbor_distance);
357 NavigationServer3D::get_singleton()->agent_set_max_neighbors(agent, max_neighbors);
358 NavigationServer3D::get_singleton()->agent_set_time_horizon_agents(agent, time_horizon_agents);
359 NavigationServer3D::get_singleton()->agent_set_time_horizon_obstacles(agent, time_horizon_obstacles);
360 NavigationServer3D::get_singleton()->agent_set_radius(agent, radius);
361 NavigationServer3D::get_singleton()->agent_set_height(agent, height);
362 NavigationServer3D::get_singleton()->agent_set_max_speed(agent, max_speed);
363 NavigationServer3D::get_singleton()->agent_set_avoidance_layers(agent, avoidance_layers);
364 NavigationServer3D::get_singleton()->agent_set_avoidance_mask(agent, avoidance_mask);
365 NavigationServer3D::get_singleton()->agent_set_avoidance_priority(agent, avoidance_priority);
366 NavigationServer3D::get_singleton()->agent_set_use_3d_avoidance(agent, use_3d_avoidance);
367 NavigationServer3D::get_singleton()->agent_set_avoidance_enabled(agent, avoidance_enabled);
368 if (avoidance_enabled) {
369 NavigationServer3D::get_singleton()->agent_set_avoidance_callback(agent, callable_mp(this, &NavigationAgent3D::_avoidance_done));
370 }
371
372 // Preallocate query and result objects to improve performance.
373 navigation_query = Ref<NavigationPathQueryParameters3D>();
374 navigation_query.instantiate();
375
376 navigation_result = Ref<NavigationPathQueryResult3D>();
377 navigation_result.instantiate();
378
379 #ifdef DEBUG_ENABLED
380 NavigationServer3D::get_singleton()->connect(SNAME("navigation_debug_changed"), callable_mp(this, &NavigationAgent3D::_navigation_debug_changed));
381 #endif // DEBUG_ENABLED
382 }
383
384 NavigationAgent3D::~NavigationAgent3D() {
385 ERR_FAIL_NULL(NavigationServer3D::get_singleton());
386 NavigationServer3D::get_singleton()->free_rid(agent);
387 agent = RID(); // Pointless
388
389 #ifdef DEBUG_ENABLED
390 NavigationServer3D::get_singleton()->disconnect(SNAME("navigation_debug_changed"), callable_mp(this, &NavigationAgent3D::_navigation_debug_changed));
391
392 ERR_FAIL_NULL(RenderingServer::get_singleton());
393 if (debug_path_instance.is_valid()) {
394 RenderingServer::get_singleton()->free_rid(debug_path_instance);
395 }
396 if (debug_path_mesh.is_valid()) {
397 RenderingServer::get_singleton()->free_rid(debug_path_mesh->get_rid());
398 }
399 #endif // DEBUG_ENABLED
400 }
401
402 void NavigationAgent3D::set_avoidance_enabled(bool p_enabled) {
403 if (avoidance_enabled == p_enabled) {
404 return;
405 }
406
407 avoidance_enabled = p_enabled;
408
409 if (avoidance_enabled) {
410 NavigationServer3D::get_singleton()->agent_set_avoidance_enabled(agent, true);
411 NavigationServer3D::get_singleton()->agent_set_avoidance_callback(agent, callable_mp(this, &NavigationAgent3D::_avoidance_done));
412 } else {
413 NavigationServer3D::get_singleton()->agent_set_avoidance_enabled(agent, false);
414 NavigationServer3D::get_singleton()->agent_set_avoidance_callback(agent, Callable());
415 }
416 }
417
418 bool NavigationAgent3D::get_avoidance_enabled() const {
419 return avoidance_enabled;
420 }
421
422 void NavigationAgent3D::set_agent_parent(Node *p_agent_parent) {
423 if (agent_parent == p_agent_parent) {
424 return;
425 }
426
427 // remove agent from any avoidance map before changing parent or there will be leftovers on the RVO map
428 NavigationServer3D::get_singleton()->agent_set_avoidance_callback(agent, Callable());
429
430 if (Object::cast_to<Node3D>(p_agent_parent) != nullptr) {
431 // place agent on navigation map first or else the RVO agent callback creation fails silently later
432 agent_parent = Object::cast_to<Node3D>(p_agent_parent);
433 if (map_override.is_valid()) {
434 NavigationServer3D::get_singleton()->agent_set_map(get_rid(), map_override);
435 } else {
436 NavigationServer3D::get_singleton()->agent_set_map(get_rid(), agent_parent->get_world_3d()->get_navigation_map());
437 }
438
439 // create new avoidance callback if enabled
440 if (avoidance_enabled) {
441 NavigationServer3D::get_singleton()->agent_set_avoidance_callback(agent, callable_mp(this, &NavigationAgent3D::_avoidance_done));
442 }
443 } else {
444 agent_parent = nullptr;
445 NavigationServer3D::get_singleton()->agent_set_map(get_rid(), RID());
446 }
447 }
448
449 void NavigationAgent3D::set_navigation_layers(uint32_t p_navigation_layers) {
450 if (navigation_layers == p_navigation_layers) {
451 return;
452 }
453
454 navigation_layers = p_navigation_layers;
455
456 if (target_position_submitted) {
457 _request_repath();
458 }
459 }
460
461 uint32_t NavigationAgent3D::get_navigation_layers() const {
462 return navigation_layers;
463 }
464
465 void NavigationAgent3D::set_navigation_layer_value(int p_layer_number, bool p_value) {
466 ERR_FAIL_COND_MSG(p_layer_number < 1, "Navigation layer number must be between 1 and 32 inclusive.");
467 ERR_FAIL_COND_MSG(p_layer_number > 32, "Navigation layer number must be between 1 and 32 inclusive.");
468 uint32_t _navigation_layers = get_navigation_layers();
469 if (p_value) {
470 _navigation_layers |= 1 << (p_layer_number - 1);
471 } else {
472 _navigation_layers &= ~(1 << (p_layer_number - 1));
473 }
474 set_navigation_layers(_navigation_layers);
475 }
476
477 bool NavigationAgent3D::get_navigation_layer_value(int p_layer_number) const {
478 ERR_FAIL_COND_V_MSG(p_layer_number < 1, false, "Navigation layer number must be between 1 and 32 inclusive.");
479 ERR_FAIL_COND_V_MSG(p_layer_number > 32, false, "Navigation layer number must be between 1 and 32 inclusive.");
480 return get_navigation_layers() & (1 << (p_layer_number - 1));
481 }
482
483 void NavigationAgent3D::set_pathfinding_algorithm(const NavigationPathQueryParameters3D::PathfindingAlgorithm p_pathfinding_algorithm) {
484 if (pathfinding_algorithm == p_pathfinding_algorithm) {
485 return;
486 }
487
488 pathfinding_algorithm = p_pathfinding_algorithm;
489
490 navigation_query->set_pathfinding_algorithm(pathfinding_algorithm);
491 }
492
493 void NavigationAgent3D::set_path_postprocessing(const NavigationPathQueryParameters3D::PathPostProcessing p_path_postprocessing) {
494 if (path_postprocessing == p_path_postprocessing) {
495 return;
496 }
497
498 path_postprocessing = p_path_postprocessing;
499
500 navigation_query->set_path_postprocessing(path_postprocessing);
501 }
502
503 void NavigationAgent3D::set_simplify_path(bool p_enabled) {
504 simplify_path = p_enabled;
505 navigation_query->set_simplify_path(simplify_path);
506 }
507
508 bool NavigationAgent3D::get_simplify_path() const {
509 return simplify_path;
510 }
511
512 void NavigationAgent3D::set_simplify_epsilon(real_t p_epsilon) {
513 simplify_epsilon = MAX(0.0, p_epsilon);
514 navigation_query->set_simplify_epsilon(simplify_epsilon);
515 }
516
517 real_t NavigationAgent3D::get_simplify_epsilon() const {
518 return simplify_epsilon;
519 }
520
521 void NavigationAgent3D::set_path_return_max_length(float p_length) {
522 path_return_max_length = MAX(0.0, p_length);
523 navigation_query->set_path_return_max_length(path_return_max_length);
524 }
525
526 float NavigationAgent3D::get_path_return_max_length() const {
527 return path_return_max_length;
528 }
529
530 void NavigationAgent3D::set_path_return_max_radius(float p_radius) {
531 path_return_max_radius = MAX(0.0, p_radius);
532 navigation_query->set_path_return_max_radius(path_return_max_radius);
533 }
534
535 float NavigationAgent3D::get_path_return_max_radius() const {
536 return path_return_max_radius;
537 }
538
539 void NavigationAgent3D::set_path_search_max_polygons(int p_max_polygons) {
540 path_search_max_polygons = p_max_polygons;
541 navigation_query->set_path_search_max_polygons(path_search_max_polygons);
542 }
543
544 int NavigationAgent3D::get_path_search_max_polygons() const {
545 return path_search_max_polygons;
546 }
547
548 void NavigationAgent3D::set_path_search_max_distance(float p_distance) {
549 path_search_max_distance = MAX(0.0, p_distance);
550 navigation_query->set_path_search_max_distance(path_search_max_distance);
551 }
552
553 float NavigationAgent3D::get_path_search_max_distance() const {
554 return path_search_max_distance;
555 }
556
557 float NavigationAgent3D::get_path_length() const {
558 return navigation_result->get_path_length();
559 }
560
561 void NavigationAgent3D::set_path_metadata_flags(BitField<NavigationPathQueryParameters3D::PathMetadataFlags> p_path_metadata_flags) {
562 if (path_metadata_flags == p_path_metadata_flags) {
563 return;
564 }
565
566 path_metadata_flags = p_path_metadata_flags;
567 }
568
569 void NavigationAgent3D::set_navigation_map(RID p_navigation_map) {
570 if (map_override == p_navigation_map) {
571 return;
572 }
573
574 map_override = p_navigation_map;
575
576 NavigationServer3D::get_singleton()->agent_set_map(agent, map_override);
577 if (target_position_submitted) {
578 _request_repath();
579 }
580 }
581
582 RID NavigationAgent3D::get_navigation_map() const {
583 if (map_override.is_valid()) {
584 return map_override;
585 } else if (agent_parent != nullptr) {
586 return agent_parent->get_world_3d()->get_navigation_map();
587 }
588 return RID();
589 }
590
591 void NavigationAgent3D::set_path_desired_distance(real_t p_path_desired_distance) {
592 if (Math::is_equal_approx(path_desired_distance, p_path_desired_distance)) {
593 return;
594 }
595
596 path_desired_distance = p_path_desired_distance;
597 }
598
599 void NavigationAgent3D::set_target_desired_distance(real_t p_target_desired_distance) {
600 if (Math::is_equal_approx(target_desired_distance, p_target_desired_distance)) {
601 return;
602 }
603
604 target_desired_distance = p_target_desired_distance;
605 }
606
607 void NavigationAgent3D::set_radius(real_t p_radius) {
608 ERR_FAIL_COND_MSG(p_radius < 0.0, "Radius must be positive.");
609 if (Math::is_equal_approx(radius, p_radius)) {
610 return;
611 }
612 radius = p_radius;
613
614 NavigationServer3D::get_singleton()->agent_set_radius(agent, radius);
615 }
616
617 void NavigationAgent3D::set_height(real_t p_height) {
618 ERR_FAIL_COND_MSG(p_height < 0.0, "Height must be positive.");
619 if (Math::is_equal_approx(height, p_height)) {
620 return;
621 }
622 height = p_height;
623 NavigationServer3D::get_singleton()->agent_set_height(agent, height);
624 }
625
626 void NavigationAgent3D::set_path_height_offset(real_t p_path_height_offset) {
627 path_height_offset = p_path_height_offset;
628 }
629
630 void NavigationAgent3D::set_use_3d_avoidance(bool p_use_3d_avoidance) {
631 use_3d_avoidance = p_use_3d_avoidance;
632 NavigationServer3D::get_singleton()->agent_set_use_3d_avoidance(agent, use_3d_avoidance);
633 notify_property_list_changed();
634 }
635
636 void NavigationAgent3D::set_keep_y_velocity(bool p_enabled) {
637 keep_y_velocity = p_enabled;
638 stored_y_velocity = 0.0;
639 }
640
641 bool NavigationAgent3D::get_keep_y_velocity() const {
642 return keep_y_velocity;
643 }
644
645 void NavigationAgent3D::set_neighbor_distance(real_t p_distance) {
646 if (Math::is_equal_approx(neighbor_distance, p_distance)) {
647 return;
648 }
649
650 neighbor_distance = p_distance;
651
652 NavigationServer3D::get_singleton()->agent_set_neighbor_distance(agent, neighbor_distance);
653 }
654
655 void NavigationAgent3D::set_max_neighbors(int p_count) {
656 if (max_neighbors == p_count) {
657 return;
658 }
659
660 max_neighbors = p_count;
661
662 NavigationServer3D::get_singleton()->agent_set_max_neighbors(agent, max_neighbors);
663 }
664
665 void NavigationAgent3D::set_time_horizon_agents(real_t p_time_horizon) {
666 ERR_FAIL_COND_MSG(p_time_horizon < 0.0, "Time horizon must be positive.");
667 if (Math::is_equal_approx(time_horizon_agents, p_time_horizon)) {
668 return;
669 }
670 time_horizon_agents = p_time_horizon;
671 NavigationServer3D::get_singleton()->agent_set_time_horizon_agents(agent, time_horizon_agents);
672 }
673
674 void NavigationAgent3D::set_time_horizon_obstacles(real_t p_time_horizon) {
675 ERR_FAIL_COND_MSG(p_time_horizon < 0.0, "Time horizon must be positive.");
676 if (Math::is_equal_approx(time_horizon_obstacles, p_time_horizon)) {
677 return;
678 }
679 time_horizon_obstacles = p_time_horizon;
680 NavigationServer3D::get_singleton()->agent_set_time_horizon_obstacles(agent, time_horizon_obstacles);
681 }
682
683 void NavigationAgent3D::set_max_speed(real_t p_max_speed) {
684 ERR_FAIL_COND_MSG(p_max_speed < 0.0, "Max speed must be positive.");
685 if (Math::is_equal_approx(max_speed, p_max_speed)) {
686 return;
687 }
688 max_speed = p_max_speed;
689
690 NavigationServer3D::get_singleton()->agent_set_max_speed(agent, max_speed);
691 }
692
693 void NavigationAgent3D::set_path_max_distance(real_t p_path_max_distance) {
694 if (Math::is_equal_approx(path_max_distance, p_path_max_distance)) {
695 return;
696 }
697
698 path_max_distance = p_path_max_distance;
699 }
700
701 real_t NavigationAgent3D::get_path_max_distance() {
702 return path_max_distance;
703 }
704
705 void NavigationAgent3D::set_target_position(Vector3 p_position) {
706 // Intentionally not checking for equality of the parameter, as we want to update the path even if the target position is the same in case the world changed.
707 // Revisit later when the navigation server can update the path without requesting a new path.
708
709 target_position = p_position;
710 target_position_submitted = true;
711
712 _request_repath();
713 }
714
715 Vector3 NavigationAgent3D::get_target_position() const {
716 return target_position;
717 }
718
719 Vector3 NavigationAgent3D::get_next_path_position() {
720 _update_navigation();
721
722 const Vector<Vector3> &navigation_path = navigation_result->get_path();
723 if (navigation_path.is_empty()) {
724 ERR_FAIL_NULL_V_MSG(agent_parent, Vector3(), "The agent has no parent.");
725 return agent_parent->get_global_position();
726 } else {
727 return navigation_path[navigation_path_index] - Vector3(0, path_height_offset, 0);
728 }
729 }
730
731 real_t NavigationAgent3D::distance_to_target() const {
732 ERR_FAIL_NULL_V_MSG(agent_parent, 0.0, "The agent has no parent.");
733 return agent_parent->get_global_position().distance_to(target_position);
734 }
735
736 bool NavigationAgent3D::is_target_reached() const {
737 return target_reached;
738 }
739
740 bool NavigationAgent3D::is_target_reachable() {
741 _update_navigation();
742 return _is_target_reachable();
743 }
744
745 bool NavigationAgent3D::_is_target_reachable() const {
746 return target_desired_distance >= _get_final_position().distance_to(target_position);
747 }
748
749 bool NavigationAgent3D::is_navigation_finished() {
750 _update_navigation();
751 return navigation_finished;
752 }
753
754 Vector3 NavigationAgent3D::get_final_position() {
755 _update_navigation();
756 return _get_final_position();
757 }
758
759 Vector3 NavigationAgent3D::_get_final_position() const {
760 const Vector<Vector3> &navigation_path = navigation_result->get_path();
761 if (navigation_path.is_empty()) {
762 return Vector3();
763 }
764 return navigation_path[navigation_path.size() - 1] - Vector3(0, path_height_offset, 0);
765 }
766
767 void NavigationAgent3D::set_velocity_forced(Vector3 p_velocity) {
768 // Intentionally not checking for equality of the parameter.
769 // We need to always submit the velocity to the navigation server, even when it is the same, in order to run avoidance every frame.
770 // Revisit later when the navigation server can update avoidance without users resubmitting the velocity.
771
772 velocity_forced = p_velocity;
773 velocity_forced_submitted = true;
774 }
775
776 void NavigationAgent3D::set_velocity(const Vector3 p_velocity) {
777 velocity = p_velocity;
778 velocity_submitted = true;
779 }
780
781 void NavigationAgent3D::_avoidance_done(Vector3 p_new_velocity) {
782 safe_velocity = p_new_velocity;
783 if (!use_3d_avoidance) {
784 safe_velocity.y = stored_y_velocity;
785 }
786 emit_signal(SNAME("velocity_computed"), safe_velocity);
787 }
788
789 PackedStringArray NavigationAgent3D::get_configuration_warnings() const {
790 PackedStringArray warnings = Node::get_configuration_warnings();
791
792 if (!Object::cast_to<Node3D>(get_parent())) {
793 warnings.push_back(RTR("The NavigationAgent3D can be used only under a Node3D inheriting parent node."));
794 }
795
796 return warnings;
797 }
798
799 void NavigationAgent3D::_update_navigation() {
800 if (agent_parent == nullptr) {
801 return;
802 }
803 if (!agent_parent->is_inside_tree()) {
804 return;
805 }
806 if (!target_position_submitted) {
807 return;
808 }
809
810 Vector3 origin = agent_parent->get_global_position();
811
812 bool reload_path = false;
813
814 if (NavigationServer3D::get_singleton()->agent_is_map_changed(agent)) {
815 reload_path = true;
816 } else if (navigation_result->get_path().is_empty()) {
817 reload_path = true;
818 } else {
819 // Check if too far from the navigation path
820 if (navigation_path_index > 0) {
821 const Vector<Vector3> &navigation_path = navigation_result->get_path();
822
823 Vector3 segment_a = navigation_path[navigation_path_index - 1];
824 Vector3 segment_b = navigation_path[navigation_path_index];
825 segment_a.y -= path_height_offset;
826 segment_b.y -= path_height_offset;
827 Vector3 p = Geometry3D::get_closest_point_to_segment(origin, segment_a, segment_b);
828 if (origin.distance_to(p) >= path_max_distance) {
829 // To faraway, reload path
830 reload_path = true;
831 }
832 }
833 }
834
835 if (reload_path) {
836 navigation_query->set_start_position(origin);
837 navigation_query->set_target_position(target_position);
838 navigation_query->set_navigation_layers(navigation_layers);
839 navigation_query->set_metadata_flags(path_metadata_flags);
840
841 if (map_override.is_valid()) {
842 navigation_query->set_map(map_override);
843 } else {
844 navigation_query->set_map(agent_parent->get_world_3d()->get_navigation_map());
845 }
846
847 NavigationServer3D::get_singleton()->query_path(navigation_query, navigation_result);
848 #ifdef DEBUG_ENABLED
849 debug_path_dirty = true;
850 #endif // DEBUG_ENABLED
851 navigation_finished = false;
852 last_waypoint_reached = false;
853 navigation_path_index = 0;
854 emit_signal(SNAME("path_changed"));
855 }
856
857 if (navigation_result->get_path().is_empty()) {
858 return;
859 }
860
861 // Check if the navigation has already finished.
862 if (navigation_finished) {
863 return;
864 }
865
866 // Check if we reached the target.
867 if (_is_within_target_distance(origin)) {
868 // Emit waypoint_reached in case we also moved within distance of a waypoint.
869 _advance_waypoints(origin);
870 _transition_to_target_reached();
871 _transition_to_navigation_finished();
872 } else {
873 // Advance waypoints if possible.
874 _advance_waypoints(origin);
875 // Keep navigation running even after reaching the last waypoint if the target is reachable.
876 if (last_waypoint_reached && !_is_target_reachable()) {
877 _transition_to_navigation_finished();
878 }
879 }
880 }
881
882 void NavigationAgent3D::_advance_waypoints(const Vector3 &p_origin) {
883 if (last_waypoint_reached) {
884 return;
885 }
886
887 // Advance to the farthest possible waypoint.
888 while (_is_within_waypoint_distance(p_origin)) {
889 _trigger_waypoint_reached();
890
891 if (_is_last_waypoint()) {
892 last_waypoint_reached = true;
893 break;
894 }
895
896 _move_to_next_waypoint();
897 }
898 }
899
900 void NavigationAgent3D::_request_repath() {
901 navigation_result->reset();
902 target_reached = false;
903 navigation_finished = false;
904 last_waypoint_reached = false;
905 }
906
907 bool NavigationAgent3D::_is_last_waypoint() const {
908 return navigation_path_index == navigation_result->get_path().size() - 1;
909 }
910
911 void NavigationAgent3D::_move_to_next_waypoint() {
912 navigation_path_index += 1;
913 }
914
915 bool NavigationAgent3D::_is_within_waypoint_distance(const Vector3 &p_origin) const {
916 const Vector<Vector3> &navigation_path = navigation_result->get_path();
917 Vector3 waypoint = navigation_path[navigation_path_index] - Vector3(0, path_height_offset, 0);
918 return p_origin.distance_to(waypoint) < path_desired_distance;
919 }
920
921 bool NavigationAgent3D::_is_within_target_distance(const Vector3 &p_origin) const {
922 return p_origin.distance_to(target_position) < target_desired_distance;
923 }
924
925 void NavigationAgent3D::_trigger_waypoint_reached() {
926 const Vector<Vector3> &navigation_path = navigation_result->get_path();
927 const Vector<int32_t> &navigation_path_types = navigation_result->get_path_types();
928 const TypedArray<RID> &navigation_path_rids = navigation_result->get_path_rids();
929 const Vector<int64_t> &navigation_path_owners = navigation_result->get_path_owner_ids();
930
931 Dictionary details;
932
933 const Vector3 waypoint = navigation_path[navigation_path_index];
934 details[CoreStringName(position)] = waypoint;
935
936 int waypoint_type = -1;
937 if (path_metadata_flags.has_flag(NavigationPathQueryParameters3D::PathMetadataFlags::PATH_METADATA_INCLUDE_TYPES)) {
938 const NavigationPathQueryResult3D::PathSegmentType type = NavigationPathQueryResult3D::PathSegmentType(navigation_path_types[navigation_path_index]);
939
940 details[SNAME("type")] = type;
941 waypoint_type = type;
942 }
943
944 if (path_metadata_flags.has_flag(NavigationPathQueryParameters3D::PathMetadataFlags::PATH_METADATA_INCLUDE_RIDS)) {
945 details[SNAME("rid")] = navigation_path_rids[navigation_path_index];
946 }
947
948 if (path_metadata_flags.has_flag(NavigationPathQueryParameters3D::PathMetadataFlags::PATH_METADATA_INCLUDE_OWNERS)) {
949 const ObjectID waypoint_owner_id = ObjectID(navigation_path_owners[navigation_path_index]);
950
951 // Get a reference to the owning object.
952 Object *owner = nullptr;
953 if (waypoint_owner_id.is_valid()) {
954 owner = ObjectDB::get_instance(waypoint_owner_id);
955 }
956
957 details[SNAME("owner")] = owner;
958
959 if (waypoint_type == NavigationPathQueryResult3D::PATH_SEGMENT_TYPE_LINK) {
960 const NavigationLink3D *navlink = Object::cast_to<NavigationLink3D>(owner);
961 if (navlink) {
962 Vector3 link_global_start_position = navlink->get_global_start_position();
963 Vector3 link_global_end_position = navlink->get_global_end_position();
964 if (waypoint.distance_to(link_global_start_position) < waypoint.distance_to(link_global_end_position)) {
965 details[SNAME("link_entry_position")] = link_global_start_position;
966 details[SNAME("link_exit_position")] = link_global_end_position;
967 } else {
968 details[SNAME("link_entry_position")] = link_global_end_position;
969 details[SNAME("link_exit_position")] = link_global_start_position;
970 }
971 }
972 }
973 }
974
975 // Emit a signal for the waypoint.
976 emit_signal(SNAME("waypoint_reached"), details);
977
978 // Emit a signal if we've reached a navigation link.
979 if (waypoint_type == NavigationPathQueryResult3D::PATH_SEGMENT_TYPE_LINK) {
980 emit_signal(SNAME("link_reached"), details);
981 }
982 }
983
984 void NavigationAgent3D::_transition_to_navigation_finished() {
985 navigation_finished = true;
986 target_position_submitted = false;
987
988 if (avoidance_enabled) {
989 NavigationServer3D::get_singleton()->agent_set_position(agent, agent_parent->get_global_transform().origin);
990 NavigationServer3D::get_singleton()->agent_set_velocity(agent, Vector3(0.0, 0.0, 0.0));
991 NavigationServer3D::get_singleton()->agent_set_velocity_forced(agent, Vector3(0.0, 0.0, 0.0));
992 stored_y_velocity = 0.0;
993 }
994
995 emit_signal(SNAME("navigation_finished"));
996 }
997
998 void NavigationAgent3D::_transition_to_target_reached() {
999 target_reached = true;
1000 emit_signal(SNAME("target_reached"));
1001 }
1002
1003 void NavigationAgent3D::set_avoidance_layers(uint32_t p_layers) {
1004 avoidance_layers = p_layers;
1005 NavigationServer3D::get_singleton()->agent_set_avoidance_layers(get_rid(), avoidance_layers);
1006 }
1007
1008 uint32_t NavigationAgent3D::get_avoidance_layers() const {
1009 return avoidance_layers;
1010 }
1011
1012 void NavigationAgent3D::set_avoidance_mask(uint32_t p_mask) {
1013 avoidance_mask = p_mask;
1014 NavigationServer3D::get_singleton()->agent_set_avoidance_mask(get_rid(), avoidance_mask);
1015 }
1016
1017 uint32_t NavigationAgent3D::get_avoidance_mask() const {
1018 return avoidance_mask;
1019 }
1020
1021 void NavigationAgent3D::set_avoidance_layer_value(int p_layer_number, bool p_value) {
1022 ERR_FAIL_COND_MSG(p_layer_number < 1, "Avoidance layer number must be between 1 and 32 inclusive.");
1023 ERR_FAIL_COND_MSG(p_layer_number > 32, "Avoidance layer number must be between 1 and 32 inclusive.");
1024 uint32_t avoidance_layers_new = get_avoidance_layers();
1025 if (p_value) {
1026 avoidance_layers_new |= 1 << (p_layer_number - 1);
1027 } else {
1028 avoidance_layers_new &= ~(1 << (p_layer_number - 1));
1029 }
1030 set_avoidance_layers(avoidance_layers_new);
1031 }
1032
1033 bool NavigationAgent3D::get_avoidance_layer_value(int p_layer_number) const {
1034 ERR_FAIL_COND_V_MSG(p_layer_number < 1, false, "Avoidance layer number must be between 1 and 32 inclusive.");
1035 ERR_FAIL_COND_V_MSG(p_layer_number > 32, false, "Avoidance layer number must be between 1 and 32 inclusive.");
1036 return get_avoidance_layers() & (1 << (p_layer_number - 1));
1037 }
1038
1039 void NavigationAgent3D::set_avoidance_mask_value(int p_mask_number, bool p_value) {
1040 ERR_FAIL_COND_MSG(p_mask_number < 1, "Avoidance mask number must be between 1 and 32 inclusive.");
1041 ERR_FAIL_COND_MSG(p_mask_number > 32, "Avoidance mask number must be between 1 and 32 inclusive.");
1042 uint32_t mask = get_avoidance_mask();
1043 if (p_value) {
1044 mask |= 1 << (p_mask_number - 1);
1045 } else {
1046 mask &= ~(1 << (p_mask_number - 1));
1047 }
1048 set_avoidance_mask(mask);
1049 }
1050
1051 bool NavigationAgent3D::get_avoidance_mask_value(int p_mask_number) const {
1052 ERR_FAIL_COND_V_MSG(p_mask_number < 1, false, "Avoidance mask number must be between 1 and 32 inclusive.");
1053 ERR_FAIL_COND_V_MSG(p_mask_number > 32, false, "Avoidance mask number must be between 1 and 32 inclusive.");
1054 return get_avoidance_mask() & (1 << (p_mask_number - 1));
1055 }
1056
1057 void NavigationAgent3D::set_avoidance_priority(real_t p_priority) {
1058 ERR_FAIL_COND_MSG(p_priority < 0.0, "Avoidance priority must be between 0.0 and 1.0 inclusive.");
1059 ERR_FAIL_COND_MSG(p_priority > 1.0, "Avoidance priority must be between 0.0 and 1.0 inclusive.");
1060 avoidance_priority = p_priority;
1061 NavigationServer3D::get_singleton()->agent_set_avoidance_priority(get_rid(), p_priority);
1062 }
1063
1064 real_t NavigationAgent3D::get_avoidance_priority() const {
1065 return avoidance_priority;
1066 }
1067
1068 ////////DEBUG////////////////////////////////////////////////////////////
1069
1070 void NavigationAgent3D::set_debug_enabled(bool p_enabled) {
1071 #ifdef DEBUG_ENABLED
1072 if (debug_enabled == p_enabled) {
1073 return;
1074 }
1075
1076 debug_enabled = p_enabled;
1077 debug_path_dirty = true;
1078 #endif // DEBUG_ENABLED
1079 }
1080
1081 bool NavigationAgent3D::get_debug_enabled() const {
1082 return debug_enabled;
1083 }
1084
1085 void NavigationAgent3D::set_debug_use_custom(bool p_enabled) {
1086 #ifdef DEBUG_ENABLED
1087 if (debug_use_custom == p_enabled) {
1088 return;
1089 }
1090
1091 debug_use_custom = p_enabled;
1092 debug_path_dirty = true;
1093 #endif // DEBUG_ENABLED
1094 }
1095
1096 bool NavigationAgent3D::get_debug_use_custom() const {
1097 return debug_use_custom;
1098 }
1099
1100 void NavigationAgent3D::set_debug_path_custom_color(Color p_color) {
1101 #ifdef DEBUG_ENABLED
1102 if (debug_path_custom_color == p_color) {
1103 return;
1104 }
1105
1106 debug_path_custom_color = p_color;
1107 debug_path_dirty = true;
1108 #endif // DEBUG_ENABLED
1109 }
1110
1111 Color NavigationAgent3D::get_debug_path_custom_color() const {
1112 return debug_path_custom_color;
1113 }
1114
1115 void NavigationAgent3D::set_debug_path_custom_point_size(float p_point_size) {
1116 #ifdef DEBUG_ENABLED
1117 if (Math::is_equal_approx(debug_path_custom_point_size, p_point_size)) {
1118 return;
1119 }
1120
1121 debug_path_custom_point_size = MAX(0.0, p_point_size);
1122 debug_path_dirty = true;
1123 #endif // DEBUG_ENABLED
1124 }
1125
1126 float NavigationAgent3D::get_debug_path_custom_point_size() const {
1127 return debug_path_custom_point_size;
1128 }
1129
1130 #ifdef DEBUG_ENABLED
1131 void NavigationAgent3D::_navigation_debug_changed() {
1132 debug_path_dirty = true;
1133 }
1134
1135 void NavigationAgent3D::_update_debug_path() {
1136 if (!debug_path_dirty) {
1137 return;
1138 }
1139 debug_path_dirty = false;
1140
1141 if (!debug_path_instance.is_valid()) {
1142 debug_path_instance = RenderingServer::get_singleton()->instance_create();
1143 }
1144
1145 if (debug_path_mesh.is_null()) {
1146 debug_path_mesh.instantiate();
1147 }
1148
1149 debug_path_mesh->clear_surfaces();
1150
1151 if (!(debug_enabled && NavigationServer3D::get_singleton()->get_debug_navigation_enable_agent_paths())) {
1152 return;
1153 }
1154
1155 if (!(agent_parent && agent_parent->is_inside_tree())) {
1156 return;
1157 }
1158
1159 const Vector<Vector3> &navigation_path = navigation_result->get_path();
1160
1161 if (navigation_path.size() <= 1) {
1162 return;
1163 }
1164
1165 Vector<Vector3> debug_path_lines_vertex_array;
1166
1167 for (int i = 0; i < navigation_path.size() - 1; i++) {
1168 debug_path_lines_vertex_array.push_back(navigation_path[i]);
1169 debug_path_lines_vertex_array.push_back(navigation_path[i + 1]);
1170 }
1171
1172 Array debug_path_lines_mesh_array;
1173 debug_path_lines_mesh_array.resize(Mesh::ARRAY_MAX);
1174 debug_path_lines_mesh_array[Mesh::ARRAY_VERTEX] = debug_path_lines_vertex_array;
1175
1176 debug_path_mesh->add_surface_from_arrays(Mesh::PRIMITIVE_LINES, debug_path_lines_mesh_array);
1177
1178 Ref<StandardMaterial3D> debug_agent_path_line_material = NavigationServer3D::get_singleton()->get_debug_navigation_agent_path_line_material();
1179 if (debug_use_custom) {
1180 if (debug_agent_path_line_custom_material.is_null()) {
1181 debug_agent_path_line_custom_material = debug_agent_path_line_material->duplicate();
1182 }
1183 debug_agent_path_line_custom_material->set_albedo(debug_path_custom_color);
1184 debug_path_mesh->surface_set_material(0, debug_agent_path_line_custom_material);
1185 } else {
1186 debug_path_mesh->surface_set_material(0, debug_agent_path_line_material);
1187 }
1188
1189 if (debug_path_custom_point_size > 0.0) {
1190 Vector<Vector3> debug_path_points_vertex_array;
1191
1192 for (int i = 0; i < navigation_path.size(); i++) {
1193 debug_path_points_vertex_array.push_back(navigation_path[i]);
1194 }
1195
1196 Array debug_path_points_mesh_array;
1197 debug_path_points_mesh_array.resize(Mesh::ARRAY_MAX);
1198 debug_path_points_mesh_array[Mesh::ARRAY_VERTEX] = debug_path_points_vertex_array;
1199
1200 debug_path_mesh->add_surface_from_arrays(Mesh::PRIMITIVE_POINTS, debug_path_points_mesh_array);
1201
1202 Ref<StandardMaterial3D> debug_agent_path_point_material = NavigationServer3D::get_singleton()->get_debug_navigation_agent_path_point_material();
1203 if (debug_use_custom) {
1204 if (debug_agent_path_point_custom_material.is_null()) {
1205 debug_agent_path_point_custom_material = debug_agent_path_point_material->duplicate();
1206 }
1207 debug_agent_path_point_custom_material->set_albedo(debug_path_custom_color);
1208 debug_agent_path_point_custom_material->set_point_size(debug_path_custom_point_size);
1209 debug_path_mesh->surface_set_material(1, debug_agent_path_point_custom_material);
1210 } else {
1211 debug_path_mesh->surface_set_material(1, debug_agent_path_point_material);
1212 }
1213 }
1214
1215 RS::get_singleton()->instance_set_base(debug_path_instance, debug_path_mesh->get_rid());
1216 RS::get_singleton()->instance_set_scenario(debug_path_instance, agent_parent->get_world_3d()->get_scenario());
1217 RS::get_singleton()->instance_set_visible(debug_path_instance, agent_parent->is_visible_in_tree());
1218 }
1219 #endif // DEBUG_ENABLED