godot_navigation_server_3d.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. /**************************************************************************/
  2. /* godot_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 GODOT_NAVIGATION_SERVER_3D_H
  31. #define GODOT_NAVIGATION_SERVER_3D_H
  32. #include "../nav_agent.h"
  33. #include "../nav_link.h"
  34. #include "../nav_map.h"
  35. #include "../nav_obstacle.h"
  36. #include "../nav_region.h"
  37. #include "core/templates/local_vector.h"
  38. #include "core/templates/rid.h"
  39. #include "core/templates/rid_owner.h"
  40. #include "servers/navigation/navigation_path_query_parameters_3d.h"
  41. #include "servers/navigation/navigation_path_query_result_3d.h"
  42. #include "servers/navigation_server_3d.h"
  43. /// The commands are functions executed during the `sync` phase.
  44. #define MERGE_INTERNAL(A, B) A##B
  45. #define MERGE(A, B) MERGE_INTERNAL(A, B)
  46. #define COMMAND_1(F_NAME, T_0, D_0) \
  47. virtual void F_NAME(T_0 D_0) override; \
  48. void MERGE(_cmd_, F_NAME)(T_0 D_0)
  49. #define COMMAND_2(F_NAME, T_0, D_0, T_1, D_1) \
  50. virtual void F_NAME(T_0 D_0, T_1 D_1) override; \
  51. void MERGE(_cmd_, F_NAME)(T_0 D_0, T_1 D_1)
  52. class GodotNavigationServer3D;
  53. #ifndef _3D_DISABLED
  54. class NavMeshGenerator3D;
  55. #endif // _3D_DISABLED
  56. struct SetCommand {
  57. virtual ~SetCommand() {}
  58. virtual void exec(GodotNavigationServer3D *server) = 0;
  59. };
  60. class GodotNavigationServer3D : public NavigationServer3D {
  61. Mutex commands_mutex;
  62. /// Mutex used to make any operation threadsafe.
  63. Mutex operations_mutex;
  64. LocalVector<SetCommand *> commands;
  65. mutable RID_Owner<NavLink> link_owner;
  66. mutable RID_Owner<NavMap> map_owner;
  67. mutable RID_Owner<NavRegion> region_owner;
  68. mutable RID_Owner<NavAgent> agent_owner;
  69. mutable RID_Owner<NavObstacle> obstacle_owner;
  70. bool active = true;
  71. LocalVector<NavMap *> active_maps;
  72. LocalVector<uint32_t> active_maps_iteration_id;
  73. #ifndef _3D_DISABLED
  74. NavMeshGenerator3D *navmesh_generator_3d = nullptr;
  75. #endif // _3D_DISABLED
  76. // Performance Monitor
  77. int pm_region_count = 0;
  78. int pm_agent_count = 0;
  79. int pm_link_count = 0;
  80. int pm_polygon_count = 0;
  81. int pm_edge_count = 0;
  82. int pm_edge_merge_count = 0;
  83. int pm_edge_connection_count = 0;
  84. int pm_edge_free_count = 0;
  85. int pm_obstacle_count = 0;
  86. public:
  87. GodotNavigationServer3D();
  88. virtual ~GodotNavigationServer3D();
  89. void add_command(SetCommand *command);
  90. virtual TypedArray<RID> get_maps() const override;
  91. virtual RID map_create() override;
  92. COMMAND_2(map_set_active, RID, p_map, bool, p_active);
  93. virtual bool map_is_active(RID p_map) const override;
  94. COMMAND_2(map_set_up, RID, p_map, Vector3, p_up);
  95. virtual Vector3 map_get_up(RID p_map) const override;
  96. COMMAND_2(map_set_cell_size, RID, p_map, real_t, p_cell_size);
  97. virtual real_t map_get_cell_size(RID p_map) const override;
  98. COMMAND_2(map_set_cell_height, RID, p_map, real_t, p_cell_height);
  99. virtual real_t map_get_cell_height(RID p_map) const override;
  100. COMMAND_2(map_set_merge_rasterizer_cell_scale, RID, p_map, float, p_value);
  101. virtual float map_get_merge_rasterizer_cell_scale(RID p_map) const override;
  102. COMMAND_2(map_set_use_edge_connections, RID, p_map, bool, p_enabled);
  103. virtual bool map_get_use_edge_connections(RID p_map) const override;
  104. COMMAND_2(map_set_edge_connection_margin, RID, p_map, real_t, p_connection_margin);
  105. virtual real_t map_get_edge_connection_margin(RID p_map) const override;
  106. COMMAND_2(map_set_link_connection_radius, RID, p_map, real_t, p_connection_radius);
  107. virtual real_t map_get_link_connection_radius(RID p_map) const override;
  108. virtual Vector<Vector3> map_get_path(RID p_map, Vector3 p_origin, Vector3 p_destination, bool p_optimize, uint32_t p_navigation_layers = 1) override;
  109. virtual Vector3 map_get_closest_point_to_segment(RID p_map, const Vector3 &p_from, const Vector3 &p_to, const bool p_use_collision = false) const override;
  110. virtual Vector3 map_get_closest_point(RID p_map, const Vector3 &p_point) const override;
  111. virtual Vector3 map_get_closest_point_normal(RID p_map, const Vector3 &p_point) const override;
  112. virtual RID map_get_closest_point_owner(RID p_map, const Vector3 &p_point) const override;
  113. virtual TypedArray<RID> map_get_links(RID p_map) const override;
  114. virtual TypedArray<RID> map_get_regions(RID p_map) const override;
  115. virtual TypedArray<RID> map_get_agents(RID p_map) const override;
  116. virtual TypedArray<RID> map_get_obstacles(RID p_map) const override;
  117. virtual void map_force_update(RID p_map) override;
  118. virtual uint32_t map_get_iteration_id(RID p_map) const override;
  119. COMMAND_2(map_set_use_async_iterations, RID, p_map, bool, p_enabled);
  120. virtual bool map_get_use_async_iterations(RID p_map) const override;
  121. virtual Vector3 map_get_random_point(RID p_map, uint32_t p_navigation_layers, bool p_uniformly) const override;
  122. virtual RID region_create() override;
  123. COMMAND_2(region_set_enabled, RID, p_region, bool, p_enabled);
  124. virtual bool region_get_enabled(RID p_region) const override;
  125. COMMAND_2(region_set_use_edge_connections, RID, p_region, bool, p_enabled);
  126. virtual bool region_get_use_edge_connections(RID p_region) const override;
  127. COMMAND_2(region_set_enter_cost, RID, p_region, real_t, p_enter_cost);
  128. virtual real_t region_get_enter_cost(RID p_region) const override;
  129. COMMAND_2(region_set_travel_cost, RID, p_region, real_t, p_travel_cost);
  130. virtual real_t region_get_travel_cost(RID p_region) const override;
  131. COMMAND_2(region_set_owner_id, RID, p_region, ObjectID, p_owner_id);
  132. virtual ObjectID region_get_owner_id(RID p_region) const override;
  133. virtual bool region_owns_point(RID p_region, const Vector3 &p_point) const override;
  134. COMMAND_2(region_set_map, RID, p_region, RID, p_map);
  135. virtual RID region_get_map(RID p_region) const override;
  136. COMMAND_2(region_set_navigation_layers, RID, p_region, uint32_t, p_navigation_layers);
  137. virtual uint32_t region_get_navigation_layers(RID p_region) const override;
  138. COMMAND_2(region_set_transform, RID, p_region, Transform3D, p_transform);
  139. virtual Transform3D region_get_transform(RID p_region) const override;
  140. COMMAND_2(region_set_navigation_mesh, RID, p_region, Ref<NavigationMesh>, p_navigation_mesh);
  141. #ifndef DISABLE_DEPRECATED
  142. virtual void region_bake_navigation_mesh(Ref<NavigationMesh> p_navigation_mesh, Node *p_root_node) override;
  143. #endif // DISABLE_DEPRECATED
  144. virtual int region_get_connections_count(RID p_region) const override;
  145. virtual Vector3 region_get_connection_pathway_start(RID p_region, int p_connection_id) const override;
  146. virtual Vector3 region_get_connection_pathway_end(RID p_region, int p_connection_id) const override;
  147. virtual Vector3 region_get_closest_point_to_segment(RID p_region, const Vector3 &p_from, const Vector3 &p_to, bool p_use_collision = false) const override;
  148. virtual Vector3 region_get_closest_point(RID p_region, const Vector3 &p_point) const override;
  149. virtual Vector3 region_get_closest_point_normal(RID p_region, const Vector3 &p_point) const override;
  150. virtual Vector3 region_get_random_point(RID p_region, uint32_t p_navigation_layers, bool p_uniformly) const override;
  151. virtual AABB region_get_bounds(RID p_region) const override;
  152. virtual RID link_create() override;
  153. COMMAND_2(link_set_map, RID, p_link, RID, p_map);
  154. virtual RID link_get_map(RID p_link) const override;
  155. COMMAND_2(link_set_enabled, RID, p_link, bool, p_enabled);
  156. virtual bool link_get_enabled(RID p_link) const override;
  157. COMMAND_2(link_set_bidirectional, RID, p_link, bool, p_bidirectional);
  158. virtual bool link_is_bidirectional(RID p_link) const override;
  159. COMMAND_2(link_set_navigation_layers, RID, p_link, uint32_t, p_navigation_layers);
  160. virtual uint32_t link_get_navigation_layers(RID p_link) const override;
  161. COMMAND_2(link_set_start_position, RID, p_link, Vector3, p_position);
  162. virtual Vector3 link_get_start_position(RID p_link) const override;
  163. COMMAND_2(link_set_end_position, RID, p_link, Vector3, p_position);
  164. virtual Vector3 link_get_end_position(RID p_link) const override;
  165. COMMAND_2(link_set_enter_cost, RID, p_link, real_t, p_enter_cost);
  166. virtual real_t link_get_enter_cost(RID p_link) const override;
  167. COMMAND_2(link_set_travel_cost, RID, p_link, real_t, p_travel_cost);
  168. virtual real_t link_get_travel_cost(RID p_link) const override;
  169. COMMAND_2(link_set_owner_id, RID, p_link, ObjectID, p_owner_id);
  170. virtual ObjectID link_get_owner_id(RID p_link) const override;
  171. virtual RID agent_create() override;
  172. COMMAND_2(agent_set_avoidance_enabled, RID, p_agent, bool, p_enabled);
  173. virtual bool agent_get_avoidance_enabled(RID p_agent) const override;
  174. COMMAND_2(agent_set_use_3d_avoidance, RID, p_agent, bool, p_enabled);
  175. virtual bool agent_get_use_3d_avoidance(RID p_agent) const override;
  176. COMMAND_2(agent_set_map, RID, p_agent, RID, p_map);
  177. virtual RID agent_get_map(RID p_agent) const override;
  178. COMMAND_2(agent_set_paused, RID, p_agent, bool, p_paused);
  179. virtual bool agent_get_paused(RID p_agent) const override;
  180. COMMAND_2(agent_set_neighbor_distance, RID, p_agent, real_t, p_distance);
  181. virtual real_t agent_get_neighbor_distance(RID p_agent) const override;
  182. COMMAND_2(agent_set_max_neighbors, RID, p_agent, int, p_count);
  183. virtual int agent_get_max_neighbors(RID p_agent) const override;
  184. COMMAND_2(agent_set_time_horizon_agents, RID, p_agent, real_t, p_time_horizon);
  185. virtual real_t agent_get_time_horizon_agents(RID p_agent) const override;
  186. COMMAND_2(agent_set_time_horizon_obstacles, RID, p_agent, real_t, p_time_horizon);
  187. virtual real_t agent_get_time_horizon_obstacles(RID p_agent) const override;
  188. COMMAND_2(agent_set_radius, RID, p_agent, real_t, p_radius);
  189. virtual real_t agent_get_radius(RID p_agent) const override;
  190. COMMAND_2(agent_set_height, RID, p_agent, real_t, p_height);
  191. virtual real_t agent_get_height(RID p_agent) const override;
  192. COMMAND_2(agent_set_max_speed, RID, p_agent, real_t, p_max_speed);
  193. virtual real_t agent_get_max_speed(RID p_agent) const override;
  194. COMMAND_2(agent_set_velocity, RID, p_agent, Vector3, p_velocity);
  195. virtual Vector3 agent_get_velocity(RID p_agent) const override;
  196. COMMAND_2(agent_set_velocity_forced, RID, p_agent, Vector3, p_velocity);
  197. COMMAND_2(agent_set_position, RID, p_agent, Vector3, p_position);
  198. virtual Vector3 agent_get_position(RID p_agent) const override;
  199. virtual bool agent_is_map_changed(RID p_agent) const override;
  200. COMMAND_2(agent_set_avoidance_callback, RID, p_agent, Callable, p_callback);
  201. virtual bool agent_has_avoidance_callback(RID p_agent) const override;
  202. COMMAND_2(agent_set_avoidance_layers, RID, p_agent, uint32_t, p_layers);
  203. virtual uint32_t agent_get_avoidance_layers(RID p_agent) const override;
  204. COMMAND_2(agent_set_avoidance_mask, RID, p_agent, uint32_t, p_mask);
  205. virtual uint32_t agent_get_avoidance_mask(RID p_agent) const override;
  206. COMMAND_2(agent_set_avoidance_priority, RID, p_agent, real_t, p_priority);
  207. virtual real_t agent_get_avoidance_priority(RID p_agent) const override;
  208. virtual RID obstacle_create() override;
  209. COMMAND_2(obstacle_set_avoidance_enabled, RID, p_obstacle, bool, p_enabled);
  210. virtual bool obstacle_get_avoidance_enabled(RID p_obstacle) const override;
  211. COMMAND_2(obstacle_set_use_3d_avoidance, RID, p_obstacle, bool, p_enabled);
  212. virtual bool obstacle_get_use_3d_avoidance(RID p_obstacle) const override;
  213. COMMAND_2(obstacle_set_map, RID, p_obstacle, RID, p_map);
  214. virtual RID obstacle_get_map(RID p_obstacle) const override;
  215. COMMAND_2(obstacle_set_paused, RID, p_obstacle, bool, p_paused);
  216. virtual bool obstacle_get_paused(RID p_obstacle) const override;
  217. COMMAND_2(obstacle_set_radius, RID, p_obstacle, real_t, p_radius);
  218. virtual real_t obstacle_get_radius(RID p_obstacle) const override;
  219. COMMAND_2(obstacle_set_velocity, RID, p_obstacle, Vector3, p_velocity);
  220. virtual Vector3 obstacle_get_velocity(RID p_obstacle) const override;
  221. COMMAND_2(obstacle_set_position, RID, p_obstacle, Vector3, p_position);
  222. virtual Vector3 obstacle_get_position(RID p_obstacle) const override;
  223. COMMAND_2(obstacle_set_height, RID, p_obstacle, real_t, p_height);
  224. virtual real_t obstacle_get_height(RID p_obstacle) const override;
  225. virtual void obstacle_set_vertices(RID p_obstacle, const Vector<Vector3> &p_vertices) override;
  226. virtual Vector<Vector3> obstacle_get_vertices(RID p_obstacle) const override;
  227. COMMAND_2(obstacle_set_avoidance_layers, RID, p_obstacle, uint32_t, p_layers);
  228. virtual uint32_t obstacle_get_avoidance_layers(RID p_obstacle) const override;
  229. virtual void parse_source_geometry_data(const Ref<NavigationMesh> &p_navigation_mesh, const Ref<NavigationMeshSourceGeometryData3D> &p_source_geometry_data, Node *p_root_node, const Callable &p_callback = Callable()) override;
  230. virtual void bake_from_source_geometry_data(const Ref<NavigationMesh> &p_navigation_mesh, const Ref<NavigationMeshSourceGeometryData3D> &p_source_geometry_data, const Callable &p_callback = Callable()) override;
  231. virtual void bake_from_source_geometry_data_async(const Ref<NavigationMesh> &p_navigation_mesh, const Ref<NavigationMeshSourceGeometryData3D> &p_source_geometry_data, const Callable &p_callback = Callable()) override;
  232. virtual bool is_baking_navigation_mesh(Ref<NavigationMesh> p_navigation_mesh) const override;
  233. virtual RID source_geometry_parser_create() override;
  234. virtual void source_geometry_parser_set_callback(RID p_parser, const Callable &p_callback) override;
  235. virtual Vector<Vector3> simplify_path(const Vector<Vector3> &p_path, real_t p_epsilon) override;
  236. public:
  237. COMMAND_1(free, RID, p_object);
  238. virtual void set_active(bool p_active) override;
  239. void flush_queries();
  240. virtual void process(real_t p_delta_time) override;
  241. virtual void init() override;
  242. virtual void sync() override;
  243. virtual void finish() override;
  244. virtual void query_path(const Ref<NavigationPathQueryParameters3D> &p_query_parameters, Ref<NavigationPathQueryResult3D> p_query_result, const Callable &p_callback = Callable()) override;
  245. int get_process_info(ProcessInfo p_info) const override;
  246. private:
  247. void internal_free_agent(RID p_object);
  248. void internal_free_obstacle(RID p_object);
  249. };
  250. #undef COMMAND_1
  251. #undef COMMAND_2
  252. #endif // GODOT_NAVIGATION_SERVER_3D_H