Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/modules/navigation_2d/2d/nav_mesh_queries_2d.h
10278 views
1
/**************************************************************************/
2
/* nav_mesh_queries_2d.h */
3
/**************************************************************************/
4
/* This file is part of: */
5
/* GODOT ENGINE */
6
/* https://godotengine.org */
7
/**************************************************************************/
8
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10
/* */
11
/* Permission is hereby granted, free of charge, to any person obtaining */
12
/* a copy of this software and associated documentation files (the */
13
/* "Software"), to deal in the Software without restriction, including */
14
/* without limitation the rights to use, copy, modify, merge, publish, */
15
/* distribute, sublicense, and/or sell copies of the Software, and to */
16
/* permit persons to whom the Software is furnished to do so, subject to */
17
/* the following conditions: */
18
/* */
19
/* The above copyright notice and this permission notice shall be */
20
/* included in all copies or substantial portions of the Software. */
21
/* */
22
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29
/**************************************************************************/
30
31
#pragma once
32
33
#include "../nav_utils_2d.h"
34
35
#include "core/templates/a_hash_map.h"
36
37
#include "servers/navigation/navigation_globals.h"
38
#include "servers/navigation/navigation_path_query_parameters_2d.h"
39
#include "servers/navigation/navigation_path_query_result_2d.h"
40
#include "servers/navigation/navigation_utilities.h"
41
42
using namespace NavigationUtilities;
43
44
class NavMap2D;
45
struct NavMapIteration2D;
46
47
class NavMeshQueries2D {
48
public:
49
struct PathQuerySlot {
50
LocalVector<Nav2D::NavigationPoly> path_corridor;
51
Heap<Nav2D::NavigationPoly *, Nav2D::NavPolyTravelCostGreaterThan, Nav2D::NavPolyHeapIndexer> traversable_polys;
52
bool in_use = false;
53
uint32_t slot_index = 0;
54
AHashMap<const Nav2D::Polygon *, uint32_t> poly_to_id;
55
};
56
57
struct NavMeshPathQueryTask2D {
58
enum TaskStatus {
59
QUERY_STARTED,
60
QUERY_FINISHED,
61
QUERY_FAILED,
62
CALLBACK_DISPATCHED,
63
CALLBACK_FAILED,
64
};
65
66
// Parameters.
67
Vector2 start_position;
68
Vector2 target_position;
69
uint32_t navigation_layers;
70
BitField<PathMetadataFlags> metadata_flags = PathMetadataFlags::PATH_INCLUDE_ALL;
71
PathfindingAlgorithm pathfinding_algorithm = PathfindingAlgorithm::PATHFINDING_ALGORITHM_ASTAR;
72
PathPostProcessing path_postprocessing = PathPostProcessing::PATH_POSTPROCESSING_CORRIDORFUNNEL;
73
bool simplify_path = false;
74
real_t simplify_epsilon = 0.0;
75
76
bool exclude_regions = false;
77
bool include_regions = false;
78
LocalVector<RID> excluded_regions;
79
LocalVector<RID> included_regions;
80
81
float path_return_max_length = 0.0;
82
float path_return_max_radius = 0.0;
83
int path_search_max_polygons = NavigationDefaults2D::path_search_max_polygons;
84
float path_search_max_distance = 0.0;
85
86
// Path building.
87
Vector2 begin_position;
88
Vector2 end_position;
89
const Nav2D::Polygon *begin_polygon = nullptr;
90
const Nav2D::Polygon *end_polygon = nullptr;
91
uint32_t least_cost_id = 0;
92
93
// Map.
94
NavMap2D *map = nullptr;
95
PathQuerySlot *path_query_slot = nullptr;
96
97
// Path points.
98
LocalVector<Vector2> path_points;
99
LocalVector<int32_t> path_meta_point_types;
100
LocalVector<RID> path_meta_point_rids;
101
LocalVector<int64_t> path_meta_point_owners;
102
float path_length = 0.0;
103
104
Ref<NavigationPathQueryParameters2D> query_parameters;
105
Ref<NavigationPathQueryResult2D> query_result;
106
Callable callback;
107
NavMeshPathQueryTask2D::TaskStatus status = NavMeshPathQueryTask2D::TaskStatus::QUERY_STARTED;
108
109
void path_clear() {
110
path_points.clear();
111
path_meta_point_types.clear();
112
path_meta_point_rids.clear();
113
path_meta_point_owners.clear();
114
}
115
116
void path_reverse() {
117
path_points.reverse();
118
path_meta_point_types.reverse();
119
path_meta_point_rids.reverse();
120
path_meta_point_owners.reverse();
121
}
122
};
123
124
static bool emit_callback(const Callable &p_callback);
125
126
static Vector2 polygons_get_random_point(const LocalVector<Nav2D::Polygon> &p_polygons, uint32_t p_navigation_layers, bool p_uniformly);
127
128
static Vector2 polygons_get_closest_point(const LocalVector<Nav2D::Polygon> &p_polygons, const Vector2 &p_point);
129
static Nav2D::ClosestPointQueryResult polygons_get_closest_point_info(const LocalVector<Nav2D::Polygon> &p_polygons, const Vector2 &p_point);
130
static RID polygons_get_closest_point_owner(const LocalVector<Nav2D::Polygon> &p_polygons, const Vector2 &p_point);
131
132
static Vector2 map_iteration_get_closest_point(const NavMapIteration2D &p_map_iteration, const Vector2 &p_point);
133
static RID map_iteration_get_closest_point_owner(const NavMapIteration2D &p_map_iteration, const Vector2 &p_point);
134
static Nav2D::ClosestPointQueryResult map_iteration_get_closest_point_info(const NavMapIteration2D &p_map_iteration, const Vector2 &p_point);
135
static Vector2 map_iteration_get_random_point(const NavMapIteration2D &p_map_iteration, uint32_t p_navigation_layers, bool p_uniformly);
136
137
static void map_query_path(NavMap2D *p_map, const Ref<NavigationPathQueryParameters2D> &p_query_parameters, Ref<NavigationPathQueryResult2D> p_query_result, const Callable &p_callback);
138
139
static void query_task_map_iteration_get_path(NavMeshPathQueryTask2D &p_query_task, const NavMapIteration2D &p_map_iteration);
140
static void _query_task_push_back_point_with_metadata(NavMeshPathQueryTask2D &p_query_task, const Vector2 &p_point, const Nav2D::Polygon *p_point_polygon);
141
static void _query_task_find_start_end_positions(NavMeshPathQueryTask2D &p_query_task, const NavMapIteration2D &p_map_iteration);
142
static void _query_task_build_path_corridor(NavMeshPathQueryTask2D &p_query_task, const NavMapIteration2D &p_map_iteration);
143
static void _query_task_post_process_corridorfunnel(NavMeshPathQueryTask2D &p_query_task);
144
static void _query_task_post_process_edgecentered(NavMeshPathQueryTask2D &p_query_task);
145
static void _query_task_post_process_nopostprocessing(NavMeshPathQueryTask2D &p_query_task);
146
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);
147
static void _query_task_simplified_path_points(NavMeshPathQueryTask2D &p_query_task);
148
static bool _query_task_is_connection_owner_usable(const NavMeshPathQueryTask2D &p_query_task, const NavBaseIteration2D *p_owner);
149
static void _query_task_process_path_result_limits(NavMeshPathQueryTask2D &p_query_task);
150
151
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);
152
153
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);
154
static LocalVector<uint32_t> get_simplified_path_indices(const LocalVector<Vector2> &p_path, real_t p_epsilon);
155
156
static float _calculate_path_length(const LocalVector<Vector2> &p_path, uint32_t p_start_index, uint32_t p_end_index);
157
};
158
159