initial commit, 4.5 stable
Some checks failed
🔗 GHA / 📊 Static checks (push) Has been cancelled
🔗 GHA / 🤖 Android (push) Has been cancelled
🔗 GHA / 🍏 iOS (push) Has been cancelled
🔗 GHA / 🐧 Linux (push) Has been cancelled
🔗 GHA / 🍎 macOS (push) Has been cancelled
🔗 GHA / 🏁 Windows (push) Has been cancelled
🔗 GHA / 🌐 Web (push) Has been cancelled
Some checks failed
🔗 GHA / 📊 Static checks (push) Has been cancelled
🔗 GHA / 🤖 Android (push) Has been cancelled
🔗 GHA / 🍏 iOS (push) Has been cancelled
🔗 GHA / 🐧 Linux (push) Has been cancelled
🔗 GHA / 🍎 macOS (push) Has been cancelled
🔗 GHA / 🏁 Windows (push) Has been cancelled
🔗 GHA / 🌐 Web (push) Has been cancelled
This commit is contained in:
1439
modules/navigation_2d/2d/godot_navigation_server_2d.cpp
Normal file
1439
modules/navigation_2d/2d/godot_navigation_server_2d.cpp
Normal file
File diff suppressed because it is too large
Load Diff
355
modules/navigation_2d/2d/godot_navigation_server_2d.h
Normal file
355
modules/navigation_2d/2d/godot_navigation_server_2d.h
Normal file
@@ -0,0 +1,355 @@
|
||||
/**************************************************************************/
|
||||
/* godot_navigation_server_2d.h */
|
||||
/**************************************************************************/
|
||||
/* This file is part of: */
|
||||
/* GODOT ENGINE */
|
||||
/* https://godotengine.org */
|
||||
/**************************************************************************/
|
||||
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
||||
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
||||
/* */
|
||||
/* Permission is hereby granted, free of charge, to any person obtaining */
|
||||
/* a copy of this software and associated documentation files (the */
|
||||
/* "Software"), to deal in the Software without restriction, including */
|
||||
/* without limitation the rights to use, copy, modify, merge, publish, */
|
||||
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
||||
/* permit persons to whom the Software is furnished to do so, subject to */
|
||||
/* the following conditions: */
|
||||
/* */
|
||||
/* The above copyright notice and this permission notice shall be */
|
||||
/* included in all copies or substantial portions of the Software. */
|
||||
/* */
|
||||
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
||||
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
||||
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
|
||||
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
||||
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
||||
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
||||
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
||||
/**************************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "../nav_agent_2d.h"
|
||||
#include "../nav_link_2d.h"
|
||||
#include "../nav_map_2d.h"
|
||||
#include "../nav_obstacle_2d.h"
|
||||
#include "../nav_region_2d.h"
|
||||
|
||||
#include "core/templates/local_vector.h"
|
||||
#include "core/templates/rid.h"
|
||||
#include "core/templates/rid_owner.h"
|
||||
#include "servers/navigation/navigation_path_query_parameters_2d.h"
|
||||
#include "servers/navigation/navigation_path_query_result_2d.h"
|
||||
#include "servers/navigation_server_2d.h"
|
||||
|
||||
/// The commands are functions executed during the `sync` phase.
|
||||
|
||||
#define MERGE_INTERNAL(A, B) A##B
|
||||
#define MERGE(A, B) MERGE_INTERNAL(A, B)
|
||||
|
||||
#define COMMAND_1(F_NAME, T_0, D_0) \
|
||||
virtual void F_NAME(T_0 D_0) override; \
|
||||
void MERGE(_cmd_, F_NAME)(T_0 D_0)
|
||||
|
||||
#define COMMAND_2(F_NAME, T_0, D_0, T_1, D_1) \
|
||||
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)
|
||||
|
||||
class GodotNavigationServer2D;
|
||||
#ifdef CLIPPER2_ENABLED
|
||||
class NavMeshGenerator2D;
|
||||
#endif // CLIPPER2_ENABLED
|
||||
|
||||
struct SetCommand2D {
|
||||
virtual ~SetCommand2D() {}
|
||||
virtual void exec(GodotNavigationServer2D *p_server) = 0;
|
||||
};
|
||||
|
||||
// This server exposes the `NavigationServer3D` features in the 2D world.
|
||||
class GodotNavigationServer2D : public NavigationServer2D {
|
||||
GDCLASS(GodotNavigationServer2D, NavigationServer2D);
|
||||
|
||||
Mutex commands_mutex;
|
||||
/// Mutex used to make any operation threadsafe.
|
||||
Mutex operations_mutex;
|
||||
|
||||
LocalVector<SetCommand2D *> commands;
|
||||
|
||||
mutable RID_Owner<NavLink2D> link_owner;
|
||||
mutable RID_Owner<NavMap2D> map_owner;
|
||||
mutable RID_Owner<NavRegion2D> region_owner;
|
||||
mutable RID_Owner<NavAgent2D> agent_owner;
|
||||
mutable RID_Owner<NavObstacle2D> obstacle_owner;
|
||||
|
||||
bool active = true;
|
||||
LocalVector<NavMap2D *> active_maps;
|
||||
|
||||
#ifdef CLIPPER2_ENABLED
|
||||
NavMeshGenerator2D *navmesh_generator_2d = nullptr;
|
||||
#endif // CLIPPER2_ENABLED
|
||||
|
||||
// Performance Monitor.
|
||||
int pm_region_count = 0;
|
||||
int pm_agent_count = 0;
|
||||
int pm_link_count = 0;
|
||||
int pm_polygon_count = 0;
|
||||
int pm_edge_count = 0;
|
||||
int pm_edge_merge_count = 0;
|
||||
int pm_edge_connection_count = 0;
|
||||
int pm_edge_free_count = 0;
|
||||
int pm_obstacle_count = 0;
|
||||
|
||||
public:
|
||||
GodotNavigationServer2D();
|
||||
virtual ~GodotNavigationServer2D();
|
||||
|
||||
void add_command(SetCommand2D *p_command);
|
||||
|
||||
virtual TypedArray<RID> get_maps() const override;
|
||||
|
||||
virtual RID map_create() override;
|
||||
COMMAND_2(map_set_active, RID, p_map, bool, p_active);
|
||||
virtual bool map_is_active(RID p_map) const override;
|
||||
|
||||
COMMAND_2(map_set_cell_size, RID, p_map, real_t, p_cell_size);
|
||||
virtual real_t map_get_cell_size(RID p_map) const override;
|
||||
|
||||
COMMAND_2(map_set_merge_rasterizer_cell_scale, RID, p_map, float, p_value);
|
||||
virtual float map_get_merge_rasterizer_cell_scale(RID p_map) const override;
|
||||
|
||||
COMMAND_2(map_set_use_edge_connections, RID, p_map, bool, p_enabled);
|
||||
virtual bool map_get_use_edge_connections(RID p_map) const override;
|
||||
|
||||
COMMAND_2(map_set_edge_connection_margin, RID, p_map, real_t, p_connection_margin);
|
||||
virtual real_t map_get_edge_connection_margin(RID p_map) const override;
|
||||
|
||||
COMMAND_2(map_set_link_connection_radius, RID, p_map, real_t, p_connection_radius);
|
||||
virtual real_t map_get_link_connection_radius(RID p_map) const override;
|
||||
|
||||
virtual Vector<Vector2> map_get_path(RID p_map, Vector2 p_origin, Vector2 p_destination, bool p_optimize, uint32_t p_navigation_layers = 1) override;
|
||||
|
||||
virtual Vector2 map_get_closest_point(RID p_map, const Vector2 &p_point) const override;
|
||||
|
||||
virtual RID map_get_closest_point_owner(RID p_map, const Vector2 &p_point) const override;
|
||||
|
||||
virtual TypedArray<RID> map_get_links(RID p_map) const override;
|
||||
virtual TypedArray<RID> map_get_regions(RID p_map) const override;
|
||||
virtual TypedArray<RID> map_get_agents(RID p_map) const override;
|
||||
virtual TypedArray<RID> map_get_obstacles(RID p_map) const override;
|
||||
|
||||
virtual void map_force_update(RID p_map) override;
|
||||
virtual uint32_t map_get_iteration_id(RID p_map) const override;
|
||||
|
||||
COMMAND_2(map_set_use_async_iterations, RID, p_map, bool, p_enabled);
|
||||
virtual bool map_get_use_async_iterations(RID p_map) const override;
|
||||
|
||||
virtual Vector2 map_get_random_point(RID p_map, uint32_t p_navigation_layers, bool p_uniformly) const override;
|
||||
|
||||
virtual RID region_create() override;
|
||||
virtual uint32_t region_get_iteration_id(RID p_region) const override;
|
||||
|
||||
COMMAND_2(region_set_use_async_iterations, RID, p_region, bool, p_enabled);
|
||||
virtual bool region_get_use_async_iterations(RID p_region) const override;
|
||||
|
||||
COMMAND_2(region_set_enabled, RID, p_region, bool, p_enabled);
|
||||
virtual bool region_get_enabled(RID p_region) const override;
|
||||
|
||||
COMMAND_2(region_set_use_edge_connections, RID, p_region, bool, p_enabled);
|
||||
virtual bool region_get_use_edge_connections(RID p_region) const override;
|
||||
|
||||
COMMAND_2(region_set_enter_cost, RID, p_region, real_t, p_enter_cost);
|
||||
virtual real_t region_get_enter_cost(RID p_region) const override;
|
||||
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 override;
|
||||
|
||||
COMMAND_2(region_set_owner_id, RID, p_region, ObjectID, p_owner_id);
|
||||
virtual ObjectID region_get_owner_id(RID p_region) const override;
|
||||
|
||||
virtual bool region_owns_point(RID p_region, const Vector2 &p_point) 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_navigation_layers, RID, p_region, uint32_t, p_navigation_layers);
|
||||
virtual uint32_t region_get_navigation_layers(RID p_region) const override;
|
||||
COMMAND_2(region_set_transform, RID, p_region, Transform2D, p_transform);
|
||||
virtual Transform2D region_get_transform(RID p_region) const override;
|
||||
COMMAND_2(region_set_navigation_polygon, RID, p_region, Ref<NavigationPolygon>, p_navigation_polygon);
|
||||
virtual int region_get_connections_count(RID p_region) const override;
|
||||
virtual Vector2 region_get_connection_pathway_start(RID p_region, int p_connection_id) const override;
|
||||
virtual Vector2 region_get_connection_pathway_end(RID p_region, int p_connection_id) const override;
|
||||
virtual Vector2 region_get_closest_point(RID p_region, const Vector2 &p_point) const override;
|
||||
virtual Vector2 region_get_random_point(RID p_region, uint32_t p_navigation_layers, bool p_uniformly) const override;
|
||||
virtual Rect2 region_get_bounds(RID p_region) const override;
|
||||
|
||||
virtual RID link_create() override;
|
||||
virtual uint32_t link_get_iteration_id(RID p_link) const override;
|
||||
|
||||
/// Set the map of this link.
|
||||
COMMAND_2(link_set_map, RID, p_link, RID, p_map);
|
||||
virtual RID link_get_map(RID p_link) const override;
|
||||
COMMAND_2(link_set_enabled, RID, p_link, bool, p_enabled);
|
||||
virtual bool link_get_enabled(RID p_link) const override;
|
||||
|
||||
/// Set whether this link travels in both directions.
|
||||
COMMAND_2(link_set_bidirectional, RID, p_link, bool, p_bidirectional);
|
||||
virtual bool link_is_bidirectional(RID p_link) const override;
|
||||
|
||||
/// Set the link's layers.
|
||||
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;
|
||||
|
||||
/// Set the start position of the link.
|
||||
COMMAND_2(link_set_start_position, RID, p_link, Vector2, p_position);
|
||||
virtual Vector2 link_get_start_position(RID p_link) const override;
|
||||
|
||||
/// Set the end position of the link.
|
||||
COMMAND_2(link_set_end_position, RID, p_link, Vector2, p_position);
|
||||
virtual Vector2 link_get_end_position(RID p_link) const override;
|
||||
|
||||
/// Set the enter cost of the link.
|
||||
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;
|
||||
|
||||
/// Set the travel cost of the link.
|
||||
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 override;
|
||||
|
||||
/// Set the node which manages this link.
|
||||
COMMAND_2(link_set_owner_id, RID, p_link, ObjectID, p_owner_id);
|
||||
virtual ObjectID link_get_owner_id(RID p_link) const override;
|
||||
|
||||
/// Creates the agent.
|
||||
virtual RID agent_create() override;
|
||||
|
||||
/// Put the agent in the map.
|
||||
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_paused, RID, p_agent, bool, p_paused);
|
||||
virtual bool agent_get_paused(RID p_agent) const override;
|
||||
|
||||
COMMAND_2(agent_set_avoidance_enabled, RID, p_agent, bool, p_enabled);
|
||||
virtual bool agent_get_avoidance_enabled(RID p_agent) const override;
|
||||
|
||||
/// The maximum distance (center point to
|
||||
/// center point) to other agents this agent
|
||||
/// takes into account in the navigation. The
|
||||
/// larger this number, the longer the running
|
||||
/// time of the simulation. If the number is too
|
||||
/// low, the simulation will not be safe.
|
||||
/// Must be non-negative.
|
||||
COMMAND_2(agent_set_neighbor_distance, RID, p_agent, real_t, p_distance);
|
||||
virtual real_t agent_get_neighbor_distance(RID p_agent) const override;
|
||||
|
||||
/// The maximum number of other agents this
|
||||
/// agent takes into account in the navigation.
|
||||
/// The larger this number, the longer the
|
||||
/// running time of the simulation. If the
|
||||
/// number is too low, the simulation will not
|
||||
/// be safe.
|
||||
COMMAND_2(agent_set_max_neighbors, RID, p_agent, int, p_count);
|
||||
virtual int agent_get_max_neighbors(RID p_agent) const override;
|
||||
|
||||
/// The minimal amount of time for which this
|
||||
/// agent's velocities that are computed by the
|
||||
/// simulation are safe with respect to other
|
||||
/// agents. The larger this number, the sooner
|
||||
/// this agent will respond to the presence of
|
||||
/// other agents, but the less freedom this
|
||||
/// agent has in choosing its velocities.
|
||||
/// Must be positive.
|
||||
COMMAND_2(agent_set_time_horizon_agents, RID, p_agent, real_t, p_time_horizon);
|
||||
virtual real_t agent_get_time_horizon_agents(RID p_agent) const override;
|
||||
COMMAND_2(agent_set_time_horizon_obstacles, RID, p_agent, real_t, p_time_horizon);
|
||||
virtual real_t agent_get_time_horizon_obstacles(RID p_agent) const override;
|
||||
|
||||
/// The radius of this agent.
|
||||
/// Must be non-negative.
|
||||
COMMAND_2(agent_set_radius, RID, p_agent, real_t, p_radius);
|
||||
virtual real_t agent_get_radius(RID p_agent) const override;
|
||||
|
||||
/// The maximum speed of this agent.
|
||||
/// Must be non-negative.
|
||||
COMMAND_2(agent_set_max_speed, RID, p_agent, real_t, p_max_speed);
|
||||
virtual real_t agent_get_max_speed(RID p_agent) const override;
|
||||
|
||||
/// forces and agent velocity change in the avoidance simulation, adds simulation instability if done recklessly
|
||||
COMMAND_2(agent_set_velocity_forced, RID, p_agent, Vector2, p_velocity);
|
||||
|
||||
/// The wanted velocity for the agent as a "suggestion" to the avoidance simulation.
|
||||
/// The simulation will try to fulfill this velocity wish if possible but may change the velocity depending on other agent's and obstacles'.
|
||||
COMMAND_2(agent_set_velocity, RID, p_agent, Vector2, p_velocity);
|
||||
virtual Vector2 agent_get_velocity(RID p_agent) const override;
|
||||
|
||||
/// Position of the agent in world space.
|
||||
COMMAND_2(agent_set_position, RID, p_agent, Vector2, p_position);
|
||||
virtual Vector2 agent_get_position(RID p_agent) const override;
|
||||
|
||||
/// Returns true if the map got changed the previous frame.
|
||||
virtual bool agent_is_map_changed(RID p_agent) const override;
|
||||
|
||||
/// Callback called at the end of the RVO process
|
||||
COMMAND_2(agent_set_avoidance_callback, RID, p_agent, Callable, p_callback);
|
||||
virtual bool agent_has_avoidance_callback(RID p_agent) const override;
|
||||
|
||||
COMMAND_2(agent_set_avoidance_layers, RID, p_agent, uint32_t, p_layers);
|
||||
virtual uint32_t agent_get_avoidance_layers(RID p_agent) const override;
|
||||
|
||||
COMMAND_2(agent_set_avoidance_mask, RID, p_agent, uint32_t, p_mask);
|
||||
virtual uint32_t agent_get_avoidance_mask(RID p_agent) const override;
|
||||
|
||||
COMMAND_2(agent_set_avoidance_priority, RID, p_agent, real_t, p_priority);
|
||||
virtual real_t agent_get_avoidance_priority(RID p_agent) const override;
|
||||
|
||||
virtual RID obstacle_create() override;
|
||||
COMMAND_2(obstacle_set_avoidance_enabled, RID, p_obstacle, bool, p_enabled);
|
||||
virtual bool obstacle_get_avoidance_enabled(RID p_obstacle) const override;
|
||||
COMMAND_2(obstacle_set_map, RID, p_obstacle, RID, p_map);
|
||||
virtual RID obstacle_get_map(RID p_obstacle) const override;
|
||||
COMMAND_2(obstacle_set_paused, RID, p_obstacle, bool, p_paused);
|
||||
virtual bool obstacle_get_paused(RID p_obstacle) const override;
|
||||
COMMAND_2(obstacle_set_radius, RID, p_obstacle, real_t, p_radius);
|
||||
virtual real_t obstacle_get_radius(RID p_obstacle) const override;
|
||||
COMMAND_2(obstacle_set_velocity, RID, p_obstacle, Vector2, p_velocity);
|
||||
virtual Vector2 obstacle_get_velocity(RID p_obstacle) const override;
|
||||
COMMAND_2(obstacle_set_position, RID, p_obstacle, Vector2, p_position);
|
||||
virtual Vector2 obstacle_get_position(RID p_obstacle) const override;
|
||||
COMMAND_2(obstacle_set_vertices, RID, p_obstacle, const Vector<Vector2> &, p_vertices);
|
||||
virtual Vector<Vector2> obstacle_get_vertices(RID p_obstacle) const override;
|
||||
COMMAND_2(obstacle_set_avoidance_layers, RID, p_obstacle, uint32_t, p_layers);
|
||||
virtual uint32_t obstacle_get_avoidance_layers(RID p_obstacle) const override;
|
||||
|
||||
virtual void query_path(const Ref<NavigationPathQueryParameters2D> &p_query_parameters, Ref<NavigationPathQueryResult2D> p_query_result, const Callable &p_callback = Callable()) override;
|
||||
|
||||
COMMAND_1(free, RID, p_object);
|
||||
|
||||
virtual void set_active(bool p_active) override;
|
||||
|
||||
void flush_queries();
|
||||
|
||||
virtual void process(double p_delta_time) override;
|
||||
virtual void physics_process(double p_delta_time) override;
|
||||
virtual void init() override;
|
||||
virtual void sync() override;
|
||||
virtual void finish() override;
|
||||
|
||||
virtual int get_process_info(ProcessInfo p_info) const override;
|
||||
|
||||
virtual void parse_source_geometry_data(const Ref<NavigationPolygon> &p_navigation_mesh, const Ref<NavigationMeshSourceGeometryData2D> &p_source_geometry_data, Node *p_root_node, const Callable &p_callback = Callable()) override;
|
||||
virtual void bake_from_source_geometry_data(const Ref<NavigationPolygon> &p_navigation_mesh, const Ref<NavigationMeshSourceGeometryData2D> &p_source_geometry_data, const Callable &p_callback = Callable()) override;
|
||||
virtual void bake_from_source_geometry_data_async(const Ref<NavigationPolygon> &p_navigation_mesh, const Ref<NavigationMeshSourceGeometryData2D> &p_source_geometry_data, const Callable &p_callback = Callable()) override;
|
||||
virtual bool is_baking_navigation_polygon(Ref<NavigationPolygon> p_navigation_polygon) const override;
|
||||
|
||||
virtual RID source_geometry_parser_create() override;
|
||||
virtual void source_geometry_parser_set_callback(RID p_parser, const Callable &p_callback) override;
|
||||
|
||||
virtual Vector<Vector2> simplify_path(const Vector<Vector2> &p_path, real_t p_epsilon) override;
|
||||
|
||||
private:
|
||||
void internal_free_agent(RID p_object);
|
||||
void internal_free_obstacle(RID p_object);
|
||||
};
|
||||
|
||||
#undef COMMAND_1
|
||||
#undef COMMAND_2
|
68
modules/navigation_2d/2d/nav_base_iteration_2d.h
Normal file
68
modules/navigation_2d/2d/nav_base_iteration_2d.h
Normal file
@@ -0,0 +1,68 @@
|
||||
/**************************************************************************/
|
||||
/* nav_base_iteration_2d.h */
|
||||
/**************************************************************************/
|
||||
/* This file is part of: */
|
||||
/* GODOT ENGINE */
|
||||
/* https://godotengine.org */
|
||||
/**************************************************************************/
|
||||
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
||||
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
||||
/* */
|
||||
/* Permission is hereby granted, free of charge, to any person obtaining */
|
||||
/* a copy of this software and associated documentation files (the */
|
||||
/* "Software"), to deal in the Software without restriction, including */
|
||||
/* without limitation the rights to use, copy, modify, merge, publish, */
|
||||
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
||||
/* permit persons to whom the Software is furnished to do so, subject to */
|
||||
/* the following conditions: */
|
||||
/* */
|
||||
/* The above copyright notice and this permission notice shall be */
|
||||
/* included in all copies or substantial portions of the Software. */
|
||||
/* */
|
||||
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
||||
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
||||
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
|
||||
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
||||
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
||||
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
||||
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
||||
/**************************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "../nav_utils_2d.h"
|
||||
|
||||
#include "core/object/ref_counted.h"
|
||||
#include "servers/navigation/navigation_utilities.h"
|
||||
|
||||
class NavBaseIteration2D : public RefCounted {
|
||||
GDCLASS(NavBaseIteration2D, RefCounted);
|
||||
|
||||
public:
|
||||
bool enabled = true;
|
||||
uint32_t navigation_layers = 1;
|
||||
real_t enter_cost = 0.0;
|
||||
real_t travel_cost = 1.0;
|
||||
NavigationUtilities::PathSegmentType owner_type;
|
||||
ObjectID owner_object_id;
|
||||
RID owner_rid;
|
||||
bool owner_use_edge_connections = false;
|
||||
LocalVector<Nav2D::Polygon> navmesh_polygons;
|
||||
LocalVector<LocalVector<Nav2D::Connection>> internal_connections;
|
||||
|
||||
bool get_enabled() const { return enabled; }
|
||||
NavigationUtilities::PathSegmentType get_type() const { return owner_type; }
|
||||
RID get_self() const { return owner_rid; }
|
||||
ObjectID get_owner_id() const { return owner_object_id; }
|
||||
uint32_t get_navigation_layers() const { return navigation_layers; }
|
||||
real_t get_enter_cost() const { return enter_cost; }
|
||||
real_t get_travel_cost() const { return travel_cost; }
|
||||
bool get_use_edge_connections() const { return owner_use_edge_connections; }
|
||||
const LocalVector<Nav2D::Polygon> &get_navmesh_polygons() const { return navmesh_polygons; }
|
||||
const LocalVector<LocalVector<Nav2D::Connection>> &get_internal_connections() const { return internal_connections; }
|
||||
|
||||
virtual ~NavBaseIteration2D() {
|
||||
navmesh_polygons.clear();
|
||||
internal_connections.clear();
|
||||
}
|
||||
};
|
425
modules/navigation_2d/2d/nav_map_builder_2d.cpp
Normal file
425
modules/navigation_2d/2d/nav_map_builder_2d.cpp
Normal file
@@ -0,0 +1,425 @@
|
||||
/**************************************************************************/
|
||||
/* nav_map_builder_2d.cpp */
|
||||
/**************************************************************************/
|
||||
/* This file is part of: */
|
||||
/* GODOT ENGINE */
|
||||
/* https://godotengine.org */
|
||||
/**************************************************************************/
|
||||
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
||||
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
||||
/* */
|
||||
/* Permission is hereby granted, free of charge, to any person obtaining */
|
||||
/* a copy of this software and associated documentation files (the */
|
||||
/* "Software"), to deal in the Software without restriction, including */
|
||||
/* without limitation the rights to use, copy, modify, merge, publish, */
|
||||
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
||||
/* permit persons to whom the Software is furnished to do so, subject to */
|
||||
/* the following conditions: */
|
||||
/* */
|
||||
/* The above copyright notice and this permission notice shall be */
|
||||
/* included in all copies or substantial portions of the Software. */
|
||||
/* */
|
||||
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
||||
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
||||
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
|
||||
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
||||
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
||||
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
||||
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
||||
/**************************************************************************/
|
||||
|
||||
#include "nav_map_builder_2d.h"
|
||||
|
||||
#include "../nav_link_2d.h"
|
||||
#include "../nav_map_2d.h"
|
||||
#include "../nav_region_2d.h"
|
||||
#include "../triangle2.h"
|
||||
#include "nav_map_iteration_2d.h"
|
||||
#include "nav_region_iteration_2d.h"
|
||||
|
||||
using namespace Nav2D;
|
||||
|
||||
PointKey NavMapBuilder2D::get_point_key(const Vector2 &p_pos, const Vector2 &p_cell_size) {
|
||||
const int x = static_cast<int>(Math::floor(p_pos.x / p_cell_size.x));
|
||||
const int y = static_cast<int>(Math::floor(p_pos.y / p_cell_size.y));
|
||||
|
||||
PointKey p;
|
||||
p.key = 0;
|
||||
p.x = x;
|
||||
p.y = y;
|
||||
return p;
|
||||
}
|
||||
|
||||
void NavMapBuilder2D::build_navmap_iteration(NavMapIterationBuild2D &r_build) {
|
||||
PerformanceData &performance_data = r_build.performance_data;
|
||||
|
||||
performance_data.pm_polygon_count = 0;
|
||||
performance_data.pm_edge_count = 0;
|
||||
performance_data.pm_edge_merge_count = 0;
|
||||
performance_data.pm_edge_connection_count = 0;
|
||||
performance_data.pm_edge_free_count = 0;
|
||||
|
||||
_build_step_gather_region_polygons(r_build);
|
||||
|
||||
_build_step_find_edge_connection_pairs(r_build);
|
||||
|
||||
_build_step_merge_edge_connection_pairs(r_build);
|
||||
|
||||
_build_step_edge_connection_margin_connections(r_build);
|
||||
|
||||
_build_step_navlink_connections(r_build);
|
||||
|
||||
_build_update_map_iteration(r_build);
|
||||
}
|
||||
|
||||
void NavMapBuilder2D::_build_step_gather_region_polygons(NavMapIterationBuild2D &r_build) {
|
||||
PerformanceData &performance_data = r_build.performance_data;
|
||||
NavMapIteration2D *map_iteration = r_build.map_iteration;
|
||||
|
||||
const LocalVector<Ref<NavRegionIteration2D>> ®ions = map_iteration->region_iterations;
|
||||
HashMap<const NavBaseIteration2D *, LocalVector<Connection>> ®ion_external_connections = map_iteration->external_region_connections;
|
||||
|
||||
map_iteration->navbases_polygons_external_connections.clear();
|
||||
|
||||
// Remove regions connections.
|
||||
region_external_connections.clear();
|
||||
|
||||
// Copy all region polygons in the map.
|
||||
int polygon_count = 0;
|
||||
for (const Ref<NavRegionIteration2D> ®ion : regions) {
|
||||
const uint32_t polygons_size = region->navmesh_polygons.size();
|
||||
polygon_count += polygons_size;
|
||||
|
||||
region_external_connections[region.ptr()] = LocalVector<Connection>();
|
||||
map_iteration->navbases_polygons_external_connections[region.ptr()] = LocalVector<LocalVector<Connection>>();
|
||||
map_iteration->navbases_polygons_external_connections[region.ptr()].resize(polygons_size);
|
||||
}
|
||||
|
||||
performance_data.pm_polygon_count = polygon_count;
|
||||
r_build.polygon_count = polygon_count;
|
||||
}
|
||||
|
||||
void NavMapBuilder2D::_build_step_find_edge_connection_pairs(NavMapIterationBuild2D &r_build) {
|
||||
PerformanceData &performance_data = r_build.performance_data;
|
||||
NavMapIteration2D *map_iteration = r_build.map_iteration;
|
||||
int polygon_count = r_build.polygon_count;
|
||||
|
||||
HashMap<EdgeKey, EdgeConnectionPair, EdgeKey> &connection_pairs_map = r_build.iter_connection_pairs_map;
|
||||
|
||||
// Group all edges per key.
|
||||
connection_pairs_map.clear();
|
||||
connection_pairs_map.reserve(polygon_count);
|
||||
int free_edges_count = 0; // How many ConnectionPairs have only one Connection.
|
||||
|
||||
for (const Ref<NavRegionIteration2D> ®ion : map_iteration->region_iterations) {
|
||||
for (const ConnectableEdge &connectable_edge : region->get_external_edges()) {
|
||||
const EdgeKey &ek = connectable_edge.ek;
|
||||
|
||||
HashMap<EdgeKey, EdgeConnectionPair, EdgeKey>::Iterator pair_it = connection_pairs_map.find(ek);
|
||||
if (!pair_it) {
|
||||
pair_it = connection_pairs_map.insert(ek, EdgeConnectionPair());
|
||||
performance_data.pm_edge_count += 1;
|
||||
++free_edges_count;
|
||||
}
|
||||
EdgeConnectionPair &pair = pair_it->value;
|
||||
if (pair.size < 2) {
|
||||
// Add the polygon/edge tuple to this key.
|
||||
Connection new_connection;
|
||||
new_connection.polygon = ®ion->navmesh_polygons[connectable_edge.polygon_index];
|
||||
new_connection.edge = connectable_edge.edge;
|
||||
new_connection.pathway_start = connectable_edge.pathway_start;
|
||||
new_connection.pathway_end = connectable_edge.pathway_end;
|
||||
|
||||
pair.connections[pair.size] = new_connection;
|
||||
++pair.size;
|
||||
if (pair.size == 2) {
|
||||
--free_edges_count;
|
||||
}
|
||||
|
||||
} else {
|
||||
// The edge is already connected with another edge, skip.
|
||||
ERR_PRINT_ONCE("Navigation map synchronization error. Attempted to merge a navigation mesh polygon edge with another already-merged edge. This is usually caused by crossing edges, overlapping polygons, or a mismatch of the NavigationMesh / NavigationPolygon baked 'cell_size' and navigation map 'cell_size'. If you're certain none of above is the case, change 'navigation/2d/merge_rasterizer_cell_scale' to 0.001.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
r_build.free_edge_count = free_edges_count;
|
||||
}
|
||||
|
||||
void NavMapBuilder2D::_build_step_merge_edge_connection_pairs(NavMapIterationBuild2D &r_build) {
|
||||
PerformanceData &performance_data = r_build.performance_data;
|
||||
|
||||
HashMap<EdgeKey, EdgeConnectionPair, EdgeKey> &connection_pairs_map = r_build.iter_connection_pairs_map;
|
||||
LocalVector<Connection> &free_edges = r_build.iter_free_edges;
|
||||
int free_edges_count = r_build.free_edge_count;
|
||||
bool use_edge_connections = r_build.use_edge_connections;
|
||||
|
||||
free_edges.clear();
|
||||
free_edges.reserve(free_edges_count);
|
||||
|
||||
NavMapIteration2D *map_iteration = r_build.map_iteration;
|
||||
|
||||
HashMap<const NavBaseIteration2D *, LocalVector<LocalVector<Nav2D::Connection>>> &navbases_polygons_external_connections = map_iteration->navbases_polygons_external_connections;
|
||||
|
||||
for (const KeyValue<EdgeKey, EdgeConnectionPair> &pair_it : connection_pairs_map) {
|
||||
const EdgeConnectionPair &pair = pair_it.value;
|
||||
if (pair.size == 2) {
|
||||
// Connect edge that are shared in different polygons.
|
||||
const Connection &c1 = pair.connections[0];
|
||||
const Connection &c2 = pair.connections[1];
|
||||
|
||||
navbases_polygons_external_connections[c1.polygon->owner][c1.polygon->id].push_back(c2);
|
||||
navbases_polygons_external_connections[c2.polygon->owner][c2.polygon->id].push_back(c1);
|
||||
performance_data.pm_edge_connection_count += 1;
|
||||
|
||||
} else {
|
||||
CRASH_COND_MSG(pair.size != 1, vformat("Number of connection != 1. Found: %d", pair.size));
|
||||
if (use_edge_connections && pair.connections[0].polygon->owner->get_use_edge_connections()) {
|
||||
free_edges.push_back(pair.connections[0]);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void NavMapBuilder2D::_build_step_edge_connection_margin_connections(NavMapIterationBuild2D &r_build) {
|
||||
PerformanceData &performance_data = r_build.performance_data;
|
||||
NavMapIteration2D *map_iteration = r_build.map_iteration;
|
||||
|
||||
real_t edge_connection_margin = r_build.edge_connection_margin;
|
||||
|
||||
LocalVector<Connection> &free_edges = r_build.iter_free_edges;
|
||||
HashMap<const NavBaseIteration2D *, LocalVector<Connection>> ®ion_external_connections = map_iteration->external_region_connections;
|
||||
|
||||
HashMap<const NavBaseIteration2D *, LocalVector<LocalVector<Nav2D::Connection>>> &navbases_polygons_external_connections = map_iteration->navbases_polygons_external_connections;
|
||||
|
||||
// Find the compatible near edges.
|
||||
//
|
||||
// Note:
|
||||
// Considering that the edges must be compatible (for obvious reasons)
|
||||
// to be connected, create new polygons to remove that small gap is
|
||||
// not really useful and would result in wasteful computation during
|
||||
// connection, integration and path finding.
|
||||
performance_data.pm_edge_free_count = free_edges.size();
|
||||
|
||||
const real_t edge_connection_margin_squared = edge_connection_margin * edge_connection_margin;
|
||||
|
||||
for (uint32_t i = 0; i < free_edges.size(); i++) {
|
||||
const Connection &free_edge = free_edges[i];
|
||||
const Vector2 &edge_p1 = free_edge.pathway_start;
|
||||
const Vector2 &edge_p2 = free_edge.pathway_end;
|
||||
|
||||
for (uint32_t j = 0; j < free_edges.size(); j++) {
|
||||
const Connection &other_edge = free_edges[j];
|
||||
if (i == j || free_edge.polygon->owner == other_edge.polygon->owner) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const Vector2 &other_edge_p1 = other_edge.pathway_start;
|
||||
const Vector2 &other_edge_p2 = other_edge.pathway_end;
|
||||
|
||||
// Compute the projection of the opposite edge on the current one
|
||||
Vector2 edge_vector = edge_p2 - edge_p1;
|
||||
real_t projected_p1_ratio = edge_vector.dot(other_edge_p1 - edge_p1) / (edge_vector.length_squared());
|
||||
real_t projected_p2_ratio = edge_vector.dot(other_edge_p2 - edge_p1) / (edge_vector.length_squared());
|
||||
if ((projected_p1_ratio < 0.0 && projected_p2_ratio < 0.0) || (projected_p1_ratio > 1.0 && projected_p2_ratio > 1.0)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// Check if the two edges are close to each other enough and compute a pathway between the two regions.
|
||||
Vector2 self1 = edge_vector * CLAMP(projected_p1_ratio, 0.0, 1.0) + edge_p1;
|
||||
Vector2 other1;
|
||||
if (projected_p1_ratio >= 0.0 && projected_p1_ratio <= 1.0) {
|
||||
other1 = other_edge_p1;
|
||||
} else {
|
||||
other1 = other_edge_p1.lerp(other_edge_p2, (1.0 - projected_p1_ratio) / (projected_p2_ratio - projected_p1_ratio));
|
||||
}
|
||||
if (other1.distance_squared_to(self1) > edge_connection_margin_squared) {
|
||||
continue;
|
||||
}
|
||||
|
||||
Vector2 self2 = edge_vector * CLAMP(projected_p2_ratio, 0.0, 1.0) + edge_p1;
|
||||
Vector2 other2;
|
||||
if (projected_p2_ratio >= 0.0 && projected_p2_ratio <= 1.0) {
|
||||
other2 = other_edge_p2;
|
||||
} else {
|
||||
other2 = other_edge_p1.lerp(other_edge_p2, (0.0 - projected_p1_ratio) / (projected_p2_ratio - projected_p1_ratio));
|
||||
}
|
||||
if (other2.distance_squared_to(self2) > edge_connection_margin_squared) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// The edges can now be connected.
|
||||
Connection new_connection = other_edge;
|
||||
new_connection.pathway_start = (self1 + other1) / 2.0;
|
||||
new_connection.pathway_end = (self2 + other2) / 2.0;
|
||||
//free_edge.polygon->connections.push_back(new_connection);
|
||||
|
||||
// Add the connection to the region_connection map.
|
||||
region_external_connections[free_edge.polygon->owner].push_back(new_connection);
|
||||
navbases_polygons_external_connections[free_edge.polygon->owner][free_edge.polygon->id].push_back(new_connection);
|
||||
performance_data.pm_edge_connection_count += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void NavMapBuilder2D::_build_step_navlink_connections(NavMapIterationBuild2D &r_build) {
|
||||
NavMapIteration2D *map_iteration = r_build.map_iteration;
|
||||
|
||||
real_t link_connection_radius = r_build.link_connection_radius;
|
||||
|
||||
const LocalVector<Ref<NavLinkIteration2D>> &links = map_iteration->link_iterations;
|
||||
|
||||
int polygon_count = r_build.polygon_count;
|
||||
|
||||
real_t link_connection_radius_sqr = link_connection_radius * link_connection_radius;
|
||||
|
||||
HashMap<const NavBaseIteration2D *, LocalVector<LocalVector<Nav2D::Connection>>> &navbases_polygons_external_connections = map_iteration->navbases_polygons_external_connections;
|
||||
LocalVector<Nav2D::Polygon> &navlink_polygons = map_iteration->navlink_polygons;
|
||||
navlink_polygons.clear();
|
||||
navlink_polygons.resize(links.size());
|
||||
uint32_t navlink_index = 0;
|
||||
|
||||
// Search for polygons within range of a nav link.
|
||||
for (const Ref<NavLinkIteration2D> &link : links) {
|
||||
polygon_count++;
|
||||
Polygon &new_polygon = navlink_polygons[navlink_index++];
|
||||
|
||||
new_polygon.id = 0;
|
||||
new_polygon.owner = link.ptr();
|
||||
|
||||
const Vector2 link_start_pos = link->get_start_position();
|
||||
const Vector2 link_end_pos = link->get_end_position();
|
||||
|
||||
Polygon *closest_start_polygon = nullptr;
|
||||
real_t closest_start_sqr_dist = link_connection_radius_sqr;
|
||||
Vector2 closest_start_point;
|
||||
|
||||
Polygon *closest_end_polygon = nullptr;
|
||||
real_t closest_end_sqr_dist = link_connection_radius_sqr;
|
||||
Vector2 closest_end_point;
|
||||
|
||||
for (const Ref<NavRegionIteration2D> ®ion : map_iteration->region_iterations) {
|
||||
Rect2 region_bounds = region->get_bounds().grow(link_connection_radius);
|
||||
if (!region_bounds.has_point(link_start_pos) && !region_bounds.has_point(link_end_pos)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
for (Polygon &polyon : region->navmesh_polygons) {
|
||||
for (uint32_t point_id = 2; point_id < polyon.vertices.size(); point_id += 1) {
|
||||
const Triangle2 triangle(polyon.vertices[0], polyon.vertices[point_id - 1], polyon.vertices[point_id]);
|
||||
|
||||
{
|
||||
const Vector2 start_point = triangle.get_closest_point_to(link_start_pos);
|
||||
const real_t sqr_dist = start_point.distance_squared_to(link_start_pos);
|
||||
|
||||
// Pick the polygon that is within our radius and is closer than anything we've seen yet.
|
||||
if (sqr_dist < closest_start_sqr_dist) {
|
||||
closest_start_sqr_dist = sqr_dist;
|
||||
closest_start_point = start_point;
|
||||
closest_start_polygon = &polyon;
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
const Vector2 end_point = triangle.get_closest_point_to(link_end_pos);
|
||||
const real_t sqr_dist = end_point.distance_squared_to(link_end_pos);
|
||||
|
||||
// Pick the polygon that is within our radius and is closer than anything we've seen yet.
|
||||
if (sqr_dist < closest_end_sqr_dist) {
|
||||
closest_end_sqr_dist = sqr_dist;
|
||||
closest_end_point = end_point;
|
||||
closest_end_polygon = &polyon;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// If we have both a start and end point, then create a synthetic polygon to route through.
|
||||
if (closest_start_polygon && closest_end_polygon) {
|
||||
new_polygon.vertices.resize(4);
|
||||
|
||||
// Build a set of vertices that create a thin polygon going from the start to the end point.
|
||||
new_polygon.vertices[0] = closest_start_point;
|
||||
new_polygon.vertices[1] = closest_start_point;
|
||||
new_polygon.vertices[2] = closest_end_point;
|
||||
new_polygon.vertices[3] = closest_end_point;
|
||||
|
||||
// Setup connections to go forward in the link.
|
||||
{
|
||||
Connection entry_connection;
|
||||
entry_connection.polygon = &new_polygon;
|
||||
entry_connection.edge = -1;
|
||||
entry_connection.pathway_start = new_polygon.vertices[0];
|
||||
entry_connection.pathway_end = new_polygon.vertices[1];
|
||||
navbases_polygons_external_connections[closest_start_polygon->owner][closest_start_polygon->id].push_back(entry_connection);
|
||||
|
||||
Connection exit_connection;
|
||||
exit_connection.polygon = closest_end_polygon;
|
||||
exit_connection.edge = -1;
|
||||
exit_connection.pathway_start = new_polygon.vertices[2];
|
||||
exit_connection.pathway_end = new_polygon.vertices[3];
|
||||
navbases_polygons_external_connections[link.ptr()].push_back(LocalVector<Nav2D::Connection>());
|
||||
navbases_polygons_external_connections[link.ptr()][new_polygon.id].push_back(exit_connection);
|
||||
}
|
||||
|
||||
// If the link is bi-directional, create connections from the end to the start.
|
||||
if (link->is_bidirectional()) {
|
||||
Connection entry_connection;
|
||||
entry_connection.polygon = &new_polygon;
|
||||
entry_connection.edge = -1;
|
||||
entry_connection.pathway_start = new_polygon.vertices[2];
|
||||
entry_connection.pathway_end = new_polygon.vertices[3];
|
||||
navbases_polygons_external_connections[closest_end_polygon->owner][closest_end_polygon->id].push_back(entry_connection);
|
||||
|
||||
Connection exit_connection;
|
||||
exit_connection.polygon = closest_start_polygon;
|
||||
exit_connection.edge = -1;
|
||||
exit_connection.pathway_start = new_polygon.vertices[0];
|
||||
exit_connection.pathway_end = new_polygon.vertices[1];
|
||||
navbases_polygons_external_connections[link.ptr()].push_back(LocalVector<Nav2D::Connection>());
|
||||
navbases_polygons_external_connections[link.ptr()][new_polygon.id].push_back(exit_connection);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
r_build.polygon_count = polygon_count;
|
||||
}
|
||||
|
||||
void NavMapBuilder2D::_build_update_map_iteration(NavMapIterationBuild2D &r_build) {
|
||||
NavMapIteration2D *map_iteration = r_build.map_iteration;
|
||||
|
||||
map_iteration->navmesh_polygon_count = r_build.polygon_count;
|
||||
|
||||
uint32_t navmesh_polygon_count = r_build.polygon_count;
|
||||
uint32_t total_polygon_count = navmesh_polygon_count;
|
||||
|
||||
map_iteration->path_query_slots_mutex.lock();
|
||||
for (NavMeshQueries2D::PathQuerySlot &p_path_query_slot : map_iteration->path_query_slots) {
|
||||
p_path_query_slot.traversable_polys.clear();
|
||||
p_path_query_slot.traversable_polys.reserve(navmesh_polygon_count * 0.25);
|
||||
p_path_query_slot.path_corridor.clear();
|
||||
|
||||
p_path_query_slot.path_corridor.resize(total_polygon_count);
|
||||
|
||||
p_path_query_slot.poly_to_id.clear();
|
||||
p_path_query_slot.poly_to_id.reserve(total_polygon_count);
|
||||
|
||||
int polygon_id = 0;
|
||||
for (Ref<NavRegionIteration2D> ®ion : map_iteration->region_iterations) {
|
||||
for (const Polygon &polygon : region->navmesh_polygons) {
|
||||
p_path_query_slot.poly_to_id[&polygon] = polygon_id;
|
||||
polygon_id++;
|
||||
}
|
||||
}
|
||||
|
||||
for (const Polygon &polygon : map_iteration->navlink_polygons) {
|
||||
p_path_query_slot.poly_to_id[&polygon] = polygon_id;
|
||||
polygon_id++;
|
||||
}
|
||||
|
||||
DEV_ASSERT(p_path_query_slot.path_corridor.size() == p_path_query_slot.poly_to_id.size());
|
||||
}
|
||||
|
||||
map_iteration->path_query_slots_mutex.unlock();
|
||||
}
|
49
modules/navigation_2d/2d/nav_map_builder_2d.h
Normal file
49
modules/navigation_2d/2d/nav_map_builder_2d.h
Normal file
@@ -0,0 +1,49 @@
|
||||
/**************************************************************************/
|
||||
/* nav_map_builder_2d.h */
|
||||
/**************************************************************************/
|
||||
/* This file is part of: */
|
||||
/* GODOT ENGINE */
|
||||
/* https://godotengine.org */
|
||||
/**************************************************************************/
|
||||
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
||||
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
||||
/* */
|
||||
/* Permission is hereby granted, free of charge, to any person obtaining */
|
||||
/* a copy of this software and associated documentation files (the */
|
||||
/* "Software"), to deal in the Software without restriction, including */
|
||||
/* without limitation the rights to use, copy, modify, merge, publish, */
|
||||
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
||||
/* permit persons to whom the Software is furnished to do so, subject to */
|
||||
/* the following conditions: */
|
||||
/* */
|
||||
/* The above copyright notice and this permission notice shall be */
|
||||
/* included in all copies or substantial portions of the Software. */
|
||||
/* */
|
||||
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
||||
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
||||
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
|
||||
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
||||
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
||||
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
||||
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
||||
/**************************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "../nav_utils_2d.h"
|
||||
|
||||
struct NavMapIterationBuild2D;
|
||||
|
||||
class NavMapBuilder2D {
|
||||
static void _build_step_gather_region_polygons(NavMapIterationBuild2D &r_build);
|
||||
static void _build_step_find_edge_connection_pairs(NavMapIterationBuild2D &r_build);
|
||||
static void _build_step_merge_edge_connection_pairs(NavMapIterationBuild2D &r_build);
|
||||
static void _build_step_edge_connection_margin_connections(NavMapIterationBuild2D &r_build);
|
||||
static void _build_step_navlink_connections(NavMapIterationBuild2D &r_build);
|
||||
static void _build_update_map_iteration(NavMapIterationBuild2D &r_build);
|
||||
|
||||
public:
|
||||
static Nav2D::PointKey get_point_key(const Vector2 &p_pos, const Vector2 &p_cell_size);
|
||||
|
||||
static void build_navmap_iteration(NavMapIterationBuild2D &r_build);
|
||||
};
|
119
modules/navigation_2d/2d/nav_map_iteration_2d.h
Normal file
119
modules/navigation_2d/2d/nav_map_iteration_2d.h
Normal file
@@ -0,0 +1,119 @@
|
||||
/**************************************************************************/
|
||||
/* nav_map_iteration_2d.h */
|
||||
/**************************************************************************/
|
||||
/* This file is part of: */
|
||||
/* GODOT ENGINE */
|
||||
/* https://godotengine.org */
|
||||
/**************************************************************************/
|
||||
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
||||
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
||||
/* */
|
||||
/* Permission is hereby granted, free of charge, to any person obtaining */
|
||||
/* a copy of this software and associated documentation files (the */
|
||||
/* "Software"), to deal in the Software without restriction, including */
|
||||
/* without limitation the rights to use, copy, modify, merge, publish, */
|
||||
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
||||
/* permit persons to whom the Software is furnished to do so, subject to */
|
||||
/* the following conditions: */
|
||||
/* */
|
||||
/* The above copyright notice and this permission notice shall be */
|
||||
/* included in all copies or substantial portions of the Software. */
|
||||
/* */
|
||||
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
||||
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
||||
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
|
||||
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
||||
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
||||
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
||||
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
||||
/**************************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "../nav_rid_2d.h"
|
||||
#include "../nav_utils_2d.h"
|
||||
#include "nav_mesh_queries_2d.h"
|
||||
|
||||
#include "core/math/math_defs.h"
|
||||
#include "core/os/semaphore.h"
|
||||
|
||||
class NavLinkIteration2D;
|
||||
class NavRegion2D;
|
||||
class NavRegionIteration2D;
|
||||
struct NavMapIteration2D;
|
||||
|
||||
struct NavMapIterationBuild2D {
|
||||
Vector2 merge_rasterizer_cell_size;
|
||||
bool use_edge_connections = true;
|
||||
real_t edge_connection_margin;
|
||||
real_t link_connection_radius;
|
||||
Nav2D::PerformanceData performance_data;
|
||||
int polygon_count = 0;
|
||||
int free_edge_count = 0;
|
||||
|
||||
HashMap<Nav2D::EdgeKey, Nav2D::EdgeConnectionPair, Nav2D::EdgeKey> iter_connection_pairs_map;
|
||||
LocalVector<Nav2D::Connection> iter_free_edges;
|
||||
|
||||
NavMapIteration2D *map_iteration = nullptr;
|
||||
|
||||
int navmesh_polygon_count = 0;
|
||||
|
||||
void reset() {
|
||||
performance_data.reset();
|
||||
|
||||
iter_connection_pairs_map.clear();
|
||||
iter_free_edges.clear();
|
||||
polygon_count = 0;
|
||||
free_edge_count = 0;
|
||||
|
||||
navmesh_polygon_count = 0;
|
||||
}
|
||||
};
|
||||
|
||||
struct NavMapIteration2D {
|
||||
mutable SafeNumeric<uint32_t> users;
|
||||
RWLock rwlock;
|
||||
|
||||
LocalVector<Ref<NavRegionIteration2D>> region_iterations;
|
||||
LocalVector<Ref<NavLinkIteration2D>> link_iterations;
|
||||
|
||||
int navmesh_polygon_count = 0;
|
||||
|
||||
// The edge connections that the map builds on top with the edge connection margin.
|
||||
HashMap<const NavBaseIteration2D *, LocalVector<Nav2D::Connection>> external_region_connections;
|
||||
HashMap<const NavBaseIteration2D *, LocalVector<LocalVector<Nav2D::Connection>>> navbases_polygons_external_connections;
|
||||
|
||||
LocalVector<Nav2D::Polygon> navlink_polygons;
|
||||
|
||||
HashMap<NavRegion2D *, Ref<NavRegionIteration2D>> region_ptr_to_region_iteration;
|
||||
|
||||
LocalVector<NavMeshQueries2D::PathQuerySlot> path_query_slots;
|
||||
Mutex path_query_slots_mutex;
|
||||
Semaphore path_query_slots_semaphore;
|
||||
|
||||
void clear() {
|
||||
navmesh_polygon_count = 0;
|
||||
|
||||
region_iterations.clear();
|
||||
link_iterations.clear();
|
||||
external_region_connections.clear();
|
||||
navbases_polygons_external_connections.clear();
|
||||
navlink_polygons.clear();
|
||||
region_ptr_to_region_iteration.clear();
|
||||
}
|
||||
};
|
||||
|
||||
class NavMapIterationRead2D {
|
||||
const NavMapIteration2D &map_iteration;
|
||||
|
||||
public:
|
||||
_ALWAYS_INLINE_ NavMapIterationRead2D(const NavMapIteration2D &p_iteration) :
|
||||
map_iteration(p_iteration) {
|
||||
map_iteration.rwlock.read_lock();
|
||||
map_iteration.users.increment();
|
||||
}
|
||||
_ALWAYS_INLINE_ ~NavMapIterationRead2D() {
|
||||
map_iteration.users.decrement();
|
||||
map_iteration.rwlock.read_unlock();
|
||||
}
|
||||
};
|
528
modules/navigation_2d/2d/nav_mesh_generator_2d.cpp
Normal file
528
modules/navigation_2d/2d/nav_mesh_generator_2d.cpp
Normal file
@@ -0,0 +1,528 @@
|
||||
/**************************************************************************/
|
||||
/* nav_mesh_generator_2d.cpp */
|
||||
/**************************************************************************/
|
||||
/* This file is part of: */
|
||||
/* GODOT ENGINE */
|
||||
/* https://godotengine.org */
|
||||
/**************************************************************************/
|
||||
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
||||
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
||||
/* */
|
||||
/* Permission is hereby granted, free of charge, to any person obtaining */
|
||||
/* a copy of this software and associated documentation files (the */
|
||||
/* "Software"), to deal in the Software without restriction, including */
|
||||
/* without limitation the rights to use, copy, modify, merge, publish, */
|
||||
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
||||
/* permit persons to whom the Software is furnished to do so, subject to */
|
||||
/* the following conditions: */
|
||||
/* */
|
||||
/* The above copyright notice and this permission notice shall be */
|
||||
/* included in all copies or substantial portions of the Software. */
|
||||
/* */
|
||||
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
||||
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
||||
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
|
||||
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
||||
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
||||
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
||||
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
||||
/**************************************************************************/
|
||||
|
||||
#ifdef CLIPPER2_ENABLED
|
||||
|
||||
#include "nav_mesh_generator_2d.h"
|
||||
|
||||
#include "core/config/project_settings.h"
|
||||
#include "scene/resources/2d/navigation_mesh_source_geometry_data_2d.h"
|
||||
#include "scene/resources/2d/navigation_polygon.h"
|
||||
|
||||
#include "thirdparty/clipper2/include/clipper2/clipper.h"
|
||||
#include "thirdparty/misc/polypartition.h"
|
||||
|
||||
NavMeshGenerator2D *NavMeshGenerator2D::singleton = nullptr;
|
||||
Mutex NavMeshGenerator2D::baking_navmesh_mutex;
|
||||
Mutex NavMeshGenerator2D::generator_task_mutex;
|
||||
RWLock NavMeshGenerator2D::generator_parsers_rwlock;
|
||||
bool NavMeshGenerator2D::use_threads = true;
|
||||
bool NavMeshGenerator2D::baking_use_multiple_threads = true;
|
||||
bool NavMeshGenerator2D::baking_use_high_priority_threads = true;
|
||||
HashSet<Ref<NavigationPolygon>> NavMeshGenerator2D::baking_navmeshes;
|
||||
HashMap<WorkerThreadPool::TaskID, NavMeshGenerator2D::NavMeshGeneratorTask2D *> NavMeshGenerator2D::generator_tasks;
|
||||
LocalVector<NavMeshGeometryParser2D *> NavMeshGenerator2D::generator_parsers;
|
||||
|
||||
NavMeshGenerator2D *NavMeshGenerator2D::get_singleton() {
|
||||
return singleton;
|
||||
}
|
||||
|
||||
NavMeshGenerator2D::NavMeshGenerator2D() {
|
||||
ERR_FAIL_COND(singleton != nullptr);
|
||||
singleton = this;
|
||||
|
||||
baking_use_multiple_threads = GLOBAL_GET("navigation/baking/thread_model/baking_use_multiple_threads");
|
||||
baking_use_high_priority_threads = GLOBAL_GET("navigation/baking/thread_model/baking_use_high_priority_threads");
|
||||
|
||||
// Using threads might cause problems on certain exports or with the Editor on certain devices.
|
||||
// This is the main switch to turn threaded navmesh baking off should the need arise.
|
||||
use_threads = baking_use_multiple_threads;
|
||||
}
|
||||
|
||||
NavMeshGenerator2D::~NavMeshGenerator2D() {
|
||||
cleanup();
|
||||
}
|
||||
|
||||
void NavMeshGenerator2D::sync() {
|
||||
if (generator_tasks.is_empty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
MutexLock baking_navmesh_lock(baking_navmesh_mutex);
|
||||
{
|
||||
MutexLock generator_task_lock(generator_task_mutex);
|
||||
|
||||
LocalVector<WorkerThreadPool::TaskID> finished_task_ids;
|
||||
|
||||
for (KeyValue<WorkerThreadPool::TaskID, NavMeshGeneratorTask2D *> &E : generator_tasks) {
|
||||
if (WorkerThreadPool::get_singleton()->is_task_completed(E.key)) {
|
||||
WorkerThreadPool::get_singleton()->wait_for_task_completion(E.key);
|
||||
finished_task_ids.push_back(E.key);
|
||||
|
||||
NavMeshGeneratorTask2D *generator_task = E.value;
|
||||
DEV_ASSERT(generator_task->status == NavMeshGeneratorTask2D::TaskStatus::BAKING_FINISHED);
|
||||
|
||||
baking_navmeshes.erase(generator_task->navigation_mesh);
|
||||
if (generator_task->callback.is_valid()) {
|
||||
generator_emit_callback(generator_task->callback);
|
||||
}
|
||||
generator_task->navigation_mesh->emit_changed();
|
||||
memdelete(generator_task);
|
||||
}
|
||||
}
|
||||
|
||||
for (WorkerThreadPool::TaskID finished_task_id : finished_task_ids) {
|
||||
generator_tasks.erase(finished_task_id);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void NavMeshGenerator2D::cleanup() {
|
||||
MutexLock baking_navmesh_lock(baking_navmesh_mutex);
|
||||
{
|
||||
MutexLock generator_task_lock(generator_task_mutex);
|
||||
|
||||
baking_navmeshes.clear();
|
||||
|
||||
for (KeyValue<WorkerThreadPool::TaskID, NavMeshGeneratorTask2D *> &E : generator_tasks) {
|
||||
WorkerThreadPool::get_singleton()->wait_for_task_completion(E.key);
|
||||
NavMeshGeneratorTask2D *generator_task = E.value;
|
||||
memdelete(generator_task);
|
||||
}
|
||||
generator_tasks.clear();
|
||||
|
||||
generator_parsers_rwlock.write_lock();
|
||||
generator_parsers.clear();
|
||||
generator_parsers_rwlock.write_unlock();
|
||||
}
|
||||
}
|
||||
|
||||
void NavMeshGenerator2D::finish() {
|
||||
cleanup();
|
||||
}
|
||||
|
||||
void NavMeshGenerator2D::parse_source_geometry_data(Ref<NavigationPolygon> p_navigation_mesh, Ref<NavigationMeshSourceGeometryData2D> p_source_geometry_data, Node *p_root_node, const Callable &p_callback) {
|
||||
ERR_FAIL_COND(!Thread::is_main_thread());
|
||||
ERR_FAIL_COND(p_navigation_mesh.is_null());
|
||||
ERR_FAIL_NULL(p_root_node);
|
||||
ERR_FAIL_COND(!p_root_node->is_inside_tree());
|
||||
ERR_FAIL_COND(p_source_geometry_data.is_null());
|
||||
|
||||
generator_parse_source_geometry_data(p_navigation_mesh, p_source_geometry_data, p_root_node);
|
||||
|
||||
if (p_callback.is_valid()) {
|
||||
generator_emit_callback(p_callback);
|
||||
}
|
||||
}
|
||||
|
||||
void NavMeshGenerator2D::bake_from_source_geometry_data(Ref<NavigationPolygon> p_navigation_mesh, Ref<NavigationMeshSourceGeometryData2D> p_source_geometry_data, const Callable &p_callback) {
|
||||
ERR_FAIL_COND(p_navigation_mesh.is_null());
|
||||
ERR_FAIL_COND(p_source_geometry_data.is_null());
|
||||
|
||||
if (p_navigation_mesh->get_outline_count() == 0 && !p_source_geometry_data->has_data()) {
|
||||
p_navigation_mesh->clear();
|
||||
if (p_callback.is_valid()) {
|
||||
generator_emit_callback(p_callback);
|
||||
}
|
||||
p_navigation_mesh->emit_changed();
|
||||
return;
|
||||
}
|
||||
|
||||
if (is_baking(p_navigation_mesh)) {
|
||||
ERR_FAIL_MSG("NavigationPolygon is already baking. Wait for current bake to finish.");
|
||||
}
|
||||
baking_navmesh_mutex.lock();
|
||||
baking_navmeshes.insert(p_navigation_mesh);
|
||||
baking_navmesh_mutex.unlock();
|
||||
|
||||
generator_bake_from_source_geometry_data(p_navigation_mesh, p_source_geometry_data);
|
||||
|
||||
baking_navmesh_mutex.lock();
|
||||
baking_navmeshes.erase(p_navigation_mesh);
|
||||
baking_navmesh_mutex.unlock();
|
||||
|
||||
if (p_callback.is_valid()) {
|
||||
generator_emit_callback(p_callback);
|
||||
}
|
||||
|
||||
p_navigation_mesh->emit_changed();
|
||||
}
|
||||
|
||||
void NavMeshGenerator2D::bake_from_source_geometry_data_async(Ref<NavigationPolygon> p_navigation_mesh, Ref<NavigationMeshSourceGeometryData2D> p_source_geometry_data, const Callable &p_callback) {
|
||||
ERR_FAIL_COND(p_navigation_mesh.is_null());
|
||||
ERR_FAIL_COND(p_source_geometry_data.is_null());
|
||||
|
||||
if (p_navigation_mesh->get_outline_count() == 0 && !p_source_geometry_data->has_data()) {
|
||||
p_navigation_mesh->clear();
|
||||
if (p_callback.is_valid()) {
|
||||
generator_emit_callback(p_callback);
|
||||
}
|
||||
p_navigation_mesh->emit_changed();
|
||||
return;
|
||||
}
|
||||
|
||||
if (!use_threads) {
|
||||
bake_from_source_geometry_data(p_navigation_mesh, p_source_geometry_data, p_callback);
|
||||
return;
|
||||
}
|
||||
|
||||
if (is_baking(p_navigation_mesh)) {
|
||||
ERR_FAIL_MSG("NavigationPolygon is already baking. Wait for current bake to finish.");
|
||||
}
|
||||
baking_navmesh_mutex.lock();
|
||||
baking_navmeshes.insert(p_navigation_mesh);
|
||||
baking_navmesh_mutex.unlock();
|
||||
|
||||
MutexLock generator_task_lock(generator_task_mutex);
|
||||
NavMeshGeneratorTask2D *generator_task = memnew(NavMeshGeneratorTask2D);
|
||||
generator_task->navigation_mesh = p_navigation_mesh;
|
||||
generator_task->source_geometry_data = p_source_geometry_data;
|
||||
generator_task->callback = p_callback;
|
||||
generator_task->status = NavMeshGeneratorTask2D::TaskStatus::BAKING_STARTED;
|
||||
generator_task->thread_task_id = WorkerThreadPool::get_singleton()->add_native_task(&NavMeshGenerator2D::generator_thread_bake, generator_task, NavMeshGenerator2D::baking_use_high_priority_threads, "NavMeshGeneratorBake2D");
|
||||
generator_tasks.insert(generator_task->thread_task_id, generator_task);
|
||||
}
|
||||
|
||||
bool NavMeshGenerator2D::is_baking(Ref<NavigationPolygon> p_navigation_polygon) {
|
||||
MutexLock baking_navmesh_lock(baking_navmesh_mutex);
|
||||
return baking_navmeshes.has(p_navigation_polygon);
|
||||
}
|
||||
|
||||
void NavMeshGenerator2D::generator_thread_bake(void *p_arg) {
|
||||
NavMeshGeneratorTask2D *generator_task = static_cast<NavMeshGeneratorTask2D *>(p_arg);
|
||||
|
||||
generator_bake_from_source_geometry_data(generator_task->navigation_mesh, generator_task->source_geometry_data);
|
||||
|
||||
generator_task->status = NavMeshGeneratorTask2D::TaskStatus::BAKING_FINISHED;
|
||||
}
|
||||
|
||||
void NavMeshGenerator2D::generator_parse_geometry_node(Ref<NavigationPolygon> p_navigation_mesh, Ref<NavigationMeshSourceGeometryData2D> p_source_geometry_data, Node *p_node, bool p_recurse_children) {
|
||||
generator_parsers_rwlock.read_lock();
|
||||
for (const NavMeshGeometryParser2D *parser : generator_parsers) {
|
||||
if (!parser->callback.is_valid()) {
|
||||
continue;
|
||||
}
|
||||
parser->callback.call(p_navigation_mesh, p_source_geometry_data, p_node);
|
||||
}
|
||||
generator_parsers_rwlock.read_unlock();
|
||||
|
||||
if (p_recurse_children) {
|
||||
for (int i = 0; i < p_node->get_child_count(); i++) {
|
||||
generator_parse_geometry_node(p_navigation_mesh, p_source_geometry_data, p_node->get_child(i), p_recurse_children);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void NavMeshGenerator2D::set_generator_parsers(LocalVector<NavMeshGeometryParser2D *> p_parsers) {
|
||||
RWLockWrite write_lock(generator_parsers_rwlock);
|
||||
generator_parsers = p_parsers;
|
||||
}
|
||||
|
||||
void NavMeshGenerator2D::generator_parse_source_geometry_data(Ref<NavigationPolygon> p_navigation_mesh, Ref<NavigationMeshSourceGeometryData2D> p_source_geometry_data, Node *p_root_node) {
|
||||
List<Node *> parse_nodes;
|
||||
|
||||
if (p_navigation_mesh->get_source_geometry_mode() == NavigationPolygon::SOURCE_GEOMETRY_ROOT_NODE_CHILDREN) {
|
||||
parse_nodes.push_back(p_root_node);
|
||||
} else {
|
||||
p_root_node->get_tree()->get_nodes_in_group(p_navigation_mesh->get_source_geometry_group_name(), &parse_nodes);
|
||||
}
|
||||
|
||||
Transform2D root_node_transform = Transform2D();
|
||||
if (Object::cast_to<Node2D>(p_root_node)) {
|
||||
root_node_transform = Object::cast_to<Node2D>(p_root_node)->get_global_transform().affine_inverse();
|
||||
}
|
||||
|
||||
p_source_geometry_data->clear();
|
||||
p_source_geometry_data->root_node_transform = root_node_transform;
|
||||
|
||||
bool recurse_children = p_navigation_mesh->get_source_geometry_mode() != NavigationPolygon::SOURCE_GEOMETRY_GROUPS_EXPLICIT;
|
||||
|
||||
for (Node *E : parse_nodes) {
|
||||
generator_parse_geometry_node(p_navigation_mesh, p_source_geometry_data, E, recurse_children);
|
||||
}
|
||||
}
|
||||
|
||||
static void generator_recursive_process_polytree_items(List<TPPLPoly> &p_tppl_in_polygon, const Clipper2Lib::PolyPathD *p_polypath_item) {
|
||||
using namespace Clipper2Lib;
|
||||
|
||||
TPPLPoly tp;
|
||||
int size = p_polypath_item->Polygon().size();
|
||||
tp.Init(size);
|
||||
|
||||
int j = 0;
|
||||
for (const PointD &polypath_point : p_polypath_item->Polygon()) {
|
||||
tp[j] = Vector2(static_cast<real_t>(polypath_point.x), static_cast<real_t>(polypath_point.y));
|
||||
++j;
|
||||
}
|
||||
|
||||
if (p_polypath_item->IsHole()) {
|
||||
tp.SetOrientation(TPPL_ORIENTATION_CW);
|
||||
tp.SetHole(true);
|
||||
} else {
|
||||
tp.SetOrientation(TPPL_ORIENTATION_CCW);
|
||||
}
|
||||
p_tppl_in_polygon.push_back(tp);
|
||||
|
||||
for (size_t i = 0; i < p_polypath_item->Count(); i++) {
|
||||
const PolyPathD *polypath_item = p_polypath_item->Child(i);
|
||||
generator_recursive_process_polytree_items(p_tppl_in_polygon, polypath_item);
|
||||
}
|
||||
}
|
||||
|
||||
bool NavMeshGenerator2D::generator_emit_callback(const Callable &p_callback) {
|
||||
ERR_FAIL_COND_V(!p_callback.is_valid(), false);
|
||||
|
||||
Callable::CallError ce;
|
||||
Variant result;
|
||||
p_callback.callp(nullptr, 0, result, ce);
|
||||
|
||||
return ce.error == Callable::CallError::CALL_OK;
|
||||
}
|
||||
|
||||
void NavMeshGenerator2D::generator_bake_from_source_geometry_data(Ref<NavigationPolygon> p_navigation_mesh, Ref<NavigationMeshSourceGeometryData2D> p_source_geometry_data) {
|
||||
if (p_navigation_mesh.is_null() || p_source_geometry_data.is_null()) {
|
||||
return;
|
||||
}
|
||||
|
||||
using namespace Clipper2Lib;
|
||||
PathsD traversable_polygon_paths;
|
||||
PathsD obstruction_polygon_paths;
|
||||
bool empty_projected_obstructions = true;
|
||||
{
|
||||
RWLockRead read_lock(p_source_geometry_data->geometry_rwlock);
|
||||
|
||||
const Vector<Vector<Vector2>> &traversable_outlines = p_source_geometry_data->traversable_outlines;
|
||||
int outline_count = p_navigation_mesh->get_outline_count();
|
||||
|
||||
if (outline_count == 0 && (!p_source_geometry_data->has_data() || (traversable_outlines.is_empty()))) {
|
||||
return;
|
||||
}
|
||||
|
||||
const Vector<Vector<Vector2>> &obstruction_outlines = p_source_geometry_data->obstruction_outlines;
|
||||
const Vector<NavigationMeshSourceGeometryData2D::ProjectedObstruction> &projected_obstructions = p_source_geometry_data->_projected_obstructions;
|
||||
|
||||
traversable_polygon_paths.reserve(outline_count + traversable_outlines.size());
|
||||
obstruction_polygon_paths.reserve(obstruction_outlines.size());
|
||||
|
||||
for (int i = 0; i < outline_count; i++) {
|
||||
const Vector<Vector2> &traversable_outline = p_navigation_mesh->get_outline(i);
|
||||
PathD subject_path;
|
||||
subject_path.reserve(traversable_outline.size());
|
||||
for (const Vector2 &traversable_point : traversable_outline) {
|
||||
subject_path.emplace_back(traversable_point.x, traversable_point.y);
|
||||
}
|
||||
traversable_polygon_paths.push_back(std::move(subject_path));
|
||||
}
|
||||
|
||||
for (const Vector<Vector2> &traversable_outline : traversable_outlines) {
|
||||
PathD subject_path;
|
||||
subject_path.reserve(traversable_outline.size());
|
||||
for (const Vector2 &traversable_point : traversable_outline) {
|
||||
subject_path.emplace_back(traversable_point.x, traversable_point.y);
|
||||
}
|
||||
traversable_polygon_paths.push_back(std::move(subject_path));
|
||||
}
|
||||
|
||||
empty_projected_obstructions = projected_obstructions.is_empty();
|
||||
if (!empty_projected_obstructions) {
|
||||
for (const NavigationMeshSourceGeometryData2D::ProjectedObstruction &projected_obstruction : projected_obstructions) {
|
||||
if (projected_obstruction.carve) {
|
||||
continue;
|
||||
}
|
||||
if (projected_obstruction.vertices.is_empty() || projected_obstruction.vertices.size() % 2 != 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
PathD clip_path;
|
||||
clip_path.reserve(projected_obstruction.vertices.size() / 2);
|
||||
for (int i = 0; i < projected_obstruction.vertices.size() / 2; i++) {
|
||||
clip_path.emplace_back(projected_obstruction.vertices[i * 2], projected_obstruction.vertices[i * 2 + 1]);
|
||||
}
|
||||
if (!IsPositive(clip_path)) {
|
||||
std::reverse(clip_path.begin(), clip_path.end());
|
||||
}
|
||||
obstruction_polygon_paths.push_back(std::move(clip_path));
|
||||
}
|
||||
}
|
||||
|
||||
for (const Vector<Vector2> &obstruction_outline : obstruction_outlines) {
|
||||
PathD clip_path;
|
||||
clip_path.reserve(obstruction_outline.size());
|
||||
for (const Vector2 &obstruction_point : obstruction_outline) {
|
||||
clip_path.emplace_back(obstruction_point.x, obstruction_point.y);
|
||||
}
|
||||
obstruction_polygon_paths.push_back(std::move(clip_path));
|
||||
}
|
||||
}
|
||||
|
||||
Rect2 baking_rect = p_navigation_mesh->get_baking_rect();
|
||||
if (baking_rect.has_area()) {
|
||||
Vector2 baking_rect_offset = p_navigation_mesh->get_baking_rect_offset();
|
||||
|
||||
const int rect_begin_x = baking_rect.position[0] + baking_rect_offset.x;
|
||||
const int rect_begin_y = baking_rect.position[1] + baking_rect_offset.y;
|
||||
const int rect_end_x = baking_rect.position[0] + baking_rect.size[0] + baking_rect_offset.x;
|
||||
const int rect_end_y = baking_rect.position[1] + baking_rect.size[1] + baking_rect_offset.y;
|
||||
|
||||
RectD clipper_rect = RectD(rect_begin_x, rect_begin_y, rect_end_x, rect_end_y);
|
||||
|
||||
traversable_polygon_paths = RectClip(clipper_rect, traversable_polygon_paths);
|
||||
obstruction_polygon_paths = RectClip(clipper_rect, obstruction_polygon_paths);
|
||||
}
|
||||
|
||||
// first merge all traversable polygons according to user specified fill rule
|
||||
PathsD dummy_clip_path;
|
||||
traversable_polygon_paths = Union(traversable_polygon_paths, dummy_clip_path, FillRule::NonZero);
|
||||
// merge all obstruction polygons, don't allow holes for what is considered "solid" 2D geometry
|
||||
obstruction_polygon_paths = Union(obstruction_polygon_paths, dummy_clip_path, FillRule::NonZero);
|
||||
|
||||
PathsD path_solution = Difference(traversable_polygon_paths, obstruction_polygon_paths, FillRule::NonZero);
|
||||
|
||||
real_t agent_radius_offset = p_navigation_mesh->get_agent_radius();
|
||||
if (agent_radius_offset > 0.0) {
|
||||
path_solution = InflatePaths(path_solution, -agent_radius_offset, JoinType::Miter, EndType::Polygon);
|
||||
}
|
||||
|
||||
// Apply obstructions that are not affected by agent radius, the ones with carve enabled.
|
||||
if (!empty_projected_obstructions) {
|
||||
RWLockRead read_lock(p_source_geometry_data->geometry_rwlock);
|
||||
const Vector<NavigationMeshSourceGeometryData2D::ProjectedObstruction> &projected_obstructions = p_source_geometry_data->_projected_obstructions;
|
||||
obstruction_polygon_paths.resize(0);
|
||||
for (const NavigationMeshSourceGeometryData2D::ProjectedObstruction &projected_obstruction : projected_obstructions) {
|
||||
if (!projected_obstruction.carve) {
|
||||
continue;
|
||||
}
|
||||
if (projected_obstruction.vertices.is_empty() || projected_obstruction.vertices.size() % 2 != 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
PathD clip_path;
|
||||
clip_path.reserve(projected_obstruction.vertices.size() / 2);
|
||||
for (int i = 0; i < projected_obstruction.vertices.size() / 2; i++) {
|
||||
clip_path.emplace_back(projected_obstruction.vertices[i * 2], projected_obstruction.vertices[i * 2 + 1]);
|
||||
}
|
||||
if (!IsPositive(clip_path)) {
|
||||
std::reverse(clip_path.begin(), clip_path.end());
|
||||
}
|
||||
obstruction_polygon_paths.push_back(std::move(clip_path));
|
||||
}
|
||||
if (obstruction_polygon_paths.size() > 0) {
|
||||
path_solution = Difference(path_solution, obstruction_polygon_paths, FillRule::NonZero);
|
||||
}
|
||||
}
|
||||
|
||||
//path_solution = RamerDouglasPeucker(path_solution, 0.025); //
|
||||
|
||||
real_t border_size = p_navigation_mesh->get_border_size();
|
||||
if (baking_rect.has_area() && border_size > 0.0) {
|
||||
Vector2 baking_rect_offset = p_navigation_mesh->get_baking_rect_offset();
|
||||
|
||||
const int rect_begin_x = baking_rect.position[0] + baking_rect_offset.x + border_size;
|
||||
const int rect_begin_y = baking_rect.position[1] + baking_rect_offset.y + border_size;
|
||||
const int rect_end_x = baking_rect.position[0] + baking_rect.size[0] + baking_rect_offset.x - border_size;
|
||||
const int rect_end_y = baking_rect.position[1] + baking_rect.size[1] + baking_rect_offset.y - border_size;
|
||||
|
||||
RectD clipper_rect = RectD(rect_begin_x, rect_begin_y, rect_end_x, rect_end_y);
|
||||
|
||||
path_solution = RectClip(clipper_rect, path_solution);
|
||||
}
|
||||
|
||||
if (path_solution.size() == 0) {
|
||||
p_navigation_mesh->clear();
|
||||
return;
|
||||
}
|
||||
|
||||
ClipType clipper_cliptype = ClipType::Union;
|
||||
|
||||
List<TPPLPoly> tppl_in_polygon, tppl_out_polygon;
|
||||
|
||||
PolyTreeD polytree;
|
||||
ClipperD clipper_D;
|
||||
|
||||
clipper_D.AddSubject(path_solution);
|
||||
clipper_D.Execute(clipper_cliptype, FillRule::NonZero, polytree);
|
||||
|
||||
for (size_t i = 0; i < polytree.Count(); i++) {
|
||||
const PolyPathD *polypath_item = polytree[i];
|
||||
generator_recursive_process_polytree_items(tppl_in_polygon, polypath_item);
|
||||
}
|
||||
|
||||
TPPLPartition tpart;
|
||||
|
||||
NavigationPolygon::SamplePartitionType sample_partition_type = p_navigation_mesh->get_sample_partition_type();
|
||||
|
||||
switch (sample_partition_type) {
|
||||
case NavigationPolygon::SamplePartitionType::SAMPLE_PARTITION_CONVEX_PARTITION:
|
||||
if (tpart.ConvexPartition_HM(&tppl_in_polygon, &tppl_out_polygon) == 0) {
|
||||
ERR_PRINT("NavigationPolygon polygon convex partition failed. Unable to create a valid navigation mesh polygon layout from provided source geometry.");
|
||||
p_navigation_mesh->set_vertices(Vector<Vector2>());
|
||||
p_navigation_mesh->clear_polygons();
|
||||
return;
|
||||
}
|
||||
break;
|
||||
case NavigationPolygon::SamplePartitionType::SAMPLE_PARTITION_TRIANGULATE:
|
||||
if (tpart.Triangulate_EC(&tppl_in_polygon, &tppl_out_polygon) == 0) {
|
||||
ERR_PRINT("NavigationPolygon polygon triangulation failed. Unable to create a valid navigation mesh polygon layout from provided source geometry.");
|
||||
p_navigation_mesh->set_vertices(Vector<Vector2>());
|
||||
p_navigation_mesh->clear_polygons();
|
||||
return;
|
||||
}
|
||||
break;
|
||||
default: {
|
||||
ERR_PRINT("NavigationPolygon polygon partitioning failed. Unrecognized partition type.");
|
||||
p_navigation_mesh->set_vertices(Vector<Vector2>());
|
||||
p_navigation_mesh->clear_polygons();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
Vector<Vector2> new_vertices;
|
||||
Vector<Vector<int>> new_polygons;
|
||||
|
||||
HashMap<Vector2, int> points;
|
||||
for (const TPPLPoly &tp : tppl_out_polygon) {
|
||||
Vector<int> new_polygon;
|
||||
|
||||
for (int64_t i = 0; i < tp.GetNumPoints(); i++) {
|
||||
HashMap<Vector2, int>::Iterator E = points.find(tp[i]);
|
||||
if (!E) {
|
||||
E = points.insert(tp[i], new_vertices.size());
|
||||
new_vertices.push_back(tp[i]);
|
||||
}
|
||||
new_polygon.push_back(E->value);
|
||||
}
|
||||
|
||||
new_polygons.push_back(new_polygon);
|
||||
}
|
||||
|
||||
p_navigation_mesh->set_data(new_vertices, new_polygons);
|
||||
}
|
||||
|
||||
#endif // CLIPPER2_ENABLED
|
103
modules/navigation_2d/2d/nav_mesh_generator_2d.h
Normal file
103
modules/navigation_2d/2d/nav_mesh_generator_2d.h
Normal file
@@ -0,0 +1,103 @@
|
||||
/**************************************************************************/
|
||||
/* nav_mesh_generator_2d.h */
|
||||
/**************************************************************************/
|
||||
/* This file is part of: */
|
||||
/* GODOT ENGINE */
|
||||
/* https://godotengine.org */
|
||||
/**************************************************************************/
|
||||
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
||||
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
||||
/* */
|
||||
/* Permission is hereby granted, free of charge, to any person obtaining */
|
||||
/* a copy of this software and associated documentation files (the */
|
||||
/* "Software"), to deal in the Software without restriction, including */
|
||||
/* without limitation the rights to use, copy, modify, merge, publish, */
|
||||
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
||||
/* permit persons to whom the Software is furnished to do so, subject to */
|
||||
/* the following conditions: */
|
||||
/* */
|
||||
/* The above copyright notice and this permission notice shall be */
|
||||
/* included in all copies or substantial portions of the Software. */
|
||||
/* */
|
||||
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
||||
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
||||
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
|
||||
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
||||
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
||||
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
||||
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
||||
/**************************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
#ifdef CLIPPER2_ENABLED
|
||||
|
||||
#include "core/object/class_db.h"
|
||||
#include "core/object/worker_thread_pool.h"
|
||||
#include "core/templates/rid_owner.h"
|
||||
#include "servers/navigation_server_2d.h"
|
||||
|
||||
class Node;
|
||||
class NavigationPolygon;
|
||||
class NavigationMeshSourceGeometryData2D;
|
||||
|
||||
class NavMeshGenerator2D : public Object {
|
||||
static NavMeshGenerator2D *singleton;
|
||||
|
||||
static Mutex baking_navmesh_mutex;
|
||||
static Mutex generator_task_mutex;
|
||||
|
||||
static RWLock generator_parsers_rwlock;
|
||||
static LocalVector<NavMeshGeometryParser2D *> generator_parsers;
|
||||
|
||||
static bool use_threads;
|
||||
static bool baking_use_multiple_threads;
|
||||
static bool baking_use_high_priority_threads;
|
||||
|
||||
struct NavMeshGeneratorTask2D {
|
||||
enum TaskStatus {
|
||||
BAKING_STARTED,
|
||||
BAKING_FINISHED,
|
||||
BAKING_FAILED,
|
||||
CALLBACK_DISPATCHED,
|
||||
CALLBACK_FAILED,
|
||||
};
|
||||
|
||||
Ref<NavigationPolygon> navigation_mesh;
|
||||
Ref<NavigationMeshSourceGeometryData2D> source_geometry_data;
|
||||
Callable callback;
|
||||
WorkerThreadPool::TaskID thread_task_id = WorkerThreadPool::INVALID_TASK_ID;
|
||||
NavMeshGeneratorTask2D::TaskStatus status = NavMeshGeneratorTask2D::TaskStatus::BAKING_STARTED;
|
||||
};
|
||||
|
||||
static HashMap<WorkerThreadPool::TaskID, NavMeshGeneratorTask2D *> generator_tasks;
|
||||
|
||||
static void generator_thread_bake(void *p_arg);
|
||||
|
||||
static HashSet<Ref<NavigationPolygon>> baking_navmeshes;
|
||||
|
||||
static void generator_parse_geometry_node(Ref<NavigationPolygon> p_navigation_mesh, Ref<NavigationMeshSourceGeometryData2D> p_source_geometry_data, Node *p_node, bool p_recurse_children);
|
||||
static void generator_parse_source_geometry_data(Ref<NavigationPolygon> p_navigation_mesh, Ref<NavigationMeshSourceGeometryData2D> p_source_geometry_data, Node *p_root_node);
|
||||
static void generator_bake_from_source_geometry_data(Ref<NavigationPolygon> p_navigation_mesh, Ref<NavigationMeshSourceGeometryData2D> p_source_geometry_data);
|
||||
|
||||
static bool generator_emit_callback(const Callable &p_callback);
|
||||
|
||||
public:
|
||||
static NavMeshGenerator2D *get_singleton();
|
||||
|
||||
static void sync();
|
||||
static void cleanup();
|
||||
static void finish();
|
||||
|
||||
static void set_generator_parsers(LocalVector<NavMeshGeometryParser2D *> p_parsers);
|
||||
|
||||
static void parse_source_geometry_data(Ref<NavigationPolygon> p_navigation_mesh, Ref<NavigationMeshSourceGeometryData2D> p_source_geometry_data, Node *p_root_node, const Callable &p_callback = Callable());
|
||||
static void bake_from_source_geometry_data(Ref<NavigationPolygon> p_navigation_mesh, Ref<NavigationMeshSourceGeometryData2D> p_source_geometry_data, const Callable &p_callback = Callable());
|
||||
static void bake_from_source_geometry_data_async(Ref<NavigationPolygon> p_navigation_mesh, Ref<NavigationMeshSourceGeometryData2D> p_source_geometry_data, const Callable &p_callback = Callable());
|
||||
static bool is_baking(Ref<NavigationPolygon> p_navigation_polygon);
|
||||
|
||||
NavMeshGenerator2D();
|
||||
~NavMeshGenerator2D();
|
||||
};
|
||||
|
||||
#endif // CLIPPER2_ENABLED
|
1226
modules/navigation_2d/2d/nav_mesh_queries_2d.cpp
Normal file
1226
modules/navigation_2d/2d/nav_mesh_queries_2d.cpp
Normal file
File diff suppressed because it is too large
Load Diff
157
modules/navigation_2d/2d/nav_mesh_queries_2d.h
Normal file
157
modules/navigation_2d/2d/nav_mesh_queries_2d.h
Normal file
@@ -0,0 +1,157 @@
|
||||
/**************************************************************************/
|
||||
/* nav_mesh_queries_2d.h */
|
||||
/**************************************************************************/
|
||||
/* This file is part of: */
|
||||
/* GODOT ENGINE */
|
||||
/* https://godotengine.org */
|
||||
/**************************************************************************/
|
||||
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
||||
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
||||
/* */
|
||||
/* Permission is hereby granted, free of charge, to any person obtaining */
|
||||
/* a copy of this software and associated documentation files (the */
|
||||
/* "Software"), to deal in the Software without restriction, including */
|
||||
/* without limitation the rights to use, copy, modify, merge, publish, */
|
||||
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
||||
/* permit persons to whom the Software is furnished to do so, subject to */
|
||||
/* the following conditions: */
|
||||
/* */
|
||||
/* The above copyright notice and this permission notice shall be */
|
||||
/* included in all copies or substantial portions of the Software. */
|
||||
/* */
|
||||
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
||||
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
||||
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
|
||||
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
||||
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
||||
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
||||
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
||||
/**************************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "../nav_utils_2d.h"
|
||||
|
||||
#include "core/templates/a_hash_map.h"
|
||||
|
||||
#include "servers/navigation/navigation_globals.h"
|
||||
#include "servers/navigation/navigation_path_query_parameters_2d.h"
|
||||
#include "servers/navigation/navigation_path_query_result_2d.h"
|
||||
#include "servers/navigation/navigation_utilities.h"
|
||||
|
||||
using namespace NavigationUtilities;
|
||||
|
||||
class NavMap2D;
|
||||
struct NavMapIteration2D;
|
||||
|
||||
class NavMeshQueries2D {
|
||||
public:
|
||||
struct PathQuerySlot {
|
||||
LocalVector<Nav2D::NavigationPoly> path_corridor;
|
||||
Heap<Nav2D::NavigationPoly *, Nav2D::NavPolyTravelCostGreaterThan, Nav2D::NavPolyHeapIndexer> traversable_polys;
|
||||
bool in_use = false;
|
||||
uint32_t slot_index = 0;
|
||||
AHashMap<const Nav2D::Polygon *, uint32_t> poly_to_id;
|
||||
};
|
||||
|
||||
struct NavMeshPathQueryTask2D {
|
||||
enum TaskStatus {
|
||||
QUERY_STARTED,
|
||||
QUERY_FINISHED,
|
||||
QUERY_FAILED,
|
||||
CALLBACK_DISPATCHED,
|
||||
CALLBACK_FAILED,
|
||||
};
|
||||
|
||||
// Parameters.
|
||||
Vector2 start_position;
|
||||
Vector2 target_position;
|
||||
uint32_t navigation_layers;
|
||||
BitField<PathMetadataFlags> metadata_flags = PathMetadataFlags::PATH_INCLUDE_ALL;
|
||||
PathfindingAlgorithm pathfinding_algorithm = PathfindingAlgorithm::PATHFINDING_ALGORITHM_ASTAR;
|
||||
PathPostProcessing path_postprocessing = PathPostProcessing::PATH_POSTPROCESSING_CORRIDORFUNNEL;
|
||||
bool simplify_path = false;
|
||||
real_t simplify_epsilon = 0.0;
|
||||
|
||||
bool exclude_regions = false;
|
||||
bool include_regions = false;
|
||||
LocalVector<RID> excluded_regions;
|
||||
LocalVector<RID> included_regions;
|
||||
|
||||
float path_return_max_length = 0.0;
|
||||
float path_return_max_radius = 0.0;
|
||||
int path_search_max_polygons = NavigationDefaults2D::path_search_max_polygons;
|
||||
float path_search_max_distance = 0.0;
|
||||
|
||||
// Path building.
|
||||
Vector2 begin_position;
|
||||
Vector2 end_position;
|
||||
const Nav2D::Polygon *begin_polygon = nullptr;
|
||||
const Nav2D::Polygon *end_polygon = nullptr;
|
||||
uint32_t least_cost_id = 0;
|
||||
|
||||
// Map.
|
||||
NavMap2D *map = nullptr;
|
||||
PathQuerySlot *path_query_slot = nullptr;
|
||||
|
||||
// Path points.
|
||||
LocalVector<Vector2> path_points;
|
||||
LocalVector<int32_t> path_meta_point_types;
|
||||
LocalVector<RID> path_meta_point_rids;
|
||||
LocalVector<int64_t> path_meta_point_owners;
|
||||
float path_length = 0.0;
|
||||
|
||||
Ref<NavigationPathQueryParameters2D> query_parameters;
|
||||
Ref<NavigationPathQueryResult2D> query_result;
|
||||
Callable callback;
|
||||
NavMeshPathQueryTask2D::TaskStatus status = NavMeshPathQueryTask2D::TaskStatus::QUERY_STARTED;
|
||||
|
||||
void path_clear() {
|
||||
path_points.clear();
|
||||
path_meta_point_types.clear();
|
||||
path_meta_point_rids.clear();
|
||||
path_meta_point_owners.clear();
|
||||
}
|
||||
|
||||
void path_reverse() {
|
||||
path_points.reverse();
|
||||
path_meta_point_types.reverse();
|
||||
path_meta_point_rids.reverse();
|
||||
path_meta_point_owners.reverse();
|
||||
}
|
||||
};
|
||||
|
||||
static bool emit_callback(const Callable &p_callback);
|
||||
|
||||
static Vector2 polygons_get_random_point(const LocalVector<Nav2D::Polygon> &p_polygons, uint32_t p_navigation_layers, bool p_uniformly);
|
||||
|
||||
static Vector2 polygons_get_closest_point(const LocalVector<Nav2D::Polygon> &p_polygons, const Vector2 &p_point);
|
||||
static Nav2D::ClosestPointQueryResult polygons_get_closest_point_info(const LocalVector<Nav2D::Polygon> &p_polygons, const Vector2 &p_point);
|
||||
static RID polygons_get_closest_point_owner(const LocalVector<Nav2D::Polygon> &p_polygons, const Vector2 &p_point);
|
||||
|
||||
static Vector2 map_iteration_get_closest_point(const NavMapIteration2D &p_map_iteration, const Vector2 &p_point);
|
||||
static RID map_iteration_get_closest_point_owner(const NavMapIteration2D &p_map_iteration, const Vector2 &p_point);
|
||||
static Nav2D::ClosestPointQueryResult map_iteration_get_closest_point_info(const NavMapIteration2D &p_map_iteration, const Vector2 &p_point);
|
||||
static Vector2 map_iteration_get_random_point(const NavMapIteration2D &p_map_iteration, uint32_t p_navigation_layers, bool p_uniformly);
|
||||
|
||||
static void map_query_path(NavMap2D *p_map, const Ref<NavigationPathQueryParameters2D> &p_query_parameters, Ref<NavigationPathQueryResult2D> p_query_result, const Callable &p_callback);
|
||||
|
||||
static void query_task_map_iteration_get_path(NavMeshPathQueryTask2D &p_query_task, const NavMapIteration2D &p_map_iteration);
|
||||
static void _query_task_push_back_point_with_metadata(NavMeshPathQueryTask2D &p_query_task, const Vector2 &p_point, const Nav2D::Polygon *p_point_polygon);
|
||||
static void _query_task_find_start_end_positions(NavMeshPathQueryTask2D &p_query_task, const NavMapIteration2D &p_map_iteration);
|
||||
static void _query_task_build_path_corridor(NavMeshPathQueryTask2D &p_query_task, const NavMapIteration2D &p_map_iteration);
|
||||
static void _query_task_post_process_corridorfunnel(NavMeshPathQueryTask2D &p_query_task);
|
||||
static void _query_task_post_process_edgecentered(NavMeshPathQueryTask2D &p_query_task);
|
||||
static void _query_task_post_process_nopostprocessing(NavMeshPathQueryTask2D &p_query_task);
|
||||
static void _query_task_clip_path(NavMeshPathQueryTask2D &p_query_task, const Nav2D::NavigationPoly *p_from_poly, const Vector2 &p_to_point, const Nav2D::NavigationPoly *p_to_poly);
|
||||
static void _query_task_simplified_path_points(NavMeshPathQueryTask2D &p_query_task);
|
||||
static bool _query_task_is_connection_owner_usable(const NavMeshPathQueryTask2D &p_query_task, const NavBaseIteration2D *p_owner);
|
||||
static void _query_task_process_path_result_limits(NavMeshPathQueryTask2D &p_query_task);
|
||||
|
||||
static void _query_task_search_polygon_connections(NavMeshPathQueryTask2D &p_query_task, const Nav2D::Connection &p_connection, uint32_t p_least_cost_id, const Nav2D::NavigationPoly &p_least_cost_poly, real_t p_poly_enter_cost, const Vector2 &p_end_point);
|
||||
|
||||
static void simplify_path_segment(int p_start_inx, int p_end_inx, const LocalVector<Vector2> &p_points, real_t p_epsilon, LocalVector<uint32_t> &r_simplified_path_indices);
|
||||
static LocalVector<uint32_t> get_simplified_path_indices(const LocalVector<Vector2> &p_path, real_t p_epsilon);
|
||||
|
||||
static float _calculate_path_length(const LocalVector<Vector2> &p_path, uint32_t p_start_index, uint32_t p_end_index);
|
||||
};
|
256
modules/navigation_2d/2d/nav_region_builder_2d.cpp
Normal file
256
modules/navigation_2d/2d/nav_region_builder_2d.cpp
Normal file
@@ -0,0 +1,256 @@
|
||||
/**************************************************************************/
|
||||
/* nav_region_builder_2d.cpp */
|
||||
/**************************************************************************/
|
||||
/* This file is part of: */
|
||||
/* GODOT ENGINE */
|
||||
/* https://godotengine.org */
|
||||
/**************************************************************************/
|
||||
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
||||
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
||||
/* */
|
||||
/* Permission is hereby granted, free of charge, to any person obtaining */
|
||||
/* a copy of this software and associated documentation files (the */
|
||||
/* "Software"), to deal in the Software without restriction, including */
|
||||
/* without limitation the rights to use, copy, modify, merge, publish, */
|
||||
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
||||
/* permit persons to whom the Software is furnished to do so, subject to */
|
||||
/* the following conditions: */
|
||||
/* */
|
||||
/* The above copyright notice and this permission notice shall be */
|
||||
/* included in all copies or substantial portions of the Software. */
|
||||
/* */
|
||||
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
||||
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
||||
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
|
||||
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
||||
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
||||
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
||||
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
||||
/**************************************************************************/
|
||||
|
||||
#include "nav_region_builder_2d.h"
|
||||
|
||||
#include "../nav_map_2d.h"
|
||||
#include "../nav_region_2d.h"
|
||||
#include "../triangle2.h"
|
||||
#include "nav_region_iteration_2d.h"
|
||||
|
||||
using namespace Nav2D;
|
||||
|
||||
void NavRegionBuilder2D::build_iteration(NavRegionIterationBuild2D &r_build) {
|
||||
PerformanceData &performance_data = r_build.performance_data;
|
||||
|
||||
performance_data.pm_polygon_count = 0;
|
||||
performance_data.pm_edge_count = 0;
|
||||
performance_data.pm_edge_merge_count = 0;
|
||||
performance_data.pm_edge_connection_count = 0;
|
||||
performance_data.pm_edge_free_count = 0;
|
||||
|
||||
_build_step_process_navmesh_data(r_build);
|
||||
|
||||
_build_step_find_edge_connection_pairs(r_build);
|
||||
|
||||
_build_step_merge_edge_connection_pairs(r_build);
|
||||
|
||||
_build_update_iteration(r_build);
|
||||
}
|
||||
|
||||
void NavRegionBuilder2D::_build_step_process_navmesh_data(NavRegionIterationBuild2D &r_build) {
|
||||
Vector<Vector2> _navmesh_vertices = r_build.navmesh_data.vertices;
|
||||
Vector<Vector<int>> _navmesh_polygons = r_build.navmesh_data.polygons;
|
||||
|
||||
if (_navmesh_vertices.is_empty() || _navmesh_polygons.is_empty()) {
|
||||
return;
|
||||
}
|
||||
|
||||
PerformanceData &performance_data = r_build.performance_data;
|
||||
Ref<NavRegionIteration2D> region_iteration = r_build.region_iteration;
|
||||
|
||||
const Transform2D ®ion_transform = region_iteration->transform;
|
||||
LocalVector<Nav2D::Polygon> &navmesh_polygons = region_iteration->navmesh_polygons;
|
||||
|
||||
const int vertex_count = _navmesh_vertices.size();
|
||||
|
||||
const Vector2 *vertices_ptr = _navmesh_vertices.ptr();
|
||||
const Vector<int> *polygons_ptr = _navmesh_polygons.ptr();
|
||||
|
||||
navmesh_polygons.resize(_navmesh_polygons.size());
|
||||
|
||||
real_t _new_region_surface_area = 0.0;
|
||||
Rect2 _new_region_bounds;
|
||||
|
||||
bool first_vertex = true;
|
||||
|
||||
for (uint32_t i = 0; i < navmesh_polygons.size(); i++) {
|
||||
Polygon &polygon = navmesh_polygons[i];
|
||||
polygon.id = i;
|
||||
polygon.owner = region_iteration.ptr();
|
||||
polygon.surface_area = 0.0;
|
||||
|
||||
Vector<int> polygon_indices = polygons_ptr[i];
|
||||
|
||||
int polygon_size = polygon_indices.size();
|
||||
if (polygon_size < 3) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const int *indices_ptr = polygon_indices.ptr();
|
||||
|
||||
bool polygon_valid = true;
|
||||
|
||||
polygon.vertices.resize(polygon_size);
|
||||
|
||||
{
|
||||
real_t _new_polygon_surface_area = 0.0;
|
||||
|
||||
for (int j(2); j < polygon_size; j++) {
|
||||
const Triangle2 triangle = Triangle2(
|
||||
region_transform.xform(vertices_ptr[indices_ptr[0]]),
|
||||
region_transform.xform(vertices_ptr[indices_ptr[j - 1]]),
|
||||
region_transform.xform(vertices_ptr[indices_ptr[j]]));
|
||||
|
||||
_new_polygon_surface_area += triangle.get_area();
|
||||
}
|
||||
|
||||
polygon.surface_area = _new_polygon_surface_area;
|
||||
_new_region_surface_area += _new_polygon_surface_area;
|
||||
}
|
||||
|
||||
for (int j(0); j < polygon_size; j++) {
|
||||
int vertex_index = indices_ptr[j];
|
||||
if (vertex_index < 0 || vertex_index >= vertex_count) {
|
||||
polygon_valid = false;
|
||||
break;
|
||||
}
|
||||
|
||||
const Vector2 point_position = region_transform.xform(vertices_ptr[vertex_index]);
|
||||
polygon.vertices[j] = point_position;
|
||||
|
||||
if (first_vertex) {
|
||||
first_vertex = false;
|
||||
_new_region_bounds.position = point_position;
|
||||
} else {
|
||||
_new_region_bounds.expand_to(point_position);
|
||||
}
|
||||
}
|
||||
|
||||
if (!polygon_valid) {
|
||||
polygon.surface_area = 0.0;
|
||||
polygon.vertices.clear();
|
||||
ERR_FAIL_COND_MSG(!polygon_valid, "Corrupted navigation mesh set on region. The indices of a polygon are out of range.");
|
||||
}
|
||||
}
|
||||
|
||||
region_iteration->surface_area = _new_region_surface_area;
|
||||
region_iteration->bounds = _new_region_bounds;
|
||||
|
||||
performance_data.pm_polygon_count = navmesh_polygons.size();
|
||||
}
|
||||
|
||||
Nav2D::PointKey NavRegionBuilder2D::get_point_key(const Vector2 &p_pos, const Vector2 &p_cell_size) {
|
||||
const int x = static_cast<int>(Math::floor(p_pos.x / p_cell_size.x));
|
||||
const int y = static_cast<int>(Math::floor(p_pos.y / p_cell_size.y));
|
||||
|
||||
PointKey p;
|
||||
p.key = 0;
|
||||
p.x = x;
|
||||
p.y = y;
|
||||
return p;
|
||||
}
|
||||
|
||||
Nav2D::EdgeKey NavRegionBuilder2D::get_edge_key(const Vector2 &p_vertex1, const Vector2 &p_vertex2, const Vector2 &p_cell_size) {
|
||||
EdgeKey ek(get_point_key(p_vertex1, p_cell_size), get_point_key(p_vertex2, p_cell_size));
|
||||
return ek;
|
||||
}
|
||||
|
||||
void NavRegionBuilder2D::_build_step_find_edge_connection_pairs(NavRegionIterationBuild2D &r_build) {
|
||||
PerformanceData &performance_data = r_build.performance_data;
|
||||
|
||||
const Vector2 &map_cell_size = r_build.map_cell_size;
|
||||
Ref<NavRegionIteration2D> region_iteration = r_build.region_iteration;
|
||||
LocalVector<Nav2D::Polygon> &navmesh_polygons = region_iteration->navmesh_polygons;
|
||||
|
||||
HashMap<EdgeKey, EdgeConnectionPair, EdgeKey> &connection_pairs_map = r_build.iter_connection_pairs_map;
|
||||
connection_pairs_map.clear();
|
||||
|
||||
region_iteration->internal_connections.clear();
|
||||
region_iteration->internal_connections.resize(navmesh_polygons.size());
|
||||
|
||||
region_iteration->external_edges.clear();
|
||||
|
||||
int free_edges_count = 0;
|
||||
|
||||
for (Polygon &poly : region_iteration->navmesh_polygons) {
|
||||
for (uint32_t p = 0; p < poly.vertices.size(); p++) {
|
||||
const int next_point = (p + 1) % poly.vertices.size();
|
||||
const EdgeKey ek = get_edge_key(poly.vertices[p], poly.vertices[next_point], map_cell_size);
|
||||
|
||||
HashMap<EdgeKey, EdgeConnectionPair, EdgeKey>::Iterator pair_it = connection_pairs_map.find(ek);
|
||||
if (!pair_it) {
|
||||
pair_it = connection_pairs_map.insert(ek, EdgeConnectionPair());
|
||||
performance_data.pm_edge_count += 1;
|
||||
++free_edges_count;
|
||||
}
|
||||
EdgeConnectionPair &pair = pair_it->value;
|
||||
if (pair.size < 2) {
|
||||
// Add the polygon/edge tuple to this key.
|
||||
Connection new_connection;
|
||||
new_connection.polygon = &poly;
|
||||
new_connection.edge = p;
|
||||
new_connection.pathway_start = poly.vertices[p];
|
||||
new_connection.pathway_end = poly.vertices[next_point];
|
||||
|
||||
pair.connections[pair.size] = new_connection;
|
||||
++pair.size;
|
||||
if (pair.size == 2) {
|
||||
--free_edges_count;
|
||||
}
|
||||
|
||||
} else {
|
||||
// The edge is already connected with another edge, skip.
|
||||
ERR_FAIL_COND_MSG(pair.size >= 2, "Navigation region synchronization error. More than 2 edges tried to occupy the same map rasterization space. This is a logical error in the navigation mesh caused by overlap or too densely placed edges.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
performance_data.pm_edge_free_count = free_edges_count;
|
||||
}
|
||||
|
||||
void NavRegionBuilder2D::_build_step_merge_edge_connection_pairs(NavRegionIterationBuild2D &r_build) {
|
||||
PerformanceData &performance_data = r_build.performance_data;
|
||||
|
||||
Ref<NavRegionIteration2D> region_iteration = r_build.region_iteration;
|
||||
|
||||
HashMap<EdgeKey, EdgeConnectionPair, EdgeKey> &connection_pairs_map = r_build.iter_connection_pairs_map;
|
||||
|
||||
for (const KeyValue<EdgeKey, EdgeConnectionPair> &pair_it : connection_pairs_map) {
|
||||
const EdgeConnectionPair &pair = pair_it.value;
|
||||
if (pair.size == 2) {
|
||||
// Connect edge that are shared in different polygons.
|
||||
const Connection &c1 = pair.connections[0];
|
||||
const Connection &c2 = pair.connections[1];
|
||||
region_iteration->internal_connections[c1.polygon->id].push_back(c2);
|
||||
region_iteration->internal_connections[c2.polygon->id].push_back(c1);
|
||||
performance_data.pm_edge_merge_count += 1;
|
||||
|
||||
} else {
|
||||
ERR_FAIL_COND_MSG(pair.size != 1, vformat("Number of connection != 1. Found: %d", pair.size));
|
||||
|
||||
const Connection &connection = pair.connections[0];
|
||||
|
||||
ConnectableEdge ce;
|
||||
ce.ek = pair_it.key;
|
||||
ce.polygon_index = connection.polygon->id;
|
||||
ce.edge = connection.edge;
|
||||
ce.pathway_start = connection.pathway_start;
|
||||
ce.pathway_end = connection.pathway_end;
|
||||
|
||||
region_iteration->external_edges.push_back(ce);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void NavRegionBuilder2D::_build_update_iteration(NavRegionIterationBuild2D &r_build) {
|
||||
ERR_FAIL_NULL(r_build.region);
|
||||
// Stub. End of the build.
|
||||
}
|
48
modules/navigation_2d/2d/nav_region_builder_2d.h
Normal file
48
modules/navigation_2d/2d/nav_region_builder_2d.h
Normal file
@@ -0,0 +1,48 @@
|
||||
/**************************************************************************/
|
||||
/* nav_region_builder_2d.h */
|
||||
/**************************************************************************/
|
||||
/* This file is part of: */
|
||||
/* GODOT ENGINE */
|
||||
/* https://godotengine.org */
|
||||
/**************************************************************************/
|
||||
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
||||
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
||||
/* */
|
||||
/* Permission is hereby granted, free of charge, to any person obtaining */
|
||||
/* a copy of this software and associated documentation files (the */
|
||||
/* "Software"), to deal in the Software without restriction, including */
|
||||
/* without limitation the rights to use, copy, modify, merge, publish, */
|
||||
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
||||
/* permit persons to whom the Software is furnished to do so, subject to */
|
||||
/* the following conditions: */
|
||||
/* */
|
||||
/* The above copyright notice and this permission notice shall be */
|
||||
/* included in all copies or substantial portions of the Software. */
|
||||
/* */
|
||||
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
||||
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
||||
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
|
||||
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
||||
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
||||
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
||||
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
||||
/**************************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "../nav_utils_2d.h"
|
||||
|
||||
struct NavRegionIterationBuild2D;
|
||||
|
||||
class NavRegionBuilder2D {
|
||||
static void _build_step_process_navmesh_data(NavRegionIterationBuild2D &r_build);
|
||||
static void _build_step_find_edge_connection_pairs(NavRegionIterationBuild2D &r_build);
|
||||
static void _build_step_merge_edge_connection_pairs(NavRegionIterationBuild2D &r_build);
|
||||
static void _build_update_iteration(NavRegionIterationBuild2D &r_build);
|
||||
|
||||
public:
|
||||
static Nav2D::PointKey get_point_key(const Vector2 &p_pos, const Vector2 &p_cell_size);
|
||||
static Nav2D::EdgeKey get_edge_key(const Vector2 &p_vertex1, const Vector2 &p_vertex2, const Vector2 &p_cell_size);
|
||||
|
||||
static void build_iteration(NavRegionIterationBuild2D &r_build);
|
||||
};
|
92
modules/navigation_2d/2d/nav_region_iteration_2d.h
Normal file
92
modules/navigation_2d/2d/nav_region_iteration_2d.h
Normal file
@@ -0,0 +1,92 @@
|
||||
/**************************************************************************/
|
||||
/* nav_region_iteration_2d.h */
|
||||
/**************************************************************************/
|
||||
/* This file is part of: */
|
||||
/* GODOT ENGINE */
|
||||
/* https://godotengine.org */
|
||||
/**************************************************************************/
|
||||
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
||||
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
||||
/* */
|
||||
/* Permission is hereby granted, free of charge, to any person obtaining */
|
||||
/* a copy of this software and associated documentation files (the */
|
||||
/* "Software"), to deal in the Software without restriction, including */
|
||||
/* without limitation the rights to use, copy, modify, merge, publish, */
|
||||
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
||||
/* permit persons to whom the Software is furnished to do so, subject to */
|
||||
/* the following conditions: */
|
||||
/* */
|
||||
/* The above copyright notice and this permission notice shall be */
|
||||
/* included in all copies or substantial portions of the Software. */
|
||||
/* */
|
||||
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
||||
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
||||
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
|
||||
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
||||
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
||||
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
||||
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
||||
/**************************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "../nav_utils_2d.h"
|
||||
#include "nav_base_iteration_2d.h"
|
||||
#include "scene/resources/2d/navigation_polygon.h"
|
||||
|
||||
#include "core/math/rect2.h"
|
||||
|
||||
class NavRegion2D;
|
||||
class NavRegionIteration2D;
|
||||
|
||||
struct NavRegionIterationBuild2D {
|
||||
Nav2D::PerformanceData performance_data;
|
||||
|
||||
NavRegion2D *region = nullptr;
|
||||
|
||||
Vector2 map_cell_size;
|
||||
Transform2D region_transform;
|
||||
|
||||
struct NavMeshData {
|
||||
Vector<Vector2> vertices;
|
||||
Vector<Vector<int>> polygons;
|
||||
|
||||
void clear() {
|
||||
vertices.clear();
|
||||
polygons.clear();
|
||||
}
|
||||
} navmesh_data;
|
||||
|
||||
Ref<NavRegionIteration2D> region_iteration;
|
||||
|
||||
HashMap<Nav2D::EdgeKey, Nav2D::EdgeConnectionPair, Nav2D::EdgeKey> iter_connection_pairs_map;
|
||||
|
||||
void reset() {
|
||||
performance_data.reset();
|
||||
|
||||
navmesh_data.clear();
|
||||
region_iteration = Ref<NavRegionIteration2D>();
|
||||
iter_connection_pairs_map.clear();
|
||||
}
|
||||
};
|
||||
|
||||
class NavRegionIteration2D : public NavBaseIteration2D {
|
||||
GDCLASS(NavRegionIteration2D, NavBaseIteration2D);
|
||||
|
||||
public:
|
||||
Transform2D transform;
|
||||
real_t surface_area = 0.0;
|
||||
Rect2 bounds;
|
||||
LocalVector<Nav2D::ConnectableEdge> external_edges;
|
||||
|
||||
const Transform2D &get_transform() const { return transform; }
|
||||
real_t get_surface_area() const { return surface_area; }
|
||||
Rect2 get_bounds() const { return bounds; }
|
||||
const LocalVector<Nav2D::ConnectableEdge> &get_external_edges() const { return external_edges; }
|
||||
|
||||
virtual ~NavRegionIteration2D() override {
|
||||
external_edges.clear();
|
||||
navmesh_polygons.clear();
|
||||
internal_connections.clear();
|
||||
}
|
||||
};
|
Reference in New Issue
Block a user