Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/core/math/a_star.h
9898 views
1
/**************************************************************************/
2
/* a_star.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 "core/object/gdvirtual.gen.inc"
34
#include "core/object/ref_counted.h"
35
#include "core/templates/a_hash_map.h"
36
37
/**
38
A* pathfinding algorithm.
39
*/
40
41
class AStar3D : public RefCounted {
42
GDCLASS(AStar3D, RefCounted);
43
friend class AStar2D;
44
45
struct Point {
46
Point() {}
47
48
int64_t id = 0;
49
Vector3 pos;
50
real_t weight_scale = 0;
51
bool enabled = false;
52
53
AHashMap<int64_t, Point *> neighbors = 4u;
54
AHashMap<int64_t, Point *> unlinked_neighbours = 4u;
55
56
// Used for pathfinding.
57
Point *prev_point = nullptr;
58
real_t g_score = 0;
59
real_t f_score = 0;
60
uint64_t open_pass = 0;
61
uint64_t closed_pass = 0;
62
63
// Used for getting closest_point_of_last_pathing_call.
64
real_t abs_g_score = 0;
65
real_t abs_f_score = 0;
66
};
67
68
struct SortPoints {
69
_FORCE_INLINE_ bool operator()(const Point *A, const Point *B) const { // Returns true when the Point A is worse than Point B.
70
if (A->f_score > B->f_score) {
71
return true;
72
} else if (A->f_score < B->f_score) {
73
return false;
74
} else {
75
return A->g_score < B->g_score; // If the f_costs are the same then prioritize the points that are further away from the start.
76
}
77
}
78
};
79
80
struct Segment {
81
Pair<int64_t, int64_t> key;
82
83
enum {
84
NONE = 0,
85
FORWARD = 1,
86
BACKWARD = 2,
87
BIDIRECTIONAL = FORWARD | BACKWARD
88
};
89
unsigned char direction = NONE;
90
91
static uint32_t hash(const Segment &p_seg) {
92
return HashMapHasherDefault::hash(p_seg.key);
93
}
94
bool operator==(const Segment &p_s) const { return key == p_s.key; }
95
96
Segment() {}
97
Segment(int64_t p_from, int64_t p_to) {
98
if (p_from < p_to) {
99
key.first = p_from;
100
key.second = p_to;
101
direction = FORWARD;
102
} else {
103
key.first = p_to;
104
key.second = p_from;
105
direction = BACKWARD;
106
}
107
}
108
};
109
110
mutable int64_t last_free_id = 0;
111
uint64_t pass = 1;
112
113
AHashMap<int64_t, Point *> points;
114
HashSet<Segment, Segment> segments;
115
Point *last_closest_point = nullptr;
116
bool neighbor_filter_enabled = false;
117
118
bool _solve(Point *begin_point, Point *end_point, bool p_allow_partial_path);
119
120
protected:
121
static void _bind_methods();
122
123
virtual real_t _estimate_cost(int64_t p_from_id, int64_t p_end_id);
124
virtual real_t _compute_cost(int64_t p_from_id, int64_t p_to_id);
125
126
GDVIRTUAL2RC(bool, _filter_neighbor, int64_t, int64_t)
127
GDVIRTUAL2RC(real_t, _estimate_cost, int64_t, int64_t)
128
GDVIRTUAL2RC(real_t, _compute_cost, int64_t, int64_t)
129
130
#ifndef DISABLE_DEPRECATED
131
Vector<int64_t> _get_id_path_bind_compat_88047(int64_t p_from_id, int64_t p_to_id);
132
Vector<Vector3> _get_point_path_bind_compat_88047(int64_t p_from_id, int64_t p_to_id);
133
static void _bind_compatibility_methods();
134
#endif
135
136
public:
137
int64_t get_available_point_id() const;
138
139
void add_point(int64_t p_id, const Vector3 &p_pos, real_t p_weight_scale = 1);
140
Vector3 get_point_position(int64_t p_id) const;
141
void set_point_position(int64_t p_id, const Vector3 &p_pos);
142
real_t get_point_weight_scale(int64_t p_id) const;
143
void set_point_weight_scale(int64_t p_id, real_t p_weight_scale);
144
void remove_point(int64_t p_id);
145
bool has_point(int64_t p_id) const;
146
Vector<int64_t> get_point_connections(int64_t p_id);
147
PackedInt64Array get_point_ids();
148
149
bool is_neighbor_filter_enabled() const;
150
void set_neighbor_filter_enabled(bool p_enabled);
151
152
void set_point_disabled(int64_t p_id, bool p_disabled = true);
153
bool is_point_disabled(int64_t p_id) const;
154
155
void connect_points(int64_t p_id, int64_t p_with_id, bool bidirectional = true);
156
void disconnect_points(int64_t p_id, int64_t p_with_id, bool bidirectional = true);
157
bool are_points_connected(int64_t p_id, int64_t p_with_id, bool bidirectional = true) const;
158
159
int64_t get_point_count() const;
160
int64_t get_point_capacity() const;
161
void reserve_space(int64_t p_num_nodes);
162
void clear();
163
164
int64_t get_closest_point(const Vector3 &p_point, bool p_include_disabled = false) const;
165
Vector3 get_closest_position_in_segment(const Vector3 &p_point) const;
166
167
Vector<Vector3> get_point_path(int64_t p_from_id, int64_t p_to_id, bool p_allow_partial_path = false);
168
Vector<int64_t> get_id_path(int64_t p_from_id, int64_t p_to_id, bool p_allow_partial_path = false);
169
170
AStar3D() {}
171
~AStar3D();
172
};
173
174
class AStar2D : public RefCounted {
175
GDCLASS(AStar2D, RefCounted);
176
AStar3D astar;
177
178
bool _solve(AStar3D::Point *begin_point, AStar3D::Point *end_point, bool p_allow_partial_path);
179
180
protected:
181
static void _bind_methods();
182
183
virtual real_t _estimate_cost(int64_t p_from_id, int64_t p_end_id);
184
virtual real_t _compute_cost(int64_t p_from_id, int64_t p_to_id);
185
186
GDVIRTUAL2RC(bool, _filter_neighbor, int64_t, int64_t)
187
GDVIRTUAL2RC(real_t, _estimate_cost, int64_t, int64_t)
188
GDVIRTUAL2RC(real_t, _compute_cost, int64_t, int64_t)
189
190
#ifndef DISABLE_DEPRECATED
191
Vector<int64_t> _get_id_path_bind_compat_88047(int64_t p_from_id, int64_t p_to_id);
192
Vector<Vector2> _get_point_path_bind_compat_88047(int64_t p_from_id, int64_t p_to_id);
193
static void _bind_compatibility_methods();
194
#endif
195
196
public:
197
int64_t get_available_point_id() const;
198
199
void add_point(int64_t p_id, const Vector2 &p_pos, real_t p_weight_scale = 1);
200
Vector2 get_point_position(int64_t p_id) const;
201
void set_point_position(int64_t p_id, const Vector2 &p_pos);
202
real_t get_point_weight_scale(int64_t p_id) const;
203
void set_point_weight_scale(int64_t p_id, real_t p_weight_scale);
204
void remove_point(int64_t p_id);
205
bool has_point(int64_t p_id) const;
206
Vector<int64_t> get_point_connections(int64_t p_id);
207
PackedInt64Array get_point_ids();
208
209
bool is_neighbor_filter_enabled() const;
210
void set_neighbor_filter_enabled(bool p_enabled);
211
212
void set_point_disabled(int64_t p_id, bool p_disabled = true);
213
bool is_point_disabled(int64_t p_id) const;
214
215
void connect_points(int64_t p_id, int64_t p_with_id, bool p_bidirectional = true);
216
void disconnect_points(int64_t p_id, int64_t p_with_id, bool p_bidirectional = true);
217
bool are_points_connected(int64_t p_id, int64_t p_with_id, bool p_bidirectional = true) const;
218
219
int64_t get_point_count() const;
220
int64_t get_point_capacity() const;
221
void reserve_space(int64_t p_num_nodes);
222
void clear();
223
224
int64_t get_closest_point(const Vector2 &p_point, bool p_include_disabled = false) const;
225
Vector2 get_closest_position_in_segment(const Vector2 &p_point) const;
226
227
Vector<Vector2> get_point_path(int64_t p_from_id, int64_t p_to_id, bool p_allow_partial_path = false);
228
Vector<int64_t> get_id_path(int64_t p_from_id, int64_t p_to_id, bool p_allow_partial_path = false);
229
230
AStar2D() {}
231
~AStar2D() {}
232
};
233
234