Add NavigationServer2D/3D API functions to find missing RID info

Utility functions for NavigationServer2D/3D to find missing RID information when working with Server API directly. e.g. from map to regions and agents, from agent or region to map, from region to map and agents and so on ....

Requirement to work with NavigationServer API exklusive without SceneTree nodes and when juggling agents and regions between multiple navigation maps.

(cherry picked from commit 371054e3e5)
This commit is contained in:
smix8 2022-05-12 02:52:48 +02:00 committed by Rémi Verschelde
parent c3b39ca1e9
commit 3977eb9107
8 changed files with 121 additions and 0 deletions

View file

@ -20,6 +20,13 @@
Creates the agent. Creates the agent.
</description> </description>
</method> </method>
<method name="agent_get_map" qualifiers="const">
<return type="RID" />
<argument index="0" name="agent" type="RID" />
<description>
Returns the navigation map [RID] the requested [code]agent[/code] is currently assigned to.
</description>
</method>
<method name="agent_is_map_changed" qualifiers="const"> <method name="agent_is_map_changed" qualifiers="const">
<return type="bool" /> <return type="bool" />
<argument index="0" name="agent" type="RID" /> <argument index="0" name="agent" type="RID" />
@ -122,6 +129,13 @@
Create a new map. Create a new map.
</description> </description>
</method> </method>
<method name="map_get_agents" qualifiers="const">
<return type="Array" />
<argument index="0" name="map" type="RID" />
<description>
Returns all navigation agents [RID]s that are currently assigned to the requested navigation [code]map[/code].
</description>
</method>
<method name="map_get_cell_size" qualifiers="const"> <method name="map_get_cell_size" qualifiers="const">
<return type="float" /> <return type="float" />
<argument index="0" name="map" type="RID" /> <argument index="0" name="map" type="RID" />
@ -162,6 +176,13 @@
Returns the navigation path to reach the destination from the origin. Returns the navigation path to reach the destination from the origin.
</description> </description>
</method> </method>
<method name="map_get_regions" qualifiers="const">
<return type="Array" />
<argument index="0" name="map" type="RID" />
<description>
Returns all navigation regions [RID]s that are currently assigned to the requested navigation [code]map[/code].
</description>
</method>
<method name="map_is_active" qualifiers="const"> <method name="map_is_active" qualifiers="const">
<return type="bool" /> <return type="bool" />
<argument index="0" name="nap" type="RID" /> <argument index="0" name="nap" type="RID" />
@ -199,6 +220,13 @@
Creates a new region. Creates a new region.
</description> </description>
</method> </method>
<method name="region_get_map" qualifiers="const">
<return type="RID" />
<argument index="0" name="region" type="RID" />
<description>
Returns the navigation map [RID] the requested [code]region[/code] is currently assigned to.
</description>
</method>
<method name="region_set_map" qualifiers="const"> <method name="region_set_map" qualifiers="const">
<return type="void" /> <return type="void" />
<argument index="0" name="region" type="RID" /> <argument index="0" name="region" type="RID" />

View file

