a_star.cpp 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941
  1. /**************************************************************************/
  2. /* a_star.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #include "a_star.h"
  31. #include "a_star.compat.inc"
  32. #include "core/math/geometry_3d.h"
  33. int64_t AStar3D::get_available_point_id() const {
  34. if (points.has(last_free_id)) {
  35. int64_t cur_new_id = last_free_id + 1;
  36. while (points.has(cur_new_id)) {
  37. cur_new_id++;
  38. }
  39. last_free_id = cur_new_id;
  40. }
  41. return last_free_id;
  42. }
  43. void AStar3D::add_point(int64_t p_id, const Vector3 &p_pos, real_t p_weight_scale) {
  44. ERR_FAIL_COND_MSG(p_id < 0, vformat("Can't add a point with negative id: %d.", p_id));
  45. ERR_FAIL_COND_MSG(p_weight_scale < 0.0, vformat("Can't add a point with weight scale less than 0.0: %f.", p_weight_scale));
  46. Point **point_entry = points.getptr(p_id);
  47. if (!point_entry) {
  48. Point *pt = memnew(Point);
  49. pt->id = p_id;
  50. pt->pos = p_pos;
  51. pt->weight_scale = p_weight_scale;
  52. pt->prev_point = nullptr;
  53. pt->open_pass = 0;
  54. pt->closed_pass = 0;
  55. pt->enabled = true;
  56. points.insert_new(p_id, pt);
  57. } else {
  58. Point *found_pt = *point_entry;
  59. found_pt->pos = p_pos;
  60. found_pt->weight_scale = p_weight_scale;
  61. }
  62. }
  63. Vector3 AStar3D::get_point_position(int64_t p_id) const {
  64. Point *const *point_entry = points.getptr(p_id);
  65. ERR_FAIL_COND_V_MSG(!point_entry, Vector3(), vformat("Can't get point's position. Point with id: %d doesn't exist.", p_id));
  66. return (*point_entry)->pos;
  67. }
  68. void AStar3D::set_point_position(int64_t p_id, const Vector3 &p_pos) {
  69. Point **point_entry = points.getptr(p_id);
  70. ERR_FAIL_COND_MSG(!point_entry, vformat("Can't set point's position. Point with id: %d doesn't exist.", p_id));
  71. (*point_entry)->pos = p_pos;
  72. }
  73. real_t AStar3D::get_point_weight_scale(int64_t p_id) const {
  74. Point *const *point_entry = points.getptr(p_id);
  75. ERR_FAIL_COND_V_MSG(!point_entry, 0, vformat("Can't get point's weight scale. Point with id: %d doesn't exist.", p_id));
  76. return (*point_entry)->weight_scale;
  77. }
  78. void AStar3D::set_point_weight_scale(int64_t p_id, real_t p_weight_scale) {
  79. Point **point_entry = points.getptr(p_id);
  80. ERR_FAIL_COND_MSG(!point_entry, vformat("Can't set point's weight scale. Point with id: %d doesn't exist.", p_id));
  81. ERR_FAIL_COND_MSG(p_weight_scale < 0.0, vformat("Can't set point's weight scale less than 0.0: %f.", p_weight_scale));
  82. (*point_entry)->weight_scale = p_weight_scale;
  83. }
  84. void AStar3D::remove_point(int64_t p_id) {
  85. Point **point_entry = points.getptr(p_id);
  86. ERR_FAIL_COND_MSG(!point_entry, vformat("Can't remove point. Point with id: %d doesn't exist.", p_id));
  87. Point *p = *point_entry;
  88. for (KeyValue<int64_t, Point *> &kv : p->neighbors) {
  89. Segment s(p_id, kv.key);
  90. segments.erase(s);
  91. kv.value->neighbors.erase(p->id);
  92. kv.value->unlinked_neighbours.erase(p->id);
  93. }
  94. for (KeyValue<int64_t, Point *> &kv : p->unlinked_neighbours) {
  95. Segment s(p_id, kv.key);
  96. segments.erase(s);
  97. kv.value->neighbors.erase(p->id);
  98. kv.value->unlinked_neighbours.erase(p->id);
  99. }
  100. memdelete(p);
  101. points.erase(p_id);
  102. last_free_id = p_id;
  103. }
  104. void AStar3D::connect_points(int64_t p_id, int64_t p_with_id, bool bidirectional) {
  105. ERR_FAIL_COND_MSG(p_id == p_with_id, vformat("Can't connect point with id: %d to itself.", p_id));
  106. Point **a_entry = points.getptr(p_id);
  107. ERR_FAIL_COND_MSG(!a_entry, vformat("Can't connect points. Point with id: %d doesn't exist.", p_id));
  108. Point *a = *a_entry;
  109. Point **b_entry = points.getptr(p_with_id);
  110. ERR_FAIL_COND_MSG(!b_entry, vformat("Can't connect points. Point with id: %d doesn't exist.", p_with_id));
  111. Point *b = *b_entry;
  112. a->neighbors.insert(b->id, b);
  113. if (bidirectional) {
  114. b->neighbors.insert(a->id, a);
  115. } else {
  116. b->unlinked_neighbours.insert(a->id, a);
  117. }
  118. Segment s(p_id, p_with_id);
  119. if (bidirectional) {
  120. s.direction = Segment::BIDIRECTIONAL;
  121. }
  122. HashSet<Segment, Segment>::Iterator element = segments.find(s);
  123. if (element) {
  124. s.direction |= element->direction;
  125. if (s.direction == Segment::BIDIRECTIONAL) {
  126. // Both are neighbors of each other now
  127. a->unlinked_neighbours.erase(b->id);
  128. b->unlinked_neighbours.erase(a->id);
  129. }
  130. segments.remove(element);
  131. }
  132. segments.insert(s);
  133. }
  134. void AStar3D::disconnect_points(int64_t p_id, int64_t p_with_id, bool bidirectional) {
  135. Point **a_entry = points.getptr(p_id);
  136. ERR_FAIL_COND_MSG(!a_entry, vformat("Can't disconnect points. Point with id: %d doesn't exist.", p_id));
  137. Point *a = *a_entry;
  138. Point **b_entry = points.getptr(p_with_id);
  139. ERR_FAIL_COND_MSG(!b_entry, vformat("Can't disconnect points. Point with id: %d doesn't exist.", p_with_id));
  140. Point *b = *b_entry;
  141. Segment s(p_id, p_with_id);
  142. int remove_direction = bidirectional ? (int)Segment::BIDIRECTIONAL : (int)s.direction;
  143. HashSet<Segment, Segment>::Iterator element = segments.find(s);
  144. if (element) {
  145. // s is the new segment
  146. // Erase the directions to be removed
  147. s.direction = (element->direction & ~remove_direction);
  148. a->neighbors.erase(b->id);
  149. if (bidirectional) {
  150. b->neighbors.erase(a->id);
  151. if (element->direction != Segment::BIDIRECTIONAL) {
  152. a->unlinked_neighbours.erase(b->id);
  153. b->unlinked_neighbours.erase(a->id);
  154. }
  155. } else {
  156. if (s.direction == Segment::NONE) {
  157. b->unlinked_neighbours.erase(a->id);
  158. } else {
  159. a->unlinked_neighbours.insert(b->id, b);
  160. }
  161. }
  162. segments.remove(element);
  163. if (s.direction != Segment::NONE) {
  164. segments.insert(s);
  165. }
  166. }
  167. }
  168. bool AStar3D::has_point(int64_t p_id) const {
  169. return points.has(p_id);
  170. }
  171. PackedInt64Array AStar3D::get_point_ids() {
  172. PackedInt64Array point_list;
  173. for (KeyValue<int64_t, Point *> &kv : points) {
  174. point_list.push_back(kv.key);
  175. }
  176. return point_list;
  177. }
  178. Vector<int64_t> AStar3D::get_point_connections(int64_t p_id) {
  179. Point **p_entry = points.getptr(p_id);
  180. ERR_FAIL_COND_V_MSG(!p_entry, Vector<int64_t>(), vformat("Can't get point's connections. Point with id: %d doesn't exist.", p_id));
  181. Vector<int64_t> point_list;
  182. for (KeyValue<int64_t, Point *> &kv : (*p_entry)->neighbors) {
  183. point_list.push_back(kv.key);
  184. }
  185. return point_list;
  186. }
  187. bool AStar3D::are_points_connected(int64_t p_id, int64_t p_with_id, bool bidirectional) const {
  188. Segment s(p_id, p_with_id);
  189. const HashSet<Segment, Segment>::Iterator element = segments.find(s);
  190. return element &&
  191. (bidirectional || (element->direction & s.direction) == s.direction);
  192. }
  193. void AStar3D::clear() {
  194. last_free_id = 0;
  195. for (KeyValue<int64_t, Point *> &kv : points) {
  196. memdelete(kv.value);
  197. }
  198. segments.clear();
  199. points.clear();
  200. }
  201. int64_t AStar3D::get_point_count() const {
  202. return points.size();
  203. }
  204. int64_t AStar3D::get_point_capacity() const {
  205. return points.get_capacity();
  206. }
  207. void AStar3D::reserve_space(int64_t p_num_nodes) {
  208. ERR_FAIL_COND_MSG(p_num_nodes <= 0, vformat("New capacity must be greater than 0, new was: %d.", p_num_nodes));
  209. points.reserve(p_num_nodes);
  210. }
  211. int64_t AStar3D::get_closest_point(const Vector3 &p_point, bool p_include_disabled) const {
  212. int64_t closest_id = -1;
  213. real_t closest_dist = 1e20;
  214. for (const KeyValue<int64_t, Point *> &kv : points) {
  215. if (!p_include_disabled && !kv.value->enabled) {
  216. continue; // Disabled points should not be considered.
  217. }
  218. // Keep the closest point's ID, and in case of multiple closest IDs,
  219. // the smallest one (makes it deterministic).
  220. real_t d = p_point.distance_squared_to(kv.value->pos);
  221. int64_t id = kv.key;
  222. if (d <= closest_dist) {
  223. if (d == closest_dist && id > closest_id) { // Keep lowest ID.
  224. continue;
  225. }
  226. closest_dist = d;
  227. closest_id = id;
  228. }
  229. }
  230. return closest_id;
  231. }
  232. Vector3 AStar3D::get_closest_position_in_segment(const Vector3 &p_point) const {
  233. real_t closest_dist = 1e20;
  234. Vector3 closest_point;
  235. for (const Segment &E : segments) {
  236. const Point *from_point = *points.getptr(E.key.first);
  237. const Point *to_point = *points.getptr(E.key.second);
  238. if (!(from_point->enabled && to_point->enabled)) {
  239. continue;
  240. }
  241. Vector3 p = Geometry3D::get_closest_point_to_segment(p_point, from_point->pos, to_point->pos);
  242. real_t d = p_point.distance_squared_to(p);
  243. if (d < closest_dist) {
  244. closest_point = p;
  245. closest_dist = d;
  246. }
  247. }
  248. return closest_point;
  249. }
  250. bool AStar3D::_solve(Point *p_begin_point, Point *p_end_point, bool p_allow_partial_path) {
  251. last_closest_point = nullptr;
  252. pass++;
  253. if (!p_begin_point->enabled) {
  254. return false;
  255. }
  256. if (p_begin_point == p_end_point) {
  257. return true;
  258. }
  259. if (!p_end_point->enabled && !p_allow_partial_path) {
  260. return false;
  261. }
  262. bool found_route = false;
  263. LocalVector<Point *> open_list;
  264. SortArray<Point *, SortPoints> sorter;
  265. p_begin_point->g_score = 0;
  266. p_begin_point->f_score = _estimate_cost(p_begin_point->id, p_end_point->id);
  267. p_begin_point->abs_g_score = 0;
  268. p_begin_point->abs_f_score = _estimate_cost(p_begin_point->id, p_end_point->id);
  269. open_list.push_back(p_begin_point);
  270. while (!open_list.is_empty()) {
  271. Point *p = open_list[0]; // The currently processed point.
  272. // Find point closer to end_point, or same distance to end_point but closer to begin_point.
  273. 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)) {
  274. last_closest_point = p;
  275. }
  276. if (p == p_end_point) {
  277. found_route = true;
  278. break;
  279. }
  280. sorter.pop_heap(0, open_list.size(), open_list.ptr()); // Remove the current point from the open list.
  281. open_list.remove_at(open_list.size() - 1);
  282. p->closed_pass = pass; // Mark the point as closed.
  283. for (const KeyValue<int64_t, Point *> &kv : p->neighbors) {
  284. Point *e = kv.value; // The neighbor point.
  285. if (!e->enabled || e->closed_pass == pass) {
  286. continue;
  287. }
  288. if (neighbor_filter_enabled) {
  289. bool filtered;
  290. if (GDVIRTUAL_CALL(_filter_neighbor, p->id, e->id, filtered) && filtered) {
  291. continue;
  292. }
  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, p_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_end_id) {
  318. real_t scost;
  319. if (GDVIRTUAL_CALL(_estimate_cost, p_from_id, p_end_id, scost)) {
  320. return scost;
  321. }
  322. Point **from_entry = points.getptr(p_from_id);
  323. ERR_FAIL_COND_V_MSG(!from_entry, 0, vformat("Can't estimate cost. Point with id: %d doesn't exist.", p_from_id));
  324. Point *from_point = *from_entry;
  325. Point **end_entry = points.getptr(p_end_id);
  326. ERR_FAIL_COND_V_MSG(!end_entry, 0, vformat("Can't estimate cost. Point with id: %d doesn't exist.", p_end_id));
  327. Point *end_point = *end_entry;
  328. return from_point->pos.distance_to(end_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_entry = points.getptr(p_from_id);
  336. ERR_FAIL_COND_V_MSG(!from_entry, 0, vformat("Can't compute cost. Point with id: %d doesn't exist.", p_from_id));
  337. Point *from_point = *from_entry;
  338. Point **to_entry = points.getptr(p_to_id);
  339. ERR_FAIL_COND_V_MSG(!to_entry, 0, vformat("Can't compute cost. Point with id: %d doesn't exist.", p_to_id));
  340. Point *to_point = *to_entry;
  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_entry = points.getptr(p_from_id);
  345. ERR_FAIL_COND_V_MSG(!a_entry, Vector<Vector3>(), vformat("Can't get point path. Point with id: %d doesn't exist.", p_from_id));
  346. Point *a = *a_entry;
  347. Point **b_entry = points.getptr(p_to_id);
  348. ERR_FAIL_COND_V_MSG(!b_entry, Vector<Vector3>(), vformat("Can't get point path. Point with id: %d doesn't exist.", p_to_id));
  349. Point *b = *b_entry;
  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_entry = points.getptr(p_from_id);
  382. ERR_FAIL_COND_V_MSG(!a_entry, Vector<int64_t>(), vformat("Can't get id path. Point with id: %d doesn't exist.", p_from_id));
  383. Point *a = *a_entry;
  384. Point **b_entry = points.getptr(p_to_id);
  385. ERR_FAIL_COND_V_MSG(!b_entry, Vector<int64_t>(), vformat("Can't get id path. Point with id: %d doesn't exist.", p_to_id));
  386. Point *b = *b_entry;
  387. Point *begin_point = a;
  388. Point *end_point = b;
  389. bool found_route = _solve(begin_point, end_point, p_allow_partial_path);
  390. if (!found_route) {
  391. if (!p_allow_partial_path || last_closest_point == nullptr) {
  392. return Vector<int64_t>();
  393. }
  394. // Use closest point instead.
  395. end_point = last_closest_point;
  396. }
  397. Point *p = end_point;
  398. int64_t pc = 1; // Begin point
  399. while (p != begin_point) {
  400. pc++;
  401. p = p->prev_point;
  402. }
  403. Vector<int64_t> path;
  404. path.resize(pc);
  405. {
  406. int64_t *w = path.ptrw();
  407. p = end_point;
  408. int64_t idx = pc - 1;
  409. while (p != begin_point) {
  410. w[idx--] = p->id;
  411. p = p->prev_point;
  412. }
  413. w[0] = p->id; // Assign first
  414. }
  415. return path;
  416. }
  417. bool AStar3D::is_neighbor_filter_enabled() const {
  418. return neighbor_filter_enabled;
  419. }
  420. void AStar3D::set_neighbor_filter_enabled(bool p_enabled) {
  421. neighbor_filter_enabled = p_enabled;
  422. }
  423. void AStar3D::set_point_disabled(int64_t p_id, bool p_disabled) {
  424. Point **p_entry = points.getptr(p_id);
  425. ERR_FAIL_COND_MSG(!p_entry, vformat("Can't set if point is disabled. Point with id: %d doesn't exist.", p_id));
  426. Point *p = *p_entry;
  427. p->enabled = !p_disabled;
  428. }
  429. bool AStar3D::is_point_disabled(int64_t p_id) const {
  430. Point *const *p_entry = points.getptr(p_id);
  431. ERR_FAIL_COND_V_MSG(!p_entry, false, vformat("Can't get if point is disabled. Point with id: %d doesn't exist.", p_id));
  432. Point *p = *p_entry;
  433. return !p->enabled;
  434. }
  435. void AStar3D::_bind_methods() {
  436. ClassDB::bind_method(D_METHOD("get_available_point_id"), &AStar3D::get_available_point_id);
  437. ClassDB::bind_method(D_METHOD("add_point", "id", "position", "weight_scale"), &AStar3D::add_point, DEFVAL(1.0));
  438. ClassDB::bind_method(D_METHOD("get_point_position", "id"), &AStar3D::get_point_position);
  439. ClassDB::bind_method(D_METHOD("set_point_position", "id", "position"), &AStar3D::set_point_position);
  440. ClassDB::bind_method(D_METHOD("get_point_weight_scale", "id"), &AStar3D::get_point_weight_scale);
  441. ClassDB::bind_method(D_METHOD("set_point_weight_scale", "id", "weight_scale"), &AStar3D::set_point_weight_scale);
  442. ClassDB::bind_method(D_METHOD("remove_point", "id"), &AStar3D::remove_point);
  443. ClassDB::bind_method(D_METHOD("has_point", "id"), &AStar3D::has_point);
  444. ClassDB::bind_method(D_METHOD("get_point_connections", "id"), &AStar3D::get_point_connections);
  445. ClassDB::bind_method(D_METHOD("get_point_ids"), &AStar3D::get_point_ids);
  446. ClassDB::bind_method(D_METHOD("set_point_disabled", "id", "disabled"), &AStar3D::set_point_disabled, DEFVAL(true));
  447. ClassDB::bind_method(D_METHOD("is_point_disabled", "id"), &AStar3D::is_point_disabled);
  448. ClassDB::bind_method(D_METHOD("set_neighbor_filter_enabled", "enabled"), &AStar3D::set_neighbor_filter_enabled);
  449. ClassDB::bind_method(D_METHOD("is_neighbor_filter_enabled"), &AStar3D::is_neighbor_filter_enabled);
  450. ClassDB::bind_method(D_METHOD("connect_points", "id", "to_id", "bidirectional"), &AStar3D::connect_points, DEFVAL(true));
  451. ClassDB::bind_method(D_METHOD("disconnect_points", "id", "to_id", "bidirectional"), &AStar3D::disconnect_points, DEFVAL(true));
  452. ClassDB::bind_method(D_METHOD("are_points_connected", "id", "to_id", "bidirectional"), &AStar3D::are_points_connected, DEFVAL(true));
  453. ClassDB::bind_method(D_METHOD("get_point_count"), &AStar3D::get_point_count);
  454. ClassDB::bind_method(D_METHOD("get_point_capacity"), &AStar3D::get_point_capacity);
  455. ClassDB::bind_method(D_METHOD("reserve_space", "num_nodes"), &AStar3D::reserve_space);
  456. ClassDB::bind_method(D_METHOD("clear"), &AStar3D::clear);
  457. ClassDB::bind_method(D_METHOD("get_closest_point", "to_position", "include_disabled"), &AStar3D::get_closest_point, DEFVAL(false));
  458. ClassDB::bind_method(D_METHOD("get_closest_position_in_segment", "to_position"), &AStar3D::get_closest_position_in_segment);
  459. ClassDB::bind_method(D_METHOD("get_point_path", "from_id", "to_id", "allow_partial_path"), &AStar3D::get_point_path, DEFVAL(false));
  460. ClassDB::bind_method(D_METHOD("get_id_path", "from_id", "to_id", "allow_partial_path"), &AStar3D::get_id_path, DEFVAL(false));
  461. GDVIRTUAL_BIND(_filter_neighbor, "from_id", "neighbor_id")
  462. GDVIRTUAL_BIND(_estimate_cost, "from_id", "end_id")
  463. GDVIRTUAL_BIND(_compute_cost, "from_id", "to_id")
  464. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "neighbor_filter_enabled"), "set_neighbor_filter_enabled", "is_neighbor_filter_enabled");
  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. bool AStar2D::is_neighbor_filter_enabled() const {
  502. return astar.neighbor_filter_enabled;
  503. }
  504. void AStar2D::set_neighbor_filter_enabled(bool p_enabled) {
  505. astar.neighbor_filter_enabled = p_enabled;
  506. }
  507. void AStar2D::set_point_disabled(int64_t p_id, bool p_disabled) {
  508. astar.set_point_disabled(p_id, p_disabled);
  509. }
  510. bool AStar2D::is_point_disabled(int64_t p_id) const {
  511. return astar.is_point_disabled(p_id);
  512. }
  513. void AStar2D::connect_points(int64_t p_id, int64_t p_with_id, bool p_bidirectional) {
  514. astar.connect_points(p_id, p_with_id, p_bidirectional);
  515. }
  516. void AStar2D::disconnect_points(int64_t p_id, int64_t p_with_id, bool p_bidirectional) {
  517. astar.disconnect_points(p_id, p_with_id, p_bidirectional);
  518. }
  519. bool AStar2D::are_points_connected(int64_t p_id, int64_t p_with_id, bool p_bidirectional) const {
  520. return astar.are_points_connected(p_id, p_with_id, p_bidirectional);
  521. }
  522. int64_t AStar2D::get_point_count() const {
  523. return astar.get_point_count();
  524. }
  525. int64_t AStar2D::get_point_capacity() const {
  526. return astar.get_point_capacity();
  527. }
  528. void AStar2D::clear() {
  529. astar.clear();
  530. }
  531. void AStar2D::reserve_space(int64_t p_num_nodes) {
  532. astar.reserve_space(p_num_nodes);
  533. }
  534. int64_t AStar2D::get_closest_point(const Vector2 &p_point, bool p_include_disabled) const {
  535. return astar.get_closest_point(Vector3(p_point.x, p_point.y, 0), p_include_disabled);
  536. }
  537. Vector2 AStar2D::get_closest_position_in_segment(const Vector2 &p_point) const {
  538. Vector3 p = astar.get_closest_position_in_segment(Vector3(p_point.x, p_point.y, 0));
  539. return Vector2(p.x, p.y);
  540. }
  541. real_t AStar2D::_estimate_cost(int64_t p_from_id, int64_t p_end_id) {
  542. real_t scost;
  543. if (GDVIRTUAL_CALL(_estimate_cost, p_from_id, p_end_id, scost)) {
  544. return scost;
  545. }
  546. AStar3D::Point **from_entry = astar.points.getptr(p_from_id);
  547. ERR_FAIL_COND_V_MSG(!from_entry, 0, vformat("Can't estimate cost. Point with id: %d doesn't exist.", p_from_id));
  548. AStar3D::Point *from_point = *from_entry;
  549. AStar3D::Point **end_entry = astar.points.getptr(p_end_id);
  550. ERR_FAIL_COND_V_MSG(!end_entry, 0, vformat("Can't estimate cost. Point with id: %d doesn't exist.", p_end_id));
  551. AStar3D::Point *end_point = *end_entry;
  552. return from_point->pos.distance_to(end_point->pos);
  553. }
  554. real_t AStar2D::_compute_cost(int64_t p_from_id, int64_t p_to_id) {
  555. real_t scost;
  556. if (GDVIRTUAL_CALL(_compute_cost, p_from_id, p_to_id, scost)) {
  557. return scost;
  558. }
  559. AStar3D::Point **from_entry = astar.points.getptr(p_from_id);
  560. ERR_FAIL_COND_V_MSG(!from_entry, 0, vformat("Can't compute cost. Point with id: %d doesn't exist.", p_from_id));
  561. AStar3D::Point *from_point = *from_entry;
  562. AStar3D::Point **to_entry = astar.points.getptr(p_to_id);
  563. ERR_FAIL_COND_V_MSG(!to_entry, 0, vformat("Can't compute cost. Point with id: %d doesn't exist.", p_to_id));
  564. AStar3D::Point *to_point = *to_entry;
  565. return from_point->pos.distance_to(to_point->pos);
  566. }
  567. Vector<Vector2> AStar2D::get_point_path(int64_t p_from_id, int64_t p_to_id, bool p_allow_partial_path) {
  568. AStar3D::Point **a_entry = astar.points.getptr(p_from_id);
  569. ERR_FAIL_COND_V_MSG(!a_entry, Vector<Vector2>(), vformat("Can't get point path. Point with id: %d doesn't exist.", p_from_id));
  570. AStar3D::Point *a = *a_entry;
  571. AStar3D::Point **b_entry = astar.points.getptr(p_to_id);
  572. ERR_FAIL_COND_V_MSG(!b_entry, Vector<Vector2>(), vformat("Can't get point path. Point with id: %d doesn't exist.", p_to_id));
  573. AStar3D::Point *b = *b_entry;
  574. AStar3D::Point *begin_point = a;
  575. AStar3D::Point *end_point = b;
  576. bool found_route = _solve(begin_point, end_point, p_allow_partial_path);
  577. if (!found_route) {
  578. if (!p_allow_partial_path || astar.last_closest_point == nullptr) {
  579. return Vector<Vector2>();
  580. }
  581. // Use closest point instead.
  582. end_point = astar.last_closest_point;
  583. }
  584. AStar3D::Point *p = end_point;
  585. int64_t pc = 1; // Begin point
  586. while (p != begin_point) {
  587. pc++;
  588. p = p->prev_point;
  589. }
  590. Vector<Vector2> path;
  591. path.resize(pc);
  592. {
  593. Vector2 *w = path.ptrw();
  594. AStar3D::Point *p2 = end_point;
  595. int64_t idx = pc - 1;
  596. while (p2 != begin_point) {
  597. w[idx--] = Vector2(p2->pos.x, p2->pos.y);
  598. p2 = p2->prev_point;
  599. }
  600. w[0] = Vector2(p2->pos.x, p2->pos.y); // Assign first
  601. }
  602. return path;
  603. }
  604. Vector<int64_t> AStar2D::get_id_path(int64_t p_from_id, int64_t p_to_id, bool p_allow_partial_path) {
  605. AStar3D::Point **a_entry = astar.points.getptr(p_from_id);
  606. ERR_FAIL_COND_V_MSG(!a_entry, Vector<int64_t>(), vformat("Can't get id path. Point with id: %d doesn't exist.", p_from_id));
  607. AStar3D::Point *a = *a_entry;
  608. AStar3D::Point **to_entry = astar.points.getptr(p_to_id);
  609. ERR_FAIL_COND_V_MSG(!to_entry, Vector<int64_t>(), vformat("Can't get id path. Point with id: %d doesn't exist.", p_to_id));
  610. AStar3D::Point *b = *to_entry;
  611. AStar3D::Point *begin_point = a;
  612. AStar3D::Point *end_point = b;
  613. bool found_route = _solve(begin_point, end_point, p_allow_partial_path);
  614. if (!found_route) {
  615. if (!p_allow_partial_path || astar.last_closest_point == nullptr) {
  616. return Vector<int64_t>();
  617. }
  618. // Use closest point instead.
  619. end_point = astar.last_closest_point;
  620. }
  621. AStar3D::Point *p = end_point;
  622. int64_t pc = 1; // Begin point
  623. while (p != begin_point) {
  624. pc++;
  625. p = p->prev_point;
  626. }
  627. Vector<int64_t> path;
  628. path.resize(pc);
  629. {
  630. int64_t *w = path.ptrw();
  631. p = end_point;
  632. int64_t idx = pc - 1;
  633. while (p != begin_point) {
  634. w[idx--] = p->id;
  635. p = p->prev_point;
  636. }
  637. w[0] = p->id; // Assign first
  638. }
  639. return path;
  640. }
  641. bool AStar2D::_solve(AStar3D::Point *p_begin_point, AStar3D::Point *p_end_point, bool p_allow_partial_path) {
  642. astar.last_closest_point = nullptr;
  643. astar.pass++;
  644. if (!p_begin_point->enabled) {
  645. return false;
  646. }
  647. if (p_begin_point == p_end_point) {
  648. return true;
  649. }
  650. if (!p_end_point->enabled && !p_allow_partial_path) {
  651. return false;
  652. }
  653. bool found_route = false;
  654. LocalVector<AStar3D::Point *> open_list;
  655. SortArray<AStar3D::Point *, AStar3D::SortPoints> sorter;
  656. p_begin_point->g_score = 0;
  657. p_begin_point->f_score = _estimate_cost(p_begin_point->id, p_end_point->id);
  658. p_begin_point->abs_g_score = 0;
  659. p_begin_point->abs_f_score = _estimate_cost(p_begin_point->id, p_end_point->id);
  660. open_list.push_back(p_begin_point);
  661. while (!open_list.is_empty()) {
  662. AStar3D::Point *p = open_list[0]; // The currently processed point.
  663. // Find point closer to end_point, or same distance to end_point but closer to begin_point.
  664. 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)) {
  665. astar.last_closest_point = p;
  666. }
  667. if (p == p_end_point) {
  668. found_route = true;
  669. break;
  670. }
  671. sorter.pop_heap(0, open_list.size(), open_list.ptr()); // Remove the current point from the open list.
  672. open_list.remove_at(open_list.size() - 1);
  673. p->closed_pass = astar.pass; // Mark the point as closed.
  674. for (KeyValue<int64_t, AStar3D::Point *> &kv : p->neighbors) {
  675. AStar3D::Point *e = kv.value; // The neighbor point.
  676. if (!e->enabled || e->closed_pass == astar.pass) {
  677. continue;
  678. }
  679. if (astar.neighbor_filter_enabled) {
  680. bool filtered;
  681. if (GDVIRTUAL_CALL(_filter_neighbor, p->id, e->id, filtered) && filtered) {
  682. continue;
  683. }
  684. }
  685. real_t tentative_g_score = p->g_score + _compute_cost(p->id, e->id) * e->weight_scale;
  686. bool new_point = false;
  687. if (e->open_pass != astar.pass) { // The point wasn't inside the open list.
  688. e->open_pass = astar.pass;
  689. open_list.push_back(e);
  690. new_point = true;
  691. } else if (tentative_g_score >= e->g_score) { // The new path is worse than the previous.
  692. continue;
  693. }
  694. e->prev_point = p;
  695. e->g_score = tentative_g_score;
  696. e->f_score = e->g_score + _estimate_cost(e->id, p_end_point->id);
  697. e->abs_g_score = tentative_g_score;
  698. e->abs_f_score = e->f_score - e->g_score;
  699. if (new_point) { // The position of the new points is already known.
  700. sorter.push_heap(0, open_list.size() - 1, 0, e, open_list.ptr());
  701. } else {
  702. sorter.push_heap(0, open_list.find(e), 0, e, open_list.ptr());
  703. }
  704. }
  705. }
  706. return found_route;
  707. }
  708. void AStar2D::_bind_methods() {
  709. ClassDB::bind_method(D_METHOD("get_available_point_id"), &AStar2D::get_available_point_id);
  710. ClassDB::bind_method(D_METHOD("add_point", "id", "position", "weight_scale"), &AStar2D::add_point, DEFVAL(1.0));
  711. ClassDB::bind_method(D_METHOD("get_point_position", "id"), &AStar2D::get_point_position);
  712. ClassDB::bind_method(D_METHOD("set_point_position", "id", "position"), &AStar2D::set_point_position);
  713. ClassDB::bind_method(D_METHOD("get_point_weight_scale", "id"), &AStar2D::get_point_weight_scale);
  714. ClassDB::bind_method(D_METHOD("set_point_weight_scale", "id", "weight_scale"), &AStar2D::set_point_weight_scale);
  715. ClassDB::bind_method(D_METHOD("remove_point", "id"), &AStar2D::remove_point);
  716. ClassDB::bind_method(D_METHOD("has_point", "id"), &AStar2D::has_point);
  717. ClassDB::bind_method(D_METHOD("get_point_connections", "id"), &AStar2D::get_point_connections);
  718. ClassDB::bind_method(D_METHOD("get_point_ids"), &AStar2D::get_point_ids);
  719. ClassDB::bind_method(D_METHOD("set_neighbor_filter_enabled", "enabled"), &AStar2D::set_neighbor_filter_enabled);
  720. ClassDB::bind_method(D_METHOD("is_neighbor_filter_enabled"), &AStar2D::is_neighbor_filter_enabled);
  721. ClassDB::bind_method(D_METHOD("set_point_disabled", "id", "disabled"), &AStar2D::set_point_disabled, DEFVAL(true));
  722. ClassDB::bind_method(D_METHOD("is_point_disabled", "id"), &AStar2D::is_point_disabled);
  723. ClassDB::bind_method(D_METHOD("connect_points", "id", "to_id", "bidirectional"), &AStar2D::connect_points, DEFVAL(true));
  724. ClassDB::bind_method(D_METHOD("disconnect_points", "id", "to_id", "bidirectional"), &AStar2D::disconnect_points, DEFVAL(true));
  725. ClassDB::bind_method(D_METHOD("are_points_connected", "id", "to_id", "bidirectional"), &AStar2D::are_points_connected, DEFVAL(true));
  726. ClassDB::bind_method(D_METHOD("get_point_count"), &AStar2D::get_point_count);
  727. ClassDB::bind_method(D_METHOD("get_point_capacity"), &AStar2D::get_point_capacity);
  728. ClassDB::bind_method(D_METHOD("reserve_space", "num_nodes"), &AStar2D::reserve_space);
  729. ClassDB::bind_method(D_METHOD("clear"), &AStar2D::clear);
  730. ClassDB::bind_method(D_METHOD("get_closest_point", "to_position", "include_disabled"), &AStar2D::get_closest_point, DEFVAL(false));
  731. ClassDB::bind_method(D_METHOD("get_closest_position_in_segment", "to_position"), &AStar2D::get_closest_position_in_segment);
  732. ClassDB::bind_method(D_METHOD("get_point_path", "from_id", "to_id", "allow_partial_path"), &AStar2D::get_point_path, DEFVAL(false));
  733. ClassDB::bind_method(D_METHOD("get_id_path", "from_id", "to_id", "allow_partial_path"), &AStar2D::get_id_path, DEFVAL(false));
  734. GDVIRTUAL_BIND(_filter_neighbor, "from_id", "neighbor_id")
  735. GDVIRTUAL_BIND(_estimate_cost, "from_id", "end_id")
  736. GDVIRTUAL_BIND(_compute_cost, "from_id", "to_id")
  737. ADD_PROPERTY(PropertyInfo(Variant::BOOL, "neighbor_filter_enabled"), "set_neighbor_filter_enabled", "is_neighbor_filter_enabled");
  738. }