diff options
author | smix8 <52464204+smix8@users.noreply.github.com> | 2022-05-12 02:52:48 +0200 |
---|---|---|
committer | smix8 <52464204+smix8@users.noreply.github.com> | 2022-05-12 20:23:19 +0200 |
commit | 371054e3e5a20325200cc7ba7bbbce6b6a4588c3 (patch) | |
tree | 05a861441d2b7b0bdc38a5d2b6d25bceb5eae120 | |
parent | 0841f72c380a285802f52f6f28240345c97a80d7 (diff) |
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.
-rw-r--r-- | doc/classes/NavigationServer2D.xml | 28 | ||||
-rw-r--r-- | doc/classes/NavigationServer3D.xml | 28 | ||||
-rw-r--r-- | modules/navigation/godot_navigation_server.cpp | 32 | ||||
-rw-r--r-- | modules/navigation/godot_navigation_server.h | 5 | ||||
-rw-r--r-- | servers/navigation_server_2d.cpp | 13 | ||||
-rw-r--r-- | servers/navigation_server_2d.h | 5 | ||||
-rw-r--r-- | servers/navigation_server_3d.cpp | 5 | ||||
-rw-r--r-- | servers/navigation_server_3d.h | 5 |
8 files changed, 121 insertions, 0 deletions
diff --git a/doc/classes/NavigationServer2D.xml b/doc/classes/NavigationServer2D.xml index e007dfd9b5..928834101f 100644 --- a/doc/classes/NavigationServer2D.xml +++ b/doc/classes/NavigationServer2D.xml @@ -21,6 +21,13 @@ Creates the agent. </description> </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"> <return type="bool" /> <argument index="0" name="agent" type="RID" /> @@ -123,6 +130,13 @@ Create a new map. </description> </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"> <return type="float" /> <argument index="0" name="map" type="RID" /> @@ -164,6 +178,13 @@ Returns the navigation path to reach the destination from the origin. [code]layers[/code] is a bitmask of all region layers that are allowed to be in the path. </description> </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"> <return type="bool" /> <argument index="0" name="nap" type="RID" /> @@ -231,6 +252,13 @@ Returns the region's layers. </description> </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_layers" qualifiers="const"> <return type="void" /> <argument index="0" name="region" type="RID" /> diff --git a/doc/classes/NavigationServer3D.xml b/doc/classes/NavigationServer3D.xml index c987bc9042..8c83fe5485 100644 --- a/doc/classes/NavigationServer3D.xml +++ b/doc/classes/NavigationServer3D.xml @@ -21,6 +21,13 @@ Creates the agent. </description> </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"> <return type="bool" /> <argument index="0" name="agent" type="RID" /> @@ -123,6 +130,13 @@ Create a new map. </description> </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"> <return type="float" /> <argument index="0" name="map" type="RID" /> @@ -182,6 +196,13 @@ Returns the navigation path to reach the destination from the origin. [code]layers[/code] is a bitmask of all region layers that are allowed to be in the path. </description> </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"> <return type="Vector3" /> <argument index="0" name="map" type="RID" /> @@ -281,6 +302,13 @@ Returns the region's layers. </description> </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_layers" qualifiers="const"> <return type="void" /> <argument index="0" name="region" type="RID" /> diff --git a/modules/navigation/godot_navigation_server.cpp b/modules/navigation/godot_navigation_server.cpp index d16d41b438..42aad77979 100644 --- a/modules/navigation/godot_navigation_server.cpp +++ b/modules/navigation/godot_navigation_server.cpp @@ -233,6 +233,38 @@ RID GodotNavigationServer::map_get_closest_point_owner(RID p_map, const Vector3 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.get_or_null(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.get_or_null(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.get_or_null(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.get_or_null(p_agent); + ERR_FAIL_COND_V(agent == nullptr, RID()); + return agent->get_map()->get_self(); +} + RID GodotNavigationServer::region_create() const { GodotNavigationServer *mut_this = const_cast<GodotNavigationServer *>(this); MutexLock lock(mut_this->operations_mutex); diff --git a/modules/navigation/godot_navigation_server.h b/modules/navigation/godot_navigation_server.h index 7ad5e2d501..89e7311e51 100644 --- a/modules/navigation/godot_navigation_server.h +++ b/modules/navigation/godot_navigation_server.h @@ -105,8 +105,12 @@ public: virtual Vector3 map_get_closest_point_normal(RID p_map, const Vector3 &p_point) const override; virtual RID map_get_closest_point_owner(RID p_map, const Vector3 &p_point) const override; + virtual Array map_get_regions(RID p_map) const override; + virtual Array map_get_agents(RID p_map) const override; + virtual RID region_create() const override; COMMAND_2(region_set_map, RID, p_region, RID, p_map); + virtual RID region_get_map(RID p_region) const override; COMMAND_2(region_set_layers, RID, p_region, uint32_t, p_layers); virtual uint32_t region_get_layers(RID p_region) const override; COMMAND_2(region_set_transform, RID, p_region, Transform3D, p_transform); @@ -118,6 +122,7 @@ public: virtual RID agent_create() const override; COMMAND_2(agent_set_map, RID, p_agent, RID, p_map); + virtual RID agent_get_map(RID p_agent) const override; 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_time_horizon, RID, p_agent, real_t, p_time); diff --git a/servers/navigation_server_2d.cpp b/servers/navigation_server_2d.cpp index a92fe8c79a..901d335017 100644 --- a/servers/navigation_server_2d.cpp +++ b/servers/navigation_server_2d.cpp @@ -170,8 +170,12 @@ void NavigationServer2D::_bind_methods() { ClassDB::bind_method(D_METHOD("map_get_closest_point", "map", "to_point"), &NavigationServer2D::map_get_closest_point); ClassDB::bind_method(D_METHOD("map_get_closest_point_owner", "map", "to_point"), &NavigationServer2D::map_get_closest_point_owner); + ClassDB::bind_method(D_METHOD("map_get_regions", "map"), &NavigationServer2D::map_get_regions); + ClassDB::bind_method(D_METHOD("map_get_agents", "map"), &NavigationServer2D::map_get_agents); + ClassDB::bind_method(D_METHOD("region_create"), &NavigationServer2D::region_create); ClassDB::bind_method(D_METHOD("region_set_map", "region", "map"), &NavigationServer2D::region_set_map); + ClassDB::bind_method(D_METHOD("region_get_map", "region"), &NavigationServer2D::region_get_map); ClassDB::bind_method(D_METHOD("region_set_layers", "region", "layers"), &NavigationServer2D::region_set_layers); ClassDB::bind_method(D_METHOD("region_get_layers", "region"), &NavigationServer2D::region_get_layers); ClassDB::bind_method(D_METHOD("region_set_transform", "region", "transform"), &NavigationServer2D::region_set_transform); @@ -182,6 +186,7 @@ void NavigationServer2D::_bind_methods() { ClassDB::bind_method(D_METHOD("agent_create"), &NavigationServer2D::agent_create); ClassDB::bind_method(D_METHOD("agent_set_map", "agent", "map"), &NavigationServer2D::agent_set_map); + ClassDB::bind_method(D_METHOD("agent_get_map", "agent"), &NavigationServer2D::agent_get_map); ClassDB::bind_method(D_METHOD("agent_set_neighbor_dist", "agent", "dist"), &NavigationServer2D::agent_set_neighbor_dist); ClassDB::bind_method(D_METHOD("agent_set_max_neighbors", "agent", "count"), &NavigationServer2D::agent_set_max_neighbors); ClassDB::bind_method(D_METHOD("agent_set_time_horizon", "agent", "time"), &NavigationServer2D::agent_set_time_horizon); @@ -208,6 +213,14 @@ NavigationServer2D::~NavigationServer2D() { 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); void FORWARD_2_C(map_set_active, RID, p_map, bool, p_active, rid_to_rid, bool_to_bool); diff --git a/servers/navigation_server_2d.h b/servers/navigation_server_2d.h index 7350eeb5b1..dfdcf5fca8 100644 --- a/servers/navigation_server_2d.h +++ b/servers/navigation_server_2d.h @@ -80,11 +80,15 @@ public: 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 Array map_get_regions(RID p_map) const; + virtual Array map_get_agents(RID p_map) const; + /// Creates a new region. virtual RID region_create() const; /// Set the map of this region. virtual void region_set_map(RID p_region, RID p_map) const; + virtual RID region_get_map(RID p_region) const; /// Set the region's layers virtual void region_set_layers(RID p_region, uint32_t p_layers) const; @@ -106,6 +110,7 @@ public: /// Put the agent in the map. 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 /// center point) to other agents this agent diff --git a/servers/navigation_server_3d.cpp b/servers/navigation_server_3d.cpp index 46192772f6..0ce869ad1a 100644 --- a/servers/navigation_server_3d.cpp +++ b/servers/navigation_server_3d.cpp @@ -48,8 +48,12 @@ void NavigationServer3D::_bind_methods() { ClassDB::bind_method(D_METHOD("map_get_closest_point_normal", "map", "to_point"), &NavigationServer3D::map_get_closest_point_normal); ClassDB::bind_method(D_METHOD("map_get_closest_point_owner", "map", "to_point"), &NavigationServer3D::map_get_closest_point_owner); + ClassDB::bind_method(D_METHOD("map_get_regions", "map"), &NavigationServer3D::map_get_regions); + ClassDB::bind_method(D_METHOD("map_get_agents", "map"), &NavigationServer3D::map_get_agents); + ClassDB::bind_method(D_METHOD("region_create"), &NavigationServer3D::region_create); ClassDB::bind_method(D_METHOD("region_set_map", "region", "map"), &NavigationServer3D::region_set_map); + ClassDB::bind_method(D_METHOD("region_get_map", "region"), &NavigationServer3D::region_get_map); ClassDB::bind_method(D_METHOD("region_set_layers", "region", "layers"), &NavigationServer3D::region_set_layers); ClassDB::bind_method(D_METHOD("region_get_layers", "region"), &NavigationServer3D::region_get_layers); ClassDB::bind_method(D_METHOD("region_set_transform", "region", "transform"), &NavigationServer3D::region_set_transform); @@ -61,6 +65,7 @@ void NavigationServer3D::_bind_methods() { ClassDB::bind_method(D_METHOD("agent_create"), &NavigationServer3D::agent_create); ClassDB::bind_method(D_METHOD("agent_set_map", "agent", "map"), &NavigationServer3D::agent_set_map); + ClassDB::bind_method(D_METHOD("agent_get_map", "agent"), &NavigationServer3D::agent_get_map); ClassDB::bind_method(D_METHOD("agent_set_neighbor_dist", "agent", "dist"), &NavigationServer3D::agent_set_neighbor_dist); ClassDB::bind_method(D_METHOD("agent_set_max_neighbors", "agent", "count"), &NavigationServer3D::agent_set_max_neighbors); ClassDB::bind_method(D_METHOD("agent_set_time_horizon", "agent", "time"), &NavigationServer3D::agent_set_time_horizon); diff --git a/servers/navigation_server_3d.h b/servers/navigation_server_3d.h index 0a75b07931..c3d3a589a9 100644 --- a/servers/navigation_server_3d.h +++ b/servers/navigation_server_3d.h @@ -91,11 +91,15 @@ public: 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 Array map_get_regions(RID p_map) const = 0; + virtual Array map_get_agents(RID p_map) const = 0; + /// Creates a new region. virtual RID region_create() const = 0; /// Set the map of this region. 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 region's layers virtual void region_set_layers(RID p_region, uint32_t p_layers) const = 0; @@ -120,6 +124,7 @@ public: /// Put the agent in the map. 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 /// center point) to other agents this agent |