navigation_agent_2d.cpp 33 KB

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