diff options
Diffstat (limited to 'modules/navigation')
-rw-r--r-- | modules/navigation/editor/navigation_mesh_editor_plugin.cpp | 30 | ||||
-rw-r--r-- | modules/navigation/godot_navigation_server.cpp | 116 | ||||
-rw-r--r-- | modules/navigation/godot_navigation_server.h | 19 | ||||
-rw-r--r-- | modules/navigation/nav_agent.cpp (renamed from modules/navigation/rvo_agent.cpp) | 44 | ||||
-rw-r--r-- | modules/navigation/nav_agent.h (renamed from modules/navigation/rvo_agent.h) | 23 | ||||
-rw-r--r-- | modules/navigation/nav_link.cpp | 8 | ||||
-rw-r--r-- | modules/navigation/nav_link.h | 16 | ||||
-rw-r--r-- | modules/navigation/nav_map.cpp | 79 | ||||
-rw-r--r-- | modules/navigation/nav_map.h | 20 | ||||
-rw-r--r-- | modules/navigation/nav_utils.h | 2 | ||||
-rw-r--r-- | modules/navigation/navigation_mesh_generator.cpp | 8 |
11 files changed, 160 insertions, 205 deletions
diff --git a/modules/navigation/editor/navigation_mesh_editor_plugin.cpp b/modules/navigation/editor/navigation_mesh_editor_plugin.cpp index 54f7abda8d..557d45b386 100644 --- a/modules/navigation/editor/navigation_mesh_editor_plugin.cpp +++ b/modules/navigation/editor/navigation_mesh_editor_plugin.cpp @@ -60,12 +60,40 @@ void NavigationMeshEditor::_bake_pressed() { button_bake->set_pressed(false); ERR_FAIL_COND(!node); - if (!node->get_navigation_mesh().is_valid()) { + Ref<NavigationMesh> navmesh = node->get_navigation_mesh(); + if (!navmesh.is_valid()) { err_dialog->set_text(TTR("A NavigationMesh resource must be set or created for this node to work.")); err_dialog->popup_centered(); return; } + String path = navmesh->get_path(); + if (!path.is_resource_file()) { + int srpos = path.find("::"); + if (srpos != -1) { + String base = path.substr(0, srpos); + if (ResourceLoader::get_resource_type(base) == "PackedScene") { + if (!get_tree()->get_edited_scene_root() || get_tree()->get_edited_scene_root()->get_scene_file_path() != base) { + err_dialog->set_text(TTR("Cannot generate navigation mesh because it does not belong to the edited scene. Make it unique first.")); + err_dialog->popup_centered(); + return; + } + } else { + if (FileAccess::exists(base + ".import")) { + err_dialog->set_text(TTR("Cannot generate navigation mesh because it belongs to a resource which was imported.")); + err_dialog->popup_centered(); + return; + } + } + } + } else { + if (FileAccess::exists(path + ".import")) { + err_dialog->set_text(TTR("Cannot generate navigation mesh because the resource was imported from another type.")); + err_dialog->popup_centered(); + return; + } + } + NavigationMeshGenerator::get_singleton()->clear(node->get_navigation_mesh()); NavigationMeshGenerator::get_singleton()->bake(node->get_navigation_mesh(), node); diff --git a/modules/navigation/godot_navigation_server.cpp b/modules/navigation/godot_navigation_server.cpp index 9b5d78d465..c3cb1c5f13 100644 --- a/modules/navigation/godot_navigation_server.cpp +++ b/modules/navigation/godot_navigation_server.cpp @@ -81,36 +81,6 @@ using namespace NavigationUtilities; } \ void GodotNavigationServer::MERGE(_cmd_, F_NAME)(T_0 D_0, T_1 D_1) -#define COMMAND_4(F_NAME, T_0, D_0, T_1, D_1, T_2, D_2, T_3, D_3) \ - struct MERGE(F_NAME, _command) : public SetCommand { \ - T_0 d_0; \ - T_1 d_1; \ - T_2 d_2; \ - T_3 d_3; \ - MERGE(F_NAME, _command) \ - ( \ - T_0 p_d_0, \ - T_1 p_d_1, \ - T_2 p_d_2, \ - T_3 p_d_3) : \ - d_0(p_d_0), \ - d_1(p_d_1), \ - d_2(p_d_2), \ - d_3(p_d_3) {} \ - virtual void exec(GodotNavigationServer *server) override { \ - server->MERGE(_cmd_, F_NAME)(d_0, d_1, d_2, d_3); \ - } \ - }; \ - void GodotNavigationServer::F_NAME(T_0 D_0, T_1 D_1, T_2 D_2, T_3 D_3) { \ - auto cmd = memnew(MERGE(F_NAME, _command)( \ - D_0, \ - D_1, \ - D_2, \ - D_3)); \ - add_command(cmd); \ - } \ - void GodotNavigationServer::MERGE(_cmd_, F_NAME)(T_0 D_0, T_1 D_1, T_2 D_2, T_3 D_3) - GodotNavigationServer::GodotNavigationServer() {} GodotNavigationServer::~GodotNavigationServer() { @@ -292,7 +262,7 @@ TypedArray<RID> GodotNavigationServer::map_get_agents(RID p_map) const { const NavMap *map = map_owner.get_or_null(p_map); ERR_FAIL_COND_V(map == nullptr, agents_rids); - const LocalVector<RvoAgent *> agents = map->get_agents(); + const LocalVector<NavAgent *> agents = map->get_agents(); agents_rids.resize(agents.size()); for (uint32_t i = 0; i < agents.size(); i++) { @@ -312,7 +282,7 @@ RID GodotNavigationServer::region_get_map(RID p_region) const { } RID GodotNavigationServer::agent_get_map(RID p_agent) const { - RvoAgent *agent = agent_owner.get_or_null(p_agent); + NavAgent *agent = agent_owner.get_or_null(p_agent); ERR_FAIL_COND_V(agent == nullptr, RID()); if (agent->get_map()) { @@ -535,32 +505,32 @@ uint32_t GodotNavigationServer::link_get_navigation_layers(const RID p_link) con return link->get_navigation_layers(); } -COMMAND_2(link_set_start_location, RID, p_link, Vector3, p_location) { +COMMAND_2(link_set_start_position, RID, p_link, Vector3, p_position) { NavLink *link = link_owner.get_or_null(p_link); ERR_FAIL_COND(link == nullptr); - link->set_start_location(p_location); + link->set_start_position(p_position); } -Vector3 GodotNavigationServer::link_get_start_location(RID p_link) const { +Vector3 GodotNavigationServer::link_get_start_position(RID p_link) const { const NavLink *link = link_owner.get_or_null(p_link); ERR_FAIL_COND_V(link == nullptr, Vector3()); - return link->get_start_location(); + return link->get_start_position(); } -COMMAND_2(link_set_end_location, RID, p_link, Vector3, p_location) { +COMMAND_2(link_set_end_position, RID, p_link, Vector3, p_position) { NavLink *link = link_owner.get_or_null(p_link); ERR_FAIL_COND(link == nullptr); - link->set_end_location(p_location); + link->set_end_position(p_position); } -Vector3 GodotNavigationServer::link_get_end_location(RID p_link) const { +Vector3 GodotNavigationServer::link_get_end_position(RID p_link) const { const NavLink *link = link_owner.get_or_null(p_link); ERR_FAIL_COND_V(link == nullptr, Vector3()); - return link->get_end_location(); + return link->get_end_position(); } COMMAND_2(link_set_enter_cost, RID, p_link, real_t, p_enter_cost) { @@ -609,13 +579,13 @@ RID GodotNavigationServer::agent_create() { MutexLock lock(operations_mutex); RID rid = agent_owner.make_rid(); - RvoAgent *agent = agent_owner.get_or_null(rid); + NavAgent *agent = agent_owner.get_or_null(rid); agent->set_self(rid); return rid; } COMMAND_2(agent_set_map, RID, p_agent, RID, p_map) { - RvoAgent *agent = agent_owner.get_or_null(p_agent); + NavAgent *agent = agent_owner.get_or_null(p_agent); ERR_FAIL_COND(agent == nullptr); if (agent->get_map()) { @@ -642,86 +612,86 @@ COMMAND_2(agent_set_map, RID, p_agent, RID, p_map) { } COMMAND_2(agent_set_neighbor_distance, RID, p_agent, real_t, p_distance) { - RvoAgent *agent = agent_owner.get_or_null(p_agent); + NavAgent *agent = agent_owner.get_or_null(p_agent); ERR_FAIL_COND(agent == nullptr); agent->get_agent()->neighborDist_ = p_distance; } COMMAND_2(agent_set_max_neighbors, RID, p_agent, int, p_count) { - RvoAgent *agent = agent_owner.get_or_null(p_agent); + NavAgent *agent = agent_owner.get_or_null(p_agent); ERR_FAIL_COND(agent == nullptr); agent->get_agent()->maxNeighbors_ = p_count; } COMMAND_2(agent_set_time_horizon, RID, p_agent, real_t, p_time) { - RvoAgent *agent = agent_owner.get_or_null(p_agent); + NavAgent *agent = agent_owner.get_or_null(p_agent); ERR_FAIL_COND(agent == nullptr); agent->get_agent()->timeHorizon_ = p_time; } COMMAND_2(agent_set_radius, RID, p_agent, real_t, p_radius) { - RvoAgent *agent = agent_owner.get_or_null(p_agent); + NavAgent *agent = agent_owner.get_or_null(p_agent); ERR_FAIL_COND(agent == nullptr); agent->get_agent()->radius_ = p_radius; } COMMAND_2(agent_set_max_speed, RID, p_agent, real_t, p_max_speed) { - RvoAgent *agent = agent_owner.get_or_null(p_agent); + NavAgent *agent = agent_owner.get_or_null(p_agent); ERR_FAIL_COND(agent == nullptr); agent->get_agent()->maxSpeed_ = p_max_speed; } COMMAND_2(agent_set_velocity, RID, p_agent, Vector3, p_velocity) { - RvoAgent *agent = agent_owner.get_or_null(p_agent); + NavAgent *agent = agent_owner.get_or_null(p_agent); ERR_FAIL_COND(agent == nullptr); agent->get_agent()->velocity_ = RVO::Vector3(p_velocity.x, p_velocity.y, p_velocity.z); } COMMAND_2(agent_set_target_velocity, RID, p_agent, Vector3, p_velocity) { - RvoAgent *agent = agent_owner.get_or_null(p_agent); + NavAgent *agent = agent_owner.get_or_null(p_agent); ERR_FAIL_COND(agent == nullptr); agent->get_agent()->prefVelocity_ = RVO::Vector3(p_velocity.x, p_velocity.y, p_velocity.z); } COMMAND_2(agent_set_position, RID, p_agent, Vector3, p_position) { - RvoAgent *agent = agent_owner.get_or_null(p_agent); + NavAgent *agent = agent_owner.get_or_null(p_agent); ERR_FAIL_COND(agent == nullptr); agent->get_agent()->position_ = RVO::Vector3(p_position.x, p_position.y, p_position.z); } COMMAND_2(agent_set_ignore_y, RID, p_agent, bool, p_ignore) { - RvoAgent *agent = agent_owner.get_or_null(p_agent); + NavAgent *agent = agent_owner.get_or_null(p_agent); ERR_FAIL_COND(agent == nullptr); agent->get_agent()->ignore_y_ = p_ignore; } bool GodotNavigationServer::agent_is_map_changed(RID p_agent) const { - RvoAgent *agent = agent_owner.get_or_null(p_agent); + NavAgent *agent = agent_owner.get_or_null(p_agent); ERR_FAIL_COND_V(agent == nullptr, false); return agent->is_map_changed(); } -COMMAND_4(agent_set_callback, RID, p_agent, ObjectID, p_object_id, StringName, p_method, Variant, p_udata) { - RvoAgent *agent = agent_owner.get_or_null(p_agent); +COMMAND_2(agent_set_callback, RID, p_agent, Callable, p_callback) { + NavAgent *agent = agent_owner.get_or_null(p_agent); ERR_FAIL_COND(agent == nullptr); - agent->set_callback(p_object_id, p_method, p_udata); + agent->set_callback(p_callback); if (agent->get_map()) { - if (p_object_id == ObjectID()) { - agent->get_map()->remove_agent_as_controlled(agent); - } else { + if (p_callback.is_valid()) { agent->get_map()->set_agent_as_controlled(agent); + } else { + agent->get_map()->remove_agent_as_controlled(agent); } } } @@ -731,24 +701,21 @@ COMMAND_1(free, RID, p_object) { NavMap *map = map_owner.get_or_null(p_object); // Removes any assigned region - LocalVector<NavRegion *> regions = map->get_regions(); - for (uint32_t i = 0; i < regions.size(); i++) { - map->remove_region(regions[i]); - regions[i]->set_map(nullptr); + for (NavRegion *region : map->get_regions()) { + map->remove_region(region); + region->set_map(nullptr); } // Removes any assigned links - LocalVector<NavLink *> links = map->get_links(); - for (uint32_t i = 0; i < links.size(); i++) { - map->remove_link(links[i]); - links[i]->set_map(nullptr); + for (NavLink *link : map->get_links()) { + map->remove_link(link); + link->set_map(nullptr); } // Remove any assigned agent - LocalVector<RvoAgent *> agents = map->get_agents(); - for (uint32_t i = 0; i < agents.size(); i++) { - map->remove_agent(agents[i]); - agents[i]->set_map(nullptr); + for (NavAgent *agent : map->get_agents()) { + map->remove_agent(agent); + agent->set_map(nullptr); } int map_index = active_maps.find(map); @@ -779,7 +746,7 @@ COMMAND_1(free, RID, p_object) { link_owner.free(p_object); } else if (agent_owner.owns(p_object)) { - RvoAgent *agent = agent_owner.get_or_null(p_object); + NavAgent *agent = agent_owner.get_or_null(p_object); // Removes this agent from the map if assigned if (agent->get_map() != nullptr) { @@ -806,9 +773,9 @@ void GodotNavigationServer::flush_queries() { MutexLock lock(commands_mutex); MutexLock lock2(operations_mutex); - for (size_t i(0); i < commands.size(); i++) { - commands[i]->exec(this); - memdelete(commands[i]); + for (SetCommand *command : commands) { + command->exec(this); + memdelete(command); } commands.clear(); } @@ -949,4 +916,3 @@ int GodotNavigationServer::get_process_info(ProcessInfo p_info) const { #undef COMMAND_1 #undef COMMAND_2 -#undef COMMAND_4 diff --git a/modules/navigation/godot_navigation_server.h b/modules/navigation/godot_navigation_server.h index a87a88d3bc..0b113b77d4 100644 --- a/modules/navigation/godot_navigation_server.h +++ b/modules/navigation/godot_navigation_server.h @@ -36,10 +36,10 @@ #include "core/templates/rid_owner.h" #include "servers/navigation_server_3d.h" +#include "nav_agent.h" #include "nav_link.h" #include "nav_map.h" #include "nav_region.h" -#include "rvo_agent.h" /// The commands are functions executed during the `sync` phase. @@ -54,10 +54,6 @@ virtual void F_NAME(T_0 D_0, T_1 D_1) override; \ void MERGE(_cmd_, F_NAME)(T_0 D_0, T_1 D_1) -#define COMMAND_4_DEF(F_NAME, T_0, D_0, T_1, D_1, T_2, D_2, T_3, D_3, D_3_DEF) \ - virtual void F_NAME(T_0 D_0, T_1 D_1, T_2 D_2, T_3 D_3 = D_3_DEF) override; \ - void MERGE(_cmd_, F_NAME)(T_0 D_0, T_1 D_1, T_2 D_2, T_3 D_3) - class GodotNavigationServer; struct SetCommand { @@ -75,7 +71,7 @@ class GodotNavigationServer : public NavigationServer3D { mutable RID_Owner<NavLink> link_owner; mutable RID_Owner<NavMap> map_owner; mutable RID_Owner<NavRegion> region_owner; - mutable RID_Owner<RvoAgent> agent_owner; + mutable RID_Owner<NavAgent> agent_owner; bool active = true; LocalVector<NavMap *> active_maps; @@ -158,10 +154,10 @@ public: virtual bool link_is_bidirectional(RID p_link) const override; COMMAND_2(link_set_navigation_layers, RID, p_link, uint32_t, p_navigation_layers); virtual uint32_t link_get_navigation_layers(RID p_link) const override; - COMMAND_2(link_set_start_location, RID, p_link, Vector3, p_location); - virtual Vector3 link_get_start_location(RID p_link) const override; - COMMAND_2(link_set_end_location, RID, p_link, Vector3, p_location); - virtual Vector3 link_get_end_location(RID p_link) const override; + COMMAND_2(link_set_start_position, RID, p_link, Vector3, p_position); + virtual Vector3 link_get_start_position(RID p_link) const override; + COMMAND_2(link_set_end_position, RID, p_link, Vector3, p_position); + virtual Vector3 link_get_end_position(RID p_link) const override; COMMAND_2(link_set_enter_cost, RID, p_link, real_t, p_enter_cost); virtual real_t link_get_enter_cost(RID p_link) const override; COMMAND_2(link_set_travel_cost, RID, p_link, real_t, p_travel_cost); @@ -182,7 +178,7 @@ public: COMMAND_2(agent_set_position, RID, p_agent, Vector3, p_position); COMMAND_2(agent_set_ignore_y, RID, p_agent, bool, p_ignore); virtual bool agent_is_map_changed(RID p_agent) const override; - COMMAND_4_DEF(agent_set_callback, RID, p_agent, ObjectID, p_object_id, StringName, p_method, Variant, p_udata, Variant()); + COMMAND_2(agent_set_callback, RID, p_agent, Callable, p_callback); COMMAND_1(free, RID, p_object); @@ -198,6 +194,5 @@ public: #undef COMMAND_1 #undef COMMAND_2 -#undef COMMAND_4_DEF #endif // GODOT_NAVIGATION_SERVER_H diff --git a/modules/navigation/rvo_agent.cpp b/modules/navigation/nav_agent.cpp index 979ef0d917..293544c0a5 100644 --- a/modules/navigation/rvo_agent.cpp +++ b/modules/navigation/nav_agent.cpp @@ -1,5 +1,5 @@ /**************************************************************************/ -/* rvo_agent.cpp */ +/* nav_agent.cpp */ /**************************************************************************/ /* This file is part of: */ /* GODOT ENGINE */ @@ -28,19 +28,15 @@ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /**************************************************************************/ -#include "rvo_agent.h" +#include "nav_agent.h" #include "nav_map.h" -RvoAgent::RvoAgent() { - callback.id = ObjectID(); -} - -void RvoAgent::set_map(NavMap *p_map) { +void NavAgent::set_map(NavMap *p_map) { map = p_map; } -bool RvoAgent::is_map_changed() { +bool NavAgent::is_map_changed() { if (map) { bool is_changed = map->get_map_update_id() != map_update_id; map_update_id = map->get_map_update_id(); @@ -50,31 +46,25 @@ bool RvoAgent::is_map_changed() { } } -void RvoAgent::set_callback(ObjectID p_id, const StringName p_method, const Variant p_udata) { - callback.id = p_id; - callback.method = p_method; - callback.udata = p_udata; +void NavAgent::set_callback(Callable p_callback) { + callback = p_callback; } -bool RvoAgent::has_callback() const { - return callback.id.is_valid(); +bool NavAgent::has_callback() const { + return callback.is_valid(); } -void RvoAgent::dispatch_callback() { - if (callback.id.is_null()) { +void NavAgent::dispatch_callback() { + if (!callback.is_valid()) { return; } - Object *obj = ObjectDB::get_instance(callback.id); - if (!obj) { - callback.id = ObjectID(); - return; - } - - Callable::CallError responseCallError; - callback.new_velocity = Vector3(agent.newVelocity_.x(), agent.newVelocity_.y(), agent.newVelocity_.z()); + Vector3 new_velocity = Vector3(agent.newVelocity_.x(), agent.newVelocity_.y(), agent.newVelocity_.z()); - const Variant *vp[2] = { &callback.new_velocity, &callback.udata }; - int argc = (callback.udata.get_type() == Variant::NIL) ? 1 : 2; - obj->callp(callback.method, vp, argc, responseCallError); + // Invoke the callback with the new velocity. + Variant args[] = { new_velocity }; + const Variant *args_p[] = { &args[0] }; + Variant return_value; + Callable::CallError call_error; + callback.callp(args_p, 1, return_value, call_error); } diff --git a/modules/navigation/rvo_agent.h b/modules/navigation/nav_agent.h index 7b19907b2b..f154ce14d9 100644 --- a/modules/navigation/rvo_agent.h +++ b/modules/navigation/nav_agent.h @@ -1,5 +1,5 @@ /**************************************************************************/ -/* rvo_agent.h */ +/* nav_agent.h */ /**************************************************************************/ /* This file is part of: */ /* GODOT ENGINE */ @@ -28,8 +28,8 @@ /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ /**************************************************************************/ -#ifndef RVO_AGENT_H -#define RVO_AGENT_H +#ifndef NAV_AGENT_H +#define NAV_AGENT_H #include "core/object/class_db.h" #include "nav_rid.h" @@ -38,22 +38,13 @@ class NavMap; -class RvoAgent : public NavRid { - struct AvoidanceComputedCallback { - ObjectID id; - StringName method; - Variant udata; - Variant new_velocity; - }; - +class NavAgent : public NavRid { NavMap *map = nullptr; RVO::Agent agent; - AvoidanceComputedCallback callback; + Callable callback = Callable(); uint32_t map_update_id = 0; public: - RvoAgent(); - void set_map(NavMap *p_map); NavMap *get_map() { return map; @@ -65,10 +56,10 @@ public: bool is_map_changed(); - void set_callback(ObjectID p_id, const StringName p_method, const Variant p_udata = Variant()); + void set_callback(Callable p_callback); bool has_callback() const; void dispatch_callback(); }; -#endif // RVO_AGENT_H +#endif // NAV_AGENT_H diff --git a/modules/navigation/nav_link.cpp b/modules/navigation/nav_link.cpp index 05d2b21487..ad87cc0b05 100644 --- a/modules/navigation/nav_link.cpp +++ b/modules/navigation/nav_link.cpp @@ -42,13 +42,13 @@ void NavLink::set_bidirectional(bool p_bidirectional) { link_dirty = true; } -void NavLink::set_start_location(const Vector3 p_location) { - start_location = p_location; +void NavLink::set_start_position(const Vector3 p_position) { + start_position = p_position; link_dirty = true; } -void NavLink::set_end_location(const Vector3 p_location) { - end_location = p_location; +void NavLink::set_end_position(const Vector3 p_position) { + end_position = p_position; link_dirty = true; } diff --git a/modules/navigation/nav_link.h b/modules/navigation/nav_link.h index 47c1211db8..0b8ad4db69 100644 --- a/modules/navigation/nav_link.h +++ b/modules/navigation/nav_link.h @@ -37,8 +37,8 @@ class NavLink : public NavBase { NavMap *map = nullptr; bool bidirectional = true; - Vector3 start_location; - Vector3 end_location; + Vector3 start_position; + Vector3 end_position; bool link_dirty = true; @@ -57,14 +57,14 @@ public: return bidirectional; } - void set_start_location(Vector3 p_location); - Vector3 get_start_location() const { - return start_location; + void set_start_position(Vector3 p_position); + Vector3 get_start_position() const { + return start_position; } - void set_end_location(Vector3 p_location); - Vector3 get_end_location() const { - return end_location; + void set_end_position(Vector3 p_position); + Vector3 get_end_position() const { + return end_position; } bool check_dirty(); diff --git a/modules/navigation/nav_map.cpp b/modules/navigation/nav_map.cpp index fd735f8793..b1674c8fc5 100644 --- a/modules/navigation/nav_map.cpp +++ b/modules/navigation/nav_map.cpp @@ -31,9 +31,9 @@ #include "nav_map.h" #include "core/object/worker_thread_pool.h" +#include "nav_agent.h" #include "nav_link.h" #include "nav_region.h" -#include "rvo_agent.h" #include <algorithm> #define THREE_POINTS_CROSS_PRODUCT(m_a, m_b, m_c) (((m_c) - (m_a)).cross((m_b) - (m_a))) @@ -103,9 +103,7 @@ Vector<Vector3> NavMap::get_path(Vector3 p_origin, Vector3 p_destination, bool p float begin_d = 1e20; float end_d = 1e20; // Find the initial poly and the end poly on this map. - for (size_t i(0); i < polygons.size(); i++) { - const gd::Polygon &p = polygons[i]; - + for (const gd::Polygon &p : polygons) { // Only consider the polygon if it in a region with compatible layers. if ((p_navigation_layers & p.owner->get_navigation_layers()) == 0) { continue; @@ -190,9 +188,7 @@ Vector<Vector3> NavMap::get_path(Vector3 p_origin, Vector3 p_destination, bool p while (true) { // Takes the current least_cost_poly neighbors (iterating over its edges) and compute the traveled_distance. - for (size_t i = 0; i < navigation_polys[least_cost_id].poly->edges.size(); i++) { - const gd::Edge &edge = navigation_polys[least_cost_id].poly->edges[i]; - + for (const gd::Edge &edge : navigation_polys[least_cost_id].poly->edges) { // Iterate over connections in this edge, then compute the new optimized travel distance assigned to this polygon. for (int connection_index = 0; connection_index < edge.connections.size(); connection_index++) { const gd::Edge::Connection &connection = edge.connections[connection_index]; @@ -229,7 +225,7 @@ Vector<Vector3> NavMap::get_path(Vector3 p_origin, Vector3 p_destination, bool p avp.entry = new_entry; } } else { - // Add the neighbour polygon to the reachable ones. + // Add the neighbor polygon to the reachable ones. gd::NavigationPoly new_navigation_poly = gd::NavigationPoly(connection.polygon); new_navigation_poly.self_id = navigation_polys.size(); new_navigation_poly.back_navigation_poly_id = least_cost_id; @@ -240,7 +236,7 @@ Vector<Vector3> NavMap::get_path(Vector3 p_origin, Vector3 p_destination, bool p new_navigation_poly.entry = new_entry; navigation_polys.push_back(new_navigation_poly); - // Add the neighbour polygon to the polygons to visit. + // Add the neighbor polygon to the polygons to visit. to_visit.push_back(navigation_polys.size() - 1); } } @@ -465,9 +461,7 @@ Vector3 NavMap::get_closest_point_to_segment(const Vector3 &p_from, const Vector Vector3 closest_point; real_t closest_point_d = 1e20; - for (size_t i(0); i < polygons.size(); i++) { - const gd::Polygon &p = polygons[i]; - + for (const gd::Polygon &p : polygons) { // For each face check the distance to the segment for (size_t point_id = 2; point_id < p.points.size(); point_id += 1) { const Face3 f(p.points[0].pos, p.points[point_id - 1].pos, p.points[point_id].pos); @@ -574,18 +568,18 @@ void NavMap::remove_link(NavLink *p_link) { } } -bool NavMap::has_agent(RvoAgent *agent) const { +bool NavMap::has_agent(NavAgent *agent) const { return (agents.find(agent) != -1); } -void NavMap::add_agent(RvoAgent *agent) { +void NavMap::add_agent(NavAgent *agent) { if (!has_agent(agent)) { agents.push_back(agent); agents_dirty = true; } } -void NavMap::remove_agent(RvoAgent *agent) { +void NavMap::remove_agent(NavAgent *agent) { remove_agent_as_controlled(agent); int64_t agent_index = agents.find(agent); if (agent_index != -1) { @@ -594,7 +588,7 @@ void NavMap::remove_agent(RvoAgent *agent) { } } -void NavMap::set_agent_as_controlled(RvoAgent *agent) { +void NavMap::set_agent_as_controlled(NavAgent *agent) { const bool exist = (controlled_agents.find(agent) != -1); if (!exist) { ERR_FAIL_COND(!has_agent(agent)); @@ -602,7 +596,7 @@ void NavMap::set_agent_as_controlled(RvoAgent *agent) { } } -void NavMap::remove_agent_as_controlled(RvoAgent *agent) { +void NavMap::remove_agent_as_controlled(NavAgent *agent) { int64_t active_avoidance_agent_index = controlled_agents.find(agent); if (active_avoidance_agent_index != -1) { controlled_agents.remove_at_unordered(active_avoidance_agent_index); @@ -623,20 +617,20 @@ void NavMap::sync() { // Check if we need to update the links. if (regenerate_polygons) { - for (uint32_t r = 0; r < regions.size(); r++) { - regions[r]->scratch_polygons(); + for (NavRegion *region : regions) { + region->scratch_polygons(); } regenerate_links = true; } - for (uint32_t r = 0; r < regions.size(); r++) { - if (regions[r]->sync()) { + for (NavRegion *region : regions) { + if (region->sync()) { regenerate_links = true; } } - for (uint32_t l = 0; l < links.size(); l++) { - if (links[l]->check_dirty()) { + for (NavLink *link : links) { + if (link->check_dirty()) { regenerate_links = true; } } @@ -649,34 +643,32 @@ void NavMap::sync() { _new_pm_edge_free_count = 0; // Remove regions connections. - for (uint32_t r = 0; r < regions.size(); r++) { - regions[r]->get_connections().clear(); + for (NavRegion *region : regions) { + region->get_connections().clear(); } // Resize the polygon count. int count = 0; - for (uint32_t r = 0; r < regions.size(); r++) { - count += regions[r]->get_polygons().size(); + for (const NavRegion *region : regions) { + count += region->get_polygons().size(); } polygons.resize(count); // 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(); + for (const NavRegion *region : regions) { + 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(); // Group all edges per key. HashMap<gd::EdgeKey, Vector<gd::Edge::Connection>, gd::EdgeKey> connections; - for (uint32_t poly_id = 0; poly_id < polygons.size(); poly_id++) { - gd::Polygon &poly(polygons[poly_id]); - + for (gd::Polygon &poly : polygons) { for (uint32_t p = 0; p < poly.points.size(); p++) { int next_point = (p + 1) % poly.points.size(); gd::EdgeKey ek(poly.points[p].key, poly.points[next_point].key); @@ -787,10 +779,9 @@ void NavMap::sync() { link_polygons.resize(links.size()); // Search for polygons within range of a nav link. - for (uint32_t l = 0; l < links.size(); l++) { - const NavLink *link = links[l]; - const Vector3 start = link->get_start_location(); - const Vector3 end = link->get_end_location(); + for (const NavLink *link : links) { + const Vector3 start = link->get_start_position(); + const Vector3 end = link->get_end_position(); gd::Polygon *closest_start_polygon = nullptr; real_t closest_start_distance = link_connection_radius; @@ -820,9 +811,7 @@ void NavMap::sync() { } // Find any polygons within the search radius of the end point. - for (uint32_t end_index = 0; end_index < polygons.size(); end_index++) { - gd::Polygon &end_poly = polygons[end_index]; - + for (gd::Polygon &end_poly : polygons) { // For each face check the distance to the end for (uint32_t end_point_id = 2; end_point_id < end_poly.points.size(); end_point_id += 1) { const Face3 end_face(end_poly.points[0].pos, end_poly.points[end_point_id - 1].pos, end_poly.points[end_point_id].pos); @@ -906,8 +895,8 @@ void NavMap::sync() { // cannot use LocalVector here as RVO library expects std::vector to build KdTree std::vector<RVO::Agent *> raw_agents; raw_agents.reserve(agents.size()); - for (size_t i(0); i < agents.size(); i++) { - raw_agents.push_back(agents[i]->get_agent()); + for (NavAgent *agent : agents) { + raw_agents.push_back(agent->get_agent()); } rvo.buildAgentTree(raw_agents); } @@ -927,7 +916,7 @@ void NavMap::sync() { pm_edge_free_count = _new_pm_edge_free_count; } -void NavMap::compute_single_step(uint32_t index, RvoAgent **agent) { +void NavMap::compute_single_step(uint32_t index, NavAgent **agent) { (*(agent + index))->get_agent()->computeNeighbors(&rvo); (*(agent + index))->get_agent()->computeNewVelocity(deltatime); } @@ -941,8 +930,8 @@ void NavMap::step(real_t p_deltatime) { } void NavMap::dispatch_callbacks() { - for (int i(0); i < static_cast<int>(controlled_agents.size()); i++) { - controlled_agents[i]->dispatch_callback(); + for (NavAgent *agent : controlled_agents) { + agent->dispatch_callback(); } } diff --git a/modules/navigation/nav_map.h b/modules/navigation/nav_map.h index fce7aff3ba..ab6a48dd70 100644 --- a/modules/navigation/nav_map.h +++ b/modules/navigation/nav_map.h @@ -42,7 +42,7 @@ class NavLink; class NavRegion; -class RvoAgent; +class NavAgent; class NavMap : public NavRid { /// Map Up @@ -78,10 +78,10 @@ class NavMap : public NavRid { bool agents_dirty = false; /// All the Agents (even the controlled one) - LocalVector<RvoAgent *> agents; + LocalVector<NavAgent *> agents; /// Controlled agents - LocalVector<RvoAgent *> controlled_agents; + LocalVector<NavAgent *> controlled_agents; /// Physics delta time real_t deltatime = 0.0; @@ -144,15 +144,15 @@ public: return links; } - bool has_agent(RvoAgent *agent) const; - void add_agent(RvoAgent *agent); - void remove_agent(RvoAgent *agent); - const LocalVector<RvoAgent *> &get_agents() const { + bool has_agent(NavAgent *agent) const; + void add_agent(NavAgent *agent); + void remove_agent(NavAgent *agent); + const LocalVector<NavAgent *> &get_agents() const { return agents; } - void set_agent_as_controlled(RvoAgent *agent); - void remove_agent_as_controlled(RvoAgent *agent); + void set_agent_as_controlled(NavAgent *agent); + void remove_agent_as_controlled(NavAgent *agent); uint32_t get_map_update_id() const { return map_update_id; @@ -173,7 +173,7 @@ public: int get_pm_edge_free_count() const { return pm_edge_free_count; } private: - void compute_single_step(uint32_t index, RvoAgent **agent); + void compute_single_step(uint32_t index, NavAgent **agent); void clip_path(const LocalVector<gd::NavigationPoly> &p_navigation_polys, Vector<Vector3> &path, const gd::NavigationPoly *from_poly, const Vector3 &p_to_point, const gd::NavigationPoly *p_to_poly, Vector<int32_t> *r_path_types, TypedArray<RID> *r_path_rids, Vector<int64_t> *r_path_owners) const; }; diff --git a/modules/navigation/nav_utils.h b/modules/navigation/nav_utils.h index 50437469aa..06a1a1f403 100644 --- a/modules/navigation/nav_utils.h +++ b/modules/navigation/nav_utils.h @@ -125,7 +125,7 @@ struct NavigationPoly { Vector3 back_navigation_edge_pathway_start; Vector3 back_navigation_edge_pathway_end; - /// The entry location of this poly. + /// The entry position of this poly. Vector3 entry; /// The distance to the destination. float traveled_distance = 0.0; diff --git a/modules/navigation/navigation_mesh_generator.cpp b/modules/navigation/navigation_mesh_generator.cpp index fff7a02fc4..74ff9312fd 100644 --- a/modules/navigation/navigation_mesh_generator.cpp +++ b/modules/navigation/navigation_mesh_generator.cpp @@ -266,9 +266,7 @@ void NavigationMeshGenerator::_parse_geometry(const Transform3D &p_navmesh_trans if (err == OK) { PackedVector3Array faces; - for (uint32_t j = 0; j < md.faces.size(); ++j) { - const Geometry3D::MeshData::Face &face = md.faces[j]; - + for (const Geometry3D::MeshData::Face &face : md.faces) { for (uint32_t k = 2; k < face.indices.size(); ++k) { faces.push_back(md.vertices[face.indices[0]]); faces.push_back(md.vertices[face.indices[k - 1]]); @@ -392,9 +390,7 @@ void NavigationMeshGenerator::_parse_geometry(const Transform3D &p_navmesh_trans if (err == OK) { PackedVector3Array faces; - for (uint32_t j = 0; j < md.faces.size(); ++j) { - const Geometry3D::MeshData::Face &face = md.faces[j]; - + for (const Geometry3D::MeshData::Face &face : md.faces) { for (uint32_t k = 2; k < face.indices.size(); ++k) { faces.push_back(md.vertices[face.indices[0]]); faces.push_back(md.vertices[face.indices[k - 1]]); |