a_star_grid_2d.cpp 25 KB

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