Path: blob/master/modules/navigation_2d/2d/nav_mesh_queries_2d.h
10278 views
/**************************************************************************/1/* nav_mesh_queries_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 "../nav_utils_2d.h"3334#include "core/templates/a_hash_map.h"3536#include "servers/navigation/navigation_globals.h"37#include "servers/navigation/navigation_path_query_parameters_2d.h"38#include "servers/navigation/navigation_path_query_result_2d.h"39#include "servers/navigation/navigation_utilities.h"4041using namespace NavigationUtilities;4243class NavMap2D;44struct NavMapIteration2D;4546class NavMeshQueries2D {47public:48struct PathQuerySlot {49LocalVector<Nav2D::NavigationPoly> path_corridor;50Heap<Nav2D::NavigationPoly *, Nav2D::NavPolyTravelCostGreaterThan, Nav2D::NavPolyHeapIndexer> traversable_polys;51bool in_use = false;52uint32_t slot_index = 0;53AHashMap<const Nav2D::Polygon *, uint32_t> poly_to_id;54};5556struct NavMeshPathQueryTask2D {57enum TaskStatus {58QUERY_STARTED,59QUERY_FINISHED,60QUERY_FAILED,61CALLBACK_DISPATCHED,62CALLBACK_FAILED,63};6465// Parameters.66Vector2 start_position;67Vector2 target_position;68uint32_t navigation_layers;69BitField<PathMetadataFlags> metadata_flags = PathMetadataFlags::PATH_INCLUDE_ALL;70PathfindingAlgorithm pathfinding_algorithm = PathfindingAlgorithm::PATHFINDING_ALGORITHM_ASTAR;71PathPostProcessing path_postprocessing = PathPostProcessing::PATH_POSTPROCESSING_CORRIDORFUNNEL;72bool simplify_path = false;73real_t simplify_epsilon = 0.0;7475bool exclude_regions = false;76bool include_regions = false;77LocalVector<RID> excluded_regions;78LocalVector<RID> included_regions;7980float path_return_max_length = 0.0;81float path_return_max_radius = 0.0;82int path_search_max_polygons = NavigationDefaults2D::path_search_max_polygons;83float path_search_max_distance = 0.0;8485// Path building.86Vector2 begin_position;87Vector2 end_position;88const Nav2D::Polygon *begin_polygon = nullptr;89const Nav2D::Polygon *end_polygon = nullptr;90uint32_t least_cost_id = 0;9192// Map.93NavMap2D *map = nullptr;94PathQuerySlot *path_query_slot = nullptr;9596// Path points.97LocalVector<Vector2> path_points;98LocalVector<int32_t> path_meta_point_types;99LocalVector<RID> path_meta_point_rids;100LocalVector<int64_t> path_meta_point_owners;101float path_length = 0.0;102103Ref<NavigationPathQueryParameters2D> query_parameters;104Ref<NavigationPathQueryResult2D> query_result;105Callable callback;106NavMeshPathQueryTask2D::TaskStatus status = NavMeshPathQueryTask2D::TaskStatus::QUERY_STARTED;107108void path_clear() {109path_points.clear();110path_meta_point_types.clear();111path_meta_point_rids.clear();112path_meta_point_owners.clear();113}114115void path_reverse() {116path_points.reverse();117path_meta_point_types.reverse();118path_meta_point_rids.reverse();119path_meta_point_owners.reverse();120}121};122123static bool emit_callback(const Callable &p_callback);124125static Vector2 polygons_get_random_point(const LocalVector<Nav2D::Polygon> &p_polygons, uint32_t p_navigation_layers, bool p_uniformly);126127static Vector2 polygons_get_closest_point(const LocalVector<Nav2D::Polygon> &p_polygons, const Vector2 &p_point);128static Nav2D::ClosestPointQueryResult polygons_get_closest_point_info(const LocalVector<Nav2D::Polygon> &p_polygons, const Vector2 &p_point);129static RID polygons_get_closest_point_owner(const LocalVector<Nav2D::Polygon> &p_polygons, const Vector2 &p_point);130131static Vector2 map_iteration_get_closest_point(const NavMapIteration2D &p_map_iteration, const Vector2 &p_point);132static RID map_iteration_get_closest_point_owner(const NavMapIteration2D &p_map_iteration, const Vector2 &p_point);133static Nav2D::ClosestPointQueryResult map_iteration_get_closest_point_info(const NavMapIteration2D &p_map_iteration, const Vector2 &p_point);134static Vector2 map_iteration_get_random_point(const NavMapIteration2D &p_map_iteration, uint32_t p_navigation_layers, bool p_uniformly);135136static void map_query_path(NavMap2D *p_map, const Ref<NavigationPathQueryParameters2D> &p_query_parameters, Ref<NavigationPathQueryResult2D> p_query_result, const Callable &p_callback);137138static void query_task_map_iteration_get_path(NavMeshPathQueryTask2D &p_query_task, const NavMapIteration2D &p_map_iteration);139static void _query_task_push_back_point_with_metadata(NavMeshPathQueryTask2D &p_query_task, const Vector2 &p_point, const Nav2D::Polygon *p_point_polygon);140static void _query_task_find_start_end_positions(NavMeshPathQueryTask2D &p_query_task, const NavMapIteration2D &p_map_iteration);141static void _query_task_build_path_corridor(NavMeshPathQueryTask2D &p_query_task, const NavMapIteration2D &p_map_iteration);142static void _query_task_post_process_corridorfunnel(NavMeshPathQueryTask2D &p_query_task);143static void _query_task_post_process_edgecentered(NavMeshPathQueryTask2D &p_query_task);144static void _query_task_post_process_nopostprocessing(NavMeshPathQueryTask2D &p_query_task);145static 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);146static void _query_task_simplified_path_points(NavMeshPathQueryTask2D &p_query_task);147static bool _query_task_is_connection_owner_usable(const NavMeshPathQueryTask2D &p_query_task, const NavBaseIteration2D *p_owner);148static void _query_task_process_path_result_limits(NavMeshPathQueryTask2D &p_query_task);149150static 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);151152static 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);153static LocalVector<uint32_t> get_simplified_path_indices(const LocalVector<Vector2> &p_path, real_t p_epsilon);154155static float _calculate_path_length(const LocalVector<Vector2> &p_path, uint32_t p_start_index, uint32_t p_end_index);156};157158159