nav_map.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. /**************************************************************************/
  2. /* nav_map.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 NAV_MAP_H
  31. #define NAV_MAP_H
  32. #include "nav_rid.h"
  33. #include "core/math/math_defs.h"
  34. #include "core/object/worker_thread_pool.h"
  35. #include "core/templates/rb_map.h"
  36. #include "nav_utils.h"
  37. #include <KdTree.h>
  38. class NavLink;
  39. class NavRegion;
  40. class NavAgent;
  41. class NavMap : public NavRid {
  42. /// Map Up
  43. Vector3 up = Vector3(0, 1, 0);
  44. /// To find the polygons edges the vertices are displaced in a grid where
  45. /// each cell has the following cell_size.
  46. real_t cell_size = 0.25;
  47. /// This value is used to detect the near edges to connect.
  48. real_t edge_connection_margin = 0.25;
  49. /// This value is used to limit how far links search to find polygons to connect to.
  50. real_t link_connection_radius = 1.0;
  51. bool regenerate_polygons = true;
  52. bool regenerate_links = true;
  53. /// Map regions
  54. LocalVector<NavRegion *> regions;
  55. /// Map links
  56. LocalVector<NavLink *> links;
  57. LocalVector<gd::Polygon> link_polygons;
  58. /// Map polygons
  59. LocalVector<gd::Polygon> polygons;
  60. /// Rvo world
  61. RVO::KdTree rvo;
  62. /// Is agent array modified?
  63. bool agents_dirty = false;
  64. /// All the Agents (even the controlled one)
  65. LocalVector<NavAgent *> agents;
  66. /// Controlled agents
  67. LocalVector<NavAgent *> controlled_agents;
  68. /// Physics delta time
  69. real_t deltatime = 0.0;
  70. /// Change the id each time the map is updated.
  71. uint32_t map_update_id = 0;
  72. // Performance Monitor
  73. int pm_region_count = 0;
  74. int pm_agent_count = 0;
  75. int pm_link_count = 0;
  76. int pm_polygon_count = 0;
  77. int pm_edge_count = 0;
  78. int pm_edge_merge_count = 0;
  79. int pm_edge_connection_count = 0;
  80. int pm_edge_free_count = 0;
  81. public:
  82. NavMap();
  83. ~NavMap();
  84. void set_up(Vector3 p_up);
  85. Vector3 get_up() const {
  86. return up;
  87. }
  88. void set_cell_size(float p_cell_size);
  89. float get_cell_size() const {
  90. return cell_size;
  91. }
  92. void set_edge_connection_margin(float p_edge_connection_margin);
  93. float get_edge_connection_margin() const {
  94. return edge_connection_margin;
  95. }
  96. void set_link_connection_radius(float p_link_connection_radius);
  97. float get_link_connection_radius() const {
  98. return link_connection_radius;
  99. }
  100. gd::PointKey get_point_key(const Vector3 &p_pos) const;
  101. Vector<Vector3> get_path(Vector3 p_origin, Vector3 p_destination, bool p_optimize, uint32_t p_navigation_layers, Vector<int32_t> *r_path_types, TypedArray<RID> *r_path_rids, Vector<int64_t> *r_path_owners) const;
  102. Vector3 get_closest_point_to_segment(const Vector3 &p_from, const Vector3 &p_to, const bool p_use_collision) const;
  103. Vector3 get_closest_point(const Vector3 &p_point) const;
  104. Vector3 get_closest_point_normal(const Vector3 &p_point) const;
  105. gd::ClosestPointQueryResult get_closest_point_info(const Vector3 &p_point) const;
  106. RID get_closest_point_owner(const Vector3 &p_point) const;
  107. void add_region(NavRegion *p_region);
  108. void remove_region(NavRegion *p_region);
  109. const LocalVector<NavRegion *> &get_regions() const {
  110. return regions;
  111. }
  112. void add_link(NavLink *p_link);
  113. void remove_link(NavLink *p_link);
  114. const LocalVector<NavLink *> &get_links() const {
  115. return links;
  116. }
  117. bool has_agent(NavAgent *agent) const;
  118. void add_agent(NavAgent *agent);
  119. void remove_agent(NavAgent *agent);
  120. const LocalVector<NavAgent *> &get_agents() const {
  121. return agents;
  122. }
  123. void set_agent_as_controlled(NavAgent *agent);
  124. void remove_agent_as_controlled(NavAgent *agent);
  125. uint32_t get_map_update_id() const {
  126. return map_update_id;
  127. }
  128. void sync();
  129. void step(real_t p_deltatime);
  130. void dispatch_callbacks();
  131. // Performance Monitor
  132. int get_pm_region_count() const { return pm_region_count; }
  133. int get_pm_agent_count() const { return pm_agent_count; }
  134. int get_pm_link_count() const { return pm_link_count; }
  135. int get_pm_polygon_count() const { return pm_polygon_count; }
  136. int get_pm_edge_count() const { return pm_edge_count; }
  137. int get_pm_edge_merge_count() const { return pm_edge_merge_count; }
  138. int get_pm_edge_connection_count() const { return pm_edge_connection_count; }
  139. int get_pm_edge_free_count() const { return pm_edge_free_count; }
  140. private:
  141. void compute_single_step(uint32_t index, NavAgent **agent);
  142. void clip_path(const LocalVector<gd::NavigationPoly> &p_navigation_polys, Vector<Vector3> &path, const gd::NavigationPoly *from_poly, const Vector3 &p_to_point, const gd::NavigationPoly *p_to_poly, Vector<int32_t> *r_path_types, TypedArray<RID> *r_path_rids, Vector<int64_t> *r_path_owners) const;
  143. };
  144. #endif // NAV_MAP_H