navigation_agent_3d.cpp 43 KB

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