Debian Codegraph @

core/math/a_star_grid_2d.h

raw symbols SCIP
1 /**************************************************************************/
2 /* a_star_grid_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 "core/object/gdvirtual.gen.inc"
34 #include "core/object/ref_counted.h"
35 #include "core/templates/local_vector.h"
36
37 class AStarGrid2D : public RefCounted {
38 GDCLASS(AStarGrid2D, RefCounted);
39
40 public:
41 enum DiagonalMode {
42 DIAGONAL_MODE_ALWAYS,
43 DIAGONAL_MODE_NEVER,
44 DIAGONAL_MODE_AT_LEAST_ONE_WALKABLE,
45 DIAGONAL_MODE_ONLY_IF_NO_OBSTACLES,
46 DIAGONAL_MODE_MAX,
47 };
48
49 enum Heuristic {
50 HEURISTIC_EUCLIDEAN,
51 HEURISTIC_MANHATTAN,
52 HEURISTIC_OCTILE,
53 HEURISTIC_CHEBYSHEV,
54 HEURISTIC_MAX,
55 };
56
57 enum CellShape {
58 CELL_SHAPE_SQUARE,
59 CELL_SHAPE_ISOMETRIC_RIGHT,
60 CELL_SHAPE_ISOMETRIC_DOWN,
61 CELL_SHAPE_MAX,
62 };
63
64 private:
65 Rect2i region;
66 Vector2 offset;
67 Size2 cell_size = Size2(1, 1);
68 bool dirty = false;
69 CellShape cell_shape = CELL_SHAPE_SQUARE;
70
71 bool jumping_enabled = false;
72 DiagonalMode diagonal_mode = DIAGONAL_MODE_ALWAYS;
73 Heuristic default_compute_heuristic = HEURISTIC_EUCLIDEAN;
74 Heuristic default_estimate_heuristic = HEURISTIC_EUCLIDEAN;
75
76 struct Point {
77 Vector2i id;
78
79 Vector2 pos;
80 real_t weight_scale = 1.0;
81
82 // Used for pathfinding.
83 Point *prev_point = nullptr;
84 real_t g_score = 0;
85 real_t f_score = 0;
86 uint64_t open_pass = 0;
87 uint64_t closed_pass = 0;
88
89 // Used for getting last_closest_point.
90 real_t abs_g_score = 0;
91 real_t abs_f_score = 0;
92
93 Point() {}
94
95 Point(const Vector2i &p_id, const Vector2 &p_pos) :
96 id(p_id), pos(p_pos) {}
97 };
98
99 struct SortPoints {
100 _FORCE_INLINE_ bool operator()(const Point *A, const Point *B) const { // Returns true when the Point A is worse than Point B.
101 if (A->f_score > B->f_score) {
102 return true;
103 } else if (A->f_score < B->f_score) {
104 return false;
105 } else {
106 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.
107 }
108 }
109 };
110
111 LocalVector<bool> solid_mask;
112 LocalVector<LocalVector<Point>> points;
113 Point *end = nullptr;
114 Point *last_closest_point = nullptr;
115
116 uint64_t pass = 1;
117
118 private: // Internal routines.
119 _FORCE_INLINE_ size_t _to_mask_index(int32_t p_x, int32_t p_y) const {
120 return ((p_y - region.position.y + 1) * (region.size.x + 2)) + p_x - region.position.x + 1;
121 }
122
123 _FORCE_INLINE_ bool _is_walkable(int32_t p_x, int32_t p_y) const {
124 return !solid_mask[_to_mask_index(p_x, p_y)];
125 }
126
127 _FORCE_INLINE_ Point *_get_point(int32_t p_x, int32_t p_y) {
128 if (region.has_point(Vector2i(p_x, p_y))) {
129 return &points[p_y - region.position.y][p_x - region.position.x];
130 }
131 return nullptr;
132 }
133
134 _FORCE_INLINE_ void _set_solid_unchecked(int32_t p_x, int32_t p_y, bool p_solid) {
135 solid_mask[_to_mask_index(p_x, p_y)] = p_solid;
136 }
137
138 _FORCE_INLINE_ void _set_solid_unchecked(const Vector2i &p_id, bool p_solid) {
139 solid_mask[_to_mask_index(p_id.x, p_id.y)] = p_solid;
140 }
141
142 _FORCE_INLINE_ bool _get_solid_unchecked(const Vector2i &p_id) const {
143 return solid_mask[_to_mask_index(p_id.x, p_id.y)];
144 }
145
146 _FORCE_INLINE_ Point *_get_point_unchecked(int32_t p_x, int32_t p_y) {
147 return &points[p_y - region.position.y][p_x - region.position.x];
148 }
149
150 _FORCE_INLINE_ Point *_get_point_unchecked(const Vector2i &p_id) {
151 return &points[p_id.y - region.position.y][p_id.x - region.position.x];
152 }
153
154 _FORCE_INLINE_ const Point *_get_point_unchecked(const Vector2i &p_id) const {
155 return &points[p_id.y - region.position.y][p_id.x - region.position.x];
156 }
157
158 void _get_nbors(Point *p_point, LocalVector<Point *> &r_nbors);
159 Point *_jump(Point *p_from, Point *p_to);
160 bool _solve(Point *p_begin_point, Point *p_end_point, bool p_allow_partial_path);
161 Point *_forced_successor(int32_t p_x, int32_t p_y, int32_t p_dx, int32_t p_dy, bool p_inclusive = false);
162
163 protected:
164 static void _bind_methods();
165
166 virtual real_t _estimate_cost(const Vector2i &p_from_id, const Vector2i &p_end_id);
167 virtual real_t _compute_cost(const Vector2i &p_from_id, const Vector2i &p_to_id);
168
169 GDVIRTUAL2RC(real_t, _estimate_cost, Vector2i, Vector2i)
170 GDVIRTUAL2RC(real_t, _compute_cost, Vector2i, Vector2i)
171
172 #ifndef DISABLE_DEPRECATED
173 TypedArray<Vector2i> _get_id_path_bind_compat_88047(const Vector2i &p_from, const Vector2i &p_to);
174 Vector<Vector2> _get_point_path_bind_compat_88047(const Vector2i &p_from, const Vector2i &p_to);
175 static void _bind_compatibility_methods();
176 #endif
177
178 public:
179 void set_region(const Rect2i &p_region);
180 Rect2i get_region() const;
181
182 void set_size(const Size2i &p_size);
183 Size2i get_size() const;
184
185 void set_offset(const Vector2 &p_offset);
186 Vector2 get_offset() const;
187
188 void set_cell_size(const Size2 &p_cell_size);
189 Size2 get_cell_size() const;
190
191 void set_cell_shape(CellShape p_cell_shape);
192 CellShape get_cell_shape() const;
193
194 void update();
195
196 bool is_in_bounds(int32_t p_x, int32_t p_y) const;
197 bool is_in_boundsv(const Vector2i &p_id) const;
198 bool is_dirty() const;
199
200 void set_jumping_enabled(bool p_enabled);
201 bool is_jumping_enabled() const;
202
203 void set_diagonal_mode(DiagonalMode p_diagonal_mode);
204 DiagonalMode get_diagonal_mode() const;
205
206 void set_default_compute_heuristic(Heuristic p_heuristic);
207 Heuristic get_default_compute_heuristic() const;
208
209 void set_default_estimate_heuristic(Heuristic p_heuristic);
210 Heuristic get_default_estimate_heuristic() const;
211
212 void set_point_solid(const Vector2i &p_id, bool p_solid = true);
213 bool is_point_solid(const Vector2i &p_id) const;
214
215 void set_point_weight_scale(const Vector2i &p_id, real_t p_weight_scale);
216 real_t get_point_weight_scale(const Vector2i &p_id) const;
217
218 void fill_solid_region(const Rect2i &p_region, bool p_solid = true);
219 void fill_weight_scale_region(const Rect2i &p_region, real_t p_weight_scale);
220
221 void clear();
222
223 Vector2 get_point_position(const Vector2i &p_id) const;
224 TypedArray<Dictionary> get_point_data_in_region(const Rect2i &p_region) const;
225 Vector<Vector2> get_point_path(const Vector2i &p_from, const Vector2i &p_to, bool p_allow_partial_path = false);
226 TypedArray<Vector2i> get_id_path(const Vector2i &p_from, const Vector2i &p_to, bool p_allow_partial_path = false);
227 };
228
229 VARIANT_ENUM_CAST(AStarGrid2D::DiagonalMode);
230 VARIANT_ENUM_CAST(AStarGrid2D::Heuristic);
231 VARIANT_ENUM_CAST(AStarGrid2D::CellShape)