godot_navigation_server.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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/rid.h"
  33. #include "servers/navigation_server.h"
  34. #include "nav_map.h"
  35. #include "nav_region.h"
  36. #include "rvo_agent.h"
  37. /// The commands are functions executed during the `sync` phase.
  38. #define MERGE_INTERNAL(A, B) A##B
  39. #define MERGE(A, B) MERGE_INTERNAL(A, B)
  40. #define COMMAND_1(F_NAME, T_0, D_0) \
  41. virtual void F_NAME(T_0 D_0) const; \
  42. void MERGE(_cmd_, F_NAME)(T_0 D_0)
  43. #define COMMAND_2(F_NAME, T_0, D_0, T_1, D_1) \
  44. virtual void F_NAME(T_0 D_0, T_1 D_1) const; \
  45. void MERGE(_cmd_, F_NAME)(T_0 D_0, T_1 D_1)
  46. #define COMMAND_4_DEF(F_NAME, T_0, D_0, T_1, D_1, T_2, D_2, T_3, D_3, D_3_DEF) \
  47. virtual void F_NAME(T_0 D_0, T_1 D_1, T_2 D_2, T_3 D_3 = D_3_DEF) const; \
  48. void MERGE(_cmd_, F_NAME)(T_0 D_0, T_1 D_1, T_2 D_2, T_3 D_3)
  49. class GodotNavigationServer;
  50. struct SetCommand {
  51. virtual ~SetCommand() {}
  52. virtual void exec(GodotNavigationServer *server) = 0;
  53. };
  54. class GodotNavigationServer : public NavigationServer {
  55. Mutex commands_mutex;
  56. /// Mutex used to make any operation threadsafe.
  57. Mutex operations_mutex;
  58. LocalVector<SetCommand *> commands;
  59. mutable RID_Owner<NavMap> map_owner;
  60. mutable RID_Owner<NavRegion> region_owner;
  61. mutable RID_Owner<RvoAgent> agent_owner;
  62. bool active = true;
  63. LocalVector<NavMap *> active_maps;
  64. LocalVector<uint32_t> active_maps_update_id;
  65. public:
  66. GodotNavigationServer();
  67. virtual ~GodotNavigationServer();
  68. void add_command(SetCommand *command) const;
  69. virtual Array get_maps() const;
  70. virtual RID map_create() const;
  71. COMMAND_2(map_set_active, RID, p_map, bool, p_active);
  72. virtual bool map_is_active(RID p_map) const;
  73. COMMAND_2(map_set_up, RID, p_map, Vector3, p_up);
  74. virtual Vector3 map_get_up(RID p_map) const;
  75. COMMAND_2(map_set_cell_size, RID, p_map, real_t, p_cell_size);
  76. virtual real_t map_get_cell_size(RID p_map) const;
  77. COMMAND_2(map_set_cell_height, RID, p_map, real_t, p_cell_height);
  78. virtual real_t map_get_cell_height(RID p_map) const;
  79. COMMAND_2(map_set_edge_connection_margin, RID, p_map, real_t, p_connection_margin);
  80. virtual real_t map_get_edge_connection_margin(RID p_map) const;
  81. virtual Vector<Vector3> map_get_path(RID p_map, Vector3 p_origin, Vector3 p_destination, bool p_optimize, uint32_t p_layers = 1) const;
  82. 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;
  83. virtual Vector3 map_get_closest_point(RID p_map, const Vector3 &p_point) const;
  84. virtual Vector3 map_get_closest_point_normal(RID p_map, const Vector3 &p_point) const;
  85. virtual RID map_get_closest_point_owner(RID p_map, const Vector3 &p_point) const;
  86. virtual Array map_get_regions(RID p_map) const;
  87. virtual Array map_get_agents(RID p_map) const;
  88. virtual void map_force_update(RID p_map);
  89. virtual RID region_create() const;
  90. COMMAND_2(region_set_enter_cost, RID, p_region, real_t, p_enter_cost);
  91. virtual real_t region_get_enter_cost(RID p_region) const;
  92. COMMAND_2(region_set_travel_cost, RID, p_region, real_t, p_travel_cost);
  93. virtual real_t region_get_travel_cost(RID p_region) const;
  94. virtual bool region_owns_point(RID p_region, const Vector3 &p_point) const;
  95. COMMAND_2(region_set_map, RID, p_region, RID, p_map);
  96. virtual RID region_get_map(RID p_region) const;
  97. COMMAND_2(region_set_navigation_layers, RID, p_region, uint32_t, p_navigation_layers);
  98. virtual uint32_t region_get_navigation_layers(RID p_region) const;
  99. COMMAND_2(region_set_transform, RID, p_region, Transform, p_transform);
  100. COMMAND_2(region_set_navmesh, RID, p_region, Ref<NavigationMesh>, p_nav_mesh);
  101. virtual void region_bake_navmesh(Ref<NavigationMesh> r_mesh, Node *p_node) const;
  102. virtual int region_get_connections_count(RID p_region) const;
  103. virtual Vector3 region_get_connection_pathway_start(RID p_region, int p_connection_id) const;
  104. virtual Vector3 region_get_connection_pathway_end(RID p_region, int p_connection_id) const;
  105. virtual RID agent_create() const;
  106. COMMAND_2(agent_set_map, RID, p_agent, RID, p_map);
  107. virtual RID agent_get_map(RID p_agent) const;
  108. COMMAND_2(agent_set_neighbor_dist, RID, p_agent, real_t, p_dist);
  109. COMMAND_2(agent_set_max_neighbors, RID, p_agent, int, p_count);
  110. COMMAND_2(agent_set_time_horizon, RID, p_agent, real_t, p_time);
  111. COMMAND_2(agent_set_radius, RID, p_agent, real_t, p_radius);
  112. COMMAND_2(agent_set_max_speed, RID, p_agent, real_t, p_max_speed);
  113. COMMAND_2(agent_set_velocity, RID, p_agent, Vector3, p_velocity);
  114. COMMAND_2(agent_set_target_velocity, RID, p_agent, Vector3, p_velocity);
  115. COMMAND_2(agent_set_position, RID, p_agent, Vector3, p_position);
  116. COMMAND_2(agent_set_ignore_y, RID, p_agent, bool, p_ignore);
  117. virtual bool agent_is_map_changed(RID p_agent) const;
  118. COMMAND_4_DEF(agent_set_callback, RID, p_agent, ObjectID, p_object_id, StringName, p_method, Variant, p_udata, Variant());
  119. COMMAND_1(free, RID, p_object);
  120. virtual void set_active(bool p_active) const;
  121. void flush_queries();
  122. virtual void process(real_t p_delta_time);
  123. };
  124. #undef COMMAND_1
  125. #undef COMMAND_2
  126. #undef COMMAND_4_DEF
  127. #endif // GODOT_NAVIGATION_SERVER_H