a_star.cpp 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918
  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 p = Geometry3D::get_closest_point_to_segment(p_point, from_point->pos, to_point->pos);
  249. real_t d = p_point.distance_squared_to(p);
  250. if (d < closest_dist) {
  251. closest_point = p;
  252. closest_dist = d;
  253. }
  254. }
  255. return closest_point;
  256. }
  257. bool AStar3D::_solve(Point *begin_point, Point *end_point, bool p_allow_partial_path) {
  258. last_closest_point = nullptr;
  259. pass++;
  260. if (!end_point->enabled && !p_allow_partial_path) {
  261. return false;
  262. }
  263. bool found_route = false;
  264. LocalVector<Point *> open_list;
  265. SortArray<Point *, SortPoints> sorter;
  266. begin_point->g_score = 0;
  267. begin_point->f_score = _estimate_cost(begin_point->id, end_point->id);
  268. begin_point->abs_g_score = 0;
  269. begin_point->abs_f_score = _estimate_cost(begin_point->id, end_point->id);
  270. open_list.push_back(begin_point);
  271. while (!open_list.is_empty()) {
  272. Point *p = open_list[0]; // The currently processed point.
  273. // Find point closer to end_point, or same distance to end_point but closer to begin_point.
  274. 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)) {
  275. last_closest_point = p;
  276. }
  277. if (p == end_point) {
  278. found_route = true;
  279. break;
  280. }
  281. sorter.pop_heap(0, open_list.size(), open_list.ptr()); // Remove the current point from the open list.
  282. open_list.remove_at(open_list.size() - 1);
  283. p->closed_pass = pass; // Mark the point as closed.
  284. for (OAHashMap<int64_t, Point *>::Iterator it = p->neighbors.iter(); it.valid; it = p->neighbors.next_iter(it)) {
  285. Point *e = *(it.value); // The neighbor point.
  286. if (!e->enabled || e->closed_pass == pass) {
  287. continue;
  288. }
  289. real_t tentative_g_score = p->g_score + _compute_cost(p->id, e->id) * e->weight_scale;
  290. bool new_point = false;
  291. if (e->open_pass != pass) { // The point wasn't inside the open list.
  292. e->open_pass = pass;
  293. open_list.push_back(e);
  294. new_point = true;
  295. } else if (tentative_g_score >= e->g_score) { // The new path is worse than the previous.
  296. continue;
  297. }
  298. e->prev_point = p;
  299. e->g_score = tentative_g_score;
  300. e->f_score = e->g_score + _estimate_cost(e->id, end_point->id);
  301. e->abs_g_score = tentative_g_score;
  302. e->abs_f_score = e->f_score - e->g_score;
  303. if (new_point) { // The position of the new points is already known.
  304. sorter.push_heap(0, open_list.size() - 1, 0, e, open_list.ptr());
  305. } else {
  306. sorter.push_heap(0, open_list.find(e), 0, e, open_list.ptr());
  307. }
  308. }
  309. }
  310. return found_route;
  311. }
  312. real_t AStar3D::_estimate_cost(int64_t p_from_id, int64_t p_end_id) {
  313. real_t scost;
  314. if (GDVIRTUAL_CALL(_estimate_cost, p_from_id, p_end_id, scost)) {
  315. return scost;
  316. }
  317. Point *from_point = nullptr;
  318. bool from_exists = points.lookup(p_from_id, from_point);
  319. ERR_FAIL_COND_V_MSG(!from_exists, 0, vformat("Can't estimate cost. Point with id: %d doesn't exist.", p_from_id));
  320. Point *end_point = nullptr;
  321. bool end_exists = points.lookup(p_end_id, end_point);
  322. ERR_FAIL_COND_V_MSG(!end_exists, 0, vformat("Can't estimate cost. Point with id: %d doesn't exist.", p_end_id));
  323. return from_point->pos.distance_to(end_point->pos);
  324. }
  325. real_t AStar3D::_compute_cost(int64_t p_from_id, int64_t p_to_id) {
  326. real_t scost;
  327. if (GDVIRTUAL_CALL(_compute_cost, p_from_id, p_to_id, scost)) {
  328. return scost;
  329. }
  330. Point *from_point = nullptr;
  331. bool from_exists = points.lookup(p_from_id, from_point);
  332. ERR_FAIL_COND_V_MSG(!from_exists, 0, vformat("Can't compute cost. Point with id: %d doesn't exist.", p_from_id));
  333. Point *to_point = nullptr;
  334. bool to_exists = points.lookup(p_to_id, to_point);
  335. ERR_FAIL_COND_V_MSG(!to_exists, 0, vformat("Can't compute cost. Point with id: %d doesn't exist.", p_to_id));
  336. return from_point->pos.distance_to(to_point->pos);
  337. }
  338. Vector<Vector3> AStar3D::get_point_path(int64_t p_from_id, int64_t p_to_id, bool p_allow_partial_path) {
  339. Point *a = nullptr;
  340. bool from_exists = points.lookup(p_from_id, a);
  341. 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));
  342. Point *b = nullptr;
  343. bool to_exists = points.lookup(p_to_id, b);
  344. 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));
  345. if (a == b) {
  346. Vector<Vector3> ret;
  347. ret.push_back(a->pos);
  348. return ret;
  349. }
  350. Point *begin_point = a;
  351. Point *end_point = b;
  352. bool found_route = _solve(begin_point, end_point, p_allow_partial_path);
  353. if (!found_route) {
  354. if (!p_allow_partial_path || last_closest_point == nullptr) {
  355. return Vector<Vector3>();
  356. }
  357. // Use closest point instead.
  358. end_point = last_closest_point;
  359. }
  360. Point *p = end_point;
  361. int64_t pc = 1; // Begin point
  362. while (p != begin_point) {
  363. pc++;
  364. p = p->prev_point;
  365. }
  366. Vector<Vector3> path;
  367. path.resize(pc);
  368. {
  369. Vector3 *w = path.ptrw();
  370. Point *p2 = end_point;
  371. int64_t idx = pc - 1;
  372. while (p2 != begin_point) {
  373. w[idx--] = p2->pos;
  374. p2 = p2->prev_point;
  375. }
  376. w[0] = p2->pos; // Assign first
  377. }
  378. return path;
  379. }
  380. Vector<int64_t> AStar3D::get_id_path(int64_t p_from_id, int64_t p_to_id, bool p_allow_partial_path) {
  381. Point *a = nullptr;
  382. bool from_exists = points.lookup(p_from_id, a);
  383. 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));
  384. Point *b = nullptr;
  385. bool to_exists = points.lookup(p_to_id, b);
  386. 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));
  387. if (a == b) {
  388. Vector<int64_t> ret;
  389. ret.push_back(a->id);
  390. return ret;
  391. }
  392. Point *begin_point = a;
  393. Point *end_point = b;
  394. bool found_route = _solve(begin_point, end_point, p_allow_partial_path);
  395. if (!found_route) {
  396. if (!p_allow_partial_path || last_closest_point == nullptr) {
  397. return Vector<int64_t>();
  398. }
  399. // Use closest point instead.
  400. end_point = last_closest_point;
  401. }
  402. Point *p = end_point;
  403. int64_t pc = 1; // Begin point
  404. while (p != begin_point) {
  405. pc++;
  406. p = p->prev_point;
  407. }
  408. Vector<int64_t> path;
  409. path.resize(pc);
  410. {
  411. int64_t *w = path.ptrw();
  412. p = end_point;
  413. int64_t idx = pc - 1;
  414. while (p != begin_point) {
  415. w[idx--] = p->id;
  416. p = p->prev_point;
  417. }
  418. w[0] = p->id; // Assign first
  419. }
  420. return path;
  421. }
  422. void AStar3D::set_point_disabled(int64_t p_id, bool p_disabled) {
  423. Point *p = nullptr;
  424. bool p_exists = points.lookup(p_id, p);
  425. ERR_FAIL_COND_MSG(!p_exists, vformat("Can't set if point is disabled. Point with id: %d doesn't exist.", p_id));
  426. p->enabled = !p_disabled;
  427. }
  428. bool AStar3D::is_point_disabled(int64_t p_id) const {
  429. Point *p = nullptr;
  430. bool p_exists = points.lookup(p_id, p);
  431. 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));
  432. return !p->enabled;
  433. }
  434. void AStar3D::_bind_methods() {
  435. ClassDB::bind_method(D_METHOD("get_available_point_id"), &AStar3D::get_available_point_id);
  436. ClassDB::bind_method(D_METHOD("add_point", "id", "position", "weight_scale"), &AStar3D::add_point, DEFVAL(1.0));
  437. ClassDB::bind_method(D_METHOD("get_point_position", "id"), &AStar3D::get_point_position);
  438. ClassDB::bind_method(D_METHOD("set_point_position", "id", "position"), &AStar3D::set_point_position);
  439. ClassDB::bind_method(D_METHOD("get_point_weight_scale", "id"), &AStar3D::get_point_weight_scale);
  440. ClassDB::bind_method(D_METHOD("set_point_weight_scale", "id", "weight_scale"), &AStar3D::set_point_weight_scale);
  441. ClassDB::bind_method(D_METHOD("remove_point", "id"), &AStar3D::remove_point);
  442. ClassDB::bind_method(D_METHOD("has_point", "id"), &AStar3D::has_point);
  443. ClassDB::bind_method(D_METHOD("get_point_connections", "id"), &AStar3D::get_point_connections);
  444. ClassDB::bind_method(D_METHOD("get_point_ids"), &AStar3D::get_point_ids);
  445. ClassDB::bind_method(D_METHOD("set_point_disabled", "id", "disabled"), &AStar3D::set_point_disabled, DEFVAL(true));
  446. ClassDB::bind_method(D_METHOD("is_point_disabled", "id"), &AStar3D::is_point_disabled);
  447. ClassDB::bind_method(D_METHOD("connect_points", "id", "to_id", "bidirectional"), &AStar3D::connect_points, DEFVAL(true));
  448. ClassDB::bind_method(D_METHOD("disconnect_points", "id", "to_id", "bidirectional"), &AStar3D::disconnect_points, DEFVAL(true));
  449. ClassDB::bind_method(D_METHOD("are_points_connected", "id", "to_id", "bidirectional"), &AStar3D::are_points_connected, DEFVAL(true));
  450. ClassDB::bind_method(D_METHOD("get_point_count"), &AStar3D::get_point_count);
  451. ClassDB::bind_method(D_METHOD("get_point_capacity"), &AStar3D::get_point_capacity);
  452. ClassDB::bind_method(D_METHOD("reserve_space", "num_nodes"), &AStar3D::reserve_space);
  453. ClassDB::bind_method(D_METHOD("clear"), &AStar3D::clear);
  454. ClassDB::bind_method(D_METHOD("get_closest_point", "to_position", "include_disabled"), &AStar3D::get_closest_point, DEFVAL(false));
  455. ClassDB::bind_method(D_METHOD("get_closest_position_in_segment", "to_position"), &AStar3D::get_closest_position_in_segment);
  456. ClassDB::bind_method(D_METHOD("get_point_path", "from_id", "to_id", "allow_partial_path"), &AStar3D::get_point_path, DEFVAL(false));
  457. ClassDB::bind_method(D_METHOD("get_id_path", "from_id", "to_id", "allow_partial_path"), &AStar3D::get_id_path, DEFVAL(false));
  458. GDVIRTUAL_BIND(_estimate_cost, "from_id", "end_id")
  459. GDVIRTUAL_BIND(_compute_cost, "from_id", "to_id")
  460. }
  461. AStar3D::~AStar3D() {
  462. clear();
  463. }
  464. /////////////////////////////////////////////////////////////
  465. int64_t AStar2D::get_available_point_id() const {
  466. return astar.get_available_point_id();
  467. }
  468. void AStar2D::add_point(int64_t p_id, const Vector2 &p_pos, real_t p_weight_scale) {
  469. astar.add_point(p_id, Vector3(p_pos.x, p_pos.y, 0), p_weight_scale);
  470. }
  471. Vector2 AStar2D::get_point_position(int64_t p_id) const {
  472. Vector3 p = astar.get_point_position(p_id);
  473. return Vector2(p.x, p.y);
  474. }
  475. void AStar2D::set_point_position(int64_t p_id, const Vector2 &p_pos) {
  476. astar.set_point_position(p_id, Vector3(p_pos.x, p_pos.y, 0));
  477. }
  478. real_t AStar2D::get_point_weight_scale(int64_t p_id) const {
  479. return astar.get_point_weight_scale(p_id);
  480. }
  481. void AStar2D::set_point_weight_scale(int64_t p_id, real_t p_weight_scale) {
  482. astar.set_point_weight_scale(p_id, p_weight_scale);
  483. }
  484. void AStar2D::remove_point(int64_t p_id) {
  485. astar.remove_point(p_id);
  486. }
  487. bool AStar2D::has_point(int64_t p_id) const {
  488. return astar.has_point(p_id);
  489. }
  490. Vector<int64_t> AStar2D::get_point_connections(int64_t p_id) {
  491. return astar.get_point_connections(p_id);
  492. }
  493. PackedInt64Array AStar2D::get_point_ids() {
  494. return astar.get_point_ids();
  495. }
  496. void AStar2D::set_point_disabled(int64_t p_id, bool p_disabled) {
  497. astar.set_point_disabled(p_id, p_disabled);
  498. }
  499. bool AStar2D::is_point_disabled(int64_t p_id) const {
  500. return astar.is_point_disabled(p_id);
  501. }
  502. void AStar2D::connect_points(int64_t p_id, int64_t p_with_id, bool p_bidirectional) {
  503. astar.connect_points(p_id, p_with_id, p_bidirectional);
  504. }
  505. void AStar2D::disconnect_points(int64_t p_id, int64_t p_with_id, bool p_bidirectional) {
  506. astar.disconnect_points(p_id, p_with_id, p_bidirectional);
  507. }
  508. bool AStar2D::are_points_connected(int64_t p_id, int64_t p_with_id, bool p_bidirectional) const {
  509. return astar.are_points_connected(p_id, p_with_id, p_bidirectional);
  510. }
  511. int64_t AStar2D::get_point_count() const {
  512. return astar.get_point_count();
  513. }
  514. int64_t AStar2D::get_point_capacity() const {
  515. return astar.get_point_capacity();
  516. }
  517. void AStar2D::clear() {
  518. astar.clear();
  519. }
  520. void AStar2D::reserve_space(int64_t p_num_nodes) {
  521. astar.reserve_space(p_num_nodes);
  522. }
  523. int64_t AStar2D::get_closest_point(const Vector2 &p_point, bool p_include_disabled) const {
  524. return astar.get_closest_point(Vector3(p_point.x, p_point.y, 0), p_include_disabled);
  525. }
  526. Vector2 AStar2D::get_closest_position_in_segment(const Vector2 &p_point) const {
  527. Vector3 p = astar.get_closest_position_in_segment(Vector3(p_point.x, p_point.y, 0));
  528. return Vector2(p.x, p.y);
  529. }
  530. real_t AStar2D::_estimate_cost(int64_t p_from_id, int64_t p_end_id) {
  531. real_t scost;
  532. if (GDVIRTUAL_CALL(_estimate_cost, p_from_id, p_end_id, scost)) {
  533. return scost;
  534. }
  535. AStar3D::Point *from_point = nullptr;
  536. bool from_exists = astar.points.lookup(p_from_id, from_point);
  537. ERR_FAIL_COND_V_MSG(!from_exists, 0, vformat("Can't estimate cost. Point with id: %d doesn't exist.", p_from_id));
  538. AStar3D::Point *end_point = nullptr;
  539. bool to_exists = astar.points.lookup(p_end_id, end_point);
  540. ERR_FAIL_COND_V_MSG(!to_exists, 0, vformat("Can't estimate cost. Point with id: %d doesn't exist.", p_end_id));
  541. return from_point->pos.distance_to(end_point->pos);
  542. }
  543. real_t AStar2D::_compute_cost(int64_t p_from_id, int64_t p_to_id) {
  544. real_t scost;
  545. if (GDVIRTUAL_CALL(_compute_cost, p_from_id, p_to_id, scost)) {
  546. return scost;
  547. }
  548. AStar3D::Point *from_point = nullptr;
  549. bool from_exists = astar.points.lookup(p_from_id, from_point);
  550. ERR_FAIL_COND_V_MSG(!from_exists, 0, vformat("Can't compute cost. Point with id: %d doesn't exist.", p_from_id));
  551. AStar3D::Point *to_point = nullptr;
  552. bool to_exists = astar.points.lookup(p_to_id, to_point);
  553. ERR_FAIL_COND_V_MSG(!to_exists, 0, vformat("Can't compute cost. Point with id: %d doesn't exist.", p_to_id));
  554. return from_point->pos.distance_to(to_point->pos);
  555. }
  556. Vector<Vector2> AStar2D::get_point_path(int64_t p_from_id, int64_t p_to_id, bool p_allow_partial_path) {
  557. AStar3D::Point *a = nullptr;
  558. bool from_exists = astar.points.lookup(p_from_id, a);
  559. 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));
  560. AStar3D::Point *b = nullptr;
  561. bool to_exists = astar.points.lookup(p_to_id, b);
  562. 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));
  563. if (a == b) {
  564. Vector<Vector2> ret = { Vector2(a->pos.x, a->pos.y) };
  565. return ret;
  566. }
  567. AStar3D::Point *begin_point = a;
  568. AStar3D::Point *end_point = b;
  569. bool found_route = _solve(begin_point, end_point, p_allow_partial_path);
  570. if (!found_route) {
  571. if (!p_allow_partial_path || astar.last_closest_point == nullptr) {
  572. return Vector<Vector2>();
  573. }
  574. // Use closest point instead.
  575. end_point = astar.last_closest_point;
  576. }
  577. AStar3D::Point *p = end_point;
  578. int64_t pc = 1; // Begin point
  579. while (p != begin_point) {
  580. pc++;
  581. p = p->prev_point;
  582. }
  583. Vector<Vector2> path;
  584. path.resize(pc);
  585. {
  586. Vector2 *w = path.ptrw();
  587. AStar3D::Point *p2 = end_point;
  588. int64_t idx = pc - 1;
  589. while (p2 != begin_point) {
  590. w[idx--] = Vector2(p2->pos.x, p2->pos.y);
  591. p2 = p2->prev_point;
  592. }
  593. w[0] = Vector2(p2->pos.x, p2->pos.y); // Assign first
  594. }
  595. return path;
  596. }
  597. Vector<int64_t> AStar2D::get_id_path(int64_t p_from_id, int64_t p_to_id, bool p_allow_partial_path) {
  598. AStar3D::Point *a = nullptr;
  599. bool from_exists = astar.points.lookup(p_from_id, a);
  600. 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));
  601. AStar3D::Point *b = nullptr;
  602. bool to_exists = astar.points.lookup(p_to_id, b);
  603. 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));
  604. if (a == b) {
  605. Vector<int64_t> ret;
  606. ret.push_back(a->id);
  607. return ret;
  608. }
  609. AStar3D::Point *begin_point = a;
  610. AStar3D::Point *end_point = b;
  611. bool found_route = _solve(begin_point, end_point, p_allow_partial_path);
  612. if (!found_route) {
  613. if (!p_allow_partial_path || astar.last_closest_point == nullptr) {
  614. return Vector<int64_t>();
  615. }
  616. // Use closest point instead.
  617. end_point = astar.last_closest_point;
  618. }
  619. AStar3D::Point *p = end_point;
  620. int64_t pc = 1; // Begin point
  621. while (p != begin_point) {
  622. pc++;
  623. p = p->prev_point;
  624. }
  625. Vector<int64_t> path;
  626. path.resize(pc);
  627. {
  628. int64_t *w = path.ptrw();
  629. p = end_point;
  630. int64_t idx = pc - 1;
  631. while (p != begin_point) {
  632. w[idx--] = p->id;
  633. p = p->prev_point;
  634. }
  635. w[0] = p->id; // Assign first
  636. }
  637. return path;
  638. }
  639. bool AStar2D::_solve(AStar3D::Point *begin_point, AStar3D::Point *end_point, bool p_allow_partial_path) {
  640. astar.last_closest_point = nullptr;
  641. astar.pass++;
  642. if (!end_point->enabled && !p_allow_partial_path) {
  643. return false;
  644. }
  645. bool found_route = false;
  646. LocalVector<AStar3D::Point *> open_list;
  647. SortArray<AStar3D::Point *, AStar3D::SortPoints> sorter;
  648. begin_point->g_score = 0;
  649. begin_point->f_score = _estimate_cost(begin_point->id, end_point->id);
  650. begin_point->abs_g_score = 0;
  651. begin_point->abs_f_score = _estimate_cost(begin_point->id, end_point->id);
  652. open_list.push_back(begin_point);
  653. while (!open_list.is_empty()) {
  654. AStar3D::Point *p = open_list[0]; // The currently processed point.
  655. // Find point closer to end_point, or same distance to end_point but closer to begin_point.
  656. 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)) {
  657. astar.last_closest_point = p;
  658. }
  659. if (p == end_point) {
  660. found_route = true;
  661. break;
  662. }
  663. sorter.pop_heap(0, open_list.size(), open_list.ptr()); // Remove the current point from the open list.
  664. open_list.remove_at(open_list.size() - 1);
  665. p->closed_pass = astar.pass; // Mark the point as closed.
  666. for (OAHashMap<int64_t, AStar3D::Point *>::Iterator it = p->neighbors.iter(); it.valid; it = p->neighbors.next_iter(it)) {
  667. AStar3D::Point *e = *(it.value); // The neighbor point.
  668. if (!e->enabled || e->closed_pass == astar.pass) {
  669. continue;
  670. }
  671. real_t tentative_g_score = p->g_score + _compute_cost(p->id, e->id) * e->weight_scale;
  672. bool new_point = false;
  673. if (e->open_pass != astar.pass) { // The point wasn't inside the open list.
  674. e->open_pass = astar.pass;
  675. open_list.push_back(e);
  676. new_point = true;
  677. } else if (tentative_g_score >= e->g_score) { // The new path is worse than the previous.
  678. continue;
  679. }
  680. e->prev_point = p;
  681. e->g_score = tentative_g_score;
  682. e->f_score = e->g_score + _estimate_cost(e->id, end_point->id);
  683. e->abs_g_score = tentative_g_score;
  684. e->abs_f_score = e->f_score - e->g_score;
  685. if (new_point) { // The position of the new points is already known.
  686. sorter.push_heap(0, open_list.size() - 1, 0, e, open_list.ptr());
  687. } else {
  688. sorter.push_heap(0, open_list.find(e), 0, e, open_list.ptr());
  689. }
  690. }
  691. }
  692. return found_route;
  693. }
  694. void AStar2D::_bind_methods() {
  695. ClassDB::bind_method(D_METHOD("get_available_point_id"), &AStar2D::get_available_point_id);
  696. ClassDB::bind_method(D_METHOD("add_point", "id", "position", "weight_scale"), &AStar2D::add_point, DEFVAL(1.0));
  697. ClassDB::bind_method(D_METHOD("get_point_position", "id"), &AStar2D::get_point_position);
  698. ClassDB::bind_method(D_METHOD("set_point_position", "id", "position"), &AStar2D::set_point_position);
  699. ClassDB::bind_method(D_METHOD("get_point_weight_scale", "id"), &AStar2D::get_point_weight_scale);
  700. ClassDB::bind_method(D_METHOD("set_point_weight_scale", "id", "weight_scale"), &AStar2D::set_point_weight_scale);
  701. ClassDB::bind_method(D_METHOD("remove_point", "id"), &AStar2D::remove_point);
  702. ClassDB::bind_method(D_METHOD("has_point", "id"), &AStar2D::has_point);
  703. ClassDB::bind_method(D_METHOD("get_point_connections", "id"), &AStar2D::get_point_connections);
  704. ClassDB::bind_method(D_METHOD("get_point_ids"), &AStar2D::get_point_ids);
  705. ClassDB::bind_method(D_METHOD("set_point_disabled", "id", "disabled"), &AStar2D::set_point_disabled, DEFVAL(true));
  706. ClassDB::bind_method(D_METHOD("is_point_disabled", "id"), &AStar2D::is_point_disabled);
  707. ClassDB::bind_method(D_METHOD("connect_points", "id", "to_id", "bidirectional"), &AStar2D::connect_points, DEFVAL(true));
  708. ClassDB::bind_method(D_METHOD("disconnect_points", "id", "to_id", "bidirectional"), &AStar2D::disconnect_points, DEFVAL(true));
  709. ClassDB::bind_method(D_METHOD("are_points_connected", "id", "to_id", "bidirectional"), &AStar2D::are_points_connected, DEFVAL(true));
  710. ClassDB::bind_method(D_METHOD("get_point_count"), &AStar2D::get_point_count);
  711. ClassDB::bind_method(D_METHOD("get_point_capacity"), &AStar2D::get_point_capacity);
  712. ClassDB::bind_method(D_METHOD("reserve_space", "num_nodes"), &AStar2D::reserve_space);
  713. ClassDB::bind_method(D_METHOD("clear"), &AStar2D::clear);
  714. ClassDB::bind_method(D_METHOD("get_closest_point", "to_position", "include_disabled"), &AStar2D::get_closest_point, DEFVAL(false));
  715. ClassDB::bind_method(D_METHOD("get_closest_position_in_segment", "to_position"), &AStar2D::get_closest_position_in_segment);
  716. ClassDB::bind_method(D_METHOD("get_point_path", "from_id", "to_id", "allow_partial_path"), &AStar2D::get_point_path, DEFVAL(false));
  717. ClassDB::bind_method(D_METHOD("get_id_path", "from_id", "to_id", "allow_partial_path"), &AStar2D::get_id_path, DEFVAL(false));
  718. GDVIRTUAL_BIND(_estimate_cost, "from_id", "end_id")
  719. GDVIRTUAL_BIND(_compute_cost, "from_id", "to_id")
  720. }