Ported from godot4: Enable assigning an owner to navigation regions and links

This allows users of the server APIs to get back the nodes that created certain regions and links.
- DarkKilauea
5769b0e8d8
This commit is contained in:
Relintai 2023-06-09 19:46:44 +02:00
parent 160cb38a50
commit 29784d5d5b
18 changed files with 146 additions and 6 deletions

View File

@ -173,6 +173,13 @@
Returns the navigation layers for this [code]link[/code].
</description>
</method>
<method name="link_get_owner_id" qualifiers="const">
<return type="int" />
<param index="0" name="link" type="RID" />
<description>
Returns the [code]ObjectID[/code] of the object which manages this link.
</description>
</method>
<method name="link_get_start_position" qualifiers="const">
<return type="Vector2" />
<param index="0" name="link" type="RID" />
@ -234,6 +241,14 @@
Set the links's navigation layers. This allows selecting links from a path request (when using [method NavigationServer2D.map_get_path]).
</description>
</method>
<method name="link_set_owner_id" qualifiers="const">
<return type="void" />
<param index="0" name="link" type="RID" />
<param index="1" name="owner_id" type="int" />
<description>
Set the [code]ObjectID[/code] of the object which manages this link.
</description>
</method>
<method name="link_set_start_position" qualifiers="const">
<return type="void" />
<param index="0" name="link" type="RID" />
@ -447,6 +462,13 @@
Returns the region's navigation layers.
</description>
</method>
<method name="region_get_owner_id" qualifiers="const">
<return type="int" />
<param index="0" name="region" type="RID" />
<description>
Returns the [code]ObjectID[/code] of the object which manages this region.
</description>
</method>
<method name="region_get_travel_cost" qualifiers="const">
<return type="float" />
<argument index="0" name="region" type="RID" />
@ -496,6 +518,14 @@
Sets the navigation mesh for the region.
</description>
</method>
<method name="region_set_owner_id" qualifiers="const">
<return type="void" />
<param index="0" name="region" type="RID" />
<param index="1" name="owner_id" type="int" />
<description>
Set the [code]ObjectID[/code] of the object which manages this region.
</description>
</method>
<method name="region_set_transform" qualifiers="const">
<return type="void" />
<argument index="0" name="region" type="RID" />

View File

@ -181,6 +181,13 @@
Returns the navigation layers for this [code]link[/code].
</description>
</method>
<method name="link_get_owner_id" qualifiers="const">
<return type="int" />
<param index="0" name="link" type="RID" />
<description>
Returns the [code]ObjectID[/code] of the object which manages this link.
</description>
</method>
<method name="link_get_start_position" qualifiers="const">
<return type="Vector3" />
<param index="0" name="link" type="RID" />
@ -242,6 +249,14 @@
Set the links's navigation layers. This allows selecting links from a path request (when using [method NavigationServer3D.map_get_path]).
</description>
</method>
<method name="link_set_owner_id" qualifiers="const">
<return type="void" />
<param index="0" name="link" type="RID" />
<param index="1" name="owner_id" type="int" />
<description>
Set the [code]ObjectID[/code] of the object which manages this link.
</description>
</method>
<method name="link_set_start_position" qualifiers="const">
<return type="void" />
<param index="0" name="link" type="RID" />
@ -497,6 +512,13 @@
Returns the region's navigation layers.
</description>
</method>
<method name="region_get_owner_id" qualifiers="const">
<return type="int" />
<param index="0" name="region" type="RID" />
<description>
Returns the [code]ObjectID[/code] of the object which manages this region.
</description>
</method>
<method name="region_get_travel_cost" qualifiers="const">
<return type="float" />
<argument index="0" name="region" type="RID" />
@ -546,6 +568,14 @@
Sets the navigation mesh for the region.
</description>
</method>
<method name="region_set_owner_id" qualifiers="const">
<return type="void" />
<param index="0" name="region" type="RID" />
<param index="1" name="owner_id" type="int" />
<description>
Set the [code]ObjectID[/code] of the object which manages this region.
</description>
</method>
<method name="region_set_transform" qualifiers="const">
<return type="void" />
<argument index="0" name="region" type="RID" />

View File

