godot_navigation_server_2d.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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. #ifndef GODOT_NAVIGATION_SERVER_2D_H
  31. #define GODOT_NAVIGATION_SERVER_2D_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 "servers/navigation_server_2d.h"
  38. #ifdef CLIPPER2_ENABLED
  39. class NavMeshGenerator2D;
  40. #endif // CLIPPER2_ENABLED
  41. // This server exposes the `NavigationServer3D` features in the 2D world.
  42. class GodotNavigationServer2D : public NavigationServer2D {
  43. GDCLASS(GodotNavigationServer2D, NavigationServer2D);
  44. #ifdef CLIPPER2_ENABLED
  45. NavMeshGenerator2D *navmesh_generator_2d = nullptr;
  46. #endif // CLIPPER2_ENABLED
  47. public:
  48. GodotNavigationServer2D();
  49. virtual ~GodotNavigationServer2D();
  50. virtual TypedArray<RID> get_maps() const override;
  51. virtual RID map_create() override;
  52. virtual void map_set_active(RID p_map, bool p_active) override;
  53. virtual bool map_is_active(RID p_map) const override;
  54. virtual void map_set_cell_size(RID p_map, real_t p_cell_size) override;
  55. virtual real_t map_get_cell_size(RID p_map) const override;
  56. virtual void map_set_use_edge_connections(RID p_map, bool p_enabled) override;
  57. virtual bool map_get_use_edge_connections(RID p_map) const override;
  58. virtual void map_set_edge_connection_margin(RID p_map, real_t p_connection_margin) override;
  59. virtual real_t map_get_edge_connection_margin(RID p_map) const override;
  60. virtual void map_set_link_connection_radius(RID p_map, real_t p_connection_radius) override;
  61. virtual real_t map_get_link_connection_radius(RID p_map) const override;
  62. 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;
  63. virtual Vector2 map_get_closest_point(RID p_map, const Vector2 &p_point) const override;
  64. virtual RID map_get_closest_point_owner(RID p_map, const Vector2 &p_point) const override;
  65. virtual TypedArray<RID> map_get_links(RID p_map) const override;
  66. virtual TypedArray<RID> map_get_regions(RID p_map) const override;
  67. virtual TypedArray<RID> map_get_agents(RID p_map) const override;
  68. virtual TypedArray<RID> map_get_obstacles(RID p_map) const override;
  69. virtual void map_force_update(RID p_map) override;
  70. virtual Vector2 map_get_random_point(RID p_map, uint32_t p_navigation_layers, bool p_uniformly) const override;
  71. virtual uint32_t map_get_iteration_id(RID p_map) const override;
  72. virtual void map_set_use_async_iterations(RID p_map, bool p_enabled) override;
  73. virtual bool map_get_use_async_iterations(RID p_map) const override;
  74. virtual RID region_create() override;
  75. virtual void region_set_enabled(RID p_region, bool p_enabled) override;
  76. virtual bool region_get_enabled(RID p_region) const override;
  77. virtual void region_set_use_edge_connections(RID p_region, bool p_enabled) override;
  78. virtual bool region_get_use_edge_connections(RID p_region) const override;
  79. virtual void region_set_enter_cost(RID p_region, real_t p_enter_cost) override;
  80. virtual real_t region_get_enter_cost(RID p_region) const override;
  81. virtual void region_set_travel_cost(RID p_region, real_t p_travel_cost) override;
  82. virtual real_t region_get_travel_cost(RID p_region) const override;
  83. virtual void region_set_owner_id(RID p_region, ObjectID p_owner_id) override;
  84. virtual ObjectID region_get_owner_id(RID p_region) const override;
  85. virtual bool region_owns_point(RID p_region, const Vector2 &p_point) const override;
  86. virtual void region_set_map(RID p_region, RID p_map) override;
  87. virtual RID region_get_map(RID p_region) const override;
  88. virtual void region_set_navigation_layers(RID p_region, uint32_t p_navigation_layers) override;
  89. virtual uint32_t region_get_navigation_layers(RID p_region) const override;
  90. virtual void region_set_transform(RID p_region, Transform2D p_transform) override;
  91. virtual Transform2D region_get_transform(RID p_region) const override;
  92. virtual void region_set_navigation_polygon(RID p_region, Ref<NavigationPolygon> p_navigation_polygon) override;
  93. virtual int region_get_connections_count(RID p_region) const override;
  94. virtual Vector2 region_get_connection_pathway_start(RID p_region, int p_connection_id) const override;
  95. virtual Vector2 region_get_connection_pathway_end(RID p_region, int p_connection_id) const override;
  96. virtual Vector2 region_get_closest_point(RID p_region, const Vector2 &p_point) const override;
  97. virtual Vector2 region_get_random_point(RID p_region, uint32_t p_navigation_layers, bool p_uniformly) const override;
  98. virtual Rect2 region_get_bounds(RID p_region) const override;
  99. virtual RID link_create() override;
  100. /// Set the map of this link.
  101. virtual void link_set_map(RID p_link, RID p_map) override;
  102. virtual RID link_get_map(RID p_link) const override;
  103. virtual void link_set_enabled(RID p_link, bool p_enabled) override;
  104. virtual bool link_get_enabled(RID p_link) const override;
  105. /// Set whether this link travels in both directions.
  106. virtual void link_set_bidirectional(RID p_link, bool p_bidirectional) override;
  107. virtual bool link_is_bidirectional(RID p_link) const override;
  108. /// Set the link's layers.
  109. virtual void link_set_navigation_layers(RID p_link, uint32_t p_navigation_layers) override;
  110. virtual uint32_t link_get_navigation_layers(RID p_link) const override;
  111. /// Set the start position of the link.
  112. virtual void link_set_start_position(RID p_link, Vector2 p_position) override;
  113. virtual Vector2 link_get_start_position(RID p_link) const override;
  114. /// Set the end position of the link.
  115. virtual void link_set_end_position(RID p_link, Vector2 p_position) override;
  116. virtual Vector2 link_get_end_position(RID p_link) const override;
  117. /// Set the enter cost of the link.
  118. virtual void link_set_enter_cost(RID p_link, real_t p_enter_cost) override;
  119. virtual real_t link_get_enter_cost(RID p_link) const override;
  120. /// Set the travel cost of the link.
  121. virtual void link_set_travel_cost(RID p_link, real_t p_travel_cost) override;
  122. virtual real_t link_get_travel_cost(RID p_link) const override;
  123. /// Set the node which manages this link.
  124. virtual void link_set_owner_id(RID p_link, ObjectID p_owner_id) override;
  125. virtual ObjectID link_get_owner_id(RID p_link) const override;
  126. /// Creates the agent.
  127. virtual RID agent_create() override;
  128. /// Put the agent in the map.
  129. virtual void agent_set_map(RID p_agent, RID p_map) override;
  130. virtual RID agent_get_map(RID p_agent) const override;
  131. virtual void agent_set_paused(RID p_agent, bool p_paused) override;
  132. virtual bool agent_get_paused(RID p_agent) const override;
  133. virtual void agent_set_avoidance_enabled(RID p_agent, bool p_enabled) override;
  134. virtual bool agent_get_avoidance_enabled(RID p_agent) const override;
  135. /// The maximum distance (center point to
  136. /// center point) to other agents this agent
  137. /// takes into account in the navigation. The
  138. /// larger this number, the longer the running
  139. /// time of the simulation. If the number is too
  140. /// low, the simulation will not be safe.
  141. /// Must be non-negative.
  142. virtual void agent_set_neighbor_distance(RID p_agent, real_t p_distance) override;
  143. virtual real_t agent_get_neighbor_distance(RID p_agent) const override;
  144. /// The maximum number of other agents this
  145. /// agent takes into account in the navigation.
  146. /// The larger this number, the longer the
  147. /// running time of the simulation. If the
  148. /// number is too low, the simulation will not
  149. /// be safe.
  150. virtual void agent_set_max_neighbors(RID p_agent, int p_count) override;
  151. virtual int agent_get_max_neighbors(RID p_agent) const override;
  152. /// The minimal amount of time for which this
  153. /// agent's velocities that are computed by the
  154. /// simulation are safe with respect to other
  155. /// agents. The larger this number, the sooner
  156. /// this agent will respond to the presence of
  157. /// other agents, but the less freedom this
  158. /// agent has in choosing its velocities.
  159. /// Must be positive.
  160. virtual void agent_set_time_horizon_agents(RID p_agent, real_t p_time_horizon) override;
  161. virtual real_t agent_get_time_horizon_agents(RID p_agent) const override;
  162. virtual void agent_set_time_horizon_obstacles(RID p_agent, real_t p_time_horizon) override;
  163. virtual real_t agent_get_time_horizon_obstacles(RID p_agent) const override;
  164. /// The radius of this agent.
  165. /// Must be non-negative.
  166. virtual void agent_set_radius(RID p_agent, real_t p_radius) override;
  167. virtual real_t agent_get_radius(RID p_agent) const override;
  168. /// The maximum speed of this agent.
  169. /// Must be non-negative.
  170. virtual void agent_set_max_speed(RID p_agent, real_t p_max_speed) override;
  171. virtual real_t agent_get_max_speed(RID p_agent) const override;
  172. /// forces and agent velocity change in the avoidance simulation, adds simulation instability if done recklessly
  173. virtual void agent_set_velocity_forced(RID p_agent, Vector2 p_velocity) override;
  174. /// The wanted velocity for the agent as a "suggestion" to the avoidance simulation.
  175. /// The simulation will try to fulfill this velocity wish if possible but may change the velocity depending on other agent's and obstacles'.
  176. virtual void agent_set_velocity(RID p_agent, Vector2 p_velocity) override;
  177. virtual Vector2 agent_get_velocity(RID p_agent) const override;
  178. /// Position of the agent in world space.
  179. virtual void agent_set_position(RID p_agent, Vector2 p_position) override;
  180. virtual Vector2 agent_get_position(RID p_agent) const override;
  181. /// Returns true if the map got changed the previous frame.
  182. virtual bool agent_is_map_changed(RID p_agent) const override;
  183. /// Callback called at the end of the RVO process
  184. virtual void agent_set_avoidance_callback(RID p_agent, Callable p_callback) override;
  185. virtual bool agent_has_avoidance_callback(RID p_agent) const override;
  186. virtual void agent_set_avoidance_layers(RID p_agent, uint32_t p_layers) override;
  187. virtual uint32_t agent_get_avoidance_layers(RID p_agent) const override;
  188. virtual void agent_set_avoidance_mask(RID p_agent, uint32_t p_mask) override;
  189. virtual uint32_t agent_get_avoidance_mask(RID p_agent) const override;
  190. virtual void agent_set_avoidance_priority(RID p_agent, real_t p_priority) override;
  191. virtual real_t agent_get_avoidance_priority(RID p_agent) const override;
  192. virtual RID obstacle_create() override;
  193. virtual void obstacle_set_avoidance_enabled(RID p_obstacle, bool p_enabled) override;
  194. virtual bool obstacle_get_avoidance_enabled(RID p_obstacle) const override;
  195. virtual void obstacle_set_map(RID p_obstacle, RID p_map) override;
  196. virtual RID obstacle_get_map(RID p_obstacle) const override;
  197. virtual void obstacle_set_paused(RID p_obstacle, bool p_paused) override;
  198. virtual bool obstacle_get_paused(RID p_obstacle) const override;
  199. virtual void obstacle_set_radius(RID p_obstacle, real_t p_radius) override;
  200. virtual real_t obstacle_get_radius(RID p_obstacle) const override;
  201. virtual void obstacle_set_velocity(RID p_obstacle, Vector2 p_velocity) override;
  202. virtual Vector2 obstacle_get_velocity(RID p_obstacle) const override;
  203. virtual void obstacle_set_position(RID p_obstacle, Vector2 p_position) override;
  204. virtual Vector2 obstacle_get_position(RID p_obstacle) const override;
  205. virtual void obstacle_set_vertices(RID p_obstacle, const Vector<Vector2> &p_vertices) override;
  206. virtual Vector<Vector2> obstacle_get_vertices(RID p_obstacle) const override;
  207. virtual void obstacle_set_avoidance_layers(RID p_obstacle, uint32_t p_layers) override;
  208. virtual uint32_t obstacle_get_avoidance_layers(RID p_obstacle) const override;
  209. virtual void query_path(const Ref<NavigationPathQueryParameters2D> &p_query_parameters, Ref<NavigationPathQueryResult2D> p_query_result, const Callable &p_callback) override;
  210. virtual void init() override;
  211. virtual void sync() override;
  212. virtual void finish() override;
  213. virtual void free(RID p_object) override;
  214. 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;
  215. 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;
  216. 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;
  217. virtual bool is_baking_navigation_polygon(Ref<NavigationPolygon> p_navigation_polygon) const override;
  218. virtual RID source_geometry_parser_create() override;
  219. virtual void source_geometry_parser_set_callback(RID p_parser, const Callable &p_callback) override;
  220. virtual Vector<Vector2> simplify_path(const Vector<Vector2> &p_path, real_t p_epsilon) override;
  221. };
  222. #endif // GODOT_NAVIGATION_SERVER_2D_H