test_navigation_server_3d.h 39 KB

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