navigation_agent_3d.cpp 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157
  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/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,None"), "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_a = navigation_path[navigation_path_index - 1];
  621. Vector3 segment_b = navigation_path[navigation_path_index];
  622. segment_a.y -= path_height_offset;
  623. segment_b.y -= path_height_offset;
  624. Vector3 p = Geometry3D::get_closest_point_to_segment(origin, segment_a, segment_b);
  625. if (origin.distance_to(p) >= path_max_distance) {
  626. // To faraway, reload path
  627. reload_path = true;
  628. }
  629. }
  630. }
  631. if (reload_path) {
  632. navigation_query->set_start_position(origin);
  633. navigation_query->set_target_position(target_position);
  634. navigation_query->set_navigation_layers(navigation_layers);
  635. navigation_query->set_metadata_flags(path_metadata_flags);
  636. if (map_override.is_valid()) {
  637. navigation_query->set_map(map_override);
  638. } else {
  639. navigation_query->set_map(agent_parent->get_world_3d()->get_navigation_map());
  640. }
  641. NavigationServer3D::get_singleton()->query_path(navigation_query, navigation_result);
  642. #ifdef DEBUG_ENABLED
  643. debug_path_dirty = true;
  644. #endif // DEBUG_ENABLED
  645. navigation_finished = false;
  646. last_waypoint_reached = false;
  647. navigation_path_index = 0;
  648. emit_signal(SNAME("path_changed"));
  649. }
  650. if (navigation_result->get_path().size() == 0) {
  651. return;
  652. }
  653. // Check if the navigation has already finished.
  654. if (navigation_finished) {
  655. return;
  656. }
  657. // Check if we reached the target.
  658. if (_is_within_target_distance(origin)) {
  659. // Emit waypoint_reached in case we also moved within distance of a waypoint.
  660. _advance_waypoints(origin);
  661. _transition_to_target_reached();
  662. _transition_to_navigation_finished();
  663. } else {
  664. // Advance waypoints if possible.
  665. _advance_waypoints(origin);
  666. // Keep navigation running even after reaching the last waypoint if the target is reachable.
  667. if (last_waypoint_reached && !_is_target_reachable()) {
  668. _transition_to_navigation_finished();
  669. }
  670. }
  671. }
  672. void NavigationAgent3D::_advance_waypoints(const Vector3 &p_origin) {
  673. if (last_waypoint_reached) {
  674. return;
  675. }
  676. // Advance to the farthest possible waypoint.
  677. while (_is_within_waypoint_distance(p_origin)) {
  678. _trigger_waypoint_reached();
  679. if (_is_last_waypoint()) {
  680. last_waypoint_reached = true;
  681. break;
  682. }
  683. _move_to_next_waypoint();
  684. }
  685. }
  686. void NavigationAgent3D::_request_repath() {
  687. navigation_result->reset();
  688. target_reached = false;
  689. navigation_finished = false;
  690. last_waypoint_reached = false;
  691. }
  692. bool NavigationAgent3D::_is_last_waypoint() const {
  693. return navigation_path_index == navigation_result->get_path().size() - 1;
  694. }
  695. void NavigationAgent3D::_move_to_next_waypoint() {
  696. navigation_path_index += 1;
  697. }
  698. bool NavigationAgent3D::_is_within_waypoint_distance(const Vector3 &p_origin) const {
  699. const Vector<Vector3> &navigation_path = navigation_result->get_path();
  700. Vector3 waypoint = navigation_path[navigation_path_index] - Vector3(0, path_height_offset, 0);
  701. return p_origin.distance_to(waypoint) < path_desired_distance;
  702. }
  703. bool NavigationAgent3D::_is_within_target_distance(const Vector3 &p_origin) const {
  704. return p_origin.distance_to(target_position) < target_desired_distance;
  705. }
  706. void NavigationAgent3D::_trigger_waypoint_reached() {
  707. const Vector<Vector3> &navigation_path = navigation_result->get_path();
  708. const Vector<int32_t> &navigation_path_types = navigation_result->get_path_types();
  709. const TypedArray<RID> &navigation_path_rids = navigation_result->get_path_rids();
  710. const Vector<int64_t> &navigation_path_owners = navigation_result->get_path_owner_ids();
  711. Dictionary details;
  712. const Vector3 waypoint = navigation_path[navigation_path_index];
  713. details[CoreStringName(position)] = waypoint;
  714. int waypoint_type = -1;
  715. if (path_metadata_flags.has_flag(NavigationPathQueryParameters3D::PathMetadataFlags::PATH_METADATA_INCLUDE_TYPES)) {
  716. const NavigationPathQueryResult3D::PathSegmentType type = NavigationPathQueryResult3D::PathSegmentType(navigation_path_types[navigation_path_index]);
  717. details[SNAME("type")] = type;
  718. waypoint_type = type;
  719. }
  720. if (path_metadata_flags.has_flag(NavigationPathQueryParameters3D::PathMetadataFlags::PATH_METADATA_INCLUDE_RIDS)) {
  721. details[SNAME("rid")] = navigation_path_rids[navigation_path_index];
  722. }
  723. if (path_metadata_flags.has_flag(NavigationPathQueryParameters3D::PathMetadataFlags::PATH_METADATA_INCLUDE_OWNERS)) {
  724. const ObjectID waypoint_owner_id = ObjectID(navigation_path_owners[navigation_path_index]);
  725. // Get a reference to the owning object.
  726. Object *owner = nullptr;
  727. if (waypoint_owner_id.is_valid()) {
  728. owner = ObjectDB::get_instance(waypoint_owner_id);
  729. }
  730. details[SNAME("owner")] = owner;
  731. if (waypoint_type == NavigationPathQueryResult3D::PATH_SEGMENT_TYPE_LINK) {
  732. const NavigationLink3D *navlink = Object::cast_to<NavigationLink3D>(owner);
  733. if (navlink) {
  734. Vector3 link_global_start_position = navlink->get_global_start_position();
  735. Vector3 link_global_end_position = navlink->get_global_end_position();
  736. if (waypoint.distance_to(link_global_start_position) < waypoint.distance_to(link_global_end_position)) {
  737. details[SNAME("link_entry_position")] = link_global_start_position;
  738. details[SNAME("link_exit_position")] = link_global_end_position;
  739. } else {
  740. details[SNAME("link_entry_position")] = link_global_end_position;
  741. details[SNAME("link_exit_position")] = link_global_start_position;
  742. }
  743. }
  744. }
  745. }
  746. // Emit a signal for the waypoint.
  747. emit_signal(SNAME("waypoint_reached"), details);
  748. // Emit a signal if we've reached a navigation link.
  749. if (waypoint_type == NavigationPathQueryResult3D::PATH_SEGMENT_TYPE_LINK) {
  750. emit_signal(SNAME("link_reached"), details);
  751. }
  752. }
  753. void NavigationAgent3D::_transition_to_navigation_finished() {
  754. navigation_finished = true;
  755. target_position_submitted = false;
  756. if (avoidance_enabled) {
  757. NavigationServer3D::get_singleton()->agent_set_position(agent, agent_parent->get_global_transform().origin);
  758. NavigationServer3D::get_singleton()->agent_set_velocity(agent, Vector3(0.0, 0.0, 0.0));
  759. NavigationServer3D::get_singleton()->agent_set_velocity_forced(agent, Vector3(0.0, 0.0, 0.0));
  760. stored_y_velocity = 0.0;
  761. }
  762. emit_signal(SNAME("navigation_finished"));
  763. }
  764. void NavigationAgent3D::_transition_to_target_reached() {
  765. target_reached = true;
  766. emit_signal(SNAME("target_reached"));
  767. }
  768. void NavigationAgent3D::set_avoidance_layers(uint32_t p_layers) {
  769. avoidance_layers = p_layers;
  770. NavigationServer3D::get_singleton()->agent_set_avoidance_layers(get_rid(), avoidance_layers);
  771. }
  772. uint32_t NavigationAgent3D::get_avoidance_layers() const {
  773. return avoidance_layers;
  774. }
  775. void NavigationAgent3D::set_avoidance_mask(uint32_t p_mask) {
  776. avoidance_mask = p_mask;
  777. NavigationServer3D::get_singleton()->agent_set_avoidance_mask(get_rid(), avoidance_mask);
  778. }
  779. uint32_t NavigationAgent3D::get_avoidance_mask() const {
  780. return avoidance_mask;
  781. }
  782. void NavigationAgent3D::set_avoidance_layer_value(int p_layer_number, bool p_value) {
  783. ERR_FAIL_COND_MSG(p_layer_number < 1, "Avoidance layer number must be between 1 and 32 inclusive.");
  784. ERR_FAIL_COND_MSG(p_layer_number > 32, "Avoidance layer number must be between 1 and 32 inclusive.");
  785. uint32_t avoidance_layers_new = get_avoidance_layers();
  786. if (p_value) {
  787. avoidance_layers_new |= 1 << (p_layer_number - 1);
  788. } else {
  789. avoidance_layers_new &= ~(1 << (p_layer_number - 1));
  790. }
  791. set_avoidance_layers(avoidance_layers_new);
  792. }
  793. bool NavigationAgent3D::get_avoidance_layer_value(int p_layer_number) const {
  794. ERR_FAIL_COND_V_MSG(p_layer_number < 1, false, "Avoidance layer number must be between 1 and 32 inclusive.");
  795. ERR_FAIL_COND_V_MSG(p_layer_number > 32, false, "Avoidance layer number must be between 1 and 32 inclusive.");
  796. return get_avoidance_layers() & (1 << (p_layer_number - 1));
  797. }
  798. void NavigationAgent3D::set_avoidance_mask_value(int p_mask_number, bool p_value) {
  799. ERR_FAIL_COND_MSG(p_mask_number < 1, "Avoidance mask number must be between 1 and 32 inclusive.");
  800. ERR_FAIL_COND_MSG(p_mask_number > 32, "Avoidance mask number must be between 1 and 32 inclusive.");
  801. uint32_t mask = get_avoidance_mask();
  802. if (p_value) {
  803. mask |= 1 << (p_mask_number - 1);
  804. } else {
  805. mask &= ~(1 << (p_mask_number - 1));
  806. }
  807. set_avoidance_mask(mask);
  808. }
  809. bool NavigationAgent3D::get_avoidance_mask_value(int p_mask_number) const {
  810. ERR_FAIL_COND_V_MSG(p_mask_number < 1, false, "Avoidance mask number must be between 1 and 32 inclusive.");
  811. ERR_FAIL_COND_V_MSG(p_mask_number > 32, false, "Avoidance mask number must be between 1 and 32 inclusive.");
  812. return get_avoidance_mask() & (1 << (p_mask_number - 1));
  813. }
  814. void NavigationAgent3D::set_avoidance_priority(real_t p_priority) {
  815. ERR_FAIL_COND_MSG(p_priority < 0.0, "Avoidance priority must be between 0.0 and 1.0 inclusive.");
  816. ERR_FAIL_COND_MSG(p_priority > 1.0, "Avoidance priority must be between 0.0 and 1.0 inclusive.");
  817. avoidance_priority = p_priority;
  818. NavigationServer3D::get_singleton()->agent_set_avoidance_priority(get_rid(), p_priority);
  819. }
  820. real_t NavigationAgent3D::get_avoidance_priority() const {
  821. return avoidance_priority;
  822. }
  823. ////////DEBUG////////////////////////////////////////////////////////////
  824. void NavigationAgent3D::set_debug_enabled(bool p_enabled) {
  825. #ifdef DEBUG_ENABLED
  826. if (debug_enabled == p_enabled) {
  827. return;
  828. }
  829. debug_enabled = p_enabled;
  830. debug_path_dirty = true;
  831. #endif // DEBUG_ENABLED
  832. }
  833. bool NavigationAgent3D::get_debug_enabled() const {
  834. return debug_enabled;
  835. }
  836. void NavigationAgent3D::set_debug_use_custom(bool p_enabled) {
  837. #ifdef DEBUG_ENABLED
  838. if (debug_use_custom == p_enabled) {
  839. return;
  840. }
  841. debug_use_custom = p_enabled;
  842. debug_path_dirty = true;
  843. #endif // DEBUG_ENABLED
  844. }
  845. bool NavigationAgent3D::get_debug_use_custom() const {
  846. return debug_use_custom;
  847. }
  848. void NavigationAgent3D::set_debug_path_custom_color(Color p_color) {
  849. #ifdef DEBUG_ENABLED
  850. if (debug_path_custom_color == p_color) {
  851. return;
  852. }
  853. debug_path_custom_color = p_color;
  854. debug_path_dirty = true;
  855. #endif // DEBUG_ENABLED
  856. }
  857. Color NavigationAgent3D::get_debug_path_custom_color() const {
  858. return debug_path_custom_color;
  859. }
  860. void NavigationAgent3D::set_debug_path_custom_point_size(float p_point_size) {
  861. #ifdef DEBUG_ENABLED
  862. if (Math::is_equal_approx(debug_path_custom_point_size, p_point_size)) {
  863. return;
  864. }
  865. debug_path_custom_point_size = MAX(0.0, p_point_size);
  866. debug_path_dirty = true;
  867. #endif // DEBUG_ENABLED
  868. }
  869. float NavigationAgent3D::get_debug_path_custom_point_size() const {
  870. return debug_path_custom_point_size;
  871. }
  872. #ifdef DEBUG_ENABLED
  873. void NavigationAgent3D::_navigation_debug_changed() {
  874. debug_path_dirty = true;
  875. }
  876. void NavigationAgent3D::_update_debug_path() {
  877. if (!debug_path_dirty) {
  878. return;
  879. }
  880. debug_path_dirty = false;
  881. if (!debug_path_instance.is_valid()) {
  882. debug_path_instance = RenderingServer::get_singleton()->instance_create();
  883. }
  884. if (debug_path_mesh.is_null()) {
  885. debug_path_mesh.instantiate();
  886. }
  887. debug_path_mesh->clear_surfaces();
  888. if (!(debug_enabled && NavigationServer3D::get_singleton()->get_debug_navigation_enable_agent_paths())) {
  889. return;
  890. }
  891. if (!(agent_parent && agent_parent->is_inside_tree())) {
  892. return;
  893. }
  894. const Vector<Vector3> &navigation_path = navigation_result->get_path();
  895. if (navigation_path.size() <= 1) {
  896. return;
  897. }
  898. Vector<Vector3> debug_path_lines_vertex_array;
  899. for (int i = 0; i < navigation_path.size() - 1; i++) {
  900. debug_path_lines_vertex_array.push_back(navigation_path[i]);
  901. debug_path_lines_vertex_array.push_back(navigation_path[i + 1]);
  902. }
  903. Array debug_path_lines_mesh_array;
  904. debug_path_lines_mesh_array.resize(Mesh::ARRAY_MAX);
  905. debug_path_lines_mesh_array[Mesh::ARRAY_VERTEX] = debug_path_lines_vertex_array;
  906. debug_path_mesh->add_surface_from_arrays(Mesh::PRIMITIVE_LINES, debug_path_lines_mesh_array);
  907. Ref<StandardMaterial3D> debug_agent_path_line_material = NavigationServer3D::get_singleton()->get_debug_navigation_agent_path_line_material();
  908. if (debug_use_custom) {
  909. if (debug_agent_path_line_custom_material.is_null()) {
  910. debug_agent_path_line_custom_material = debug_agent_path_line_material->duplicate();
  911. }
  912. debug_agent_path_line_custom_material->set_albedo(debug_path_custom_color);
  913. debug_path_mesh->surface_set_material(0, debug_agent_path_line_custom_material);
  914. } else {
  915. debug_path_mesh->surface_set_material(0, debug_agent_path_line_material);
  916. }
  917. if (debug_path_custom_point_size > 0.0) {
  918. Vector<Vector3> debug_path_points_vertex_array;
  919. for (int i = 0; i < navigation_path.size(); i++) {
  920. debug_path_points_vertex_array.push_back(navigation_path[i]);
  921. }
  922. Array debug_path_points_mesh_array;
  923. debug_path_points_mesh_array.resize(Mesh::ARRAY_MAX);
  924. debug_path_points_mesh_array[Mesh::ARRAY_VERTEX] = debug_path_points_vertex_array;
  925. debug_path_mesh->add_surface_from_arrays(Mesh::PRIMITIVE_POINTS, debug_path_points_mesh_array);
  926. Ref<StandardMaterial3D> debug_agent_path_point_material = NavigationServer3D::get_singleton()->get_debug_navigation_agent_path_point_material();
  927. if (debug_use_custom) {
  928. if (debug_agent_path_point_custom_material.is_null()) {
  929. debug_agent_path_point_custom_material = debug_agent_path_point_material->duplicate();
  930. }
  931. debug_agent_path_point_custom_material->set_albedo(debug_path_custom_color);
  932. debug_agent_path_point_custom_material->set_point_size(debug_path_custom_point_size);
  933. debug_path_mesh->surface_set_material(1, debug_agent_path_point_custom_material);
  934. } else {
  935. debug_path_mesh->surface_set_material(1, debug_agent_path_point_material);
  936. }
  937. }
  938. RS::get_singleton()->instance_set_base(debug_path_instance, debug_path_mesh->get_rid());
  939. RS::get_singleton()->instance_set_scenario(debug_path_instance, agent_parent->get_world_3d()->get_scenario());
  940. RS::get_singleton()->instance_set_visible(debug_path_instance, agent_parent->is_visible_in_tree());
  941. }
  942. #endif // DEBUG_ENABLED