Backported from godot4: Add NavigationServer API to enable regions and links

Adds NavigationServer API to enable regions and links.
- smix8
69fad39cf5
This commit is contained in:
Relintai 2023-09-04 20:22:28 +02:00
parent b37e0e6a4e
commit a222159442
21 changed files with 188 additions and 50 deletions

View File

@ -208,6 +208,13 @@
Create a new link between two positions on a map.
</description>
</method>
<method name="link_get_enabled" qualifiers="const">
<return type="bool" />
<param index="0" name="link" type="RID" />
<description>
Returns [code]true[/code] if the specified [param link] is enabled.
</description>
</method>
<method name="link_get_end_position" qualifiers="const">
<return type="Vector2" />
<argument index="0" name="link" type="RID" />
@ -272,6 +279,14 @@
Sets whether this [code]link[/code] can be travelled in both directions.
</description>
</method>
<method name="link_set_enabled">
<return type="void" />
<param index="0" name="link" type="RID" />
<param index="1" name="enabled" type="bool" />
<description>
If [param enabled] is [code]true[/code] the specified [param link] will contribute to its current navigation map.
</description>
</method>
<method name="link_set_end_position">
<return type="void" />
<argument index="0" name="link" type="RID" />
@ -609,6 +624,13 @@
Returns how many connections this [code]region[/code] has with other regions in the map.
</description>
</method>
<method name="region_get_enabled" qualifiers="const">
<return type="bool" />
<param index="0" name="region" type="RID" />
<description>
Returns [code]true[/code] if the specified [param region] is enabled.
</description>
</method>
<method name="region_get_enter_cost" qualifiers="const">
<return type="float" />
<argument index="0" name="region" type="RID" />
@ -661,6 +683,14 @@
[b]Note:[/b] If navigation meshes from different navigation regions overlap (which should be avoided in general) the result might not be what is expected.
</description>
</method>
<method name="region_set_enabled">
<return type="void" />
<param index="0" name="region" type="RID" />
<param index="1" name="enabled" type="bool" />
<description>
If [param enabled] is [code]true[/code] the specified [param region] will contribute to its current navigation map.
</description>
</method>
<method name="region_set_enter_cost">
<return type="void" />
<argument index="0" name="region" type="RID" />

View File

@ -239,6 +239,13 @@
Create a new link between two positions on a map.
</description>
</method>
<method name="link_get_enabled" qualifiers="const">
<return type="bool" />
<param index="0" name="link" type="RID" />
<description>
Returns [code]true[/code] if the specified [param link] is enabled.
</description>
</method>
<method name="link_get_end_position" qualifiers="const">
<return type="Vector3" />
<argument index="0" name="link" type="RID" />
@ -303,6 +310,14 @@
Sets whether this [code]link[/code] can be travelled in both directions.
</description>
</method>
<method name="link_set_enabled">
<return type="void" />
<param index="0" name="link" type="RID" />
<param index="1" name="enabled" type="bool" />
<description>
If [param enabled] is [code]true[/code] the specified [param link] will contribute to its current navigation map.
</description>
</method>
<method name="link_set_end_position">
<return type="void" />
<argument index="0" name="link" type="RID" />
@ -704,6 +719,13 @@
Returns how many connections this [code]region[/code] has with other regions in the map.
</description>
</method>
<method name="region_get_enabled" qualifiers="const">
<return type="bool" />
<param index="0" name="region" type="RID" />
<description>
Returns [code]true[/code] if the specified [param region] is enabled.
</description>
</method>
<method name="region_get_enter_cost" qualifiers="const">
<return type="float" />
<argument index="0" name="region" type="RID" />
@ -756,6 +778,14 @@
[b]Note:[/b] If navigation meshes from different navigation regions overlap (which should be avoided in general) the result might not be what is expected.
</description>
</method>
<method name="region_set_enabled">
<return type="void" />
<param index="0" name="region" type="RID" />
<param index="1" name="enabled" type="bool" />
<description>
If [param enabled] is [code]true[/code] the specified [param region] will contribute to its current navigation map.
</description>
</method>
<method name="region_set_enter_cost">
<return type="void" />
<argument index="0" name="region" type="RID" />

View File

