navigation_server.h 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /**************************************************************************/
  2. /* navigation_server.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 NAVIGATION_SERVER_H
  31. #define NAVIGATION_SERVER_H
  32. #include "core/object.h"
  33. #include "core/rid.h"
  34. #include "scene/3d/navigation_mesh_instance.h"
  35. /// This server uses the concept of internal mutability.
  36. /// All the constant functions can be called in multithread because internally
  37. /// the server takes care to schedule the functions access.
  38. ///
  39. /// Note: All the `set` functions are commands executed during the `sync` phase,
  40. /// don't expect that a change is immediately propagated.
  41. class NavigationServer : public Object {
  42. GDCLASS(NavigationServer, Object);
  43. static NavigationServer *singleton;
  44. protected:
  45. static void _bind_methods();
  46. public:
  47. /// Thread safe, can be used across many threads.
  48. static const NavigationServer *get_singleton();
  49. /// MUST be used in single thread!
  50. static NavigationServer *get_singleton_mut();
  51. virtual Array get_maps() const = 0;
  52. /// Create a new map.
  53. virtual RID map_create() const = 0;
  54. /// Set map active.
  55. virtual void map_set_active(RID p_map, bool p_active) const = 0;
  56. /// Returns true if the map is active.
  57. virtual bool map_is_active(RID p_map) const = 0;
  58. /// Set the map UP direction.
  59. virtual void map_set_up(RID p_map, Vector3 p_up) const = 0;
  60. /// Returns the map UP direction.
  61. virtual Vector3 map_get_up(RID p_map) const = 0;
  62. /// Set the map cell size used to weld the navigation mesh polygons.
  63. virtual void map_set_cell_size(RID p_map, real_t p_cell_size) const = 0;
  64. /// Returns the map cell size.
  65. virtual real_t map_get_cell_size(RID p_map) const = 0;
  66. /// Set the map cell height used to weld the navigation mesh polygons.
  67. virtual void map_set_cell_height(RID p_map, real_t p_cell_height) const = 0;
  68. /// Returns the map cell height.
  69. virtual real_t map_get_cell_height(RID p_map) const = 0;
  70. /// Set the map edge connection margin used to weld the compatible region edges.
  71. virtual void map_set_edge_connection_margin(RID p_map, real_t p_connection_margin) const = 0;
  72. /// Returns the edge connection margin of this map.
  73. virtual real_t map_get_edge_connection_margin(RID p_map) const = 0;
  74. /// Returns the navigation path to reach the destination from the origin.
  75. virtual Vector<Vector3> map_get_path(RID p_map, Vector3 p_origin, Vector3 p_destination, bool p_optimize, uint32_t p_navigation_layers = 1) const = 0;
  76. 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 = 0;
  77. virtual Vector3 map_get_closest_point(RID p_map, const Vector3 &p_point) const = 0;
  78. virtual Vector3 map_get_closest_point_normal(RID p_map, const Vector3 &p_point) const = 0;
  79. virtual RID map_get_closest_point_owner(RID p_map, const Vector3 &p_point) const = 0;
  80. virtual Array map_get_regions(RID p_map) const = 0;
  81. virtual Array map_get_agents(RID p_map) const = 0;
  82. virtual void map_force_update(RID p_map) = 0;
  83. /// Creates a new region.
  84. virtual RID region_create() const = 0;
  85. /// Set the enter_cost of a region
  86. virtual void region_set_enter_cost(RID p_region, real_t p_enter_cost) const = 0;
  87. virtual real_t region_get_enter_cost(RID p_region) const = 0;
  88. /// Set the travel_cost of a region
  89. virtual void region_set_travel_cost(RID p_region, real_t p_travel_cost) const = 0;
  90. virtual real_t region_get_travel_cost(RID p_region) const = 0;
  91. virtual bool region_owns_point(RID p_region, const Vector3 &p_point) const = 0;
  92. /// Set the map of this region.
  93. virtual void region_set_map(RID p_region, RID p_map) const = 0;
  94. virtual RID region_get_map(RID p_region) const = 0;
  95. /// Set the region's layers
  96. virtual void region_set_navigation_layers(RID p_region, uint32_t p_navigation_layers) const = 0;
  97. virtual uint32_t region_get_navigation_layers(RID p_region) const = 0;
  98. /// Set the global transformation of this region.
  99. virtual void region_set_transform(RID p_region, Transform p_transform) const = 0;
  100. /// Set the navigation mesh of this region.
  101. virtual void region_set_navmesh(RID p_region, Ref<NavigationMesh> p_nav_mesh) const = 0;
  102. /// Bake the navigation mesh.
  103. virtual void region_bake_navmesh(Ref<NavigationMesh> r_mesh, Node *p_node) const = 0;
  104. /// Get a list of a region's connection to other regions.
  105. virtual int region_get_connections_count(RID p_region) const = 0;
  106. virtual Vector3 region_get_connection_pathway_start(RID p_region, int p_connection_id) const = 0;
  107. virtual Vector3 region_get_connection_pathway_end(RID p_region, int p_connection_id) const = 0;
  108. /// Creates the agent.
  109. virtual RID agent_create() const = 0;
  110. /// Put the agent in the map.
  111. virtual void agent_set_map(RID p_agent, RID p_map) const = 0;
  112. virtual RID agent_get_map(RID p_agent) const = 0;
  113. /// The maximum distance (center point to
  114. /// center point) to other agents this agent
  115. /// takes into account in the navigation. The
  116. /// larger this number, the longer the running
  117. /// time of the simulation. If the number is too
  118. /// low, the simulation will not be safe.
  119. /// Must be non-negative.
  120. virtual void agent_set_neighbor_dist(RID p_agent, real_t p_dist) const = 0;
  121. /// The maximum number of other agents this
  122. /// agent takes into account in the navigation.
  123. /// The larger this number, the longer the
  124. /// running time of the simulation. If the
  125. /// number is too low, the simulation will not
  126. /// be safe.
  127. virtual void agent_set_max_neighbors(RID p_agent, int p_count) const = 0;
  128. /// The minimal amount of time for which this
  129. /// agent's velocities that are computed by the
  130. /// simulation are safe with respect to other
  131. /// agents. The larger this number, the sooner
  132. /// this agent will respond to the presence of
  133. /// other agents, but the less freedom this
  134. /// agent has in choosing its velocities.
  135. /// Must be positive.
  136. virtual void agent_set_time_horizon(RID p_agent, real_t p_time) const = 0;
  137. /// The radius of this agent.
  138. /// Must be non-negative.
  139. virtual void agent_set_radius(RID p_agent, real_t p_radius) const = 0;
  140. /// The maximum speed of this agent.
  141. /// Must be non-negative.
  142. virtual void agent_set_max_speed(RID p_agent, real_t p_max_speed) const = 0;
  143. /// Current velocity of the agent
  144. virtual void agent_set_velocity(RID p_agent, Vector3 p_velocity) const = 0;
  145. /// The new target velocity.
  146. virtual void agent_set_target_velocity(RID p_agent, Vector3 p_velocity) const = 0;
  147. /// Position of the agent in world space.
  148. virtual void agent_set_position(RID p_agent, Vector3 p_position) const = 0;
  149. /// Agent ignore the Y axis and avoid collisions by moving only on the horizontal plane
  150. virtual void agent_set_ignore_y(RID p_agent, bool p_ignore) const = 0;
  151. /// Returns true if the map got changed the previous frame.
  152. virtual bool agent_is_map_changed(RID p_agent) const = 0;
  153. /// Callback called at the end of the RVO process
  154. virtual void agent_set_callback(RID p_agent, Object *p_receiver, StringName p_method, Variant p_udata = Variant()) const = 0;
  155. /// Destroy the `RID`
  156. virtual void free(RID p_object) const = 0;
  157. /// Control activation of this server.
  158. virtual void set_active(bool p_active) const = 0;
  159. /// Process the collision avoidance agents.
  160. /// The result of this process is needed by the physics server,
  161. /// so this must be called in the main thread.
  162. /// Note: This function is not thread safe.
  163. virtual void process(real_t delta_time) = 0;
  164. NavigationServer();
  165. virtual ~NavigationServer();
  166. };
  167. typedef NavigationServer *(*NavigationServerCallback)();
  168. /// Manager used for the server singleton registration
  169. class NavigationServerManager {
  170. static NavigationServerCallback create_callback;
  171. public:
  172. static void set_default_server(NavigationServerCallback p_callback);
  173. static NavigationServer *new_default_server();
  174. };
  175. #endif // NAVIGATION_SERVER_H