godot_navigation_server_2d.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. /**************************************************************************/
  2. /* godot_navigation_server_2d.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. #pragma once
  31. #include "../nav_agent_2d.h"
  32. #include "../nav_link_2d.h"
  33. #include "../nav_map_2d.h"
  34. #include "../nav_obstacle_2d.h"
  35. #include "../nav_region_2d.h"
  36. #include "core/templates/local_vector.h"
  37. #include "core/templates/rid.h"
  38. #include "core/templates/rid_owner.h"
  39. #include "servers/navigation/navigation_path_query_parameters_2d.h"
  40. #include "servers/navigation/navigation_path_query_result_2d.h"
  41. #include "servers/navigation_server_2d.h"
  42. /// The commands are functions executed during the `sync` phase.
  43. #define MERGE_INTERNAL(A, B) A##B
  44. #define MERGE(A, B) MERGE_INTERNAL(A, B)
  45. #define COMMAND_1(F_NAME, T_0, D_0) \
  46. virtual void F_NAME(T_0 D_0) override; \
  47. void MERGE(_cmd_, F_NAME)(T_0 D_0)
  48. #define COMMAND_2(F_NAME, T_0, D_0, T_1, D_1) \
  49. virtual void F_NAME(T_0 D_0, T_1 D_1) override; \
  50. void MERGE(_cmd_, F_NAME)(T_0 D_0, T_1 D_1)
  51. class GodotNavigationServer2D;
  52. #ifdef CLIPPER2_ENABLED
  53. class NavMeshGenerator2D;
  54. #endif // CLIPPER2_ENABLED
  55. struct SetCommand2D {
  56. virtual ~SetCommand2D() {}
  57. virtual void exec(GodotNavigationServer2D *p_server) = 0;
  58. };
  59. // This server exposes the `NavigationServer3D` features in the 2D world.
  60. class GodotNavigationServer2D : public NavigationServer2D {
  61. GDCLASS(GodotNavigationServer2D, NavigationServer2D);
  62. Mutex commands_mutex;
  63. /// Mutex used to make any operation threadsafe.
  64. Mutex operations_mutex;
  65. LocalVector<SetCommand2D *> commands;
  66. mutable RID_Owner<NavLink2D> link_owner;
  67. mutable RID_Owner<NavMap2D> map_owner;
  68. mutable RID_Owner<NavRegion2D> region_owner;
  69. mutable RID_Owner<NavAgent2D> agent_owner;
  70. mutable RID_Owner<NavObstacle2D> obstacle_owner;
  71. bool active = true;
  72. LocalVector<NavMap2D *> active_maps;
  73. #ifdef CLIPPER2_ENABLED
  74. NavMeshGenerator2D *navmesh_generator_2d = nullptr;
  75. #endif // CLIPPER2_ENABLED
  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. GodotNavigationServer2D();
  88. virtual ~GodotNavigationServer2D();
  89. void add_command(SetCommand2D *p_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_cell_size, RID, p_map, real_t, p_cell_size);
  95. virtual real_t map_get_cell_size(RID p_map) const override;
  96. COMMAND_2(map_set_merge_rasterizer_cell_scale, RID, p_map, float, p_value);
  97. virtual float map_get_merge_rasterizer_cell_scale(RID p_map) const override;
  98. COMMAND_2(map_set_use_edge_connections, RID, p_map, bool, p_enabled);
  99. virtual bool map_get_use_edge_connections(RID p_map) const override;
  100. COMMAND_2(map_set_edge_connection_margin, RID, p_map, real_t, p_connection_margin);
  101. virtual real_t map_get_edge_connection_margin(RID p_map) const override;
  102. COMMAND_2(map_set_link_connection_radius, RID, p_map, real_t, p_connection_radius);
  103. virtual real_t map_get_link_connection_radius(RID p_map) const override;
  104. virtual Vector<Vector2> map_get_path(RID p_map, Vector2 p_origin, Vector2 p_destination, bool p_optimize, uint32_t p_navigation_layers = 1) override;
  105. virtual Vector2 map_get_closest_point(RID p_map, const Vector2 &p_point) const override;
  106. virtual RID map_get_closest_point_owner(RID p_map, const Vector2 &p_point) const override;
  107. virtual TypedArray<RID> map_get_links(RID p_map) const override;
  108. virtual TypedArray<RID> map_get_regions(RID p_map) const override;
  109. virtual TypedArray<RID> map_get_agents(RID p_map) const override;
  110. virtual TypedArray<RID> map_get_obstacles(RID p_map) const override;
  111. virtual void map_force_update(RID p_map) override;
  112. virtual uint32_t map_get_iteration_id(RID p_map) const override;
  113. COMMAND_2(map_set_use_async_iterations, RID, p_map, bool, p_enabled);
  114. virtual bool map_get_use_async_iterations(RID p_map) const override;
  115. virtual Vector2 map_get_random_point(RID p_map, uint32_t p_navigation_layers, bool p_uniformly) const override;
  116. virtual RID region_create() override;
  117. virtual uint32_t region_get_iteration_id(RID p_region) const override;
  118. COMMAND_2(region_set_use_async_iterations, RID, p_region, bool, p_enabled);
  119. virtual bool region_get_use_async_iterations(RID p_region) const override;
  120. COMMAND_2(region_set_enabled, RID, p_region, bool, p_enabled);
  121. virtual bool region_get_enabled(RID p_region) const override;
  122. COMMAND_2(region_set_use_edge_connections, RID, p_region, bool, p_enabled);
  123. virtual bool region_get_use_edge_connections(RID p_region) const override;
  124. COMMAND_2(region_set_enter_cost, RID, p_region, real_t, p_enter_cost);
  125. virtual real_t region_get_enter_cost(RID p_region) const override;
  126. COMMAND_2(region_set_travel_cost, RID, p_region, real_t, p_travel_cost);
  127. virtual real_t region_get_travel_cost(RID p_region) const override;
  128. COMMAND_2(region_set_owner_id, RID, p_region, ObjectID, p_owner_id);
  129. virtual ObjectID region_get_owner_id(RID p_region) const override;
  130. virtual bool region_owns_point(RID p_region, const Vector2 &p_point) const override;
  131. COMMAND_2(region_set_map, RID, p_region, RID, p_map);
  132. virtual RID region_get_map(RID p_region) const override;
  133. COMMAND_2(region_set_navigation_layers, RID, p_region, uint32_t, p_navigation_layers);
  134. virtual uint32_t region_get_navigation_layers(RID p_region) const override;
  135. COMMAND_2(region_set_transform, RID, p_region, Transform2D, p_transform);
  136. virtual Transform2D region_get_transform(RID p_region) const override;
  137. COMMAND_2(region_set_navigation_polygon, RID, p_region, Ref<NavigationPolygon>, p_navigation_polygon);
  138. virtual int region_get_connections_count(RID p_region) const override;
  139. virtual Vector2 region_get_connection_pathway_start(RID p_region, int p_connection_id) const override;
  140. virtual Vector2 region_get_connection_pathway_end(RID p_region, int p_connection_id) const override;
  141. virtual Vector2 region_get_closest_point(RID p_region, const Vector2 &p_point) const override;
  142. virtual Vector2 region_get_random_point(RID p_region, uint32_t p_navigation_layers, bool p_uniformly) const override;
  143. virtual Rect2 region_get_bounds(RID p_region) const override;
  144. virtual RID link_create() override;
  145. virtual uint32_t link_get_iteration_id(RID p_link) const override;
  146. /// Set the map of this link.
  147. COMMAND_2(link_set_map, RID, p_link, RID, p_map);
  148. virtual RID link_get_map(RID p_link) const override;
  149. COMMAND_2(link_set_enabled, RID, p_link, bool, p_enabled);
  150. virtual bool link_get_enabled(RID p_link) const override;
  151. /// Set whether this link travels in both directions.
  152. COMMAND_2(link_set_bidirectional, RID, p_link, bool, p_bidirectional);
  153. virtual bool link_is_bidirectional(RID p_link) const override;
  154. /// Set the link's layers.
  155. COMMAND_2(link_set_navigation_layers, RID, p_link, uint32_t, p_navigation_layers);
  156. virtual uint32_t link_get_navigation_layers(RID p_link) const override;
  157. /// Set the start position of the link.
  158. COMMAND_2(link_set_start_position, RID, p_link, Vector2, p_position);
  159. virtual Vector2 link_get_start_position(RID p_link) const override;
  160. /// Set the end position of the link.
  161. COMMAND_2(link_set_end_position, RID, p_link, Vector2, p_position);
  162. virtual Vector2 link_get_end_position(RID p_link) const override;
  163. /// Set the enter cost of the link.
  164. COMMAND_2(link_set_enter_cost, RID, p_link, real_t, p_enter_cost);
  165. virtual real_t link_get_enter_cost(RID p_link) const override;
  166. /// Set the travel cost of the link.
  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. /// Set the node which manages this link.
  170. COMMAND_2(link_set_owner_id, RID, p_link, ObjectID, p_owner_id);
  171. virtual ObjectID link_get_owner_id(RID p_link) const override;
  172. /// Creates the agent.
  173. virtual RID agent_create() override;
  174. /// Put the agent in the map.
  175. COMMAND_2(agent_set_map, RID, p_agent, RID, p_map);
  176. virtual RID agent_get_map(RID p_agent) const override;
  177. COMMAND_2(agent_set_paused, RID, p_agent, bool, p_paused);
  178. virtual bool agent_get_paused(RID p_agent) const override;
  179. COMMAND_2(agent_set_avoidance_enabled, RID, p_agent, bool, p_enabled);
  180. virtual bool agent_get_avoidance_enabled(RID p_agent) const override;
  181. /// The maximum distance (center point to
  182. /// center point) to other agents this agent
  183. /// takes into account in the navigation. The
  184. /// larger this number, the longer the running
  185. /// time of the simulation. If the number is too
  186. /// low, the simulation will not be safe.
  187. /// Must be non-negative.
  188. COMMAND_2(agent_set_neighbor_distance, RID, p_agent, real_t, p_distance);
  189. virtual real_t agent_get_neighbor_distance(RID p_agent) const override;
  190. /// The maximum number of other agents this
  191. /// agent takes into account in the navigation.
  192. /// The larger this number, the longer the
  193. /// running time of the simulation. If the
  194. /// number is too low, the simulation will not
  195. /// be safe.
  196. COMMAND_2(agent_set_max_neighbors, RID, p_agent, int, p_count);
  197. virtual int agent_get_max_neighbors(RID p_agent) const override;
  198. /// The minimal amount of time for which this
  199. /// agent's velocities that are computed by the
  200. /// simulation are safe with respect to other
  201. /// agents. The larger this number, the sooner
  202. /// this agent will respond to the presence of
  203. /// other agents, but the less freedom this
  204. /// agent has in choosing its velocities.
  205. /// Must be positive.
  206. COMMAND_2(agent_set_time_horizon_agents, RID, p_agent, real_t, p_time_horizon);
  207. virtual real_t agent_get_time_horizon_agents(RID p_agent) const override;
  208. COMMAND_2(agent_set_time_horizon_obstacles, RID, p_agent, real_t, p_time_horizon);
  209. virtual real_t agent_get_time_horizon_obstacles(RID p_agent) const override;
  210. /// The radius of this agent.
  211. /// Must be non-negative.
  212. COMMAND_2(agent_set_radius, RID, p_agent, real_t, p_radius);
  213. virtual real_t agent_get_radius(RID p_agent) const override;
  214. /// The maximum speed of this agent.
  215. /// Must be non-negative.
  216. COMMAND_2(agent_set_max_speed, RID, p_agent, real_t, p_max_speed);
  217. virtual real_t agent_get_max_speed(RID p_agent) const override;
  218. /// forces and agent velocity change in the avoidance simulation, adds simulation instability if done recklessly
  219. COMMAND_2(agent_set_velocity_forced, RID, p_agent, Vector2, p_velocity);
  220. /// The wanted velocity for the agent as a "suggestion" to the avoidance simulation.
  221. /// The simulation will try to fulfill this velocity wish if possible but may change the velocity depending on other agent's and obstacles'.
  222. COMMAND_2(agent_set_velocity, RID, p_agent, Vector2, p_velocity);
  223. virtual Vector2 agent_get_velocity(RID p_agent) const override;
  224. /// Position of the agent in world space.
  225. COMMAND_2(agent_set_position, RID, p_agent, Vector2, p_position);
  226. virtual Vector2 agent_get_position(RID p_agent) const override;
  227. /// Returns true if the map got changed the previous frame.
  228. virtual bool agent_is_map_changed(RID p_agent) const override;
  229. /// Callback called at the end of the RVO process
  230. COMMAND_2(agent_set_avoidance_callback, RID, p_agent, Callable, p_callback);
  231. virtual bool agent_has_avoidance_callback(RID p_agent) const override;
  232. COMMAND_2(agent_set_avoidance_layers, RID, p_agent, uint32_t, p_layers);
  233. virtual uint32_t agent_get_avoidance_layers(RID p_agent) const override;
  234. COMMAND_2(agent_set_avoidance_mask, RID, p_agent, uint32_t, p_mask);
  235. virtual uint32_t agent_get_avoidance_mask(RID p_agent) const override;
  236. COMMAND_2(agent_set_avoidance_priority, RID, p_agent, real_t, p_priority);
  237. virtual real_t agent_get_avoidance_priority(RID p_agent) const override;
  238. virtual RID obstacle_create() override;
  239. COMMAND_2(obstacle_set_avoidance_enabled, RID, p_obstacle, bool, p_enabled);
  240. virtual bool obstacle_get_avoidance_enabled(RID p_obstacle) const override;
  241. COMMAND_2(obstacle_set_map, RID, p_obstacle, RID, p_map);
  242. virtual RID obstacle_get_map(RID p_obstacle) const override;
  243. COMMAND_2(obstacle_set_paused, RID, p_obstacle, bool, p_paused);
  244. virtual bool obstacle_get_paused(RID p_obstacle) const override;
  245. COMMAND_2(obstacle_set_radius, RID, p_obstacle, real_t, p_radius);
  246. virtual real_t obstacle_get_radius(RID p_obstacle) const override;
  247. COMMAND_2(obstacle_set_velocity, RID, p_obstacle, Vector2, p_velocity);
  248. virtual Vector2 obstacle_get_velocity(RID p_obstacle) const override;
  249. COMMAND_2(obstacle_set_position, RID, p_obstacle, Vector2, p_position);
  250. virtual Vector2 obstacle_get_position(RID p_obstacle) const override;
  251. COMMAND_2(obstacle_set_vertices, RID, p_obstacle, const Vector<Vector2> &, p_vertices);
  252. virtual Vector<Vector2> obstacle_get_vertices(RID p_obstacle) const override;
  253. COMMAND_2(obstacle_set_avoidance_layers, RID, p_obstacle, uint32_t, p_layers);
  254. virtual uint32_t obstacle_get_avoidance_layers(RID p_obstacle) const override;
  255. virtual void query_path(const Ref<NavigationPathQueryParameters2D> &p_query_parameters, Ref<NavigationPathQueryResult2D> p_query_result, const Callable &p_callback = Callable()) override;
  256. COMMAND_1(free, RID, p_object);
  257. virtual void set_active(bool p_active) override;
  258. void flush_queries();
  259. virtual void process(double p_delta_time) override;
  260. virtual void physics_process(double p_delta_time) override;
  261. virtual void init() override;
  262. virtual void sync() override;
  263. virtual void finish() override;
  264. virtual int get_process_info(ProcessInfo p_info) const override;
  265. virtual void parse_source_geometry_data(const Ref<NavigationPolygon> &p_navigation_mesh, const Ref<NavigationMeshSourceGeometryData2D> &p_source_geometry_data, Node *p_root_node, const Callable &p_callback = Callable()) override;
  266. virtual void bake_from_source_geometry_data(const Ref<NavigationPolygon> &p_navigation_mesh, const Ref<NavigationMeshSourceGeometryData2D> &p_source_geometry_data, const Callable &p_callback = Callable()) override;
  267. virtual void bake_from_source_geometry_data_async(const Ref<NavigationPolygon> &p_navigation_mesh, const Ref<NavigationMeshSourceGeometryData2D> &p_source_geometry_data, const Callable &p_callback = Callable()) override;
  268. virtual bool is_baking_navigation_polygon(Ref<NavigationPolygon> p_navigation_polygon) const override;
  269. virtual RID source_geometry_parser_create() override;
  270. virtual void source_geometry_parser_set_callback(RID p_parser, const Callable &p_callback) override;
  271. virtual Vector<Vector2> simplify_path(const Vector<Vector2> &p_path, real_t p_epsilon) override;
  272. private:
  273. void internal_free_agent(RID p_object);
  274. void internal_free_obstacle(RID p_object);
  275. };
  276. #undef COMMAND_1
  277. #undef COMMAND_2