a_star.cpp 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910
  1. /**************************************************************************/
  2. /* a_star.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.h"
  31. #include "a_star.compat.inc"
  32. #include "core/math/geometry_3d.h"
  33. int64_t AStar3D::get_available_point_id() const {
  34. if (points.has(last_free_id)) {
  35. int64_t cur_new_id = last_free_id + 1;
  36. while (points.has(cur_new_id)) {
  37. cur_new_id++;
  38. }
  39. last_free_id = cur_new_id;
  40. }
  41. return last_free_id;
  42. }
  43. void AStar3D::add_point(int64_t p_id, const Vector3 &p_pos, real_t p_weight_scale) {
  44. ERR_FAIL_COND_MSG(p_id < 0, vformat("Can't add a point with negative id: %d.", p_id));
  45. ERR_FAIL_COND_MSG(p_weight_scale < 0.0, vformat("Can't add a point with weight scale less than 0.0: %f.", p_weight_scale));
  46. Point **point_entry = points.getptr(p_id);
  47. if (!point_entry) {
  48. Point *pt = memnew(Point);
  49. pt->id = p_id;
  50. pt->pos = p_pos;
  51. pt->weight_scale = p_weight_scale;
  52. pt->prev_point = nullptr;
  53. pt->open_pass = 0;
  54. pt->closed_pass = 0;
  55. pt->enabled = true;
  56. points.insert_new(p_id, pt);
  57. } else {
  58. Point *found_pt = *point_entry;
  59. found_pt->pos = p_pos;
  60. found_pt->weight_scale = p_weight_scale;
  61. }
  62. }
  63. Vector3 AStar3D::get_point_position(int64_t p_id) const {
  64. Point *const *point_entry = points.getptr(p_id);
  65. ERR_FAIL_COND_V_MSG(!point_entry, Vector3(), vformat("Can't get point's position. Point with id: %d doesn't exist.", p_id));
  66. return (*point_entry)->pos;
  67. }
  68. void AStar3D::set_point_position(int64_t p_id, const Vector3 &p_pos) {
  69. Point **point_entry = points.getptr(p_id);
  70. ERR_FAIL_COND_MSG(!point_entry, vformat("Can't set point's position. Point with id: %d doesn't exist.", p_id));
  71. (*point_entry)->pos = p_pos;
  72. }
  73. real_t AStar3D::get_point_weight_scale(int64_t p_id) const {
  74. Point *const *point_entry = points.getptr(p_id);
  75. ERR_FAIL_COND_V_MSG(!point_entry, 0, vformat("Can't get point's weight scale. Point with id: %d doesn't exist.", p_id));
  76. return (*point_entry)->weight_scale;
  77. }
  78. void AStar3D::set_point_weight_scale(int64_t p_id, real_t p_weight_scale) {
  79. Point **point_entry = points.getptr(p_id);
  80. ERR_FAIL_COND_MSG(!point_entry, vformat("Can't set point's weight scale. Point with id: %d doesn't exist.", p_id));
  81. 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));
  82. (*point_entry)->weight_scale = p_weight_scale;
  83. }
  84. void AStar3D::remove_point(int64_t p_id) {
  85. Point **point_entry = points.getptr(p_id);
  86. ERR_FAIL_COND_MSG(!point_entry, vformat("Can't remove point. Point with id: %d doesn't exist.", p_id));
  87. Point *p = *point_entry;
  88. for (KeyValue<int64_t, Point *> &kv : p->neighbors) {
  89. Segment s(p_id, kv.key);
  90. segments.erase(s);
  91. kv.value->neighbors.erase(p->id);
  92. kv.value->unlinked_neighbours.erase(p->id);
  93. }
  94. for (KeyValue<int64_t, Point *> &kv : p->unlinked_neighbours) {
  95. Segment s(p_id, kv.key);
  96. segments.erase(s);
  97. kv.value->neighbors.erase(p->id);
  98. kv.value->unlinked_neighbours.erase(p->id);
  99. }
  100. memdelete(p);
  101. points.erase(p_id);
  102. last_free_id = p_id;
  103. }
  104. void AStar3D::connect_points(int64_t p_id, int64_t p_with_id, bool bidirectional) {
  105. ERR_FAIL_COND_MSG(p_id == p_with_id, vformat("Can't connect point with id: %d to itself.", p_id));
  106. Point **a_entry = points.getptr(p_id);
  107. ERR_FAIL_COND_MSG(!a_entry, vformat("Can't connect points. Point with id: %d doesn't exist.", p_id));
  108. Point *a = *a_entry;
  109. Point **b_entry = points.getptr(p_with_id);
  110. ERR_FAIL_COND_MSG(!b_entry, vformat("Can't connect points. Point with id: %d doesn't exist.", p_with_id));
  111. Point *b = *b_entry;
  112. a->neighbors.insert(b->id, b);
  113. if (bidirectional) {
  114. b->neighbors.insert(a->id, a);
  115. } else {
  116. b->unlinked_neighbours.insert(a->id, a);
  117. }
  118. Segment s(p_id, p_with_id);
  119. if (bidirectional) {
  120. s.direction = Segment::BIDIRECTIONAL;
  121. }
  122. HashSet<Segment, Segment>::Iterator element = segments.find(s);
  123. if (element) {
  124. s.direction |= element->direction;
  125. if (s.direction == Segment::BIDIRECTIONAL) {
  126. // Both are neighbors of each other now
  127. a->unlinked_neighbours.erase(b->id);
  128. b->unlinked_neighbours.erase(a->id);
  129. }
  130. segments.remove(element);
  131. }
  132. segments.insert(s);
  133. }
  134. void AStar3D::disconnect_points(int64_t p_id, int64_t p_with_id, bool bidirectional) {
  135. Point **a_entry = points.getptr(p_id);
  136. ERR_FAIL_COND_MSG(!a_entry, vformat("Can't disconnect points. Point with id: %d doesn't exist.", p_id));
  137. Point *a = *a_entry;
  138. Point **b_entry = points.getptr(p_with_id);
  139. ERR_FAIL_COND_MSG(!b_entry, vformat("Can't disconnect points. Point with id: %d doesn't exist.", p_with_id));
  140. Point *b = *b_entry;
  141. Segment s(p_id, p_with_id);
  142. int remove_direction = bidirectional ? (int)Segment::BIDIRECTIONAL : (int)s.direction;
  143. HashSet<Segment, Segment>::Iterator element = segments.find(s);
  144. if (element) {
  145. // s is the new segment
  146. // Erase the directions to be removed
  147. s.direction = (element->direction & ~remove_direction);
  148. a->neighbors.erase(b->id);
  149. if (bidirectional) {
  150. b->neighbors.erase(a->id);
  151. if (element->direction != Segment::BIDIRECTIONAL) {
  152. a->unlinked_neighbours.erase(b->id);
  153. b->unlinked_neighbours.erase(a->id);
  154. }
  155. } else {
  156. if (s.direction == Segment::NONE) {
  157. b->unlinked_neighbours.erase(a->id);
  158. } else {
  159. a->unlinked_neighbours.insert(b->id, b);
  160. }
  161. }
  162. segments.remove(element);
  163. if (s.direction != Segment::NONE) {
  164. segments.insert(s);
  165. }
  166. }
  167. }
  168. bool AStar3D::has_point(int64_t p_id) const {
  169. return points.has(p_id);
  170. }
  171. PackedInt64Array AStar3D::get_point_ids() {
  172. PackedInt64Array point_list;
  173. for (KeyValue<int64_t, Point *> &kv : points) {
  174. point_list.push_back(kv.key);
  175. }
  176. return point_list;
  177. }
  178. Vector<int64_t> AStar3D::get_point_connections(int64_t p_id) {
  179. Point **p_entry = points.getptr(p_id);
  180. ERR_FAIL_COND_V_MSG(!p_entry, Vector<int64_t>(), vformat("Can't get point's connections. Point with id: %d doesn't exist.", p_id));
  181. Vector<int64_t> point_list;
  182. for (KeyValue<int64_t, Point *> &kv : (*p_entry)->neighbors) {
  183. point_list.push_back(kv.key);
  184. }
  185. return point_list;
  186. }
  187. bool AStar3D::are_points_connected(int64_t p_id, int64_t p_with_id, bool bidirectional) const {
  188. Segment s(p_id, p_with_id);
  189. const HashSet<Segment, Segment>::Iterator element = segments.find(s);
  190. return element &&
  191. (bidirectional || (element->direction & s.direction) == s.direction);
  192. }
  193. void AStar3D::clear() {
  194. last_free_id = 0;
  195. for (KeyValue<int64_t, Point *> &kv : points) {
  196. memdelete(kv.value);
  197. }
  198. segments.clear();
  199. points.clear();
  200. }
  201. int64_t AStar3D::get_point_count() const {
  202. return points.size();
  203. }
  204. int64_t AStar3D::get_point_capacity() const {
  205. return points.get_capacity();
  206. }
  207. void AStar3D::reserve_space(int64_t p_num_nodes) {
  208. ERR_FAIL_COND_MSG(p_num_nodes <= 0, vformat("New capacity must be greater than 0, new was: %d.", p_num_nodes));
  209. points.reserve(p_num_nodes);
  210. }
  211. int64_t AStar3D::get_closest_point(const Vector3 &p_point, bool p_include_disabled) const {
  212. int64_t closest_id = -1;
  213. real_t closest_dist = 1e20;
  214. for (const KeyValue<int64_t, Point *> &kv : points) {
  215. if (!p_include_disabled && !kv.value->enabled) {
  216. continue; // Disabled points should not be considered.
  217. }
  218. // Keep the closest point's ID, and in case of multiple closest IDs,
  219. // the smallest one (makes it deterministic).
  220. real_t d = p_point.distance_squared_to(kv.value->pos);
  221. int64_t id = kv.key;
  222. if (d <= closest_dist) {
  223. if (d == closest_dist && id > closest_id) { // Keep lowest ID.
  224. continue;
  225. }
  226. closest_dist = d;
  227. closest_id = id;
  228. }
  229. }
  230. return closest_id;
  231. }
  232. Vector3 AStar3D::get_closest_position_in_segment(const Vector3 &p_point) const {
  233. real_t closest_dist = 1e20;
  234. Vector3 closest_point;
  235. for (const Segment &E : segments) {
  236. const Point *from_point = *points.getptr(E.key.first);
  237. const Point *to_point = *points.getptr(E.key.second);
  238. if (!(from_point->enabled && to_point->enabled)) {
  239. continue;
  240. }
  241. Vector3 p = Geometry3D::get_closest_point_to_segment(p_point, from_point->pos, to_point->pos);
  242. real_t d = p_point.distance_squared_to(p);
  243. if (d < closest_dist) {
  244. closest_point = p;
  245. closest_dist = d;
  246. }
  247. }
  248. return closest_point;
  249. }
  250. bool AStar3D::_solve(Point *begin_point, Point *end_point, bool p_allow_partial_path) {
  251. last_closest_point = nullptr;
  252. pass++;
  253. if (!end_point->enabled && !p_allow_partial_path) {
  254. return false;
  255. }
  256. bool found_route = false;
  257. LocalVector<Point *> open_list;
  258. SortArray<Point *, SortPoints> sorter;
  259. begin_point->g_score = 0;
  260. begin_point->f_score = _estimate_cost(begin_point->id, end_point->id);
  261. begin_point->abs_g_score = 0;
  262. begin_point->abs_f_score = _estimate_cost(begin_point->id, end_point->id);
  263. open_list.push_back(begin_point);
  264. while (!open_list.is_empty()) {
  265. Point *p = open_list[0]; // The currently processed point.
  266. // Find point closer to end_point, or same distance to end_point but closer to begin_point.
  267. 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)) {
  268. last_closest_point = p;
  269. }
  270. if (p == end_point) {
  271. found_route = true;
  272. break;
  273. }
  274. sorter.pop_heap(0, open_list.size(), open_list.ptr()); // Remove the current point from the open list.
  275. open_list.remove_at(open_list.size() - 1);
  276. p->closed_pass = pass; // Mark the point as closed.
  277. for (const KeyValue<int64_t, Point *> &kv : p->neighbors) {
  278. Point *e = kv.value; // The neighbor point.
  279. if (!e->enabled || e->closed_pass == pass) {
  280. continue;
  281. }
  282. real_t tentative_g_score = p->g_score + _compute_cost(p->id, e->id) * e->weight_scale;
  283. bool new_point = false;
  284. if (e->open_pass != pass) { // The point wasn't inside the open list.
  285. e->open_pass = pass;
  286. open_list.push_back(e);
  287. new_point = true;
  288. } else if (tentative_g_score >= e->g_score) { // The new path is worse than the previous.
  289. continue;
  290. }
  291. e->prev_point = p;
  292. e->g_score = tentative_g_score;
  293. e->f_score = e->g_score + _estimate_cost(e->id, end_point->id);
  294. e->abs_g_score = tentative_g_score;
  295. e->abs_f_score = e->f_score - e->g_score;
  296. if (new_point) { // The position of the new points is already known.
  297. sorter.push_heap(0, open_list.size() - 1, 0, e, open_list.ptr());
  298. } else {
  299. sorter.push_heap(0, open_list.find(e), 0, e, open_list.ptr());
  300. }
  301. }
  302. }
  303. return found_route;
  304. }
  305. real_t AStar3D::_estimate_cost(int64_t p_from_id, int64_t p_end_id) {
  306. real_t scost;
  307. if (GDVIRTUAL_CALL(_estimate_cost, p_from_id, p_end_id, scost)) {
  308. return scost;
  309. }
  310. Point **from_entry = points.getptr(p_from_id);
  311. ERR_FAIL_COND_V_MSG(!from_entry, 0, vformat("Can't estimate cost. Point with id: %d doesn't exist.", p_from_id));
  312. Point *from_point = *from_entry;
  313. Point **end_entry = points.getptr(p_end_id);
  314. ERR_FAIL_COND_V_MSG(!end_entry, 0, vformat("Can't estimate cost. Point with id: %d doesn't exist.", p_end_id));
  315. Point *end_point = *end_entry;
  316. return from_point->pos.distance_to(end_point->pos);
  317. }
  318. real_t AStar3D::_compute_cost(int64_t p_from_id, int64_t p_to_id) {
  319. real_t scost;
  320. if (GDVIRTUAL_CALL(_compute_cost, p_from_id, p_to_id, scost)) {
  321. return scost;
  322. }
  323. Point **from_entry = points.getptr(p_from_id);
  324. ERR_FAIL_COND_V_MSG(!from_entry, 0, vformat("Can't compute cost. Point with id: %d doesn't exist.", p_from_id));
  325. Point *from_point = *from_entry;
  326. Point **to_entry = points.getptr(p_to_id);
  327. ERR_FAIL_COND_V_MSG(!to_entry, 0, vformat("Can't compute cost. Point with id: %d doesn't exist.", p_to_id));
  328. Point *to_point = *to_entry;
  329. return from_point->pos.distance_to(to_point->pos);
  330. }
  331. Vector<Vector3> AStar3D::get_point_path(int64_t p_from_id, int64_t p_to_id, bool p_allow_partial_path) {
  332. Point **a_entry = points.getptr(p_from_id);
  333. ERR_FAIL_COND_V_MSG(!a_entry, Vector<Vector3>(), vformat("Can't get point path. Point with id: %d doesn't exist.", p_from_id));
  334. Point *a = *a_entry;
  335. Point **b_entry = points.getptr(p_to_id);
  336. ERR_FAIL_COND_V_MSG(!b_entry, Vector<Vector3>(), vformat("Can't get point path. Point with id: %d doesn't exist.", p_to_id));
  337. Point *b = *b_entry;
  338. if (a == b) {
  339. Vector<Vector3> ret;
  340. ret.push_back(a->pos);
  341. return ret;
  342. }
  343. Point *begin_point = a;
  344. Point *end_point = b;
  345. bool found_route = _solve(begin_point, end_point, p_allow_partial_path);
  346. if (!found_route) {
  347. if (!p_allow_partial_path || last_closest_point == nullptr) {
  348. return Vector<Vector3>();
  349. }
  350. // Use closest point instead.
  351. end_point = last_closest_point;
  352. }
  353. Point *p = end_point;
  354. int64_t pc = 1; // Begin point
  355. while (p != begin_point) {
  356. pc++;
  357. p = p->prev_point;
  358. }
  359. Vector<Vector3> path;
  360. path.resize(pc);
  361. {
  362. Vector3 *w = path.ptrw();
  363. Point *p2 = end_point;
  364. int64_t idx = pc - 1;
  365. while (p2 != begin_point) {
  366. w[idx--] = p2->pos;
  367. p2 = p2->prev_point;
  368. }
  369. w[0] = p2->pos; // Assign first
  370. }
  371. return path;
  372. }
  373. Vector<int64_t> AStar3D::get_id_path(int64_t p_from_id, int64_t p_to_id, bool p_allow_partial_path) {
  374. Point **a_entry = points.getptr(p_from_id);
  375. ERR_FAIL_COND_V_MSG(!a_entry, Vector<int64_t>(), vformat("Can't get id path. Point with id: %d doesn't exist.", p_from_id));
  376. Point *a = *a_entry;
  377. Point **b_entry = points.getptr(p_to_id);
  378. ERR_FAIL_COND_V_MSG(!b_entry, Vector<int64_t>(), vformat("Can't get id path. Point with id: %d doesn't exist.", p_to_id));
  379. Point *b = *b_entry;
  380. if (a == b) {
  381. Vector<int64_t> ret;
  382. ret.push_back(a->id);
  383. return ret;
  384. }
  385. Point *begin_point = a;
  386. Point *end_point = b;
  387. bool found_route = _solve(begin_point, end_point, p_allow_partial_path);
  388. if (!found_route) {
  389. if (!p_allow_partial_path || last_closest_point == nullptr) {
  390. return Vector<int64_t>();
  391. }
  392. // Use closest point instead.
  393. end_point = last_closest_point;
  394. }
  395. Point *p = end_point;
  396. int64_t pc = 1; // Begin point
  397. while (p != begin_point) {
  398. pc++;
  399. p = p->prev_point;
  400. }
  401. Vector<int64_t> path;
  402. path.resize(pc);
  403. {
  404. int64_t *w = path.ptrw();
  405. p = end_point;
  406. int64_t idx = pc - 1;
  407. while (p != begin_point) {
  408. w[idx--] = p->id;
  409. p = p->prev_point;
  410. }
  411. w[0] = p->id; // Assign first
  412. }
  413. return path;
  414. }
  415. void AStar3D::set_point_disabled(int64_t p_id, bool p_disabled) {
  416. Point **p_entry = points.getptr(p_id);
  417. ERR_FAIL_COND_MSG(!p_entry, vformat("Can't set if point is disabled. Point with id: %d doesn't exist.", p_id));
  418. Point *p = *p_entry;
  419. p->enabled = !p_disabled;
  420. }
  421. bool AStar3D::is_point_disabled(int64_t p_id) const {
  422. Point *const *p_entry = points.getptr(p_id);
  423. ERR_FAIL_COND_V_MSG(!p_entry, false, vformat("Can't get if point is disabled. Point with id: %d doesn't exist.", p_id));
  424. Point *p = *p_entry;
  425. return !p->enabled;
  426. }
  427. void AStar3D::_bind_methods() {
  428. ClassDB::bind_method(D_METHOD("get_available_point_id"), &AStar3D::get_available_point_id);
  429. ClassDB::bind_method(D_METHOD("add_point", "id", "position", "weight_scale"), &AStar3D::add_point, DEFVAL(1.0));
  430. ClassDB::bind_method(D_METHOD("get_point_position", "id"), &AStar3D::get_point_position);
  431. ClassDB::bind_method(D_METHOD("set_point_position", "id", "position"), &AStar3D::set_point_position);
  432. ClassDB::bind_method(D_METHOD("get_point_weight_scale", "id"), &AStar3D::get_point_weight_scale);
  433. ClassDB::bind_method(D_METHOD("set_point_weight_scale", "id", "weight_scale"), &AStar3D::set_point_weight_scale);
  434. ClassDB::bind_method(D_METHOD("remove_point", "id"), &AStar3D::remove_point);
  435. ClassDB::bind_method(D_METHOD("has_point", "id"), &AStar3D::has_point);
  436. ClassDB::bind_method(D_METHOD("get_point_connections", "id"), &AStar3D::get_point_connections);
  437. ClassDB::bind_method(D_METHOD("get_point_ids"), &AStar3D::get_point_ids);
  438. ClassDB::bind_method(D_METHOD("set_point_disabled", "id", "disabled"), &AStar3D::set_point_disabled, DEFVAL(true));
  439. ClassDB::bind_method(D_METHOD("is_point_disabled", "id"), &AStar3D::is_point_disabled);
  440. ClassDB::bind_method(D_METHOD("connect_points", "id", "to_id", "bidirectional"), &AStar3D::connect_points, DEFVAL(true));
  441. ClassDB::bind_method(D_METHOD("disconnect_points", "id", "to_id", "bidirectional"), &AStar3D::disconnect_points, DEFVAL(true));
  442. ClassDB::bind_method(D_METHOD("are_points_connected", "id", "to_id", "bidirectional"), &AStar3D::are_points_connected, DEFVAL(true));
  443. ClassDB::bind_method(D_METHOD("get_point_count"), &AStar3D::get_point_count);
  444. ClassDB::bind_method(D_METHOD("get_point_capacity"), &AStar3D::get_point_capacity);
  445. ClassDB::bind_method(D_METHOD("reserve_space", "num_nodes"), &AStar3D::reserve_space);
  446. ClassDB::bind_method(D_METHOD("clear"), &AStar3D::clear);
  447. ClassDB::bind_method(D_METHOD("get_closest_point", "to_position", "include_disabled"), &AStar3D::get_closest_point, DEFVAL(false));
  448. ClassDB::bind_method(D_METHOD("get_closest_position_in_segment", "to_position"), &AStar3D::get_closest_position_in_segment);
  449. ClassDB::bind_method(D_METHOD("get_point_path", "from_id", "to_id", "allow_partial_path"), &AStar3D::get_point_path, DEFVAL(false));
  450. ClassDB::bind_method(D_METHOD("get_id_path", "from_id", "to_id", "allow_partial_path"), &AStar3D::get_id_path, DEFVAL(false));
  451. GDVIRTUAL_BIND(_estimate_cost, "from_id", "end_id")
  452. GDVIRTUAL_BIND(_compute_cost, "from_id", "to_id")
  453. }
  454. AStar3D::~AStar3D() {
  455. clear();
  456. }
  457. /////////////////////////////////////////////////////////////
  458. int64_t AStar2D::get_available_point_id() const {
  459. return astar.get_available_point_id();
  460. }
  461. void AStar2D::add_point(int64_t p_id, const Vector2 &p_pos, real_t p_weight_scale) {
  462. astar.add_point(p_id, Vector3(p_pos.x, p_pos.y, 0), p_weight_scale);
  463. }
  464. Vector2 AStar2D::get_point_position(int64_t p_id) const {
  465. Vector3 p = astar.get_point_position(p_id);
  466. return Vector2(p.x, p.y);
  467. }
  468. void AStar2D::set_point_position(int64_t p_id, const Vector2 &p_pos) {
  469. astar.set_point_position(p_id, Vector3(p_pos.x, p_pos.y, 0));
  470. }
  471. real_t AStar2D::get_point_weight_scale(int64_t p_id) const {
  472. return astar.get_point_weight_scale(p_id);
  473. }
  474. void AStar2D::set_point_weight_scale(int64_t p_id, real_t p_weight_scale) {
  475. astar.set_point_weight_scale(p_id, p_weight_scale);
  476. }
  477. void AStar2D::remove_point(int64_t p_id) {
  478. astar.remove_point(p_id);
  479. }
  480. bool AStar2D::has_point(int64_t p_id) const {
  481. return astar.has_point(p_id);
  482. }
  483. Vector<int64_t> AStar2D::get_point_connections(int64_t p_id) {
  484. return astar.get_point_connections(p_id);
  485. }
  486. PackedInt64Array AStar2D::get_point_ids() {
  487. return astar.get_point_ids();
  488. }
  489. void AStar2D::set_point_disabled(int64_t p_id, bool p_disabled) {
  490. astar.set_point_disabled(p_id, p_disabled);
  491. }
  492. bool AStar2D::is_point_disabled(int64_t p_id) const {
  493. return astar.is_point_disabled(p_id);
  494. }
  495. void AStar2D::connect_points(int64_t p_id, int64_t p_with_id, bool p_bidirectional) {
  496. astar.connect_points(p_id, p_with_id, p_bidirectional);
  497. }
  498. void AStar2D::disconnect_points(int64_t p_id, int64_t p_with_id, bool p_bidirectional) {
  499. astar.disconnect_points(p_id, p_with_id, p_bidirectional);
  500. }
  501. bool AStar2D::are_points_connected(int64_t p_id, int64_t p_with_id, bool p_bidirectional) const {
  502. return astar.are_points_connected(p_id, p_with_id, p_bidirectional);
  503. }
  504. int64_t AStar2D::get_point_count() const {
  505. return astar.get_point_count();
  506. }
  507. int64_t AStar2D::get_point_capacity() const {
  508. return astar.get_point_capacity();
  509. }
  510. void AStar2D::clear() {
  511. astar.clear();
  512. }
  513. void AStar2D::reserve_space(int64_t p_num_nodes) {
  514. astar.reserve_space(p_num_nodes);
  515. }
  516. int64_t AStar2D::get_closest_point(const Vector2 &p_point, bool p_include_disabled) const {
  517. return astar.get_closest_point(Vector3(p_point.x, p_point.y, 0), p_include_disabled);
  518. }
  519. Vector2 AStar2D::get_closest_position_in_segment(const Vector2 &p_point) const {
  520. Vector3 p = astar.get_closest_position_in_segment(Vector3(p_point.x, p_point.y, 0));
  521. return Vector2(p.x, p.y);
  522. }
  523. real_t AStar2D::_estimate_cost(int64_t p_from_id, int64_t p_end_id) {
  524. real_t scost;
  525. if (GDVIRTUAL_CALL(_estimate_cost, p_from_id, p_end_id, scost)) {
  526. return scost;
  527. }
  528. AStar3D::Point **from_entry = astar.points.getptr(p_from_id);
  529. ERR_FAIL_COND_V_MSG(!from_entry, 0, vformat("Can't estimate cost. Point with id: %d doesn't exist.", p_from_id));
  530. AStar3D::Point *from_point = *from_entry;
  531. AStar3D::Point **end_entry = astar.points.getptr(p_end_id);
  532. ERR_FAIL_COND_V_MSG(!end_entry, 0, vformat("Can't estimate cost. Point with id: %d doesn't exist.", p_end_id));
  533. AStar3D::Point *end_point = *end_entry;
  534. return from_point->pos.distance_to(end_point->pos);
  535. }
  536. real_t AStar2D::_compute_cost(int64_t p_from_id, int64_t p_to_id) {
  537. real_t scost;
  538. if (GDVIRTUAL_CALL(_compute_cost, p_from_id, p_to_id, scost)) {
  539. return scost;
  540. }
  541. AStar3D::Point **from_entry = astar.points.getptr(p_from_id);
  542. ERR_FAIL_COND_V_MSG(!from_entry, 0, vformat("Can't compute cost. Point with id: %d doesn't exist.", p_from_id));
  543. AStar3D::Point *from_point = *from_entry;
  544. AStar3D::Point **to_entry = astar.points.getptr(p_to_id);
  545. ERR_FAIL_COND_V_MSG(!to_entry, 0, vformat("Can't compute cost. Point with id: %d doesn't exist.", p_to_id));
  546. AStar3D::Point *to_point = *to_entry;
  547. return from_point->pos.distance_to(to_point->pos);
  548. }
  549. Vector<Vector2> AStar2D::get_point_path(int64_t p_from_id, int64_t p_to_id, bool p_allow_partial_path) {
  550. AStar3D::Point **a_entry = astar.points.getptr(p_from_id);
  551. ERR_FAIL_COND_V_MSG(!a_entry, Vector<Vector2>(), vformat("Can't get point path. Point with id: %d doesn't exist.", p_from_id));
  552. AStar3D::Point *a = *a_entry;
  553. AStar3D::Point **b_entry = astar.points.getptr(p_to_id);
  554. ERR_FAIL_COND_V_MSG(!b_entry, Vector<Vector2>(), vformat("Can't get point path. Point with id: %d doesn't exist.", p_to_id));
  555. AStar3D::Point *b = *b_entry;
  556. if (a == b) {
  557. Vector<Vector2> ret = { Vector2(a->pos.x, a->pos.y) };
  558. return ret;
  559. }
  560. AStar3D::Point *begin_point = a;
  561. AStar3D::Point *end_point = b;
  562. bool found_route = _solve(begin_point, end_point, p_allow_partial_path);
  563. if (!found_route) {
  564. if (!p_allow_partial_path || astar.last_closest_point == nullptr) {
  565. return Vector<Vector2>();
  566. }
  567. // Use closest point instead.
  568. end_point = astar.last_closest_point;
  569. }
  570. AStar3D::Point *p = end_point;
  571. int64_t pc = 1; // Begin point
  572. while (p != begin_point) {
  573. pc++;
  574. p = p->prev_point;
  575. }
  576. Vector<Vector2> path;
  577. path.resize(pc);
  578. {
  579. Vector2 *w = path.ptrw();
  580. AStar3D::Point *p2 = end_point;
  581. int64_t idx = pc - 1;
  582. while (p2 != begin_point) {
  583. w[idx--] = Vector2(p2->pos.x, p2->pos.y);
  584. p2 = p2->prev_point;
  585. }
  586. w[0] = Vector2(p2->pos.x, p2->pos.y); // Assign first
  587. }
  588. return path;
  589. }
  590. Vector<int64_t> AStar2D::get_id_path(int64_t p_from_id, int64_t p_to_id, bool p_allow_partial_path) {
  591. AStar3D::Point **a_entry = astar.points.getptr(p_from_id);
  592. ERR_FAIL_COND_V_MSG(!a_entry, Vector<int64_t>(), vformat("Can't get id path. Point with id: %d doesn't exist.", p_from_id));
  593. AStar3D::Point *a = *a_entry;
  594. AStar3D::Point **to_entry = astar.points.getptr(p_to_id);
  595. ERR_FAIL_COND_V_MSG(!to_entry, Vector<int64_t>(), vformat("Can't get id path. Point with id: %d doesn't exist.", p_to_id));
  596. AStar3D::Point *b = *to_entry;
  597. if (a == b) {
  598. Vector<int64_t> ret;
  599. ret.push_back(a->id);
  600. return ret;
  601. }
  602. AStar3D::Point *begin_point = a;
  603. AStar3D::Point *end_point = b;
  604. bool found_route = _solve(begin_point, end_point, p_allow_partial_path);
  605. if (!found_route) {
  606. if (!p_allow_partial_path || astar.last_closest_point == nullptr) {
  607. return Vector<int64_t>();
  608. }
  609. // Use closest point instead.
  610. end_point = astar.last_closest_point;
  611. }
  612. AStar3D::Point *p = end_point;
  613. int64_t pc = 1; // Begin point
  614. while (p != begin_point) {
  615. pc++;
  616. p = p->prev_point;
  617. }
  618. Vector<int64_t> path;
  619. path.resize(pc);
  620. {
  621. int64_t *w = path.ptrw();
  622. p = end_point;
  623. int64_t idx = pc - 1;
  624. while (p != begin_point) {
  625. w[idx--] = p->id;
  626. p = p->prev_point;
  627. }
  628. w[0] = p->id; // Assign first
  629. }
  630. return path;
  631. }
  632. bool AStar2D::_solve(AStar3D::Point *begin_point, AStar3D::Point *end_point, bool p_allow_partial_path) {
  633. astar.last_closest_point = nullptr;
  634. astar.pass++;
  635. if (!end_point->enabled && !p_allow_partial_path) {
  636. return false;
  637. }
  638. bool found_route = false;
  639. LocalVector<AStar3D::Point *> open_list;
  640. SortArray<AStar3D::Point *, AStar3D::SortPoints> sorter;
  641. begin_point->g_score = 0;
  642. begin_point->f_score = _estimate_cost(begin_point->id, end_point->id);
  643. begin_point->abs_g_score = 0;
  644. begin_point->abs_f_score = _estimate_cost(begin_point->id, end_point->id);
  645. open_list.push_back(begin_point);
  646. while (!open_list.is_empty()) {
  647. AStar3D::Point *p = open_list[0]; // The currently processed point.
  648. // Find point closer to end_point, or same distance to end_point but closer to begin_point.
  649. if (astar.last_closest_point == nullptr || astar.last_closest_point->abs_f_score > p->abs_f_score || (astar.last_closest_point->abs_f_score >= p->abs_f_score && astar.last_closest_point->abs_g_score > p->abs_g_score)) {
  650. astar.last_closest_point = p;
  651. }
  652. if (p == end_point) {
  653. found_route = true;
  654. break;
  655. }
  656. sorter.pop_heap(0, open_list.size(), open_list.ptr()); // Remove the current point from the open list.
  657. open_list.remove_at(open_list.size() - 1);
  658. p->closed_pass = astar.pass; // Mark the point as closed.
  659. for (KeyValue<int64_t, AStar3D::Point *> &kv : p->neighbors) {
  660. AStar3D::Point *e = kv.value; // The neighbor point.
  661. if (!e->enabled || e->closed_pass == astar.pass) {
  662. continue;
  663. }
  664. real_t tentative_g_score = p->g_score + _compute_cost(p->id, e->id) * e->weight_scale;
  665. bool new_point = false;
  666. if (e->open_pass != astar.pass) { // The point wasn't inside the open list.
  667. e->open_pass = astar.pass;
  668. open_list.push_back(e);
  669. new_point = true;
  670. } else if (tentative_g_score >= e->g_score) { // The new path is worse than the previous.
  671. continue;
  672. }
  673. e->prev_point = p;
  674. e->g_score = tentative_g_score;
  675. e->f_score = e->g_score + _estimate_cost(e->id, end_point->id);
  676. e->abs_g_score = tentative_g_score;
  677. e->abs_f_score = e->f_score - e->g_score;
  678. if (new_point) { // The position of the new points is already known.
  679. sorter.push_heap(0, open_list.size() - 1, 0, e, open_list.ptr());
  680. } else {
  681. sorter.push_heap(0, open_list.find(e), 0, e, open_list.ptr());
  682. }
  683. }
  684. }
  685. return found_route;
  686. }
  687. void AStar2D::_bind_methods() {
  688. ClassDB::bind_method(D_METHOD("get_available_point_id"), &AStar2D::get_available_point_id);
  689. ClassDB::bind_method(D_METHOD("add_point", "id", "position", "weight_scale"), &AStar2D::add_point, DEFVAL(1.0));
  690. ClassDB::bind_method(D_METHOD("get_point_position", "id"), &AStar2D::get_point_position);
  691. ClassDB::bind_method(D_METHOD("set_point_position", "id", "position"), &AStar2D::set_point_position);
  692. ClassDB::bind_method(D_METHOD("get_point_weight_scale", "id"), &AStar2D::get_point_weight_scale);
  693. ClassDB::bind_method(D_METHOD("set_point_weight_scale", "id", "weight_scale"), &AStar2D::set_point_weight_scale);
  694. ClassDB::bind_method(D_METHOD("remove_point", "id"), &AStar2D::remove_point);
  695. ClassDB::bind_method(D_METHOD("has_point", "id"), &AStar2D::has_point);
  696. ClassDB::bind_method(D_METHOD("get_point_connections", "id"), &AStar2D::get_point_connections);
  697. ClassDB::bind_method(D_METHOD("get_point_ids"), &AStar2D::get_point_ids);
  698. ClassDB::bind_method(D_METHOD("set_point_disabled", "id", "disabled"), &AStar2D::set_point_disabled, DEFVAL(true));
  699. ClassDB::bind_method(D_METHOD("is_point_disabled", "id"), &AStar2D::is_point_disabled);
  700. ClassDB::bind_method(D_METHOD("connect_points", "id", "to_id", "bidirectional"), &AStar2D::connect_points, DEFVAL(true));
  701. ClassDB::bind_method(D_METHOD("disconnect_points", "id", "to_id", "bidirectional"), &AStar2D::disconnect_points, DEFVAL(true));
  702. ClassDB::bind_method(D_METHOD("are_points_connected", "id", "to_id", "bidirectional"), &AStar2D::are_points_connected, DEFVAL(true));
  703. ClassDB::bind_method(D_METHOD("get_point_count"), &AStar2D::get_point_count);
  704. ClassDB::bind_method(D_METHOD("get_point_capacity"), &AStar2D::get_point_capacity);
  705. ClassDB::bind_method(D_METHOD("reserve_space", "num_nodes"), &AStar2D::reserve_space);
  706. ClassDB::bind_method(D_METHOD("clear"), &AStar2D::clear);
  707. ClassDB::bind_method(D_METHOD("get_closest_point", "to_position", "include_disabled"), &AStar2D::get_closest_point, DEFVAL(false));
  708. ClassDB::bind_method(D_METHOD("get_closest_position_in_segment", "to_position"), &AStar2D::get_closest_position_in_segment);
  709. ClassDB::bind_method(D_METHOD("get_point_path", "from_id", "to_id", "allow_partial_path"), &AStar2D::get_point_path, DEFVAL(false));
  710. ClassDB::bind_method(D_METHOD("get_id_path", "from_id", "to_id", "allow_partial_path"), &AStar2D::get_id_path, DEFVAL(false));
  711. GDVIRTUAL_BIND(_estimate_cost, "from_id", "end_id")
  712. GDVIRTUAL_BIND(_compute_cost, "from_id", "to_id")
  713. }