a_star.cpp 31 KB

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