a_star_grid_2d.cpp 23 KB

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