@ -40,6 +40,16 @@ void NavLink::set_map(NavMap *p_map) {
link_dirty = true;
}
void NavLink::set_enabled(bool p_enabled) {
if (enabled == p_enabled) {
return;
}
enabled = p_enabled;
// TODO: This should not require a full rebuild as the link has not really changed.
link_dirty = true;
};
void NavLink::set_bidirectional(bool p_bidirectional) {
if (bidirectional == p_bidirectional) {
return;

View File

@ -40,6 +40,7 @@ class NavLink : public NavBase {
bool bidirectional;
Vector3 start_position;
Vector3 end_position;
bool enabled;
bool link_dirty;
@ -48,6 +49,7 @@ public:
type = NavigationUtilities::PathSegmentType::PATH_SEGMENT_TYPE_LINK;
map = nullptr;
bidirectional = true;
enabled = true;
link_dirty = true;
}
@ -56,6 +58,9 @@ public:
return map;
}
void set_enabled(bool p_enabled);
bool get_enabled() const { return enabled; }
void set_bidirectional(bool p_bidirectional);
bool is_bidirectional() const {
return bidirectional;

View File

@ -839,7 +839,13 @@ void NavMap::sync() {
// Resize the polygon count.
int count = 0;
for (uint32_t r = 0; r < regions.size(); r++) {
count += regions[r]->get_polygons().size();
NavRegion *region = regions[r];
if (!region->get_enabled()) {
continue;
}
count += region->get_polygons().size();
}
polygons.resize(count);
@ -847,12 +853,18 @@ void NavMap::sync() {
// Copy all region polygons in the map.
count = 0;
for (uint32_t r = 0; r < regions.size(); r++) {
const LocalVector<gd::Polygon> &polygons_source = regions[r]->get_polygons();
NavRegion *region = regions[r];
if (!region->get_enabled()) {
continue;
}
const LocalVector<gd::Polygon> &polygons_source = region->get_polygons();
for (uint32_t n = 0; n < polygons_source.size(); n++) {
polygons[count + n] = polygons_source[n];
}
count += regions[r]->get_polygons().size();
count += region->get_polygons().size();
}
_new_pm_polygon_count = polygons.size();

View File

@ -45,6 +45,16 @@ void NavRegion::set_map(NavMap *p_map) {
}
}
void NavRegion::set_enabled(bool p_enabled) {
if (enabled == p_enabled) {
return;
}
enabled = p_enabled;
// TODO: This should not require a full rebuild as the region has not really changed.
polygons_dirty = true;
};
void NavRegion::set_use_edge_connections(bool p_enabled) {
if (use_edge_connections != p_enabled) {
use_edge_connections = p_enabled;
@ -184,6 +194,7 @@ NavRegion::NavRegion() {
enter_cost = 0.0;
travel_cost = 1.0;
polygons_dirty = true;
enabled = true;
use_edge_connections = true;
}

View File

@ -42,6 +42,7 @@ class NavRegion : public NavBase {
Transform transform;
Ref<NavigationMesh> mesh;
Vector<gd::Edge::Connection> connections;
bool enabled;
bool use_edge_connections;
@ -55,6 +56,9 @@ public:
polygons_dirty = true;
}
void set_enabled(bool p_enabled);
bool get_enabled() const { return enabled; }
void set_map(NavMap *p_map);
NavMap *get_map() const {
return map;

View File

@ -268,6 +268,8 @@ RID FORWARD_2_C(map_get_closest_point_owner, RID, p_map, const Vector2 &, p_poin
RID FORWARD_0(region_create);
void FORWARD_2(region_set_enabled, RID, p_region, bool, p_enabled, rid_to_rid, bool_to_bool);
bool FORWARD_1_C(region_get_enabled, RID, p_region, rid_to_rid);
void FORWARD_2(region_set_use_edge_connections, RID, p_region, bool, p_enabled, rid_to_rid, bool_to_bool);
bool FORWARD_1_C(region_get_use_edge_connections, RID, p_region, rid_to_rid);
@ -296,6 +298,8 @@ RID FORWARD_0(link_create);
void FORWARD_2(link_set_map, RID, p_link, RID, p_map, rid_to_rid, rid_to_rid);
RID FORWARD_1_C(link_get_map, RID, p_link, rid_to_rid);
void FORWARD_2(link_set_enabled, RID, p_link, bool, p_enabled, rid_to_rid, bool_to_bool);
bool FORWARD_1_C(link_get_enabled, RID, p_link, rid_to_rid);
void FORWARD_2(link_set_bidirectional, RID, p_link, bool, p_bidirectional, rid_to_rid, bool_to_bool);
bool FORWARD_1_C(link_is_bidirectional, RID, p_link, rid_to_rid);
void FORWARD_2(link_set_navigation_layers, RID, p_link, uint32_t, p_navigation_layers, rid_to_rid, uint32_to_uint32);

View File

@ -98,6 +98,9 @@ public:
/// Creates a new region.
virtual RID region_create();
virtual void region_set_enabled(RID p_region, bool p_enabled);
virtual bool region_get_enabled(RID p_region) const;
virtual void region_set_use_edge_connections(RID p_region, bool p_enabled);
virtual bool region_get_use_edge_connections(RID p_region) const;
@ -141,6 +144,9 @@ public:
virtual void link_set_map(RID p_link, RID p_map);
virtual RID link_get_map(RID p_link) const;
virtual void link_set_enabled(RID p_link, bool p_enabled);
virtual bool link_get_enabled(RID p_link) const;
/// Set whether this link travels in both directions.
virtual void link_set_bidirectional(RID p_link, bool p_bidirectional);
virtual bool link_is_bidirectional(RID p_link) const;

View File

@ -388,6 +388,20 @@ RID PandemoniumNavigationServer::region_create() {
return rid;
}
COMMAND_2(region_set_enabled, RID, p_region, bool, p_enabled) {
NavRegion *region = region_owner.getornull(p_region);
ERR_FAIL_COND(region == nullptr);
region->set_enabled(p_enabled);
}
bool PandemoniumNavigationServer::region_get_enabled(RID p_region) const {
const NavRegion *region = region_owner.getornull(p_region);
ERR_FAIL_COND_V(region == nullptr, false);
return region->get_enabled();
}
COMMAND_2(region_set_use_edge_connections, RID, p_region, bool, p_enabled) {
NavRegion *region = region_owner.getornull(p_region);
ERR_FAIL_COND(region == nullptr);
@ -568,6 +582,20 @@ RID PandemoniumNavigationServer::link_get_map(const RID p_link) const {
return RID();
}
COMMAND_2(link_set_enabled, RID, p_link, bool, p_enabled) {
NavLink *link = link_owner.getornull(p_link);
ERR_FAIL_COND(link == nullptr);
link->set_enabled(p_enabled);
}
bool PandemoniumNavigationServer::link_get_enabled(RID p_link) const {
const NavLink *link = link_owner.getornull(p_link);
ERR_FAIL_COND_V(link == nullptr, false);
return link->get_enabled();
}
COMMAND_2(link_set_bidirectional, RID, p_link, bool, p_bidirectional) {
NavLink *link = link_owner.getornull(p_link);
ERR_FAIL_COND(link == nullptr);

View File

@ -137,6 +137,9 @@ public:
virtual RID region_create();
COMMAND_2(region_set_enabled, RID, p_region, bool, p_enabled);
virtual bool region_get_enabled(RID p_region) const;
COMMAND_2(region_set_use_edge_connections, RID, p_region, bool, p_enabled);
virtual bool region_get_use_edge_connections(RID p_region) const;
@ -163,6 +166,8 @@ public:
virtual RID link_create();
COMMAND_2(link_set_map, RID, p_link, RID, p_map);
virtual RID link_get_map(RID p_link) const;
COMMAND_2(link_set_enabled, RID, p_link, bool, p_enabled);
virtual bool link_get_enabled(RID p_link) const;
COMMAND_2(link_set_bidirectional, RID, p_link, bool, p_bidirectional);
virtual bool link_is_bidirectional(RID p_link) const;
COMMAND_2(link_set_navigation_layers, RID, p_link, uint32_t, p_navigation_layers);

View File

@ -18,8 +18,8 @@ public:
virtual real_t map_get_cell_size(RID p_map) const { return 0; }
virtual void map_set_cell_height(RID p_map, real_t p_cell_height) {}
virtual real_t map_get_cell_height(RID p_map) const { return 0; }
void map_set_use_edge_connections(RID p_map, bool p_enabled) {}
bool map_get_use_edge_connections(RID p_map) const { return false; }
virtual void map_set_use_edge_connections(RID p_map, bool p_enabled) {}
virtual bool map_get_use_edge_connections(RID p_map) const { return false; }
virtual void map_set_edge_connection_margin(RID p_map, real_t p_connection_margin) {}
virtual real_t map_get_edge_connection_margin(RID p_map) const { return 0; }
@ -37,8 +37,10 @@ public:
virtual void map_force_update(RID p_map) {}
virtual RID region_create() { return RID(); }
void region_set_use_edge_connections(RID p_region, bool p_enabled) {}
bool region_get_use_edge_connections(RID p_region) const { return false; }
virtual void region_set_enabled(RID p_region, bool p_enabled) {}
virtual bool region_get_enabled(RID p_region) const { return false; }
virtual void region_set_use_edge_connections(RID p_region, bool p_enabled) {}
virtual bool region_get_use_edge_connections(RID p_region) const { return false; }
virtual void region_set_enter_cost(RID p_region, real_t p_enter_cost) {}
virtual real_t region_get_enter_cost(RID p_region) const { return 0; }
virtual void region_set_travel_cost(RID p_region, real_t p_travel_cost) {}
@ -59,6 +61,8 @@ public:
virtual RID link_create() { return RID(); }
virtual void link_set_map(RID p_link, RID p_map) {}
virtual RID link_get_map(RID p_link) const { return RID(); }
virtual void link_set_enabled(RID p_link, bool p_enabled) {}
virtual bool link_get_enabled(RID p_link) const { return false; }
virtual void link_set_bidirectional(RID p_link, bool p_bidirectional) {}
virtual bool link_is_bidirectional(RID p_link) const { return false; }
virtual void link_set_navigation_layers(RID p_link, uint32_t p_navigation_layers) {}

View File

@ -19,8 +19,8 @@ public:
virtual real_t map_get_cell_size(RID p_map) const { return 0; }
virtual void map_set_cell_height(RID p_map, real_t p_cell_height) {}
virtual real_t map_get_cell_height(RID p_map) const { return 0; }
void map_set_use_edge_connections(RID p_map, bool p_enabled) {}
bool map_get_use_edge_connections(RID p_map) const { return false; }
virtual void map_set_use_edge_connections(RID p_map, bool p_enabled) {}
virtual bool map_get_use_edge_connections(RID p_map) const { return false; }
virtual void map_set_edge_connection_margin(RID p_map, real_t p_connection_margin) {}
virtual real_t map_get_edge_connection_margin(RID p_map) const { return 0; }
virtual void map_set_link_connection_radius(RID p_map, real_t p_connection_radius) {}
@ -39,8 +39,10 @@ public:
virtual void map_force_update(RID p_map) {}
virtual RID region_create() { return RID(); }
void region_set_use_edge_connections(RID p_region, bool p_enabled) {}
bool region_get_use_edge_connections(RID p_region) const { return false; }
virtual void region_set_enabled(RID p_region, bool p_enabled) {}
virtual bool region_get_enabled(RID p_region) const { return false; }
virtual void region_set_use_edge_connections(RID p_region, bool p_enabled) {}
virtual bool region_get_use_edge_connections(RID p_region) const { return false; }
virtual void region_set_enter_cost(RID p_region, real_t p_enter_cost) {}
virtual real_t region_get_enter_cost(RID p_region) const { return 0; }
virtual void region_set_travel_cost(RID p_region, real_t p_travel_cost) {}
@ -61,6 +63,8 @@ public:
virtual RID link_create() { return RID(); }
virtual void link_set_map(RID p_link, RID p_map) {}
virtual RID link_get_map(RID p_link) const { return RID(); }
virtual void link_set_enabled(RID p_link, bool p_enabled) {}
virtual bool link_get_enabled(RID p_link) const { return false; }
virtual void link_set_bidirectional(RID p_link, bool p_bidirectional) {}
virtual bool link_is_bidirectional(RID p_link) const { return false; }
virtual void link_set_navigation_layers(RID p_link, uint32_t p_navigation_layers) {}

View File

@ -157,15 +157,7 @@ void NavigationLink2D::set_enabled(bool p_enabled) {
enabled = p_enabled;
if (!is_inside_tree()) {
return;
}
if (!enabled) {
Navigation2DServer::get_singleton()->link_set_map(link, RID());
} else {
Navigation2DServer::get_singleton()->link_set_map(link, get_world_2d()->get_navigation_map());
}
Navigation2DServer::get_singleton()->link_set_enabled(link, enabled);
#ifdef DEBUG_ENABLED
if (Engine::get_singleton()->is_editor_hint() || Navigation2DServer::get_singleton()->get_debug_navigation_enabled()) {

View File

@ -54,9 +54,7 @@ void NavigationPolygonInstance::set_enabled(bool p_enabled) {
}
enabled = p_enabled;
if (!is_inside_tree()) {
return;
}
Navigation2DServer::get_singleton()->region_set_enabled(region, enabled);
if (!enabled) {
Navigation2DServer::get_singleton()->region_set_map(region, RID());

View File

@ -31,9 +31,9 @@
#include "navigation_link_3d.h"
#include "core/config/engine.h"
#include "mesh_instance.h"
#include "scene/resources/mesh.h"
#include "scene/resources/world_3d.h"
#include "mesh_instance.h"
#include "servers/navigation_server.h"
#ifdef DEBUG_ENABLED
@ -268,15 +268,7 @@ void NavigationLink3D::set_enabled(bool p_enabled) {
enabled = p_enabled;
if (!is_inside_tree()) {
return;
}
if (enabled) {
NavigationServer::get_singleton()->link_set_map(link, get_world_3d()->get_navigation_map());
} else {
NavigationServer::get_singleton()->link_set_map(link, RID());
}
NavigationServer::get_singleton()->link_set_enabled(link, enabled);
#ifdef DEBUG_ENABLED
if (debug_instance.is_valid() && debug_mesh.is_valid()) {

View File

@ -47,23 +47,7 @@ void NavigationMeshInstance::set_enabled(bool p_enabled) {
}
enabled = p_enabled;
if (!is_inside_tree()) {
return;
}
if (!enabled) {
NavigationServer::get_singleton()->region_set_map(region, RID());
} else {
if (navigation) {
NavigationServer::get_singleton()->region_set_map(region, navigation->get_rid());
} else {
if (map_override.is_valid()) {
NavigationServer::get_singleton()->region_set_map(region, map_override);
} else {
NavigationServer::get_singleton()->region_set_map(region, get_world_3d()->get_navigation_map());
}
}
}
NavigationServer::get_singleton()->region_set_enabled(region, enabled);
#ifdef DEBUG_ENABLED
if (debug_instance.is_valid()) {

View File

@ -64,6 +64,8 @@ void Navigation2DServer::_bind_methods() {
ClassDB::bind_method(D_METHOD("map_force_update", "map"), &Navigation2DServer::map_force_update);
ClassDB::bind_method(D_METHOD("region_create"), &Navigation2DServer::region_create);
ClassDB::bind_method(D_METHOD("region_set_enabled", "region", "enabled"), &Navigation2DServer::region_set_enabled);
ClassDB::bind_method(D_METHOD("region_get_enabled", "region"), &Navigation2DServer::region_get_enabled);
ClassDB::bind_method(D_METHOD("region_set_use_edge_connections", "region", "enabled"), &Navigation2DServer::region_set_use_edge_connections);
ClassDB::bind_method(D_METHOD("region_get_use_edge_connections", "region"), &Navigation2DServer::region_get_use_edge_connections);
ClassDB::bind_method(D_METHOD("region_set_enter_cost", "region", "enter_cost"), &Navigation2DServer::region_set_enter_cost);
@ -86,6 +88,8 @@ void Navigation2DServer::_bind_methods() {
ClassDB::bind_method(D_METHOD("link_create"), &Navigation2DServer::link_create);
ClassDB::bind_method(D_METHOD("link_set_map", "link", "map"), &Navigation2DServer::link_set_map);
ClassDB::bind_method(D_METHOD("link_get_map", "link"), &Navigation2DServer::link_get_map);
ClassDB::bind_method(D_METHOD("link_set_enabled", "link", "enabled"), &Navigation2DServer::link_set_enabled);
ClassDB::bind_method(D_METHOD("link_get_enabled", "link"), &Navigation2DServer::link_get_enabled);
ClassDB::bind_method(D_METHOD("link_set_bidirectional", "link", "bidirectional"), &Navigation2DServer::link_set_bidirectional);
ClassDB::bind_method(D_METHOD("link_is_bidirectional", "link"), &Navigation2DServer::link_is_bidirectional);
ClassDB::bind_method(D_METHOD("link_set_navigation_layers", "link", "navigation_layers"), &Navigation2DServer::link_set_navigation_layers);

View File

@ -105,6 +105,9 @@ public:
/// Creates a new region.
virtual RID region_create() = 0;
virtual void region_set_enabled(RID p_region, bool p_enabled) = 0;
virtual bool region_get_enabled(RID p_region) const = 0;
virtual void region_set_use_edge_connections(RID p_region, bool p_enabled) = 0;
virtual bool region_get_use_edge_connections(RID p_region) const = 0;
@ -148,6 +151,9 @@ public:
virtual void link_set_map(RID p_link, RID p_map) = 0;
virtual RID link_get_map(RID p_link) const = 0;
virtual void link_set_enabled(RID p_link, bool p_enabled) = 0;
virtual bool link_get_enabled(RID p_link) const = 0;
/// Set whether this link travels in both directions.
virtual void link_set_bidirectional(RID p_link, bool p_bidirectional) = 0;
virtual bool link_is_bidirectional(RID p_link) const = 0;

View File

@ -83,6 +83,7 @@ void NavigationServer::_bind_methods() {
ClassDB::bind_method(D_METHOD("query_path", "parameters", "result"), &NavigationServer::query_path);
ClassDB::bind_method(D_METHOD("region_create"), &NavigationServer::region_create);
ClassDB::bind_method(D_METHOD("region_set_enabled", "region", "enabled"), &NavigationServer::region_set_enabled);
ClassDB::bind_method(D_METHOD("region_set_use_edge_connections", "region", "enabled"), &NavigationServer::region_set_use_edge_connections);
ClassDB::bind_method(D_METHOD("region_get_use_edge_connections", "region"), &NavigationServer::region_get_use_edge_connections);
ClassDB::bind_method(D_METHOD("region_set_enter_cost", "region", "enter_cost"), &NavigationServer::region_set_enter_cost);
@ -108,6 +109,8 @@ void NavigationServer::_bind_methods() {
ClassDB::bind_method(D_METHOD("link_create"), &NavigationServer::link_create);
ClassDB::bind_method(D_METHOD("link_set_map", "link", "map"), &NavigationServer::link_set_map);
ClassDB::bind_method(D_METHOD("link_get_map", "link"), &NavigationServer::link_get_map);
ClassDB::bind_method(D_METHOD("link_set_enabled", "link", "enabled"), &NavigationServer::link_set_enabled);
ClassDB::bind_method(D_METHOD("link_get_enabled", "link"), &NavigationServer::link_get_enabled);
ClassDB::bind_method(D_METHOD("link_set_bidirectional", "link", "bidirectional"), &NavigationServer::link_set_bidirectional);
ClassDB::bind_method(D_METHOD("link_is_bidirectional", "link"), &NavigationServer::link_is_bidirectional);
ClassDB::bind_method(D_METHOD("link_set_navigation_layers", "link", "navigation_layers"), &NavigationServer::link_set_navigation_layers);

View File

@ -121,6 +121,9 @@ public:
/// Creates a new region.
virtual RID region_create() = 0;
virtual void region_set_enabled(RID p_region, bool p_enabled) = 0;
virtual bool region_get_enabled(RID p_region) const = 0;
virtual void region_set_use_edge_connections(RID p_region, bool p_enabled) = 0;
virtual bool region_get_use_edge_connections(RID p_region) const = 0;
@ -164,6 +167,9 @@ public:
virtual void link_set_map(RID p_link, RID p_map) = 0;
virtual RID link_get_map(RID p_link) const = 0;
virtual void link_set_enabled(RID p_link, bool p_enabled) = 0;
virtual bool link_get_enabled(RID p_link) const = 0;
/// Set whether this link travels in both directions.
virtual void link_set_bidirectional(RID p_link, bool p_bidirectional) = 0;
virtual bool link_is_bidirectional(RID p_link) const = 0;