test_navigation_server_2d.h 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767
  1. /**************************************************************************/
  2. /* test_navigation_server_2d.h */
  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. #pragma once
  31. #include "modules/navigation_2d/nav_utils_2d.h"
  32. #include "servers/navigation_server_2d.h"
  33. #include "scene/2d/polygon_2d.h"
  34. #include "tests/test_macros.h"
  35. namespace TestNavigationServer2D {
  36. // TODO: Find a more generic way to create `Callable` mocks.
  37. class CallableMock : public Object {
  38. GDCLASS(CallableMock, Object);
  39. public:
  40. void function1(Variant arg0) {
  41. function1_calls++;
  42. function1_latest_arg0 = arg0;
  43. }
  44. unsigned function1_calls{ 0 };
  45. Variant function1_latest_arg0;
  46. };
  47. static inline Array build_array() {
  48. return Array();
  49. }
  50. template <typename... Targs>
  51. static inline Array build_array(Variant item, Targs... Fargs) {
  52. Array a = build_array(Fargs...);
  53. a.push_front(item);
  54. return a;
  55. }
  56. struct GreaterThan {
  57. bool operator()(int p_a, int p_b) const { return p_a > p_b; }
  58. };
  59. struct CompareArrayValues {
  60. const int *array;
  61. CompareArrayValues(const int *p_array) :
  62. array(p_array) {}
  63. bool operator()(uint32_t p_index_a, uint32_t p_index_b) const {
  64. return array[p_index_a] < array[p_index_b];
  65. }
  66. };
  67. struct RegisterHeapIndexes {
  68. uint32_t *indexes;
  69. RegisterHeapIndexes(uint32_t *p_indexes) :
  70. indexes(p_indexes) {}
  71. void operator()(uint32_t p_vector_index, uint32_t p_heap_index) {
  72. indexes[p_vector_index] = p_heap_index;
  73. }
  74. };
  75. TEST_SUITE("[Navigation2D]") {
  76. TEST_CASE("[NavigationServer2D] Server should be empty when initialized") {
  77. NavigationServer2D *navigation_server = NavigationServer2D::get_singleton();
  78. CHECK_EQ(navigation_server->get_maps().size(), 0);
  79. SUBCASE("'ProcessInfo' should report all counters empty as well") {
  80. CHECK_EQ(navigation_server->get_process_info(NavigationServer2D::INFO_ACTIVE_MAPS), 0);
  81. CHECK_EQ(navigation_server->get_process_info(NavigationServer2D::INFO_REGION_COUNT), 0);
  82. CHECK_EQ(navigation_server->get_process_info(NavigationServer2D::INFO_AGENT_COUNT), 0);
  83. CHECK_EQ(navigation_server->get_process_info(NavigationServer2D::INFO_LINK_COUNT), 0);
  84. CHECK_EQ(navigation_server->get_process_info(NavigationServer2D::INFO_POLYGON_COUNT), 0);
  85. CHECK_EQ(navigation_server->get_process_info(NavigationServer2D::INFO_EDGE_COUNT), 0);
  86. CHECK_EQ(navigation_server->get_process_info(NavigationServer2D::INFO_EDGE_MERGE_COUNT), 0);
  87. CHECK_EQ(navigation_server->get_process_info(NavigationServer2D::INFO_EDGE_CONNECTION_COUNT), 0);
  88. CHECK_EQ(navigation_server->get_process_info(NavigationServer2D::INFO_EDGE_FREE_COUNT), 0);
  89. }
  90. }
  91. TEST_CASE("[NavigationServer2D] Server should manage agent properly") {
  92. NavigationServer2D *navigation_server = NavigationServer2D::get_singleton();
  93. RID agent = navigation_server->agent_create();
  94. CHECK(agent.is_valid());
  95. SUBCASE("'ProcessInfo' should not report dangling agent") {
  96. CHECK_EQ(navigation_server->get_process_info(NavigationServer2D::INFO_AGENT_COUNT), 0);
  97. }
  98. SUBCASE("Setters/getters should work") {
  99. bool initial_avoidance_enabled = navigation_server->agent_get_avoidance_enabled(agent);
  100. navigation_server->agent_set_avoidance_enabled(agent, !initial_avoidance_enabled);
  101. navigation_server->process(0.0); // Give server some cycles to commit.
  102. CHECK_EQ(navigation_server->agent_get_avoidance_enabled(agent), !initial_avoidance_enabled);
  103. // TODO: Add remaining setters/getters once the missing getters are added.
  104. }
  105. SUBCASE("'ProcessInfo' should report agent with active map") {
  106. RID map = navigation_server->map_create();
  107. CHECK(map.is_valid());
  108. navigation_server->map_set_active(map, true);
  109. navigation_server->agent_set_map(agent, map);
  110. navigation_server->process(0.0); // Give server some cycles to commit.
  111. CHECK_EQ(navigation_server->get_process_info(NavigationServer2D::INFO_AGENT_COUNT), 1);
  112. navigation_server->agent_set_map(agent, RID());
  113. navigation_server->free(map);
  114. navigation_server->process(0.0); // Give server some cycles to commit.
  115. CHECK_EQ(navigation_server->get_process_info(NavigationServer2D::INFO_AGENT_COUNT), 0);
  116. }
  117. navigation_server->free(agent);
  118. }
  119. TEST_CASE("[NavigationServer2D] Server should manage map properly") {
  120. NavigationServer2D *navigation_server = NavigationServer2D::get_singleton();
  121. RID map;
  122. CHECK_FALSE(map.is_valid());
  123. SUBCASE("Queries against invalid map should return empty or invalid values") {
  124. ERR_PRINT_OFF;
  125. CHECK_EQ(navigation_server->map_get_closest_point(map, Vector2(7, 7)), Vector2());
  126. CHECK_FALSE(navigation_server->map_get_closest_point_owner(map, Vector2(7, 7)).is_valid());
  127. CHECK_EQ(navigation_server->map_get_path(map, Vector2(7, 7), Vector2(8, 8), true).size(), 0);
  128. CHECK_EQ(navigation_server->map_get_path(map, Vector2(7, 7), Vector2(8, 8), false).size(), 0);
  129. Ref<NavigationPathQueryParameters2D> query_parameters;
  130. query_parameters.instantiate();
  131. query_parameters->set_map(map);
  132. query_parameters->set_start_position(Vector2(7, 7));
  133. query_parameters->set_target_position(Vector2(8, 8));
  134. Ref<NavigationPathQueryResult2D> query_result;
  135. query_result.instantiate();
  136. navigation_server->query_path(query_parameters, query_result);
  137. CHECK_EQ(query_result->get_path().size(), 0);
  138. CHECK_EQ(query_result->get_path_types().size(), 0);
  139. CHECK_EQ(query_result->get_path_rids().size(), 0);
  140. CHECK_EQ(query_result->get_path_owner_ids().size(), 0);
  141. ERR_PRINT_ON;
  142. }
  143. map = navigation_server->map_create();
  144. CHECK(map.is_valid());
  145. CHECK_EQ(navigation_server->get_maps().size(), 1);
  146. SUBCASE("'ProcessInfo' should not report inactive map") {
  147. CHECK_EQ(navigation_server->get_process_info(NavigationServer2D::INFO_ACTIVE_MAPS), 0);
  148. }
  149. SUBCASE("Setters/getters should work") {
  150. navigation_server->map_set_cell_size(map, 0.55);
  151. navigation_server->map_set_edge_connection_margin(map, 0.66);
  152. navigation_server->map_set_link_connection_radius(map, 0.77);
  153. bool initial_use_edge_connections = navigation_server->map_get_use_edge_connections(map);
  154. navigation_server->map_set_use_edge_connections(map, !initial_use_edge_connections);
  155. navigation_server->process(0.0); // Give server some cycles to commit.
  156. CHECK_EQ(navigation_server->map_get_cell_size(map), doctest::Approx(0.55));
  157. CHECK_EQ(navigation_server->map_get_edge_connection_margin(map), doctest::Approx(0.66));
  158. CHECK_EQ(navigation_server->map_get_link_connection_radius(map), doctest::Approx(0.77));
  159. CHECK_EQ(navigation_server->map_get_use_edge_connections(map), !initial_use_edge_connections);
  160. }
  161. SUBCASE("'ProcessInfo' should report map iff active") {
  162. navigation_server->map_set_active(map, true);
  163. navigation_server->process(0.0); // Give server some cycles to commit.
  164. CHECK(navigation_server->map_is_active(map));
  165. CHECK_EQ(navigation_server->get_process_info(NavigationServer2D::INFO_ACTIVE_MAPS), 1);
  166. navigation_server->map_set_active(map, false);
  167. navigation_server->process(0.0); // Give server some cycles to commit.
  168. CHECK_EQ(navigation_server->get_process_info(NavigationServer2D::INFO_ACTIVE_MAPS), 0);
  169. }
  170. SUBCASE("Number of agents should be reported properly") {
  171. RID agent = navigation_server->agent_create();
  172. CHECK(agent.is_valid());
  173. navigation_server->agent_set_map(agent, map);
  174. navigation_server->process(0.0); // Give server some cycles to commit.
  175. CHECK_EQ(navigation_server->map_get_agents(map).size(), 1);
  176. navigation_server->free(agent);
  177. navigation_server->process(0.0); // Give server some cycles to commit.
  178. CHECK_EQ(navigation_server->map_get_agents(map).size(), 0);
  179. }
  180. SUBCASE("Number of links should be reported properly") {
  181. RID link = navigation_server->link_create();
  182. CHECK(link.is_valid());
  183. navigation_server->link_set_map(link, map);
  184. navigation_server->process(0.0); // Give server some cycles to commit.
  185. CHECK_EQ(navigation_server->map_get_links(map).size(), 1);
  186. navigation_server->free(link);
  187. navigation_server->process(0.0); // Give server some cycles to commit.
  188. CHECK_EQ(navigation_server->map_get_links(map).size(), 0);
  189. }
  190. SUBCASE("Number of obstacles should be reported properly") {
  191. RID obstacle = navigation_server->obstacle_create();
  192. CHECK(obstacle.is_valid());
  193. navigation_server->obstacle_set_map(obstacle, map);
  194. navigation_server->process(0.0); // Give server some cycles to commit.
  195. CHECK_EQ(navigation_server->map_get_obstacles(map).size(), 1);
  196. navigation_server->free(obstacle);
  197. navigation_server->process(0.0); // Give server some cycles to commit.
  198. CHECK_EQ(navigation_server->map_get_obstacles(map).size(), 0);
  199. }
  200. SUBCASE("Number of regions should be reported properly") {
  201. RID region = navigation_server->region_create();
  202. CHECK(region.is_valid());
  203. navigation_server->region_set_map(region, map);
  204. navigation_server->process(0.0); // Give server some cycles to commit.
  205. CHECK_EQ(navigation_server->map_get_regions(map).size(), 1);
  206. navigation_server->free(region);
  207. navigation_server->process(0.0); // Give server some cycles to commit.
  208. CHECK_EQ(navigation_server->map_get_regions(map).size(), 0);
  209. }
  210. SUBCASE("Queries against empty map should return empty or invalid values") {
  211. navigation_server->map_set_active(map, true);
  212. navigation_server->process(0.0); // Give server some cycles to commit.
  213. ERR_PRINT_OFF;
  214. CHECK_EQ(navigation_server->map_get_closest_point(map, Vector2(7, 7)), Vector2());
  215. CHECK_FALSE(navigation_server->map_get_closest_point_owner(map, Vector2(7, 7)).is_valid());
  216. CHECK_EQ(navigation_server->map_get_path(map, Vector2(7, 7), Vector2(8, 8), true).size(), 0);
  217. CHECK_EQ(navigation_server->map_get_path(map, Vector2(7, 7), Vector2(8, 8), false).size(), 0);
  218. Ref<NavigationPathQueryParameters2D> query_parameters;
  219. query_parameters.instantiate();
  220. query_parameters->set_map(map);
  221. query_parameters->set_start_position(Vector2(7, 7));
  222. query_parameters->set_target_position(Vector2(8, 8));
  223. Ref<NavigationPathQueryResult2D> query_result;
  224. query_result.instantiate();
  225. navigation_server->query_path(query_parameters, query_result);
  226. CHECK_EQ(query_result->get_path().size(), 0);
  227. CHECK_EQ(query_result->get_path_types().size(), 0);
  228. CHECK_EQ(query_result->get_path_rids().size(), 0);
  229. CHECK_EQ(query_result->get_path_owner_ids().size(), 0);
  230. ERR_PRINT_ON;
  231. navigation_server->map_set_active(map, false);
  232. navigation_server->process(0.0); // Give server some cycles to commit.
  233. }
  234. navigation_server->free(map);
  235. navigation_server->process(0.0); // Give server some cycles to actually remove map.
  236. CHECK_EQ(navigation_server->get_maps().size(), 0);
  237. }
  238. TEST_CASE("[NavigationServer2D] Server should manage link properly") {
  239. NavigationServer2D *navigation_server = NavigationServer2D::get_singleton();
  240. RID link = navigation_server->link_create();
  241. CHECK(link.is_valid());
  242. SUBCASE("'ProcessInfo' should not report dangling link") {
  243. CHECK_EQ(navigation_server->get_process_info(NavigationServer2D::INFO_LINK_COUNT), 0);
  244. }
  245. SUBCASE("Setters/getters should work") {
  246. bool initial_bidirectional = navigation_server->link_is_bidirectional(link);
  247. navigation_server->link_set_bidirectional(link, !initial_bidirectional);
  248. navigation_server->link_set_end_position(link, Vector2(7, 7));
  249. navigation_server->link_set_enter_cost(link, 0.55);
  250. navigation_server->link_set_navigation_layers(link, 6);
  251. navigation_server->link_set_owner_id(link, ObjectID((int64_t)7));
  252. navigation_server->link_set_start_position(link, Vector2(8, 8));
  253. navigation_server->link_set_travel_cost(link, 0.66);
  254. navigation_server->process(0.0); // Give server some cycles to commit.
  255. CHECK_EQ(navigation_server->link_is_bidirectional(link), !initial_bidirectional);
  256. CHECK_EQ(navigation_server->link_get_end_position(link), Vector2(7, 7));
  257. CHECK_EQ(navigation_server->link_get_enter_cost(link), doctest::Approx(0.55));
  258. CHECK_EQ(navigation_server->link_get_navigation_layers(link), 6);
  259. CHECK_EQ(navigation_server->link_get_owner_id(link), ObjectID((int64_t)7));
  260. CHECK_EQ(navigation_server->link_get_start_position(link), Vector2(8, 8));
  261. CHECK_EQ(navigation_server->link_get_travel_cost(link), doctest::Approx(0.66));
  262. }
  263. SUBCASE("'ProcessInfo' should report link with active map") {
  264. RID map = navigation_server->map_create();
  265. CHECK(map.is_valid());
  266. navigation_server->map_set_active(map, true);
  267. navigation_server->link_set_map(link, map);
  268. navigation_server->process(0.0); // Give server some cycles to commit.
  269. CHECK_EQ(navigation_server->get_process_info(NavigationServer2D::INFO_LINK_COUNT), 1);
  270. navigation_server->link_set_map(link, RID());
  271. navigation_server->free(map);
  272. navigation_server->process(0.0); // Give server some cycles to commit.
  273. CHECK_EQ(navigation_server->get_process_info(NavigationServer2D::INFO_LINK_COUNT), 0);
  274. }
  275. navigation_server->free(link);
  276. }
  277. TEST_CASE("[NavigationServer2D] Server should manage obstacles properly") {
  278. NavigationServer2D *navigation_server = NavigationServer2D::get_singleton();
  279. RID obstacle = navigation_server->obstacle_create();
  280. CHECK(obstacle.is_valid());
  281. // TODO: Add tests for setters/getters once getters are added.
  282. navigation_server->free(obstacle);
  283. }
  284. TEST_CASE("[NavigationServer2D] Server should manage regions properly") {
  285. NavigationServer2D *navigation_server = NavigationServer2D::get_singleton();
  286. RID region = navigation_server->region_create();
  287. CHECK(region.is_valid());
  288. SUBCASE("'ProcessInfo' should not report dangling region") {
  289. CHECK_EQ(navigation_server->get_process_info(NavigationServer2D::INFO_REGION_COUNT), 0);
  290. }
  291. SUBCASE("Setters/getters should work") {
  292. bool initial_use_edge_connections = navigation_server->region_get_use_edge_connections(region);
  293. navigation_server->region_set_enter_cost(region, 0.55);
  294. navigation_server->region_set_navigation_layers(region, 5);
  295. navigation_server->region_set_owner_id(region, ObjectID((int64_t)7));
  296. navigation_server->region_set_travel_cost(region, 0.66);
  297. navigation_server->region_set_use_edge_connections(region, !initial_use_edge_connections);
  298. navigation_server->process(0.0); // Give server some cycles to commit.
  299. CHECK_EQ(navigation_server->region_get_enter_cost(region), doctest::Approx(0.55));
  300. CHECK_EQ(navigation_server->region_get_navigation_layers(region), 5);
  301. CHECK_EQ(navigation_server->region_get_owner_id(region), ObjectID((int64_t)7));
  302. CHECK_EQ(navigation_server->region_get_travel_cost(region), doctest::Approx(0.66));
  303. CHECK_EQ(navigation_server->region_get_use_edge_connections(region), !initial_use_edge_connections);
  304. }
  305. SUBCASE("'ProcessInfo' should report region with active map") {
  306. RID map = navigation_server->map_create();
  307. CHECK(map.is_valid());
  308. navigation_server->map_set_active(map, true);
  309. navigation_server->region_set_map(region, map);
  310. navigation_server->process(0.0); // Give server some cycles to commit.
  311. CHECK_EQ(navigation_server->get_process_info(NavigationServer2D::INFO_REGION_COUNT), 1);
  312. navigation_server->region_set_map(region, RID());
  313. navigation_server->free(map);
  314. navigation_server->process(0.0); // Give server some cycles to commit.
  315. CHECK_EQ(navigation_server->get_process_info(NavigationServer2D::INFO_REGION_COUNT), 0);
  316. }
  317. SUBCASE("Queries against empty region should return empty or invalid values") {
  318. ERR_PRINT_OFF;
  319. CHECK_EQ(navigation_server->region_get_connections_count(region), 0);
  320. CHECK_EQ(navigation_server->region_get_connection_pathway_end(region, 55), Vector2());
  321. CHECK_EQ(navigation_server->region_get_connection_pathway_start(region, 55), Vector2());
  322. ERR_PRINT_ON;
  323. }
  324. navigation_server->free(region);
  325. }
  326. // This test case does not check precise values on purpose - to not be too sensitivte.
  327. TEST_CASE("[NavigationServer2D] Server should move agent properly") {
  328. NavigationServer2D *navigation_server = NavigationServer2D::get_singleton();
  329. RID map = navigation_server->map_create();
  330. RID agent = navigation_server->agent_create();
  331. navigation_server->map_set_active(map, true);
  332. navigation_server->agent_set_map(agent, map);
  333. navigation_server->agent_set_avoidance_enabled(agent, true);
  334. navigation_server->agent_set_velocity(agent, Vector2(1, 1));
  335. CallableMock agent_avoidance_callback_mock;
  336. navigation_server->agent_set_avoidance_callback(agent, callable_mp(&agent_avoidance_callback_mock, &CallableMock::function1));
  337. CHECK_EQ(agent_avoidance_callback_mock.function1_calls, 0);
  338. navigation_server->process(0.0); // Give server some cycles to commit.
  339. CHECK_EQ(agent_avoidance_callback_mock.function1_calls, 1);
  340. CHECK_NE(agent_avoidance_callback_mock.function1_latest_arg0, Vector3(0, 0, 0));
  341. navigation_server->free(agent);
  342. navigation_server->free(map);
  343. }
  344. // This test case does not check precise values on purpose - to not be too sensitivte.
  345. TEST_CASE("[NavigationServer2D] Server should make agents avoid each other when avoidance enabled") {
  346. NavigationServer2D *navigation_server = NavigationServer2D::get_singleton();
  347. RID map = navigation_server->map_create();
  348. RID agent_1 = navigation_server->agent_create();
  349. RID agent_2 = navigation_server->agent_create();
  350. navigation_server->map_set_active(map, true);
  351. navigation_server->agent_set_map(agent_1, map);
  352. navigation_server->agent_set_avoidance_enabled(agent_1, true);
  353. navigation_server->agent_set_position(agent_1, Vector2(0, 0));
  354. navigation_server->agent_set_radius(agent_1, 1);
  355. navigation_server->agent_set_velocity(agent_1, Vector2(1, 0));
  356. CallableMock agent_1_avoidance_callback_mock;
  357. navigation_server->agent_set_avoidance_callback(agent_1, callable_mp(&agent_1_avoidance_callback_mock, &CallableMock::function1));
  358. navigation_server->agent_set_map(agent_2, map);
  359. navigation_server->agent_set_avoidance_enabled(agent_2, true);
  360. navigation_server->agent_set_position(agent_2, Vector2(2.5, 0.5));
  361. navigation_server->agent_set_radius(agent_2, 1);
  362. navigation_server->agent_set_velocity(agent_2, Vector2(-1, 0));
  363. CallableMock agent_2_avoidance_callback_mock;
  364. navigation_server->agent_set_avoidance_callback(agent_2, callable_mp(&agent_2_avoidance_callback_mock, &CallableMock::function1));
  365. CHECK_EQ(agent_1_avoidance_callback_mock.function1_calls, 0);
  366. CHECK_EQ(agent_2_avoidance_callback_mock.function1_calls, 0);
  367. navigation_server->process(0.0); // Give server some cycles to commit.
  368. CHECK_EQ(agent_1_avoidance_callback_mock.function1_calls, 1);
  369. CHECK_EQ(agent_2_avoidance_callback_mock.function1_calls, 1);
  370. Vector3 agent_1_safe_velocity = agent_1_avoidance_callback_mock.function1_latest_arg0;
  371. Vector3 agent_2_safe_velocity = agent_2_avoidance_callback_mock.function1_latest_arg0;
  372. CHECK_MESSAGE(agent_1_safe_velocity.x > 0, "agent 1 should move a bit along desired velocity (+X)");
  373. CHECK_MESSAGE(agent_2_safe_velocity.x < 0, "agent 2 should move a bit along desired velocity (-X)");
  374. CHECK_MESSAGE(agent_1_safe_velocity.z < 0, "agent 1 should move a bit to the side so that it avoids agent 2");
  375. CHECK_MESSAGE(agent_2_safe_velocity.z > 0, "agent 2 should move a bit to the side so that it avoids agent 1");
  376. navigation_server->free(agent_2);
  377. navigation_server->free(agent_1);
  378. navigation_server->free(map);
  379. }
  380. TEST_CASE("[NavigationServer2D] Server should make agents avoid dynamic obstacles when avoidance enabled") {
  381. NavigationServer2D *navigation_server = NavigationServer2D::get_singleton();
  382. RID map = navigation_server->map_create();
  383. RID agent_1 = navigation_server->agent_create();
  384. RID obstacle_1 = navigation_server->obstacle_create();
  385. navigation_server->map_set_active(map, true);
  386. navigation_server->agent_set_map(agent_1, map);
  387. navigation_server->agent_set_avoidance_enabled(agent_1, true);
  388. navigation_server->agent_set_position(agent_1, Vector2(0, 0));
  389. navigation_server->agent_set_radius(agent_1, 1);
  390. navigation_server->agent_set_velocity(agent_1, Vector2(1, 0));
  391. CallableMock agent_1_avoidance_callback_mock;
  392. navigation_server->agent_set_avoidance_callback(agent_1, callable_mp(&agent_1_avoidance_callback_mock, &CallableMock::function1));
  393. navigation_server->obstacle_set_map(obstacle_1, map);
  394. navigation_server->obstacle_set_avoidance_enabled(obstacle_1, true);
  395. navigation_server->obstacle_set_position(obstacle_1, Vector2(2.5, 0.5));
  396. navigation_server->obstacle_set_radius(obstacle_1, 1);
  397. CHECK_EQ(agent_1_avoidance_callback_mock.function1_calls, 0);
  398. navigation_server->process(0.0); // Give server some cycles to commit.
  399. CHECK_EQ(agent_1_avoidance_callback_mock.function1_calls, 1);
  400. Vector3 agent_1_safe_velocity = agent_1_avoidance_callback_mock.function1_latest_arg0;
  401. CHECK_MESSAGE(agent_1_safe_velocity.x > 0, "Agent 1 should move a bit along desired velocity (+X).");
  402. CHECK_MESSAGE(agent_1_safe_velocity.z < 0, "Agent 1 should move a bit to the side so that it avoids obstacle.");
  403. navigation_server->free(obstacle_1);
  404. navigation_server->free(agent_1);
  405. navigation_server->free(map);
  406. navigation_server->process(0.0); // Give server some cycles to commit.
  407. }
  408. TEST_CASE("[NavigationServer2D] Server should make agents avoid static obstacles when avoidance enabled") {
  409. NavigationServer2D *navigation_server = NavigationServer2D::get_singleton();
  410. RID map = navigation_server->map_create();
  411. RID agent_1 = navigation_server->agent_create();
  412. RID agent_2 = navigation_server->agent_create();
  413. RID obstacle_1 = navigation_server->obstacle_create();
  414. navigation_server->map_set_active(map, true);
  415. navigation_server->agent_set_map(agent_1, map);
  416. navigation_server->agent_set_avoidance_enabled(agent_1, true);
  417. navigation_server->agent_set_radius(agent_1, 1.6); // Have hit the obstacle already.
  418. navigation_server->agent_set_velocity(agent_1, Vector2(1, 0));
  419. CallableMock agent_1_avoidance_callback_mock;
  420. navigation_server->agent_set_avoidance_callback(agent_1, callable_mp(&agent_1_avoidance_callback_mock, &CallableMock::function1));
  421. navigation_server->agent_set_map(agent_2, map);
  422. navigation_server->agent_set_avoidance_enabled(agent_2, true);
  423. navigation_server->agent_set_radius(agent_2, 1.4); // Haven't hit the obstacle yet.
  424. navigation_server->agent_set_velocity(agent_2, Vector2(1, 0));
  425. CallableMock agent_2_avoidance_callback_mock;
  426. navigation_server->agent_set_avoidance_callback(agent_2, callable_mp(&agent_2_avoidance_callback_mock, &CallableMock::function1));
  427. navigation_server->obstacle_set_map(obstacle_1, map);
  428. navigation_server->obstacle_set_avoidance_enabled(obstacle_1, true);
  429. PackedVector2Array obstacle_1_vertices;
  430. SUBCASE("Static obstacles should work on ground level") {
  431. navigation_server->agent_set_position(agent_1, Vector2(0, 0));
  432. navigation_server->agent_set_position(agent_2, Vector2(0, 5));
  433. obstacle_1_vertices.push_back(Vector2(1.5, 0.5));
  434. obstacle_1_vertices.push_back(Vector2(1.5, 4.5));
  435. }
  436. navigation_server->obstacle_set_vertices(obstacle_1, obstacle_1_vertices);
  437. CHECK_EQ(agent_1_avoidance_callback_mock.function1_calls, 0);
  438. CHECK_EQ(agent_2_avoidance_callback_mock.function1_calls, 0);
  439. navigation_server->process(0.0); // Give server some cycles to commit.
  440. CHECK_EQ(agent_1_avoidance_callback_mock.function1_calls, 1);
  441. CHECK_EQ(agent_2_avoidance_callback_mock.function1_calls, 1);
  442. Vector3 agent_1_safe_velocity = agent_1_avoidance_callback_mock.function1_latest_arg0;
  443. Vector3 agent_2_safe_velocity = agent_2_avoidance_callback_mock.function1_latest_arg0;
  444. CHECK_MESSAGE(agent_1_safe_velocity.x > 0, "Agent 1 should move a bit along desired velocity (+X).");
  445. CHECK_MESSAGE(agent_1_safe_velocity.z < 0, "Agent 1 should move a bit to the side so that it avoids obstacle.");
  446. CHECK_MESSAGE(agent_2_safe_velocity.x > 0, "Agent 2 should move a bit along desired velocity (+X).");
  447. CHECK_MESSAGE(agent_2_safe_velocity.z == 0, "Agent 2 should not move to the side.");
  448. navigation_server->free(obstacle_1);
  449. navigation_server->free(agent_2);
  450. navigation_server->free(agent_1);
  451. navigation_server->free(map);
  452. navigation_server->process(0.0); // Give server some cycles to commit.
  453. }
  454. TEST_CASE("[NavigationServer2D][SceneTree] Server should be able to parse geometry") {
  455. NavigationServer2D *navigation_server = NavigationServer2D::get_singleton();
  456. // Prepare scene tree with simple mesh to serve as an input geometry.
  457. Node2D *node_2d = memnew(Node2D);
  458. SceneTree::get_singleton()->get_root()->add_child(node_2d);
  459. Polygon2D *polygon = memnew(Polygon2D);
  460. polygon->set_polygon(PackedVector2Array({ Vector2(200.0, 200.0), Vector2(400.0, 200.0), Vector2(400.0, 400.0), Vector2(200.0, 400.0) }));
  461. node_2d->add_child(polygon);
  462. // TODO: Use MeshInstance2D as well?
  463. Ref<NavigationPolygon> navigation_polygon;
  464. navigation_polygon.instantiate();
  465. Ref<NavigationMeshSourceGeometryData2D> source_geometry;
  466. source_geometry.instantiate();
  467. CHECK_EQ(source_geometry->get_traversable_outlines().size(), 0);
  468. CHECK_EQ(source_geometry->get_obstruction_outlines().size(), 0);
  469. navigation_server->parse_source_geometry_data(navigation_polygon, source_geometry, polygon);
  470. CHECK_EQ(source_geometry->get_traversable_outlines().size(), 0);
  471. REQUIRE_EQ(source_geometry->get_obstruction_outlines().size(), 1);
  472. CHECK_EQ(((PackedVector2Array)source_geometry->get_obstruction_outlines()[0]).size(), 4);
  473. SUBCASE("By default, parsing should remove any data that was parsed before") {
  474. navigation_server->parse_source_geometry_data(navigation_polygon, source_geometry, polygon);
  475. CHECK_EQ(source_geometry->get_traversable_outlines().size(), 0);
  476. REQUIRE_EQ(source_geometry->get_obstruction_outlines().size(), 1);
  477. CHECK_EQ(((PackedVector2Array)source_geometry->get_obstruction_outlines()[0]).size(), 4);
  478. }
  479. SUBCASE("Parsed geometry should be extendible with other geometry") {
  480. source_geometry->merge(source_geometry); // Merging with itself.
  481. CHECK_EQ(source_geometry->get_traversable_outlines().size(), 0);
  482. REQUIRE_EQ(source_geometry->get_obstruction_outlines().size(), 2);
  483. const PackedVector2Array obstruction_outline_1 = source_geometry->get_obstruction_outlines()[0];
  484. const PackedVector2Array obstruction_outline_2 = source_geometry->get_obstruction_outlines()[1];
  485. REQUIRE_EQ(obstruction_outline_1.size(), 4);
  486. REQUIRE_EQ(obstruction_outline_2.size(), 4);
  487. CHECK_EQ(obstruction_outline_1[0], obstruction_outline_2[0]);
  488. CHECK_EQ(obstruction_outline_1[1], obstruction_outline_2[1]);
  489. CHECK_EQ(obstruction_outline_1[2], obstruction_outline_2[2]);
  490. CHECK_EQ(obstruction_outline_1[3], obstruction_outline_2[3]);
  491. }
  492. memdelete(polygon);
  493. memdelete(node_2d);
  494. }
  495. // This test case uses only public APIs on purpose - other test cases use simplified baking.
  496. TEST_CASE("[NavigationServer2D][SceneTree] Server should be able to bake map correctly") {
  497. NavigationServer2D *navigation_server = NavigationServer2D::get_singleton();
  498. // Prepare scene tree with simple mesh to serve as an input geometry.
  499. Node2D *node_2d = memnew(Node2D);
  500. SceneTree::get_singleton()->get_root()->add_child(node_2d);
  501. Polygon2D *polygon = memnew(Polygon2D);
  502. polygon->set_polygon(PackedVector2Array({ Vector2(-200.0, -200.0), Vector2(200.0, -200.0), Vector2(200.0, 200.0), Vector2(-200.0, 200.0) }));
  503. node_2d->add_child(polygon);
  504. // TODO: Use MeshInstance2D as well?
  505. // Prepare anything necessary to bake navigation polygon.
  506. RID map = navigation_server->map_create();
  507. RID region = navigation_server->region_create();
  508. Ref<NavigationPolygon> navigation_polygon;
  509. navigation_polygon.instantiate();
  510. navigation_polygon->add_outline(PackedVector2Array({ Vector2(-1000.0, -1000.0), Vector2(1000.0, -1000.0), Vector2(1000.0, 1000.0), Vector2(-1000.0, 1000.0) }));
  511. navigation_server->map_set_active(map, true);
  512. navigation_server->map_set_use_async_iterations(map, false);
  513. navigation_server->region_set_map(region, map);
  514. navigation_server->region_set_navigation_polygon(region, navigation_polygon);
  515. navigation_server->process(0.0); // Give server some cycles to commit.
  516. CHECK_EQ(navigation_polygon->get_polygon_count(), 0);
  517. CHECK_EQ(navigation_polygon->get_vertices().size(), 0);
  518. CHECK_EQ(navigation_polygon->get_outline_count(), 1);
  519. Ref<NavigationMeshSourceGeometryData2D> source_geometry;
  520. source_geometry.instantiate();
  521. navigation_server->parse_source_geometry_data(navigation_polygon, source_geometry, node_2d);
  522. navigation_server->bake_from_source_geometry_data(navigation_polygon, source_geometry, Callable());
  523. // FIXME: The above line should trigger the update (line below) under the hood.
  524. navigation_server->region_set_navigation_polygon(region, navigation_polygon); // Force update.
  525. CHECK_EQ(navigation_polygon->get_polygon_count(), 4);
  526. CHECK_EQ(navigation_polygon->get_vertices().size(), 8);
  527. CHECK_EQ(navigation_polygon->get_outline_count(), 1);
  528. SUBCASE("Map should emit signal and take newly baked navigation mesh into account") {
  529. SIGNAL_WATCH(navigation_server, "map_changed");
  530. SIGNAL_CHECK_FALSE("map_changed");
  531. navigation_server->process(0.0); // Give server some cycles to commit.
  532. SIGNAL_CHECK("map_changed", build_array(build_array(map)));
  533. SIGNAL_UNWATCH(navigation_server, "map_changed");
  534. CHECK_NE(navigation_server->map_get_closest_point(map, Vector2(0, 0)), Vector2(0, 0));
  535. }
  536. navigation_server->free(region);
  537. navigation_server->free(map);
  538. navigation_server->process(0.0); // Give server some cycles to commit.
  539. memdelete(polygon);
  540. memdelete(node_2d);
  541. }
  542. // This test case does not check precise values on purpose - to not be too sensitivte.
  543. TEST_CASE("[NavigationServer2D] Server should respond to queries against valid map properly") {
  544. NavigationServer2D *navigation_server = NavigationServer2D::get_singleton();
  545. Ref<NavigationPolygon> navigation_polygon;
  546. navigation_polygon.instantiate();
  547. Ref<NavigationMeshSourceGeometryData2D> source_geometry;
  548. source_geometry.instantiate();
  549. navigation_polygon->add_outline(PackedVector2Array({ Vector2(-1000.0, -1000.0), Vector2(1000.0, -1000.0), Vector2(1000.0, 1000.0), Vector2(-1000.0, 1000.0) }));
  550. // TODO: Other input?
  551. source_geometry->add_obstruction_outline(PackedVector2Array({ Vector2(-200.0, -200.0), Vector2(200.0, -200.0), Vector2(200.0, 200.0), Vector2(-200.0, 200.0) }));
  552. navigation_server->bake_from_source_geometry_data(navigation_polygon, source_geometry, Callable());
  553. CHECK_NE(navigation_polygon->get_polygon_count(), 0);
  554. CHECK_NE(navigation_polygon->get_vertices().size(), 0);
  555. CHECK_NE(navigation_polygon->get_outline_count(), 0);
  556. RID map = navigation_server->map_create();
  557. RID region = navigation_server->region_create();
  558. navigation_server->map_set_active(map, true);
  559. navigation_server->map_set_use_async_iterations(map, false);
  560. navigation_server->region_set_map(region, map);
  561. navigation_server->region_set_navigation_polygon(region, navigation_polygon);
  562. navigation_server->process(0.0); // Give server some cycles to commit.
  563. SUBCASE("Simple queries should return non-default values") {
  564. CHECK_NE(navigation_server->map_get_closest_point(map, Vector2(0.0, 0.0)), Vector2(0, 0));
  565. CHECK(navigation_server->map_get_closest_point_owner(map, Vector2(0.0, 0.0)).is_valid());
  566. CHECK_NE(navigation_server->map_get_path(map, Vector2(0, 0), Vector2(10, 10), true).size(), 0);
  567. CHECK_NE(navigation_server->map_get_path(map, Vector2(0, 0), Vector2(10, 10), false).size(), 0);
  568. }
  569. SUBCASE("Elaborate query with 'CORRIDORFUNNEL' post-processing should yield non-empty result") {
  570. Ref<NavigationPathQueryParameters2D> query_parameters;
  571. query_parameters.instantiate();
  572. query_parameters->set_map(map);
  573. query_parameters->set_start_position(Vector2(0, 0));
  574. query_parameters->set_target_position(Vector2(10, 10));
  575. query_parameters->set_path_postprocessing(NavigationPathQueryParameters2D::PATH_POSTPROCESSING_CORRIDORFUNNEL);
  576. Ref<NavigationPathQueryResult2D> query_result;
  577. query_result.instantiate();
  578. navigation_server->query_path(query_parameters, query_result);
  579. CHECK_NE(query_result->get_path().size(), 0);
  580. CHECK_NE(query_result->get_path_types().size(), 0);
  581. CHECK_NE(query_result->get_path_rids().size(), 0);
  582. CHECK_NE(query_result->get_path_owner_ids().size(), 0);
  583. }
  584. SUBCASE("Elaborate query with 'EDGECENTERED' post-processing should yield non-empty result") {
  585. Ref<NavigationPathQueryParameters2D> query_parameters;
  586. query_parameters.instantiate();
  587. query_parameters->set_map(map);
  588. query_parameters->set_start_position(Vector2(10, 10));
  589. query_parameters->set_target_position(Vector2(0, 0));
  590. query_parameters->set_path_postprocessing(NavigationPathQueryParameters2D::PATH_POSTPROCESSING_EDGECENTERED);
  591. Ref<NavigationPathQueryResult2D> query_result;
  592. query_result.instantiate();
  593. navigation_server->query_path(query_parameters, query_result);
  594. CHECK_NE(query_result->get_path().size(), 0);
  595. CHECK_NE(query_result->get_path_types().size(), 0);
  596. CHECK_NE(query_result->get_path_rids().size(), 0);
  597. CHECK_NE(query_result->get_path_owner_ids().size(), 0);
  598. }
  599. SUBCASE("Elaborate query with non-matching navigation layer mask should yield empty result") {
  600. Ref<NavigationPathQueryParameters2D> query_parameters;
  601. query_parameters.instantiate();
  602. query_parameters->set_map(map);
  603. query_parameters->set_start_position(Vector2(10, 10));
  604. query_parameters->set_target_position(Vector2(0, 0));
  605. query_parameters->set_navigation_layers(2);
  606. Ref<NavigationPathQueryResult2D> query_result;
  607. query_result.instantiate();
  608. navigation_server->query_path(query_parameters, query_result);
  609. CHECK_EQ(query_result->get_path().size(), 0);
  610. CHECK_EQ(query_result->get_path_types().size(), 0);
  611. CHECK_EQ(query_result->get_path_rids().size(), 0);
  612. CHECK_EQ(query_result->get_path_owner_ids().size(), 0);
  613. }
  614. SUBCASE("Elaborate query without metadata flags should yield path only") {
  615. Ref<NavigationPathQueryParameters2D> query_parameters;
  616. query_parameters.instantiate();
  617. query_parameters->set_map(map);
  618. query_parameters->set_start_position(Vector2(10, 10));
  619. query_parameters->set_target_position(Vector2(0, 0));
  620. query_parameters->set_metadata_flags(0);
  621. Ref<NavigationPathQueryResult2D> query_result;
  622. query_result.instantiate();
  623. navigation_server->query_path(query_parameters, query_result);
  624. CHECK_NE(query_result->get_path().size(), 0);
  625. CHECK_EQ(query_result->get_path_types().size(), 0);
  626. CHECK_EQ(query_result->get_path_rids().size(), 0);
  627. CHECK_EQ(query_result->get_path_owner_ids().size(), 0);
  628. }
  629. navigation_server->free(region);
  630. navigation_server->free(map);
  631. navigation_server->process(0.0); // Give server some cycles to commit.
  632. }
  633. TEST_CASE("[NavigationServer2D] Server should simplify path properly") {
  634. real_t simplify_epsilon = 0.2;
  635. Vector<Vector2> source_path;
  636. source_path.resize(7);
  637. source_path.write[0] = Vector2(0.0, 0.0);
  638. source_path.write[1] = Vector2(0.0, 1.0); // This point needs to go.
  639. source_path.write[2] = Vector2(0.0, 2.0); // This point needs to go.
  640. source_path.write[3] = Vector2(0.0, 2.0);
  641. source_path.write[4] = Vector2(2.0, 3.0);
  642. source_path.write[5] = Vector2(2.5, 4.0); // This point needs to go.
  643. source_path.write[6] = Vector2(3.0, 5.0);
  644. Vector<Vector2> simplified_path = NavigationServer2D::get_singleton()->simplify_path(source_path, simplify_epsilon);
  645. CHECK_EQ(simplified_path.size(), 4);
  646. }
  647. }
  648. } //namespace TestNavigationServer2D