Debian Codegraph @

core/math/a_star.h

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