Skip to content

Commit

Permalink
Allow Navigation to be more flexible
Browse files Browse the repository at this point in the history
  • Loading branch information
groud committed Mar 15, 2021
1 parent 6eef187 commit ac7073f
Show file tree
Hide file tree
Showing 18 changed files with 501 additions and 329 deletions.
40 changes: 40 additions & 0 deletions doc/classes/NavigationServer2D.xml
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,37 @@
Creates a new region.
</description>
</method>
<method name="region_get_connection_pathway_end" qualifiers="const">
<return type="Vector2">
</return>
<argument index="0" name="region" type="RID">
</argument>
<argument index="1" name="connection" type="int">
</argument>
<description>
Returns the ending point of a connection door. [code]connection[/code] is an index between 0 and the return value of [method region_get_connections_count].
</description>
</method>
<method name="region_get_connection_pathway_start" qualifiers="const">
<return type="Vector2">
</return>
<argument index="0" name="region" type="RID">
</argument>
<argument index="1" name="connection" type="int">
</argument>
<description>
Returns the starting point of a connection door. [code]connection[/code] is an index between 0 and the return value of [method region_get_connections_count].
</description>
</method>
<method name="region_get_connections_count" qualifiers="const">
<return type="int">
</return>
<argument index="0" name="region" type="RID">
</argument>
<description>
Returns how many connections this [code]region[/code] has with other regions in the map.
</description>
</method>
<method name="region_get_layers" qualifiers="const">
<return type="int">
</return>
Expand Down Expand Up @@ -321,6 +352,15 @@
</description>
</method>
</methods>
<signals>
<signal name="map_changed">
<argument index="0" name="map" type="RID">
</argument>
<description>
Emitted when a navigation map is updated, when a region moves or is modified.
</description>
</signal>
</signals>
<constants>
</constants>
</class>
40 changes: 40 additions & 0 deletions doc/classes/NavigationServer3D.xml
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,37 @@
Creates a new region.
</description>
</method>
<method name="region_get_connection_pathway_end" qualifiers="const">
<return type="Vector3">
</return>
<argument index="0" name="region" type="RID">
</argument>
<argument index="1" name="connection" type="int">
</argument>
<description>
Returns the ending point of a connection door. [code]connection[/code] is an index between 0 and the return value of [method region_get_connections_count].
</description>
</method>
<method name="region_get_connection_pathway_start" qualifiers="const">
<return type="Vector3">
</return>
<argument index="0" name="region" type="RID">
</argument>
<argument index="1" name="connection" type="int">
</argument>
<description>
Returns the starting point of a connection door. [code]connection[/code] is an index between 0 and the return value of [method region_get_connections_count].
</description>
</method>
<method name="region_get_connections_count" qualifiers="const">
<return type="int">
</return>
<argument index="0" name="region" type="RID">
</argument>
<description>
Returns how many connections this [code]region[/code] has with other regions in the map.
</description>
</method>
<method name="region_get_layers" qualifiers="const">
<return type="int">
</return>
Expand Down Expand Up @@ -398,6 +429,15 @@
</description>
</method>
</methods>
<signals>
<signal name="map_changed">
<argument index="0" name="map" type="RID">
</argument>
<description>
Emitted when a navigation map is updated, when a region moves or is modified.
</description>
</signal>
</signals>
<constants>
</constants>
</class>
40 changes: 37 additions & 3 deletions modules/gdnavigation/gd_navigation_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -145,9 +145,13 @@ COMMAND_2(map_set_active, RID, p_map, bool, p_active) {
if (p_active) {
if (!map_is_active(p_map)) {
active_maps.push_back(map);
active_maps_update_id.push_back(map->get_map_update_id());
}
} else {
active_maps.erase(map);
int map_index = active_maps.find(map);
ERR_FAIL_COND(map_index < 0);
active_maps.remove(map_index);
active_maps_update_id.remove(map_index);
}
}

Expand Down Expand Up @@ -304,6 +308,27 @@ void GdNavigationServer::region_bake_navmesh(Ref<NavigationMesh> r_mesh, Node *p
#endif
}

int GdNavigationServer::region_get_connections_count(RID p_region) const {
NavRegion *region = region_owner.getornull(p_region);
ERR_FAIL_COND_V(!region, 0);

return region->get_connections_count();
}

Vector3 GdNavigationServer::region_get_connection_pathway_start(RID p_region, int p_connection_id) const {
NavRegion *region = region_owner.getornull(p_region);
ERR_FAIL_COND_V(!region, Vector3());

return region->get_connection_pathway_start(p_connection_id);
}

Vector3 GdNavigationServer::region_get_connection_pathway_end(RID p_region, int p_connection_id) const {
NavRegion *region = region_owner.getornull(p_region);
ERR_FAIL_COND_V(!region, Vector3());

return region->get_connection_pathway_end(p_connection_id);
}

RID GdNavigationServer::agent_create() const {
auto mut_this = const_cast<GdNavigationServer *>(this);
MutexLock lock(mut_this->operations_mutex);
Expand Down Expand Up @@ -443,7 +468,9 @@ COMMAND_1(free, RID, p_object) {
agents[i]->set_map(nullptr);
}

active_maps.erase(map);
int map_index = active_maps.find(map);
active_maps.remove(map_index);
active_maps_update_id.remove(map_index);
map_owner.free(p_object);
memdelete(map);

Expand Down Expand Up @@ -504,10 +531,17 @@ void GdNavigationServer::process(real_t p_delta_time) {
// In c++ we can't be sure that this is performed in the main thread
// even with mutable functions.
MutexLock lock(operations_mutex);
for (int i(0); i < active_maps.size(); i++) {
for (uint32_t i(0); i < active_maps.size(); i++) {
active_maps[i]->sync();
active_maps[i]->step(p_delta_time);
active_maps[i]->dispatch_callbacks();

// Emit a signal if a map changed.
const uint32_t new_map_update_id = active_maps[i]->get_map_update_id();
if (new_map_update_id != active_maps_update_id[i]) {
emit_signal("map_changed", active_maps[i]->get_self());
active_maps_update_id[i] = new_map_update_id;
}
}
}

Expand Down
7 changes: 6 additions & 1 deletion modules/gdnavigation/gd_navigation_server.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#ifndef GD_NAVIGATION_SERVER_H
#define GD_NAVIGATION_SERVER_H

#include "core/templates/local_vector.h"
#include "core/templates/rid.h"
#include "core/templates/rid_owner.h"
#include "servers/navigation_server_3d.h"
Expand Down Expand Up @@ -79,7 +80,8 @@ class GdNavigationServer : public NavigationServer3D {
mutable RID_PtrOwner<RvoAgent> agent_owner;

bool active = true;
Vector<NavMap *> active_maps;
LocalVector<NavMap *> active_maps;
LocalVector<uint32_t> active_maps_update_id;

public:
GdNavigationServer();
Expand Down Expand Up @@ -114,6 +116,9 @@ class GdNavigationServer : public NavigationServer3D {
COMMAND_2(region_set_transform, RID, p_region, Transform, p_transform);
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 int region_get_connections_count(RID p_region) const;
virtual Vector3 region_get_connection_pathway_start(RID p_region, int p_connection_id) const;
virtual Vector3 region_get_connection_pathway_end(RID p_region, int p_connection_id) const;

virtual RID agent_create() const;
COMMAND_2(agent_set_map, RID, p_agent, RID, p_map);
Expand Down
Loading

0 comments on commit ac7073f

Please sign in to comment.