a_star.cpp 31 KB

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