@ -20,6 +20,13 @@
Creates the agent. Creates the agent.
</description> </description>
</method> </method>
<method name="agent_get_map" qualifiers="const">
<return type="RID" />
<argument index="0" name="agent" type="RID" />
<description>
Returns the navigation map [RID] the requested [code]agent[/code] is currently assigned to.
</description>
</method>
<method name="agent_is_map_changed" qualifiers="const"> <method name="agent_is_map_changed" qualifiers="const">
<return type="bool" /> <return type="bool" />
<argument index="0" name="agent" type="RID" /> <argument index="0" name="agent" type="RID" />
@ -122,6 +129,13 @@
Create a new map. Create a new map.
</description> </description>
</method> </method>
<method name="map_get_agents" qualifiers="const">
<return type="Array" />
<argument index="0" name="map" type="RID" />
<description>
Returns all navigation agents [RID]s that are currently assigned to the requested navigation [code]map[/code].
</description>
</method>
<method name="map_get_cell_height" qualifiers="const"> <method name="map_get_cell_height" qualifiers="const">
<return type="float" /> <return type="float" />
<argument index="0" name="map" type="RID" /> <argument index="0" name="map" type="RID" />
@ -187,6 +201,13 @@
Returns the navigation path to reach the destination from the origin. Returns the navigation path to reach the destination from the origin.
</description> </description>
</method> </method>
<method name="map_get_regions" qualifiers="const">
<return type="Array" />
<argument index="0" name="map" type="RID" />
<description>
Returns all navigation regions [RID]s that are currently assigned to the requested navigation [code]map[/code].
</description>
</method>
<method name="map_get_up" qualifiers="const"> <method name="map_get_up" qualifiers="const">
<return type="Vector3" /> <return type="Vector3" />
<argument index="0" name="map" type="RID" /> <argument index="0" name="map" type="RID" />
@ -264,6 +285,13 @@
Creates a new region. Creates a new region.
</description> </description>
</method> </method>
<method name="region_get_map" qualifiers="const">
<return type="RID" />
<argument index="0" name="region" type="RID" />
<description>
Returns the navigation map [RID] the requested [code]region[/code] is currently assigned to.
</description>
</method>
<method name="region_set_map" qualifiers="const"> <method name="region_set_map" qualifiers="const">
<return type="void" /> <return type="void" />
<argument index="0" name="region" type="RID" /> <argument index="0" name="region" type="RID" />

View file

