navigation_server_3d.h 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /*************************************************************************/
  2. /* navigation_server_3d.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2022 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. #ifndef NAVIGATION_SERVER_3D_H
  31. #define NAVIGATION_SERVER_3D_H
  32. #include "core/object/class_db.h"
  33. #include "core/templates/rid.h"
  34. #include "scene/3d/navigation_region_3d.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 NavigationServer3D : public Object {
  42. GDCLASS(NavigationServer3D, Object);
  43. static NavigationServer3D *singleton;
  44. protected:
  45. static void _bind_methods();
  46. public:
  47. /// Thread safe, can be used across many threads.
  48. static const NavigationServer3D *get_singleton();
  49. /// MUST be used in single thread!
  50. static NavigationServer3D *get_singleton_mut();
  51. /// Create a new map.
  52. virtual RID map_create() const = 0;
  53. /// Set map active.
  54. virtual void map_set_active(RID p_map, bool p_active) const = 0;
  55. /// Returns true if the map is active.
  56. virtual bool map_is_active(RID p_map) const = 0;
  57. /// Set the map UP direction.
  58. virtual void map_set_up(RID p_map, Vector3 p_up) const = 0;
  59. /// Returns the map UP direction.
  60. virtual Vector3 map_get_up(RID p_map) const = 0;
  61. /// Set the map cell size used to weld the navigation mesh polygons.
  62. virtual void map_set_cell_size(RID p_map, real_t p_cell_size) const = 0;
  63. /// Returns the map cell size.
  64. virtual real_t map_get_cell_size(RID p_map) const = 0;
  65. /// Set the map edge connection margin used to weld the compatible region edges.
  66. virtual void map_set_edge_connection_margin(RID p_map, real_t p_connection_margin) const = 0;
  67. /// Returns the edge connection margin of this map.
  68. virtual real_t map_get_edge_connection_margin(RID p_map) const = 0;
  69. /// Returns the navigation path to reach the destination from the origin.
  70. virtual Vector<Vector3> map_get_path(RID p_map, Vector3 p_origin, Vector3 p_destination, bool p_optimize, uint32_t p_navigable_layers = 1) const = 0;
  71. 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;
  72. virtual Vector3 map_get_closest_point(RID p_map, const Vector3 &p_point) const = 0;
  73. virtual Vector3 map_get_closest_point_normal(RID p_map, const Vector3 &p_point) const = 0;
  74. virtual RID map_get_closest_point_owner(RID p_map, const Vector3 &p_point) const = 0;
  75. /// Creates a new region.
  76. virtual RID region_create() const = 0;
  77. /// Set the map of this region.
  78. virtual void region_set_map(RID p_region, RID p_map) const = 0;
  79. /// Set the region's layers
  80. virtual void region_set_layers(RID p_region, uint32_t p_layers) const = 0;
  81. virtual uint32_t region_get_layers(RID p_region) const = 0;
  82. /// Set the global transformation of this region.
  83. virtual void region_set_transform(RID p_region, Transform3D p_transform) const = 0;
  84. /// Set the navigation mesh of this region.
  85. virtual void region_set_navmesh(RID p_region, Ref<NavigationMesh> p_nav_mesh) const = 0;
  86. /// Bake the navigation mesh.
  87. virtual void region_bake_navmesh(Ref<NavigationMesh> r_mesh, Node *p_node) const = 0;
  88. /// Get a list of a region's connection to other regions.
  89. virtual int region_get_connections_count(RID p_region) const = 0;
  90. virtual Vector3 region_get_connection_pathway_start(RID p_region, int p_connection_id) const = 0;
  91. virtual Vector3 region_get_connection_pathway_end(RID p_region, int p_connection_id) const = 0;
  92. /// Creates the agent.
  93. virtual RID agent_create() const = 0;
  94. /// Put the agent in the map.
  95. virtual void agent_set_map(RID p_agent, RID p_map) const = 0;
  96. /// The maximum distance (center point to
  97. /// center point) to other agents this agent
  98. /// takes into account in the navigation. The
  99. /// larger this number, the longer the running
  100. /// time of the simulation. If the number is too
  101. /// low, the simulation will not be safe.
  102. /// Must be non-negative.
  103. virtual void agent_set_neighbor_dist(RID p_agent, real_t p_dist) const = 0;
  104. /// The maximum number of other agents this
  105. /// agent takes into account in the navigation.
  106. /// The larger this number, the longer the
  107. /// running time of the simulation. If the
  108. /// number is too low, the simulation will not
  109. /// be safe.
  110. virtual void agent_set_max_neighbors(RID p_agent, int p_count) const = 0;
  111. /// The minimal amount of time for which this
  112. /// agent's velocities that are computed by the
  113. /// simulation are safe with respect to other
  114. /// agents. The larger this number, the sooner
  115. /// this agent will respond to the presence of
  116. /// other agents, but the less freedom this
  117. /// agent has in choosing its velocities.
  118. /// Must be positive.
  119. virtual void agent_set_time_horizon(RID p_agent, real_t p_time) const = 0;
  120. /// The radius of this agent.
  121. /// Must be non-negative.
  122. virtual void agent_set_radius(RID p_agent, real_t p_radius) const = 0;
  123. /// The maximum speed of this agent.
  124. /// Must be non-negative.
  125. virtual void agent_set_max_speed(RID p_agent, real_t p_max_speed) const = 0;
  126. /// Current velocity of the agent
  127. virtual void agent_set_velocity(RID p_agent, Vector3 p_velocity) const = 0;
  128. /// The new target velocity.
  129. virtual void agent_set_target_velocity(RID p_agent, Vector3 p_velocity) const = 0;
  130. /// Position of the agent in world space.
  131. virtual void agent_set_position(RID p_agent, Vector3 p_position) const = 0;
  132. /// Agent ignore the Y axis and avoid collisions by moving only on the horizontal plane
  133. virtual void agent_set_ignore_y(RID p_agent, bool p_ignore) const = 0;
  134. /// Returns true if the map got changed the previous frame.
  135. virtual bool agent_is_map_changed(RID p_agent) const = 0;
  136. /// Callback called at the end of the RVO process
  137. virtual void agent_set_callback(RID p_agent, Object *p_receiver, StringName p_method, Variant p_udata = Variant()) const = 0;
  138. /// Destroy the `RID`
  139. virtual void free(RID p_object) const = 0;
  140. /// Control activation of this server.
  141. virtual void set_active(bool p_active) const = 0;
  142. /// Process the collision avoidance agents.
  143. /// The result of this process is needed by the physics server,
  144. /// so this must be called in the main thread.
  145. /// Note: This function is not thread safe.
  146. virtual void process(real_t delta_time) = 0;
  147. NavigationServer3D();
  148. virtual ~NavigationServer3D();
  149. };
  150. typedef NavigationServer3D *(*NavigationServer3DCallback)();
  151. /// Manager used for the server singleton registration
  152. class NavigationServer3DManager {
  153. static NavigationServer3DCallback create_callback;
  154. public:
  155. static void set_default_server(NavigationServer3DCallback p_callback);
  156. static NavigationServer3D *new_default_server();
  157. };
  158. #endif // NAVIGATION_SERVER_3D_H