Path: blob/master/modules/navigation_2d/nav_map_2d.h
10277 views
/**************************************************************************/1/* nav_map_2d.h */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#pragma once3132#include "2d/nav_map_iteration_2d.h"33#include "2d/nav_mesh_queries_2d.h"34#include "nav_rid_2d.h"35#include "nav_utils_2d.h"3637#include "core/math/math_defs.h"38#include "core/object/worker_thread_pool.h"39#include "servers/navigation/navigation_globals.h"4041#include <KdTree2d.h>42#include <RVOSimulator2d.h>4344class NavLink2D;45class NavRegion2D;46class NavAgent2D;47class NavObstacle2D;4849class NavMap2D : public NavRid2D {50/// To find the polygons edges the vertices are displaced in a grid where51/// each cell has the following cell_size.52real_t cell_size = NavigationDefaults2D::NAV_MESH_CELL_SIZE;5354// For the inter-region merging to work, internal rasterization is performed.55Vector2 merge_rasterizer_cell_size = Vector2(cell_size, cell_size);5657// This value is used to control sensitivity of internal rasterizer.58float merge_rasterizer_cell_scale = 1.0;5960bool use_edge_connections = true;61/// This value is used to detect the near edges to connect.62real_t edge_connection_margin = NavigationDefaults2D::EDGE_CONNECTION_MARGIN;6364/// This value is used to limit how far links search to find polygons to connect to.65real_t link_connection_radius = NavigationDefaults2D::LINK_CONNECTION_RADIUS;6667bool map_settings_dirty = true;6869/// Map regions.70LocalVector<NavRegion2D *> regions;7172/// Map links.73LocalVector<NavLink2D *> links;7475/// RVO avoidance world.76RVO2D::RVOSimulator2D rvo_simulation;7778/// Avoidance controlled agents.79LocalVector<NavAgent2D *> active_avoidance_agents;8081/// dirty flag when one of the agent's arrays are modified.82bool agents_dirty = true;8384/// All the Agents (even the controlled one).85LocalVector<NavAgent2D *> agents;8687/// All the avoidance obstacles (both static and dynamic).88LocalVector<NavObstacle2D *> obstacles;8990/// Are rvo obstacles modified?91bool obstacles_dirty = true;9293/// Change the id each time the map is updated.94uint32_t iteration_id = 0;9596bool use_threads = true;97bool avoidance_use_multiple_threads = true;98bool avoidance_use_high_priority_threads = true;99100// Performance Monitor101Nav2D::PerformanceData performance_data;102103struct {104struct {105RWLock rwlock;106SelfList<NavRegion2D>::List list;107} regions;108struct {109RWLock rwlock;110SelfList<NavLink2D>::List list;111} links;112struct {113RWLock rwlock;114SelfList<NavAgent2D>::List list;115} agents;116struct {117RWLock rwlock;118SelfList<NavObstacle2D>::List list;119} obstacles;120} sync_dirty_requests;121122struct {123struct {124RWLock rwlock;125SelfList<NavRegion2D>::List list;126} regions;127} async_dirty_requests;128129int path_query_slots_max = 4;130131bool use_async_iterations = true;132133uint32_t iteration_slot_index = 0;134LocalVector<NavMapIteration2D> iteration_slots;135mutable RWLock iteration_slot_rwlock;136137NavMapIterationBuild2D iteration_build;138WorkerThreadPool::TaskID iteration_build_thread_task_id = WorkerThreadPool::INVALID_TASK_ID;139static void _build_iteration_threaded(void *p_arg);140141bool iteration_dirty = true;142bool iteration_building = false;143bool iteration_ready = false;144145void _build_iteration();146void _sync_iteration();147148public:149NavMap2D();150~NavMap2D();151152uint32_t get_iteration_id() const { return iteration_id; }153154void set_cell_size(real_t p_cell_size);155real_t get_cell_size() const {156return cell_size;157}158159void set_merge_rasterizer_cell_scale(float p_value);160float get_merge_rasterizer_cell_scale() const {161return merge_rasterizer_cell_scale;162}163164void set_use_edge_connections(bool p_enabled);165bool get_use_edge_connections() const {166return use_edge_connections;167}168169void set_edge_connection_margin(real_t p_edge_connection_margin);170real_t get_edge_connection_margin() const {171return edge_connection_margin;172}173174void set_link_connection_radius(real_t p_link_connection_radius);175real_t get_link_connection_radius() const {176return link_connection_radius;177}178179Nav2D::PointKey get_point_key(const Vector2 &p_pos) const;180const Vector2 &get_merge_rasterizer_cell_size() const;181182void query_path(NavMeshQueries2D::NavMeshPathQueryTask2D &p_query_task);183184Vector2 get_closest_point(const Vector2 &p_point) const;185Nav2D::ClosestPointQueryResult get_closest_point_info(const Vector2 &p_point) const;186RID get_closest_point_owner(const Vector2 &p_point) const;187188void add_region(NavRegion2D *p_region);189void remove_region(NavRegion2D *p_region);190const LocalVector<NavRegion2D *> &get_regions() const {191return regions;192}193194void add_link(NavLink2D *p_link);195void remove_link(NavLink2D *p_link);196const LocalVector<NavLink2D *> &get_links() const {197return links;198}199200bool has_agent(NavAgent2D *p_agent) const;201void add_agent(NavAgent2D *p_agent);202void remove_agent(NavAgent2D *p_agent);203const LocalVector<NavAgent2D *> &get_agents() const {204return agents;205}206207void set_agent_as_controlled(NavAgent2D *p_agent);208void remove_agent_as_controlled(NavAgent2D *p_agent);209210bool has_obstacle(NavObstacle2D *p_obstacle) const;211void add_obstacle(NavObstacle2D *p_obstacle);212void remove_obstacle(NavObstacle2D *p_obstacle);213const LocalVector<NavObstacle2D *> &get_obstacles() const {214return obstacles;215}216217Vector2 get_random_point(uint32_t p_navigation_layers, bool p_uniformly) const;218219void sync();220void step(double p_delta_time);221void dispatch_callbacks();222223// Performance Monitor224int get_pm_region_count() const { return performance_data.pm_region_count; }225int get_pm_agent_count() const { return performance_data.pm_agent_count; }226int get_pm_link_count() const { return performance_data.pm_link_count; }227int get_pm_polygon_count() const { return performance_data.pm_polygon_count; }228int get_pm_edge_count() const { return performance_data.pm_edge_count; }229int get_pm_edge_merge_count() const { return performance_data.pm_edge_merge_count; }230int get_pm_edge_connection_count() const { return performance_data.pm_edge_connection_count; }231int get_pm_edge_free_count() const { return performance_data.pm_edge_free_count; }232int get_pm_obstacle_count() const { return performance_data.pm_obstacle_count; }233234int get_region_connections_count(NavRegion2D *p_region) const;235Vector2 get_region_connection_pathway_start(NavRegion2D *p_region, int p_connection_id) const;236Vector2 get_region_connection_pathway_end(NavRegion2D *p_region, int p_connection_id) const;237238void add_region_async_thread_join_request(SelfList<NavRegion2D> *p_async_request);239void remove_region_async_thread_join_request(SelfList<NavRegion2D> *p_async_request);240241void add_region_sync_dirty_request(SelfList<NavRegion2D> *p_sync_request);242void add_link_sync_dirty_request(SelfList<NavLink2D> *p_sync_request);243void add_agent_sync_dirty_request(SelfList<NavAgent2D> *p_sync_request);244void add_obstacle_sync_dirty_request(SelfList<NavObstacle2D> *p_sync_request);245246void remove_region_sync_dirty_request(SelfList<NavRegion2D> *p_sync_request);247void remove_link_sync_dirty_request(SelfList<NavLink2D> *p_sync_request);248void remove_agent_sync_dirty_request(SelfList<NavAgent2D> *p_sync_request);249void remove_obstacle_sync_dirty_request(SelfList<NavObstacle2D> *p_sync_request);250251void set_use_async_iterations(bool p_enabled);252bool get_use_async_iterations() const;253254private:255void _sync_dirty_map_update_requests();256void _sync_dirty_avoidance_update_requests();257void _sync_async_tasks();258259void compute_single_step(uint32_t p_index, NavAgent2D **p_agent);260261void compute_single_avoidance_step(uint32_t p_index, NavAgent2D **p_agent);262263void _sync_avoidance();264void _update_rvo_simulation();265void _update_rvo_obstacles_tree();266void _update_rvo_agents_tree();267268void _update_merge_rasterizer_cell_dimensions();269};270271272