a_star_grid_2d.cpp 22 KB

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