godot_navigation_server.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667
  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. /// Creates a struct for each function and a function that once called creates
  36. /// an instance of that struct with the submitted parameters.
  37. /// Then, that struct is stored in an array; the `sync` function consume that array.
  38. #define COMMAND_1(F_NAME, T_0, D_0) \
  39. struct MERGE(F_NAME, _command) : public SetCommand { \
  40. T_0 d_0; \
  41. MERGE(F_NAME, _command) \
  42. (T_0 p_d_0) : \
  43. d_0(p_d_0) {} \
  44. virtual void exec(GodotNavigationServer *server) { \
  45. server->MERGE(_cmd_, F_NAME)(d_0); \
  46. } \
  47. }; \
  48. void GodotNavigationServer::F_NAME(T_0 D_0) const { \
  49. auto cmd = memnew(MERGE(F_NAME, _command)( \
  50. D_0)); \
  51. add_command(cmd); \
  52. } \
  53. void GodotNavigationServer::MERGE(_cmd_, F_NAME)(T_0 D_0)
  54. #define COMMAND_2(F_NAME, T_0, D_0, T_1, D_1) \
  55. struct MERGE(F_NAME, _command) : public SetCommand { \
  56. T_0 d_0; \
  57. T_1 d_1; \
  58. MERGE(F_NAME, _command) \
  59. ( \
  60. T_0 p_d_0, \
  61. T_1 p_d_1) : \
  62. d_0(p_d_0), \
  63. d_1(p_d_1) {} \
  64. virtual void exec(GodotNavigationServer *server) { \
  65. server->MERGE(_cmd_, F_NAME)(d_0, d_1); \
  66. } \
  67. }; \
  68. void GodotNavigationServer::F_NAME(T_0 D_0, T_1 D_1) const { \
  69. auto cmd = memnew(MERGE(F_NAME, _command)( \
  70. D_0, \
  71. D_1)); \
  72. add_command(cmd); \
  73. } \
  74. void GodotNavigationServer::MERGE(_cmd_, F_NAME)(T_0 D_0, T_1 D_1)
  75. #define COMMAND_4(F_NAME, T_0, D_0, T_1, D_1, T_2, D_2, T_3, D_3) \
  76. struct MERGE(F_NAME, _command) : public SetCommand { \
  77. T_0 d_0; \
  78. T_1 d_1; \
  79. T_2 d_2; \
  80. T_3 d_3; \
  81. MERGE(F_NAME, _command) \
  82. ( \
  83. T_0 p_d_0, \
  84. T_1 p_d_1, \
  85. T_2 p_d_2, \
  86. T_3 p_d_3) : \
  87. d_0(p_d_0), \
  88. d_1(p_d_1), \
  89. d_2(p_d_2), \
  90. d_3(p_d_3) {} \
  91. virtual void exec(GodotNavigationServer *server) { \
  92. server->MERGE(_cmd_, F_NAME)(d_0, d_1, d_2, d_3); \
  93. } \
  94. }; \
  95. void GodotNavigationServer::F_NAME(T_0 D_0, T_1 D_1, T_2 D_2, T_3 D_3) const { \
  96. auto cmd = memnew(MERGE(F_NAME, _command)( \
  97. D_0, \
  98. D_1, \
  99. D_2, \
  100. D_3)); \
  101. add_command(cmd); \
  102. } \
  103. void GodotNavigationServer::MERGE(_cmd_, F_NAME)(T_0 D_0, T_1 D_1, T_2 D_2, T_3 D_3)
  104. GodotNavigationServer::GodotNavigationServer() {}
  105. GodotNavigationServer::~GodotNavigationServer() {
  106. flush_queries();
  107. }
  108. void GodotNavigationServer::add_command(SetCommand *command) const {
  109. GodotNavigationServer *mut_this = const_cast<GodotNavigationServer *>(this);
  110. {
  111. MutexLock lock(commands_mutex);
  112. mut_this->commands.push_back(command);
  113. }
  114. }
  115. Array GodotNavigationServer::get_maps() const {
  116. Array all_map_rids;
  117. List<RID> maps_owned;
  118. map_owner.get_owned_list(&maps_owned);
  119. if (maps_owned.size()) {
  120. for (List<RID>::Element *E = maps_owned.front(); E; E = E->next()) {
  121. all_map_rids.push_back(E->get());
  122. }
  123. }
  124. return all_map_rids;
  125. }
  126. RID GodotNavigationServer::map_create() const {
  127. GodotNavigationServer *mut_this = const_cast<GodotNavigationServer *>(this);
  128. MutexLock lock(mut_this->operations_mutex);
  129. NavMap *space = memnew(NavMap);
  130. RID rid = map_owner.make_rid(space);
  131. space->set_self(rid);
  132. return rid;
  133. }
  134. COMMAND_2(map_set_active, RID, p_map, bool, p_active) {
  135. NavMap *map = map_owner.getornull(p_map);
  136. ERR_FAIL_COND(map == nullptr);
  137. if (p_active) {
  138. if (!map_is_active(p_map)) {
  139. active_maps.push_back(map);
  140. active_maps_update_id.push_back(map->get_map_update_id());
  141. }
  142. } else {
  143. int map_index = active_maps.find(map);
  144. ERR_FAIL_COND(map_index < 0);
  145. active_maps.remove(map_index);
  146. active_maps_update_id.remove(map_index);
  147. }
  148. }
  149. bool GodotNavigationServer::map_is_active(RID p_map) const {
  150. NavMap *map = map_owner.getornull(p_map);
  151. ERR_FAIL_COND_V(map == nullptr, false);
  152. return active_maps.find(map) >= 0;
  153. }
  154. COMMAND_2(map_set_up, RID, p_map, Vector3, p_up) {
  155. NavMap *map = map_owner.getornull(p_map);
  156. ERR_FAIL_COND(map == nullptr);
  157. map->set_up(p_up);
  158. }
  159. Vector3 GodotNavigationServer::map_get_up(RID p_map) const {
  160. const NavMap *map = map_owner.getornull(p_map);
  161. ERR_FAIL_COND_V(map == nullptr, Vector3());
  162. return map->get_up();
  163. }
  164. COMMAND_2(map_set_cell_size, RID, p_map, real_t, p_cell_size) {
  165. NavMap *map = map_owner.getornull(p_map);
  166. ERR_FAIL_COND(map == nullptr);
  167. map->set_cell_size(p_cell_size);
  168. }
  169. real_t GodotNavigationServer::map_get_cell_size(RID p_map) const {
  170. const NavMap *map = map_owner.getornull(p_map);
  171. ERR_FAIL_COND_V(map == nullptr, 0);
  172. return map->get_cell_size();
  173. }
  174. COMMAND_2(map_set_cell_height, RID, p_map, real_t, p_cell_height) {
  175. NavMap *map = map_owner.getornull(p_map);
  176. ERR_FAIL_COND(map == nullptr);
  177. map->set_cell_height(p_cell_height);
  178. }
  179. real_t GodotNavigationServer::map_get_cell_height(RID p_map) const {
  180. const NavMap *map = map_owner.getornull(p_map);
  181. ERR_FAIL_COND_V(map == nullptr, 0);
  182. return map->get_cell_height();
  183. }
  184. COMMAND_2(map_set_edge_connection_margin, RID, p_map, real_t, p_connection_margin) {
  185. NavMap *map = map_owner.getornull(p_map);
  186. ERR_FAIL_COND(map == nullptr);
  187. map->set_edge_connection_margin(p_connection_margin);
  188. }
  189. real_t GodotNavigationServer::map_get_edge_connection_margin(RID p_map) const {
  190. const NavMap *map = map_owner.getornull(p_map);
  191. ERR_FAIL_COND_V(map == nullptr, 0);
  192. return map->get_edge_connection_margin();
  193. }
  194. Vector<Vector3> GodotNavigationServer::map_get_path(RID p_map, Vector3 p_origin, Vector3 p_destination, bool p_optimize, uint32_t p_layers) const {
  195. const NavMap *map = map_owner.getornull(p_map);
  196. ERR_FAIL_COND_V(map == nullptr, Vector<Vector3>());
  197. return map->get_path(p_origin, p_destination, p_optimize, p_layers);
  198. }
  199. 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 {
  200. const NavMap *map = map_owner.getornull(p_map);
  201. ERR_FAIL_COND_V(map == nullptr, Vector3());
  202. return map->get_closest_point_to_segment(p_from, p_to, p_use_collision);
  203. }
  204. Vector3 GodotNavigationServer::map_get_closest_point(RID p_map, const Vector3 &p_point) const {
  205. const NavMap *map = map_owner.getornull(p_map);
  206. ERR_FAIL_COND_V(map == nullptr, Vector3());
  207. return map->get_closest_point(p_point);
  208. }
  209. Vector3 GodotNavigationServer::map_get_closest_point_normal(RID p_map, const Vector3 &p_point) const {
  210. const NavMap *map = map_owner.getornull(p_map);
  211. ERR_FAIL_COND_V(map == nullptr, Vector3());
  212. return map->get_closest_point_normal(p_point);
  213. }
  214. RID GodotNavigationServer::map_get_closest_point_owner(RID p_map, const Vector3 &p_point) const {
  215. const NavMap *map = map_owner.getornull(p_map);
  216. ERR_FAIL_COND_V(map == nullptr, RID());
  217. return map->get_closest_point_owner(p_point);
  218. }
  219. Array GodotNavigationServer::map_get_regions(RID p_map) const {
  220. Array regions_rids;
  221. const NavMap *map = map_owner.getornull(p_map);
  222. ERR_FAIL_COND_V(map == nullptr, regions_rids);
  223. const LocalVector<NavRegion *> regions = map->get_regions();
  224. regions_rids.resize(regions.size());
  225. for (uint32_t i = 0; i < regions.size(); i++) {
  226. regions_rids[i] = regions[i]->get_self();
  227. }
  228. return regions_rids;
  229. }
  230. Array GodotNavigationServer::map_get_agents(RID p_map) const {
  231. Array agents_rids;
  232. const NavMap *map = map_owner.getornull(p_map);
  233. ERR_FAIL_COND_V(map == nullptr, agents_rids);
  234. const LocalVector<RvoAgent *> agents = map->get_agents();
  235. agents_rids.resize(agents.size());
  236. for (uint32_t i = 0; i < agents.size(); i++) {
  237. agents_rids[i] = agents[i]->get_self();
  238. }
  239. return agents_rids;
  240. }
  241. RID GodotNavigationServer::region_get_map(RID p_region) const {
  242. NavRegion *region = region_owner.getornull(p_region);
  243. ERR_FAIL_COND_V(region == nullptr, RID());
  244. if (region->get_map()) {
  245. return region->get_map()->get_self();
  246. }
  247. return RID();
  248. }
  249. RID GodotNavigationServer::agent_get_map(RID p_agent) const {
  250. RvoAgent *agent = agent_owner.getornull(p_agent);
  251. ERR_FAIL_COND_V(agent == nullptr, RID());
  252. if (agent->get_map()) {
  253. return agent->get_map()->get_self();
  254. }
  255. return RID();
  256. }
  257. RID GodotNavigationServer::region_create() const {
  258. GodotNavigationServer *mut_this = const_cast<GodotNavigationServer *>(this);
  259. MutexLock lock(mut_this->operations_mutex);
  260. NavRegion *reg = memnew(NavRegion);
  261. RID rid = region_owner.make_rid(reg);
  262. reg->set_self(rid);
  263. return rid;
  264. }
  265. COMMAND_2(region_set_map, RID, p_region, RID, p_map) {
  266. NavRegion *region = region_owner.getornull(p_region);
  267. ERR_FAIL_COND(region == nullptr);
  268. if (region->get_map() != nullptr) {
  269. if (region->get_map()->get_self() == p_map) {
  270. return; // Pointless
  271. }
  272. region->get_map()->remove_region(region);
  273. region->set_map(nullptr);
  274. }
  275. if (p_map.is_valid()) {
  276. NavMap *map = map_owner.getornull(p_map);
  277. ERR_FAIL_COND(map == nullptr);
  278. map->add_region(region);
  279. region->set_map(map);
  280. }
  281. }
  282. COMMAND_2(region_set_transform, RID, p_region, Transform, p_transform) {
  283. NavRegion *region = region_owner.getornull(p_region);
  284. ERR_FAIL_COND(region == nullptr);
  285. region->set_transform(p_transform);
  286. }
  287. COMMAND_2(region_set_enter_cost, RID, p_region, real_t, p_enter_cost) {
  288. NavRegion *region = region_owner.getornull(p_region);
  289. ERR_FAIL_COND(region == nullptr);
  290. ERR_FAIL_COND(p_enter_cost < 0.0);
  291. region->set_enter_cost(p_enter_cost);
  292. }
  293. real_t GodotNavigationServer::region_get_enter_cost(RID p_region) const {
  294. NavRegion *region = region_owner.getornull(p_region);
  295. ERR_FAIL_COND_V(region == nullptr, 0);
  296. return region->get_enter_cost();
  297. }
  298. COMMAND_2(region_set_travel_cost, RID, p_region, real_t, p_travel_cost) {
  299. NavRegion *region = region_owner.getornull(p_region);
  300. ERR_FAIL_COND(region == nullptr);
  301. ERR_FAIL_COND(p_travel_cost < 0.0);
  302. region->set_travel_cost(p_travel_cost);
  303. }
  304. real_t GodotNavigationServer::region_get_travel_cost(RID p_region) const {
  305. NavRegion *region = region_owner.getornull(p_region);
  306. ERR_FAIL_COND_V(region == nullptr, 0);
  307. return region->get_travel_cost();
  308. }
  309. bool GodotNavigationServer::region_owns_point(RID p_region, const Vector3 &p_point) const {
  310. const NavRegion *region = region_owner.getornull(p_region);
  311. ERR_FAIL_COND_V(region == nullptr, false);
  312. if (region->get_map()) {
  313. RID closest_point_owner = map_get_closest_point_owner(region->get_map()->get_self(), p_point);
  314. return closest_point_owner == region->get_self();
  315. }
  316. return false;
  317. }
  318. COMMAND_2(region_set_navigation_layers, RID, p_region, uint32_t, p_navigation_layers) {
  319. NavRegion *region = region_owner.getornull(p_region);
  320. ERR_FAIL_COND(region == nullptr);
  321. region->set_navigation_layers(p_navigation_layers);
  322. }
  323. uint32_t GodotNavigationServer::region_get_navigation_layers(RID p_region) const {
  324. NavRegion *region = region_owner.getornull(p_region);
  325. ERR_FAIL_COND_V(region == nullptr, 0);
  326. return region->get_navigation_layers();
  327. }
  328. COMMAND_2(region_set_navmesh, RID, p_region, Ref<NavigationMesh>, p_nav_mesh) {
  329. NavRegion *region = region_owner.getornull(p_region);
  330. ERR_FAIL_COND(region == nullptr);
  331. region->set_mesh(p_nav_mesh);
  332. }
  333. void GodotNavigationServer::region_bake_navmesh(Ref<NavigationMesh> r_mesh, Node *p_node) const {
  334. ERR_FAIL_COND(r_mesh.is_null());
  335. ERR_FAIL_COND(p_node == nullptr);
  336. #ifndef _3D_DISABLED
  337. NavigationMeshGenerator::get_singleton()->clear(r_mesh);
  338. NavigationMeshGenerator::get_singleton()->bake(r_mesh, p_node);
  339. #endif
  340. }
  341. int GodotNavigationServer::region_get_connections_count(RID p_region) const {
  342. NavRegion *region = region_owner.getornull(p_region);
  343. ERR_FAIL_COND_V(!region, 0);
  344. return region->get_connections_count();
  345. }
  346. Vector3 GodotNavigationServer::region_get_connection_pathway_start(RID p_region, int p_connection_id) const {
  347. NavRegion *region = region_owner.getornull(p_region);
  348. ERR_FAIL_COND_V(!region, Vector3());
  349. return region->get_connection_pathway_start(p_connection_id);
  350. }
  351. Vector3 GodotNavigationServer::region_get_connection_pathway_end(RID p_region, int p_connection_id) const {
  352. NavRegion *region = region_owner.getornull(p_region);
  353. ERR_FAIL_COND_V(!region, Vector3());
  354. return region->get_connection_pathway_end(p_connection_id);
  355. }
  356. RID GodotNavigationServer::agent_create() const {
  357. GodotNavigationServer *mut_this = const_cast<GodotNavigationServer *>(this);
  358. MutexLock lock(mut_this->operations_mutex);
  359. RvoAgent *agent = memnew(RvoAgent());
  360. RID rid = agent_owner.make_rid(agent);
  361. agent->set_self(rid);
  362. return rid;
  363. }
  364. COMMAND_2(agent_set_map, RID, p_agent, RID, p_map) {
  365. RvoAgent *agent = agent_owner.getornull(p_agent);
  366. ERR_FAIL_COND(agent == nullptr);
  367. if (agent->get_map()) {
  368. if (agent->get_map()->get_self() == p_map) {
  369. return; // Pointless
  370. }
  371. agent->get_map()->remove_agent(agent);
  372. }
  373. agent->set_map(nullptr);
  374. if (p_map.is_valid()) {
  375. NavMap *map = map_owner.getornull(p_map);
  376. ERR_FAIL_COND(map == nullptr);
  377. agent->set_map(map);
  378. map->add_agent(agent);
  379. if (agent->has_callback()) {
  380. map->set_agent_as_controlled(agent);
  381. }
  382. }
  383. }
  384. COMMAND_2(agent_set_neighbor_dist, RID, p_agent, real_t, p_dist) {
  385. RvoAgent *agent = agent_owner.getornull(p_agent);
  386. ERR_FAIL_COND(agent == nullptr);
  387. agent->get_agent()->neighborDist_ = p_dist;
  388. }
  389. COMMAND_2(agent_set_max_neighbors, RID, p_agent, int, p_count) {
  390. RvoAgent *agent = agent_owner.getornull(p_agent);
  391. ERR_FAIL_COND(agent == nullptr);
  392. agent->get_agent()->maxNeighbors_ = p_count;
  393. }
  394. COMMAND_2(agent_set_time_horizon, RID, p_agent, real_t, p_time) {
  395. RvoAgent *agent = agent_owner.getornull(p_agent);
  396. ERR_FAIL_COND(agent == nullptr);
  397. agent->get_agent()->timeHorizon_ = p_time;
  398. }
  399. COMMAND_2(agent_set_radius, RID, p_agent, real_t, p_radius) {
  400. RvoAgent *agent = agent_owner.getornull(p_agent);
  401. ERR_FAIL_COND(agent == nullptr);
  402. agent->get_agent()->radius_ = p_radius;
  403. }
  404. COMMAND_2(agent_set_max_speed, RID, p_agent, real_t, p_max_speed) {
  405. RvoAgent *agent = agent_owner.getornull(p_agent);
  406. ERR_FAIL_COND(agent == nullptr);
  407. agent->get_agent()->maxSpeed_ = p_max_speed;
  408. }
  409. COMMAND_2(agent_set_velocity, RID, p_agent, Vector3, p_velocity) {
  410. RvoAgent *agent = agent_owner.getornull(p_agent);
  411. ERR_FAIL_COND(agent == nullptr);
  412. agent->get_agent()->velocity_ = RVO::Vector3(p_velocity.x, p_velocity.y, p_velocity.z);
  413. }
  414. COMMAND_2(agent_set_target_velocity, RID, p_agent, Vector3, p_velocity) {
  415. RvoAgent *agent = agent_owner.getornull(p_agent);
  416. ERR_FAIL_COND(agent == nullptr);
  417. agent->get_agent()->prefVelocity_ = RVO::Vector3(p_velocity.x, p_velocity.y, p_velocity.z);
  418. }
  419. COMMAND_2(agent_set_position, RID, p_agent, Vector3, p_position) {
  420. RvoAgent *agent = agent_owner.getornull(p_agent);
  421. ERR_FAIL_COND(agent == nullptr);
  422. agent->get_agent()->position_ = RVO::Vector3(p_position.x, p_position.y, p_position.z);
  423. }
  424. COMMAND_2(agent_set_ignore_y, RID, p_agent, bool, p_ignore) {
  425. RvoAgent *agent = agent_owner.getornull(p_agent);
  426. ERR_FAIL_COND(agent == nullptr);
  427. agent->get_agent()->ignore_y_ = p_ignore;
  428. }
  429. bool GodotNavigationServer::agent_is_map_changed(RID p_agent) const {
  430. RvoAgent *agent = agent_owner.getornull(p_agent);
  431. ERR_FAIL_COND_V(agent == nullptr, false);
  432. return agent->is_map_changed();
  433. }
  434. COMMAND_4(agent_set_callback, RID, p_agent, ObjectID, p_object_id, StringName, p_method, Variant, p_udata) {
  435. RvoAgent *agent = agent_owner.getornull(p_agent);
  436. ERR_FAIL_COND(agent == nullptr);
  437. agent->set_callback(p_object_id, p_method, p_udata);
  438. if (agent->get_map()) {
  439. if (p_object_id == ObjectID()) {
  440. agent->get_map()->remove_agent_as_controlled(agent);
  441. } else {
  442. agent->get_map()->set_agent_as_controlled(agent);
  443. }
  444. }
  445. }
  446. COMMAND_1(free, RID, p_object) {
  447. if (!p_object.is_valid()) {
  448. ERR_FAIL_MSG("NavigationServer attempted to free a NULL RID.");
  449. return;
  450. }
  451. if (map_owner.owns(p_object)) {
  452. NavMap *map = map_owner.getornull(p_object);
  453. // Removes any assigned region
  454. LocalVector<NavRegion *> regions = map->get_regions();
  455. for (uint32_t i = 0; i < regions.size(); i++) {
  456. map->remove_region(regions[i]);
  457. regions[i]->set_map(nullptr);
  458. }
  459. // Remove any assigned agent
  460. LocalVector<RvoAgent *> agents = map->get_agents();
  461. for (uint32_t i = 0; i < agents.size(); i++) {
  462. map->remove_agent(agents[i]);
  463. agents[i]->set_map(nullptr);
  464. }
  465. int map_index = active_maps.find(map);
  466. active_maps.remove(map_index);
  467. active_maps_update_id.remove(map_index);
  468. map_owner.free(p_object);
  469. memdelete(map);
  470. } else if (region_owner.owns(p_object)) {
  471. NavRegion *region = region_owner.getornull(p_object);
  472. // Removes this region from the map if assigned
  473. if (region->get_map() != nullptr) {
  474. region->get_map()->remove_region(region);
  475. region->set_map(nullptr);
  476. }
  477. region_owner.free(p_object);
  478. memdelete(region);
  479. } else if (agent_owner.owns(p_object)) {
  480. RvoAgent *agent = agent_owner.getornull(p_object);
  481. // Removes this agent from the map if assigned
  482. if (agent->get_map() != nullptr) {
  483. agent->get_map()->remove_agent(agent);
  484. agent->set_map(nullptr);
  485. }
  486. agent_owner.free(p_object);
  487. memdelete(agent);
  488. } else {
  489. ERR_PRINT("Attempted to free a NavigationServer RID that did not exist (or was already freed).");
  490. }
  491. }
  492. void GodotNavigationServer::set_active(bool p_active) const {
  493. GodotNavigationServer *mut_this = const_cast<GodotNavigationServer *>(this);
  494. MutexLock lock(mut_this->operations_mutex);
  495. mut_this->active = p_active;
  496. }
  497. void GodotNavigationServer::flush_queries() {
  498. // In C++ we can't be sure that this is performed in the main thread
  499. // even with mutable functions.
  500. MutexLock lock(commands_mutex);
  501. MutexLock lock2(operations_mutex);
  502. for (size_t i(0); i < commands.size(); i++) {
  503. commands[i]->exec(this);
  504. memdelete(commands[i]);
  505. }
  506. commands.clear();
  507. }
  508. void GodotNavigationServer::map_force_update(RID p_map) {
  509. NavMap *map = map_owner.getornull(p_map);
  510. ERR_FAIL_COND(map == nullptr);
  511. flush_queries();
  512. map->sync();
  513. }
  514. void GodotNavigationServer::process(real_t p_delta_time) {
  515. flush_queries();
  516. if (!active) {
  517. return;
  518. }
  519. // In c++ we can't be sure that this is performed in the main thread
  520. // even with mutable functions.
  521. MutexLock lock(operations_mutex);
  522. for (uint32_t i(0); i < active_maps.size(); i++) {
  523. active_maps[i]->sync();
  524. active_maps[i]->step(p_delta_time);
  525. active_maps[i]->dispatch_callbacks();
  526. // Emit a signal if a map changed.
  527. const uint32_t new_map_update_id = active_maps[i]->get_map_update_id();
  528. if (new_map_update_id != active_maps_update_id[i]) {
  529. emit_signal("map_changed", active_maps[i]->get_self());
  530. active_maps_update_id[i] = new_map_update_id;
  531. }
  532. }
  533. }
  534. #undef COMMAND_1
  535. #undef COMMAND_2
  536. #undef COMMAND_4