Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/core/math/a_star_grid_2d.cpp
9896 views
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_end_point->id) && !p_allow_partial_path) {
499
return false;
500
}
501
502
bool found_route = false;
503
504
LocalVector<Point *> open_list;
505
SortArray<Point *, SortPoints> sorter;
506
LocalVector<Point *> nbors;
507
508
p_begin_point->g_score = 0;
509
p_begin_point->f_score = _estimate_cost(p_begin_point->id, p_end_point->id);
510
p_begin_point->abs_g_score = 0;
511
p_begin_point->abs_f_score = _estimate_cost(p_begin_point->id, p_end_point->id);
512
open_list.push_back(p_begin_point);
513
end = p_end_point;
514
515
while (!open_list.is_empty()) {
516
Point *p = open_list[0]; // The currently processed point.
517
518
// Find point closer to end_point, or same distance to end_point but closer to begin_point.
519
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)) {
520
last_closest_point = p;
521
}
522
523
if (p == p_end_point) {
524
found_route = true;
525
break;
526
}
527
528
sorter.pop_heap(0, open_list.size(), open_list.ptr()); // Remove the current point from the open list.
529
open_list.remove_at(open_list.size() - 1);
530
p->closed_pass = pass; // Mark the point as closed.
531
532
nbors.clear();
533
_get_nbors(p, nbors);
534
535
for (Point *e : nbors) {
536
real_t weight_scale = 1.0;
537
538
if (jumping_enabled) {
539
// TODO: Make it works with weight_scale.
540
e = _jump(p, e);
541
if (!e || e->closed_pass == pass) {
542
continue;
543
}
544
} else {
545
if (_get_solid_unchecked(e->id) || e->closed_pass == pass) {
546
continue;
547
}
548
weight_scale = e->weight_scale;
549
}
550
551
real_t tentative_g_score = p->g_score + _compute_cost(p->id, e->id) * weight_scale;
552
bool new_point = false;
553
554
if (e->open_pass != pass) { // The point wasn't inside the open list.
555
e->open_pass = pass;
556
open_list.push_back(e);
557
new_point = true;
558
} else if (tentative_g_score >= e->g_score) { // The new path is worse than the previous.
559
continue;
560
}
561
562
e->prev_point = p;
563
e->g_score = tentative_g_score;
564
e->f_score = e->g_score + _estimate_cost(e->id, p_end_point->id);
565
566
e->abs_g_score = tentative_g_score;
567
e->abs_f_score = e->f_score - e->g_score;
568
569
if (new_point) { // The position of the new points is already known.
570
sorter.push_heap(0, open_list.size() - 1, 0, e, open_list.ptr());
571
} else {
572
sorter.push_heap(0, open_list.find(e), 0, e, open_list.ptr());
573
}
574
}
575
}
576
577
return found_route;
578
}
579
580
real_t AStarGrid2D::_estimate_cost(const Vector2i &p_from_id, const Vector2i &p_end_id) {
581
real_t scost;
582
if (GDVIRTUAL_CALL(_estimate_cost, p_from_id, p_end_id, scost)) {
583
return scost;
584
}
585
return heuristics[default_estimate_heuristic](p_from_id, p_end_id);
586
}
587
588
real_t AStarGrid2D::_compute_cost(const Vector2i &p_from_id, const Vector2i &p_to_id) {
589
real_t scost;
590
if (GDVIRTUAL_CALL(_compute_cost, p_from_id, p_to_id, scost)) {
591
return scost;
592
}
593
return heuristics[default_compute_heuristic](p_from_id, p_to_id);
594
}
595
596
void AStarGrid2D::clear() {
597
points.clear();
598
region = Rect2i();
599
}
600
601
Vector2 AStarGrid2D::get_point_position(const Vector2i &p_id) const {
602
ERR_FAIL_COND_V_MSG(dirty, Vector2(), "Grid is not initialized. Call the update method.");
603
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));
604
return _get_point_unchecked(p_id)->pos;
605
}
606
607
TypedArray<Dictionary> AStarGrid2D::get_point_data_in_region(const Rect2i &p_region) const {
608
ERR_FAIL_COND_V_MSG(dirty, TypedArray<Dictionary>(), "Grid is not initialized. Call the update method.");
609
const Rect2i inter_region = region.intersection(p_region);
610
611
const int32_t start_x = inter_region.position.x - region.position.x;
612
const int32_t start_y = inter_region.position.y - region.position.y;
613
const int32_t end_x = inter_region.get_end().x - region.position.x;
614
const int32_t end_y = inter_region.get_end().y - region.position.y;
615
616
TypedArray<Dictionary> data;
617
618
for (int32_t y = start_y; y < end_y; y++) {
619
for (int32_t x = start_x; x < end_x; x++) {
620
const Point &p = points[y][x];
621
622
Dictionary dict;
623
dict["id"] = p.id;
624
dict["position"] = p.pos;
625
dict["solid"] = _get_solid_unchecked(p.id);
626
dict["weight_scale"] = p.weight_scale;
627
data.push_back(dict);
628
}
629
}
630
631
return data;
632
}
633
634
Vector<Vector2> AStarGrid2D::get_point_path(const Vector2i &p_from_id, const Vector2i &p_to_id, bool p_allow_partial_path) {
635
ERR_FAIL_COND_V_MSG(dirty, Vector<Vector2>(), "Grid is not initialized. Call the update method.");
636
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));
637
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));
638
639
Point *a = _get_point(p_from_id.x, p_from_id.y);
640
Point *b = _get_point(p_to_id.x, p_to_id.y);
641
642
if (a == b) {
643
Vector<Vector2> ret;
644
ret.push_back(a->pos);
645
return ret;
646
}
647
648
Point *begin_point = a;
649
Point *end_point = b;
650
651
bool found_route = _solve(begin_point, end_point, p_allow_partial_path);
652
if (!found_route) {
653
if (!p_allow_partial_path || last_closest_point == nullptr) {
654
return Vector<Vector2>();
655
}
656
657
// Use closest point instead.
658
end_point = last_closest_point;
659
}
660
661
Point *p = end_point;
662
int32_t pc = 1;
663
while (p != begin_point) {
664
pc++;
665
p = p->prev_point;
666
}
667
668
Vector<Vector2> path;
669
path.resize(pc);
670
671
{
672
Vector2 *w = path.ptrw();
673
674
p = end_point;
675
int32_t idx = pc - 1;
676
while (p != begin_point) {
677
w[idx--] = p->pos;
678
p = p->prev_point;
679
}
680
681
w[0] = p->pos;
682
}
683
684
return path;
685
}
686
687
TypedArray<Vector2i> AStarGrid2D::get_id_path(const Vector2i &p_from_id, const Vector2i &p_to_id, bool p_allow_partial_path) {
688
ERR_FAIL_COND_V_MSG(dirty, TypedArray<Vector2i>(), "Grid is not initialized. Call the update method.");
689
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));
690
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));
691
692
Point *a = _get_point(p_from_id.x, p_from_id.y);
693
Point *b = _get_point(p_to_id.x, p_to_id.y);
694
695
if (a == b) {
696
TypedArray<Vector2i> ret;
697
ret.push_back(a->id);
698
return ret;
699
}
700
701
Point *begin_point = a;
702
Point *end_point = b;
703
704
bool found_route = _solve(begin_point, end_point, p_allow_partial_path);
705
if (!found_route) {
706
if (!p_allow_partial_path || last_closest_point == nullptr) {
707
return TypedArray<Vector2i>();
708
}
709
710
// Use closest point instead.
711
end_point = last_closest_point;
712
}
713
714
Point *p = end_point;
715
int32_t pc = 1;
716
while (p != begin_point) {
717
pc++;
718
p = p->prev_point;
719
}
720
721
TypedArray<Vector2i> path;
722
path.resize(pc);
723
724
{
725
p = end_point;
726
int32_t idx = pc - 1;
727
while (p != begin_point) {
728
path[idx--] = p->id;
729
p = p->prev_point;
730
}
731
732
path[0] = p->id;
733
}
734
735
return path;
736
}
737
738
void AStarGrid2D::_bind_methods() {
739
ClassDB::bind_method(D_METHOD("set_region", "region"), &AStarGrid2D::set_region);
740
ClassDB::bind_method(D_METHOD("get_region"), &AStarGrid2D::get_region);
741
ClassDB::bind_method(D_METHOD("set_size", "size"), &AStarGrid2D::set_size);
742
ClassDB::bind_method(D_METHOD("get_size"), &AStarGrid2D::get_size);
743
ClassDB::bind_method(D_METHOD("set_offset", "offset"), &AStarGrid2D::set_offset);
744
ClassDB::bind_method(D_METHOD("get_offset"), &AStarGrid2D::get_offset);
745
ClassDB::bind_method(D_METHOD("set_cell_size", "cell_size"), &AStarGrid2D::set_cell_size);
746
ClassDB::bind_method(D_METHOD("get_cell_size"), &AStarGrid2D::get_cell_size);
747
ClassDB::bind_method(D_METHOD("set_cell_shape", "cell_shape"), &AStarGrid2D::set_cell_shape);
748
ClassDB::bind_method(D_METHOD("get_cell_shape"), &AStarGrid2D::get_cell_shape);
749
ClassDB::bind_method(D_METHOD("is_in_bounds", "x", "y"), &AStarGrid2D::is_in_bounds);
750
ClassDB::bind_method(D_METHOD("is_in_boundsv", "id"), &AStarGrid2D::is_in_boundsv);
751
ClassDB::bind_method(D_METHOD("is_dirty"), &AStarGrid2D::is_dirty);
752
ClassDB::bind_method(D_METHOD("update"), &AStarGrid2D::update);
753
ClassDB::bind_method(D_METHOD("set_jumping_enabled", "enabled"), &AStarGrid2D::set_jumping_enabled);
754
ClassDB::bind_method(D_METHOD("is_jumping_enabled"), &AStarGrid2D::is_jumping_enabled);
755
ClassDB::bind_method(D_METHOD("set_diagonal_mode", "mode"), &AStarGrid2D::set_diagonal_mode);
756
ClassDB::bind_method(D_METHOD("get_diagonal_mode"), &AStarGrid2D::get_diagonal_mode);
757
ClassDB::bind_method(D_METHOD("set_default_compute_heuristic", "heuristic"), &AStarGrid2D::set_default_compute_heuristic);
758
ClassDB::bind_method(D_METHOD("get_default_compute_heuristic"), &AStarGrid2D::get_default_compute_heuristic);
759
ClassDB::bind_method(D_METHOD("set_default_estimate_heuristic", "heuristic"), &AStarGrid2D::set_default_estimate_heuristic);
760
ClassDB::bind_method(D_METHOD("get_default_estimate_heuristic"), &AStarGrid2D::get_default_estimate_heuristic);
761
ClassDB::bind_method(D_METHOD("set_point_solid", "id", "solid"), &AStarGrid2D::set_point_solid, DEFVAL(true));
762
ClassDB::bind_method(D_METHOD("is_point_solid", "id"), &AStarGrid2D::is_point_solid);
763
ClassDB::bind_method(D_METHOD("set_point_weight_scale", "id", "weight_scale"), &AStarGrid2D::set_point_weight_scale);
764
ClassDB::bind_method(D_METHOD("get_point_weight_scale", "id"), &AStarGrid2D::get_point_weight_scale);
765
ClassDB::bind_method(D_METHOD("fill_solid_region", "region", "solid"), &AStarGrid2D::fill_solid_region, DEFVAL(true));
766
ClassDB::bind_method(D_METHOD("fill_weight_scale_region", "region", "weight_scale"), &AStarGrid2D::fill_weight_scale_region);
767
ClassDB::bind_method(D_METHOD("clear"), &AStarGrid2D::clear);
768
769
ClassDB::bind_method(D_METHOD("get_point_position", "id"), &AStarGrid2D::get_point_position);
770
ClassDB::bind_method(D_METHOD("get_point_data_in_region", "region"), &AStarGrid2D::get_point_data_in_region);
771
ClassDB::bind_method(D_METHOD("get_point_path", "from_id", "to_id", "allow_partial_path"), &AStarGrid2D::get_point_path, DEFVAL(false));
772
ClassDB::bind_method(D_METHOD("get_id_path", "from_id", "to_id", "allow_partial_path"), &AStarGrid2D::get_id_path, DEFVAL(false));
773
774
GDVIRTUAL_BIND(_estimate_cost, "from_id", "end_id")
775
GDVIRTUAL_BIND(_compute_cost, "from_id", "to_id")
776
777
ADD_PROPERTY(PropertyInfo(Variant::RECT2I, "region"), "set_region", "get_region");
778
ADD_PROPERTY(PropertyInfo(Variant::VECTOR2I, "size"), "set_size", "get_size");
779
ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "offset"), "set_offset", "get_offset");
780
ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "cell_size"), "set_cell_size", "get_cell_size");
781
ADD_PROPERTY(PropertyInfo(Variant::INT, "cell_shape", PROPERTY_HINT_ENUM, "Square,IsometricRight,IsometricDown"), "set_cell_shape", "get_cell_shape");
782
783
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "jumping_enabled"), "set_jumping_enabled", "is_jumping_enabled");
784
ADD_PROPERTY(PropertyInfo(Variant::INT, "default_compute_heuristic", PROPERTY_HINT_ENUM, "Euclidean,Manhattan,Octile,Chebyshev"), "set_default_compute_heuristic", "get_default_compute_heuristic");
785
ADD_PROPERTY(PropertyInfo(Variant::INT, "default_estimate_heuristic", PROPERTY_HINT_ENUM, "Euclidean,Manhattan,Octile,Chebyshev"), "set_default_estimate_heuristic", "get_default_estimate_heuristic");
786
ADD_PROPERTY(PropertyInfo(Variant::INT, "diagonal_mode", PROPERTY_HINT_ENUM, "Never,Always,At Least One Walkable,Only If No Obstacles"), "set_diagonal_mode", "get_diagonal_mode");
787
788
BIND_ENUM_CONSTANT(HEURISTIC_EUCLIDEAN);
789
BIND_ENUM_CONSTANT(HEURISTIC_MANHATTAN);
790
BIND_ENUM_CONSTANT(HEURISTIC_OCTILE);
791
BIND_ENUM_CONSTANT(HEURISTIC_CHEBYSHEV);
792
BIND_ENUM_CONSTANT(HEURISTIC_MAX);
793
794
BIND_ENUM_CONSTANT(DIAGONAL_MODE_ALWAYS);
795
BIND_ENUM_CONSTANT(DIAGONAL_MODE_NEVER);
796
BIND_ENUM_CONSTANT(DIAGONAL_MODE_AT_LEAST_ONE_WALKABLE);
797
BIND_ENUM_CONSTANT(DIAGONAL_MODE_ONLY_IF_NO_OBSTACLES);
798
BIND_ENUM_CONSTANT(DIAGONAL_MODE_MAX);
799
800
BIND_ENUM_CONSTANT(CELL_SHAPE_SQUARE);
801
BIND_ENUM_CONSTANT(CELL_SHAPE_ISOMETRIC_RIGHT);
802
BIND_ENUM_CONSTANT(CELL_SHAPE_ISOMETRIC_DOWN);
803
BIND_ENUM_CONSTANT(CELL_SHAPE_MAX);
804
}
805
806