godot_navigation_server.cpp 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918
  1. /**************************************************************************/
  2. /* godot_navigation_server.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 "godot_navigation_server.h"
  31. #include "core/os/mutex.h"
  32. #ifndef _3D_DISABLED
  33. #include "navigation_mesh_generator.h"
  34. #endif
  35. using namespace NavigationUtilities;
  36. /// Creates a struct for each function and a function that once called creates
  37. /// an instance of that struct with the submitted parameters.
  38. /// Then, that struct is stored in an array; the `sync` function consume that array.
  39. #define COMMAND_1(F_NAME, T_0, D_0) \
  40. struct MERGE(F_NAME, _command) : public SetCommand { \
  41. T_0 d_0; \
  42. MERGE(F_NAME, _command) \
  43. (T_0 p_d_0) : \
  44. d_0(p_d_0) {} \
  45. virtual void exec(GodotNavigationServer *server) override { \
  46. server->MERGE(_cmd_, F_NAME)(d_0); \
  47. } \
  48. }; \
  49. void GodotNavigationServer::F_NAME(T_0 D_0) { \
  50. auto cmd = memnew(MERGE(F_NAME, _command)( \
  51. D_0)); \
  52. add_command(cmd); \
  53. } \
  54. void GodotNavigationServer::MERGE(_cmd_, F_NAME)(T_0 D_0)
  55. #define COMMAND_2(F_NAME, T_0, D_0, T_1, D_1) \
  56. struct MERGE(F_NAME, _command) : public SetCommand { \
  57. T_0 d_0; \
  58. T_1 d_1; \
  59. MERGE(F_NAME, _command) \
  60. ( \
  61. T_0 p_d_0, \
  62. T_1 p_d_1) : \
  63. d_0(p_d_0), \
  64. d_1(p_d_1) {} \
  65. virtual void exec(GodotNavigationServer *server) override { \
  66. server->MERGE(_cmd_, F_NAME)(d_0, d_1); \
  67. } \
  68. }; \
  69. void GodotNavigationServer::F_NAME(T_0 D_0, T_1 D_1) { \
  70. auto cmd = memnew(MERGE(F_NAME, _command)( \
  71. D_0, \
  72. D_1)); \
  73. add_command(cmd); \
  74. } \
  75. void GodotNavigationServer::MERGE(_cmd_, F_NAME)(T_0 D_0, T_1 D_1)
  76. GodotNavigationServer::GodotNavigationServer() {}
  77. GodotNavigationServer::~GodotNavigationServer() {
  78. flush_queries();
  79. }
  80. void GodotNavigationServer::add_command(SetCommand *command) {
  81. MutexLock lock(commands_mutex);
  82. commands.push_back(command);
  83. }
  84. TypedArray<RID> GodotNavigationServer::get_maps() const {
  85. TypedArray<RID> all_map_rids;
  86. List<RID> maps_owned;
  87. map_owner.get_owned_list(&maps_owned);
  88. if (maps_owned.size()) {
  89. for (const RID &E : maps_owned) {
  90. all_map_rids.push_back(E);
  91. }
  92. }
  93. return all_map_rids;
  94. }
  95. RID GodotNavigationServer::map_create() {
  96. MutexLock lock(operations_mutex);
  97. RID rid = map_owner.make_rid();
  98. NavMap *map = map_owner.get_or_null(rid);
  99. map->set_self(rid);
  100. return rid;
  101. }
  102. COMMAND_2(map_set_active, RID, p_map, bool, p_active) {
  103. NavMap *map = map_owner.get_or_null(p_map);
  104. ERR_FAIL_COND(map == nullptr);
  105. if (p_active) {
  106. if (!map_is_active(p_map)) {
  107. active_maps.push_back(map);
  108. active_maps_update_id.push_back(map->get_map_update_id());
  109. }
  110. } else {
  111. int map_index = active_maps.find(map);
  112. ERR_FAIL_COND(map_index < 0);
  113. active_maps.remove_at(map_index);
  114. active_maps_update_id.remove_at(map_index);
  115. }
  116. }
  117. bool GodotNavigationServer::map_is_active(RID p_map) const {
  118. NavMap *map = map_owner.get_or_null(p_map);
  119. ERR_FAIL_COND_V(map == nullptr, false);
  120. return active_maps.find(map) >= 0;
  121. }
  122. COMMAND_2(map_set_up, RID, p_map, Vector3, p_up) {
  123. NavMap *map = map_owner.get_or_null(p_map);
  124. ERR_FAIL_COND(map == nullptr);
  125. map->set_up(p_up);
  126. }
  127. Vector3 GodotNavigationServer::map_get_up(RID p_map) const {
  128. const NavMap *map = map_owner.get_or_null(p_map);
  129. ERR_FAIL_COND_V(map == nullptr, Vector3());
  130. return map->get_up();
  131. }
  132. COMMAND_2(map_set_cell_size, RID, p_map, real_t, p_cell_size) {
  133. NavMap *map = map_owner.get_or_null(p_map);
  134. ERR_FAIL_COND(map == nullptr);
  135. map->set_cell_size(p_cell_size);
  136. }
  137. real_t GodotNavigationServer::map_get_cell_size(RID p_map) const {
  138. const NavMap *map = map_owner.get_or_null(p_map);
  139. ERR_FAIL_COND_V(map == nullptr, 0);
  140. return map->get_cell_size();
  141. }
  142. COMMAND_2(map_set_edge_connection_margin, RID, p_map, real_t, p_connection_margin) {
  143. NavMap *map = map_owner.get_or_null(p_map);
  144. ERR_FAIL_COND(map == nullptr);
  145. map->set_edge_connection_margin(p_connection_margin);
  146. }
  147. real_t GodotNavigationServer::map_get_edge_connection_margin(RID p_map) const {
  148. const NavMap *map = map_owner.get_or_null(p_map);
  149. ERR_FAIL_COND_V(map == nullptr, 0);
  150. return map->get_edge_connection_margin();
  151. }
  152. COMMAND_2(map_set_link_connection_radius, RID, p_map, real_t, p_connection_radius) {
  153. NavMap *map = map_owner.get_or_null(p_map);
  154. ERR_FAIL_COND(map == nullptr);
  155. map->set_link_connection_radius(p_connection_radius);
  156. }
  157. real_t GodotNavigationServer::map_get_link_connection_radius(RID p_map) const {
  158. const NavMap *map = map_owner.get_or_null(p_map);
  159. ERR_FAIL_COND_V(map == nullptr, 0);
  160. return map->get_link_connection_radius();
  161. }
  162. Vector<Vector3> GodotNavigationServer::map_get_path(RID p_map, Vector3 p_origin, Vector3 p_destination, bool p_optimize, uint32_t p_navigation_layers) const {
  163. const NavMap *map = map_owner.get_or_null(p_map);
  164. ERR_FAIL_COND_V(map == nullptr, Vector<Vector3>());
  165. return map->get_path(p_origin, p_destination, p_optimize, p_navigation_layers, nullptr, nullptr, nullptr);
  166. }
  167. Vector3 GodotNavigationServer::map_get_closest_point_to_segment(RID p_map, const Vector3 &p_from, const Vector3 &p_to, const bool p_use_collision) const {
  168. const NavMap *map = map_owner.get_or_null(p_map);
  169. ERR_FAIL_COND_V(map == nullptr, Vector3());
  170. return map->get_closest_point_to_segment(p_from, p_to, p_use_collision);
  171. }
  172. Vector3 GodotNavigationServer::map_get_closest_point(RID p_map, const Vector3 &p_point) const {
  173. const NavMap *map = map_owner.get_or_null(p_map);
  174. ERR_FAIL_COND_V(map == nullptr, Vector3());
  175. return map->get_closest_point(p_point);
  176. }
  177. Vector3 GodotNavigationServer::map_get_closest_point_normal(RID p_map, const Vector3 &p_point) const {
  178. const NavMap *map = map_owner.get_or_null(p_map);
  179. ERR_FAIL_COND_V(map == nullptr, Vector3());
  180. return map->get_closest_point_normal(p_point);
  181. }
  182. RID GodotNavigationServer::map_get_closest_point_owner(RID p_map, const Vector3 &p_point) const {
  183. const NavMap *map = map_owner.get_or_null(p_map);
  184. ERR_FAIL_COND_V(map == nullptr, RID());
  185. return map->get_closest_point_owner(p_point);
  186. }
  187. TypedArray<RID> GodotNavigationServer::map_get_links(RID p_map) const {
  188. TypedArray<RID> link_rids;
  189. const NavMap *map = map_owner.get_or_null(p_map);
  190. ERR_FAIL_COND_V(map == nullptr, link_rids);
  191. const LocalVector<NavLink *> links = map->get_links();
  192. link_rids.resize(links.size());
  193. for (uint32_t i = 0; i < links.size(); i++) {
  194. link_rids[i] = links[i]->get_self();
  195. }
  196. return link_rids;
  197. }
  198. TypedArray<RID> GodotNavigationServer::map_get_regions(RID p_map) const {
  199. TypedArray<RID> regions_rids;
  200. const NavMap *map = map_owner.get_or_null(p_map);
  201. ERR_FAIL_COND_V(map == nullptr, regions_rids);
  202. const LocalVector<NavRegion *> regions = map->get_regions();
  203. regions_rids.resize(regions.size());
  204. for (uint32_t i = 0; i < regions.size(); i++) {
  205. regions_rids[i] = regions[i]->get_self();
  206. }
  207. return regions_rids;
  208. }
  209. TypedArray<RID> GodotNavigationServer::map_get_agents(RID p_map) const {
  210. TypedArray<RID> agents_rids;
  211. const NavMap *map = map_owner.get_or_null(p_map);
  212. ERR_FAIL_COND_V(map == nullptr, agents_rids);
  213. const LocalVector<NavAgent *> agents = map->get_agents();
  214. agents_rids.resize(agents.size());
  215. for (uint32_t i = 0; i < agents.size(); i++) {
  216. agents_rids[i] = agents[i]->get_self();
  217. }
  218. return agents_rids;
  219. }
  220. RID GodotNavigationServer::region_get_map(RID p_region) const {
  221. NavRegion *region = region_owner.get_or_null(p_region);
  222. ERR_FAIL_COND_V(region == nullptr, RID());
  223. if (region->get_map()) {
  224. return region->get_map()->get_self();
  225. }
  226. return RID();
  227. }
  228. RID GodotNavigationServer::agent_get_map(RID p_agent) const {
  229. NavAgent *agent = agent_owner.get_or_null(p_agent);
  230. ERR_FAIL_COND_V(agent == nullptr, RID());
  231. if (agent->get_map()) {
  232. return agent->get_map()->get_self();
  233. }
  234. return RID();
  235. }
  236. RID GodotNavigationServer::region_create() {
  237. MutexLock lock(operations_mutex);
  238. RID rid = region_owner.make_rid();
  239. NavRegion *reg = region_owner.get_or_null(rid);
  240. reg->set_self(rid);
  241. return rid;
  242. }
  243. COMMAND_2(region_set_map, RID, p_region, RID, p_map) {
  244. NavRegion *region = region_owner.get_or_null(p_region);
  245. ERR_FAIL_COND(region == nullptr);
  246. if (region->get_map() != nullptr) {
  247. if (region->get_map()->get_self() == p_map) {
  248. return; // Pointless
  249. }
  250. region->get_map()->remove_region(region);
  251. region->set_map(nullptr);
  252. }
  253. if (p_map.is_valid()) {
  254. NavMap *map = map_owner.get_or_null(p_map);
  255. ERR_FAIL_COND(map == nullptr);
  256. map->add_region(region);
  257. region->set_map(map);
  258. }
  259. }
  260. COMMAND_2(region_set_transform, RID, p_region, Transform3D, p_transform) {
  261. NavRegion *region = region_owner.get_or_null(p_region);
  262. ERR_FAIL_COND(region == nullptr);
  263. region->set_transform(p_transform);
  264. }
  265. COMMAND_2(region_set_enter_cost, RID, p_region, real_t, p_enter_cost) {
  266. NavRegion *region = region_owner.get_or_null(p_region);
  267. ERR_FAIL_COND(region == nullptr);
  268. ERR_FAIL_COND(p_enter_cost < 0.0);
  269. region->set_enter_cost(p_enter_cost);
  270. }
  271. real_t GodotNavigationServer::region_get_enter_cost(RID p_region) const {
  272. NavRegion *region = region_owner.get_or_null(p_region);
  273. ERR_FAIL_COND_V(region == nullptr, 0);
  274. return region->get_enter_cost();
  275. }
  276. COMMAND_2(region_set_travel_cost, RID, p_region, real_t, p_travel_cost) {
  277. NavRegion *region = region_owner.get_or_null(p_region);
  278. ERR_FAIL_COND(region == nullptr);
  279. ERR_FAIL_COND(p_travel_cost < 0.0);
  280. region->set_travel_cost(p_travel_cost);
  281. }
  282. real_t GodotNavigationServer::region_get_travel_cost(RID p_region) const {
  283. NavRegion *region = region_owner.get_or_null(p_region);
  284. ERR_FAIL_COND_V(region == nullptr, 0);
  285. return region->get_travel_cost();
  286. }
  287. COMMAND_2(region_set_owner_id, RID, p_region, ObjectID, p_owner_id) {
  288. NavRegion *region = region_owner.get_or_null(p_region);
  289. ERR_FAIL_COND(region == nullptr);
  290. region->set_owner_id(p_owner_id);
  291. }
  292. ObjectID GodotNavigationServer::region_get_owner_id(RID p_region) const {
  293. const NavRegion *region = region_owner.get_or_null(p_region);
  294. ERR_FAIL_COND_V(region == nullptr, ObjectID());
  295. return region->get_owner_id();
  296. }
  297. bool GodotNavigationServer::region_owns_point(RID p_region, const Vector3 &p_point) const {
  298. const NavRegion *region = region_owner.get_or_null(p_region);
  299. ERR_FAIL_COND_V(region == nullptr, false);
  300. if (region->get_map()) {
  301. RID closest_point_owner = map_get_closest_point_owner(region->get_map()->get_self(), p_point);
  302. return closest_point_owner == region->get_self();
  303. }
  304. return false;
  305. }
  306. COMMAND_2(region_set_navigation_layers, RID, p_region, uint32_t, p_navigation_layers) {
  307. NavRegion *region = region_owner.get_or_null(p_region);
  308. ERR_FAIL_COND(region == nullptr);
  309. region->set_navigation_layers(p_navigation_layers);
  310. }
  311. uint32_t GodotNavigationServer::region_get_navigation_layers(RID p_region) const {
  312. NavRegion *region = region_owner.get_or_null(p_region);
  313. ERR_FAIL_COND_V(region == nullptr, 0);
  314. return region->get_navigation_layers();
  315. }
  316. COMMAND_2(region_set_navigation_mesh, RID, p_region, Ref<NavigationMesh>, p_navigation_mesh) {
  317. NavRegion *region = region_owner.get_or_null(p_region);
  318. ERR_FAIL_COND(region == nullptr);
  319. region->set_mesh(p_navigation_mesh);
  320. }
  321. void GodotNavigationServer::region_bake_navigation_mesh(Ref<NavigationMesh> p_navigation_mesh, Node *p_root_node) {
  322. ERR_FAIL_COND(p_navigation_mesh.is_null());
  323. ERR_FAIL_COND(p_root_node == nullptr);
  324. #ifndef _3D_DISABLED
  325. NavigationMeshGenerator::get_singleton()->clear(p_navigation_mesh);
  326. NavigationMeshGenerator::get_singleton()->bake(p_navigation_mesh, p_root_node);
  327. #endif
  328. }
  329. int GodotNavigationServer::region_get_connections_count(RID p_region) const {
  330. NavRegion *region = region_owner.get_or_null(p_region);
  331. ERR_FAIL_COND_V(!region, 0);
  332. return region->get_connections_count();
  333. }
  334. Vector3 GodotNavigationServer::region_get_connection_pathway_start(RID p_region, int p_connection_id) const {
  335. NavRegion *region = region_owner.get_or_null(p_region);
  336. ERR_FAIL_COND_V(!region, Vector3());
  337. return region->get_connection_pathway_start(p_connection_id);
  338. }
  339. Vector3 GodotNavigationServer::region_get_connection_pathway_end(RID p_region, int p_connection_id) const {
  340. NavRegion *region = region_owner.get_or_null(p_region);
  341. ERR_FAIL_COND_V(!region, Vector3());
  342. return region->get_connection_pathway_end(p_connection_id);
  343. }
  344. RID GodotNavigationServer::link_create() {
  345. MutexLock lock(operations_mutex);
  346. RID rid = link_owner.make_rid();
  347. NavLink *link = link_owner.get_or_null(rid);
  348. link->set_self(rid);
  349. return rid;
  350. }
  351. COMMAND_2(link_set_map, RID, p_link, RID, p_map) {
  352. NavLink *link = link_owner.get_or_null(p_link);
  353. ERR_FAIL_COND(link == nullptr);
  354. if (link->get_map() != nullptr) {
  355. if (link->get_map()->get_self() == p_map) {
  356. return; // Pointless
  357. }
  358. link->get_map()->remove_link(link);
  359. link->set_map(nullptr);
  360. }
  361. if (p_map.is_valid()) {
  362. NavMap *map = map_owner.get_or_null(p_map);
  363. ERR_FAIL_COND(map == nullptr);
  364. map->add_link(link);
  365. link->set_map(map);
  366. }
  367. }
  368. RID GodotNavigationServer::link_get_map(const RID p_link) const {
  369. const NavLink *link = link_owner.get_or_null(p_link);
  370. ERR_FAIL_COND_V(link == nullptr, RID());
  371. if (link->get_map()) {
  372. return link->get_map()->get_self();
  373. }
  374. return RID();
  375. }
  376. COMMAND_2(link_set_bidirectional, RID, p_link, bool, p_bidirectional) {
  377. NavLink *link = link_owner.get_or_null(p_link);
  378. ERR_FAIL_COND(link == nullptr);
  379. link->set_bidirectional(p_bidirectional);
  380. }
  381. bool GodotNavigationServer::link_is_bidirectional(RID p_link) const {
  382. const NavLink *link = link_owner.get_or_null(p_link);
  383. ERR_FAIL_COND_V(link == nullptr, false);
  384. return link->is_bidirectional();
  385. }
  386. COMMAND_2(link_set_navigation_layers, RID, p_link, uint32_t, p_navigation_layers) {
  387. NavLink *link = link_owner.get_or_null(p_link);
  388. ERR_FAIL_COND(link == nullptr);
  389. link->set_navigation_layers(p_navigation_layers);
  390. }
  391. uint32_t GodotNavigationServer::link_get_navigation_layers(const RID p_link) const {
  392. const NavLink *link = link_owner.get_or_null(p_link);
  393. ERR_FAIL_COND_V(link == nullptr, 0);
  394. return link->get_navigation_layers();
  395. }
  396. COMMAND_2(link_set_start_position, RID, p_link, Vector3, p_position) {
  397. NavLink *link = link_owner.get_or_null(p_link);
  398. ERR_FAIL_COND(link == nullptr);
  399. link->set_start_position(p_position);
  400. }
  401. Vector3 GodotNavigationServer::link_get_start_position(RID p_link) const {
  402. const NavLink *link = link_owner.get_or_null(p_link);
  403. ERR_FAIL_COND_V(link == nullptr, Vector3());
  404. return link->get_start_position();
  405. }
  406. COMMAND_2(link_set_end_position, RID, p_link, Vector3, p_position) {
  407. NavLink *link = link_owner.get_or_null(p_link);
  408. ERR_FAIL_COND(link == nullptr);
  409. link->set_end_position(p_position);
  410. }
  411. Vector3 GodotNavigationServer::link_get_end_position(RID p_link) const {
  412. const NavLink *link = link_owner.get_or_null(p_link);
  413. ERR_FAIL_COND_V(link == nullptr, Vector3());
  414. return link->get_end_position();
  415. }
  416. COMMAND_2(link_set_enter_cost, RID, p_link, real_t, p_enter_cost) {
  417. NavLink *link = link_owner.get_or_null(p_link);
  418. ERR_FAIL_COND(link == nullptr);
  419. link->set_enter_cost(p_enter_cost);
  420. }
  421. real_t GodotNavigationServer::link_get_enter_cost(const RID p_link) const {
  422. const NavLink *link = link_owner.get_or_null(p_link);
  423. ERR_FAIL_COND_V(link == nullptr, 0);
  424. return link->get_enter_cost();
  425. }
  426. COMMAND_2(link_set_travel_cost, RID, p_link, real_t, p_travel_cost) {
  427. NavLink *link = link_owner.get_or_null(p_link);
  428. ERR_FAIL_COND(link == nullptr);
  429. link->set_travel_cost(p_travel_cost);
  430. }
  431. real_t GodotNavigationServer::link_get_travel_cost(const RID p_link) const {
  432. const NavLink *link = link_owner.get_or_null(p_link);
  433. ERR_FAIL_COND_V(link == nullptr, 0);
  434. return link->get_travel_cost();
  435. }
  436. COMMAND_2(link_set_owner_id, RID, p_link, ObjectID, p_owner_id) {
  437. NavLink *link = link_owner.get_or_null(p_link);
  438. ERR_FAIL_COND(link == nullptr);
  439. link->set_owner_id(p_owner_id);
  440. }
  441. ObjectID GodotNavigationServer::link_get_owner_id(RID p_link) const {
  442. const NavLink *link = link_owner.get_or_null(p_link);
  443. ERR_FAIL_COND_V(link == nullptr, ObjectID());
  444. return link->get_owner_id();
  445. }
  446. RID GodotNavigationServer::agent_create() {
  447. MutexLock lock(operations_mutex);
  448. RID rid = agent_owner.make_rid();
  449. NavAgent *agent = agent_owner.get_or_null(rid);
  450. agent->set_self(rid);
  451. return rid;
  452. }
  453. COMMAND_2(agent_set_map, RID, p_agent, RID, p_map) {
  454. NavAgent *agent = agent_owner.get_or_null(p_agent);
  455. ERR_FAIL_COND(agent == nullptr);
  456. if (agent->get_map()) {
  457. if (agent->get_map()->get_self() == p_map) {
  458. return; // Pointless
  459. }
  460. agent->get_map()->remove_agent(agent);
  461. }
  462. agent->set_map(nullptr);
  463. if (p_map.is_valid()) {
  464. NavMap *map = map_owner.get_or_null(p_map);
  465. ERR_FAIL_COND(map == nullptr);
  466. agent->set_map(map);
  467. map->add_agent(agent);
  468. if (agent->has_callback()) {
  469. map->set_agent_as_controlled(agent);
  470. }
  471. }
  472. }
  473. COMMAND_2(agent_set_neighbor_distance, RID, p_agent, real_t, p_distance) {
  474. NavAgent *agent = agent_owner.get_or_null(p_agent);
  475. ERR_FAIL_COND(agent == nullptr);
  476. agent->get_agent()->neighborDist_ = p_distance;
  477. }
  478. COMMAND_2(agent_set_max_neighbors, RID, p_agent, int, p_count) {
  479. NavAgent *agent = agent_owner.get_or_null(p_agent);
  480. ERR_FAIL_COND(agent == nullptr);
  481. agent->get_agent()->maxNeighbors_ = p_count;
  482. }
  483. COMMAND_2(agent_set_time_horizon, RID, p_agent, real_t, p_time) {
  484. NavAgent *agent = agent_owner.get_or_null(p_agent);
  485. ERR_FAIL_COND(agent == nullptr);
  486. agent->get_agent()->timeHorizon_ = p_time;
  487. }
  488. COMMAND_2(agent_set_radius, RID, p_agent, real_t, p_radius) {
  489. NavAgent *agent = agent_owner.get_or_null(p_agent);
  490. ERR_FAIL_COND(agent == nullptr);
  491. agent->get_agent()->radius_ = p_radius;
  492. }
  493. COMMAND_2(agent_set_max_speed, RID, p_agent, real_t, p_max_speed) {
  494. NavAgent *agent = agent_owner.get_or_null(p_agent);
  495. ERR_FAIL_COND(agent == nullptr);
  496. agent->get_agent()->maxSpeed_ = p_max_speed;
  497. }
  498. COMMAND_2(agent_set_velocity, RID, p_agent, Vector3, p_velocity) {
  499. NavAgent *agent = agent_owner.get_or_null(p_agent);
  500. ERR_FAIL_COND(agent == nullptr);
  501. agent->get_agent()->velocity_ = RVO::Vector3(p_velocity.x, p_velocity.y, p_velocity.z);
  502. }
  503. COMMAND_2(agent_set_target_velocity, RID, p_agent, Vector3, p_velocity) {
  504. NavAgent *agent = agent_owner.get_or_null(p_agent);
  505. ERR_FAIL_COND(agent == nullptr);
  506. agent->get_agent()->prefVelocity_ = RVO::Vector3(p_velocity.x, p_velocity.y, p_velocity.z);
  507. }
  508. COMMAND_2(agent_set_position, RID, p_agent, Vector3, p_position) {
  509. NavAgent *agent = agent_owner.get_or_null(p_agent);
  510. ERR_FAIL_COND(agent == nullptr);
  511. agent->get_agent()->position_ = RVO::Vector3(p_position.x, p_position.y, p_position.z);
  512. }
  513. COMMAND_2(agent_set_ignore_y, RID, p_agent, bool, p_ignore) {
  514. NavAgent *agent = agent_owner.get_or_null(p_agent);
  515. ERR_FAIL_COND(agent == nullptr);
  516. agent->get_agent()->ignore_y_ = p_ignore;
  517. }
  518. bool GodotNavigationServer::agent_is_map_changed(RID p_agent) const {
  519. NavAgent *agent = agent_owner.get_or_null(p_agent);
  520. ERR_FAIL_COND_V(agent == nullptr, false);
  521. return agent->is_map_changed();
  522. }
  523. COMMAND_2(agent_set_callback, RID, p_agent, Callable, p_callback) {
  524. NavAgent *agent = agent_owner.get_or_null(p_agent);
  525. ERR_FAIL_COND(agent == nullptr);
  526. agent->set_callback(p_callback);
  527. if (agent->get_map()) {
  528. if (p_callback.is_valid()) {
  529. agent->get_map()->set_agent_as_controlled(agent);
  530. } else {
  531. agent->get_map()->remove_agent_as_controlled(agent);
  532. }
  533. }
  534. }
  535. COMMAND_1(free, RID, p_object) {
  536. if (map_owner.owns(p_object)) {
  537. NavMap *map = map_owner.get_or_null(p_object);
  538. // Removes any assigned region
  539. for (NavRegion *region : map->get_regions()) {
  540. map->remove_region(region);
  541. region->set_map(nullptr);
  542. }
  543. // Removes any assigned links
  544. for (NavLink *link : map->get_links()) {
  545. map->remove_link(link);
  546. link->set_map(nullptr);
  547. }
  548. // Remove any assigned agent
  549. for (NavAgent *agent : map->get_agents()) {
  550. map->remove_agent(agent);
  551. agent->set_map(nullptr);
  552. }
  553. int map_index = active_maps.find(map);
  554. active_maps.remove_at(map_index);
  555. active_maps_update_id.remove_at(map_index);
  556. map_owner.free(p_object);
  557. } else if (region_owner.owns(p_object)) {
  558. NavRegion *region = region_owner.get_or_null(p_object);
  559. // Removes this region from the map if assigned
  560. if (region->get_map() != nullptr) {
  561. region->get_map()->remove_region(region);
  562. region->set_map(nullptr);
  563. }
  564. region_owner.free(p_object);
  565. } else if (link_owner.owns(p_object)) {
  566. NavLink *link = link_owner.get_or_null(p_object);
  567. // Removes this link from the map if assigned
  568. if (link->get_map() != nullptr) {
  569. link->get_map()->remove_link(link);
  570. link->set_map(nullptr);
  571. }
  572. link_owner.free(p_object);
  573. } else if (agent_owner.owns(p_object)) {
  574. NavAgent *agent = agent_owner.get_or_null(p_object);
  575. // Removes this agent from the map if assigned
  576. if (agent->get_map() != nullptr) {
  577. agent->get_map()->remove_agent(agent);
  578. agent->set_map(nullptr);
  579. }
  580. agent_owner.free(p_object);
  581. } else {
  582. ERR_FAIL_COND("Attempted to free a NavigationServer RID that did not exist (or was already freed).");
  583. }
  584. }
  585. void GodotNavigationServer::set_active(bool p_active) {
  586. MutexLock lock(operations_mutex);
  587. active = p_active;
  588. }
  589. void GodotNavigationServer::flush_queries() {
  590. // In c++ we can't be sure that this is performed in the main thread
  591. // even with mutable functions.
  592. MutexLock lock(commands_mutex);
  593. MutexLock lock2(operations_mutex);
  594. for (SetCommand *command : commands) {
  595. command->exec(this);
  596. memdelete(command);
  597. }
  598. commands.clear();
  599. }
  600. void GodotNavigationServer::map_force_update(RID p_map) {
  601. NavMap *map = map_owner.get_or_null(p_map);
  602. ERR_FAIL_COND(map == nullptr);
  603. flush_queries();
  604. map->sync();
  605. }
  606. void GodotNavigationServer::process(real_t p_delta_time) {
  607. flush_queries();
  608. if (!active) {
  609. return;
  610. }
  611. int _new_pm_region_count = 0;
  612. int _new_pm_agent_count = 0;
  613. int _new_pm_link_count = 0;
  614. int _new_pm_polygon_count = 0;
  615. int _new_pm_edge_count = 0;
  616. int _new_pm_edge_merge_count = 0;
  617. int _new_pm_edge_connection_count = 0;
  618. int _new_pm_edge_free_count = 0;
  619. // In c++ we can't be sure that this is performed in the main thread
  620. // even with mutable functions.
  621. MutexLock lock(operations_mutex);
  622. for (uint32_t i(0); i < active_maps.size(); i++) {
  623. active_maps[i]->sync();
  624. active_maps[i]->step(p_delta_time);
  625. active_maps[i]->dispatch_callbacks();
  626. _new_pm_region_count += active_maps[i]->get_pm_region_count();
  627. _new_pm_agent_count += active_maps[i]->get_pm_agent_count();
  628. _new_pm_link_count += active_maps[i]->get_pm_link_count();
  629. _new_pm_polygon_count += active_maps[i]->get_pm_polygon_count();
  630. _new_pm_edge_count += active_maps[i]->get_pm_edge_count();
  631. _new_pm_edge_merge_count += active_maps[i]->get_pm_edge_merge_count();
  632. _new_pm_edge_connection_count += active_maps[i]->get_pm_edge_connection_count();
  633. _new_pm_edge_free_count += active_maps[i]->get_pm_edge_free_count();
  634. // Emit a signal if a map changed.
  635. const uint32_t new_map_update_id = active_maps[i]->get_map_update_id();
  636. if (new_map_update_id != active_maps_update_id[i]) {
  637. emit_signal(SNAME("map_changed"), active_maps[i]->get_self());
  638. active_maps_update_id[i] = new_map_update_id;
  639. }
  640. }
  641. pm_region_count = _new_pm_region_count;
  642. pm_agent_count = _new_pm_agent_count;
  643. pm_link_count = _new_pm_link_count;
  644. pm_polygon_count = _new_pm_polygon_count;
  645. pm_edge_count = _new_pm_edge_count;
  646. pm_edge_merge_count = _new_pm_edge_merge_count;
  647. pm_edge_connection_count = _new_pm_edge_connection_count;
  648. pm_edge_free_count = _new_pm_edge_free_count;
  649. }
  650. PathQueryResult GodotNavigationServer::_query_path(const PathQueryParameters &p_parameters) const {
  651. PathQueryResult r_query_result;
  652. const NavMap *map = map_owner.get_or_null(p_parameters.map);
  653. ERR_FAIL_COND_V(map == nullptr, r_query_result);
  654. // run the pathfinding
  655. if (p_parameters.pathfinding_algorithm == PathfindingAlgorithm::PATHFINDING_ALGORITHM_ASTAR) {
  656. // while postprocessing is still part of map.get_path() need to check and route it here for the correct "optimize" post-processing
  657. if (p_parameters.path_postprocessing == PathPostProcessing::PATH_POSTPROCESSING_CORRIDORFUNNEL) {
  658. r_query_result.path = map->get_path(
  659. p_parameters.start_position,
  660. p_parameters.target_position,
  661. true,
  662. p_parameters.navigation_layers,
  663. p_parameters.metadata_flags.has_flag(PathMetadataFlags::PATH_INCLUDE_TYPES) ? &r_query_result.path_types : nullptr,
  664. p_parameters.metadata_flags.has_flag(PathMetadataFlags::PATH_INCLUDE_RIDS) ? &r_query_result.path_rids : nullptr,
  665. p_parameters.metadata_flags.has_flag(PathMetadataFlags::PATH_INCLUDE_OWNERS) ? &r_query_result.path_owner_ids : nullptr);
  666. } else if (p_parameters.path_postprocessing == PathPostProcessing::PATH_POSTPROCESSING_EDGECENTERED) {
  667. r_query_result.path = map->get_path(
  668. p_parameters.start_position,
  669. p_parameters.target_position,
  670. false,
  671. p_parameters.navigation_layers,
  672. p_parameters.metadata_flags.has_flag(PathMetadataFlags::PATH_INCLUDE_TYPES) ? &r_query_result.path_types : nullptr,
  673. p_parameters.metadata_flags.has_flag(PathMetadataFlags::PATH_INCLUDE_RIDS) ? &r_query_result.path_rids : nullptr,
  674. p_parameters.metadata_flags.has_flag(PathMetadataFlags::PATH_INCLUDE_OWNERS) ? &r_query_result.path_owner_ids : nullptr);
  675. }
  676. } else {
  677. return r_query_result;
  678. }
  679. // add path postprocessing
  680. // add path stats
  681. return r_query_result;
  682. }
  683. int GodotNavigationServer::get_process_info(ProcessInfo p_info) const {
  684. switch (p_info) {
  685. case INFO_ACTIVE_MAPS: {
  686. return active_maps.size();
  687. } break;
  688. case INFO_REGION_COUNT: {
  689. return pm_region_count;
  690. } break;
  691. case INFO_AGENT_COUNT: {
  692. return pm_agent_count;
  693. } break;
  694. case INFO_LINK_COUNT: {
  695. return pm_link_count;
  696. } break;
  697. case INFO_POLYGON_COUNT: {
  698. return pm_polygon_count;
  699. } break;
  700. case INFO_EDGE_COUNT: {
  701. return pm_edge_count;
  702. } break;
  703. case INFO_EDGE_MERGE_COUNT: {
  704. return pm_edge_merge_count;
  705. } break;
  706. case INFO_EDGE_CONNECTION_COUNT: {
  707. return pm_edge_connection_count;
  708. } break;
  709. case INFO_EDGE_FREE_COUNT: {
  710. return pm_edge_free_count;
  711. } break;
  712. }
  713. return 0;
  714. }
  715. #undef COMMAND_1
  716. #undef COMMAND_2