godot_navigation_server.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. /**************************************************************************/
  2. /* godot_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 GODOT_NAVIGATION_SERVER_H
  31. #define GODOT_NAVIGATION_SERVER_H
  32. #include "core/templates/local_vector.h"
  33. #include "core/templates/rid.h"
  34. #include "core/templates/rid_owner.h"
  35. #include "servers/navigation_server_3d.h"
  36. #include "nav_agent.h"
  37. #include "nav_link.h"
  38. #include "nav_map.h"
  39. #include "nav_region.h"
  40. /// The commands are functions executed during the `sync` phase.
  41. #define MERGE_INTERNAL(A, B) A##B
  42. #define MERGE(A, B) MERGE_INTERNAL(A, B)
  43. #define COMMAND_1(F_NAME, T_0, D_0) \
  44. virtual void F_NAME(T_0 D_0) override; \
  45. void MERGE(_cmd_, F_NAME)(T_0 D_0)
  46. #define COMMAND_2(F_NAME, T_0, D_0, T_1, D_1) \
  47. virtual void F_NAME(T_0 D_0, T_1 D_1) override; \
  48. void MERGE(_cmd_, F_NAME)(T_0 D_0, T_1 D_1)
  49. class GodotNavigationServer;
  50. struct SetCommand {
  51. virtual ~SetCommand() {}
  52. virtual void exec(GodotNavigationServer *server) = 0;
  53. };
  54. class GodotNavigationServer : public NavigationServer3D {
  55. Mutex commands_mutex;
  56. /// Mutex used to make any operation threadsafe.
  57. Mutex operations_mutex;
  58. LocalVector<SetCommand *> commands;
  59. mutable RID_Owner<NavLink> link_owner;
  60. mutable RID_Owner<NavMap> map_owner;
  61. mutable RID_Owner<NavRegion> region_owner;
  62. mutable RID_Owner<NavAgent> agent_owner;
  63. bool active = true;
  64. LocalVector<NavMap *> active_maps;
  65. LocalVector<uint32_t> active_maps_update_id;
  66. // Performance Monitor
  67. int pm_region_count = 0;
  68. int pm_agent_count = 0;
  69. int pm_link_count = 0;
  70. int pm_polygon_count = 0;
  71. int pm_edge_count = 0;
  72. int pm_edge_merge_count = 0;
  73. int pm_edge_connection_count = 0;
  74. int pm_edge_free_count = 0;
  75. public:
  76. GodotNavigationServer();
  77. virtual ~GodotNavigationServer();
  78. void add_command(SetCommand *command);
  79. virtual TypedArray<RID> get_maps() const override;
  80. virtual RID map_create() override;
  81. COMMAND_2(map_set_active, RID, p_map, bool, p_active);
  82. virtual bool map_is_active(RID p_map) const override;
  83. COMMAND_2(map_set_up, RID, p_map, Vector3, p_up);
  84. virtual Vector3 map_get_up(RID p_map) const override;
  85. COMMAND_2(map_set_cell_size, RID, p_map, real_t, p_cell_size);
  86. virtual real_t map_get_cell_size(RID p_map) const override;
  87. COMMAND_2(map_set_edge_connection_margin, RID, p_map, real_t, p_connection_margin);
  88. virtual real_t map_get_edge_connection_margin(RID p_map) const override;
  89. COMMAND_2(map_set_link_connection_radius, RID, p_map, real_t, p_connection_radius);
  90. virtual real_t map_get_link_connection_radius(RID p_map) const override;
  91. 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 override;
  92. 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 override;
  93. virtual Vector3 map_get_closest_point(RID p_map, const Vector3 &p_point) const override;
  94. virtual Vector3 map_get_closest_point_normal(RID p_map, const Vector3 &p_point) const override;
  95. virtual RID map_get_closest_point_owner(RID p_map, const Vector3 &p_point) const override;
  96. virtual TypedArray<RID> map_get_links(RID p_map) const override;
  97. virtual TypedArray<RID> map_get_regions(RID p_map) const override;
  98. virtual TypedArray<RID> map_get_agents(RID p_map) const override;
  99. virtual void map_force_update(RID p_map) override;
  100. virtual RID region_create() override;
  101. COMMAND_2(region_set_enter_cost, RID, p_region, real_t, p_enter_cost);
  102. virtual real_t region_get_enter_cost(RID p_region) const override;
  103. COMMAND_2(region_set_travel_cost, RID, p_region, real_t, p_travel_cost);
  104. virtual real_t region_get_travel_cost(RID p_region) const override;
  105. COMMAND_2(region_set_owner_id, RID, p_region, ObjectID, p_owner_id);
  106. virtual ObjectID region_get_owner_id(RID p_region) const override;
  107. virtual bool region_owns_point(RID p_region, const Vector3 &p_point) const override;
  108. COMMAND_2(region_set_map, RID, p_region, RID, p_map);
  109. virtual RID region_get_map(RID p_region) const override;
  110. COMMAND_2(region_set_navigation_layers, RID, p_region, uint32_t, p_navigation_layers);
  111. virtual uint32_t region_get_navigation_layers(RID p_region) const override;
  112. COMMAND_2(region_set_transform, RID, p_region, Transform3D, p_transform);
  113. COMMAND_2(region_set_navigation_mesh, RID, p_region, Ref<NavigationMesh>, p_navigation_mesh);
  114. virtual void region_bake_navigation_mesh(Ref<NavigationMesh> p_navigation_mesh, Node *p_root_node) override;
  115. virtual int region_get_connections_count(RID p_region) const override;
  116. virtual Vector3 region_get_connection_pathway_start(RID p_region, int p_connection_id) const override;
  117. virtual Vector3 region_get_connection_pathway_end(RID p_region, int p_connection_id) const override;
  118. virtual RID link_create() override;
  119. COMMAND_2(link_set_map, RID, p_link, RID, p_map);
  120. virtual RID link_get_map(RID p_link) const override;
  121. COMMAND_2(link_set_bidirectional, RID, p_link, bool, p_bidirectional);
  122. virtual bool link_is_bidirectional(RID p_link) const override;
  123. COMMAND_2(link_set_navigation_layers, RID, p_link, uint32_t, p_navigation_layers);
  124. virtual uint32_t link_get_navigation_layers(RID p_link) const override;
  125. COMMAND_2(link_set_start_position, RID, p_link, Vector3, p_position);
  126. virtual Vector3 link_get_start_position(RID p_link) const override;
  127. COMMAND_2(link_set_end_position, RID, p_link, Vector3, p_position);
  128. virtual Vector3 link_get_end_position(RID p_link) const override;
  129. COMMAND_2(link_set_enter_cost, RID, p_link, real_t, p_enter_cost);
  130. virtual real_t link_get_enter_cost(RID p_link) const override;
  131. COMMAND_2(link_set_travel_cost, RID, p_link, real_t, p_travel_cost);
  132. virtual real_t link_get_travel_cost(RID p_link) const override;
  133. COMMAND_2(link_set_owner_id, RID, p_link, ObjectID, p_owner_id);
  134. virtual ObjectID link_get_owner_id(RID p_link) const override;
  135. virtual RID agent_create() override;
  136. COMMAND_2(agent_set_map, RID, p_agent, RID, p_map);
  137. virtual RID agent_get_map(RID p_agent) const override;
  138. COMMAND_2(agent_set_neighbor_distance, RID, p_agent, real_t, p_distance);
  139. COMMAND_2(agent_set_max_neighbors, RID, p_agent, int, p_count);
  140. COMMAND_2(agent_set_time_horizon, RID, p_agent, real_t, p_time);
  141. COMMAND_2(agent_set_radius, RID, p_agent, real_t, p_radius);
  142. COMMAND_2(agent_set_max_speed, RID, p_agent, real_t, p_max_speed);
  143. COMMAND_2(agent_set_velocity, RID, p_agent, Vector3, p_velocity);
  144. COMMAND_2(agent_set_target_velocity, RID, p_agent, Vector3, p_velocity);
  145. COMMAND_2(agent_set_position, RID, p_agent, Vector3, p_position);
  146. COMMAND_2(agent_set_ignore_y, RID, p_agent, bool, p_ignore);
  147. virtual bool agent_is_map_changed(RID p_agent) const override;
  148. COMMAND_2(agent_set_callback, RID, p_agent, Callable, p_callback);
  149. COMMAND_1(free, RID, p_object);
  150. virtual void set_active(bool p_active) override;
  151. void flush_queries();
  152. virtual void process(real_t p_delta_time) override;
  153. virtual NavigationUtilities::PathQueryResult _query_path(const NavigationUtilities::PathQueryParameters &p_parameters) const override;
  154. int get_process_info(ProcessInfo p_info) const override;
  155. };
  156. #undef COMMAND_1
  157. #undef COMMAND_2
  158. #endif // GODOT_NAVIGATION_SERVER_H