diff --git a/modules/navigation/3d/godot_navigation_server_3d.cpp b/modules/navigation/3d/godot_navigation_server_3d.cpp index fc96506795f..11a5de608b0 100644 --- a/modules/navigation/3d/godot_navigation_server_3d.cpp +++ b/modules/navigation/3d/godot_navigation_server_3d.cpp @@ -509,22 +509,31 @@ void GodotNavigationServer3D::region_bake_navigation_mesh(Ref p_ int GodotNavigationServer3D::region_get_connections_count(RID p_region) const { NavRegion *region = region_owner.get_or_null(p_region); ERR_FAIL_NULL_V(region, 0); - - return region->get_connections_count(); + NavMap *map = region->get_map(); + if (map) { + return map->get_region_connections_count(region); + } + return 0; } Vector3 GodotNavigationServer3D::region_get_connection_pathway_start(RID p_region, int p_connection_id) const { NavRegion *region = region_owner.get_or_null(p_region); ERR_FAIL_NULL_V(region, Vector3()); - - return region->get_connection_pathway_start(p_connection_id); + NavMap *map = region->get_map(); + if (map) { + return map->get_region_connection_pathway_start(region, p_connection_id); + } + return Vector3(); } Vector3 GodotNavigationServer3D::region_get_connection_pathway_end(RID p_region, int p_connection_id) const { NavRegion *region = region_owner.get_or_null(p_region); ERR_FAIL_NULL_V(region, Vector3()); - - return region->get_connection_pathway_end(p_connection_id); + NavMap *map = region->get_map(); + if (map) { + return map->get_region_connection_pathway_end(region, p_connection_id); + } + return Vector3(); } Vector3 GodotNavigationServer3D::region_get_random_point(RID p_region, uint32_t p_navigation_layers, bool p_uniformly) const { diff --git a/modules/navigation/nav_map.cpp b/modules/navigation/nav_map.cpp index 9796fa350d7..0c91e8dea3e 100644 --- a/modules/navigation/nav_map.cpp +++ b/modules/navigation/nav_map.cpp @@ -937,8 +937,9 @@ void NavMap::sync() { _new_pm_edge_free_count = 0; // Remove regions connections. + region_external_connections.clear(); for (NavRegion *region : regions) { - region->get_connections().clear(); + region_external_connections[region] = LocalVector(); } // Resize the polygon count. @@ -1072,7 +1073,7 @@ void NavMap::sync() { free_edge.polygon->edges[free_edge.edge].connections.push_back(new_connection); // Add the connection to the region_connection map. - ((NavRegion *)free_edge.polygon->owner)->get_connections().push_back(new_connection); + region_external_connections[(NavRegion *)free_edge.polygon->owner].push_back(new_connection); _new_pm_edge_connection_count += 1; } } @@ -1428,6 +1429,40 @@ void NavMap::_update_merge_rasterizer_cell_dimensions() { merge_rasterizer_cell_height = cell_height * merge_rasterizer_cell_scale; } +int NavMap::get_region_connections_count(NavRegion *p_region) const { + ERR_FAIL_NULL_V(p_region, 0); + + HashMap>::ConstIterator found_connections = region_external_connections.find(p_region); + if (found_connections) { + return found_connections->value.size(); + } + return 0; +} + +Vector3 NavMap::get_region_connection_pathway_start(NavRegion *p_region, int p_connection_id) const { + ERR_FAIL_NULL_V(p_region, Vector3()); + + HashMap>::ConstIterator found_connections = region_external_connections.find(p_region); + if (found_connections) { + ERR_FAIL_INDEX_V(p_connection_id, int(found_connections->value.size()), Vector3()); + return found_connections->value[p_connection_id].pathway_start; + } + + return Vector3(); +} + +Vector3 NavMap::get_region_connection_pathway_end(NavRegion *p_region, int p_connection_id) const { + ERR_FAIL_NULL_V(p_region, Vector3()); + + HashMap>::ConstIterator found_connections = region_external_connections.find(p_region); + if (found_connections) { + ERR_FAIL_INDEX_V(p_connection_id, int(found_connections->value.size()), Vector3()); + return found_connections->value[p_connection_id].pathway_end; + } + + return Vector3(); +} + NavMap::NavMap() { avoidance_use_multiple_threads = GLOBAL_GET("navigation/avoidance/thread_model/avoidance_use_multiple_threads"); avoidance_use_high_priority_threads = GLOBAL_GET("navigation/avoidance/thread_model/avoidance_use_high_priority_threads"); diff --git a/modules/navigation/nav_map.h b/modules/navigation/nav_map.h index 3dd6db21dd9..82e8854b7aa 100644 --- a/modules/navigation/nav_map.h +++ b/modules/navigation/nav_map.h @@ -125,6 +125,8 @@ class NavMap : public NavRid { int pm_edge_free_count = 0; int pm_obstacle_count = 0; + HashMap> region_external_connections; + public: NavMap(); ~NavMap(); @@ -219,6 +221,10 @@ public: int get_pm_edge_free_count() const { return pm_edge_free_count; } int get_pm_obstacle_count() const { return pm_obstacle_count; } + int get_region_connections_count(NavRegion *p_region) const; + Vector3 get_region_connection_pathway_start(NavRegion *p_region, int p_connection_id) const; + Vector3 get_region_connection_pathway_end(NavRegion *p_region, int p_connection_id) const; + private: void compute_single_step(uint32_t index, NavAgent **agent); diff --git a/modules/navigation/nav_region.cpp b/modules/navigation/nav_region.cpp index f30855d6975..85510bd4163 100644 --- a/modules/navigation/nav_region.cpp +++ b/modules/navigation/nav_region.cpp @@ -44,8 +44,6 @@ void NavRegion::set_map(NavMap *p_map) { map = p_map; polygons_dirty = true; - connections.clear(); - if (map) { map->add_region(this); } @@ -105,25 +103,6 @@ void NavRegion::set_navigation_mesh(Ref p_navigation_mesh) { polygons_dirty = true; } -int NavRegion::get_connections_count() const { - if (!map) { - return 0; - } - return connections.size(); -} - -Vector3 NavRegion::get_connection_pathway_start(int p_connection_id) const { - ERR_FAIL_NULL_V(map, Vector3()); - ERR_FAIL_INDEX_V(p_connection_id, connections.size(), Vector3()); - return connections[p_connection_id].pathway_start; -} - -Vector3 NavRegion::get_connection_pathway_end(int p_connection_id) const { - ERR_FAIL_NULL_V(map, Vector3()); - ERR_FAIL_INDEX_V(p_connection_id, connections.size(), Vector3()); - return connections[p_connection_id].pathway_end; -} - Vector3 NavRegion::get_random_point(uint32_t p_navigation_layers, bool p_uniformly) const { if (!get_enabled()) { return Vector3(); diff --git a/modules/navigation/nav_region.h b/modules/navigation/nav_region.h index ebc082bd2f5..662a32c47a4 100644 --- a/modules/navigation/nav_region.h +++ b/modules/navigation/nav_region.h @@ -40,7 +40,6 @@ class NavRegion : public NavBase { NavMap *map = nullptr; Transform3D transform; - Vector connections; bool enabled = true; bool use_edge_connections = true; @@ -85,13 +84,6 @@ public: void set_navigation_mesh(Ref p_navigation_mesh); - Vector &get_connections() { - return connections; - } - int get_connections_count() const; - Vector3 get_connection_pathway_start(int p_connection_id) const; - Vector3 get_connection_pathway_end(int p_connection_id) const; - LocalVector const &get_polygons() const { return polygons; }