Debian Codegraph @

modules/navigation_2d/nav_region_2d.cpp

raw symbols SCIP
1 /**************************************************************************/
2 /* nav_region_2d.cpp */
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 #include "nav_region_2d.h"
32
33 #include "nav_map_2d.h"
34
35 #include "2d/nav_mesh_queries_2d.h"
36 #include "2d/nav_region_builder_2d.h"
37 #include "2d/nav_region_iteration_2d.h"
38 #include "core/config/project_settings.h"
39
40 using namespace Nav2D;
41
42 void NavRegion2D::set_map(NavMap2D *p_map) {
43 if (map == p_map) {
44 return;
45 }
46
47 cancel_async_thread_join();
48 cancel_sync_request();
49
50 if (map) {
51 map->remove_region(this);
52 }
53
54 map = p_map;
55 iteration_dirty = true;
56
57 if (map) {
58 map->add_region(this);
59 request_sync();
60 if (iteration_build_thread_task_id != WorkerThreadPool::INVALID_TASK_ID) {
61 request_async_thread_join();
62 }
63 }
64 }
65
66 void NavRegion2D::set_enabled(bool p_enabled) {
67 if (enabled == p_enabled) {
68 return;
69 }
70 enabled = p_enabled;
71 iteration_dirty = true;
72
73 request_sync();
74 }
75
76 void NavRegion2D::set_use_edge_connections(bool p_enabled) {
77 if (use_edge_connections != p_enabled) {
78 use_edge_connections = p_enabled;
79 iteration_dirty = true;
80 }
81
82 request_sync();
83 }
84
85 void NavRegion2D::set_transform(Transform2D p_transform) {
86 if (transform == p_transform) {
87 return;
88 }
89 transform = p_transform;
90 iteration_dirty = true;
91
92 request_sync();
93 }
94
95 void NavRegion2D::set_navigation_mesh(Ref<NavigationPolygon> p_navigation_mesh) {
96 #ifdef DEBUG_ENABLED
97 if (map && p_navigation_mesh.is_valid() && GLOBAL_GET_CACHED(bool, "navigation/2d/warnings/navmesh_cell_size_mismatch")) {
98 const double map_cell_size = double(map->get_cell_size());
99 const double navmesh_cell_size = double(p_navigation_mesh->get_cell_size());
100
101 if (map_cell_size > navmesh_cell_size) {
102 WARN_PRINT(vformat("A navigation mesh that uses a `cell_size` of %s was assigned to a navigation map set to a larger `cell_size` of %s.\nThis mismatch in cell size can cause rasterization errors with navigation mesh edges on the navigation map.\nThe cell size for navigation maps can be changed by using the NavigationServer map_set_cell_size() function.\nThe cell size for default navigation maps can also be changed in the project settings.\nThis warning can be toggled under 'navigation/2d/warnings/navmesh_cell_size_mismatch' in the project settings.", navmesh_cell_size, map_cell_size));
103 }
104 }
105 #endif // DEBUG_ENABLED
106
107 navmesh = p_navigation_mesh;
108
109 iteration_dirty = true;
110
111 request_sync();
112 }
113
114 ClosestPointQueryResult NavRegion2D::get_closest_point_info(const Vector2 &p_point) const {
115 RWLockRead read_lock(region_rwlock);
116
117 return NavMeshQueries2D::polygons_get_closest_point_info(get_polygons(), p_point);
118 }
119
120 Vector2 NavRegion2D::get_random_point(uint32_t p_navigation_layers, bool p_uniformly) const {
121 RWLockRead read_lock(region_rwlock);
122
123 if (!get_enabled()) {
124 return Vector2();
125 }
126
127 return NavMeshQueries2D::polygons_get_random_point(get_polygons(), p_navigation_layers, p_uniformly);
128 }
129
130 void NavRegion2D::set_navigation_layers(uint32_t p_navigation_layers) {
131 if (navigation_layers == p_navigation_layers) {
132 return;
133 }
134 navigation_layers = p_navigation_layers;
135 iteration_dirty = true;
136
137 request_sync();
138 }
139
140 void NavRegion2D::set_enter_cost(real_t p_enter_cost) {
141 real_t new_enter_cost = MAX(p_enter_cost, 0.0);
142 if (enter_cost == new_enter_cost) {
143 return;
144 }
145 enter_cost = new_enter_cost;
146 iteration_dirty = true;
147
148 request_sync();
149 }
150
151 void NavRegion2D::set_travel_cost(real_t p_travel_cost) {
152 real_t new_travel_cost = MAX(p_travel_cost, 0.0);
153 if (travel_cost == new_travel_cost) {
154 return;
155 }
156 travel_cost = new_travel_cost;
157 iteration_dirty = true;
158
159 request_sync();
160 }
161
162 void NavRegion2D::set_owner_id(ObjectID p_owner_id) {
163 if (owner_id == p_owner_id) {
164 return;
165 }
166 owner_id = p_owner_id;
167 iteration_dirty = true;
168
169 request_sync();
170 }
171
172 void NavRegion2D::scratch_polygons() {
173 iteration_dirty = true;
174
175 request_sync();
176 }
177
178 real_t NavRegion2D::get_surface_area() const {
179 RWLockRead read_lock(iteration_rwlock);
180 return iteration->get_surface_area();
181 }
182
183 Rect2 NavRegion2D::get_bounds() const {
184 RWLockRead read_lock(iteration_rwlock);
185 return iteration->get_bounds();
186 }
187
188 LocalVector<Nav2D::Polygon> const &NavRegion2D::get_polygons() const {
189 RWLockRead read_lock(iteration_rwlock);
190 return iteration->get_navmesh_polygons();
191 }
192
193 bool NavRegion2D::sync() {
194 bool requires_map_update = false;
195 if (!map) {
196 return requires_map_update;
197 }
198
199 if (iteration_dirty && !iteration_building && !iteration_ready) {
200 _build_iteration();
201 }
202
203 if (iteration_ready) {
204 _sync_iteration();
205 requires_map_update = true;
206 }
207
208 return requires_map_update;
209 }
210
211 void NavRegion2D::sync_async_tasks() {
212 if (iteration_build_thread_task_id != WorkerThreadPool::INVALID_TASK_ID) {
213 if (WorkerThreadPool::get_singleton()->is_task_completed(iteration_build_thread_task_id)) {
214 WorkerThreadPool::get_singleton()->wait_for_task_completion(iteration_build_thread_task_id);
215
216 iteration_build_thread_task_id = WorkerThreadPool::INVALID_TASK_ID;
217 iteration_building = false;
218 iteration_ready = true;
219 request_sync();
220 }
221 }
222 }
223
224 void NavRegion2D::_build_iteration() {
225 if (!iteration_dirty || iteration_building || iteration_ready) {
226 return;
227 }
228
229 iteration_dirty = false;
230 iteration_building = true;
231 iteration_ready = false;
232
233 iteration_build.reset();
234
235 if (navmesh.is_valid()) {
236 navmesh->get_data(iteration_build.navmesh_data.vertices, iteration_build.navmesh_data.polygons);
237 }
238
239 iteration_build.map_cell_size = map->get_merge_rasterizer_cell_size();
240
241 Ref<NavRegionIteration2D> new_iteration;
242 new_iteration.instantiate();
243
244 new_iteration->navigation_layers = get_navigation_layers();
245 new_iteration->enter_cost = get_enter_cost();
246 new_iteration->travel_cost = get_travel_cost();
247 new_iteration->owner_object_id = get_owner_id();
248 new_iteration->owner_type = get_type();
249 new_iteration->owner_rid = get_self();
250 new_iteration->enabled = get_enabled();
251 new_iteration->transform = get_transform();
252 new_iteration->owner_use_edge_connections = get_use_edge_connections();
253
254 iteration_build.region_iteration = new_iteration;
255
256 if (use_async_iterations) {
257 iteration_build_thread_task_id = WorkerThreadPool::get_singleton()->add_native_task(&NavRegion2D::_build_iteration_threaded, &iteration_build, true, SNAME("NavRegionBuilder2D"));
258 request_async_thread_join();
259 } else {
260 NavRegionBuilder2D::build_iteration(iteration_build);
261
262 iteration_building = false;
263 iteration_ready = true;
264 }
265 }
266
267 void NavRegion2D::_build_iteration_threaded(void *p_arg) {
268 NavRegionIterationBuild2D *_iteration_build = static_cast<NavRegionIterationBuild2D *>(p_arg);
269
270 NavRegionBuilder2D::build_iteration(*_iteration_build);
271 }
272
273 void NavRegion2D::_sync_iteration() {
274 if (iteration_building || !iteration_ready) {
275 return;
276 }
277
278 performance_data.pm_polygon_count = iteration_build.performance_data.pm_polygon_count;
279 performance_data.pm_edge_count = iteration_build.performance_data.pm_edge_count;
280 performance_data.pm_edge_merge_count = iteration_build.performance_data.pm_edge_merge_count;
281
282 RWLockWrite write_lock(iteration_rwlock);
283 ERR_FAIL_COND(iteration.is_null());
284 iteration = Ref<NavRegionIteration2D>();
285 DEV_ASSERT(iteration.is_null());
286 iteration = iteration_build.region_iteration;
287 iteration_build.region_iteration = Ref<NavRegionIteration2D>();
288 DEV_ASSERT(iteration_build.region_iteration.is_null());
289 iteration_id = iteration_id % UINT32_MAX + 1;
290
291 iteration_ready = false;
292
293 cancel_async_thread_join();
294 }
295
296 Ref<NavRegionIteration2D> NavRegion2D::get_iteration() {
297 RWLockRead read_lock(iteration_rwlock);
298 return iteration;
299 }
300
301 void NavRegion2D::request_async_thread_join() {
302 DEV_ASSERT(map);
303 if (map && !async_list_element.in_list()) {
304 map->add_region_async_thread_join_request(&async_list_element);
305 }
306 }
307
308 void NavRegion2D::cancel_async_thread_join() {
309 if (map && async_list_element.in_list()) {
310 map->remove_region_async_thread_join_request(&async_list_element);
311 }
312 }
313
314 void NavRegion2D::request_sync() {
315 if (map && !sync_dirty_request_list_element.in_list()) {
316 map->add_region_sync_dirty_request(&sync_dirty_request_list_element);
317 }
318 }
319
320 void NavRegion2D::cancel_sync_request() {
321 if (map && sync_dirty_request_list_element.in_list()) {
322 map->remove_region_sync_dirty_request(&sync_dirty_request_list_element);
323 }
324 }
325
326 void NavRegion2D::set_use_async_iterations(bool p_enabled) {
327 if (use_async_iterations == p_enabled) {
328 return;
329 }
330 #ifdef THREADS_ENABLED
331 use_async_iterations = p_enabled;
332 #endif
333 }
334
335 bool NavRegion2D::get_use_async_iterations() const {
336 return use_async_iterations;
337 }
338
339 NavRegion2D::NavRegion2D() :
340 sync_dirty_request_list_element(this), async_list_element(this) {
341 type = NavigationEnums2D::PathSegmentType::PATH_SEGMENT_TYPE_REGION;
342 iteration_build.region = this;
343 iteration.instantiate();
344
345 #ifdef THREADS_ENABLED
346 use_async_iterations = GLOBAL_GET("navigation/world/region_use_async_iterations");
347 #else
348 use_async_iterations = false;
349 #endif
350 }
351
352 NavRegion2D::~NavRegion2D() {
353 cancel_async_thread_join();
354 cancel_sync_request();
355
356 if (iteration_build_thread_task_id != WorkerThreadPool::INVALID_TASK_ID) {
357 WorkerThreadPool::get_singleton()->wait_for_task_completion(iteration_build_thread_task_id);
358 iteration_build_thread_task_id = WorkerThreadPool::INVALID_TASK_ID;
359 }
360
361 iteration_build.region = nullptr;
362 iteration_build.region_iteration = Ref<NavRegionIteration2D>();
363 iteration = Ref<NavRegionIteration2D>();
364 }