navigation_server_3d.h 8.2 KB

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