gd_navigation_server.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  1. /*************************************************************************/
  2. /* gd_navigation_server.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
  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 "gd_navigation_server.h"
  31. #include "core/os/mutex.h"
  32. #ifndef _3D_DISABLED
  33. #include "navigation_mesh_generator.h"
  34. #endif
  35. /**
  36. @author AndreaCatania
  37. */
  38. /// Creates a struct for each function and a function that once called creates
  39. /// an instance of that struct with the submitted parameters.
  40. /// Then, that struct is stored in an array; the `sync` function consume that array.
  41. #define COMMAND_1(F_NAME, T_0, D_0) \
  42. struct MERGE(F_NAME, _command) : public SetCommand { \
  43. T_0 d_0; \
  44. MERGE(F_NAME, _command) \
  45. (T_0 p_d_0) : \
  46. d_0(p_d_0) {} \
  47. virtual void exec(GdNavigationServer *server) { \
  48. server->MERGE(_cmd_, F_NAME)(d_0); \
  49. } \
  50. }; \
  51. void GdNavigationServer::F_NAME(T_0 D_0) const { \
  52. auto cmd = memnew(MERGE(F_NAME, _command)( \
  53. D_0)); \
  54. add_command(cmd); \
  55. } \
  56. void GdNavigationServer::MERGE(_cmd_, F_NAME)(T_0 D_0)
  57. #define COMMAND_2(F_NAME, T_0, D_0, T_1, D_1) \
  58. struct MERGE(F_NAME, _command) : public SetCommand { \
  59. T_0 d_0; \
  60. T_1 d_1; \
  61. MERGE(F_NAME, _command) \
  62. ( \
  63. T_0 p_d_0, \
  64. T_1 p_d_1) : \
  65. d_0(p_d_0), \
  66. d_1(p_d_1) {} \
  67. virtual void exec(GdNavigationServer *server) { \
  68. server->MERGE(_cmd_, F_NAME)(d_0, d_1); \
  69. } \
  70. }; \
  71. void GdNavigationServer::F_NAME(T_0 D_0, T_1 D_1) const { \
  72. auto cmd = memnew(MERGE(F_NAME, _command)( \
  73. D_0, \
  74. D_1)); \
  75. add_command(cmd); \
  76. } \
  77. void GdNavigationServer::MERGE(_cmd_, F_NAME)(T_0 D_0, T_1 D_1)
  78. #define COMMAND_4(F_NAME, T_0, D_0, T_1, D_1, T_2, D_2, T_3, D_3) \
  79. struct MERGE(F_NAME, _command) : public SetCommand { \
  80. T_0 d_0; \
  81. T_1 d_1; \
  82. T_2 d_2; \
  83. T_3 d_3; \
  84. MERGE(F_NAME, _command) \
  85. ( \
  86. T_0 p_d_0, \
  87. T_1 p_d_1, \
  88. T_2 p_d_2, \
  89. T_3 p_d_3) : \
  90. d_0(p_d_0), \
  91. d_1(p_d_1), \
  92. d_2(p_d_2), \
  93. d_3(p_d_3) {} \
  94. virtual void exec(GdNavigationServer *server) { \
  95. server->MERGE(_cmd_, F_NAME)(d_0, d_1, d_2, d_3); \
  96. } \
  97. }; \
  98. void GdNavigationServer::F_NAME(T_0 D_0, T_1 D_1, T_2 D_2, T_3 D_3) const { \
  99. auto cmd = memnew(MERGE(F_NAME, _command)( \
  100. D_0, \
  101. D_1, \
  102. D_2, \
  103. D_3)); \
  104. add_command(cmd); \
  105. } \
  106. void GdNavigationServer::MERGE(_cmd_, F_NAME)(T_0 D_0, T_1 D_1, T_2 D_2, T_3 D_3)
  107. GdNavigationServer::GdNavigationServer() :
  108. NavigationServer3D() {
  109. }
  110. GdNavigationServer::~GdNavigationServer() {
  111. flush_queries();
  112. }
  113. void GdNavigationServer::add_command(SetCommand *command) const {
  114. auto mut_this = const_cast<GdNavigationServer *>(this);
  115. {
  116. MutexLock lock(commands_mutex);
  117. mut_this->commands.push_back(command);
  118. }
  119. }
  120. RID GdNavigationServer::map_create() const {
  121. auto mut_this = const_cast<GdNavigationServer *>(this);
  122. MutexLock lock(mut_this->operations_mutex);
  123. NavMap *space = memnew(NavMap);
  124. RID rid = map_owner.make_rid(space);
  125. space->set_self(rid);
  126. return rid;
  127. }
  128. COMMAND_2(map_set_active, RID, p_map, bool, p_active) {
  129. NavMap *map = map_owner.getornull(p_map);
  130. ERR_FAIL_COND(map == nullptr);
  131. if (p_active) {
  132. if (!map_is_active(p_map)) {
  133. active_maps.push_back(map);
  134. }
  135. } else {
  136. active_maps.erase(map);
  137. }
  138. }
  139. bool GdNavigationServer::map_is_active(RID p_map) const {
  140. NavMap *map = map_owner.getornull(p_map);
  141. ERR_FAIL_COND_V(map == nullptr, false);
  142. return active_maps.find(map) >= 0;
  143. }
  144. COMMAND_2(map_set_up, RID, p_map, Vector3, p_up) {
  145. NavMap *map = map_owner.getornull(p_map);
  146. ERR_FAIL_COND(map == nullptr);
  147. map->set_up(p_up);
  148. }
  149. Vector3 GdNavigationServer::map_get_up(RID p_map) const {
  150. const NavMap *map = map_owner.getornull(p_map);
  151. ERR_FAIL_COND_V(map == nullptr, Vector3());
  152. return map->get_up();
  153. }
  154. COMMAND_2(map_set_cell_size, RID, p_map, real_t, p_cell_size) {
  155. NavMap *map = map_owner.getornull(p_map);
  156. ERR_FAIL_COND(map == nullptr);
  157. map->set_cell_size(p_cell_size);
  158. }
  159. real_t GdNavigationServer::map_get_cell_size(RID p_map) const {
  160. const NavMap *map = map_owner.getornull(p_map);
  161. ERR_FAIL_COND_V(map == nullptr, 0);
  162. return map->get_cell_size();
  163. }
  164. COMMAND_2(map_set_edge_connection_margin, RID, p_map, real_t, p_connection_margin) {
  165. NavMap *map = map_owner.getornull(p_map);
  166. ERR_FAIL_COND(map == nullptr);
  167. map->set_edge_connection_margin(p_connection_margin);
  168. }
  169. real_t GdNavigationServer::map_get_edge_connection_margin(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_edge_connection_margin();
  173. }
  174. Vector<Vector3> GdNavigationServer::map_get_path(RID p_map, Vector3 p_origin, Vector3 p_destination, bool p_optimize) const {
  175. const NavMap *map = map_owner.getornull(p_map);
  176. ERR_FAIL_COND_V(map == nullptr, Vector<Vector3>());
  177. return map->get_path(p_origin, p_destination, p_optimize);
  178. }
  179. Vector3 GdNavigationServer::map_get_closest_point_to_segment(RID p_map, const Vector3 &p_from, const Vector3 &p_to, const bool p_use_collision) const {
  180. const NavMap *map = map_owner.getornull(p_map);
  181. ERR_FAIL_COND_V(map == nullptr, Vector3());
  182. return map->get_closest_point_to_segment(p_from, p_to, p_use_collision);
  183. }
  184. Vector3 GdNavigationServer::map_get_closest_point(RID p_map, const Vector3 &p_point) const {
  185. const NavMap *map = map_owner.getornull(p_map);
  186. ERR_FAIL_COND_V(map == nullptr, Vector3());
  187. return map->get_closest_point(p_point);
  188. }
  189. Vector3 GdNavigationServer::map_get_closest_point_normal(RID p_map, const Vector3 &p_point) const {
  190. const NavMap *map = map_owner.getornull(p_map);
  191. ERR_FAIL_COND_V(map == nullptr, Vector3());
  192. return map->get_closest_point_normal(p_point);
  193. }
  194. RID GdNavigationServer::map_get_closest_point_owner(RID p_map, const Vector3 &p_point) const {
  195. const NavMap *map = map_owner.getornull(p_map);
  196. ERR_FAIL_COND_V(map == nullptr, RID());
  197. return map->get_closest_point_owner(p_point);
  198. }
  199. RID GdNavigationServer::region_create() const {
  200. auto mut_this = const_cast<GdNavigationServer *>(this);
  201. MutexLock lock(mut_this->operations_mutex);
  202. NavRegion *reg = memnew(NavRegion);
  203. RID rid = region_owner.make_rid(reg);
  204. reg->set_self(rid);
  205. return rid;
  206. }
  207. COMMAND_2(region_set_map, RID, p_region, RID, p_map) {
  208. NavRegion *region = region_owner.getornull(p_region);
  209. ERR_FAIL_COND(region == nullptr);
  210. if (region->get_map() != nullptr) {
  211. if (region->get_map()->get_self() == p_map) {
  212. return; // Pointless
  213. }
  214. region->get_map()->remove_region(region);
  215. region->set_map(nullptr);
  216. }
  217. if (p_map.is_valid()) {
  218. NavMap *map = map_owner.getornull(p_map);
  219. ERR_FAIL_COND(map == nullptr);
  220. map->add_region(region);
  221. region->set_map(map);
  222. }
  223. }
  224. COMMAND_2(region_set_transform, RID, p_region, Transform, p_transform) {
  225. NavRegion *region = region_owner.getornull(p_region);
  226. ERR_FAIL_COND(region == nullptr);
  227. region->set_transform(p_transform);
  228. }
  229. COMMAND_2(region_set_navmesh, RID, p_region, Ref<NavigationMesh>, p_nav_mesh) {
  230. NavRegion *region = region_owner.getornull(p_region);
  231. ERR_FAIL_COND(region == nullptr);
  232. region->set_mesh(p_nav_mesh);
  233. }
  234. void GdNavigationServer::region_bake_navmesh(Ref<NavigationMesh> r_mesh, Node *p_node) const {
  235. ERR_FAIL_COND(r_mesh.is_null());
  236. ERR_FAIL_COND(p_node == nullptr);
  237. #ifndef _3D_DISABLED
  238. NavigationMeshGenerator::get_singleton()->clear(r_mesh);
  239. NavigationMeshGenerator::get_singleton()->bake(r_mesh, p_node);
  240. #endif
  241. }
  242. RID GdNavigationServer::agent_create() const {
  243. auto mut_this = const_cast<GdNavigationServer *>(this);
  244. MutexLock lock(mut_this->operations_mutex);
  245. RvoAgent *agent = memnew(RvoAgent());
  246. RID rid = agent_owner.make_rid(agent);
  247. agent->set_self(rid);
  248. return rid;
  249. }
  250. COMMAND_2(agent_set_map, RID, p_agent, RID, p_map) {
  251. RvoAgent *agent = agent_owner.getornull(p_agent);
  252. ERR_FAIL_COND(agent == nullptr);
  253. if (agent->get_map()) {
  254. if (agent->get_map()->get_self() == p_map) {
  255. return; // Pointless
  256. }
  257. agent->get_map()->remove_agent(agent);
  258. }
  259. agent->set_map(nullptr);
  260. if (p_map.is_valid()) {
  261. NavMap *map = map_owner.getornull(p_map);
  262. ERR_FAIL_COND(map == nullptr);
  263. agent->set_map(map);
  264. map->add_agent(agent);
  265. if (agent->has_callback()) {
  266. map->set_agent_as_controlled(agent);
  267. }
  268. }
  269. }
  270. COMMAND_2(agent_set_neighbor_dist, RID, p_agent, real_t, p_dist) {
  271. RvoAgent *agent = agent_owner.getornull(p_agent);
  272. ERR_FAIL_COND(agent == nullptr);
  273. agent->get_agent()->neighborDist_ = p_dist;
  274. }
  275. COMMAND_2(agent_set_max_neighbors, RID, p_agent, int, p_count) {
  276. RvoAgent *agent = agent_owner.getornull(p_agent);
  277. ERR_FAIL_COND(agent == nullptr);
  278. agent->get_agent()->maxNeighbors_ = p_count;
  279. }
  280. COMMAND_2(agent_set_time_horizon, RID, p_agent, real_t, p_time) {
  281. RvoAgent *agent = agent_owner.getornull(p_agent);
  282. ERR_FAIL_COND(agent == nullptr);
  283. agent->get_agent()->timeHorizon_ = p_time;
  284. }
  285. COMMAND_2(agent_set_radius, RID, p_agent, real_t, p_radius) {
  286. RvoAgent *agent = agent_owner.getornull(p_agent);
  287. ERR_FAIL_COND(agent == nullptr);
  288. agent->get_agent()->radius_ = p_radius;
  289. }
  290. COMMAND_2(agent_set_max_speed, RID, p_agent, real_t, p_max_speed) {
  291. RvoAgent *agent = agent_owner.getornull(p_agent);
  292. ERR_FAIL_COND(agent == nullptr);
  293. agent->get_agent()->maxSpeed_ = p_max_speed;
  294. }
  295. COMMAND_2(agent_set_velocity, RID, p_agent, Vector3, p_velocity) {
  296. RvoAgent *agent = agent_owner.getornull(p_agent);
  297. ERR_FAIL_COND(agent == nullptr);
  298. agent->get_agent()->velocity_ = RVO::Vector3(p_velocity.x, p_velocity.y, p_velocity.z);
  299. }
  300. COMMAND_2(agent_set_target_velocity, RID, p_agent, Vector3, p_velocity) {
  301. RvoAgent *agent = agent_owner.getornull(p_agent);
  302. ERR_FAIL_COND(agent == nullptr);
  303. agent->get_agent()->prefVelocity_ = RVO::Vector3(p_velocity.x, p_velocity.y, p_velocity.z);
  304. }
  305. COMMAND_2(agent_set_position, RID, p_agent, Vector3, p_position) {
  306. RvoAgent *agent = agent_owner.getornull(p_agent);
  307. ERR_FAIL_COND(agent == nullptr);
  308. agent->get_agent()->position_ = RVO::Vector3(p_position.x, p_position.y, p_position.z);
  309. }
  310. COMMAND_2(agent_set_ignore_y, RID, p_agent, bool, p_ignore) {
  311. RvoAgent *agent = agent_owner.getornull(p_agent);
  312. ERR_FAIL_COND(agent == nullptr);
  313. agent->get_agent()->ignore_y_ = p_ignore;
  314. }
  315. bool GdNavigationServer::agent_is_map_changed(RID p_agent) const {
  316. RvoAgent *agent = agent_owner.getornull(p_agent);
  317. ERR_FAIL_COND_V(agent == nullptr, false);
  318. return agent->is_map_changed();
  319. }
  320. COMMAND_4(agent_set_callback, RID, p_agent, Object *, p_receiver, StringName, p_method, Variant, p_udata) {
  321. RvoAgent *agent = agent_owner.getornull(p_agent);
  322. ERR_FAIL_COND(agent == nullptr);
  323. agent->set_callback(p_receiver == nullptr ? ObjectID() : p_receiver->get_instance_id(), p_method, p_udata);
  324. if (agent->get_map()) {
  325. if (p_receiver == nullptr) {
  326. agent->get_map()->remove_agent_as_controlled(agent);
  327. } else {
  328. agent->get_map()->set_agent_as_controlled(agent);
  329. }
  330. }
  331. }
  332. COMMAND_1(free, RID, p_object) {
  333. if (map_owner.owns(p_object)) {
  334. NavMap *map = map_owner.getornull(p_object);
  335. // Removes any assigned region
  336. std::vector<NavRegion *> regions = map->get_regions();
  337. for (size_t i(0); i < regions.size(); i++) {
  338. map->remove_region(regions[i]);
  339. regions[i]->set_map(nullptr);
  340. }
  341. // Remove any assigned agent
  342. std::vector<RvoAgent *> agents = map->get_agents();
  343. for (size_t i(0); i < agents.size(); i++) {
  344. map->remove_agent(agents[i]);
  345. agents[i]->set_map(nullptr);
  346. }
  347. active_maps.erase(map);
  348. map_owner.free(p_object);
  349. memdelete(map);
  350. } else if (region_owner.owns(p_object)) {
  351. NavRegion *region = region_owner.getornull(p_object);
  352. // Removes this region from the map if assigned
  353. if (region->get_map() != nullptr) {
  354. region->get_map()->remove_region(region);
  355. region->set_map(nullptr);
  356. }
  357. region_owner.free(p_object);
  358. memdelete(region);
  359. } else if (agent_owner.owns(p_object)) {
  360. RvoAgent *agent = agent_owner.getornull(p_object);
  361. // Removes this agent from the map if assigned
  362. if (agent->get_map() != nullptr) {
  363. agent->get_map()->remove_agent(agent);
  364. agent->set_map(nullptr);
  365. }
  366. agent_owner.free(p_object);
  367. memdelete(agent);
  368. } else {
  369. ERR_FAIL_COND("Invalid ID.");
  370. }
  371. }
  372. void GdNavigationServer::set_active(bool p_active) const {
  373. auto mut_this = const_cast<GdNavigationServer *>(this);
  374. MutexLock lock(mut_this->operations_mutex);
  375. mut_this->active = p_active;
  376. }
  377. void GdNavigationServer::flush_queries() {
  378. // In c++ we can't be sure that this is performed in the main thread
  379. // even with mutable functions.
  380. MutexLock lock(commands_mutex);
  381. MutexLock lock2(operations_mutex);
  382. for (size_t i(0); i < commands.size(); i++) {
  383. commands[i]->exec(this);
  384. memdelete(commands[i]);
  385. }
  386. commands.clear();
  387. }
  388. void GdNavigationServer::process(real_t p_delta_time) {
  389. flush_queries();
  390. if (!active) {
  391. return;
  392. }
  393. // In c++ we can't be sure that this is performed in the main thread
  394. // even with mutable functions.
  395. MutexLock lock(operations_mutex);
  396. for (int i(0); i < active_maps.size(); i++) {
  397. active_maps[i]->sync();
  398. active_maps[i]->step(p_delta_time);
  399. active_maps[i]->dispatch_callbacks();
  400. }
  401. }
  402. #undef COMMAND_1
  403. #undef COMMAND_2
  404. #undef COMMAND_4