gd_navigation_server.h 6.5 KB

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