navigation_agent_3d.cpp 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159
  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. #include "navigation_agent_3d.h"
  31. #include "scene/3d/navigation_link_3d.h"
  32. #include "servers/navigation_server_3d.h"
  33. void NavigationAgent3D::_bind_methods() {
  34. ClassDB::bind_method(D_METHOD("get_rid"), &NavigationAgent3D::get_rid);
  35. ClassDB::bind_method(D_METHOD("set_avoidance_enabled", "enabled"), &NavigationAgent3D::set_avoidance_enabled);
  36. ClassDB::bind_method(D_METHOD("get_avoidance_enabled"), &NavigationAgent3D::get_avoidance_enabled);
  37. ClassDB::bind_method(D_METHOD("set_path_desired_distance", "desired_distance"), &NavigationAgent3D::set_path_desired_distance);
  38. ClassDB::bind_method(D_METHOD("get_path_desired_distance"), &NavigationAgent3D::get_path_desired_distance);
  39. ClassDB::bind_method(D_METHOD("set_target_desired_distance", "desired_distance"), &NavigationAgent3D::set_target_desired_distance);
  40. ClassDB::bind_method(D_METHOD("get_target_desired_distance"), &NavigationAgent3D::get_target_desired_distance);
  41. ClassDB::bind_method(D_METHOD("set_radius", "radius"), &NavigationAgent3D::set_radius);
  42. ClassDB::bind_method(D_METHOD("get_radius"), &NavigationAgent3D::get_radius);
  43. ClassDB::bind_method(D_METHOD("set_height", "height"), &NavigationAgent3D::set_height);
  44. ClassDB::bind_method(D_METHOD("get_height"), &NavigationAgent3D::get_height);
  45. ClassDB::bind_method(D_METHOD("set_path_height_offset", "path_height_offset"), &NavigationAgent3D::set_path_height_offset);
  46. ClassDB::bind_method(D_METHOD("get_path_height_offset"), &NavigationAgent3D::get_path_height_offset);
  47. ClassDB::bind_method(D_METHOD("set_use_3d_avoidance", "enabled"), &NavigationAgent3D::set_use_3d_avoidance);
  48. ClassDB::bind_method(D_METHOD("get_use_3d_avoidance"), &NavigationAgent3D::get_use_3d_avoidance);
  49. ClassDB::bind_method(D_METHOD("set_keep_y_velocity", "enabled"), &NavigationAgent3D::set_keep_y_velocity);
  50. ClassDB::bind_method(D_METHOD("get_keep_y_velocity"), &NavigationAgent3D::get_keep_y_velocity);
  51. ClassDB::bind_method(D_METHOD("set_neighbor_distance", "neighbor_distance"), &NavigationAgent3D::set_neighbor_distance);
  52. ClassDB::bind_method(D_METHOD("get_neighbor_distance"), &NavigationAgent3D::get_neighbor_distance);
  53. ClassDB::bind_method(D_METHOD("set_max_neighbors", "max_neighbors"), &NavigationAgent3D::set_max_neighbors);
  54. ClassDB::bind_method(D_METHOD("get_max_neighbors"), &NavigationAgent3D::get_max_neighbors);
  55. ClassDB::bind_method(D_METHOD("set_time_horizon_agents", "time_horizon"), &NavigationAgent3D::set_time_horizon_agents);
  56. ClassDB::bind_method(D_METHOD("get_time_horizon_agents"), &NavigationAgent3D::get_time_horizon_agents);
  57. ClassDB::bind_method(D_METHOD("set_time_horizon_obstacles", "time_horizon"), &NavigationAgent3D::set_time_horizon_obstacles);
  58. ClassDB::bind_method(D_METHOD("get_time_horizon_obstacles"), &NavigationAgent3D::get_time_horizon_obstacles);
  59. ClassDB::bind_method(D_METHOD("set_max_speed", "max_speed"), &NavigationAgent3D::set_max_speed);
  60. ClassDB::bind_method(D_METHOD("get_max_speed"), &NavigationAgent3D::get_max_speed);
  61. ClassDB::bind_method(D_METHOD("set_path_max_distance", "max_speed"), &NavigationAgent3D::set_path_max_distance);
  62. ClassDB::bind_method(D_METHOD("get_path_max_distance"), &NavigationAgent3D::get_path_max_distance);
  63. ClassDB::bind_method(D_METHOD("set_navigation_layers", "navigation_layers"), &NavigationAgent3D::set_navigation_layers);
  64. ClassDB::bind_method(D_METHOD("get_navigation_layers"), &NavigationAgent3D::get_navigation_layers);
  65. ClassDB::bind_method(D_METHOD("set_navigation_layer_value", "layer_number", "value"), &NavigationAgent3D::set_navigation_layer_value);
  66. ClassDB::bind_method(D_METHOD("get_navigation_layer_value", "layer_number"), &NavigationAgent3D::get_navigation_layer_value);
  67. ClassDB::bind_method(D_METHOD("set_pathfinding_algorithm", "pathfinding_algorithm"), &NavigationAgent3D::set_pathfinding_algorithm);
  68. ClassDB::bind_method(D_METHOD("get_pathfinding_algorithm"), &NavigationAgent3D::get_pathfinding_algorithm);
  69. ClassDB::bind_method(D_METHOD("set_path_postprocessing", "path_postprocessing"), &NavigationAgent3D::set_path_postprocessing);
  70. ClassDB::bind_method(D_METHOD("get_path_postprocessing"), &NavigationAgent3D::get_path_postprocessing);
  71. ClassDB::bind_method(D_METHOD("set_path_metadata_flags", "flags"), &NavigationAgent3D::set_path_metadata_flags);
  72. ClassDB::bind_method(D_METHOD("get_path_metadata_flags"), &NavigationAgent3D::get_path_metadata_flags);
  73. ClassDB::bind_method(D_METHOD("set_navigation_map", "navigation_map"), &NavigationAgent3D::set_navigation_map);
  74. ClassDB::bind_method(D_METHOD("get_navigation_map"), &NavigationAgent3D::get_navigation_map);
  75. ClassDB::bind_method(D_METHOD("set_target_position", "position"), &NavigationAgent3D::set_target_position);
  76. ClassDB::bind_method(D_METHOD("get_target_position"), &NavigationAgent3D::get_target_position);
  77. ClassDB::bind_method(D_METHOD("set_simplify_path", "enabled"), &NavigationAgent3D::set_simplify_path);
  78. ClassDB::bind_method(D_METHOD("get_simplify_path"), &NavigationAgent3D::get_simplify_path);
  79. ClassDB::bind_method(D_METHOD("set_simplify_epsilon", "epsilon"), &NavigationAgent3D::set_simplify_epsilon);
  80. ClassDB::bind_method(D_METHOD("get_simplify_epsilon"), &NavigationAgent3D::get_simplify_epsilon);
  81. ClassDB::bind_method(D_METHOD("get_next_path_position"), &NavigationAgent3D::get_next_path_position);
  82. ClassDB::bind_method(D_METHOD("set_velocity_forced", "velocity"), &NavigationAgent3D::set_velocity_forced);
  83. ClassDB::bind_method(D_METHOD("set_velocity", "velocity"), &NavigationAgent3D::set_velocity);
  84. ClassDB::bind_method(D_METHOD("get_velocity"), &NavigationAgent3D::get_velocity);
  85. ClassDB::bind_method(D_METHOD("distance_to_target"), &NavigationAgent3D::distance_to_target);
  86. ClassDB::bind_method(D_METHOD("get_current_navigation_result"), &NavigationAgent3D::get_current_navigation_result);
  87. ClassDB::bind_method(D_METHOD("get_current_navigation_path"), &NavigationAgent3D::get_current_navigation_path);
  88. ClassDB::bind_method(D_METHOD("get_current_navigation_path_index"), &NavigationAgent3D::get_current_navigation_path_index);
  89. ClassDB::bind_method(D_METHOD("is_target_reached"), &NavigationAgent3D::is_target_reached);
  90. ClassDB::bind_method(D_METHOD("is_target_reachable"), &NavigationAgent3D::is_target_reachable);
  91. ClassDB::bind_method(D_METHOD("is_navigation_finished"), &NavigationAgent3D::is_navigation_finished);
  92. ClassDB::bind_method(D_METHOD("get_final_position"), &NavigationAgent3D::get_final_position);
  93. ClassDB::bind_method(D_METHOD("_avoidance_done", "new_velocity"), &NavigationAgent3D::_avoidance_done);
  94. ClassDB::bind_method(D_METHOD("set_avoidance_layers", "layers"), &NavigationAgent3D::set_avoidance_layers);
  95. ClassDB::bind_method(D_METHOD("get_avoidance_layers"), &NavigationAgent3D::get_avoidance_layers);
  96. ClassDB::bind_method(D_METHOD("set_avoidance_mask", "mask"), &NavigationAgent3D::set_avoidance_mask);
  97. ClassDB::bind_method(D_METHOD("get_avoidance_mask"), &NavigationAgent3D::get_avoidance_mask);
  98. ClassDB::bind_method(D_METHOD("set_avoidance_layer_value", "layer_number", "value"), &NavigationAgent3D::set_avoidance_layer_value);
  99. ClassDB::bind_method(D_METHOD("get_avoidance_layer_value", "layer_number"), &NavigationAgent3D::get_avoidance_layer_value);
  100. ClassDB::bind_method(D_METHOD("set_avoidance_mask_value", "mask_number", "value"), &NavigationAgent3D::set_avoidance_mask_value);
  101. ClassDB::bind_method(D_METHOD("get_avoidance_mask_value", "mask_number"), &NavigationAgent3D::get_avoidance_mask_value);
  102. ClassDB::bind_method(D_METHOD("set_avoidance_priority", "priority"), &NavigationAgent3D::set_avoidance_priority);
  103. ClassDB::bind_method(D_METHOD("get_avoidance_priority"), &NavigationAgent3D::get_avoidance_priority);
  104. ADD_GROUP("Pathfinding", "");
  105. ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "target_position", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR), "set_target_position", "get_target_position");
  106. 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");
  107. 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");
  108. 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");
  109. 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");
  110. ADD_PROPERTY(PropertyInfo(Variant::INT, "navigation_layers", PROPERTY_HINT_LAYERS_3D_NAVIGATION), "set_navigation_layers", "get_navigation_layers");
  111. ADD_PROPERTY(PropertyInfo(Variant::INT, "pathfinding_algorithm", PROPERTY_HINT_ENUM, "AStar"), "set_pathfinding_algorithm", "get_pathfinding_algorithm");
  112. ADD_PROPERTY(PropertyInfo(Variant::INT, "path_postprocessing", PROPERTY_HINT_ENUM, "Corridorfunnel,Edgecentered"), "set_path_postprocessing", "get_path_postprocessing");
  113. 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");
  114. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "simplify_path"), "set_simplify_path", "get_simplify_path");
  115. 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");
  116. ADD_GROUP("Avoidance", "");
  117. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "avoidance_enabled"), "set_avoidance_enabled", "get_avoidance_enabled");
  118. ADD_PROPERTY(PropertyInfo(Variant::VECTOR3, "velocity", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR), "set_velocity", "get_velocity");
  119. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "height", PROPERTY_HINT_RANGE, "0.01,100,0.01,or_greater,suffix:m"), "set_height", "get_height");
  120. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "radius", PROPERTY_HINT_RANGE, "0.01,100,0.01,or_greater,suffix:m"), "set_radius", "get_radius");
  121. 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");
  122. ADD_PROPERTY(PropertyInfo(Variant::INT, "max_neighbors", PROPERTY_HINT_RANGE, "1,10000,1,or_greater"), "set_max_neighbors", "get_max_neighbors");
  123. 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");
  124. 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");
  125. 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");
  126. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "use_3d_avoidance"), "set_use_3d_avoidance", "get_use_3d_avoidance");
  127. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "keep_y_velocity"), "set_keep_y_velocity", "get_keep_y_velocity");
  128. ADD_PROPERTY(PropertyInfo(Variant::INT, "avoidance_layers", PROPERTY_HINT_LAYERS_AVOIDANCE), "set_avoidance_layers", "get_avoidance_layers");
  129. ADD_PROPERTY(PropertyInfo(Variant::INT, "avoidance_mask", PROPERTY_HINT_LAYERS_AVOIDANCE), "set_avoidance_mask", "get_avoidance_mask");
  130. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "avoidance_priority", PROPERTY_HINT_RANGE, "0.0,1.0,0.01"), "set_avoidance_priority", "get_avoidance_priority");
  131. ADD_SIGNAL(MethodInfo("path_changed"));
  132. ADD_SIGNAL(MethodInfo("target_reached"));
  133. ADD_SIGNAL(MethodInfo("waypoint_reached", PropertyInfo(Variant::DICTIONARY, "details")));
  134. ADD_SIGNAL(MethodInfo("link_reached", PropertyInfo(Variant::DICTIONARY, "details")));
  135. ADD_SIGNAL(MethodInfo("navigation_finished"));
  136. ADD_SIGNAL(MethodInfo("velocity_computed", PropertyInfo(Variant::VECTOR3, "safe_velocity")));
  137. ClassDB::bind_method(D_METHOD("set_debug_enabled", "enabled"), &NavigationAgent3D::set_debug_enabled);
  138. ClassDB::bind_method(D_METHOD("get_debug_enabled"), &NavigationAgent3D::get_debug_enabled);
  139. ClassDB::bind_method(D_METHOD("set_debug_use_custom", "enabled"), &NavigationAgent3D::set_debug_use_custom);
  140. ClassDB::bind_method(D_METHOD("get_debug_use_custom"), &NavigationAgent3D::get_debug_use_custom);
  141. ClassDB::bind_method(D_METHOD("set_debug_path_custom_color", "color"), &NavigationAgent3D::set_debug_path_custom_color);
  142. ClassDB::bind_method(D_METHOD("get_debug_path_custom_color"), &NavigationAgent3D::get_debug_path_custom_color);
  143. ClassDB::bind_method(D_METHOD("set_debug_path_custom_point_size", "point_size"), &NavigationAgent3D::set_debug_path_custom_point_size);
  144. ClassDB::bind_method(D_METHOD("get_debug_path_custom_point_size"), &NavigationAgent3D::get_debug_path_custom_point_size);
  145. ADD_GROUP("Debug", "debug_");
  146. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "debug_enabled"), "set_debug_enabled", "get_debug_enabled");
  147. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "debug_use_custom"), "set_debug_use_custom", "get_debug_use_custom");
  148. ADD_PROPERTY(PropertyInfo(Variant::COLOR, "debug_path_custom_color"), "set_debug_path_custom_color", "get_debug_path_custom_color");
  149. 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");
  150. }
  151. #ifndef DISABLE_DEPRECATED
  152. // Compatibility with Godot 4.0 beta 10 or below.
  153. // Functions in block below all renamed or replaced in 4.0 beta 1X avoidance rework.
  154. bool NavigationAgent3D::_set(const StringName &p_name, const Variant &p_value) {
  155. if (p_name == "time_horizon") {
  156. set_time_horizon_agents(p_value);
  157. return true;
  158. }
  159. if (p_name == "target_location") {
  160. set_target_position(p_value);
  161. return true;
  162. }
  163. if (p_name == "agent_height_offset") {
  164. set_path_height_offset(p_value);
  165. return true;
  166. }
  167. return false;
  168. }
  169. bool NavigationAgent3D::_get(const StringName &p_name, Variant &r_ret) const {
  170. if (p_name == "time_horizon") {
  171. r_ret = get_time_horizon_agents();
  172. return true;
  173. }
  174. if (p_name == "target_location") {
  175. r_ret = get_target_position();
  176. return true;
  177. }
  178. if (p_name == "agent_height_offset") {
  179. r_ret = get_path_height_offset();
  180. return true;
  181. }
  182. return false;
  183. }
  184. #endif // DISABLE_DEPRECATED
  185. void NavigationAgent3D::_notification(int p_what) {
  186. switch (p_what) {
  187. case NOTIFICATION_POST_ENTER_TREE: {
  188. // need to use POST_ENTER_TREE cause with normal ENTER_TREE not all required Nodes are ready.
  189. // cannot use READY as ready does not get called if Node is re-added to SceneTree
  190. set_agent_parent(get_parent());
  191. set_physics_process_internal(true);
  192. if (agent_parent && avoidance_enabled) {
  193. NavigationServer3D::get_singleton()->agent_set_position(agent, agent_parent->get_global_transform().origin);
  194. }
  195. #ifdef DEBUG_ENABLED
  196. if (NavigationServer3D::get_singleton()->get_debug_enabled()) {
  197. debug_path_dirty = true;
  198. }
  199. #endif // DEBUG_ENABLED
  200. } break;
  201. case NOTIFICATION_PARENTED: {
  202. if (is_inside_tree() && (get_parent() != agent_parent)) {
  203. // only react to PARENTED notifications when already inside_tree and parent changed, e.g. users switch nodes around
  204. // PARENTED notification fires also when Node is added in scripts to a parent
  205. // this would spam transforms fails and world fails while Node is outside SceneTree
  206. // when node gets reparented when joining the tree POST_ENTER_TREE takes care of this
  207. set_agent_parent(get_parent());
  208. set_physics_process_internal(true);
  209. }
  210. } break;
  211. case NOTIFICATION_UNPARENTED: {
  212. // if agent has no parent no point in processing it until reparented
  213. set_agent_parent(nullptr);
  214. set_physics_process_internal(false);
  215. } break;
  216. case NOTIFICATION_EXIT_TREE: {
  217. set_agent_parent(nullptr);
  218. set_physics_process_internal(false);
  219. #ifdef DEBUG_ENABLED
  220. if (debug_path_instance.is_valid()) {
  221. RS::get_singleton()->instance_set_visible(debug_path_instance, false);
  222. }
  223. #endif // DEBUG_ENABLED
  224. } break;
  225. case NOTIFICATION_SUSPENDED:
  226. case NOTIFICATION_PAUSED: {
  227. if (agent_parent) {
  228. NavigationServer3D::get_singleton()->agent_set_paused(get_rid(), !agent_parent->can_process());
  229. }
  230. } break;
  231. case NOTIFICATION_UNSUSPENDED: {
  232. if (get_tree()->is_paused()) {
  233. break;
  234. }
  235. [[fallthrough]];
  236. }
  237. case NOTIFICATION_UNPAUSED: {
  238. if (agent_parent) {
  239. NavigationServer3D::get_singleton()->agent_set_paused(get_rid(), !agent_parent->can_process());
  240. }
  241. } break;
  242. case NOTIFICATION_INTERNAL_PHYSICS_PROCESS: {
  243. if (agent_parent && avoidance_enabled) {
  244. NavigationServer3D::get_singleton()->agent_set_position(agent, agent_parent->get_global_position());
  245. }
  246. if (agent_parent && target_position_submitted) {
  247. if (velocity_submitted) {
  248. velocity_submitted = false;
  249. if (avoidance_enabled) {
  250. if (!use_3d_avoidance) {
  251. if (keep_y_velocity) {
  252. stored_y_velocity = velocity.y;
  253. }
  254. velocity.y = 0.0;
  255. }
  256. NavigationServer3D::get_singleton()->agent_set_velocity(agent, velocity);
  257. }
  258. }
  259. if (velocity_forced_submitted) {
  260. velocity_forced_submitted = false;
  261. if (avoidance_enabled) {
  262. NavigationServer3D::get_singleton()->agent_set_velocity_forced(agent, velocity_forced);
  263. }
  264. }
  265. }
  266. #ifdef DEBUG_ENABLED
  267. if (debug_path_dirty) {
  268. _update_debug_path();
  269. }
  270. #endif // DEBUG_ENABLED
  271. } break;
  272. }
  273. }
  274. void NavigationAgent3D::_validate_property(PropertyInfo &p_property) const {
  275. if (p_property.name == "keep_y_velocity" && use_3d_avoidance) {
  276. p_property.usage = PROPERTY_USAGE_NONE;
  277. return;
  278. }
  279. }
  280. NavigationAgent3D::NavigationAgent3D() {
  281. agent = NavigationServer3D::get_singleton()->agent_create();
  282. NavigationServer3D::get_singleton()->agent_set_neighbor_distance(agent, neighbor_distance);
  283. NavigationServer3D::get_singleton()->agent_set_max_neighbors(agent, max_neighbors);
  284. NavigationServer3D::get_singleton()->agent_set_time_horizon_agents(agent, time_horizon_agents);
  285. NavigationServer3D::get_singleton()->agent_set_time_horizon_obstacles(agent, time_horizon_obstacles);
  286. NavigationServer3D::get_singleton()->agent_set_radius(agent, radius);
  287. NavigationServer3D::get_singleton()->agent_set_height(agent, height);
  288. NavigationServer3D::get_singleton()->agent_set_max_speed(agent, max_speed);
  289. NavigationServer3D::get_singleton()->agent_set_avoidance_layers(agent, avoidance_layers);
  290. NavigationServer3D::get_singleton()->agent_set_avoidance_mask(agent, avoidance_mask);
  291. NavigationServer3D::get_singleton()->agent_set_avoidance_priority(agent, avoidance_priority);
  292. NavigationServer3D::get_singleton()->agent_set_use_3d_avoidance(agent, use_3d_avoidance);
  293. NavigationServer3D::get_singleton()->agent_set_avoidance_enabled(agent, avoidance_enabled);
  294. if (avoidance_enabled) {
  295. NavigationServer3D::get_singleton()->agent_set_avoidance_callback(agent, callable_mp(this, &NavigationAgent3D::_avoidance_done));
  296. }
  297. // Preallocate query and result objects to improve performance.
  298. navigation_query = Ref<NavigationPathQueryParameters3D>();
  299. navigation_query.instantiate();
  300. navigation_result = Ref<NavigationPathQueryResult3D>();
  301. navigation_result.instantiate();
  302. #ifdef DEBUG_ENABLED
  303. NavigationServer3D::get_singleton()->connect(SNAME("navigation_debug_changed"), callable_mp(this, &NavigationAgent3D::_navigation_debug_changed));
  304. #endif // DEBUG_ENABLED
  305. }
  306. NavigationAgent3D::~NavigationAgent3D() {
  307. ERR_FAIL_NULL(NavigationServer3D::get_singleton());
  308. NavigationServer3D::get_singleton()->free(agent);
  309. agent = RID(); // Pointless
  310. #ifdef DEBUG_ENABLED
  311. NavigationServer3D::get_singleton()->disconnect(SNAME("navigation_debug_changed"), callable_mp(this, &NavigationAgent3D::_navigation_debug_changed));
  312. ERR_FAIL_NULL(RenderingServer::get_singleton());
  313. if (debug_path_instance.is_valid()) {
  314. RenderingServer::get_singleton()->free(debug_path_instance);
  315. }
  316. if (debug_path_mesh.is_valid()) {
  317. RenderingServer::get_singleton()->free(debug_path_mesh->get_rid());
  318. }
  319. #endif // DEBUG_ENABLED
  320. }
  321. void NavigationAgent3D::set_avoidance_enabled(bool p_enabled) {
  322. if (avoidance_enabled == p_enabled) {
  323. return;
  324. }
  325. avoidance_enabled = p_enabled;
  326. if (avoidance_enabled) {
  327. NavigationServer3D::get_singleton()->agent_set_avoidance_enabled(agent, true);
  328. NavigationServer3D::get_singleton()->agent_set_avoidance_callback(agent, callable_mp(this, &NavigationAgent3D::_avoidance_done));
  329. } else {
  330. NavigationServer3D::get_singleton()->agent_set_avoidance_enabled(agent, false);
  331. NavigationServer3D::get_singleton()->agent_set_avoidance_callback(agent, Callable());
  332. }
  333. }
  334. bool NavigationAgent3D::get_avoidance_enabled() const {
  335. return avoidance_enabled;
  336. }
  337. void NavigationAgent3D::set_agent_parent(Node *p_agent_parent) {
  338. if (agent_parent == p_agent_parent) {
  339. return;
  340. }
  341. // remove agent from any avoidance map before changing parent or there will be leftovers on the RVO map
  342. NavigationServer3D::get_singleton()->agent_set_avoidance_callback(agent, Callable());
  343. if (Object::cast_to<Node3D>(p_agent_parent) != nullptr) {
  344. // place agent on navigation map first or else the RVO agent callback creation fails silently later
  345. agent_parent = Object::cast_to<Node3D>(p_agent_parent);
  346. if (map_override.is_valid()) {
  347. NavigationServer3D::get_singleton()->agent_set_map(get_rid(), map_override);
  348. } else {
  349. NavigationServer3D::get_singleton()->agent_set_map(get_rid(), agent_parent->get_world_3d()->get_navigation_map());
  350. }
  351. // create new avoidance callback if enabled
  352. if (avoidance_enabled) {
  353. NavigationServer3D::get_singleton()->agent_set_avoidance_callback(agent, callable_mp(this, &NavigationAgent3D::_avoidance_done));
  354. }
  355. } else {
  356. agent_parent = nullptr;
  357. NavigationServer3D::get_singleton()->agent_set_map(get_rid(), RID());
  358. }
  359. }
  360. void NavigationAgent3D::set_navigation_layers(uint32_t p_navigation_layers) {
  361. if (navigation_layers == p_navigation_layers) {
  362. return;
  363. }
  364. navigation_layers = p_navigation_layers;
  365. _request_repath();
  366. }
  367. uint32_t NavigationAgent3D::get_navigation_layers() const {
  368. return navigation_layers;
  369. }
  370. void NavigationAgent3D::set_navigation_layer_value(int p_layer_number, bool p_value) {
  371. ERR_FAIL_COND_MSG(p_layer_number < 1, "Navigation layer number must be between 1 and 32 inclusive.");
  372. ERR_FAIL_COND_MSG(p_layer_number > 32, "Navigation layer number must be between 1 and 32 inclusive.");
  373. uint32_t _navigation_layers = get_navigation_layers();
  374. if (p_value) {
  375. _navigation_layers |= 1 << (p_layer_number - 1);
  376. } else {
  377. _navigation_layers &= ~(1 << (p_layer_number - 1));
  378. }
  379. set_navigation_layers(_navigation_layers);
  380. }
  381. bool NavigationAgent3D::get_navigation_layer_value(int p_layer_number) const {
  382. ERR_FAIL_COND_V_MSG(p_layer_number < 1, false, "Navigation layer number must be between 1 and 32 inclusive.");
  383. ERR_FAIL_COND_V_MSG(p_layer_number > 32, false, "Navigation layer number must be between 1 and 32 inclusive.");
  384. return get_navigation_layers() & (1 << (p_layer_number - 1));
  385. }
  386. void NavigationAgent3D::set_pathfinding_algorithm(const NavigationPathQueryParameters3D::PathfindingAlgorithm p_pathfinding_algorithm) {
  387. if (pathfinding_algorithm == p_pathfinding_algorithm) {
  388. return;
  389. }
  390. pathfinding_algorithm = p_pathfinding_algorithm;
  391. navigation_query->set_pathfinding_algorithm(pathfinding_algorithm);
  392. }
  393. void NavigationAgent3D::set_path_postprocessing(const NavigationPathQueryParameters3D::PathPostProcessing p_path_postprocessing) {
  394. if (path_postprocessing == p_path_postprocessing) {
  395. return;
  396. }
  397. path_postprocessing = p_path_postprocessing;
  398. navigation_query->set_path_postprocessing(path_postprocessing);
  399. }
  400. void NavigationAgent3D::set_simplify_path(bool p_enabled) {
  401. simplify_path = p_enabled;
  402. navigation_query->set_simplify_path(simplify_path);
  403. }
  404. bool NavigationAgent3D::get_simplify_path() const {
  405. return simplify_path;
  406. }
  407. void NavigationAgent3D::set_simplify_epsilon(real_t p_epsilon) {
  408. simplify_epsilon = MAX(0.0, p_epsilon);
  409. navigation_query->set_simplify_epsilon(simplify_epsilon);
  410. }
  411. real_t NavigationAgent3D::get_simplify_epsilon() const {
  412. return simplify_epsilon;
  413. }
  414. void NavigationAgent3D::set_path_metadata_flags(BitField<NavigationPathQueryParameters3D::PathMetadataFlags> p_path_metadata_flags) {
  415. if (path_metadata_flags == p_path_metadata_flags) {
  416. return;
  417. }
  418. path_metadata_flags = p_path_metadata_flags;
  419. }
  420. void NavigationAgent3D::set_navigation_map(RID p_navigation_map) {
  421. if (map_override == p_navigation_map) {
  422. return;
  423. }
  424. map_override = p_navigation_map;
  425. NavigationServer3D::get_singleton()->agent_set_map(agent, map_override);
  426. _request_repath();
  427. }
  428. RID NavigationAgent3D::get_navigation_map() const {
  429. if (map_override.is_valid()) {
  430. return map_override;
  431. } else if (agent_parent != nullptr) {
  432. return agent_parent->get_world_3d()->get_navigation_map();
  433. }
  434. return RID();
  435. }
  436. void NavigationAgent3D::set_path_desired_distance(real_t p_path_desired_distance) {
  437. if (Math::is_equal_approx(path_desired_distance, p_path_desired_distance)) {
  438. return;
  439. }
  440. path_desired_distance = p_path_desired_distance;
  441. }
  442. void NavigationAgent3D::set_target_desired_distance(real_t p_target_desired_distance) {
  443. if (Math::is_equal_approx(target_desired_distance, p_target_desired_distance)) {
  444. return;
  445. }
  446. target_desired_distance = p_target_desired_distance;
  447. }
  448. void NavigationAgent3D::set_radius(real_t p_radius) {
  449. ERR_FAIL_COND_MSG(p_radius < 0.0, "Radius must be positive.");
  450. if (Math::is_equal_approx(radius, p_radius)) {
  451. return;
  452. }
  453. radius = p_radius;
  454. NavigationServer3D::get_singleton()->agent_set_radius(agent, radius);
  455. }
  456. void NavigationAgent3D::set_height(real_t p_height) {
  457. ERR_FAIL_COND_MSG(p_height < 0.0, "Height must be positive.");
  458. if (Math::is_equal_approx(height, p_height)) {
  459. return;
  460. }
  461. height = p_height;
  462. NavigationServer3D::get_singleton()->agent_set_height(agent, height);
  463. }
  464. void NavigationAgent3D::set_path_height_offset(real_t p_path_height_offset) {
  465. path_height_offset = p_path_height_offset;
  466. }
  467. void NavigationAgent3D::set_use_3d_avoidance(bool p_use_3d_avoidance) {
  468. use_3d_avoidance = p_use_3d_avoidance;
  469. NavigationServer3D::get_singleton()->agent_set_use_3d_avoidance(agent, use_3d_avoidance);
  470. notify_property_list_changed();
  471. }
  472. void NavigationAgent3D::set_keep_y_velocity(bool p_enabled) {
  473. keep_y_velocity = p_enabled;
  474. stored_y_velocity = 0.0;
  475. }
  476. bool NavigationAgent3D::get_keep_y_velocity() const {
  477. return keep_y_velocity;
  478. }
  479. void NavigationAgent3D::set_neighbor_distance(real_t p_distance) {
  480. if (Math::is_equal_approx(neighbor_distance, p_distance)) {
  481. return;
  482. }
  483. neighbor_distance = p_distance;
  484. NavigationServer3D::get_singleton()->agent_set_neighbor_distance(agent, neighbor_distance);
  485. }
  486. void NavigationAgent3D::set_max_neighbors(int p_count) {
  487. if (max_neighbors == p_count) {
  488. return;
  489. }
  490. max_neighbors = p_count;
  491. NavigationServer3D::get_singleton()->agent_set_max_neighbors(agent, max_neighbors);
  492. }
  493. void NavigationAgent3D::set_time_horizon_agents(real_t p_time_horizon) {
  494. ERR_FAIL_COND_MSG(p_time_horizon < 0.0, "Time horizon must be positive.");
  495. if (Math::is_equal_approx(time_horizon_agents, p_time_horizon)) {
  496. return;
  497. }
  498. time_horizon_agents = p_time_horizon;
  499. NavigationServer3D::get_singleton()->agent_set_time_horizon_agents(agent, time_horizon_agents);
  500. }
  501. void NavigationAgent3D::set_time_horizon_obstacles(real_t p_time_horizon) {
  502. ERR_FAIL_COND_MSG(p_time_horizon < 0.0, "Time horizon must be positive.");
  503. if (Math::is_equal_approx(time_horizon_obstacles, p_time_horizon)) {
  504. return;
  505. }
  506. time_horizon_obstacles = p_time_horizon;
  507. NavigationServer3D::get_singleton()->agent_set_time_horizon_obstacles(agent, time_horizon_obstacles);
  508. }
  509. void NavigationAgent3D::set_max_speed(real_t p_max_speed) {
  510. ERR_FAIL_COND_MSG(p_max_speed < 0.0, "Max speed must be positive.");
  511. if (Math::is_equal_approx(max_speed, p_max_speed)) {
  512. return;
  513. }
  514. max_speed = p_max_speed;
  515. NavigationServer3D::get_singleton()->agent_set_max_speed(agent, max_speed);
  516. }
  517. void NavigationAgent3D::set_path_max_distance(real_t p_path_max_distance) {
  518. if (Math::is_equal_approx(path_max_distance, p_path_max_distance)) {
  519. return;
  520. }
  521. path_max_distance = p_path_max_distance;
  522. }
  523. real_t NavigationAgent3D::get_path_max_distance() {
  524. return path_max_distance;
  525. }
  526. void NavigationAgent3D::set_target_position(Vector3 p_position) {
  527. // 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.
  528. // Revisit later when the navigation server can update the path without requesting a new path.
  529. target_position = p_position;
  530. target_position_submitted = true;
  531. _request_repath();
  532. }
  533. Vector3 NavigationAgent3D::get_target_position() const {
  534. return target_position;
  535. }
  536. Vector3 NavigationAgent3D::get_next_path_position() {
  537. _update_navigation();
  538. const Vector<Vector3> &navigation_path = navigation_result->get_path();
  539. if (navigation_path.size() == 0) {
  540. ERR_FAIL_NULL_V_MSG(agent_parent, Vector3(), "The agent has no parent.");
  541. return agent_parent->get_global_position();
  542. } else {
  543. return navigation_path[navigation_path_index] - Vector3(0, path_height_offset, 0);
  544. }
  545. }
  546. real_t NavigationAgent3D::distance_to_target() const {
  547. ERR_FAIL_NULL_V_MSG(agent_parent, 0.0, "The agent has no parent.");
  548. return agent_parent->get_global_position().distance_to(target_position);
  549. }
  550. bool NavigationAgent3D::is_target_reached() const {
  551. return target_reached;
  552. }
  553. bool NavigationAgent3D::is_target_reachable() {
  554. _update_navigation();
  555. return _is_target_reachable();
  556. }
  557. bool NavigationAgent3D::_is_target_reachable() const {
  558. return target_desired_distance >= _get_final_position().distance_to(target_position);
  559. }
  560. bool NavigationAgent3D::is_navigation_finished() {
  561. _update_navigation();
  562. return navigation_finished;
  563. }
  564. Vector3 NavigationAgent3D::get_final_position() {
  565. _update_navigation();
  566. return _get_final_position();
  567. }
  568. Vector3 NavigationAgent3D::_get_final_position() const {
  569. const Vector<Vector3> &navigation_path = navigation_result->get_path();
  570. if (navigation_path.size() == 0) {
  571. return Vector3();
  572. }
  573. return navigation_path[navigation_path.size() - 1] - Vector3(0, path_height_offset, 0);
  574. }
  575. void NavigationAgent3D::set_velocity_forced(Vector3 p_velocity) {
  576. // Intentionally not checking for equality of the parameter.
  577. // We need to always submit the velocity to the navigation server, even when it is the same, in order to run avoidance every frame.
  578. // Revisit later when the navigation server can update avoidance without users resubmitting the velocity.
  579. velocity_forced = p_velocity;
  580. velocity_forced_submitted = true;
  581. }
  582. void NavigationAgent3D::set_velocity(const Vector3 p_velocity) {
  583. velocity = p_velocity;
  584. velocity_submitted = true;
  585. }
  586. void NavigationAgent3D::_avoidance_done(Vector3 p_new_velocity) {
  587. safe_velocity = p_new_velocity;
  588. if (!use_3d_avoidance) {
  589. safe_velocity.y = stored_y_velocity;
  590. }
  591. emit_signal(SNAME("velocity_computed"), safe_velocity);
  592. }
  593. PackedStringArray NavigationAgent3D::get_configuration_warnings() const {
  594. PackedStringArray warnings = Node::get_configuration_warnings();
  595. if (!Object::cast_to<Node3D>(get_parent())) {
  596. warnings.push_back(RTR("The NavigationAgent3D can be used only under a Node3D inheriting parent node."));
  597. }
  598. return warnings;
  599. }
  600. void NavigationAgent3D::_update_navigation() {
  601. if (agent_parent == nullptr) {
  602. return;
  603. }
  604. if (!agent_parent->is_inside_tree()) {
  605. return;
  606. }
  607. if (!target_position_submitted) {
  608. return;
  609. }
  610. Vector3 origin = agent_parent->get_global_position();
  611. bool reload_path = false;
  612. if (NavigationServer3D::get_singleton()->agent_is_map_changed(agent)) {
  613. reload_path = true;
  614. } else if (navigation_result->get_path().size() == 0) {
  615. reload_path = true;
  616. } else {
  617. // Check if too far from the navigation path
  618. if (navigation_path_index > 0) {
  619. const Vector<Vector3> &navigation_path = navigation_result->get_path();
  620. Vector3 segment[2];
  621. segment[0] = navigation_path[navigation_path_index - 1];
  622. segment[1] = navigation_path[navigation_path_index];
  623. segment[0].y -= path_height_offset;
  624. segment[1].y -= path_height_offset;
  625. Vector3 p = Geometry3D::get_closest_point_to_segment(origin, segment);
  626. if (origin.distance_to(p) >= path_max_distance) {
  627. // To faraway, reload path
  628. reload_path = true;
  629. }
  630. }
  631. }
  632. if (reload_path) {
  633. navigation_query->set_start_position(origin);
  634. navigation_query->set_target_position(target_position);
  635. navigation_query->set_navigation_layers(navigation_layers);
  636. navigation_query->set_metadata_flags(path_metadata_flags);
  637. if (map_override.is_valid()) {
  638. navigation_query->set_map(map_override);
  639. } else {
  640. navigation_query->set_map(agent_parent->get_world_3d()->get_navigation_map());
  641. }
  642. NavigationServer3D::get_singleton()->query_path(navigation_query, navigation_result);
  643. #ifdef DEBUG_ENABLED
  644. debug_path_dirty = true;
  645. #endif // DEBUG_ENABLED
  646. navigation_finished = false;
  647. last_waypoint_reached = false;
  648. navigation_path_index = 0;
  649. emit_signal(SNAME("path_changed"));
  650. }
  651. if (navigation_result->get_path().size() == 0) {
  652. return;
  653. }
  654. // Check if the navigation has already finished.
  655. if (navigation_finished) {
  656. return;
  657. }
  658. // Check if we reached the target.
  659. if (_is_within_target_distance(origin)) {
  660. // Emit waypoint_reached in case we also moved within distance of a waypoint.
  661. _advance_waypoints(origin);
  662. _transition_to_target_reached();
  663. _transition_to_navigation_finished();
  664. } else {
  665. // Advance waypoints if possible.
  666. _advance_waypoints(origin);
  667. // Keep navigation running even after reaching the last waypoint if the target is reachable.
  668. if (last_waypoint_reached && !_is_target_reachable()) {
  669. _transition_to_navigation_finished();
  670. }
  671. }
  672. }
  673. void NavigationAgent3D::_advance_waypoints(const Vector3 &p_origin) {
  674. if (last_waypoint_reached) {
  675. return;
  676. }
  677. // Advance to the farthest possible waypoint.
  678. while (_is_within_waypoint_distance(p_origin)) {
  679. _trigger_waypoint_reached();
  680. if (_is_last_waypoint()) {
  681. last_waypoint_reached = true;
  682. break;
  683. }
  684. _move_to_next_waypoint();
  685. }
  686. }
  687. void NavigationAgent3D::_request_repath() {
  688. navigation_result->reset();
  689. target_reached = false;
  690. navigation_finished = false;
  691. last_waypoint_reached = false;
  692. }
  693. bool NavigationAgent3D::_is_last_waypoint() const {
  694. return navigation_path_index == navigation_result->get_path().size() - 1;
  695. }
  696. void NavigationAgent3D::_move_to_next_waypoint() {
  697. navigation_path_index += 1;
  698. }
  699. bool NavigationAgent3D::_is_within_waypoint_distance(const Vector3 &p_origin) const {
  700. const Vector<Vector3> &navigation_path = navigation_result->get_path();
  701. Vector3 waypoint = navigation_path[navigation_path_index] - Vector3(0, path_height_offset, 0);
  702. return p_origin.distance_to(waypoint) < path_desired_distance;
  703. }
  704. bool NavigationAgent3D::_is_within_target_distance(const Vector3 &p_origin) const {
  705. return p_origin.distance_to(target_position) < target_desired_distance;
  706. }
  707. void NavigationAgent3D::_trigger_waypoint_reached() {
  708. const Vector<Vector3> &navigation_path = navigation_result->get_path();
  709. const Vector<int32_t> &navigation_path_types = navigation_result->get_path_types();
  710. const TypedArray<RID> &navigation_path_rids = navigation_result->get_path_rids();
  711. const Vector<int64_t> &navigation_path_owners = navigation_result->get_path_owner_ids();
  712. Dictionary details;
  713. const Vector3 waypoint = navigation_path[navigation_path_index];
  714. details[CoreStringName(position)] = waypoint;
  715. int waypoint_type = -1;
  716. if (path_metadata_flags.has_flag(NavigationPathQueryParameters3D::PathMetadataFlags::PATH_METADATA_INCLUDE_TYPES)) {
  717. const NavigationPathQueryResult3D::PathSegmentType type = NavigationPathQueryResult3D::PathSegmentType(navigation_path_types[navigation_path_index]);
  718. details[SNAME("type")] = type;
  719. waypoint_type = type;
  720. }
  721. if (path_metadata_flags.has_flag(NavigationPathQueryParameters3D::PathMetadataFlags::PATH_METADATA_INCLUDE_RIDS)) {
  722. details[SNAME("rid")] = navigation_path_rids[navigation_path_index];
  723. }
  724. if (path_metadata_flags.has_flag(NavigationPathQueryParameters3D::PathMetadataFlags::PATH_METADATA_INCLUDE_OWNERS)) {
  725. const ObjectID waypoint_owner_id = ObjectID(navigation_path_owners[navigation_path_index]);
  726. // Get a reference to the owning object.
  727. Object *owner = nullptr;
  728. if (waypoint_owner_id.is_valid()) {
  729. owner = ObjectDB::get_instance(waypoint_owner_id);
  730. }
  731. details[SNAME("owner")] = owner;
  732. if (waypoint_type == NavigationPathQueryResult3D::PATH_SEGMENT_TYPE_LINK) {
  733. const NavigationLink3D *navlink = Object::cast_to<NavigationLink3D>(owner);
  734. if (navlink) {
  735. Vector3 link_global_start_position = navlink->get_global_start_position();
  736. Vector3 link_global_end_position = navlink->get_global_end_position();
  737. if (waypoint.distance_to(link_global_start_position) < waypoint.distance_to(link_global_end_position)) {
  738. details[SNAME("link_entry_position")] = link_global_start_position;
  739. details[SNAME("link_exit_position")] = link_global_end_position;
  740. } else {
  741. details[SNAME("link_entry_position")] = link_global_end_position;
  742. details[SNAME("link_exit_position")] = link_global_start_position;
  743. }
  744. }
  745. }
  746. }
  747. // Emit a signal for the waypoint.
  748. emit_signal(SNAME("waypoint_reached"), details);
  749. // Emit a signal if we've reached a navigation link.
  750. if (waypoint_type == NavigationPathQueryResult3D::PATH_SEGMENT_TYPE_LINK) {
  751. emit_signal(SNAME("link_reached"), details);
  752. }
  753. }
  754. void NavigationAgent3D::_transition_to_navigation_finished() {
  755. navigation_finished = true;
  756. target_position_submitted = false;
  757. if (avoidance_enabled) {
  758. NavigationServer3D::get_singleton()->agent_set_position(agent, agent_parent->get_global_transform().origin);
  759. NavigationServer3D::get_singleton()->agent_set_velocity(agent, Vector3(0.0, 0.0, 0.0));
  760. NavigationServer3D::get_singleton()->agent_set_velocity_forced(agent, Vector3(0.0, 0.0, 0.0));
  761. stored_y_velocity = 0.0;
  762. }
  763. emit_signal(SNAME("navigation_finished"));
  764. }
  765. void NavigationAgent3D::_transition_to_target_reached() {
  766. target_reached = true;
  767. emit_signal(SNAME("target_reached"));
  768. }
  769. void NavigationAgent3D::set_avoidance_layers(uint32_t p_layers) {
  770. avoidance_layers = p_layers;
  771. NavigationServer3D::get_singleton()->agent_set_avoidance_layers(get_rid(), avoidance_layers);
  772. }
  773. uint32_t NavigationAgent3D::get_avoidance_layers() const {
  774. return avoidance_layers;
  775. }
  776. void NavigationAgent3D::set_avoidance_mask(uint32_t p_mask) {
  777. avoidance_mask = p_mask;
  778. NavigationServer3D::get_singleton()->agent_set_avoidance_mask(get_rid(), avoidance_mask);
  779. }
  780. uint32_t NavigationAgent3D::get_avoidance_mask() const {
  781. return avoidance_mask;
  782. }
  783. void NavigationAgent3D::set_avoidance_layer_value(int p_layer_number, bool p_value) {
  784. ERR_FAIL_COND_MSG(p_layer_number < 1, "Avoidance layer number must be between 1 and 32 inclusive.");
  785. ERR_FAIL_COND_MSG(p_layer_number > 32, "Avoidance layer number must be between 1 and 32 inclusive.");
  786. uint32_t avoidance_layers_new = get_avoidance_layers();
  787. if (p_value) {
  788. avoidance_layers_new |= 1 << (p_layer_number - 1);
  789. } else {
  790. avoidance_layers_new &= ~(1 << (p_layer_number - 1));
  791. }
  792. set_avoidance_layers(avoidance_layers_new);
  793. }
  794. bool NavigationAgent3D::get_avoidance_layer_value(int p_layer_number) const {
  795. ERR_FAIL_COND_V_MSG(p_layer_number < 1, false, "Avoidance layer number must be between 1 and 32 inclusive.");
  796. ERR_FAIL_COND_V_MSG(p_layer_number > 32, false, "Avoidance layer number must be between 1 and 32 inclusive.");
  797. return get_avoidance_layers() & (1 << (p_layer_number - 1));
  798. }
  799. void NavigationAgent3D::set_avoidance_mask_value(int p_mask_number, bool p_value) {
  800. ERR_FAIL_COND_MSG(p_mask_number < 1, "Avoidance mask number must be between 1 and 32 inclusive.");
  801. ERR_FAIL_COND_MSG(p_mask_number > 32, "Avoidance mask number must be between 1 and 32 inclusive.");
  802. uint32_t mask = get_avoidance_mask();
  803. if (p_value) {
  804. mask |= 1 << (p_mask_number - 1);
  805. } else {
  806. mask &= ~(1 << (p_mask_number - 1));
  807. }
  808. set_avoidance_mask(mask);
  809. }
  810. bool NavigationAgent3D::get_avoidance_mask_value(int p_mask_number) const {
  811. ERR_FAIL_COND_V_MSG(p_mask_number < 1, false, "Avoidance mask number must be between 1 and 32 inclusive.");
  812. ERR_FAIL_COND_V_MSG(p_mask_number > 32, false, "Avoidance mask number must be between 1 and 32 inclusive.");
  813. return get_avoidance_mask() & (1 << (p_mask_number - 1));
  814. }
  815. void NavigationAgent3D::set_avoidance_priority(real_t p_priority) {
  816. ERR_FAIL_COND_MSG(p_priority < 0.0, "Avoidance priority must be between 0.0 and 1.0 inclusive.");
  817. ERR_FAIL_COND_MSG(p_priority > 1.0, "Avoidance priority must be between 0.0 and 1.0 inclusive.");
  818. avoidance_priority = p_priority;
  819. NavigationServer3D::get_singleton()->agent_set_avoidance_priority(get_rid(), p_priority);
  820. }
  821. real_t NavigationAgent3D::get_avoidance_priority() const {
  822. return avoidance_priority;
  823. }
  824. ////////DEBUG////////////////////////////////////////////////////////////
  825. void NavigationAgent3D::set_debug_enabled(bool p_enabled) {
  826. #ifdef DEBUG_ENABLED
  827. if (debug_enabled == p_enabled) {
  828. return;
  829. }
  830. debug_enabled = p_enabled;
  831. debug_path_dirty = true;
  832. #endif // DEBUG_ENABLED
  833. }
  834. bool NavigationAgent3D::get_debug_enabled() const {
  835. return debug_enabled;
  836. }
  837. void NavigationAgent3D::set_debug_use_custom(bool p_enabled) {
  838. #ifdef DEBUG_ENABLED
  839. if (debug_use_custom == p_enabled) {
  840. return;
  841. }
  842. debug_use_custom = p_enabled;
  843. debug_path_dirty = true;
  844. #endif // DEBUG_ENABLED
  845. }
  846. bool NavigationAgent3D::get_debug_use_custom() const {
  847. return debug_use_custom;
  848. }
  849. void NavigationAgent3D::set_debug_path_custom_color(Color p_color) {
  850. #ifdef DEBUG_ENABLED
  851. if (debug_path_custom_color == p_color) {
  852. return;
  853. }
  854. debug_path_custom_color = p_color;
  855. debug_path_dirty = true;
  856. #endif // DEBUG_ENABLED
  857. }
  858. Color NavigationAgent3D::get_debug_path_custom_color() const {
  859. return debug_path_custom_color;
  860. }
  861. void NavigationAgent3D::set_debug_path_custom_point_size(float p_point_size) {
  862. #ifdef DEBUG_ENABLED
  863. if (Math::is_equal_approx(debug_path_custom_point_size, p_point_size)) {
  864. return;
  865. }
  866. debug_path_custom_point_size = MAX(0.0, p_point_size);
  867. debug_path_dirty = true;
  868. #endif // DEBUG_ENABLED
  869. }
  870. float NavigationAgent3D::get_debug_path_custom_point_size() const {
  871. return debug_path_custom_point_size;
  872. }
  873. #ifdef DEBUG_ENABLED
  874. void NavigationAgent3D::_navigation_debug_changed() {
  875. debug_path_dirty = true;
  876. }
  877. void NavigationAgent3D::_update_debug_path() {
  878. if (!debug_path_dirty) {
  879. return;
  880. }
  881. debug_path_dirty = false;
  882. if (!debug_path_instance.is_valid()) {
  883. debug_path_instance = RenderingServer::get_singleton()->instance_create();
  884. }
  885. if (debug_path_mesh.is_null()) {
  886. debug_path_mesh.instantiate();
  887. }
  888. debug_path_mesh->clear_surfaces();
  889. if (!(debug_enabled && NavigationServer3D::get_singleton()->get_debug_navigation_enable_agent_paths())) {
  890. return;
  891. }
  892. if (!(agent_parent && agent_parent->is_inside_tree())) {
  893. return;
  894. }
  895. const Vector<Vector3> &navigation_path = navigation_result->get_path();
  896. if (navigation_path.size() <= 1) {
  897. return;
  898. }
  899. Vector<Vector3> debug_path_lines_vertex_array;
  900. for (int i = 0; i < navigation_path.size() - 1; i++) {
  901. debug_path_lines_vertex_array.push_back(navigation_path[i]);
  902. debug_path_lines_vertex_array.push_back(navigation_path[i + 1]);
  903. }
  904. Array debug_path_lines_mesh_array;
  905. debug_path_lines_mesh_array.resize(Mesh::ARRAY_MAX);
  906. debug_path_lines_mesh_array[Mesh::ARRAY_VERTEX] = debug_path_lines_vertex_array;
  907. debug_path_mesh->add_surface_from_arrays(Mesh::PRIMITIVE_LINES, debug_path_lines_mesh_array);
  908. Ref<StandardMaterial3D> debug_agent_path_line_material = NavigationServer3D::get_singleton()->get_debug_navigation_agent_path_line_material();
  909. if (debug_use_custom) {
  910. if (!debug_agent_path_line_custom_material.is_valid()) {
  911. debug_agent_path_line_custom_material = debug_agent_path_line_material->duplicate();
  912. }
  913. debug_agent_path_line_custom_material->set_albedo(debug_path_custom_color);
  914. debug_path_mesh->surface_set_material(0, debug_agent_path_line_custom_material);
  915. } else {
  916. debug_path_mesh->surface_set_material(0, debug_agent_path_line_material);
  917. }
  918. if (debug_path_custom_point_size > 0.0) {
  919. Vector<Vector3> debug_path_points_vertex_array;
  920. for (int i = 0; i < navigation_path.size(); i++) {
  921. debug_path_points_vertex_array.push_back(navigation_path[i]);
  922. }
  923. Array debug_path_points_mesh_array;
  924. debug_path_points_mesh_array.resize(Mesh::ARRAY_MAX);
  925. debug_path_points_mesh_array[Mesh::ARRAY_VERTEX] = debug_path_points_vertex_array;
  926. debug_path_mesh->add_surface_from_arrays(Mesh::PRIMITIVE_POINTS, debug_path_points_mesh_array);
  927. Ref<StandardMaterial3D> debug_agent_path_point_material = NavigationServer3D::get_singleton()->get_debug_navigation_agent_path_point_material();
  928. if (debug_use_custom) {
  929. if (!debug_agent_path_point_custom_material.is_valid()) {
  930. debug_agent_path_point_custom_material = debug_agent_path_point_material->duplicate();
  931. }
  932. debug_agent_path_point_custom_material->set_albedo(debug_path_custom_color);
  933. debug_agent_path_point_custom_material->set_point_size(debug_path_custom_point_size);
  934. debug_path_mesh->surface_set_material(1, debug_agent_path_point_custom_material);
  935. } else {
  936. debug_path_mesh->surface_set_material(1, debug_agent_path_point_material);
  937. }
  938. }
  939. RS::get_singleton()->instance_set_base(debug_path_instance, debug_path_mesh->get_rid());
  940. RS::get_singleton()->instance_set_scenario(debug_path_instance, agent_parent->get_world_3d()->get_scenario());
  941. RS::get_singleton()->instance_set_visible(debug_path_instance, agent_parent->is_visible_in_tree());
  942. }
  943. #endif // DEBUG_ENABLED