navigation_agent_2d.cpp 34 KB

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