a_star_grid_2d.cpp 27 KB

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