| 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 |  | 
|---|
| 31 | #ifndef NAV_MAP_H | 
|---|
| 32 | #define NAV_MAP_H | 
|---|
| 33 |  | 
|---|
| 34 | #include "nav_rid.h" | 
|---|
| 35 | #include "nav_utils.h" | 
|---|
| 36 |  | 
|---|
| 37 | #include "core/math/math_defs.h" | 
|---|
| 38 | #include "core/object/worker_thread_pool.h" | 
|---|
| 39 |  | 
|---|
| 40 | #include <KdTree2d.h> | 
|---|
| 41 | #include <KdTree3d.h> | 
|---|
| 42 | #include <RVOSimulator2d.h> | 
|---|
| 43 | #include <RVOSimulator3d.h> | 
|---|
| 44 |  | 
|---|
| 45 | class NavLink; | 
|---|
| 46 | class NavRegion; | 
|---|
| 47 | class NavAgent; | 
|---|
| 48 | class NavObstacle; | 
|---|
| 49 |  | 
|---|
| 50 | class NavMap : public NavRid { | 
|---|
| 51 | /// Map Up | 
|---|
| 52 | Vector3 up = Vector3(0, 1, 0); | 
|---|
| 53 |  | 
|---|
| 54 | /// To find the polygons edges the vertices are displaced in a grid where | 
|---|
| 55 | /// each cell has the following cell_size and cell_height. | 
|---|
| 56 | real_t cell_size = 0.25; // Must match ProjectSettings default 3D cell_size and NavigationMesh cell_size. | 
|---|
| 57 | real_t cell_height = 0.25; // Must match ProjectSettings default 3D cell_height and NavigationMesh cell_height. | 
|---|
| 58 |  | 
|---|
| 59 | bool use_edge_connections = true; | 
|---|
| 60 | /// This value is used to detect the near edges to connect. | 
|---|
| 61 | real_t edge_connection_margin = 0.25; | 
|---|
| 62 |  | 
|---|
| 63 | /// This value is used to limit how far links search to find polygons to connect to. | 
|---|
| 64 | real_t link_connection_radius = 1.0; | 
|---|
| 65 |  | 
|---|
| 66 | bool regenerate_polygons = true; | 
|---|
| 67 | bool regenerate_links = true; | 
|---|
| 68 |  | 
|---|
| 69 | /// Map regions | 
|---|
| 70 | LocalVector<NavRegion *> regions; | 
|---|
| 71 |  | 
|---|
| 72 | /// Map links | 
|---|
| 73 | LocalVector<NavLink *> links; | 
|---|
| 74 | LocalVector<gd::Polygon> link_polygons; | 
|---|
| 75 |  | 
|---|
| 76 | /// Map polygons | 
|---|
| 77 | LocalVector<gd::Polygon> polygons; | 
|---|
| 78 |  | 
|---|
| 79 | /// RVO avoidance worlds | 
|---|
| 80 | RVO2D::RVOSimulator2D rvo_simulation_2d; | 
|---|
| 81 | RVO3D::RVOSimulator3D rvo_simulation_3d; | 
|---|
| 82 |  | 
|---|
| 83 | /// avoidance controlled agents | 
|---|
| 84 | LocalVector<NavAgent *> active_2d_avoidance_agents; | 
|---|
| 85 | LocalVector<NavAgent *> active_3d_avoidance_agents; | 
|---|
| 86 |  | 
|---|
| 87 | /// dirty flag when one of the agent's arrays are modified | 
|---|
| 88 | bool agents_dirty = true; | 
|---|
| 89 |  | 
|---|
| 90 | /// All the Agents (even the controlled one) | 
|---|
| 91 | LocalVector<NavAgent *> agents; | 
|---|
| 92 |  | 
|---|
| 93 | /// All the avoidance obstacles (both static and dynamic) | 
|---|
| 94 | LocalVector<NavObstacle *> obstacles; | 
|---|
| 95 |  | 
|---|
| 96 | /// Are rvo obstacles modified? | 
|---|
| 97 | bool obstacles_dirty = true; | 
|---|
| 98 |  | 
|---|
| 99 | /// Physics delta time | 
|---|
| 100 | real_t deltatime = 0.0; | 
|---|
| 101 |  | 
|---|
| 102 | /// Change the id each time the map is updated. | 
|---|
| 103 | uint32_t map_update_id = 0; | 
|---|
| 104 |  | 
|---|
| 105 | bool use_threads = true; | 
|---|
| 106 | bool avoidance_use_multiple_threads = true; | 
|---|
| 107 | bool avoidance_use_high_priority_threads = true; | 
|---|
| 108 |  | 
|---|
| 109 | // Performance Monitor | 
|---|
| 110 | int pm_region_count = 0; | 
|---|
| 111 | int pm_agent_count = 0; | 
|---|
| 112 | int pm_link_count = 0; | 
|---|
| 113 | int pm_polygon_count = 0; | 
|---|
| 114 | int pm_edge_count = 0; | 
|---|
| 115 | int pm_edge_merge_count = 0; | 
|---|
| 116 | int pm_edge_connection_count = 0; | 
|---|
| 117 | int pm_edge_free_count = 0; | 
|---|
| 118 |  | 
|---|
| 119 | public: | 
|---|
| 120 | NavMap(); | 
|---|
| 121 | ~NavMap(); | 
|---|
| 122 |  | 
|---|
| 123 | void set_up(Vector3 p_up); | 
|---|
| 124 | Vector3 get_up() const { | 
|---|
| 125 | return up; | 
|---|
| 126 | } | 
|---|
| 127 |  | 
|---|
| 128 | void set_cell_size(real_t p_cell_size); | 
|---|
| 129 | real_t get_cell_size() const { | 
|---|
| 130 | return cell_size; | 
|---|
| 131 | } | 
|---|
| 132 |  | 
|---|
| 133 | void set_cell_height(real_t p_cell_height); | 
|---|
| 134 | real_t get_cell_height() const { return cell_height; } | 
|---|
| 135 |  | 
|---|
| 136 | void set_use_edge_connections(bool p_enabled); | 
|---|
| 137 | bool get_use_edge_connections() const { | 
|---|
| 138 | return use_edge_connections; | 
|---|
| 139 | } | 
|---|
| 140 |  | 
|---|
| 141 | void set_edge_connection_margin(real_t p_edge_connection_margin); | 
|---|
| 142 | real_t get_edge_connection_margin() const { | 
|---|
| 143 | return edge_connection_margin; | 
|---|
| 144 | } | 
|---|
| 145 |  | 
|---|
| 146 | void set_link_connection_radius(real_t p_link_connection_radius); | 
|---|
| 147 | real_t get_link_connection_radius() const { | 
|---|
| 148 | return link_connection_radius; | 
|---|
| 149 | } | 
|---|
| 150 |  | 
|---|
| 151 | gd::PointKey get_point_key(const Vector3 &p_pos) const; | 
|---|
| 152 |  | 
|---|
| 153 | 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; | 
|---|
| 154 | Vector3 get_closest_point_to_segment(const Vector3 &p_from, const Vector3 &p_to, const bool p_use_collision) const; | 
|---|
| 155 | Vector3 get_closest_point(const Vector3 &p_point) const; | 
|---|
| 156 | Vector3 get_closest_point_normal(const Vector3 &p_point) const; | 
|---|
| 157 | gd::ClosestPointQueryResult get_closest_point_info(const Vector3 &p_point) const; | 
|---|
| 158 | RID get_closest_point_owner(const Vector3 &p_point) const; | 
|---|
| 159 |  | 
|---|
| 160 | void add_region(NavRegion *p_region); | 
|---|
| 161 | void remove_region(NavRegion *p_region); | 
|---|
| 162 | const LocalVector<NavRegion *> &get_regions() const { | 
|---|
| 163 | return regions; | 
|---|
| 164 | } | 
|---|
| 165 |  | 
|---|
| 166 | void add_link(NavLink *p_link); | 
|---|
| 167 | void remove_link(NavLink *p_link); | 
|---|
| 168 | const LocalVector<NavLink *> &get_links() const { | 
|---|
| 169 | return links; | 
|---|
| 170 | } | 
|---|
| 171 |  | 
|---|
| 172 | bool has_agent(NavAgent *agent) const; | 
|---|
| 173 | void add_agent(NavAgent *agent); | 
|---|
| 174 | void remove_agent(NavAgent *agent); | 
|---|
| 175 | const LocalVector<NavAgent *> &get_agents() const { | 
|---|
| 176 | return agents; | 
|---|
| 177 | } | 
|---|
| 178 |  | 
|---|
| 179 | void set_agent_as_controlled(NavAgent *agent); | 
|---|
| 180 | void remove_agent_as_controlled(NavAgent *agent); | 
|---|
| 181 |  | 
|---|
| 182 | bool has_obstacle(NavObstacle *obstacle) const; | 
|---|
| 183 | void add_obstacle(NavObstacle *obstacle); | 
|---|
| 184 | void remove_obstacle(NavObstacle *obstacle); | 
|---|
| 185 | const LocalVector<NavObstacle *> &get_obstacles() const { | 
|---|
| 186 | return obstacles; | 
|---|
| 187 | } | 
|---|
| 188 |  | 
|---|
| 189 | uint32_t get_map_update_id() const { | 
|---|
| 190 | return map_update_id; | 
|---|
| 191 | } | 
|---|
| 192 |  | 
|---|
| 193 | void sync(); | 
|---|
| 194 | void step(real_t p_deltatime); | 
|---|
| 195 | void dispatch_callbacks(); | 
|---|
| 196 |  | 
|---|
| 197 | // Performance Monitor | 
|---|
| 198 | int get_pm_region_count() const { return pm_region_count; } | 
|---|
| 199 | int get_pm_agent_count() const { return pm_agent_count; } | 
|---|
| 200 | int get_pm_link_count() const { return pm_link_count; } | 
|---|
| 201 | int get_pm_polygon_count() const { return pm_polygon_count; } | 
|---|
| 202 | int get_pm_edge_count() const { return pm_edge_count; } | 
|---|
| 203 | int get_pm_edge_merge_count() const { return pm_edge_merge_count; } | 
|---|
| 204 | int get_pm_edge_connection_count() const { return pm_edge_connection_count; } | 
|---|
| 205 | int get_pm_edge_free_count() const { return pm_edge_free_count; } | 
|---|
| 206 |  | 
|---|
| 207 | private: | 
|---|
| 208 | void compute_single_step(uint32_t index, NavAgent **agent); | 
|---|
| 209 |  | 
|---|
| 210 | void compute_single_avoidance_step_2d(uint32_t index, NavAgent **agent); | 
|---|
| 211 | void compute_single_avoidance_step_3d(uint32_t index, NavAgent **agent); | 
|---|
| 212 |  | 
|---|
| 213 | 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; | 
|---|
| 214 | void _update_rvo_simulation(); | 
|---|
| 215 | void _update_rvo_obstacles_tree_2d(); | 
|---|
| 216 | void _update_rvo_agents_tree_2d(); | 
|---|
| 217 | void _update_rvo_agents_tree_3d(); | 
|---|
| 218 | }; | 
|---|
| 219 |  | 
|---|
| 220 | #endif // NAV_MAP_H | 
|---|
| 221 |  | 
|---|