navigation_agent_3d.cpp 44 KB

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