Debian Codegraph @

modules/navigation_3d/nav_map_3d.cpp

raw symbols SCIP
1 /**************************************************************************/
2 /* nav_map_3d.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_map_3d.h"
32
33 #include "3d/nav_map_builder_3d.h"
34 #include "3d/nav_mesh_queries_3d.h"
35 #include "3d/nav_region_iteration_3d.h"
36 #include "nav_agent_3d.h"
37 #include "nav_link_3d.h"
38 #include "nav_obstacle_3d.h"
39 #include "nav_region_3d.h"
40
41 #include "core/config/project_settings.h"
42 #include "core/object/worker_thread_pool.h"
43 #include "servers/navigation_3d/navigation_server_3d.h"
44
45 #include <Obstacle2d.h>
46
47 using namespace Nav3D;
48
49 #ifdef DEBUG_ENABLED
50 #define NAVMAP_ITERATION_ZERO_ERROR_MSG() \
51 ERR_PRINT_ONCE("NavigationServer navigation map query failed because it was made before first map synchronization.\n\
52 NavigationServer 'map_changed' signal can be used to receive update notifications.\n\
53 NavigationServer 'map_get_iteration_id()' can be used to check if a map has finished its newest iteration.");
54 #else
55 #define NAVMAP_ITERATION_ZERO_ERROR_MSG()
56 #endif // DEBUG_ENABLED
57
58 #define GET_MAP_ITERATION() \
59 iteration_slot_rwlock.read_lock(); \
60 NavMapIteration3D &map_iteration = iteration_slots[iteration_slot_index]; \
61 NavMapIterationRead3D iteration_read_lock(map_iteration); \
62 iteration_slot_rwlock.read_unlock();
63
64 #define GET_MAP_ITERATION_CONST() \
65 iteration_slot_rwlock.read_lock(); \
66 const NavMapIteration3D &map_iteration = iteration_slots[iteration_slot_index]; \
67 NavMapIterationRead3D iteration_read_lock(map_iteration); \
68 iteration_slot_rwlock.read_unlock();
69
70 void NavMap3D::set_up(Vector3 p_up) {
71 if (up == p_up) {
72 return;
73 }
74 up = p_up;
75 map_settings_dirty = true;
76 }
77
78 void NavMap3D::set_cell_size(real_t p_cell_size) {
79 if (cell_size == p_cell_size) {
80 return;
81 }
82 cell_size = MAX(p_cell_size, NavigationDefaults3D::NAV_MESH_CELL_SIZE_MIN);
83 _update_merge_rasterizer_cell_dimensions();
84 map_settings_dirty = true;
85 }
86
87 void NavMap3D::set_cell_height(real_t p_cell_height) {
88 if (cell_height == p_cell_height) {
89 return;
90 }
91 cell_height = MAX(p_cell_height, NavigationDefaults3D::NAV_MESH_CELL_SIZE_MIN);
92 _update_merge_rasterizer_cell_dimensions();
93 map_settings_dirty = true;
94 }
95
96 void NavMap3D::set_merge_rasterizer_cell_scale(float p_value) {
97 if (merge_rasterizer_cell_scale == p_value) {
98 return;
99 }
100 merge_rasterizer_cell_scale = MAX(MIN(p_value, 0.1), NavigationDefaults3D::NAV_MESH_CELL_SIZE_MIN);
101 _update_merge_rasterizer_cell_dimensions();
102 map_settings_dirty = true;
103 }
104
105 void NavMap3D::set_use_edge_connections(bool p_enabled) {
106 if (use_edge_connections == p_enabled) {
107 return;
108 }
109 use_edge_connections = p_enabled;
110 iteration_dirty = true;
111 }
112
113 void NavMap3D::set_edge_connection_margin(real_t p_edge_connection_margin) {
114 if (edge_connection_margin == p_edge_connection_margin) {
115 return;
116 }
117 edge_connection_margin = p_edge_connection_margin;
118 iteration_dirty = true;
119 }
120
121 void NavMap3D::set_link_connection_radius(real_t p_link_connection_radius) {
122 if (link_connection_radius == p_link_connection_radius) {
123 return;
124 }
125 link_connection_radius = p_link_connection_radius;
126 iteration_dirty = true;
127 }
128
129 const Vector3 &NavMap3D::get_merge_rasterizer_cell_size() const {
130 return merge_rasterizer_cell_size;
131 }
132
133 PointKey NavMap3D::get_point_key(const Vector3 &p_pos) const {
134 const int x = static_cast<int>(Math::floor(p_pos.x / merge_rasterizer_cell_size.x));
135 const int y = static_cast<int>(Math::floor(p_pos.y / merge_rasterizer_cell_size.y));
136 const int z = static_cast<int>(Math::floor(p_pos.z / merge_rasterizer_cell_size.z));
137
138 PointKey p;
139 p.key = 0;
140 p.x = x;
141 p.y = y;
142 p.z = z;
143 return p;
144 }
145
146 void NavMap3D::query_path(NavMeshQueries3D::NavMeshPathQueryTask3D &p_query_task) {
147 if (iteration_id == 0) {
148 return;
149 }
150
151 GET_MAP_ITERATION();
152
153 map_iteration.path_query_slots_semaphore.wait();
154
155 map_iteration.path_query_slots_mutex.lock();
156 for (NavMeshQueries3D::PathQuerySlot &p_path_query_slot : map_iteration.path_query_slots) {
157 if (!p_path_query_slot.in_use) {
158 p_path_query_slot.in_use = true;
159 p_query_task.path_query_slot = &p_path_query_slot;
160 break;
161 }
162 }
163 map_iteration.path_query_slots_mutex.unlock();
164
165 if (p_query_task.path_query_slot == nullptr) {
166 map_iteration.path_query_slots_semaphore.post();
167 ERR_FAIL_NULL_MSG(p_query_task.path_query_slot, "No unused NavMap3D path query slot found! This should never happen :(.");
168 }
169
170 p_query_task.map_up = map_iteration.map_up;
171
172 NavMeshQueries3D::query_task_map_iteration_get_path(p_query_task, map_iteration);
173
174 map_iteration.path_query_slots_mutex.lock();
175 uint32_t used_slot_index = p_query_task.path_query_slot->slot_index;
176 map_iteration.path_query_slots[used_slot_index].in_use = false;
177 p_query_task.path_query_slot = nullptr;
178 map_iteration.path_query_slots_mutex.unlock();
179
180 map_iteration.path_query_slots_semaphore.post();
181 }
182
183 Vector3 NavMap3D::get_closest_point_to_segment(const Vector3 &p_from, const Vector3 &p_to, const bool p_use_collision) const {
184 if (iteration_id == 0) {
185 NAVMAP_ITERATION_ZERO_ERROR_MSG();
186 return Vector3();
187 }
188
189 GET_MAP_ITERATION_CONST();
190
191 return NavMeshQueries3D::map_iteration_get_closest_point_to_segment(map_iteration, p_from, p_to, p_use_collision);
192 }
193
194 Vector3 NavMap3D::get_closest_point(const Vector3 &p_point) const {
195 if (iteration_id == 0) {
196 NAVMAP_ITERATION_ZERO_ERROR_MSG();
197 return Vector3();
198 }
199
200 GET_MAP_ITERATION_CONST();
201
202 return NavMeshQueries3D::map_iteration_get_closest_point(map_iteration, p_point);
203 }
204
205 Vector3 NavMap3D::get_closest_point_normal(const Vector3 &p_point) const {
206 if (iteration_id == 0) {
207 NAVMAP_ITERATION_ZERO_ERROR_MSG();
208 return Vector3();
209 }
210
211 GET_MAP_ITERATION_CONST();
212
213 return NavMeshQueries3D::map_iteration_get_closest_point_normal(map_iteration, p_point);
214 }
215
216 RID NavMap3D::get_closest_point_owner(const Vector3 &p_point) const {
217 if (iteration_id == 0) {
218 NAVMAP_ITERATION_ZERO_ERROR_MSG();
219 return RID();
220 }
221
222 GET_MAP_ITERATION_CONST();
223
224 return NavMeshQueries3D::map_iteration_get_closest_point_owner(map_iteration, p_point);
225 }
226
227 ClosestPointQueryResult NavMap3D::get_closest_point_info(const Vector3 &p_point) const {
228 GET_MAP_ITERATION_CONST();
229
230 return NavMeshQueries3D::map_iteration_get_closest_point_info(map_iteration, p_point);
231 }
232
233 void NavMap3D::add_region(NavRegion3D *p_region) {
234 DEV_ASSERT(!regions.has(p_region));
235
236 regions.push_back(p_region);
237 iteration_dirty = true;
238 }
239
240 void NavMap3D::remove_region(NavRegion3D *p_region) {
241 if (regions.erase_unordered(p_region)) {
242 iteration_dirty = true;
243 }
244 }
245
246 void NavMap3D::add_link(NavLink3D *p_link) {
247 DEV_ASSERT(!links.has(p_link));
248
249 links.push_back(p_link);
250 iteration_dirty = true;
251 }
252
253 void NavMap3D::remove_link(NavLink3D *p_link) {
254 if (links.erase_unordered(p_link)) {
255 iteration_dirty = true;
256 }
257 }
258
259 bool NavMap3D::has_agent(NavAgent3D *agent) const {
260 return agents.has(agent);
261 }
262
263 void NavMap3D::add_agent(NavAgent3D *agent) {
264 if (!has_agent(agent)) {
265 agents.push_back(agent);
266 agents_dirty = true;
267 }
268 }
269
270 void NavMap3D::remove_agent(NavAgent3D *agent) {
271 remove_agent_as_controlled(agent);
272 if (agents.erase_unordered(agent)) {
273 agents_dirty = true;
274 }
275 }
276
277 bool NavMap3D::has_obstacle(NavObstacle3D *obstacle) const {
278 return obstacles.has(obstacle);
279 }
280
281 void NavMap3D::add_obstacle(NavObstacle3D *obstacle) {
282 if (obstacle->get_paused()) {
283 // No point in adding a paused obstacle, it will add itself when unpaused again.
284 return;
285 }
286
287 if (!has_obstacle(obstacle)) {
288 obstacles.push_back(obstacle);
289 obstacles_dirty = true;
290 }
291 }
292
293 void NavMap3D::remove_obstacle(NavObstacle3D *obstacle) {
294 if (obstacles.erase_unordered(obstacle)) {
295 obstacles_dirty = true;
296 }
297 }
298
299 void NavMap3D::set_agent_as_controlled(NavAgent3D *agent) {
300 remove_agent_as_controlled(agent);
301
302 if (agent->get_paused()) {
303 // No point in adding a paused agent, it will add itself when unpaused again.
304 return;
305 }
306
307 if (agent->get_use_3d_avoidance()) {
308 int64_t agent_3d_index = active_3d_avoidance_agents.find(agent);
309 if (agent_3d_index < 0) {
310 active_3d_avoidance_agents.push_back(agent);
311 agents_dirty = true;
312 }
313 } else {
314 int64_t agent_2d_index = active_2d_avoidance_agents.find(agent);
315 if (agent_2d_index < 0) {
316 active_2d_avoidance_agents.push_back(agent);
317 agents_dirty = true;
318 }
319 }
320 }
321
322 void NavMap3D::remove_agent_as_controlled(NavAgent3D *agent) {
323 if (active_3d_avoidance_agents.erase_unordered(agent)) {
324 agents_dirty = true;
325 }
326 if (active_2d_avoidance_agents.erase_unordered(agent)) {
327 agents_dirty = true;
328 }
329 }
330
331 Vector3 NavMap3D::get_random_point(uint32_t p_navigation_layers, bool p_uniformly) const {
332 GET_MAP_ITERATION_CONST();
333
334 return NavMeshQueries3D::map_iteration_get_random_point(map_iteration, p_navigation_layers, p_uniformly);
335 }
336
337 void NavMap3D::_build_iteration() {
338 if (!iteration_dirty || iteration_building || iteration_ready) {
339 return;
340 }
341
342 // Get the next free iteration slot that should be potentially unused.
343 iteration_slot_rwlock.read_lock();
344 NavMapIteration3D &next_map_iteration = iteration_slots[(iteration_slot_index + 1) % 2];
345 // Check if the iteration slot is truly free or still used by an external thread.
346 bool iteration_is_free = next_map_iteration.users.get() == 0;
347 iteration_slot_rwlock.read_unlock();
348
349 if (!iteration_is_free) {
350 // A long running pathfinding thread or something is still reading
351 // from this older iteration and needs to finish first.
352 // Return and wait for the next sync cycle to check again.
353 return;
354 }
355
356 // Iteration slot is free and no longer used by anything, let's build.
357
358 iteration_dirty = false;
359 iteration_building = true;
360 iteration_ready = false;
361
362 // We don't need to hold any lock because at this point nothing else can touch it.
363 // All new queries are already forwarded to the other iteration slot.
364
365 iteration_build.reset();
366
367 iteration_build.merge_rasterizer_cell_size = get_merge_rasterizer_cell_size();
368 iteration_build.use_edge_connections = get_use_edge_connections();
369 iteration_build.edge_connection_margin = get_edge_connection_margin();
370 iteration_build.link_connection_radius = get_link_connection_radius();
371
372 next_map_iteration.clear();
373
374 next_map_iteration.region_iterations.resize(regions.size());
375 next_map_iteration.link_iterations.resize(links.size());
376
377 uint32_t region_id_count = 0;
378 uint32_t link_id_count = 0;
379
380 for (NavRegion3D *region : regions) {
381 const Ref<NavRegionIteration3D> region_iteration = region->get_iteration();
382 next_map_iteration.region_iterations[region_id_count++] = region_iteration;
383 next_map_iteration.region_ptr_to_region_iteration[region] = region_iteration;
384 }
385 for (NavLink3D *link : links) {
386 const Ref<NavLinkIteration3D> link_iteration = link->get_iteration();
387 next_map_iteration.link_iterations[link_id_count++] = link_iteration;
388 }
389
390 next_map_iteration.map_up = get_up();
391
392 iteration_build.map_iteration = &next_map_iteration;
393
394 if (use_async_iterations) {
395 iteration_build_thread_task_id = WorkerThreadPool::get_singleton()->add_native_task(&NavMap3D::_build_iteration_threaded, &iteration_build, true, SNAME("NavMapBuilder3D"));
396 } else {
397 NavMapBuilder3D::build_navmap_iteration(iteration_build);
398
399 iteration_building = false;
400 iteration_ready = true;
401 }
402 }
403
404 void NavMap3D::_build_iteration_threaded(void *p_arg) {
405 NavMapIterationBuild3D *_iteration_build = static_cast<NavMapIterationBuild3D *>(p_arg);
406
407 NavMapBuilder3D::build_navmap_iteration(*_iteration_build);
408 }
409
410 void NavMap3D::_sync_iteration() {
411 if (iteration_building || !iteration_ready) {
412 return;
413 }
414
415 performance_data.pm_edge_connection_count = iteration_build.performance_data.pm_edge_connection_count;
416 performance_data.pm_edge_free_count = iteration_build.performance_data.pm_edge_free_count;
417
418 iteration_id = iteration_id % UINT32_MAX + 1;
419
420 // Finally ping-pong switch the iteration slot.
421 iteration_slot_rwlock.write_lock();
422 uint32_t next_iteration_slot_index = (iteration_slot_index + 1) % 2;
423 iteration_slot_index = next_iteration_slot_index;
424 iteration_slot_rwlock.write_unlock();
425
426 iteration_ready = false;
427 }
428
429 void NavMap3D::sync() {
430 // Performance Monitor.
431 performance_data.pm_region_count = regions.size();
432 performance_data.pm_agent_count = agents.size();
433 performance_data.pm_link_count = links.size();
434 performance_data.pm_obstacle_count = obstacles.size();
435
436 _sync_async_tasks();
437
438 _sync_dirty_map_update_requests();
439
440 if (iteration_dirty && !iteration_building && !iteration_ready) {
441 _build_iteration();
442 }
443 if (use_async_iterations && iteration_build_thread_task_id != WorkerThreadPool::INVALID_TASK_ID) {
444 if (WorkerThreadPool::get_singleton()->is_task_completed(iteration_build_thread_task_id)) {
445 WorkerThreadPool::get_singleton()->wait_for_task_completion(iteration_build_thread_task_id);
446
447 iteration_build_thread_task_id = WorkerThreadPool::INVALID_TASK_ID;
448 iteration_building = false;
449 iteration_ready = true;
450 }
451 }
452 if (iteration_ready) {
453 _sync_iteration();
454
455 NavigationServer3D::get_singleton()->emit_signal(SNAME("map_changed"), get_self());
456 }
457
458 map_settings_dirty = false;
459
460 _sync_avoidance();
461
462 performance_data.pm_polygon_count = 0;
463 performance_data.pm_edge_count = 0;
464 performance_data.pm_edge_merge_count = 0;
465
466 for (NavRegion3D *region : regions) {
467 performance_data.pm_polygon_count += region->get_pm_polygon_count();
468 performance_data.pm_edge_count += region->get_pm_edge_count();
469 performance_data.pm_edge_merge_count += region->get_pm_edge_merge_count();
470 }
471 }
472
473 void NavMap3D::_sync_avoidance() {
474 _sync_dirty_avoidance_update_requests();
475
476 if (obstacles_dirty || agents_dirty) {
477 _update_rvo_simulation();
478 }
479
480 obstacles_dirty = false;
481 agents_dirty = false;
482 }
483
484 void NavMap3D::_update_rvo_obstacles_tree_2d() {
485 int obstacle_vertex_count = 0;
486 for (NavObstacle3D *obstacle : obstacles) {
487 obstacle_vertex_count += obstacle->get_vertices().size();
488 }
489
490 // Cleaning old obstacles.
491 for (size_t i = 0; i < rvo_simulation_2d.obstacles_.size(); ++i) {
492 delete rvo_simulation_2d.obstacles_[i];
493 }
494 rvo_simulation_2d.obstacles_.clear();
495
496 // Cannot use LocalVector here as RVO library expects std::vector to build KdTree
497 std::vector<RVO2D::Obstacle2D *> &raw_obstacles = rvo_simulation_2d.obstacles_;
498 raw_obstacles.reserve(obstacle_vertex_count);
499
500 // The following block is modified copy from RVO2D::AddObstacle()
501 // Obstacles are linked and depend on all other obstacles.
502 for (NavObstacle3D *obstacle : obstacles) {
503 if (!obstacle->is_avoidance_enabled()) {
504 continue;
505 }
506 const Vector3 &_obstacle_position = obstacle->get_position();
507 const Vector<Vector3> &_obstacle_vertices = obstacle->get_vertices();
508
509 if (_obstacle_vertices.size() < 2) {
510 continue;
511 }
512
513 std::vector<RVO2D::Vector2> rvo_2d_vertices;
514 rvo_2d_vertices.reserve(_obstacle_vertices.size());
515
516 uint32_t _obstacle_avoidance_layers = obstacle->get_avoidance_layers();
517 real_t _obstacle_height = obstacle->get_height();
518
519 for (const Vector3 &_obstacle_vertex : _obstacle_vertices) {
520 #ifdef TOOLS_ENABLED
521 if (_obstacle_vertex.y != 0) {
522 WARN_PRINT_ONCE("Y coordinates of static obstacle vertices are ignored. Please use obstacle position Y to change elevation of obstacle.");
523 }
524 #endif
525 rvo_2d_vertices.push_back(RVO2D::Vector2(_obstacle_vertex.x + _obstacle_position.x, _obstacle_vertex.z + _obstacle_position.z));
526 }
527
528 const size_t obstacleNo = raw_obstacles.size();
529
530 for (size_t i = 0; i < rvo_2d_vertices.size(); i++) {
531 RVO2D::Obstacle2D *rvo_2d_obstacle = new RVO2D::Obstacle2D();
532 rvo_2d_obstacle->point_ = rvo_2d_vertices[i];
533 rvo_2d_obstacle->height_ = _obstacle_height;
534 rvo_2d_obstacle->elevation_ = _obstacle_position.y;
535
536 rvo_2d_obstacle->avoidance_layers_ = _obstacle_avoidance_layers;
537
538 if (i != 0) {
539 rvo_2d_obstacle->prevObstacle_ = raw_obstacles.back();
540 rvo_2d_obstacle->prevObstacle_->nextObstacle_ = rvo_2d_obstacle;
541 }
542
543 if (i == rvo_2d_vertices.size() - 1) {
544 rvo_2d_obstacle->nextObstacle_ = raw_obstacles[obstacleNo];
545 rvo_2d_obstacle->nextObstacle_->prevObstacle_ = rvo_2d_obstacle;
546 }
547
548 rvo_2d_obstacle->unitDir_ = normalize(rvo_2d_vertices[(i == rvo_2d_vertices.size() - 1 ? 0 : i + 1)] - rvo_2d_vertices[i]);
549
550 if (rvo_2d_vertices.size() == 2) {
551 rvo_2d_obstacle->isConvex_ = true;
552 } else {
553 rvo_2d_obstacle->isConvex_ = (leftOf(rvo_2d_vertices[(i == 0 ? rvo_2d_vertices.size() - 1 : i - 1)], rvo_2d_vertices[i], rvo_2d_vertices[(i == rvo_2d_vertices.size() - 1 ? 0 : i + 1)]) >= 0.0f);
554 }
555
556 rvo_2d_obstacle->id_ = raw_obstacles.size();
557
558 raw_obstacles.push_back(rvo_2d_obstacle);
559 }
560 }
561
562 rvo_simulation_2d.kdTree_->buildObstacleTree(raw_obstacles);
563 }
564
565 void NavMap3D::_update_rvo_agents_tree_2d() {
566 // Cannot use LocalVector here as RVO library expects std::vector to build KdTree.
567 std::vector<RVO2D::Agent2D *> raw_agents;
568 raw_agents.reserve(active_2d_avoidance_agents.size());
569 for (NavAgent3D *agent : active_2d_avoidance_agents) {
570 raw_agents.push_back(agent->get_rvo_agent_2d());
571 }
572 rvo_simulation_2d.kdTree_->buildAgentTree(raw_agents);
573 }
574
575 void NavMap3D::_update_rvo_agents_tree_3d() {
576 // Cannot use LocalVector here as RVO library expects std::vector to build KdTree.
577 std::vector<RVO3D::Agent3D *> raw_agents;
578 raw_agents.reserve(active_3d_avoidance_agents.size());
579 for (NavAgent3D *agent : active_3d_avoidance_agents) {
580 raw_agents.push_back(agent->get_rvo_agent_3d());
581 }
582 rvo_simulation_3d.kdTree_->buildAgentTree(raw_agents);
583 }
584
585 void NavMap3D::_update_rvo_simulation() {
586 if (obstacles_dirty) {
587 _update_rvo_obstacles_tree_2d();
588 }
589 if (agents_dirty) {
590 _update_rvo_agents_tree_2d();
591 _update_rvo_agents_tree_3d();
592 }
593 }
594
595 void NavMap3D::compute_single_avoidance_step_2d(uint32_t index, NavAgent3D **agent) {
596 (*(agent + index))->get_rvo_agent_2d()->computeNeighbors(&rvo_simulation_2d);
597 (*(agent + index))->get_rvo_agent_2d()->computeNewVelocity(&rvo_simulation_2d);
598 (*(agent + index))->get_rvo_agent_2d()->update(&rvo_simulation_2d);
599 (*(agent + index))->update();
600 }
601
602 void NavMap3D::compute_single_avoidance_step_3d(uint32_t index, NavAgent3D **agent) {
603 (*(agent + index))->get_rvo_agent_3d()->computeNeighbors(&rvo_simulation_3d);
604 (*(agent + index))->get_rvo_agent_3d()->computeNewVelocity(&rvo_simulation_3d);
605 (*(agent + index))->get_rvo_agent_3d()->update(&rvo_simulation_3d);
606 (*(agent + index))->update();
607 }
608
609 void NavMap3D::step(double p_delta_time) {
610 rvo_simulation_2d.setTimeStep(float(p_delta_time));
611 rvo_simulation_3d.setTimeStep(float(p_delta_time));
612
613 if (active_2d_avoidance_agents.size() > 0) {
614 if (use_threads && avoidance_use_multiple_threads) {
615 WorkerThreadPool::GroupID group_task = WorkerThreadPool::get_singleton()->add_template_group_task(this, &NavMap3D::compute_single_avoidance_step_2d, active_2d_avoidance_agents.ptr(), active_2d_avoidance_agents.size(), -1, true, SNAME("RVOAvoidanceAgents2D"));
616 WorkerThreadPool::get_singleton()->wait_for_group_task_completion(group_task);
617 } else {
618 for (NavAgent3D *agent : active_2d_avoidance_agents) {
619 agent->get_rvo_agent_2d()->computeNeighbors(&rvo_simulation_2d);
620 agent->get_rvo_agent_2d()->computeNewVelocity(&rvo_simulation_2d);
621 agent->get_rvo_agent_2d()->update(&rvo_simulation_2d);
622 agent->update();
623 }
624 }
625 }
626
627 if (active_3d_avoidance_agents.size() > 0) {
628 if (use_threads && avoidance_use_multiple_threads) {
629 WorkerThreadPool::GroupID group_task = WorkerThreadPool::get_singleton()->add_template_group_task(this, &NavMap3D::compute_single_avoidance_step_3d, active_3d_avoidance_agents.ptr(), active_3d_avoidance_agents.size(), -1, true, SNAME("RVOAvoidanceAgents3D"));
630 WorkerThreadPool::get_singleton()->wait_for_group_task_completion(group_task);
631 } else {
632 for (NavAgent3D *agent : active_3d_avoidance_agents) {
633 agent->get_rvo_agent_3d()->computeNeighbors(&rvo_simulation_3d);
634 agent->get_rvo_agent_3d()->computeNewVelocity(&rvo_simulation_3d);
635 agent->get_rvo_agent_3d()->update(&rvo_simulation_3d);
636 agent->update();
637 }
638 }
639 }
640 }
641
642 void NavMap3D::dispatch_callbacks() {
643 for (NavAgent3D *agent : active_2d_avoidance_agents) {
644 agent->dispatch_avoidance_callback();
645 }
646
647 for (NavAgent3D *agent : active_3d_avoidance_agents) {
648 agent->dispatch_avoidance_callback();
649 }
650 }
651
652 void NavMap3D::_update_merge_rasterizer_cell_dimensions() {
653 merge_rasterizer_cell_size.x = cell_size * merge_rasterizer_cell_scale;
654 merge_rasterizer_cell_size.y = cell_height * merge_rasterizer_cell_scale;
655 merge_rasterizer_cell_size.z = cell_size * merge_rasterizer_cell_scale;
656 }
657
658 int NavMap3D::get_region_connections_count(NavRegion3D *p_region) const {
659 ERR_FAIL_NULL_V(p_region, 0);
660
661 GET_MAP_ITERATION_CONST();
662
663 HashMap<NavRegion3D *, Ref<NavRegionIteration3D>>::ConstIterator found_id = map_iteration.region_ptr_to_region_iteration.find(p_region);
664 if (found_id) {
665 HashMap<const NavBaseIteration3D *, LocalVector<Connection>>::ConstIterator found_connections = map_iteration.external_region_connections.find(found_id->value.ptr());
666 if (found_connections) {
667 return found_connections->value.size();
668 }
669 }
670
671 return 0;
672 }
673
674 Vector3 NavMap3D::get_region_connection_pathway_start(NavRegion3D *p_region, int p_connection_id) const {
675 ERR_FAIL_NULL_V(p_region, Vector3());
676
677 GET_MAP_ITERATION_CONST();
678
679 HashMap<NavRegion3D *, Ref<NavRegionIteration3D>>::ConstIterator found_id = map_iteration.region_ptr_to_region_iteration.find(p_region);
680 if (found_id) {
681 HashMap<const NavBaseIteration3D *, LocalVector<Connection>>::ConstIterator found_connections = map_iteration.external_region_connections.find(found_id->value.ptr());
682 if (found_connections) {
683 ERR_FAIL_INDEX_V(p_connection_id, int(found_connections->value.size()), Vector3());
684 return found_connections->value[p_connection_id].pathway_start;
685 }
686 }
687
688 return Vector3();
689 }
690
691 Vector3 NavMap3D::get_region_connection_pathway_end(NavRegion3D *p_region, int p_connection_id) const {
692 ERR_FAIL_NULL_V(p_region, Vector3());
693
694 GET_MAP_ITERATION_CONST();
695
696 HashMap<NavRegion3D *, Ref<NavRegionIteration3D>>::ConstIterator found_id = map_iteration.region_ptr_to_region_iteration.find(p_region);
697 if (found_id) {
698 HashMap<const NavBaseIteration3D *, LocalVector<Connection>>::ConstIterator found_connections = map_iteration.external_region_connections.find(found_id->value.ptr());
699 if (found_connections) {
700 ERR_FAIL_INDEX_V(p_connection_id, int(found_connections->value.size()), Vector3());
701 return found_connections->value[p_connection_id].pathway_end;
702 }
703 }
704
705 return Vector3();
706 }
707
708 void NavMap3D::add_region_sync_dirty_request(SelfList<NavRegion3D> *p_sync_request) {
709 if (p_sync_request->in_list()) {
710 return;
711 }
712 RWLockWrite write_lock(sync_dirty_requests.regions.rwlock);
713 sync_dirty_requests.regions.list.add(p_sync_request);
714 }
715
716 void NavMap3D::add_link_sync_dirty_request(SelfList<NavLink3D> *p_sync_request) {
717 if (p_sync_request->in_list()) {
718 return;
719 }
720 RWLockWrite write_lock(sync_dirty_requests.links.rwlock);
721 sync_dirty_requests.links.list.add(p_sync_request);
722 }
723
724 void NavMap3D::add_agent_sync_dirty_request(SelfList<NavAgent3D> *p_sync_request) {
725 if (p_sync_request->in_list()) {
726 return;
727 }
728 sync_dirty_requests.agents.list.add(p_sync_request);
729 }
730
731 void NavMap3D::add_obstacle_sync_dirty_request(SelfList<NavObstacle3D> *p_sync_request) {
732 if (p_sync_request->in_list()) {
733 return;
734 }
735 sync_dirty_requests.obstacles.list.add(p_sync_request);
736 }
737
738 void NavMap3D::remove_region_sync_dirty_request(SelfList<NavRegion3D> *p_sync_request) {
739 if (!p_sync_request->in_list()) {
740 return;
741 }
742 RWLockWrite write_lock(sync_dirty_requests.regions.rwlock);
743 sync_dirty_requests.regions.list.remove(p_sync_request);
744 }
745
746 void NavMap3D::remove_link_sync_dirty_request(SelfList<NavLink3D> *p_sync_request) {
747 if (!p_sync_request->in_list()) {
748 return;
749 }
750 RWLockWrite write_lock(sync_dirty_requests.links.rwlock);
751 sync_dirty_requests.links.list.remove(p_sync_request);
752 }
753
754 void NavMap3D::remove_agent_sync_dirty_request(SelfList<NavAgent3D> *p_sync_request) {
755 if (!p_sync_request->in_list()) {
756 return;
757 }
758 sync_dirty_requests.agents.list.remove(p_sync_request);
759 }
760
761 void NavMap3D::remove_obstacle_sync_dirty_request(SelfList<NavObstacle3D> *p_sync_request) {
762 if (!p_sync_request->in_list()) {
763 return;
764 }
765 sync_dirty_requests.obstacles.list.remove(p_sync_request);
766 }
767
768 void NavMap3D::_sync_dirty_map_update_requests() {
769 // If entire map settings changed make all regions dirty.
770 if (map_settings_dirty) {
771 for (NavRegion3D *region : regions) {
772 region->scratch_polygons();
773 }
774 iteration_dirty = true;
775 }
776
777 // Sync NavRegions.
778 RWLockWrite write_lock_regions(sync_dirty_requests.regions.rwlock);
779 for (SelfList<NavRegion3D> *element = sync_dirty_requests.regions.list.first(); element; element = element->next()) {
780 bool requires_map_update = element->self()->sync();
781 if (requires_map_update) {
782 iteration_dirty = true;
783 }
784 }
785 sync_dirty_requests.regions.list.clear();
786
787 // Sync NavLinks.
788 RWLockWrite write_lock_links(sync_dirty_requests.links.rwlock);
789 for (SelfList<NavLink3D> *element = sync_dirty_requests.links.list.first(); element; element = element->next()) {
790 bool requires_map_update = element->self()->sync();
791 if (requires_map_update) {
792 iteration_dirty = true;
793 }
794 }
795 sync_dirty_requests.links.list.clear();
796 }
797
798 void NavMap3D::_sync_dirty_avoidance_update_requests() {
799 // Sync NavAgents.
800 if (!agents_dirty) {
801 agents_dirty = sync_dirty_requests.agents.list.first();
802 }
803 for (SelfList<NavAgent3D> *element = sync_dirty_requests.agents.list.first(); element; element = element->next()) {
804 element->self()->sync();
805 }
806 sync_dirty_requests.agents.list.clear();
807
808 // Sync NavObstacles.
809 if (!obstacles_dirty) {
810 obstacles_dirty = sync_dirty_requests.obstacles.list.first();
811 }
812 for (SelfList<NavObstacle3D> *element = sync_dirty_requests.obstacles.list.first(); element; element = element->next()) {
813 element->self()->sync();
814 }
815 sync_dirty_requests.obstacles.list.clear();
816 }
817
818 void NavMap3D::add_region_async_thread_join_request(SelfList<NavRegion3D> *p_async_request) {
819 if (p_async_request->in_list()) {
820 return;
821 }
822 RWLockWrite write_lock(async_dirty_requests.regions.rwlock);
823 async_dirty_requests.regions.list.add(p_async_request);
824 }
825
826 void NavMap3D::remove_region_async_thread_join_request(SelfList<NavRegion3D> *p_async_request) {
827 if (!p_async_request->in_list()) {
828 return;
829 }
830 RWLockWrite write_lock(async_dirty_requests.regions.rwlock);
831 async_dirty_requests.regions.list.remove(p_async_request);
832 }
833
834 void NavMap3D::_sync_async_tasks() {
835 // Sync NavRegions that run async thread tasks.
836 RWLockWrite write_lock_regions(async_dirty_requests.regions.rwlock);
837 for (SelfList<NavRegion3D> *element = async_dirty_requests.regions.list.first(); element; element = element->next()) {
838 element->self()->sync_async_tasks();
839 }
840 }
841
842 void NavMap3D::set_use_async_iterations(bool p_enabled) {
843 if (use_async_iterations == p_enabled) {
844 return;
845 }
846 #ifdef THREADS_ENABLED
847 use_async_iterations = p_enabled;
848 #endif
849 }
850
851 bool NavMap3D::get_use_async_iterations() const {
852 return use_async_iterations;
853 }
854
855 NavMap3D::NavMap3D() {
856 avoidance_use_multiple_threads = GLOBAL_GET("navigation/avoidance/thread_model/avoidance_use_multiple_threads");
857 avoidance_use_high_priority_threads = GLOBAL_GET("navigation/avoidance/thread_model/avoidance_use_high_priority_threads");
858
859 path_query_slots_max = GLOBAL_GET("navigation/pathfinding/max_threads");
860
861 int processor_count = OS::get_singleton()->get_processor_count();
862 if (path_query_slots_max < 0) {
863 path_query_slots_max = processor_count;
864 }
865 if (processor_count < path_query_slots_max) {
866 path_query_slots_max = processor_count;
867 }
868 if (path_query_slots_max < 1) {
869 path_query_slots_max = 1;
870 }
871
872 iteration_slots.resize(2);
873
874 for (NavMapIteration3D &iteration_slot : iteration_slots) {
875 iteration_slot.path_query_slots.resize(path_query_slots_max);
876 for (uint32_t i = 0; i < iteration_slot.path_query_slots.size(); i++) {
877 iteration_slot.path_query_slots[i].slot_index = i;
878 }
879 iteration_slot.path_query_slots_semaphore.post(path_query_slots_max);
880 }
881
882 #ifdef THREADS_ENABLED
883 use_async_iterations = GLOBAL_GET("navigation/world/map_use_async_iterations");
884 #else
885 use_async_iterations = false;
886 #endif
887 }
888
889 NavMap3D::~NavMap3D() {
890 if (iteration_build_thread_task_id != WorkerThreadPool::INVALID_TASK_ID) {
891 WorkerThreadPool::get_singleton()->wait_for_task_completion(iteration_build_thread_task_id);
892 iteration_build_thread_task_id = WorkerThreadPool::INVALID_TASK_ID;
893 }
894
895 RWLockWrite write_lock(iteration_slot_rwlock);
896 for (NavMapIteration3D &iteration_slot : iteration_slots) {
897 iteration_slot.clear();
898 }
899 }