@ -582,6 +582,7 @@ bool GridMap::_octant_update(const OctantKey &p_key) {
if (bake_navigation) {
RID region = NavigationServer::get_singleton()->region_create();
NavigationServer::get_singleton()->region_set_owner_id(region, get_instance_id());
NavigationServer::get_singleton()->region_set_navigation_layers(region, navigation_layers);
NavigationServer::get_singleton()->region_set_navmesh(region, navmesh);
NavigationServer::get_singleton()->region_set_transform(region, get_global_transform() * nm.xform);
@ -700,6 +701,7 @@ void GridMap::_octant_enter_world(const OctantKey &p_key) {
Ref<NavigationMesh> nm = mesh_library->get_item_navmesh(cell_map[E->key()].item);
if (nm.is_valid()) {
RID region = NavigationServer::get_singleton()->region_create();
NavigationServer::get_singleton()->region_set_owner_id(region, get_instance_id());
NavigationServer::get_singleton()->region_set_navigation_layers(region, navigation_layers);
NavigationServer::get_singleton()->region_set_navmesh(region, nm);
NavigationServer::get_singleton()->region_set_transform(region, get_global_transform() * E->get().xform);

View File

@ -52,9 +52,9 @@
return NavigationServer::get_singleton()->FUNC_NAME(CONV_0(D_0)); \
}
#define FORWARD_1_R_C(CONV_R, FUNC_NAME, T_0, D_0, CONV_0) \
PandemoniumNavigation2DServer::FUNC_NAME(T_0 D_0) \
const { \
#define FORWARD_1_R_C(CONV_R, FUNC_NAME, T_0, D_0, CONV_0) \
PandemoniumNavigation2DServer::FUNC_NAME(T_0 D_0) \
const { \
return CONV_R(NavigationServer::get_singleton()->FUNC_NAME(CONV_0(D_0))); \
}
@ -243,6 +243,8 @@ void FORWARD_2_C(region_set_enter_cost, RID, p_region, real_t, p_enter_cost, rid
real_t FORWARD_1_C(region_get_enter_cost, RID, p_region, rid_to_rid);
void FORWARD_2_C(region_set_travel_cost, RID, p_region, real_t, p_travel_cost, rid_to_rid, real_to_real);
real_t FORWARD_1_C(region_get_travel_cost, RID, p_region, rid_to_rid);
void FORWARD_2_C(region_set_owner_id, RID, p_region, ObjectID, p_owner_id, rid_to_rid, id_to_id);
ObjectID FORWARD_1_C(region_get_owner_id, RID, p_region, rid_to_rid);
bool FORWARD_2_C(region_owns_point, RID, p_region, const Vector2 &, p_point, rid_to_rid, v2_to_v3);
void FORWARD_2_C(region_set_map, RID, p_region, RID, p_map, rid_to_rid, rid_to_rid);
@ -274,6 +276,8 @@ void FORWARD_2_C(link_set_enter_cost, RID, p_link, real_t, p_enter_cost, rid_to_
real_t FORWARD_1_C(link_get_enter_cost, RID, p_link, rid_to_rid);
void FORWARD_2_C(link_set_travel_cost, RID, p_link, real_t, p_travel_cost, rid_to_rid, real_to_real);
real_t FORWARD_1_C(link_get_travel_cost, RID, p_link, rid_to_rid);
void FORWARD_2_C(link_set_owner_id, RID, p_link, ObjectID, p_owner_id, rid_to_rid, id_to_id);
ObjectID FORWARD_1_C(link_get_owner_id, RID, p_link, rid_to_rid);
RID PandemoniumNavigation2DServer::agent_create() const {
RID agent = NavigationServer::get_singleton()->agent_create();

View File

@ -102,6 +102,10 @@ public:
virtual void region_set_travel_cost(RID p_region, real_t p_travel_cost) const;
virtual real_t region_get_travel_cost(RID p_region) const;
/// Set the node which manages this region.
virtual void region_set_owner_id(RID p_region, ObjectID p_owner_id) const;
virtual ObjectID region_get_owner_id(RID p_region) const;
virtual bool region_owns_point(RID p_region, const Vector2 &p_point) const;
/// Set the map of this region.
@ -154,6 +158,10 @@ public:
virtual void link_set_travel_cost(RID p_link, real_t p_travel_cost) const;
virtual real_t link_get_travel_cost(RID p_link) const;
/// Set the node which manages this link.
virtual void link_set_owner_id(RID p_link, ObjectID p_owner_id) const;
virtual ObjectID link_get_owner_id(RID p_link) const;
/// Creates the agent.
virtual RID agent_create() const;

View File

@ -412,6 +412,20 @@ real_t PandemoniumNavigationServer::region_get_travel_cost(RID p_region) const {
return region->get_travel_cost();
}
COMMAND_2(region_set_owner_id, RID, p_region, ObjectID, p_owner_id) {
NavRegion *region = region_owner.getornull(p_region);
ERR_FAIL_COND(region == nullptr);
region->set_owner_id(p_owner_id);
}
ObjectID PandemoniumNavigationServer::region_get_owner_id(RID p_region) const {
const NavRegion *region = region_owner.getornull(p_region);
ERR_FAIL_COND_V(region == nullptr, ObjectID());
return region->get_owner_id();
}
bool PandemoniumNavigationServer::region_owns_point(RID p_region, const Vector3 &p_point) const {
const NavRegion *region = region_owner.getornull(p_region);
ERR_FAIL_COND_V(region == nullptr, false);
@ -589,6 +603,20 @@ real_t PandemoniumNavigationServer::link_get_travel_cost(const RID p_link) const
return link->get_travel_cost();
}
COMMAND_2(link_set_owner_id, RID, p_link, ObjectID, p_owner_id) {
NavLink *link = link_owner.getornull(p_link);
ERR_FAIL_COND(link == nullptr);
link->set_owner_id(p_owner_id);
}
ObjectID PandemoniumNavigationServer::link_get_owner_id(RID p_link) const {
const NavLink *link = link_owner.getornull(p_link);
ERR_FAIL_COND_V(link == nullptr, ObjectID());
return link->get_owner_id();
}
RID PandemoniumNavigationServer::agent_create() const {
PandemoniumNavigationServer *mut_this = const_cast<PandemoniumNavigationServer *>(this);
MutexLock lock(mut_this->operations_mutex);

View File

@ -135,6 +135,8 @@ public:
virtual real_t region_get_enter_cost(RID p_region) const;
COMMAND_2(region_set_travel_cost, RID, p_region, real_t, p_travel_cost);
virtual real_t region_get_travel_cost(RID p_region) const;
COMMAND_2(region_set_owner_id, RID, p_region, ObjectID, p_owner_id);
virtual ObjectID region_get_owner_id(RID p_region) const;
virtual bool region_owns_point(RID p_region, const Vector3 &p_point) const;
@ -164,6 +166,8 @@ public:
virtual real_t link_get_enter_cost(RID p_link) const;
COMMAND_2(link_set_travel_cost, RID, p_link, real_t, p_travel_cost);
virtual real_t link_get_travel_cost(RID p_link) const;
COMMAND_2(link_set_owner_id, RID, p_link, ObjectID, p_owner_id);
virtual ObjectID link_get_owner_id(RID p_link) const;
virtual RID agent_create() const;
COMMAND_2(agent_set_map, RID, p_agent, RID, p_map);

View File

@ -38,6 +38,8 @@ public:
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) const {}
virtual real_t region_get_travel_cost(RID p_region) const { return 0; }
virtual void region_set_owner_id(RID p_region, ObjectID p_owner_id) const {}
virtual ObjectID region_get_owner_id(RID p_region) const { return 0; }
virtual bool region_owns_point(RID p_region, const Vector2 &p_point) const { return false; }
virtual void region_set_map(RID p_region, RID p_map) const {}
virtual RID region_get_map(RID p_region) const { return RID(); }
@ -64,6 +66,8 @@ public:
virtual real_t link_get_enter_cost(RID p_link) const { return 0; }
virtual void link_set_travel_cost(RID p_link, real_t p_travel_cost) const {}
virtual real_t link_get_travel_cost(RID p_link) const { return 0; }
virtual void link_set_owner_id(RID p_link, ObjectID p_owner_id) const {}
virtual ObjectID link_get_owner_id(RID p_link) const { return 0; }
virtual RID agent_create() const { return RID(); }
virtual void agent_set_map(RID p_agent, RID p_map) const {}

View File

@ -40,6 +40,8 @@ public:
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) const {}
virtual real_t region_get_travel_cost(RID p_region) const { return 0; }
virtual void region_set_owner_id(RID p_region, ObjectID p_owner_id) const {}
virtual ObjectID region_get_owner_id(RID p_region) const { return 0; }
virtual bool region_owns_point(RID p_region, const Vector3 &p_point) const { return false; }
virtual void region_set_map(RID p_region, RID p_map) const {}
virtual RID region_get_map(RID p_region) const { return RID(); }
@ -66,6 +68,8 @@ public:
virtual real_t link_get_enter_cost(RID p_link) const { return 0; }
virtual void link_set_travel_cost(RID p_link, real_t p_travel_cost) const {}
virtual real_t link_get_travel_cost(RID p_link) const { return 0; }
virtual void link_set_owner_id(RID p_link, ObjectID p_owner_id) const {}
virtual ObjectID link_get_owner_id(RID p_link) const { return 0; }
virtual RID agent_create() const { return RID(); }
virtual void agent_set_map(RID p_agent, RID p_map) const {}

View File

@ -679,6 +679,7 @@ void TileMap::update_dirty_quadrants() {
_fix_cell_transform(xform, c, npoly_ofs, s);
RID region = Navigation2DServer::get_singleton()->region_create();
Navigation2DServer::get_singleton()->region_set_owner_id(region, get_instance_id());
if (navigation) {
Navigation2DServer::get_singleton()->region_set_map(region, navigation->get_rid());
} else {

View File

@ -332,7 +332,7 @@ NavigationLink2D::NavigationLink2D() {
travel_cost = 1.0;
link = Navigation2DServer::get_singleton()->link_create();
//Navigation2DServer::get_singleton()->link_set_owner_id(link, get_instance_id());
Navigation2DServer::get_singleton()->link_set_owner_id(link, get_instance_id());
set_notify_transform(true);
}

View File

@ -394,7 +394,7 @@ NavigationPolygonInstance::NavigationPolygonInstance() {
navigation_layers = 1;
baking_started = false;
Navigation2DServer::get_singleton()->region_set_owner_id(region, get_instance_id());
Navigation2DServer::get_singleton()->region_set_enter_cost(region, get_enter_cost());
Navigation2DServer::get_singleton()->region_set_travel_cost(region, get_travel_cost());

View File

@ -240,7 +240,7 @@ void NavigationLink3D::_notification(int p_what) {
NavigationLink3D::NavigationLink3D() {
link = NavigationServer::get_singleton()->link_create();
//NavigationServer::get_singleton()->link_set_owner_id(link, get_instance_id());
NavigationServer::get_singleton()->link_set_owner_id(link, get_instance_id());
set_notify_transform(true);
}

View File

@ -404,6 +404,7 @@ NavigationMeshInstance::NavigationMeshInstance() {
baking_started = false;
region = NavigationServer::get_singleton()->region_create();
NavigationServer::get_singleton()->region_set_owner_id(region, get_instance_id());
NavigationServer::get_singleton()->region_set_enter_cost(region, get_enter_cost());
NavigationServer::get_singleton()->region_set_travel_cost(region, get_travel_cost());
enabled = true;

View File

@ -65,6 +65,8 @@ void Navigation2DServer::_bind_methods() {
ClassDB::bind_method(D_METHOD("region_get_enter_cost", "region"), &Navigation2DServer::region_get_enter_cost);
ClassDB::bind_method(D_METHOD("region_set_travel_cost", "region", "travel_cost"), &Navigation2DServer::region_set_travel_cost);
ClassDB::bind_method(D_METHOD("region_get_travel_cost", "region"), &Navigation2DServer::region_get_travel_cost);
ClassDB::bind_method(D_METHOD("region_set_owner_id", "region", "owner_id"), &Navigation2DServer::region_set_owner_id);
ClassDB::bind_method(D_METHOD("region_get_owner_id", "region"), &Navigation2DServer::region_get_owner_id);
ClassDB::bind_method(D_METHOD("region_owns_point", "region", "point"), &Navigation2DServer::region_owns_point);
ClassDB::bind_method(D_METHOD("region_set_map", "region", "map"), &Navigation2DServer::region_set_map);
ClassDB::bind_method(D_METHOD("region_get_map", "region"), &Navigation2DServer::region_get_map);
@ -91,6 +93,8 @@ void Navigation2DServer::_bind_methods() {
ClassDB::bind_method(D_METHOD("link_get_enter_cost", "link"), &Navigation2DServer::link_get_enter_cost);
ClassDB::bind_method(D_METHOD("link_set_travel_cost", "link", "travel_cost"), &Navigation2DServer::link_set_travel_cost);
ClassDB::bind_method(D_METHOD("link_get_travel_cost", "link"), &Navigation2DServer::link_get_travel_cost);
ClassDB::bind_method(D_METHOD("link_set_owner_id", "link", "owner_id"), &Navigation2DServer::link_set_owner_id);
ClassDB::bind_method(D_METHOD("link_get_owner_id", "link"), &Navigation2DServer::link_get_owner_id);
ClassDB::bind_method(D_METHOD("agent_create"), &Navigation2DServer::agent_create);
ClassDB::bind_method(D_METHOD("agent_set_map", "agent", "map"), &Navigation2DServer::agent_set_map);

View File

@ -113,6 +113,10 @@ public:
virtual void region_set_travel_cost(RID p_region, real_t p_travel_cost) const = 0;
virtual real_t region_get_travel_cost(RID p_region) const = 0;
/// Set the node which manages this region.
virtual void region_set_owner_id(RID p_region, ObjectID p_owner_id) const = 0;
virtual ObjectID region_get_owner_id(RID p_region) const = 0;
virtual bool region_owns_point(RID p_region, const Vector2 &p_point) const = 0;
/// Set the map of this region.
@ -165,6 +169,10 @@ public:
virtual void link_set_travel_cost(RID p_link, real_t p_travel_cost) const = 0;
virtual real_t link_get_travel_cost(RID p_link) const = 0;
/// Set the node which manages this link.
virtual void link_set_owner_id(RID p_link, ObjectID p_owner_id) const = 0;
virtual ObjectID link_get_owner_id(RID p_link) const = 0;
/// Creates the agent.
virtual RID agent_create() const = 0;

View File

@ -81,6 +81,8 @@ void NavigationServer::_bind_methods() {
ClassDB::bind_method(D_METHOD("region_get_enter_cost", "region"), &NavigationServer::region_get_enter_cost);
ClassDB::bind_method(D_METHOD("region_set_travel_cost", "region", "travel_cost"), &NavigationServer::region_set_travel_cost);
ClassDB::bind_method(D_METHOD("region_get_travel_cost", "region"), &NavigationServer::region_get_travel_cost);
ClassDB::bind_method(D_METHOD("region_set_owner_id", "region", "owner_id"), &NavigationServer::region_set_owner_id);
ClassDB::bind_method(D_METHOD("region_get_owner_id", "region"), &NavigationServer::region_get_owner_id);
ClassDB::bind_method(D_METHOD("region_owns_point", "region", "point"), &NavigationServer::region_owns_point);
ClassDB::bind_method(D_METHOD("region_set_map", "region", "map"), &NavigationServer::region_set_map);
ClassDB::bind_method(D_METHOD("region_get_map", "region"), &NavigationServer::region_get_map);
@ -110,6 +112,8 @@ void NavigationServer::_bind_methods() {
ClassDB::bind_method(D_METHOD("link_get_enter_cost", "link"), &NavigationServer::link_get_enter_cost);
ClassDB::bind_method(D_METHOD("link_set_travel_cost", "link", "travel_cost"), &NavigationServer::link_set_travel_cost);
ClassDB::bind_method(D_METHOD("link_get_travel_cost", "link"), &NavigationServer::link_get_travel_cost);
ClassDB::bind_method(D_METHOD("link_set_owner_id", "link", "owner_id"), &NavigationServer::link_set_owner_id);
ClassDB::bind_method(D_METHOD("link_get_owner_id", "link"), &NavigationServer::link_get_owner_id);
ClassDB::bind_method(D_METHOD("agent_create"), &NavigationServer::agent_create);
ClassDB::bind_method(D_METHOD("agent_set_map", "agent", "map"), &NavigationServer::agent_set_map);

View File

@ -129,6 +129,10 @@ public:
virtual void region_set_travel_cost(RID p_region, real_t p_travel_cost) const = 0;
virtual real_t region_get_travel_cost(RID p_region) const = 0;
/// Set the node which manages this region.
virtual void region_set_owner_id(RID p_region, ObjectID p_owner_id) const = 0;
virtual ObjectID region_get_owner_id(RID p_region) const = 0;
virtual bool region_owns_point(RID p_region, const Vector3 &p_point) const = 0;
/// Set the map of this region.
@ -181,6 +185,10 @@ public:
virtual void link_set_travel_cost(RID p_link, real_t p_travel_cost) const = 0;
virtual real_t link_get_travel_cost(RID p_link) const = 0;
/// Set the node which manages this link.
virtual void link_set_owner_id(RID p_link, ObjectID p_owner_id) const = 0;
virtual ObjectID link_get_owner_id(RID p_link) const = 0;
/// Creates the agent.
virtual RID agent_create() const = 0;