@ -250,6 +250,38 @@ RID GodotNavigationServer::map_get_closest_point_owner(RID p_map, const Vector3
return map->get_closest_point_owner(p_point); return map->get_closest_point_owner(p_point);
} }
Array GodotNavigationServer::map_get_regions(RID p_map) const {
Array regions_rids;
const NavMap *map = map_owner.getornull(p_map);
ERR_FAIL_COND_V(map == nullptr, regions_rids);
for (NavRegion *region : map->get_regions()) {
regions_rids.push_back(region->get_self());
}
return regions_rids;
}
Array GodotNavigationServer::map_get_agents(RID p_map) const {
Array agents_rids;
const NavMap *map = map_owner.getornull(p_map);
ERR_FAIL_COND_V(map == nullptr, agents_rids);
for (RvoAgent *agent : map->get_agents()) {
agents_rids.push_back(agent->get_self());
}
return agents_rids;
}
RID GodotNavigationServer::region_get_map(RID p_region) const {
NavRegion *region = region_owner.getornull(p_region);
ERR_FAIL_COND_V(region == nullptr, RID());
return region->get_map()->get_self();
}
RID GodotNavigationServer::agent_get_map(RID p_agent) const {
RvoAgent *agent = agent_owner.getornull(p_agent);
ERR_FAIL_COND_V(agent == nullptr, RID());
return agent->get_map()->get_self();
}
RID GodotNavigationServer::region_create() const { RID GodotNavigationServer::region_create() const {
auto mut_this = const_cast<GodotNavigationServer *>(this); auto mut_this = const_cast<GodotNavigationServer *>(this);
MutexLock lock(mut_this->operations_mutex); MutexLock lock(mut_this->operations_mutex);

View file

@ -108,14 +108,19 @@ public:
virtual Vector3 map_get_closest_point_normal(RID p_map, const Vector3 &p_point) const; virtual Vector3 map_get_closest_point_normal(RID p_map, const Vector3 &p_point) const;
virtual RID map_get_closest_point_owner(RID p_map, const Vector3 &p_point) const; virtual RID map_get_closest_point_owner(RID p_map, const Vector3 &p_point) const;
virtual Array map_get_regions(RID p_map) const;
virtual Array map_get_agents(RID p_map) const;
virtual RID region_create() const; virtual RID region_create() const;
COMMAND_2(region_set_map, RID, p_region, RID, p_map); COMMAND_2(region_set_map, RID, p_region, RID, p_map);
virtual RID region_get_map(RID p_region) const;
COMMAND_2(region_set_transform, RID, p_region, Transform, p_transform); COMMAND_2(region_set_transform, RID, p_region, Transform, p_transform);
COMMAND_2(region_set_navmesh, RID, p_region, Ref<NavigationMesh>, p_nav_mesh); COMMAND_2(region_set_navmesh, RID, p_region, Ref<NavigationMesh>, p_nav_mesh);
virtual void region_bake_navmesh(Ref<NavigationMesh> r_mesh, Node *p_node) const; virtual void region_bake_navmesh(Ref<NavigationMesh> r_mesh, Node *p_node) const;
virtual RID agent_create() const; virtual RID agent_create() const;
COMMAND_2(agent_set_map, RID, p_agent, RID, p_map); COMMAND_2(agent_set_map, RID, p_agent, RID, p_map);
virtual RID agent_get_map(RID p_agent) const;
COMMAND_2(agent_set_neighbor_dist, RID, p_agent, real_t, p_dist); COMMAND_2(agent_set_neighbor_dist, RID, p_agent, real_t, p_dist);
COMMAND_2(agent_set_max_neighbors, RID, p_agent, int, p_count); COMMAND_2(agent_set_max_neighbors, RID, p_agent, int, p_count);
COMMAND_2(agent_set_time_horizon, RID, p_agent, real_t, p_time); COMMAND_2(agent_set_time_horizon, RID, p_agent, real_t, p_time);

View file

@ -142,13 +142,18 @@ void Navigation2DServer::_bind_methods() {
ClassDB::bind_method(D_METHOD("map_get_closest_point", "map", "to_point"), &Navigation2DServer::map_get_closest_point); ClassDB::bind_method(D_METHOD("map_get_closest_point", "map", "to_point"), &Navigation2DServer::map_get_closest_point);
ClassDB::bind_method(D_METHOD("map_get_closest_point_owner", "map", "to_point"), &Navigation2DServer::map_get_closest_point_owner); ClassDB::bind_method(D_METHOD("map_get_closest_point_owner", "map", "to_point"), &Navigation2DServer::map_get_closest_point_owner);
ClassDB::bind_method(D_METHOD("map_get_regions", "map"), &Navigation2DServer::map_get_regions);
ClassDB::bind_method(D_METHOD("map_get_agents", "map"), &Navigation2DServer::map_get_agents);
ClassDB::bind_method(D_METHOD("region_create"), &Navigation2DServer::region_create); ClassDB::bind_method(D_METHOD("region_create"), &Navigation2DServer::region_create);
ClassDB::bind_method(D_METHOD("region_set_map", "region", "map"), &Navigation2DServer::region_set_map); 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);
ClassDB::bind_method(D_METHOD("region_set_transform", "region", "transform"), &Navigation2DServer::region_set_transform); ClassDB::bind_method(D_METHOD("region_set_transform", "region", "transform"), &Navigation2DServer::region_set_transform);
ClassDB::bind_method(D_METHOD("region_set_navpoly", "region", "nav_poly"), &Navigation2DServer::region_set_navpoly); ClassDB::bind_method(D_METHOD("region_set_navpoly", "region", "nav_poly"), &Navigation2DServer::region_set_navpoly);
ClassDB::bind_method(D_METHOD("agent_create"), &Navigation2DServer::agent_create); ClassDB::bind_method(D_METHOD("agent_create"), &Navigation2DServer::agent_create);
ClassDB::bind_method(D_METHOD("agent_set_map", "agent", "map"), &Navigation2DServer::agent_set_map); ClassDB::bind_method(D_METHOD("agent_set_map", "agent", "map"), &Navigation2DServer::agent_set_map);
ClassDB::bind_method(D_METHOD("agent_get_map", "agent"), &Navigation2DServer::agent_get_map);
ClassDB::bind_method(D_METHOD("agent_set_neighbor_dist", "agent", "dist"), &Navigation2DServer::agent_set_neighbor_dist); ClassDB::bind_method(D_METHOD("agent_set_neighbor_dist", "agent", "dist"), &Navigation2DServer::agent_set_neighbor_dist);
ClassDB::bind_method(D_METHOD("agent_set_max_neighbors", "agent", "count"), &Navigation2DServer::agent_set_max_neighbors); ClassDB::bind_method(D_METHOD("agent_set_max_neighbors", "agent", "count"), &Navigation2DServer::agent_set_max_neighbors);
ClassDB::bind_method(D_METHOD("agent_set_time_horizon", "agent", "time"), &Navigation2DServer::agent_set_time_horizon); ClassDB::bind_method(D_METHOD("agent_set_time_horizon", "agent", "time"), &Navigation2DServer::agent_set_time_horizon);
@ -171,6 +176,14 @@ Navigation2DServer::~Navigation2DServer() {
singleton = nullptr; singleton = nullptr;
} }
Array FORWARD_1_C(map_get_regions, RID, p_map, rid_to_rid);
Array FORWARD_1_C(map_get_agents, RID, p_map, rid_to_rid);
RID FORWARD_1_C(region_get_map, RID, p_region, rid_to_rid);
RID FORWARD_1_C(agent_get_map, RID, p_agent, rid_to_rid);
RID FORWARD_0_C(map_create); RID FORWARD_0_C(map_create);
void FORWARD_2_C(map_set_active, RID, p_map, bool, p_active, rid_to_rid, bool_to_bool); void FORWARD_2_C(map_set_active, RID, p_map, bool, p_active, rid_to_rid, bool_to_bool);

View file

@ -82,11 +82,15 @@ public:
virtual Vector2 map_get_closest_point(RID p_map, const Vector2 &p_point) const; virtual Vector2 map_get_closest_point(RID p_map, const Vector2 &p_point) const;
virtual RID map_get_closest_point_owner(RID p_map, const Vector2 &p_point) const; virtual RID map_get_closest_point_owner(RID p_map, const Vector2 &p_point) const;
virtual Array map_get_regions(RID p_map) const;
virtual Array map_get_agents(RID p_map) const;
/// Creates a new region. /// Creates a new region.
virtual RID region_create() const; virtual RID region_create() const;
/// Set the map of this region. /// Set the map of this region.
virtual void region_set_map(RID p_region, RID p_map) const; virtual void region_set_map(RID p_region, RID p_map) const;
virtual RID region_get_map(RID p_region) const;
/// Set the global transformation of this region. /// Set the global transformation of this region.
virtual void region_set_transform(RID p_region, Transform2D p_transform) const; virtual void region_set_transform(RID p_region, Transform2D p_transform) const;
@ -99,6 +103,7 @@ public:
/// Put the agent in the map. /// Put the agent in the map.
virtual void agent_set_map(RID p_agent, RID p_map) const; virtual void agent_set_map(RID p_agent, RID p_map) const;
virtual RID agent_get_map(RID p_agent) const;
/// The maximum distance (center point to /// The maximum distance (center point to
/// center point) to other agents this agent /// center point) to other agents this agent

View file

@ -54,14 +54,19 @@ void NavigationServer::_bind_methods() {
ClassDB::bind_method(D_METHOD("map_get_closest_point_normal", "map", "to_point"), &NavigationServer::map_get_closest_point_normal); ClassDB::bind_method(D_METHOD("map_get_closest_point_normal", "map", "to_point"), &NavigationServer::map_get_closest_point_normal);
ClassDB::bind_method(D_METHOD("map_get_closest_point_owner", "map", "to_point"), &NavigationServer::map_get_closest_point_owner); ClassDB::bind_method(D_METHOD("map_get_closest_point_owner", "map", "to_point"), &NavigationServer::map_get_closest_point_owner);
ClassDB::bind_method(D_METHOD("map_get_regions", "map"), &NavigationServer::map_get_regions);
ClassDB::bind_method(D_METHOD("map_get_agents", "map"), &NavigationServer::map_get_agents);
ClassDB::bind_method(D_METHOD("region_create"), &NavigationServer::region_create); ClassDB::bind_method(D_METHOD("region_create"), &NavigationServer::region_create);
ClassDB::bind_method(D_METHOD("region_set_map", "region", "map"), &NavigationServer::region_set_map); 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);
ClassDB::bind_method(D_METHOD("region_set_transform", "region", "transform"), &NavigationServer::region_set_transform); ClassDB::bind_method(D_METHOD("region_set_transform", "region", "transform"), &NavigationServer::region_set_transform);
ClassDB::bind_method(D_METHOD("region_set_navmesh", "region", "nav_mesh"), &NavigationServer::region_set_navmesh); ClassDB::bind_method(D_METHOD("region_set_navmesh", "region", "nav_mesh"), &NavigationServer::region_set_navmesh);
ClassDB::bind_method(D_METHOD("region_bake_navmesh", "mesh", "node"), &NavigationServer::region_bake_navmesh); ClassDB::bind_method(D_METHOD("region_bake_navmesh", "mesh", "node"), &NavigationServer::region_bake_navmesh);
ClassDB::bind_method(D_METHOD("agent_create"), &NavigationServer::agent_create); ClassDB::bind_method(D_METHOD("agent_create"), &NavigationServer::agent_create);
ClassDB::bind_method(D_METHOD("agent_set_map", "agent", "map"), &NavigationServer::agent_set_map); ClassDB::bind_method(D_METHOD("agent_set_map", "agent", "map"), &NavigationServer::agent_set_map);
ClassDB::bind_method(D_METHOD("agent_get_map", "agent"), &NavigationServer::agent_get_map);
ClassDB::bind_method(D_METHOD("agent_set_neighbor_dist", "agent", "dist"), &NavigationServer::agent_set_neighbor_dist); ClassDB::bind_method(D_METHOD("agent_set_neighbor_dist", "agent", "dist"), &NavigationServer::agent_set_neighbor_dist);
ClassDB::bind_method(D_METHOD("agent_set_max_neighbors", "agent", "count"), &NavigationServer::agent_set_max_neighbors); ClassDB::bind_method(D_METHOD("agent_set_max_neighbors", "agent", "count"), &NavigationServer::agent_set_max_neighbors);
ClassDB::bind_method(D_METHOD("agent_set_time_horizon", "agent", "time"), &NavigationServer::agent_set_time_horizon); ClassDB::bind_method(D_METHOD("agent_set_time_horizon", "agent", "time"), &NavigationServer::agent_set_time_horizon);

View file

@ -101,11 +101,15 @@ public:
virtual Vector3 map_get_closest_point_normal(RID p_map, const Vector3 &p_point) const = 0; virtual Vector3 map_get_closest_point_normal(RID p_map, const Vector3 &p_point) const = 0;
virtual RID map_get_closest_point_owner(RID p_map, const Vector3 &p_point) const = 0; virtual RID map_get_closest_point_owner(RID p_map, const Vector3 &p_point) const = 0;
virtual Array map_get_regions(RID p_map) const = 0;
virtual Array map_get_agents(RID p_map) const = 0;
/// Creates a new region. /// Creates a new region.
virtual RID region_create() const = 0; virtual RID region_create() const = 0;
/// Set the map of this region. /// Set the map of this region.
virtual void region_set_map(RID p_region, RID p_map) const = 0; virtual void region_set_map(RID p_region, RID p_map) const = 0;
virtual RID region_get_map(RID p_region) const = 0;
/// Set the global transformation of this region. /// Set the global transformation of this region.
virtual void region_set_transform(RID p_region, Transform p_transform) const = 0; virtual void region_set_transform(RID p_region, Transform p_transform) const = 0;
@ -121,6 +125,7 @@ public:
/// Put the agent in the map. /// Put the agent in the map.
virtual void agent_set_map(RID p_agent, RID p_map) const = 0; virtual void agent_set_map(RID p_agent, RID p_map) const = 0;
virtual RID agent_get_map(RID p_agent) const = 0;
/// The maximum distance (center point to /// The maximum distance (center point to
/// center point) to other agents this agent /// center point) to other agents this agent