Debian Codegraph @

core/math/a_star_grid_2d.cpp

raw symbols SCIP
1 /**************************************************************************/
2 /* a_star_grid_2d.cpp */
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 #include "a_star_grid_2d.h"
32 #include "a_star_grid_2d.compat.inc"
33
34 #include "core/variant/typed_array.h"
35
36 static real_t heuristic_euclidean(const Vector2i &p_from, const Vector2i &p_to) {
37 real_t dx = (real_t)Math::abs(p_to.x - p_from.x);
38 real_t dy = (real_t)Math::abs(p_to.y - p_from.y);
39 return (real_t)Math::sqrt(dx * dx + dy * dy);
40 }
41
42 static real_t heuristic_manhattan(const Vector2i &p_from, const Vector2i &p_to) {
43 real_t dx = (real_t)Math::abs(p_to.x - p_from.x);
44 real_t dy = (real_t)Math::abs(p_to.y - p_from.y);
45 return dx + dy;
46 }
47
48 static real_t heuristic_octile(const Vector2i &p_from, const Vector2i &p_to) {
49 real_t dx = (real_t)Math::abs(p_to.x - p_from.x);
50 real_t dy = (real_t)Math::abs(p_to.y - p_from.y);
51 real_t F = Math::SQRT2 - 1;
52 return (dx < dy) ? F * dx + dy : F * dy + dx;
53 }
54
55 static real_t heuristic_chebyshev(const Vector2i &p_from, const Vector2i &p_to) {
56 real_t dx = (real_t)Math::abs(p_to.x - p_from.x);
57 real_t dy = (real_t)Math::abs(p_to.y - p_from.y);
58 return MAX(dx, dy);
59 }
60
61 static real_t (*heuristics[AStarGrid2D::HEURISTIC_MAX])(const Vector2i &, const Vector2i &) = { heuristic_euclidean, heuristic_manhattan, heuristic_octile, heuristic_chebyshev };
62
63 void AStarGrid2D::set_region(const Rect2i &p_region) {
64 ERR_FAIL_COND(p_region.size.x < 0 || p_region.size.y < 0);
65 if (p_region != region) {
66 region = p_region;
67 dirty = true;
68 }
69 }
70
71 Rect2i AStarGrid2D::get_region() const {
72 return region;
73 }
74
75 void AStarGrid2D::set_size(const Size2i &p_size) {
76 WARN_DEPRECATED_MSG(R"(The "size" property is deprecated, use "region" instead.)");
77 ERR_FAIL_COND(p_size.x < 0 || p_size.y < 0);
78 if (p_size != region.size) {
79 region.size = p_size;
80 dirty = true;
81 }
82 }
83
84 Size2i AStarGrid2D::get_size() const {
85 return region.size;
86 }
87
88 void AStarGrid2D::set_offset(const Vector2 &p_offset) {
89 if (!offset.is_equal_approx(p_offset)) {
90 offset = p_offset;
91 dirty = true;
92 }
93 }
94
95 Vector2 AStarGrid2D::get_offset() const {
96 return offset;
97 }
98
99 void AStarGrid2D::set_cell_size(const Size2 &p_cell_size) {
100 if (!cell_size.is_equal_approx(p_cell_size)) {
101 cell_size = p_cell_size;
102 dirty = true;
103 }
104 }
105
106 Size2 AStarGrid2D::get_cell_size() const {
107 return cell_size;
108 }
109
110 void AStarGrid2D::set_cell_shape(CellShape p_cell_shape) {
111 if (cell_shape == p_cell_shape) {
112 return;
113 }
114
115 ERR_FAIL_INDEX(p_cell_shape, CellShape::CELL_SHAPE_MAX);
116 cell_shape = p_cell_shape;
117 dirty = true;
118 }
119
120 AStarGrid2D::CellShape AStarGrid2D::get_cell_shape() const {
121 return cell_shape;
122 }
123
124 void AStarGrid2D::update() {
125 if (!dirty) {
126 return;
127 }
128
129 points.clear();
130 solid_mask.clear();
131
132 const int32_t end_x = region.get_end().x;
133 const int32_t end_y = region.get_end().y;
134 const Vector2 half_cell_size = cell_size / 2;
135 for (int32_t x = region.position.x; x < end_x + 2; x++) {
136 solid_mask.push_back(true);
137 }
138
139 for (int32_t y = region.position.y; y < end_y; y++) {
140 LocalVector<Point> line;
141 solid_mask.push_back(true);
142 for (int32_t x = region.position.x; x < end_x; x++) {
143 Vector2 v = offset;
144 switch (cell_shape) {
145 case CELL_SHAPE_ISOMETRIC_RIGHT:
146 v += half_cell_size + Vector2(x + y, y - x) * half_cell_size;
147 break;
148 case CELL_SHAPE_ISOMETRIC_DOWN:
149 v += half_cell_size + Vector2(x - y, x + y) * half_cell_size;
150 break;
151 case CELL_SHAPE_SQUARE:
152 v += Vector2(x, y) * cell_size;
153 break;
154 default:
155 break;
156 }
157 line.push_back(Point(Vector2i(x, y), v));
158 solid_mask.push_back(false);
159 }
160 solid_mask.push_back(true);
161 points.push_back(line);
162 }
163
164 for (int32_t x = region.position.x; x < end_x + 2; x++) {
165 solid_mask.push_back(true);
166 }
167
168 dirty = false;
169 }
170
171 bool AStarGrid2D::is_in_bounds(int32_t p_x, int32_t p_y) const {
172 return region.has_point(Vector2i(p_x, p_y));
173 }
174
175 bool AStarGrid2D::is_in_boundsv(const Vector2i &p_id) const {
176 return region.has_point(p_id);
177 }
178
179 bool AStarGrid2D::is_dirty() const {
180 return dirty;
181 }
182
183 void AStarGrid2D::set_jumping_enabled(bool p_enabled) {
184 jumping_enabled = p_enabled;
185 }
186
187 bool AStarGrid2D::is_jumping_enabled() const {
188 return jumping_enabled;
189 }
190
191 void AStarGrid2D::set_diagonal_mode(DiagonalMode p_diagonal_mode) {
192 ERR_FAIL_INDEX((int)p_diagonal_mode, (int)DIAGONAL_MODE_MAX);
193 diagonal_mode = p_diagonal_mode;
194 }
195
196 AStarGrid2D::DiagonalMode AStarGrid2D::get_diagonal_mode() const {
197 return diagonal_mode;
198 }
199
200 void AStarGrid2D::set_default_compute_heuristic(Heuristic p_heuristic) {
201 ERR_FAIL_INDEX((int)p_heuristic, (int)HEURISTIC_MAX);
202 default_compute_heuristic = p_heuristic;
203 }
204
205 AStarGrid2D::Heuristic AStarGrid2D::get_default_compute_heuristic() const {
206 return default_compute_heuristic;
207 }
208
209 void AStarGrid2D::set_default_estimate_heuristic(Heuristic p_heuristic) {
210 ERR_FAIL_INDEX((int)p_heuristic, (int)HEURISTIC_MAX);
211 default_estimate_heuristic = p_heuristic;
212 }
213
214 AStarGrid2D::Heuristic AStarGrid2D::get_default_estimate_heuristic() const {
215 return default_estimate_heuristic;
216 }
217
218 void AStarGrid2D::set_point_solid(const Vector2i &p_id, bool p_solid) {
219 ERR_FAIL_COND_MSG(dirty, "Grid is not initialized. Call the update method.");
220 ERR_FAIL_COND_MSG(!is_in_boundsv(p_id), vformat("Can't set if point is disabled. Point %s out of bounds %s.", p_id, region));
221 _set_solid_unchecked(p_id, p_solid);
222 }
223
224 bool AStarGrid2D::is_point_solid(const Vector2i &p_id) const {
225 ERR_FAIL_COND_V_MSG(dirty, false, "Grid is not initialized. Call the update method.");
226 ERR_FAIL_COND_V_MSG(!is_in_boundsv(p_id), false, vformat("Can't get if point is disabled. Point %s out of bounds %s.", p_id, region));
227 return _get_solid_unchecked(p_id);
228 }
229
230 void AStarGrid2D::set_point_weight_scale(const Vector2i &p_id, real_t p_weight_scale) {
231 ERR_FAIL_COND_MSG(dirty, "Grid is not initialized. Call the update method.");
232 ERR_FAIL_COND_MSG(!is_in_boundsv(p_id), vformat("Can't set point's weight scale. Point %s out of bounds %s.", p_id, region));
233 ERR_FAIL_COND_MSG(p_weight_scale < 0.0, vformat("Can't set point's weight scale less than 0.0: %f.", p_weight_scale));
234 _get_point_unchecked(p_id)->weight_scale = p_weight_scale;
235 }
236
237 real_t AStarGrid2D::get_point_weight_scale(const Vector2i &p_id) const {
238 ERR_FAIL_COND_V_MSG(dirty, 0, "Grid is not initialized. Call the update method.");
239 ERR_FAIL_COND_V_MSG(!is_in_boundsv(p_id), 0, vformat("Can't get point's weight scale. Point %s out of bounds %s.", p_id, region));
240 return _get_point_unchecked(p_id)->weight_scale;
241 }
242
243 void AStarGrid2D::fill_solid_region(const Rect2i &p_region, bool p_solid) {
244 ERR_FAIL_COND_MSG(dirty, "Grid is not initialized. Call the update method.");
245
246 const Rect2i safe_region = p_region.intersection(region);
247 const int32_t end_x = safe_region.get_end().x;
248 const int32_t end_y = safe_region.get_end().y;
249
250 for (int32_t y = safe_region.position.y; y < end_y; y++) {
251 for (int32_t x = safe_region.position.x; x < end_x; x++) {
252 _set_solid_unchecked(x, y, p_solid);
253 }
254 }
255 }
256
257 void AStarGrid2D::fill_weight_scale_region(const Rect2i &p_region, real_t p_weight_scale) {
258 ERR_FAIL_COND_MSG(dirty, "Grid is not initialized. Call the update method.");
259 ERR_FAIL_COND_MSG(p_weight_scale < 0.0, vformat("Can't set point's weight scale less than 0.0: %f.", p_weight_scale));
260
261 const Rect2i safe_region = p_region.intersection(region);
262 const int32_t end_x = safe_region.get_end().x;
263 const int32_t end_y = safe_region.get_end().y;
264
265 for (int32_t y = safe_region.position.y; y < end_y; y++) {
266 for (int32_t x = safe_region.position.x; x < end_x; x++) {
267 _get_point_unchecked(x, y)->weight_scale = p_weight_scale;
268 }
269 }
270 }
271
272 AStarGrid2D::Point *AStarGrid2D::_jump(Point *p_from, Point *p_to) {
273 int32_t from_x = p_from->id.x;
274 int32_t from_y = p_from->id.y;
275
276 int32_t to_x = p_to->id.x;
277 int32_t to_y = p_to->id.y;
278
279 int32_t dx = to_x - from_x;
280 int32_t dy = to_y - from_y;
281
282 if (diagonal_mode == DIAGONAL_MODE_ALWAYS || diagonal_mode == DIAGONAL_MODE_AT_LEAST_ONE_WALKABLE) {
283 if (dx == 0 || dy == 0) {
284 return _forced_successor(to_x, to_y, dx, dy);
285 }
286
287 while (_is_walkable(to_x, to_y) && (diagonal_mode == DIAGONAL_MODE_ALWAYS || _is_walkable(to_x, to_y - dy) || _is_walkable(to_x - dx, to_y))) {
288 if (end->id.x == to_x && end->id.y == to_y) {
289 return end;
290 }
291
292 if ((_is_walkable(to_x - dx, to_y + dy) && !_is_walkable(to_x - dx, to_y)) || (_is_walkable(to_x + dx, to_y - dy) && !_is_walkable(to_x, to_y - dy))) {
293 return _get_point_unchecked(to_x, to_y);
294 }
295
296 if (_forced_successor(to_x + dx, to_y, dx, 0) != nullptr || _forced_successor(to_x, to_y + dy, 0, dy) != nullptr) {
297 return _get_point_unchecked(to_x, to_y);
298 }
299
300 to_x += dx;
301 to_y += dy;
302 }
303
304 } else if (diagonal_mode == DIAGONAL_MODE_ONLY_IF_NO_OBSTACLES) {
305 if (dx == 0 || dy == 0) {
306 return _forced_successor(from_x, from_y, dx, dy, true);
307 }
308
309 while (_is_walkable(to_x, to_y) && _is_walkable(to_x, to_y - dy) && _is_walkable(to_x - dx, to_y)) {
310 if (end->id.x == to_x && end->id.y == to_y) {
311 return end;
312 }
313
314 if ((_is_walkable(to_x + dx, to_y + dy) && !_is_walkable(to_x, to_y + dy)) || !_is_walkable(to_x + dx, to_y)) {
315 return _get_point_unchecked(to_x, to_y);
316 }
317
318 if (_forced_successor(to_x, to_y, dx, 0) != nullptr || _forced_successor(to_x, to_y, 0, dy) != nullptr) {
319 return _get_point_unchecked(to_x, to_y);
320 }
321
322 to_x += dx;
323 to_y += dy;
324 }
325
326 } else { // DIAGONAL_MODE_NEVER
327 if (dy == 0) {
328 return _forced_successor(from_x, from_y, dx, 0, true);
329 }
330
331 while (_is_walkable(to_x, to_y)) {
332 if (end->id.x == to_x && end->id.y == to_y) {
333 return end;
334 }
335
336 if ((_is_walkable(to_x - 1, to_y) && !_is_walkable(to_x - 1, to_y - dy)) || (_is_walkable(to_x + 1, to_y) && !_is_walkable(to_x + 1, to_y - dy))) {
337 return _get_point_unchecked(to_x, to_y);
338 }
339
340 if (_forced_successor(to_x, to_y, 1, 0, true) != nullptr || _forced_successor(to_x, to_y, -1, 0, true) != nullptr) {
341 return _get_point_unchecked(to_x, to_y);
342 }
343
344 to_y += dy;
345 }
346 }
347
348 return nullptr;
349 }
350
351 AStarGrid2D::Point *AStarGrid2D::_forced_successor(int32_t p_x, int32_t p_y, int32_t p_dx, int32_t p_dy, bool p_inclusive) {
352 // Remembering previous results can improve performance.
353 bool l_prev = false, r_prev = false, l = false, r = false;
354
355 int32_t o_x = p_x, o_y = p_y;
356 if (p_inclusive) {
357 o_x += p_dx;
358 o_y += p_dy;
359 }
360
361 int32_t l_x = p_x - p_dy, l_y = p_y - p_dx;
362 int32_t r_x = p_x + p_dy, r_y = p_y + p_dx;
363
364 while (_is_walkable(o_x, o_y)) {
365 if (end->id.x == o_x && end->id.y == o_y) {
366 return end;
367 }
368
369 l_prev = l || _is_walkable(l_x, l_y);
370 r_prev = r || _is_walkable(r_x, r_y);
371
372 l_x += p_dx;
373 l_y += p_dy;
374 r_x += p_dx;
375 r_y += p_dy;
376
377 l = _is_walkable(l_x, l_y);
378 r = _is_walkable(r_x, r_y);
379
380 if ((l && !l_prev) || (r && !r_prev)) {
381 return _get_point_unchecked(o_x, o_y);
382 }
383
384 o_x += p_dx;
385 o_y += p_dy;
386 }
387 return nullptr;
388 }
389
390 void AStarGrid2D::_get_nbors(Point *p_point, LocalVector<Point *> &r_nbors) {
391 bool ts0 = false, td0 = false,
392 ts1 = false, td1 = false,
393 ts2 = false, td2 = false,
394 ts3 = false, td3 = false;
395
396 Point *left = nullptr;
397 Point *right = nullptr;
398 Point *top = nullptr;
399 Point *bottom = nullptr;
400
401 Point *top_left = nullptr;
402 Point *top_right = nullptr;
403 Point *bottom_left = nullptr;
404 Point *bottom_right = nullptr;
405
406 {
407 bool has_left = false;
408 bool has_right = false;
409
410 if (p_point->id.x - 1 >= region.position.x) {
411 left = _get_point_unchecked(p_point->id.x - 1, p_point->id.y);
412 has_left = true;
413 }
414 if (p_point->id.x + 1 < region.position.x + region.size.width) {
415 right = _get_point_unchecked(p_point->id.x + 1, p_point->id.y);
416 has_right = true;
417 }
418 if (p_point->id.y - 1 >= region.position.y) {
419 top = _get_point_unchecked(p_point->id.x, p_point->id.y - 1);
420 if (has_left) {
421 top_left = _get_point_unchecked(p_point->id.x - 1, p_point->id.y - 1);
422 }
423 if (has_right) {
424 top_right = _get_point_unchecked(p_point->id.x + 1, p_point->id.y - 1);
425 }
426 }
427 if (p_point->id.y + 1 < region.position.y + region.size.height) {
428 bottom = _get_point_unchecked(p_point->id.x, p_point->id.y + 1);
429 if (has_left) {
430 bottom_left = _get_point_unchecked(p_point->id.x - 1, p_point->id.y + 1);
431 }
432 if (has_right) {
433 bottom_right = _get_point_unchecked(p_point->id.x + 1, p_point->id.y + 1);
434 }
435 }
436 }
437
438 if (top && !_get_solid_unchecked(top->id)) {
439 r_nbors.push_back(top);
440 ts0 = true;
441 }
442 if (right && !_get_solid_unchecked(right->id)) {
443 r_nbors.push_back(right);
444 ts1 = true;
445 }
446 if (bottom && !_get_solid_unchecked(bottom->id)) {
447 r_nbors.push_back(bottom);
448 ts2 = true;
449 }
450 if (left && !_get_solid_unchecked(left->id)) {
451 r_nbors.push_back(left);
452 ts3 = true;
453 }
454
455 switch (diagonal_mode) {
456 case DIAGONAL_MODE_ALWAYS: {
457 td0 = true;
458 td1 = true;
459 td2 = true;
460 td3 = true;
461 } break;
462 case DIAGONAL_MODE_NEVER: {
463 } break;
464 case DIAGONAL_MODE_AT_LEAST_ONE_WALKABLE: {
465 td0 = ts3 || ts0;
466 td1 = ts0 || ts1;
467 td2 = ts1 || ts2;
468 td3 = ts2 || ts3;
469 } break;
470 case DIAGONAL_MODE_ONLY_IF_NO_OBSTACLES: {
471 td0 = ts3 && ts0;
472 td1 = ts0 && ts1;
473 td2 = ts1 && ts2;
474 td3 = ts2 && ts3;
475 } break;
476 default:
477 break;
478 }
479
480 if (td0 && (top_left && !_get_solid_unchecked(top_left->id))) {
481 r_nbors.push_back(top_left);
482 }
483 if (td1 && (top_right && !_get_solid_unchecked(top_right->id))) {
484 r_nbors.push_back(top_right);
485 }
486 if (td2 && (bottom_right && !_get_solid_unchecked(bottom_right->id))) {
487 r_nbors.push_back(bottom_right);
488 }
489 if (td3 && (bottom_left && !_get_solid_unchecked(bottom_left->id))) {
490 r_nbors.push_back(bottom_left);
491 }
492 }
493
494 bool AStarGrid2D::_solve(Point *p_begin_point, Point *p_end_point, bool p_allow_partial_path) {
495 last_closest_point = nullptr;
496 pass++;
497
498 if (_get_solid_unchecked(p_begin_point->id)) {
499 return false;
500 }
501 if (p_begin_point == p_end_point) {
502 return true;
503 }
504 if (_get_solid_unchecked(p_end_point->id) && !p_allow_partial_path) {
505 return false;
506 }
507
508 bool found_route = false;
509
510 LocalVector<Point *> open_list;
511 SortArray<Point *, SortPoints> sorter;
512 LocalVector<Point *> nbors;
513
514 p_begin_point->g_score = 0;
515 p_begin_point->f_score = _estimate_cost(p_begin_point->id, p_end_point->id);
516 p_begin_point->abs_g_score = 0;
517 p_begin_point->abs_f_score = _estimate_cost(p_begin_point->id, p_end_point->id);
518 open_list.push_back(p_begin_point);
519 end = p_end_point;
520
521 while (!open_list.is_empty()) {
522 Point *p = open_list[0]; // The currently processed point.
523
524 // Find point closer to end_point, or same distance to end_point but closer to begin_point.
525 if (last_closest_point == nullptr || last_closest_point->abs_f_score > p->abs_f_score || (last_closest_point->abs_f_score >= p->abs_f_score && last_closest_point->abs_g_score > p->abs_g_score)) {
526 last_closest_point = p;
527 }
528
529 if (p == p_end_point) {
530 found_route = true;
531 break;
532 }
533
534 sorter.pop_heap(0, open_list.size(), open_list.ptr()); // Remove the current point from the open list.
535 open_list.remove_at(open_list.size() - 1);
536 p->closed_pass = pass; // Mark the point as closed.
537
538 nbors.clear();
539 _get_nbors(p, nbors);
540
541 for (Point *e : nbors) {
542 real_t weight_scale = 1.0;
543
544 if (jumping_enabled) {
545 // TODO: Make it works with weight_scale.
546 e = _jump(p, e);
547 if (!e || e->closed_pass == pass) {
548 continue;
549 }
550 } else {
551 if (_get_solid_unchecked(e->id) || e->closed_pass == pass) {
552 continue;
553 }
554 weight_scale = e->weight_scale;
555 }
556
557 real_t tentative_g_score = p->g_score + _compute_cost(p->id, e->id) * weight_scale;
558 bool new_point = false;
559
560 if (e->open_pass != pass) { // The point wasn't inside the open list.
561 e->open_pass = pass;
562 open_list.push_back(e);
563 new_point = true;
564 } else if (tentative_g_score >= e->g_score) { // The new path is worse than the previous.
565 continue;
566 }
567
568 e->prev_point = p;
569 e->g_score = tentative_g_score;
570 e->f_score = e->g_score + _estimate_cost(e->id, p_end_point->id);
571
572 e->abs_g_score = tentative_g_score;
573 e->abs_f_score = e->f_score - e->g_score;
574
575 if (new_point) { // The position of the new points is already known.
576 sorter.push_heap(0, open_list.size() - 1, 0, e, open_list.ptr());
577 } else {
578 sorter.push_heap(0, open_list.find(e), 0, e, open_list.ptr());
579 }
580 }
581 }
582
583 return found_route;
584 }
585
586 real_t AStarGrid2D::_estimate_cost(const Vector2i &p_from_id, const Vector2i &p_end_id) {
587 real_t scost;
588 if (GDVIRTUAL_CALL(_estimate_cost, p_from_id, p_end_id, scost)) {
589 return scost;
590 }
591 return heuristics[default_estimate_heuristic](p_from_id, p_end_id);
592 }
593
594 real_t AStarGrid2D::_compute_cost(const Vector2i &p_from_id, const Vector2i &p_to_id) {
595 real_t scost;
596 if (GDVIRTUAL_CALL(_compute_cost, p_from_id, p_to_id, scost)) {
597 return scost;
598 }
599 return heuristics[default_compute_heuristic](p_from_id, p_to_id);
600 }
601
602 void AStarGrid2D::clear() {
603 points.clear();
604 region = Rect2i();
605 }
606
607 Vector2 AStarGrid2D::get_point_position(const Vector2i &p_id) const {
608 ERR_FAIL_COND_V_MSG(dirty, Vector2(), "Grid is not initialized. Call the update method.");
609 ERR_FAIL_COND_V_MSG(!is_in_boundsv(p_id), Vector2(), vformat("Can't get point's position. Point %s out of bounds %s.", p_id, region));
610 return _get_point_unchecked(p_id)->pos;
611 }
612
613 TypedArray<Dictionary> AStarGrid2D::get_point_data_in_region(const Rect2i &p_region) const {
614 ERR_FAIL_COND_V_MSG(dirty, TypedArray<Dictionary>(), "Grid is not initialized. Call the update method.");
615 const Rect2i inter_region = region.intersection(p_region);
616
617 const int32_t start_x = inter_region.position.x - region.position.x;
618 const int32_t start_y = inter_region.position.y - region.position.y;
619 const int32_t end_x = inter_region.get_end().x - region.position.x;
620 const int32_t end_y = inter_region.get_end().y - region.position.y;
621
622 TypedArray<Dictionary> data;
623
624 for (int32_t y = start_y; y < end_y; y++) {
625 for (int32_t x = start_x; x < end_x; x++) {
626 const Point &p = points[y][x];
627
628 Dictionary dict;
629 dict["id"] = p.id;
630 dict["position"] = p.pos;
631 dict["solid"] = _get_solid_unchecked(p.id);
632 dict["weight_scale"] = p.weight_scale;
633 data.push_back(dict);
634 }
635 }
636
637 return data;
638 }
639
640 Vector<Vector2> AStarGrid2D::get_point_path(const Vector2i &p_from_id, const Vector2i &p_to_id, bool p_allow_partial_path) {
641 ERR_FAIL_COND_V_MSG(dirty, Vector<Vector2>(), "Grid is not initialized. Call the update method.");
642 ERR_FAIL_COND_V_MSG(!is_in_boundsv(p_from_id), Vector<Vector2>(), vformat("Can't get id path. Point %s out of bounds %s.", p_from_id, region));
643 ERR_FAIL_COND_V_MSG(!is_in_boundsv(p_to_id), Vector<Vector2>(), vformat("Can't get id path. Point %s out of bounds %s.", p_to_id, region));
644
645 Point *begin_point = _get_point(p_from_id.x, p_from_id.y);
646 Point *end_point = _get_point(p_to_id.x, p_to_id.y);
647
648 bool found_route = _solve(begin_point, end_point, p_allow_partial_path);
649 if (!found_route) {
650 if (!p_allow_partial_path || last_closest_point == nullptr) {
651 return Vector<Vector2>();
652 }
653
654 // Use closest point instead.
655 end_point = last_closest_point;
656 }
657
658 Point *p = end_point;
659 int32_t pc = 1;
660 while (p != begin_point) {
661 pc++;
662 p = p->prev_point;
663 }
664
665 Vector<Vector2> path;
666 path.resize(pc);
667
668 {
669 Vector2 *w = path.ptrw();
670
671 p = end_point;
672 int32_t idx = pc - 1;
673 while (p != begin_point) {
674 w[idx--] = p->pos;
675 p = p->prev_point;
676 }
677
678 w[0] = p->pos;
679 }
680
681 return path;
682 }
683
684 TypedArray<Vector2i> AStarGrid2D::get_id_path(const Vector2i &p_from_id, const Vector2i &p_to_id, bool p_allow_partial_path) {
685 ERR_FAIL_COND_V_MSG(dirty, TypedArray<Vector2i>(), "Grid is not initialized. Call the update method.");
686 ERR_FAIL_COND_V_MSG(!is_in_boundsv(p_from_id), TypedArray<Vector2i>(), vformat("Can't get id path. Point %s out of bounds %s.", p_from_id, region));
687 ERR_FAIL_COND_V_MSG(!is_in_boundsv(p_to_id), TypedArray<Vector2i>(), vformat("Can't get id path. Point %s out of bounds %s.", p_to_id, region));
688
689 Point *begin_point = _get_point(p_from_id.x, p_from_id.y);
690 Point *end_point = _get_point(p_to_id.x, p_to_id.y);
691
692 bool found_route = _solve(begin_point, end_point, p_allow_partial_path);
693 if (!found_route) {
694 if (!p_allow_partial_path || last_closest_point == nullptr) {
695 return TypedArray<Vector2i>();
696 }
697
698 // Use closest point instead.
699 end_point = last_closest_point;
700 }
701
702 Point *p = end_point;
703 int32_t pc = 1;
704 while (p != begin_point) {
705 pc++;
706 p = p->prev_point;
707 }
708
709 TypedArray<Vector2i> path;
710 path.resize(pc);
711
712 {
713 p = end_point;
714 int32_t idx = pc - 1;
715 while (p != begin_point) {
716 path[idx--] = p->id;
717 p = p->prev_point;
718 }
719
720 path[0] = p->id;
721 }
722
723 return path;
724 }
725
726 void AStarGrid2D::_bind_methods() {
727 ClassDB::bind_method(D_METHOD("set_region", "region"), &AStarGrid2D::set_region);
728 ClassDB::bind_method(D_METHOD("get_region"), &AStarGrid2D::get_region);
729 ClassDB::bind_method(D_METHOD("set_size", "size"), &AStarGrid2D::set_size);
730 ClassDB::bind_method(D_METHOD("get_size"), &AStarGrid2D::get_size);
731 ClassDB::bind_method(D_METHOD("set_offset", "offset"), &AStarGrid2D::set_offset);
732 ClassDB::bind_method(D_METHOD("get_offset"), &AStarGrid2D::get_offset);
733 ClassDB::bind_method(D_METHOD("set_cell_size", "cell_size"), &AStarGrid2D::set_cell_size);
734 ClassDB::bind_method(D_METHOD("get_cell_size"), &AStarGrid2D::get_cell_size);
735 ClassDB::bind_method(D_METHOD("set_cell_shape", "cell_shape"), &AStarGrid2D::set_cell_shape);
736 ClassDB::bind_method(D_METHOD("get_cell_shape"), &AStarGrid2D::get_cell_shape);
737 ClassDB::bind_method(D_METHOD("is_in_bounds", "x", "y"), &AStarGrid2D::is_in_bounds);
738 ClassDB::bind_method(D_METHOD("is_in_boundsv", "id"), &AStarGrid2D::is_in_boundsv);
739 ClassDB::bind_method(D_METHOD("is_dirty"), &AStarGrid2D::is_dirty);
740 ClassDB::bind_method(D_METHOD("update"), &AStarGrid2D::update);
741 ClassDB::bind_method(D_METHOD("set_jumping_enabled", "enabled"), &AStarGrid2D::set_jumping_enabled);
742 ClassDB::bind_method(D_METHOD("is_jumping_enabled"), &AStarGrid2D::is_jumping_enabled);
743 ClassDB::bind_method(D_METHOD("set_diagonal_mode", "mode"), &AStarGrid2D::set_diagonal_mode);
744 ClassDB::bind_method(D_METHOD("get_diagonal_mode"), &AStarGrid2D::get_diagonal_mode);
745 ClassDB::bind_method(D_METHOD("set_default_compute_heuristic", "heuristic"), &AStarGrid2D::set_default_compute_heuristic);
746 ClassDB::bind_method(D_METHOD("get_default_compute_heuristic"), &AStarGrid2D::get_default_compute_heuristic);
747 ClassDB::bind_method(D_METHOD("set_default_estimate_heuristic", "heuristic"), &AStarGrid2D::set_default_estimate_heuristic);
748 ClassDB::bind_method(D_METHOD("get_default_estimate_heuristic"), &AStarGrid2D::get_default_estimate_heuristic);
749 ClassDB::bind_method(D_METHOD("set_point_solid", "id", "solid"), &AStarGrid2D::set_point_solid, DEFVAL(true));
750 ClassDB::bind_method(D_METHOD("is_point_solid", "id"), &AStarGrid2D::is_point_solid);
751 ClassDB::bind_method(D_METHOD("set_point_weight_scale", "id", "weight_scale"), &AStarGrid2D::set_point_weight_scale);
752 ClassDB::bind_method(D_METHOD("get_point_weight_scale", "id"), &AStarGrid2D::get_point_weight_scale);
753 ClassDB::bind_method(D_METHOD("fill_solid_region", "region", "solid"), &AStarGrid2D::fill_solid_region, DEFVAL(true));
754 ClassDB::bind_method(D_METHOD("fill_weight_scale_region", "region", "weight_scale"), &AStarGrid2D::fill_weight_scale_region);
755 ClassDB::bind_method(D_METHOD("clear"), &AStarGrid2D::clear);
756
757 ClassDB::bind_method(D_METHOD("get_point_position", "id"), &AStarGrid2D::get_point_position);
758 ClassDB::bind_method(D_METHOD("get_point_data_in_region", "region"), &AStarGrid2D::get_point_data_in_region);
759 ClassDB::bind_method(D_METHOD("get_point_path", "from_id", "to_id", "allow_partial_path"), &AStarGrid2D::get_point_path, DEFVAL(false));
760 ClassDB::bind_method(D_METHOD("get_id_path", "from_id", "to_id", "allow_partial_path"), &AStarGrid2D::get_id_path, DEFVAL(false));
761
762 GDVIRTUAL_BIND(_estimate_cost, "from_id", "end_id")
763 GDVIRTUAL_BIND(_compute_cost, "from_id", "to_id")
764
765 ADD_PROPERTY(PropertyInfo(Variant::RECT2I, "region"), "set_region", "get_region");
766 ADD_PROPERTY(PropertyInfo(Variant::VECTOR2I, "size"), "set_size", "get_size");
767 ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "offset"), "set_offset", "get_offset");
768 ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "cell_size"), "set_cell_size", "get_cell_size");
769 ADD_PROPERTY(PropertyInfo(Variant::INT, "cell_shape", PROPERTY_HINT_ENUM, "Square,IsometricRight,IsometricDown"), "set_cell_shape", "get_cell_shape");
770
771 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "jumping_enabled"), "set_jumping_enabled", "is_jumping_enabled");
772 ADD_PROPERTY(PropertyInfo(Variant::INT, "default_compute_heuristic", PROPERTY_HINT_ENUM, "Euclidean,Manhattan,Octile,Chebyshev"), "set_default_compute_heuristic", "get_default_compute_heuristic");
773 ADD_PROPERTY(PropertyInfo(Variant::INT, "default_estimate_heuristic", PROPERTY_HINT_ENUM, "Euclidean,Manhattan,Octile,Chebyshev"), "set_default_estimate_heuristic", "get_default_estimate_heuristic");
774 ADD_PROPERTY(PropertyInfo(Variant::INT, "diagonal_mode", PROPERTY_HINT_ENUM, "Always,Never,At Least One Walkable,Only If No Obstacles"), "set_diagonal_mode", "get_diagonal_mode");
775
776 BIND_ENUM_CONSTANT(HEURISTIC_EUCLIDEAN);
777 BIND_ENUM_CONSTANT(HEURISTIC_MANHATTAN);
778 BIND_ENUM_CONSTANT(HEURISTIC_OCTILE);
779 BIND_ENUM_CONSTANT(HEURISTIC_CHEBYSHEV);
780 BIND_ENUM_CONSTANT(HEURISTIC_MAX);
781
782 BIND_ENUM_CONSTANT(DIAGONAL_MODE_ALWAYS);
783 BIND_ENUM_CONSTANT(DIAGONAL_MODE_NEVER);
784 BIND_ENUM_CONSTANT(DIAGONAL_MODE_AT_LEAST_ONE_WALKABLE);
785 BIND_ENUM_CONSTANT(DIAGONAL_MODE_ONLY_IF_NO_OBSTACLES);
786 BIND_ENUM_CONSTANT(DIAGONAL_MODE_MAX);
787
788 BIND_ENUM_CONSTANT(CELL_SHAPE_SQUARE);
789 BIND_ENUM_CONSTANT(CELL_SHAPE_ISOMETRIC_RIGHT);
790 BIND_ENUM_CONSTANT(CELL_SHAPE_ISOMETRIC_DOWN);
791 BIND_ENUM_CONSTANT(CELL_SHAPE_MAX);
792 }