navigation_agent_2d.cpp 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857
  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", "time_horizon"), &NavigationAgent2D::set_time_horizon);
  50. ClassDB::bind_method(D_METHOD("get_time_horizon"), &NavigationAgent2D::get_time_horizon);
  51. ClassDB::bind_method(D_METHOD("set_max_speed", "max_speed"), &NavigationAgent2D::set_max_speed);
  52. ClassDB::bind_method(D_METHOD("get_max_speed"), &NavigationAgent2D::get_max_speed);
  53. ClassDB::bind_method(D_METHOD("set_path_max_distance", "max_speed"), &NavigationAgent2D::set_path_max_distance);
  54. ClassDB::bind_method(D_METHOD("get_path_max_distance"), &NavigationAgent2D::get_path_max_distance);
  55. ClassDB::bind_method(D_METHOD("set_navigation_layers", "navigation_layers"), &NavigationAgent2D::set_navigation_layers);
  56. ClassDB::bind_method(D_METHOD("get_navigation_layers"), &NavigationAgent2D::get_navigation_layers);
  57. ClassDB::bind_method(D_METHOD("set_navigation_layer_value", "layer_number", "value"), &NavigationAgent2D::set_navigation_layer_value);
  58. ClassDB::bind_method(D_METHOD("get_navigation_layer_value", "layer_number"), &NavigationAgent2D::get_navigation_layer_value);
  59. ClassDB::bind_method(D_METHOD("set_pathfinding_algorithm", "pathfinding_algorithm"), &NavigationAgent2D::set_pathfinding_algorithm);
  60. ClassDB::bind_method(D_METHOD("get_pathfinding_algorithm"), &NavigationAgent2D::get_pathfinding_algorithm);
  61. ClassDB::bind_method(D_METHOD("set_path_postprocessing", "path_postprocessing"), &NavigationAgent2D::set_path_postprocessing);
  62. ClassDB::bind_method(D_METHOD("get_path_postprocessing"), &NavigationAgent2D::get_path_postprocessing);
  63. ClassDB::bind_method(D_METHOD("set_path_metadata_flags", "flags"), &NavigationAgent2D::set_path_metadata_flags);
  64. ClassDB::bind_method(D_METHOD("get_path_metadata_flags"), &NavigationAgent2D::get_path_metadata_flags);
  65. ClassDB::bind_method(D_METHOD("set_navigation_map", "navigation_map"), &NavigationAgent2D::set_navigation_map);
  66. ClassDB::bind_method(D_METHOD("get_navigation_map"), &NavigationAgent2D::get_navigation_map);
  67. ClassDB::bind_method(D_METHOD("set_target_position", "position"), &NavigationAgent2D::set_target_position);
  68. ClassDB::bind_method(D_METHOD("get_target_position"), &NavigationAgent2D::get_target_position);
  69. ClassDB::bind_method(D_METHOD("get_next_path_position"), &NavigationAgent2D::get_next_path_position);
  70. ClassDB::bind_method(D_METHOD("distance_to_target"), &NavigationAgent2D::distance_to_target);
  71. ClassDB::bind_method(D_METHOD("set_velocity", "velocity"), &NavigationAgent2D::set_velocity);
  72. ClassDB::bind_method(D_METHOD("get_current_navigation_result"), &NavigationAgent2D::get_current_navigation_result);
  73. ClassDB::bind_method(D_METHOD("get_current_navigation_path"), &NavigationAgent2D::get_current_navigation_path);
  74. ClassDB::bind_method(D_METHOD("get_current_navigation_path_index"), &NavigationAgent2D::get_current_navigation_path_index);
  75. ClassDB::bind_method(D_METHOD("is_target_reached"), &NavigationAgent2D::is_target_reached);
  76. ClassDB::bind_method(D_METHOD("is_target_reachable"), &NavigationAgent2D::is_target_reachable);
  77. ClassDB::bind_method(D_METHOD("is_navigation_finished"), &NavigationAgent2D::is_navigation_finished);
  78. ClassDB::bind_method(D_METHOD("get_final_position"), &NavigationAgent2D::get_final_position);
  79. ClassDB::bind_method(D_METHOD("_avoidance_done", "new_velocity"), &NavigationAgent2D::_avoidance_done);
  80. ADD_GROUP("Pathfinding", "");
  81. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "target_position", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_NO_EDITOR), "set_target_position", "get_target_position");
  82. 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");
  83. 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");
  84. 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");
  85. ADD_PROPERTY(PropertyInfo(Variant::INT, "navigation_layers", PROPERTY_HINT_LAYERS_2D_NAVIGATION), "set_navigation_layers", "get_navigation_layers");
  86. ADD_PROPERTY(PropertyInfo(Variant::INT, "pathfinding_algorithm", PROPERTY_HINT_ENUM, "AStar"), "set_pathfinding_algorithm", "get_pathfinding_algorithm");
  87. ADD_PROPERTY(PropertyInfo(Variant::INT, "path_postprocessing", PROPERTY_HINT_ENUM, "Corridorfunnel,Edgecentered"), "set_path_postprocessing", "get_path_postprocessing");
  88. 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");
  89. ADD_GROUP("Avoidance", "");
  90. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "avoidance_enabled"), "set_avoidance_enabled", "get_avoidance_enabled");
  91. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "radius", PROPERTY_HINT_RANGE, "0.1,500,0.01,or_greater,suffix:px"), "set_radius", "get_radius");
  92. 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");
  93. ADD_PROPERTY(PropertyInfo(Variant::INT, "max_neighbors", PROPERTY_HINT_RANGE, "1,10000,or_greater,1"), "set_max_neighbors", "get_max_neighbors");
  94. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "time_horizon", PROPERTY_HINT_RANGE, "0.1,10,0.01,or_greater,suffix:s"), "set_time_horizon", "get_time_horizon");
  95. ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "max_speed", PROPERTY_HINT_RANGE, "0.1,10000,0.01,or_greater,suffix:px/s"), "set_max_speed", "get_max_speed");
  96. ClassDB::bind_method(D_METHOD("set_debug_enabled", "enabled"), &NavigationAgent2D::set_debug_enabled);
  97. ClassDB::bind_method(D_METHOD("get_debug_enabled"), &NavigationAgent2D::get_debug_enabled);
  98. ClassDB::bind_method(D_METHOD("set_debug_use_custom", "enabled"), &NavigationAgent2D::set_debug_use_custom);
  99. ClassDB::bind_method(D_METHOD("get_debug_use_custom"), &NavigationAgent2D::get_debug_use_custom);
  100. ClassDB::bind_method(D_METHOD("set_debug_path_custom_color", "color"), &NavigationAgent2D::set_debug_path_custom_color);
  101. ClassDB::bind_method(D_METHOD("get_debug_path_custom_color"), &NavigationAgent2D::get_debug_path_custom_color);
  102. ClassDB::bind_method(D_METHOD("set_debug_path_custom_point_size", "point_size"), &NavigationAgent2D::set_debug_path_custom_point_size);
  103. ClassDB::bind_method(D_METHOD("get_debug_path_custom_point_size"), &NavigationAgent2D::get_debug_path_custom_point_size);
  104. ClassDB::bind_method(D_METHOD("set_debug_path_custom_line_width", "line_width"), &NavigationAgent2D::set_debug_path_custom_line_width);
  105. ClassDB::bind_method(D_METHOD("get_debug_path_custom_line_width"), &NavigationAgent2D::get_debug_path_custom_line_width);
  106. ADD_GROUP("Debug", "debug_");
  107. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "debug_enabled"), "set_debug_enabled", "get_debug_enabled");
  108. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "debug_use_custom"), "set_debug_use_custom", "get_debug_use_custom");
  109. ADD_PROPERTY(PropertyInfo(Variant::COLOR, "debug_path_custom_color"), "set_debug_path_custom_color", "get_debug_path_custom_color");
  110. 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");
  111. 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");
  112. ADD_SIGNAL(MethodInfo("path_changed"));
  113. ADD_SIGNAL(MethodInfo("target_reached"));
  114. ADD_SIGNAL(MethodInfo("waypoint_reached", PropertyInfo(Variant::DICTIONARY, "details")));
  115. ADD_SIGNAL(MethodInfo("link_reached", PropertyInfo(Variant::DICTIONARY, "details")));
  116. ADD_SIGNAL(MethodInfo("navigation_finished"));
  117. ADD_SIGNAL(MethodInfo("velocity_computed", PropertyInfo(Variant::VECTOR2, "safe_velocity")));
  118. }
  119. void NavigationAgent2D::_notification(int p_what) {
  120. switch (p_what) {
  121. case NOTIFICATION_POST_ENTER_TREE: {
  122. // need to use POST_ENTER_TREE cause with normal ENTER_TREE not all required Nodes are ready.
  123. // cannot use READY as ready does not get called if Node is re-added to SceneTree
  124. set_agent_parent(get_parent());
  125. set_physics_process_internal(true);
  126. if (agent_parent && avoidance_enabled) {
  127. NavigationServer2D::get_singleton()->agent_set_position(agent, agent_parent->get_global_position());
  128. }
  129. #ifdef DEBUG_ENABLED
  130. if (NavigationServer2D::get_singleton()->get_debug_enabled()) {
  131. debug_path_dirty = true;
  132. }
  133. #endif // DEBUG_ENABLED
  134. } break;
  135. case NOTIFICATION_PARENTED: {
  136. if (is_inside_tree() && (get_parent() != agent_parent)) {
  137. // only react to PARENTED notifications when already inside_tree and parent changed, e.g. users switch nodes around
  138. // PARENTED notification fires also when Node is added in scripts to a parent
  139. // this would spam transforms fails and world fails while Node is outside SceneTree
  140. // when node gets reparented when joining the tree POST_ENTER_TREE takes care of this
  141. set_agent_parent(get_parent());
  142. set_physics_process_internal(true);
  143. }
  144. } break;
  145. case NOTIFICATION_UNPARENTED: {
  146. // if agent has no parent no point in processing it until reparented
  147. set_agent_parent(nullptr);
  148. set_physics_process_internal(false);
  149. } break;
  150. case NOTIFICATION_EXIT_TREE: {
  151. set_agent_parent(nullptr);
  152. set_physics_process_internal(false);
  153. #ifdef DEBUG_ENABLED
  154. if (debug_path_instance.is_valid()) {
  155. RenderingServer::get_singleton()->canvas_item_set_visible(debug_path_instance, false);
  156. }
  157. #endif // DEBUG_ENABLED
  158. } break;
  159. case NOTIFICATION_PAUSED: {
  160. if (agent_parent && !agent_parent->can_process()) {
  161. map_before_pause = NavigationServer2D::get_singleton()->agent_get_map(get_rid());
  162. NavigationServer2D::get_singleton()->agent_set_map(get_rid(), RID());
  163. } else if (agent_parent && agent_parent->can_process() && !(map_before_pause == RID())) {
  164. NavigationServer2D::get_singleton()->agent_set_map(get_rid(), map_before_pause);
  165. map_before_pause = RID();
  166. }
  167. } break;
  168. case NOTIFICATION_UNPAUSED: {
  169. if (agent_parent && !agent_parent->can_process()) {
  170. map_before_pause = NavigationServer2D::get_singleton()->agent_get_map(get_rid());
  171. NavigationServer2D::get_singleton()->agent_set_map(get_rid(), RID());
  172. } else if (agent_parent && agent_parent->can_process() && !(map_before_pause == RID())) {
  173. NavigationServer2D::get_singleton()->agent_set_map(get_rid(), map_before_pause);
  174. map_before_pause = RID();
  175. }
  176. } break;
  177. case NOTIFICATION_INTERNAL_PHYSICS_PROCESS: {
  178. if (agent_parent && target_position_submitted) {
  179. if (avoidance_enabled) {
  180. // agent_position on NavigationServer is avoidance only and has nothing to do with pathfinding
  181. // no point in flooding NavigationServer queue with agent position updates that get send to the void if avoidance is not used
  182. NavigationServer2D::get_singleton()->agent_set_position(agent, agent_parent->get_global_position());
  183. }
  184. _check_distance_to_target();
  185. }
  186. #ifdef DEBUG_ENABLED
  187. if (debug_path_dirty) {
  188. _update_debug_path();
  189. }
  190. #endif // DEBUG_ENABLED
  191. } break;
  192. }
  193. }
  194. NavigationAgent2D::NavigationAgent2D() {
  195. agent = NavigationServer2D::get_singleton()->agent_create();
  196. NavigationServer2D::get_singleton()->agent_set_neighbor_distance(agent, neighbor_distance);
  197. NavigationServer2D::get_singleton()->agent_set_max_neighbors(agent, max_neighbors);
  198. NavigationServer2D::get_singleton()->agent_set_time_horizon(agent, time_horizon);
  199. NavigationServer2D::get_singleton()->agent_set_radius(agent, radius);
  200. NavigationServer2D::get_singleton()->agent_set_max_speed(agent, max_speed);
  201. // Preallocate query and result objects to improve performance.
  202. navigation_query = Ref<NavigationPathQueryParameters2D>();
  203. navigation_query.instantiate();
  204. navigation_result = Ref<NavigationPathQueryResult2D>();
  205. navigation_result.instantiate();
  206. #ifdef DEBUG_ENABLED
  207. NavigationServer2D::get_singleton()->connect(SNAME("navigation_debug_changed"), callable_mp(this, &NavigationAgent2D::_navigation_debug_changed));
  208. #endif // DEBUG_ENABLED
  209. }
  210. NavigationAgent2D::~NavigationAgent2D() {
  211. ERR_FAIL_NULL(NavigationServer2D::get_singleton());
  212. NavigationServer2D::get_singleton()->free(agent);
  213. agent = RID(); // Pointless
  214. #ifdef DEBUG_ENABLED
  215. NavigationServer2D::get_singleton()->disconnect(SNAME("navigation_debug_changed"), callable_mp(this, &NavigationAgent2D::_navigation_debug_changed));
  216. ERR_FAIL_NULL(RenderingServer::get_singleton());
  217. if (debug_path_instance.is_valid()) {
  218. RenderingServer::get_singleton()->free(debug_path_instance);
  219. }
  220. #endif // DEBUG_ENABLED
  221. }
  222. void NavigationAgent2D::set_avoidance_enabled(bool p_enabled) {
  223. if (avoidance_enabled == p_enabled) {
  224. return;
  225. }
  226. avoidance_enabled = p_enabled;
  227. if (avoidance_enabled) {
  228. NavigationServer2D::get_singleton()->agent_set_callback(agent, callable_mp(this, &NavigationAgent2D::_avoidance_done));
  229. } else {
  230. NavigationServer2D::get_singleton()->agent_set_callback(agent, Callable());
  231. }
  232. }
  233. bool NavigationAgent2D::get_avoidance_enabled() const {
  234. return avoidance_enabled;
  235. }
  236. void NavigationAgent2D::set_agent_parent(Node *p_agent_parent) {
  237. if (agent_parent == p_agent_parent) {
  238. return;
  239. }
  240. // remove agent from any avoidance map before changing parent or there will be leftovers on the RVO map
  241. NavigationServer2D::get_singleton()->agent_set_callback(agent, Callable());
  242. if (Object::cast_to<Node2D>(p_agent_parent) != nullptr) {
  243. // place agent on navigation map first or else the RVO agent callback creation fails silently later
  244. agent_parent = Object::cast_to<Node2D>(p_agent_parent);
  245. if (map_override.is_valid()) {
  246. NavigationServer2D::get_singleton()->agent_set_map(get_rid(), map_override);
  247. } else {
  248. NavigationServer2D::get_singleton()->agent_set_map(get_rid(), agent_parent->get_world_2d()->get_navigation_map());
  249. }
  250. // create new avoidance callback if enabled
  251. if (avoidance_enabled) {
  252. NavigationServer2D::get_singleton()->agent_set_callback(agent, callable_mp(this, &NavigationAgent2D::_avoidance_done));
  253. }
  254. } else {
  255. agent_parent = nullptr;
  256. NavigationServer2D::get_singleton()->agent_set_map(get_rid(), RID());
  257. }
  258. }
  259. void NavigationAgent2D::set_navigation_layers(uint32_t p_navigation_layers) {
  260. if (navigation_layers == p_navigation_layers) {
  261. return;
  262. }
  263. navigation_layers = p_navigation_layers;
  264. _request_repath();
  265. }
  266. uint32_t NavigationAgent2D::get_navigation_layers() const {
  267. return navigation_layers;
  268. }
  269. void NavigationAgent2D::set_navigation_layer_value(int p_layer_number, bool p_value) {
  270. ERR_FAIL_COND_MSG(p_layer_number < 1, "Navigation layer number must be between 1 and 32 inclusive.");
  271. ERR_FAIL_COND_MSG(p_layer_number > 32, "Navigation layer number must be between 1 and 32 inclusive.");
  272. uint32_t _navigation_layers = get_navigation_layers();
  273. if (p_value) {
  274. _navigation_layers |= 1 << (p_layer_number - 1);
  275. } else {
  276. _navigation_layers &= ~(1 << (p_layer_number - 1));
  277. }
  278. set_navigation_layers(_navigation_layers);
  279. }
  280. bool NavigationAgent2D::get_navigation_layer_value(int p_layer_number) const {
  281. ERR_FAIL_COND_V_MSG(p_layer_number < 1, false, "Navigation layer number must be between 1 and 32 inclusive.");
  282. ERR_FAIL_COND_V_MSG(p_layer_number > 32, false, "Navigation layer number must be between 1 and 32 inclusive.");
  283. return get_navigation_layers() & (1 << (p_layer_number - 1));
  284. }
  285. void NavigationAgent2D::set_pathfinding_algorithm(const NavigationPathQueryParameters2D::PathfindingAlgorithm p_pathfinding_algorithm) {
  286. if (pathfinding_algorithm == p_pathfinding_algorithm) {
  287. return;
  288. }
  289. pathfinding_algorithm = p_pathfinding_algorithm;
  290. navigation_query->set_pathfinding_algorithm(pathfinding_algorithm);
  291. }
  292. void NavigationAgent2D::set_path_postprocessing(const NavigationPathQueryParameters2D::PathPostProcessing p_path_postprocessing) {
  293. if (path_postprocessing == p_path_postprocessing) {
  294. return;
  295. }
  296. path_postprocessing = p_path_postprocessing;
  297. navigation_query->set_path_postprocessing(path_postprocessing);
  298. }
  299. void NavigationAgent2D::set_path_metadata_flags(BitField<NavigationPathQueryParameters2D::PathMetadataFlags> p_path_metadata_flags) {
  300. if (path_metadata_flags == p_path_metadata_flags) {
  301. return;
  302. }
  303. path_metadata_flags = p_path_metadata_flags;
  304. }
  305. void NavigationAgent2D::set_navigation_map(RID p_navigation_map) {
  306. if (map_override == p_navigation_map) {
  307. return;
  308. }
  309. map_override = p_navigation_map;
  310. NavigationServer2D::get_singleton()->agent_set_map(agent, map_override);
  311. _request_repath();
  312. }
  313. RID NavigationAgent2D::get_navigation_map() const {
  314. if (map_override.is_valid()) {
  315. return map_override;
  316. } else if (agent_parent != nullptr) {
  317. return agent_parent->get_world_2d()->get_navigation_map();
  318. }
  319. return RID();
  320. }
  321. void NavigationAgent2D::set_path_desired_distance(real_t p_path_desired_distance) {
  322. if (Math::is_equal_approx(path_desired_distance, p_path_desired_distance)) {
  323. return;
  324. }
  325. path_desired_distance = p_path_desired_distance;
  326. }
  327. void NavigationAgent2D::set_target_desired_distance(real_t p_target_desired_distance) {
  328. if (Math::is_equal_approx(target_desired_distance, p_target_desired_distance)) {
  329. return;
  330. }
  331. target_desired_distance = p_target_desired_distance;
  332. }
  333. void NavigationAgent2D::set_radius(real_t p_radius) {
  334. if (Math::is_equal_approx(radius, p_radius)) {
  335. return;
  336. }
  337. radius = p_radius;
  338. NavigationServer2D::get_singleton()->agent_set_radius(agent, radius);
  339. }
  340. void NavigationAgent2D::set_neighbor_distance(real_t p_distance) {
  341. if (Math::is_equal_approx(neighbor_distance, p_distance)) {
  342. return;
  343. }
  344. neighbor_distance = p_distance;
  345. NavigationServer2D::get_singleton()->agent_set_neighbor_distance(agent, neighbor_distance);
  346. }
  347. void NavigationAgent2D::set_max_neighbors(int p_count) {
  348. if (max_neighbors == p_count) {
  349. return;
  350. }
  351. max_neighbors = p_count;
  352. NavigationServer2D::get_singleton()->agent_set_max_neighbors(agent, max_neighbors);
  353. }
  354. void NavigationAgent2D::set_time_horizon(real_t p_time) {
  355. if (Math::is_equal_approx(time_horizon, p_time)) {
  356. return;
  357. }
  358. time_horizon = p_time;
  359. NavigationServer2D::get_singleton()->agent_set_time_horizon(agent, time_horizon);
  360. }
  361. void NavigationAgent2D::set_max_speed(real_t p_max_speed) {
  362. if (Math::is_equal_approx(max_speed, p_max_speed)) {
  363. return;
  364. }
  365. max_speed = p_max_speed;
  366. NavigationServer2D::get_singleton()->agent_set_max_speed(agent, max_speed);
  367. }
  368. void NavigationAgent2D::set_path_max_distance(real_t p_path_max_distance) {
  369. if (Math::is_equal_approx(path_max_distance, p_path_max_distance)) {
  370. return;
  371. }
  372. path_max_distance = p_path_max_distance;
  373. }
  374. real_t NavigationAgent2D::get_path_max_distance() {
  375. return path_max_distance;
  376. }
  377. void NavigationAgent2D::set_target_position(Vector2 p_position) {
  378. // 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.
  379. // Revisit later when the navigation server can update the path without requesting a new path.
  380. target_position = p_position;
  381. target_position_submitted = true;
  382. _request_repath();
  383. }
  384. Vector2 NavigationAgent2D::get_target_position() const {
  385. return target_position;
  386. }
  387. Vector2 NavigationAgent2D::get_next_path_position() {
  388. update_navigation();
  389. const Vector<Vector2> &navigation_path = navigation_result->get_path();
  390. if (navigation_path.size() == 0) {
  391. ERR_FAIL_COND_V_MSG(agent_parent == nullptr, Vector2(), "The agent has no parent.");
  392. return agent_parent->get_global_position();
  393. } else {
  394. return navigation_path[navigation_path_index];
  395. }
  396. }
  397. real_t NavigationAgent2D::distance_to_target() const {
  398. ERR_FAIL_COND_V_MSG(agent_parent == nullptr, 0.0, "The agent has no parent.");
  399. return agent_parent->get_global_position().distance_to(target_position);
  400. }
  401. bool NavigationAgent2D::is_target_reached() const {
  402. return target_reached;
  403. }
  404. bool NavigationAgent2D::is_target_reachable() {
  405. return target_desired_distance >= get_final_position().distance_to(target_position);
  406. }
  407. bool NavigationAgent2D::is_navigation_finished() {
  408. update_navigation();
  409. return navigation_finished;
  410. }
  411. Vector2 NavigationAgent2D::get_final_position() {
  412. update_navigation();
  413. const Vector<Vector2> &navigation_path = navigation_result->get_path();
  414. if (navigation_path.size() == 0) {
  415. return Vector2();
  416. }
  417. return navigation_path[navigation_path.size() - 1];
  418. }
  419. void NavigationAgent2D::set_velocity(Vector2 p_velocity) {
  420. // Intentionally not checking for equality of the parameter.
  421. // We need to always submit the velocity to the navigation server, even when it is the same, in order to run avoidance every frame.
  422. // Revisit later when the navigation server can update avoidance without users resubmitting the velocity.
  423. target_velocity = p_velocity;
  424. velocity_submitted = true;
  425. NavigationServer2D::get_singleton()->agent_set_target_velocity(agent, target_velocity);
  426. NavigationServer2D::get_singleton()->agent_set_velocity(agent, prev_safe_velocity);
  427. }
  428. void NavigationAgent2D::_avoidance_done(Vector3 p_new_velocity) {
  429. const Vector2 velocity = Vector2(p_new_velocity.x, p_new_velocity.z);
  430. prev_safe_velocity = velocity;
  431. if (!velocity_submitted) {
  432. target_velocity = Vector2();
  433. return;
  434. }
  435. velocity_submitted = false;
  436. emit_signal(SNAME("velocity_computed"), velocity);
  437. }
  438. PackedStringArray NavigationAgent2D::get_configuration_warnings() const {
  439. PackedStringArray warnings = Node::get_configuration_warnings();
  440. if (!Object::cast_to<Node2D>(get_parent())) {
  441. warnings.push_back(RTR("The NavigationAgent2D can be used only under a Node2D inheriting parent node."));
  442. }
  443. return warnings;
  444. }
  445. void NavigationAgent2D::update_navigation() {
  446. if (agent_parent == nullptr) {
  447. return;
  448. }
  449. if (!agent_parent->is_inside_tree()) {
  450. return;
  451. }
  452. if (!target_position_submitted) {
  453. return;
  454. }
  455. if (update_frame_id == Engine::get_singleton()->get_physics_frames()) {
  456. return;
  457. }
  458. update_frame_id = Engine::get_singleton()->get_physics_frames();
  459. Vector2 origin = agent_parent->get_global_position();
  460. bool reload_path = false;
  461. if (NavigationServer2D::get_singleton()->agent_is_map_changed(agent)) {
  462. reload_path = true;
  463. } else if (navigation_result->get_path().size() == 0) {
  464. reload_path = true;
  465. } else {
  466. // Check if too far from the navigation path
  467. if (navigation_path_index > 0) {
  468. const Vector<Vector2> &navigation_path = navigation_result->get_path();
  469. Vector2 segment[2];
  470. segment[0] = navigation_path[navigation_path_index - 1];
  471. segment[1] = navigation_path[navigation_path_index];
  472. Vector2 p = Geometry2D::get_closest_point_to_segment(origin, segment);
  473. if (origin.distance_to(p) >= path_max_distance) {
  474. // To faraway, reload path
  475. reload_path = true;
  476. }
  477. }
  478. }
  479. if (reload_path) {
  480. navigation_query->set_start_position(origin);
  481. navigation_query->set_target_position(target_position);
  482. navigation_query->set_navigation_layers(navigation_layers);
  483. navigation_query->set_metadata_flags(path_metadata_flags);
  484. if (map_override.is_valid()) {
  485. navigation_query->set_map(map_override);
  486. } else {
  487. navigation_query->set_map(agent_parent->get_world_2d()->get_navigation_map());
  488. }
  489. NavigationServer2D::get_singleton()->query_path(navigation_query, navigation_result);
  490. #ifdef DEBUG_ENABLED
  491. debug_path_dirty = true;
  492. #endif // DEBUG_ENABLED
  493. navigation_finished = false;
  494. navigation_path_index = 0;
  495. emit_signal(SNAME("path_changed"));
  496. }
  497. if (navigation_result->get_path().size() == 0) {
  498. return;
  499. }
  500. // Check if we can advance the navigation path
  501. if (navigation_finished == false) {
  502. // Advances to the next far away position.
  503. const Vector<Vector2> &navigation_path = navigation_result->get_path();
  504. const Vector<int32_t> &navigation_path_types = navigation_result->get_path_types();
  505. const TypedArray<RID> &navigation_path_rids = navigation_result->get_path_rids();
  506. const Vector<int64_t> &navigation_path_owners = navigation_result->get_path_owner_ids();
  507. while (origin.distance_to(navigation_path[navigation_path_index]) < path_desired_distance) {
  508. Dictionary details;
  509. const Vector2 waypoint = navigation_path[navigation_path_index];
  510. details[SNAME("position")] = waypoint;
  511. int waypoint_type = -1;
  512. if (path_metadata_flags.has_flag(NavigationPathQueryParameters2D::PathMetadataFlags::PATH_METADATA_INCLUDE_TYPES)) {
  513. const NavigationPathQueryResult2D::PathSegmentType type = NavigationPathQueryResult2D::PathSegmentType(navigation_path_types[navigation_path_index]);
  514. details[SNAME("type")] = type;
  515. waypoint_type = type;
  516. }
  517. if (path_metadata_flags.has_flag(NavigationPathQueryParameters2D::PathMetadataFlags::PATH_METADATA_INCLUDE_RIDS)) {
  518. details[SNAME("rid")] = navigation_path_rids[navigation_path_index];
  519. }
  520. if (path_metadata_flags.has_flag(NavigationPathQueryParameters2D::PathMetadataFlags::PATH_METADATA_INCLUDE_OWNERS)) {
  521. const ObjectID waypoint_owner_id = ObjectID(navigation_path_owners[navigation_path_index]);
  522. // Get a reference to the owning object.
  523. Object *owner = nullptr;
  524. if (waypoint_owner_id.is_valid()) {
  525. owner = ObjectDB::get_instance(waypoint_owner_id);
  526. }
  527. details[SNAME("owner")] = owner;
  528. if (waypoint_type == NavigationPathQueryResult2D::PATH_SEGMENT_TYPE_LINK) {
  529. const NavigationLink2D *navlink = Object::cast_to<NavigationLink2D>(owner);
  530. if (navlink) {
  531. Vector2 link_global_start_position = navlink->get_global_start_position();
  532. Vector2 link_global_end_position = navlink->get_global_end_position();
  533. if (waypoint.distance_to(link_global_start_position) < waypoint.distance_to(link_global_end_position)) {
  534. details[SNAME("link_entry_position")] = link_global_start_position;
  535. details[SNAME("link_exit_position")] = link_global_end_position;
  536. } else {
  537. details[SNAME("link_entry_position")] = link_global_end_position;
  538. details[SNAME("link_exit_position")] = link_global_start_position;
  539. }
  540. }
  541. }
  542. }
  543. // Emit a signal for the waypoint
  544. emit_signal(SNAME("waypoint_reached"), details);
  545. // Emit a signal if we've reached a navigation link
  546. if (waypoint_type == NavigationPathQueryResult2D::PATH_SEGMENT_TYPE_LINK) {
  547. emit_signal(SNAME("link_reached"), details);
  548. }
  549. // Move to the next waypoint on the list
  550. navigation_path_index += 1;
  551. // Check to see if we've finished our route
  552. if (navigation_path_index == navigation_path.size()) {
  553. _check_distance_to_target();
  554. navigation_path_index -= 1;
  555. navigation_finished = true;
  556. target_position_submitted = false;
  557. emit_signal(SNAME("navigation_finished"));
  558. break;
  559. }
  560. }
  561. }
  562. }
  563. void NavigationAgent2D::_request_repath() {
  564. navigation_result->reset();
  565. target_reached = false;
  566. navigation_finished = false;
  567. update_frame_id = 0;
  568. }
  569. void NavigationAgent2D::_check_distance_to_target() {
  570. if (!target_reached) {
  571. if (distance_to_target() < target_desired_distance) {
  572. target_reached = true;
  573. emit_signal(SNAME("target_reached"));
  574. }
  575. }
  576. }
  577. ////////DEBUG////////////////////////////////////////////////////////////
  578. void NavigationAgent2D::set_debug_enabled(bool p_enabled) {
  579. #ifdef DEBUG_ENABLED
  580. if (debug_enabled == p_enabled) {
  581. return;
  582. }
  583. debug_enabled = p_enabled;
  584. debug_path_dirty = true;
  585. #endif // DEBUG_ENABLED
  586. }
  587. bool NavigationAgent2D::get_debug_enabled() const {
  588. return debug_enabled;
  589. }
  590. void NavigationAgent2D::set_debug_use_custom(bool p_enabled) {
  591. #ifdef DEBUG_ENABLED
  592. if (debug_use_custom == p_enabled) {
  593. return;
  594. }
  595. debug_use_custom = p_enabled;
  596. debug_path_dirty = true;
  597. #endif // DEBUG_ENABLED
  598. }
  599. bool NavigationAgent2D::get_debug_use_custom() const {
  600. return debug_use_custom;
  601. }
  602. void NavigationAgent2D::set_debug_path_custom_color(Color p_color) {
  603. #ifdef DEBUG_ENABLED
  604. if (debug_path_custom_color == p_color) {
  605. return;
  606. }
  607. debug_path_custom_color = p_color;
  608. debug_path_dirty = true;
  609. #endif // DEBUG_ENABLED
  610. }
  611. Color NavigationAgent2D::get_debug_path_custom_color() const {
  612. return debug_path_custom_color;
  613. }
  614. void NavigationAgent2D::set_debug_path_custom_point_size(float p_point_size) {
  615. #ifdef DEBUG_ENABLED
  616. if (Math::is_equal_approx(debug_path_custom_point_size, p_point_size)) {
  617. return;
  618. }
  619. debug_path_custom_point_size = MAX(0.0, p_point_size);
  620. debug_path_dirty = true;
  621. #endif // DEBUG_ENABLED
  622. }
  623. float NavigationAgent2D::get_debug_path_custom_point_size() const {
  624. return debug_path_custom_point_size;
  625. }
  626. void NavigationAgent2D::set_debug_path_custom_line_width(float p_line_width) {
  627. #ifdef DEBUG_ENABLED
  628. if (Math::is_equal_approx(debug_path_custom_line_width, p_line_width)) {
  629. return;
  630. }
  631. debug_path_custom_line_width = p_line_width;
  632. debug_path_dirty = true;
  633. #endif // DEBUG_ENABLED
  634. }
  635. float NavigationAgent2D::get_debug_path_custom_line_width() const {
  636. return debug_path_custom_line_width;
  637. }
  638. #ifdef DEBUG_ENABLED
  639. void NavigationAgent2D::_navigation_debug_changed() {
  640. debug_path_dirty = true;
  641. }
  642. void NavigationAgent2D::_update_debug_path() {
  643. if (!debug_path_dirty) {
  644. return;
  645. }
  646. debug_path_dirty = false;
  647. if (!debug_path_instance.is_valid()) {
  648. debug_path_instance = RenderingServer::get_singleton()->canvas_item_create();
  649. }
  650. RenderingServer::get_singleton()->canvas_item_clear(debug_path_instance);
  651. if (!(debug_enabled && NavigationServer2D::get_singleton()->get_debug_navigation_enable_agent_paths())) {
  652. return;
  653. }
  654. if (!(agent_parent && agent_parent->is_inside_tree())) {
  655. return;
  656. }
  657. RenderingServer::get_singleton()->canvas_item_set_parent(debug_path_instance, agent_parent->get_canvas());
  658. RenderingServer::get_singleton()->canvas_item_set_visible(debug_path_instance, agent_parent->is_visible_in_tree());
  659. const Vector<Vector2> &navigation_path = navigation_result->get_path();
  660. if (navigation_path.size() <= 1) {
  661. return;
  662. }
  663. Color debug_path_color = NavigationServer2D::get_singleton()->get_debug_navigation_agent_path_color();
  664. if (debug_use_custom) {
  665. debug_path_color = debug_path_custom_color;
  666. }
  667. Vector<Color> debug_path_colors;
  668. debug_path_colors.resize(navigation_path.size());
  669. debug_path_colors.fill(debug_path_color);
  670. RenderingServer::get_singleton()->canvas_item_add_polyline(debug_path_instance, navigation_path, debug_path_colors, debug_path_custom_line_width, false);
  671. if (debug_path_custom_point_size <= 0.0) {
  672. return;
  673. }
  674. float point_size = NavigationServer2D::get_singleton()->get_debug_navigation_agent_path_point_size();
  675. float half_point_size = point_size * 0.5;
  676. if (debug_use_custom) {
  677. point_size = debug_path_custom_point_size;
  678. half_point_size = debug_path_custom_point_size * 0.5;
  679. }
  680. for (int i = 0; i < navigation_path.size(); i++) {
  681. const Vector2 &vert = navigation_path[i];
  682. Rect2 path_point_rect = Rect2(vert.x - half_point_size, vert.y - half_point_size, point_size, point_size);
  683. RenderingServer::get_singleton()->canvas_item_add_rect(debug_path_instance, path_point_rect, debug_path_color);
  684. }
  685. }
  686. #endif // DEBUG_ENABLED