navigation_agent_2d.cpp 44 KB

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