Debian Codegraph @

modules/navigation_2d/nav_map_2d.h

raw symbols SCIP
1 /**************************************************************************/
2 /* nav_map_2d.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 #pragma once
32
33 #include "2d/nav_map_iteration_2d.h"
34 #include "2d/nav_mesh_queries_2d.h"
35 #include "nav_rid_2d.h"
36 #include "nav_utils_2d.h"
37
38 #include "core/math/math_defs.h"
39 #include "core/object/worker_thread_pool.h"
40 #include "servers/navigation_2d/navigation_constants_2d.h"
41
42 #include <KdTree2d.h>
43 #include <RVOSimulator2d.h>
44
45 class NavLink2D;
46 class NavRegion2D;
47 class NavAgent2D;
48 class NavObstacle2D;
49
50 class NavMap2D : public NavRid2D {
51 /// To find the polygons edges the vertices are displaced in a grid where
52 /// each cell has the following cell_size.
53 real_t cell_size = NavigationDefaults2D::NAV_MESH_CELL_SIZE;
54
55 // For the inter-region merging to work, internal rasterization is performed.
56 Vector2 merge_rasterizer_cell_size = Vector2(cell_size, cell_size);
57
58 // This value is used to control sensitivity of internal rasterizer.
59 float merge_rasterizer_cell_scale = 0.1;
60
61 bool use_edge_connections = true;
62 /// This value is used to detect the near edges to connect.
63 real_t edge_connection_margin = NavigationDefaults2D::EDGE_CONNECTION_MARGIN;
64
65 /// This value is used to limit how far links search to find polygons to connect to.
66 real_t link_connection_radius = NavigationDefaults2D::LINK_CONNECTION_RADIUS;
67
68 bool map_settings_dirty = true;
69
70 /// Map regions.
71 LocalVector<NavRegion2D *> regions;
72
73 /// Map links.
74 LocalVector<NavLink2D *> links;
75
76 /// RVO avoidance world.
77 RVO2D::RVOSimulator2D rvo_simulation;
78
79 /// Avoidance controlled agents.
80 LocalVector<NavAgent2D *> active_avoidance_agents;
81
82 /// dirty flag when one of the agent's arrays are modified.
83 bool agents_dirty = true;
84
85 /// All the Agents (even the controlled one).
86 LocalVector<NavAgent2D *> agents;
87
88 /// All the avoidance obstacles (both static and dynamic).
89 LocalVector<NavObstacle2D *> obstacles;
90
91 /// Are rvo obstacles modified?
92 bool obstacles_dirty = true;
93
94 /// Change the id each time the map is updated.
95 uint32_t iteration_id = 0;
96
97 bool use_threads = true;
98 bool avoidance_use_multiple_threads = true;
99 bool avoidance_use_high_priority_threads = true;
100
101 // Performance Monitor
102 Nav2D::PerformanceData performance_data;
103
104 struct {
105 struct {
106 RWLock rwlock;
107 SelfList<NavRegion2D>::List list;
108 } regions;
109 struct {
110 RWLock rwlock;
111 SelfList<NavLink2D>::List list;
112 } links;
113 struct {
114 RWLock rwlock;
115 SelfList<NavAgent2D>::List list;
116 } agents;
117 struct {
118 RWLock rwlock;
119 SelfList<NavObstacle2D>::List list;
120 } obstacles;
121 } sync_dirty_requests;
122
123 struct {
124 struct {
125 RWLock rwlock;
126 SelfList<NavRegion2D>::List list;
127 } regions;
128 } async_dirty_requests;
129
130 int path_query_slots_max = 4;
131
132 bool use_async_iterations = true;
133
134 uint32_t iteration_slot_index = 0;
135 LocalVector<NavMapIteration2D> iteration_slots;
136 mutable RWLock iteration_slot_rwlock;
137
138 NavMapIterationBuild2D iteration_build;
139 WorkerThreadPool::TaskID iteration_build_thread_task_id = WorkerThreadPool::INVALID_TASK_ID;
140 static void _build_iteration_threaded(void *p_arg);
141
142 bool iteration_dirty = true;
143 bool iteration_building = false;
144 bool iteration_ready = false;
145
146 void _build_iteration();
147 void _sync_iteration();
148
149 public:
150 NavMap2D();
151 ~NavMap2D();
152
153 uint32_t get_iteration_id() const { return iteration_id; }
154
155 void set_cell_size(real_t p_cell_size);
156 real_t get_cell_size() const {
157 return cell_size;
158 }
159
160 void set_merge_rasterizer_cell_scale(float p_value);
161 float get_merge_rasterizer_cell_scale() const {
162 return merge_rasterizer_cell_scale;
163 }
164
165 void set_use_edge_connections(bool p_enabled);
166 bool get_use_edge_connections() const {
167 return use_edge_connections;
168 }
169
170 void set_edge_connection_margin(real_t p_edge_connection_margin);
171 real_t get_edge_connection_margin() const {
172 return edge_connection_margin;
173 }
174
175 void set_link_connection_radius(real_t p_link_connection_radius);
176 real_t get_link_connection_radius() const {
177 return link_connection_radius;
178 }
179
180 Nav2D::PointKey get_point_key(const Vector2 &p_pos) const;
181 const Vector2 &get_merge_rasterizer_cell_size() const;
182
183 void query_path(NavMeshQueries2D::NavMeshPathQueryTask2D &p_query_task);
184
185 Vector2 get_closest_point(const Vector2 &p_point) const;
186 Nav2D::ClosestPointQueryResult get_closest_point_info(const Vector2 &p_point) const;
187 RID get_closest_point_owner(const Vector2 &p_point) const;
188
189 void add_region(NavRegion2D *p_region);
190 void remove_region(NavRegion2D *p_region);
191 const LocalVector<NavRegion2D *> &get_regions() const {
192 return regions;
193 }
194
195 void add_link(NavLink2D *p_link);
196 void remove_link(NavLink2D *p_link);
197 const LocalVector<NavLink2D *> &get_links() const {
198 return links;
199 }
200
201 bool has_agent(NavAgent2D *p_agent) const;
202 void add_agent(NavAgent2D *p_agent);
203 void remove_agent(NavAgent2D *p_agent);
204 const LocalVector<NavAgent2D *> &get_agents() const {
205 return agents;
206 }
207
208 void set_agent_as_controlled(NavAgent2D *p_agent);
209 void remove_agent_as_controlled(NavAgent2D *p_agent);
210
211 bool has_obstacle(NavObstacle2D *p_obstacle) const;
212 void add_obstacle(NavObstacle2D *p_obstacle);
213 void remove_obstacle(NavObstacle2D *p_obstacle);
214 const LocalVector<NavObstacle2D *> &get_obstacles() const {
215 return obstacles;
216 }
217
218 Vector2 get_random_point(uint32_t p_navigation_layers, bool p_uniformly) const;
219
220 void sync();
221 void step(double p_delta_time);
222 void dispatch_callbacks();
223
224 // Performance Monitor
225 int get_pm_region_count() const { return performance_data.pm_region_count; }
226 int get_pm_agent_count() const { return performance_data.pm_agent_count; }
227 int get_pm_link_count() const { return performance_data.pm_link_count; }
228 int get_pm_polygon_count() const { return performance_data.pm_polygon_count; }
229 int get_pm_edge_count() const { return performance_data.pm_edge_count; }
230 int get_pm_edge_merge_count() const { return performance_data.pm_edge_merge_count; }
231 int get_pm_edge_connection_count() const { return performance_data.pm_edge_connection_count; }
232 int get_pm_edge_free_count() const { return performance_data.pm_edge_free_count; }
233 int get_pm_obstacle_count() const { return performance_data.pm_obstacle_count; }
234
235 int get_region_connections_count(NavRegion2D *p_region) const;
236 Vector2 get_region_connection_pathway_start(NavRegion2D *p_region, int p_connection_id) const;
237 Vector2 get_region_connection_pathway_end(NavRegion2D *p_region, int p_connection_id) const;
238
239 void add_region_async_thread_join_request(SelfList<NavRegion2D> *p_async_request);
240 void remove_region_async_thread_join_request(SelfList<NavRegion2D> *p_async_request);
241
242 void add_region_sync_dirty_request(SelfList<NavRegion2D> *p_sync_request);
243 void add_link_sync_dirty_request(SelfList<NavLink2D> *p_sync_request);
244 void add_agent_sync_dirty_request(SelfList<NavAgent2D> *p_sync_request);
245 void add_obstacle_sync_dirty_request(SelfList<NavObstacle2D> *p_sync_request);
246
247 void remove_region_sync_dirty_request(SelfList<NavRegion2D> *p_sync_request);
248 void remove_link_sync_dirty_request(SelfList<NavLink2D> *p_sync_request);
249 void remove_agent_sync_dirty_request(SelfList<NavAgent2D> *p_sync_request);
250 void remove_obstacle_sync_dirty_request(SelfList<NavObstacle2D> *p_sync_request);
251
252 void set_use_async_iterations(bool p_enabled);
253 bool get_use_async_iterations() const;
254
255 private:
256 void _sync_dirty_map_update_requests();
257 void _sync_dirty_avoidance_update_requests();
258 void _sync_async_tasks();
259
260 void compute_single_step(uint32_t p_index, NavAgent2D **p_agent);
261
262 void compute_single_avoidance_step(uint32_t p_index, NavAgent2D **p_agent);
263
264 void _sync_avoidance();
265 void _update_rvo_simulation();
266 void _update_rvo_obstacles_tree();
267 void _update_rvo_agents_tree();
268
269 void _update_merge_rasterizer_cell_dimensions();
270 };