a_star_grid_2d.cpp 23 KB

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