a_star_grid_2d.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620
  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. #include "a_star_grid_2d.h"
  31. #include "core/variant/typed_array.h"
  32. static real_t heuristic_euclidian(const Vector2i &p_from, const Vector2i &p_to) {
  33. real_t dx = (real_t)ABS(p_to.x - p_from.x);
  34. real_t dy = (real_t)ABS(p_to.y - p_from.y);
  35. return (real_t)Math::sqrt(dx * dx + dy * dy);
  36. }
  37. static real_t heuristic_manhattan(const Vector2i &p_from, const Vector2i &p_to) {
  38. real_t dx = (real_t)ABS(p_to.x - p_from.x);
  39. real_t dy = (real_t)ABS(p_to.y - p_from.y);
  40. return dx + dy;
  41. }
  42. static real_t heuristic_octile(const Vector2i &p_from, const Vector2i &p_to) {
  43. real_t dx = (real_t)ABS(p_to.x - p_from.x);
  44. real_t dy = (real_t)ABS(p_to.y - p_from.y);
  45. real_t F = Math_SQRT2 - 1;
  46. return (dx < dy) ? F * dx + dy : F * dy + dx;
  47. }
  48. static real_t heuristic_chebyshev(const Vector2i &p_from, const Vector2i &p_to) {
  49. real_t dx = (real_t)ABS(p_to.x - p_from.x);
  50. real_t dy = (real_t)ABS(p_to.y - p_from.y);
  51. return MAX(dx, dy);
  52. }
  53. static real_t (*heuristics[AStarGrid2D::HEURISTIC_MAX])(const Vector2i &, const Vector2i &) = { heuristic_euclidian, heuristic_manhattan, heuristic_octile, heuristic_chebyshev };
  54. void AStarGrid2D::set_size(const Size2i &p_size) {
  55. ERR_FAIL_COND(p_size.x < 0 || p_size.y < 0);
  56. if (p_size != size) {
  57. size = p_size;
  58. dirty = true;
  59. }
  60. }
  61. Size2i AStarGrid2D::get_size() const {
  62. return size;
  63. }
  64. void AStarGrid2D::set_offset(const Vector2 &p_offset) {
  65. if (!offset.is_equal_approx(p_offset)) {
  66. offset = p_offset;
  67. dirty = true;
  68. }
  69. }
  70. Vector2 AStarGrid2D::get_offset() const {
  71. return offset;
  72. }
  73. void AStarGrid2D::set_cell_size(const Size2 &p_cell_size) {
  74. if (!cell_size.is_equal_approx(p_cell_size)) {
  75. cell_size = p_cell_size;
  76. dirty = true;
  77. }
  78. }
  79. Size2 AStarGrid2D::get_cell_size() const {
  80. return cell_size;
  81. }
  82. void AStarGrid2D::update() {
  83. points.clear();
  84. for (int64_t y = 0; y < size.y; y++) {
  85. LocalVector<Point> line;
  86. for (int64_t x = 0; x < size.x; x++) {
  87. line.push_back(Point(Vector2i(x, y), offset + Vector2(x, y) * cell_size));
  88. }
  89. points.push_back(line);
  90. }
  91. dirty = false;
  92. }
  93. bool AStarGrid2D::is_in_bounds(int p_x, int p_y) const {
  94. return p_x >= 0 && p_x < size.width && p_y >= 0 && p_y < size.height;
  95. }
  96. bool AStarGrid2D::is_in_boundsv(const Vector2i &p_id) const {
  97. return p_id.x >= 0 && p_id.x < size.width && p_id.y >= 0 && p_id.y < size.height;
  98. }
  99. bool AStarGrid2D::is_dirty() const {
  100. return dirty;
  101. }
  102. void AStarGrid2D::set_jumping_enabled(bool p_enabled) {
  103. jumping_enabled = p_enabled;
  104. }
  105. bool AStarGrid2D::is_jumping_enabled() const {
  106. return jumping_enabled;
  107. }
  108. void AStarGrid2D::set_diagonal_mode(DiagonalMode p_diagonal_mode) {
  109. ERR_FAIL_INDEX((int)p_diagonal_mode, (int)DIAGONAL_MODE_MAX);
  110. diagonal_mode = p_diagonal_mode;
  111. }
  112. AStarGrid2D::DiagonalMode AStarGrid2D::get_diagonal_mode() const {
  113. return diagonal_mode;
  114. }
  115. void AStarGrid2D::set_default_compute_heuristic(Heuristic p_heuristic) {
  116. ERR_FAIL_INDEX((int)p_heuristic, (int)HEURISTIC_MAX);
  117. default_compute_heuristic = p_heuristic;
  118. }
  119. AStarGrid2D::Heuristic AStarGrid2D::get_default_compute_heuristic() const {
  120. return default_compute_heuristic;
  121. }
  122. void AStarGrid2D::set_default_estimate_heuristic(Heuristic p_heuristic) {
  123. ERR_FAIL_INDEX((int)p_heuristic, (int)HEURISTIC_MAX);
  124. default_estimate_heuristic = p_heuristic;
  125. }
  126. AStarGrid2D::Heuristic AStarGrid2D::get_default_estimate_heuristic() const {
  127. return default_estimate_heuristic;
  128. }
  129. void AStarGrid2D::set_point_solid(const Vector2i &p_id, bool p_solid) {
  130. ERR_FAIL_COND_MSG(dirty, "Grid is not initialized. Call the update method.");
  131. ERR_FAIL_COND_MSG(!is_in_boundsv(p_id), vformat("Can't set if point is disabled. Point out of bounds (%s/%s, %s/%s).", p_id.x, size.width, p_id.y, size.height));
  132. points[p_id.y][p_id.x].solid = p_solid;
  133. }
  134. bool AStarGrid2D::is_point_solid(const Vector2i &p_id) const {
  135. ERR_FAIL_COND_V_MSG(dirty, false, "Grid is not initialized. Call the update method.");
  136. ERR_FAIL_COND_V_MSG(!is_in_boundsv(p_id), false, vformat("Can't get if point is disabled. Point out of bounds (%s/%s, %s/%s).", p_id.x, size.width, p_id.y, size.height));
  137. return points[p_id.y][p_id.x].solid;
  138. }
  139. void AStarGrid2D::set_point_weight_scale(const Vector2i &p_id, real_t p_weight_scale) {
  140. ERR_FAIL_COND_MSG(dirty, "Grid is not initialized. Call the update method.");
  141. ERR_FAIL_COND_MSG(!is_in_boundsv(p_id), vformat("Can't set point's weight scale. Point out of bounds (%s/%s, %s/%s).", p_id.x, size.width, p_id.y, size.height));
  142. 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));
  143. points[p_id.y][p_id.x].weight_scale = p_weight_scale;
  144. }
  145. real_t AStarGrid2D::get_point_weight_scale(const Vector2i &p_id) const {
  146. ERR_FAIL_COND_V_MSG(dirty, 0, "Grid is not initialized. Call the update method.");
  147. ERR_FAIL_COND_V_MSG(!is_in_boundsv(p_id), 0, vformat("Can't get point's weight scale. Point out of bounds (%s/%s, %s/%s).", p_id.x, size.width, p_id.y, size.height));
  148. return points[p_id.y][p_id.x].weight_scale;
  149. }
  150. AStarGrid2D::Point *AStarGrid2D::_jump(Point *p_from, Point *p_to) {
  151. if (!p_to || p_to->solid) {
  152. return nullptr;
  153. }
  154. if (p_to == end) {
  155. return p_to;
  156. }
  157. int64_t from_x = p_from->id.x;
  158. int64_t from_y = p_from->id.y;
  159. int64_t to_x = p_to->id.x;
  160. int64_t to_y = p_to->id.y;
  161. int64_t dx = to_x - from_x;
  162. int64_t dy = to_y - from_y;
  163. if (diagonal_mode == DIAGONAL_MODE_ALWAYS || diagonal_mode == DIAGONAL_MODE_AT_LEAST_ONE_WALKABLE) {
  164. if (dx != 0 && dy != 0) {
  165. 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))) {
  166. return p_to;
  167. }
  168. if (_jump(p_to, _get_point(to_x + dx, to_y)) != nullptr) {
  169. return p_to;
  170. }
  171. if (_jump(p_to, _get_point(to_x, to_y + dy)) != nullptr) {
  172. return p_to;
  173. }
  174. } else {
  175. if (dx != 0) {
  176. if ((_is_walkable(to_x + dx, to_y + 1) && !_is_walkable(to_x, to_y + 1)) || (_is_walkable(to_x + dx, to_y - 1) && !_is_walkable(to_x, to_y - 1))) {
  177. return p_to;
  178. }
  179. } else {
  180. if ((_is_walkable(to_x + 1, to_y + dy) && !_is_walkable(to_x + 1, to_y)) || (_is_walkable(to_x - 1, to_y + dy) && !_is_walkable(to_x - 1, to_y))) {
  181. return p_to;
  182. }
  183. }
  184. }
  185. if (_is_walkable(to_x + dx, to_y + dy) && (diagonal_mode == DIAGONAL_MODE_ALWAYS || (_is_walkable(to_x + dx, to_y) || _is_walkable(to_x, to_y + dy)))) {
  186. return _jump(p_to, _get_point(to_x + dx, to_y + dy));
  187. }
  188. } else if (diagonal_mode == DIAGONAL_MODE_ONLY_IF_NO_OBSTACLES) {
  189. if (dx != 0 && dy != 0) {
  190. if ((_is_walkable(to_x + dx, to_y + dy) && !_is_walkable(to_x, to_y + dy)) || !_is_walkable(to_x + dx, to_y)) {
  191. return p_to;
  192. }
  193. if (_jump(p_to, _get_point(to_x + dx, to_y)) != nullptr) {
  194. return p_to;
  195. }
  196. if (_jump(p_to, _get_point(to_x, to_y + dy)) != nullptr) {
  197. return p_to;
  198. }
  199. } else {
  200. if (dx != 0) {
  201. if ((_is_walkable(to_x, to_y + 1) && !_is_walkable(to_x - dx, to_y + 1)) || (_is_walkable(to_x, to_y - 1) && !_is_walkable(to_x - dx, to_y - 1))) {
  202. return p_to;
  203. }
  204. } else {
  205. 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))) {
  206. return p_to;
  207. }
  208. }
  209. }
  210. if (_is_walkable(to_x + dx, to_y + dy) && _is_walkable(to_x + dx, to_y) && _is_walkable(to_x, to_y + dy)) {
  211. return _jump(p_to, _get_point(to_x + dx, to_y + dy));
  212. }
  213. } else { // DIAGONAL_MODE_NEVER
  214. if (dx != 0) {
  215. if ((_is_walkable(to_x, to_y - 1) && !_is_walkable(to_x - dx, to_y - 1)) || (_is_walkable(to_x, to_y + 1) && !_is_walkable(to_x - dx, to_y + 1))) {
  216. return p_to;
  217. }
  218. } else if (dy != 0) {
  219. 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))) {
  220. return p_to;
  221. }
  222. if (_jump(p_to, _get_point(to_x + 1, to_y)) != nullptr) {
  223. return p_to;
  224. }
  225. if (_jump(p_to, _get_point(to_x - 1, to_y)) != nullptr) {
  226. return p_to;
  227. }
  228. }
  229. return _jump(p_to, _get_point(to_x + dx, to_y + dy));
  230. }
  231. return nullptr;
  232. }
  233. void AStarGrid2D::_get_nbors(Point *p_point, LocalVector<Point *> &r_nbors) {
  234. bool ts0 = false, td0 = false,
  235. ts1 = false, td1 = false,
  236. ts2 = false, td2 = false,
  237. ts3 = false, td3 = false;
  238. Point *left = nullptr;
  239. Point *right = nullptr;
  240. Point *top = nullptr;
  241. Point *bottom = nullptr;
  242. Point *top_left = nullptr;
  243. Point *top_right = nullptr;
  244. Point *bottom_left = nullptr;
  245. Point *bottom_right = nullptr;
  246. {
  247. bool has_left = false;
  248. bool has_right = false;
  249. if (p_point->id.x - 1 >= 0) {
  250. left = _get_point_unchecked(p_point->id.x - 1, p_point->id.y);
  251. has_left = true;
  252. }
  253. if (p_point->id.x + 1 < size.width) {
  254. right = _get_point_unchecked(p_point->id.x + 1, p_point->id.y);
  255. has_right = true;
  256. }
  257. if (p_point->id.y - 1 >= 0) {
  258. top = _get_point_unchecked(p_point->id.x, p_point->id.y - 1);
  259. if (has_left) {
  260. top_left = _get_point_unchecked(p_point->id.x - 1, p_point->id.y - 1);
  261. }
  262. if (has_right) {
  263. top_right = _get_point_unchecked(p_point->id.x + 1, p_point->id.y - 1);
  264. }
  265. }
  266. if (p_point->id.y + 1 < size.height) {
  267. bottom = _get_point_unchecked(p_point->id.x, p_point->id.y + 1);
  268. if (has_left) {
  269. bottom_left = _get_point_unchecked(p_point->id.x - 1, p_point->id.y + 1);
  270. }
  271. if (has_right) {
  272. bottom_right = _get_point_unchecked(p_point->id.x + 1, p_point->id.y + 1);
  273. }
  274. }
  275. }
  276. if (top && !top->solid) {
  277. r_nbors.push_back(top);
  278. ts0 = true;
  279. }
  280. if (right && !right->solid) {
  281. r_nbors.push_back(right);
  282. ts1 = true;
  283. }
  284. if (bottom && !bottom->solid) {
  285. r_nbors.push_back(bottom);
  286. ts2 = true;
  287. }
  288. if (left && !left->solid) {
  289. r_nbors.push_back(left);
  290. ts3 = true;
  291. }
  292. switch (diagonal_mode) {
  293. case DIAGONAL_MODE_ALWAYS: {
  294. td0 = true;
  295. td1 = true;
  296. td2 = true;
  297. td3 = true;
  298. } break;
  299. case DIAGONAL_MODE_NEVER: {
  300. } break;
  301. case DIAGONAL_MODE_AT_LEAST_ONE_WALKABLE: {
  302. td0 = ts3 || ts0;
  303. td1 = ts0 || ts1;
  304. td2 = ts1 || ts2;
  305. td3 = ts2 || ts3;
  306. } break;
  307. case DIAGONAL_MODE_ONLY_IF_NO_OBSTACLES: {
  308. td0 = ts3 && ts0;
  309. td1 = ts0 && ts1;
  310. td2 = ts1 && ts2;
  311. td3 = ts2 && ts3;
  312. } break;
  313. default:
  314. break;
  315. }
  316. if (td0 && (top_left && !top_left->solid)) {
  317. r_nbors.push_back(top_left);
  318. }
  319. if (td1 && (top_right && !top_right->solid)) {
  320. r_nbors.push_back(top_right);
  321. }
  322. if (td2 && (bottom_right && !bottom_right->solid)) {
  323. r_nbors.push_back(bottom_right);
  324. }
  325. if (td3 && (bottom_left && !bottom_left->solid)) {
  326. r_nbors.push_back(bottom_left);
  327. }
  328. }
  329. bool AStarGrid2D::_solve(Point *p_begin_point, Point *p_end_point) {
  330. pass++;
  331. if (p_end_point->solid) {
  332. return false;
  333. }
  334. bool found_route = false;
  335. LocalVector<Point *> open_list;
  336. SortArray<Point *, SortPoints> sorter;
  337. p_begin_point->g_score = 0;
  338. p_begin_point->f_score = _estimate_cost(p_begin_point->id, p_end_point->id);
  339. open_list.push_back(p_begin_point);
  340. end = p_end_point;
  341. while (!open_list.is_empty()) {
  342. Point *p = open_list[0]; // The currently processed point.
  343. if (p == p_end_point) {
  344. found_route = true;
  345. break;
  346. }
  347. sorter.pop_heap(0, open_list.size(), open_list.ptr()); // Remove the current point from the open list.
  348. open_list.remove_at(open_list.size() - 1);
  349. p->closed_pass = pass; // Mark the point as closed.
  350. LocalVector<Point *> nbors;
  351. _get_nbors(p, nbors);
  352. for (Point *e : nbors) {
  353. real_t weight_scale = 1.0;
  354. if (jumping_enabled) {
  355. // TODO: Make it works with weight_scale.
  356. e = _jump(p, e);
  357. if (!e || e->closed_pass == pass) {
  358. continue;
  359. }
  360. } else {
  361. if (e->solid || e->closed_pass == pass) {
  362. continue;
  363. }
  364. weight_scale = e->weight_scale;
  365. }
  366. real_t tentative_g_score = p->g_score + _compute_cost(p->id, e->id) * weight_scale;
  367. bool new_point = false;
  368. if (e->open_pass != pass) { // The point wasn't inside the open list.
  369. e->open_pass = pass;
  370. open_list.push_back(e);
  371. new_point = true;
  372. } else if (tentative_g_score >= e->g_score) { // The new path is worse than the previous.
  373. continue;
  374. }
  375. e->prev_point = p;
  376. e->g_score = tentative_g_score;
  377. e->f_score = e->g_score + _estimate_cost(e->id, p_end_point->id);
  378. if (new_point) { // The position of the new points is already known.
  379. sorter.push_heap(0, open_list.size() - 1, 0, e, open_list.ptr());
  380. } else {
  381. sorter.push_heap(0, open_list.find(e), 0, e, open_list.ptr());
  382. }
  383. }
  384. }
  385. return found_route;
  386. }
  387. real_t AStarGrid2D::_estimate_cost(const Vector2i &p_from_id, const Vector2i &p_to_id) {
  388. real_t scost;
  389. if (GDVIRTUAL_CALL(_estimate_cost, p_from_id, p_to_id, scost)) {
  390. return scost;
  391. }
  392. return heuristics[default_estimate_heuristic](p_from_id, p_to_id);
  393. }
  394. real_t AStarGrid2D::_compute_cost(const Vector2i &p_from_id, const Vector2i &p_to_id) {
  395. real_t scost;
  396. if (GDVIRTUAL_CALL(_compute_cost, p_from_id, p_to_id, scost)) {
  397. return scost;
  398. }
  399. return heuristics[default_compute_heuristic](p_from_id, p_to_id);
  400. }
  401. void AStarGrid2D::clear() {
  402. points.clear();
  403. size = Vector2i();
  404. }
  405. Vector2 AStarGrid2D::get_point_position(const Vector2i &p_id) const {
  406. ERR_FAIL_COND_V_MSG(dirty, Vector2(), "Grid is not initialized. Call the update method.");
  407. ERR_FAIL_COND_V_MSG(!is_in_boundsv(p_id), Vector2(), vformat("Can't get point's position. Point out of bounds (%s/%s, %s/%s).", p_id.x, size.width, p_id.y, size.height));
  408. return points[p_id.y][p_id.x].pos;
  409. }
  410. Vector<Vector2> AStarGrid2D::get_point_path(const Vector2i &p_from_id, const Vector2i &p_to_id) {
  411. ERR_FAIL_COND_V_MSG(dirty, Vector<Vector2>(), "Grid is not initialized. Call the update method.");
  412. ERR_FAIL_COND_V_MSG(!is_in_boundsv(p_from_id), Vector<Vector2>(), vformat("Can't get id path. Point out of bounds (%s/%s, %s/%s)", p_from_id.x, size.width, p_from_id.y, size.height));
  413. ERR_FAIL_COND_V_MSG(!is_in_boundsv(p_to_id), Vector<Vector2>(), vformat("Can't get id path. Point out of bounds (%s/%s, %s/%s)", p_to_id.x, size.width, p_to_id.y, size.height));
  414. Point *a = _get_point(p_from_id.x, p_from_id.y);
  415. Point *b = _get_point(p_to_id.x, p_to_id.y);
  416. if (a == b) {
  417. Vector<Vector2> ret;
  418. ret.push_back(a->pos);
  419. return ret;
  420. }
  421. Point *begin_point = a;
  422. Point *end_point = b;
  423. bool found_route = _solve(begin_point, end_point);
  424. if (!found_route) {
  425. return Vector<Vector2>();
  426. }
  427. Point *p = end_point;
  428. int64_t pc = 1;
  429. while (p != begin_point) {
  430. pc++;
  431. p = p->prev_point;
  432. }
  433. Vector<Vector2> path;
  434. path.resize(pc);
  435. {
  436. Vector2 *w = path.ptrw();
  437. p = end_point;
  438. int64_t idx = pc - 1;
  439. while (p != begin_point) {
  440. w[idx--] = p->pos;
  441. p = p->prev_point;
  442. }
  443. w[0] = p->pos;
  444. }
  445. return path;
  446. }
  447. TypedArray<Vector2i> AStarGrid2D::get_id_path(const Vector2i &p_from_id, const Vector2i &p_to_id) {
  448. ERR_FAIL_COND_V_MSG(dirty, TypedArray<Vector2i>(), "Grid is not initialized. Call the update method.");
  449. ERR_FAIL_COND_V_MSG(!is_in_boundsv(p_from_id), TypedArray<Vector2i>(), vformat("Can't get id path. Point out of bounds (%s/%s, %s/%s)", p_from_id.x, size.width, p_from_id.y, size.height));
  450. ERR_FAIL_COND_V_MSG(!is_in_boundsv(p_to_id), TypedArray<Vector2i>(), vformat("Can't get id path. Point out of bounds (%s/%s, %s/%s)", p_to_id.x, size.width, p_to_id.y, size.height));
  451. Point *a = _get_point(p_from_id.x, p_from_id.y);
  452. Point *b = _get_point(p_to_id.x, p_to_id.y);
  453. if (a == b) {
  454. TypedArray<Vector2i> ret;
  455. ret.push_back(a->id);
  456. return ret;
  457. }
  458. Point *begin_point = a;
  459. Point *end_point = b;
  460. bool found_route = _solve(begin_point, end_point);
  461. if (!found_route) {
  462. return TypedArray<Vector2i>();
  463. }
  464. Point *p = end_point;
  465. int64_t pc = 1;
  466. while (p != begin_point) {
  467. pc++;
  468. p = p->prev_point;
  469. }
  470. TypedArray<Vector2i> path;
  471. path.resize(pc);
  472. {
  473. p = end_point;
  474. int64_t idx = pc - 1;
  475. while (p != begin_point) {
  476. path[idx--] = p->id;
  477. p = p->prev_point;
  478. }
  479. path[0] = p->id;
  480. }
  481. return path;
  482. }
  483. void AStarGrid2D::_bind_methods() {
  484. ClassDB::bind_method(D_METHOD("set_size", "size"), &AStarGrid2D::set_size);
  485. ClassDB::bind_method(D_METHOD("get_size"), &AStarGrid2D::get_size);
  486. ClassDB::bind_method(D_METHOD("set_offset", "offset"), &AStarGrid2D::set_offset);
  487. ClassDB::bind_method(D_METHOD("get_offset"), &AStarGrid2D::get_offset);
  488. ClassDB::bind_method(D_METHOD("set_cell_size", "cell_size"), &AStarGrid2D::set_cell_size);
  489. ClassDB::bind_method(D_METHOD("get_cell_size"), &AStarGrid2D::get_cell_size);
  490. ClassDB::bind_method(D_METHOD("is_in_bounds", "x", "y"), &AStarGrid2D::is_in_bounds);
  491. ClassDB::bind_method(D_METHOD("is_in_boundsv", "id"), &AStarGrid2D::is_in_boundsv);
  492. ClassDB::bind_method(D_METHOD("is_dirty"), &AStarGrid2D::is_dirty);
  493. ClassDB::bind_method(D_METHOD("update"), &AStarGrid2D::update);
  494. ClassDB::bind_method(D_METHOD("set_jumping_enabled", "enabled"), &AStarGrid2D::set_jumping_enabled);
  495. ClassDB::bind_method(D_METHOD("is_jumping_enabled"), &AStarGrid2D::is_jumping_enabled);
  496. ClassDB::bind_method(D_METHOD("set_diagonal_mode", "mode"), &AStarGrid2D::set_diagonal_mode);
  497. ClassDB::bind_method(D_METHOD("get_diagonal_mode"), &AStarGrid2D::get_diagonal_mode);
  498. ClassDB::bind_method(D_METHOD("set_default_compute_heuristic", "heuristic"), &AStarGrid2D::set_default_compute_heuristic);
  499. ClassDB::bind_method(D_METHOD("get_default_compute_heuristic"), &AStarGrid2D::get_default_compute_heuristic);
  500. ClassDB::bind_method(D_METHOD("set_default_estimate_heuristic", "heuristic"), &AStarGrid2D::set_default_estimate_heuristic);
  501. ClassDB::bind_method(D_METHOD("get_default_estimate_heuristic"), &AStarGrid2D::get_default_estimate_heuristic);
  502. ClassDB::bind_method(D_METHOD("set_point_solid", "id", "solid"), &AStarGrid2D::set_point_solid, DEFVAL(true));
  503. ClassDB::bind_method(D_METHOD("is_point_solid", "id"), &AStarGrid2D::is_point_solid);
  504. ClassDB::bind_method(D_METHOD("set_point_weight_scale", "id", "weight_scale"), &AStarGrid2D::set_point_weight_scale);
  505. ClassDB::bind_method(D_METHOD("get_point_weight_scale", "id"), &AStarGrid2D::get_point_weight_scale);
  506. ClassDB::bind_method(D_METHOD("clear"), &AStarGrid2D::clear);
  507. ClassDB::bind_method(D_METHOD("get_point_position", "id"), &AStarGrid2D::get_point_position);
  508. ClassDB::bind_method(D_METHOD("get_point_path", "from_id", "to_id"), &AStarGrid2D::get_point_path);
  509. ClassDB::bind_method(D_METHOD("get_id_path", "from_id", "to_id"), &AStarGrid2D::get_id_path);
  510. GDVIRTUAL_BIND(_estimate_cost, "from_id", "to_id")
  511. GDVIRTUAL_BIND(_compute_cost, "from_id", "to_id")
  512. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2I, "size"), "set_size", "get_size");
  513. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "offset"), "set_offset", "get_offset");
  514. ADD_PROPERTY(PropertyInfo(Variant::VECTOR2, "cell_size"), "set_cell_size", "get_cell_size");
  515. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "jumping_enabled"), "set_jumping_enabled", "is_jumping_enabled");
  516. ADD_PROPERTY(PropertyInfo(Variant::INT, "default_compute_heuristic", PROPERTY_HINT_ENUM, "Euclidean,Manhattan,Octile,Chebyshev"), "set_default_compute_heuristic", "get_default_compute_heuristic");
  517. ADD_PROPERTY(PropertyInfo(Variant::INT, "default_estimate_heuristic", PROPERTY_HINT_ENUM, "Euclidean,Manhattan,Octile,Chebyshev"), "set_default_estimate_heuristic", "get_default_estimate_heuristic");
  518. 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");
  519. BIND_ENUM_CONSTANT(HEURISTIC_EUCLIDEAN);
  520. BIND_ENUM_CONSTANT(HEURISTIC_MANHATTAN);
  521. BIND_ENUM_CONSTANT(HEURISTIC_OCTILE);
  522. BIND_ENUM_CONSTANT(HEURISTIC_CHEBYSHEV);
  523. BIND_ENUM_CONSTANT(HEURISTIC_MAX);
  524. BIND_ENUM_CONSTANT(DIAGONAL_MODE_ALWAYS);
  525. BIND_ENUM_CONSTANT(DIAGONAL_MODE_NEVER);
  526. BIND_ENUM_CONSTANT(DIAGONAL_MODE_AT_LEAST_ONE_WALKABLE);
  527. BIND_ENUM_CONSTANT(DIAGONAL_MODE_ONLY_IF_NO_OBSTACLES);
  528. BIND_ENUM_CONSTANT(DIAGONAL_MODE_MAX);
  529. }