a_star.cpp 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891
  1. /*************************************************************************/
  2. /* a_star.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
  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 "core/math/geometry.h"
  32. #include "core/script_language.h"
  33. #include "scene/scene_string_names.h"
  34. int AStar::get_available_point_id() const {
  35. if (points.has(last_free_id)) {
  36. int cur_new_id = last_free_id + 1;
  37. while (points.has(cur_new_id)) {
  38. cur_new_id++;
  39. }
  40. const_cast<int &>(last_free_id) = cur_new_id;
  41. }
  42. return last_free_id;
  43. }
  44. void AStar::add_point(int 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 < 1, vformat("Can't add a point with weight scale less than one: %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 AStar::get_point_position(int p_id) const {
  65. Point *p;
  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 AStar::set_point_position(int p_id, const Vector3 &p_pos) {
  71. Point *p;
  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 AStar::get_point_weight_scale(int p_id) const {
  77. Point *p;
  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 AStar::set_point_weight_scale(int p_id, real_t p_weight_scale) {
  83. Point *p;
  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 < 1, vformat("Can't set point's weight scale less than one: %f.", p_weight_scale));
  87. p->weight_scale = p_weight_scale;
  88. }
  89. void AStar::remove_point(int p_id) {
  90. Point *p;
  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<int, Point *>::Iterator it = p->neighbours.iter(); it.valid; it = p->neighbours.next_iter(it)) {
  94. Segment s(p_id, (*it.key));
  95. segments.erase(s);
  96. (*it.value)->neighbours.remove(p->id);
  97. (*it.value)->unlinked_neighbours.remove(p->id);
  98. }
  99. for (OAHashMap<int, 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)->neighbours.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 AStar::connect_points(int p_id, int 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;
  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;
  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->neighbours.set(b->id, b);
  118. if (bidirectional) {
  119. b->neighbours.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. Set<Segment>::Element *element = segments.find(s);
  128. if (element != nullptr) {
  129. s.direction |= element->get().direction;
  130. if (s.direction == Segment::BIDIRECTIONAL) {
  131. // Both are neighbours of each other now
  132. a->unlinked_neighbours.remove(b->id);
  133. b->unlinked_neighbours.remove(a->id);
  134. }
  135. segments.erase(element);
  136. }
  137. segments.insert(s);
  138. }
  139. void AStar::disconnect_points(int p_id, int p_with_id, bool bidirectional) {
  140. Point *a;
  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;
  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 : s.direction;
  148. Set<Segment>::Element *element = segments.find(s);
  149. if (element != nullptr) {
  150. // s is the new segment
  151. // Erase the directions to be removed
  152. s.direction = (element->get().direction & ~remove_direction);
  153. a->neighbours.remove(b->id);
  154. if (bidirectional) {
  155. b->neighbours.remove(a->id);
  156. if (element->get().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.erase(element);
  168. if (s.direction != Segment::NONE) {
  169. segments.insert(s);
  170. }
  171. }
  172. }
  173. bool AStar::has_point(int p_id) const {
  174. return points.has(p_id);
  175. }
  176. Array AStar::get_points() {
  177. Array point_list;
  178. for (OAHashMap<int, 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. PoolVector<int> AStar::get_point_connections(int p_id) {
  184. Point *p;
  185. bool p_exists = points.lookup(p_id, p);
  186. ERR_FAIL_COND_V_MSG(!p_exists, PoolVector<int>(), vformat("Can't get point's connections. Point with id: %d doesn't exist.", p_id));
  187. PoolVector<int> point_list;
  188. for (OAHashMap<int, Point *>::Iterator it = p->neighbours.iter(); it.valid; it = p->neighbours.next_iter(it)) {
  189. point_list.push_back((*it.key));
  190. }
  191. return point_list;
  192. }
  193. bool AStar::are_points_connected(int p_id, int p_with_id, bool bidirectional) const {
  194. Segment s(p_id, p_with_id);
  195. const Set<Segment>::Element *element = segments.find(s);
  196. return element != nullptr &&
  197. (bidirectional || (element->get().direction & s.direction) == s.direction);
  198. }
  199. void AStar::clear() {
  200. last_free_id = 0;
  201. for (OAHashMap<int, 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. int AStar::get_point_count() const {
  208. return points.get_num_elements();
  209. }
  210. int AStar::get_point_capacity() const {
  211. return points.get_capacity();
  212. }
  213. void AStar::reserve_space(int 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. int AStar::get_closest_point(const Vector3 &p_point, bool p_include_disabled) const {
  219. int closest_id = -1;
  220. real_t closest_dist = 1e20;
  221. for (OAHashMap<int, 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. int 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 AStar::get_closest_position_in_segment(const Vector3 &p_point) const {
  240. real_t closest_dist = 1e20;
  241. Vector3 closest_point;
  242. for (const Set<Segment>::Element *E = segments.front(); E; E = E->next()) {
  243. Point *from_point = nullptr, *to_point = nullptr;
  244. points.lookup(E->get().u, from_point);
  245. points.lookup(E->get().v, 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 = Geometry::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 AStar::_solve(Point *begin_point, Point *end_point) {
  263. pass++;
  264. if (!end_point->enabled) {
  265. return false;
  266. }
  267. bool found_route = false;
  268. Vector<Point *> open_list;
  269. SortArray<Point *, SortPoints> sorter;
  270. begin_point->g_score = 0;
  271. begin_point->f_score = _estimate_cost(begin_point->id, end_point->id);
  272. open_list.push_back(begin_point);
  273. while (!open_list.empty()) {
  274. Point *p = open_list[0]; // The currently processed point
  275. if (p == end_point) {
  276. found_route = true;
  277. break;
  278. }
  279. sorter.pop_heap(0, open_list.size(), open_list.ptrw()); // Remove the current point from the open list
  280. open_list.remove(open_list.size() - 1);
  281. p->closed_pass = pass; // Mark the point as closed
  282. for (OAHashMap<int, Point *>::Iterator it = p->neighbours.iter(); it.valid; it = p->neighbours.next_iter(it)) {
  283. Point *e = *(it.value); // The neighbour point
  284. if (!e->enabled || e->closed_pass == pass) {
  285. continue;
  286. }
  287. real_t tentative_g_score = p->g_score + _compute_cost(p->id, e->id) * e->weight_scale;
  288. bool new_point = false;
  289. if (e->open_pass != pass) { // The point wasn't inside the open list.
  290. e->open_pass = pass;
  291. open_list.push_back(e);
  292. new_point = true;
  293. } else if (tentative_g_score >= e->g_score) { // The new path is worse than the previous.
  294. continue;
  295. }
  296. e->prev_point = p;
  297. e->g_score = tentative_g_score;
  298. e->f_score = e->g_score + _estimate_cost(e->id, end_point->id);
  299. if (new_point) { // The position of the new points is already known.
  300. sorter.push_heap(0, open_list.size() - 1, 0, e, open_list.ptrw());
  301. } else {
  302. sorter.push_heap(0, open_list.find(e), 0, e, open_list.ptrw());
  303. }
  304. }
  305. }
  306. return found_route;
  307. }
  308. real_t AStar::_estimate_cost(int p_from_id, int p_to_id) {
  309. if (get_script_instance() && get_script_instance()->has_method(SceneStringNames::get_singleton()->_estimate_cost)) {
  310. return get_script_instance()->call(SceneStringNames::get_singleton()->_estimate_cost, p_from_id, p_to_id);
  311. }
  312. Point *from_point;
  313. bool from_exists = points.lookup(p_from_id, from_point);
  314. ERR_FAIL_COND_V_MSG(!from_exists, 0, vformat("Can't estimate cost. Point with id: %d doesn't exist.", p_from_id));
  315. Point *to_point;
  316. bool to_exists = points.lookup(p_to_id, to_point);
  317. ERR_FAIL_COND_V_MSG(!to_exists, 0, vformat("Can't estimate cost. Point with id: %d doesn't exist.", p_to_id));
  318. return from_point->pos.distance_to(to_point->pos);
  319. }
  320. real_t AStar::_compute_cost(int p_from_id, int p_to_id) {
  321. if (get_script_instance() && get_script_instance()->has_method(SceneStringNames::get_singleton()->_compute_cost)) {
  322. return get_script_instance()->call(SceneStringNames::get_singleton()->_compute_cost, p_from_id, p_to_id);
  323. }
  324. Point *from_point;
  325. bool from_exists = points.lookup(p_from_id, from_point);
  326. ERR_FAIL_COND_V_MSG(!from_exists, 0, vformat("Can't compute cost. Point with id: %d doesn't exist.", p_from_id));
  327. Point *to_point;
  328. bool to_exists = points.lookup(p_to_id, to_point);
  329. ERR_FAIL_COND_V_MSG(!to_exists, 0, vformat("Can't compute cost. Point with id: %d doesn't exist.", p_to_id));
  330. return from_point->pos.distance_to(to_point->pos);
  331. }
  332. PoolVector<Vector3> AStar::get_point_path(int p_from_id, int p_to_id) {
  333. Point *a;
  334. bool from_exists = points.lookup(p_from_id, a);
  335. ERR_FAIL_COND_V_MSG(!from_exists, PoolVector<Vector3>(), vformat("Can't get point path. Point with id: %d doesn't exist.", p_from_id));
  336. Point *b;
  337. bool to_exists = points.lookup(p_to_id, b);
  338. ERR_FAIL_COND_V_MSG(!to_exists, PoolVector<Vector3>(), vformat("Can't get point path. Point with id: %d doesn't exist.", p_to_id));
  339. if (a == b) {
  340. PoolVector<Vector3> ret;
  341. ret.push_back(a->pos);
  342. return ret;
  343. }
  344. Point *begin_point = a;
  345. Point *end_point = b;
  346. bool found_route = _solve(begin_point, end_point);
  347. if (!found_route) {
  348. return PoolVector<Vector3>();
  349. }
  350. Point *p = end_point;
  351. int pc = 1; // Begin point
  352. while (p != begin_point) {
  353. pc++;
  354. p = p->prev_point;
  355. }
  356. PoolVector<Vector3> path;
  357. path.resize(pc);
  358. {
  359. PoolVector<Vector3>::Write w = path.write();
  360. Point *p2 = end_point;
  361. int idx = pc - 1;
  362. while (p2 != begin_point) {
  363. w[idx--] = p2->pos;
  364. p2 = p2->prev_point;
  365. }
  366. w[0] = p2->pos; // Assign first
  367. }
  368. return path;
  369. }
  370. PoolVector<int> AStar::get_id_path(int p_from_id, int p_to_id) {
  371. Point *a;
  372. bool from_exists = points.lookup(p_from_id, a);
  373. ERR_FAIL_COND_V_MSG(!from_exists, PoolVector<int>(), vformat("Can't get id path. Point with id: %d doesn't exist.", p_from_id));
  374. Point *b;
  375. bool to_exists = points.lookup(p_to_id, b);
  376. ERR_FAIL_COND_V_MSG(!to_exists, PoolVector<int>(), vformat("Can't get id path. Point with id: %d doesn't exist.", p_to_id));
  377. if (a == b) {
  378. PoolVector<int> ret;
  379. ret.push_back(a->id);
  380. return ret;
  381. }
  382. Point *begin_point = a;
  383. Point *end_point = b;
  384. bool found_route = _solve(begin_point, end_point);
  385. if (!found_route) {
  386. return PoolVector<int>();
  387. }
  388. Point *p = end_point;
  389. int pc = 1; // Begin point
  390. while (p != begin_point) {
  391. pc++;
  392. p = p->prev_point;
  393. }
  394. PoolVector<int> path;
  395. path.resize(pc);
  396. {
  397. PoolVector<int>::Write w = path.write();
  398. p = end_point;
  399. int idx = pc - 1;
  400. while (p != begin_point) {
  401. w[idx--] = p->id;
  402. p = p->prev_point;
  403. }
  404. w[0] = p->id; // Assign first
  405. }
  406. return path;
  407. }
  408. void AStar::set_point_disabled(int p_id, bool p_disabled) {
  409. Point *p;
  410. bool p_exists = points.lookup(p_id, p);
  411. ERR_FAIL_COND_MSG(!p_exists, vformat("Can't set if point is disabled. Point with id: %d doesn't exist.", p_id));
  412. p->enabled = !p_disabled;
  413. }
  414. bool AStar::is_point_disabled(int p_id) const {
  415. Point *p;
  416. bool p_exists = points.lookup(p_id, p);
  417. 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));
  418. return !p->enabled;
  419. }
  420. void AStar::_bind_methods() {
  421. ClassDB::bind_method(D_METHOD("get_available_point_id"), &AStar::get_available_point_id);
  422. ClassDB::bind_method(D_METHOD("add_point", "id", "position", "weight_scale"), &AStar::add_point, DEFVAL(1.0));
  423. ClassDB::bind_method(D_METHOD("get_point_position", "id"), &AStar::get_point_position);
  424. ClassDB::bind_method(D_METHOD("set_point_position", "id", "position"), &AStar::set_point_position);
  425. ClassDB::bind_method(D_METHOD("get_point_weight_scale", "id"), &AStar::get_point_weight_scale);
  426. ClassDB::bind_method(D_METHOD("set_point_weight_scale", "id", "weight_scale"), &AStar::set_point_weight_scale);
  427. ClassDB::bind_method(D_METHOD("remove_point", "id"), &AStar::remove_point);
  428. ClassDB::bind_method(D_METHOD("has_point", "id"), &AStar::has_point);
  429. ClassDB::bind_method(D_METHOD("get_point_connections", "id"), &AStar::get_point_connections);
  430. ClassDB::bind_method(D_METHOD("get_points"), &AStar::get_points);
  431. ClassDB::bind_method(D_METHOD("set_point_disabled", "id", "disabled"), &AStar::set_point_disabled, DEFVAL(true));
  432. ClassDB::bind_method(D_METHOD("is_point_disabled", "id"), &AStar::is_point_disabled);
  433. ClassDB::bind_method(D_METHOD("connect_points", "id", "to_id", "bidirectional"), &AStar::connect_points, DEFVAL(true));
  434. ClassDB::bind_method(D_METHOD("disconnect_points", "id", "to_id", "bidirectional"), &AStar::disconnect_points, DEFVAL(true));
  435. ClassDB::bind_method(D_METHOD("are_points_connected", "id", "to_id", "bidirectional"), &AStar::are_points_connected, DEFVAL(true));
  436. ClassDB::bind_method(D_METHOD("get_point_count"), &AStar::get_point_count);
  437. ClassDB::bind_method(D_METHOD("get_point_capacity"), &AStar::get_point_capacity);
  438. ClassDB::bind_method(D_METHOD("reserve_space", "num_nodes"), &AStar::reserve_space);
  439. ClassDB::bind_method(D_METHOD("clear"), &AStar::clear);
  440. ClassDB::bind_method(D_METHOD("get_closest_point", "to_position", "include_disabled"), &AStar::get_closest_point, DEFVAL(false));
  441. ClassDB::bind_method(D_METHOD("get_closest_position_in_segment", "to_position"), &AStar::get_closest_position_in_segment);
  442. ClassDB::bind_method(D_METHOD("get_point_path", "from_id", "to_id"), &AStar::get_point_path);
  443. ClassDB::bind_method(D_METHOD("get_id_path", "from_id", "to_id"), &AStar::get_id_path);
  444. BIND_VMETHOD(MethodInfo(Variant::REAL, "_estimate_cost", PropertyInfo(Variant::INT, "from_id"), PropertyInfo(Variant::INT, "to_id")));
  445. BIND_VMETHOD(MethodInfo(Variant::REAL, "_compute_cost", PropertyInfo(Variant::INT, "from_id"), PropertyInfo(Variant::INT, "to_id")));
  446. }
  447. AStar::AStar() {
  448. last_free_id = 0;
  449. pass = 1;
  450. }
  451. AStar::~AStar() {
  452. clear();
  453. }
  454. /////////////////////////////////////////////////////////////
  455. int AStar2D::get_available_point_id() const {
  456. return astar.get_available_point_id();
  457. }
  458. void AStar2D::add_point(int p_id, const Vector2 &p_pos, real_t p_weight_scale) {
  459. astar.add_point(p_id, Vector3(p_pos.x, p_pos.y, 0), p_weight_scale);
  460. }
  461. Vector2 AStar2D::get_point_position(int p_id) const {
  462. Vector3 p = astar.get_point_position(p_id);
  463. return Vector2(p.x, p.y);
  464. }
  465. void AStar2D::set_point_position(int p_id, const Vector2 &p_pos) {
  466. astar.set_point_position(p_id, Vector3(p_pos.x, p_pos.y, 0));
  467. }
  468. real_t AStar2D::get_point_weight_scale(int p_id) const {
  469. return astar.get_point_weight_scale(p_id);
  470. }
  471. void AStar2D::set_point_weight_scale(int p_id, real_t p_weight_scale) {
  472. astar.set_point_weight_scale(p_id, p_weight_scale);
  473. }
  474. void AStar2D::remove_point(int p_id) {
  475. astar.remove_point(p_id);
  476. }
  477. bool AStar2D::has_point(int p_id) const {
  478. return astar.has_point(p_id);
  479. }
  480. PoolVector<int> AStar2D::get_point_connections(int p_id) {
  481. return astar.get_point_connections(p_id);
  482. }
  483. Array AStar2D::get_points() {
  484. return astar.get_points();
  485. }
  486. void AStar2D::set_point_disabled(int p_id, bool p_disabled) {
  487. astar.set_point_disabled(p_id, p_disabled);
  488. }
  489. bool AStar2D::is_point_disabled(int p_id) const {
  490. return astar.is_point_disabled(p_id);
  491. }
  492. void AStar2D::connect_points(int p_id, int p_with_id, bool p_bidirectional) {
  493. astar.connect_points(p_id, p_with_id, p_bidirectional);
  494. }
  495. void AStar2D::disconnect_points(int p_id, int p_with_id) {
  496. astar.disconnect_points(p_id, p_with_id);
  497. }
  498. bool AStar2D::are_points_connected(int p_id, int p_with_id) const {
  499. return astar.are_points_connected(p_id, p_with_id);
  500. }
  501. int AStar2D::get_point_count() const {
  502. return astar.get_point_count();
  503. }
  504. int AStar2D::get_point_capacity() const {
  505. return astar.get_point_capacity();
  506. }
  507. void AStar2D::clear() {
  508. astar.clear();
  509. }
  510. void AStar2D::reserve_space(int p_num_nodes) {
  511. astar.reserve_space(p_num_nodes);
  512. }
  513. int AStar2D::get_closest_point(const Vector2 &p_point, bool p_include_disabled) const {
  514. return astar.get_closest_point(Vector3(p_point.x, p_point.y, 0), p_include_disabled);
  515. }
  516. Vector2 AStar2D::get_closest_position_in_segment(const Vector2 &p_point) const {
  517. Vector3 p = astar.get_closest_position_in_segment(Vector3(p_point.x, p_point.y, 0));
  518. return Vector2(p.x, p.y);
  519. }
  520. real_t AStar2D::_estimate_cost(int p_from_id, int p_to_id) {
  521. if (get_script_instance() && get_script_instance()->has_method(SceneStringNames::get_singleton()->_estimate_cost)) {
  522. return get_script_instance()->call(SceneStringNames::get_singleton()->_estimate_cost, p_from_id, p_to_id);
  523. }
  524. AStar::Point *from_point;
  525. bool from_exists = astar.points.lookup(p_from_id, from_point);
  526. ERR_FAIL_COND_V_MSG(!from_exists, 0, vformat("Can't estimate cost. Point with id: %d doesn't exist.", p_from_id));
  527. AStar::Point *to_point;
  528. bool to_exists = astar.points.lookup(p_to_id, to_point);
  529. ERR_FAIL_COND_V_MSG(!to_exists, 0, vformat("Can't estimate cost. Point with id: %d doesn't exist.", p_to_id));
  530. return from_point->pos.distance_to(to_point->pos);
  531. }
  532. real_t AStar2D::_compute_cost(int p_from_id, int p_to_id) {
  533. if (get_script_instance() && get_script_instance()->has_method(SceneStringNames::get_singleton()->_compute_cost)) {
  534. return get_script_instance()->call(SceneStringNames::get_singleton()->_compute_cost, p_from_id, p_to_id);
  535. }
  536. AStar::Point *from_point;
  537. bool from_exists = astar.points.lookup(p_from_id, from_point);
  538. ERR_FAIL_COND_V_MSG(!from_exists, 0, vformat("Can't compute cost. Point with id: %d doesn't exist.", p_from_id));
  539. AStar::Point *to_point;
  540. bool to_exists = astar.points.lookup(p_to_id, to_point);
  541. ERR_FAIL_COND_V_MSG(!to_exists, 0, vformat("Can't compute cost. Point with id: %d doesn't exist.", p_to_id));
  542. return from_point->pos.distance_to(to_point->pos);
  543. }
  544. PoolVector<Vector2> AStar2D::get_point_path(int p_from_id, int p_to_id) {
  545. AStar::Point *a;
  546. bool from_exists = astar.points.lookup(p_from_id, a);
  547. ERR_FAIL_COND_V_MSG(!from_exists, PoolVector<Vector2>(), vformat("Can't get point path. Point with id: %d doesn't exist.", p_from_id));
  548. AStar::Point *b;
  549. bool to_exists = astar.points.lookup(p_to_id, b);
  550. ERR_FAIL_COND_V_MSG(!to_exists, PoolVector<Vector2>(), vformat("Can't get point path. Point with id: %d doesn't exist.", p_to_id));
  551. if (a == b) {
  552. PoolVector<Vector2> ret;
  553. ret.push_back(Vector2(a->pos.x, a->pos.y));
  554. return ret;
  555. }
  556. AStar::Point *begin_point = a;
  557. AStar::Point *end_point = b;
  558. bool found_route = _solve(begin_point, end_point);
  559. if (!found_route) {
  560. return PoolVector<Vector2>();
  561. }
  562. AStar::Point *p = end_point;
  563. int pc = 1; // Begin point
  564. while (p != begin_point) {
  565. pc++;
  566. p = p->prev_point;
  567. }
  568. PoolVector<Vector2> path;
  569. path.resize(pc);
  570. {
  571. PoolVector<Vector2>::Write w = path.write();
  572. AStar::Point *p2 = end_point;
  573. int idx = pc - 1;
  574. while (p2 != begin_point) {
  575. w[idx--] = Vector2(p2->pos.x, p2->pos.y);
  576. p2 = p2->prev_point;
  577. }
  578. w[0] = Vector2(p2->pos.x, p2->pos.y); // Assign first
  579. }
  580. return path;
  581. }
  582. PoolVector<int> AStar2D::get_id_path(int p_from_id, int p_to_id) {
  583. AStar::Point *a;
  584. bool from_exists = astar.points.lookup(p_from_id, a);
  585. ERR_FAIL_COND_V_MSG(!from_exists, PoolVector<int>(), vformat("Can't get id path. Point with id: %d doesn't exist.", p_from_id));
  586. AStar::Point *b;
  587. bool to_exists = astar.points.lookup(p_to_id, b);
  588. ERR_FAIL_COND_V_MSG(!to_exists, PoolVector<int>(), vformat("Can't get id path. Point with id: %d doesn't exist.", p_to_id));
  589. if (a == b) {
  590. PoolVector<int> ret;
  591. ret.push_back(a->id);
  592. return ret;
  593. }
  594. AStar::Point *begin_point = a;
  595. AStar::Point *end_point = b;
  596. bool found_route = _solve(begin_point, end_point);
  597. if (!found_route) {
  598. return PoolVector<int>();
  599. }
  600. AStar::Point *p = end_point;
  601. int pc = 1; // Begin point
  602. while (p != begin_point) {
  603. pc++;
  604. p = p->prev_point;
  605. }
  606. PoolVector<int> path;
  607. path.resize(pc);
  608. {
  609. PoolVector<int>::Write w = path.write();
  610. p = end_point;
  611. int idx = pc - 1;
  612. while (p != begin_point) {
  613. w[idx--] = p->id;
  614. p = p->prev_point;
  615. }
  616. w[0] = p->id; // Assign first
  617. }
  618. return path;
  619. }
  620. bool AStar2D::_solve(AStar::Point *begin_point, AStar::Point *end_point) {
  621. astar.pass++;
  622. if (!end_point->enabled) {
  623. return false;
  624. }
  625. bool found_route = false;
  626. Vector<AStar::Point *> open_list;
  627. SortArray<AStar::Point *, AStar::SortPoints> sorter;
  628. begin_point->g_score = 0;
  629. begin_point->f_score = _estimate_cost(begin_point->id, end_point->id);
  630. open_list.push_back(begin_point);
  631. while (!open_list.empty()) {
  632. AStar::Point *p = open_list[0]; // The currently processed point
  633. if (p == end_point) {
  634. found_route = true;
  635. break;
  636. }
  637. sorter.pop_heap(0, open_list.size(), open_list.ptrw()); // Remove the current point from the open list
  638. open_list.remove(open_list.size() - 1);
  639. p->closed_pass = astar.pass; // Mark the point as closed
  640. for (OAHashMap<int, AStar::Point *>::Iterator it = p->neighbours.iter(); it.valid; it = p->neighbours.next_iter(it)) {
  641. AStar::Point *e = *(it.value); // The neighbour point
  642. if (!e->enabled || e->closed_pass == astar.pass) {
  643. continue;
  644. }
  645. real_t tentative_g_score = p->g_score + _compute_cost(p->id, e->id) * e->weight_scale;
  646. bool new_point = false;
  647. if (e->open_pass != astar.pass) { // The point wasn't inside the open list.
  648. e->open_pass = astar.pass;
  649. open_list.push_back(e);
  650. new_point = true;
  651. } else if (tentative_g_score >= e->g_score) { // The new path is worse than the previous.
  652. continue;
  653. }
  654. e->prev_point = p;
  655. e->g_score = tentative_g_score;
  656. e->f_score = e->g_score + _estimate_cost(e->id, end_point->id);
  657. if (new_point) { // The position of the new points is already known.
  658. sorter.push_heap(0, open_list.size() - 1, 0, e, open_list.ptrw());
  659. } else {
  660. sorter.push_heap(0, open_list.find(e), 0, e, open_list.ptrw());
  661. }
  662. }
  663. }
  664. return found_route;
  665. }
  666. void AStar2D::_bind_methods() {
  667. ClassDB::bind_method(D_METHOD("get_available_point_id"), &AStar2D::get_available_point_id);
  668. ClassDB::bind_method(D_METHOD("add_point", "id", "position", "weight_scale"), &AStar2D::add_point, DEFVAL(1.0));
  669. ClassDB::bind_method(D_METHOD("get_point_position", "id"), &AStar2D::get_point_position);
  670. ClassDB::bind_method(D_METHOD("set_point_position", "id", "position"), &AStar2D::set_point_position);
  671. ClassDB::bind_method(D_METHOD("get_point_weight_scale", "id"), &AStar2D::get_point_weight_scale);
  672. ClassDB::bind_method(D_METHOD("set_point_weight_scale", "id", "weight_scale"), &AStar2D::set_point_weight_scale);
  673. ClassDB::bind_method(D_METHOD("remove_point", "id"), &AStar2D::remove_point);
  674. ClassDB::bind_method(D_METHOD("has_point", "id"), &AStar2D::has_point);
  675. ClassDB::bind_method(D_METHOD("get_point_connections", "id"), &AStar2D::get_point_connections);
  676. ClassDB::bind_method(D_METHOD("get_points"), &AStar2D::get_points);
  677. ClassDB::bind_method(D_METHOD("set_point_disabled", "id", "disabled"), &AStar2D::set_point_disabled, DEFVAL(true));
  678. ClassDB::bind_method(D_METHOD("is_point_disabled", "id"), &AStar2D::is_point_disabled);
  679. ClassDB::bind_method(D_METHOD("connect_points", "id", "to_id", "bidirectional"), &AStar2D::connect_points, DEFVAL(true));
  680. ClassDB::bind_method(D_METHOD("disconnect_points", "id", "to_id"), &AStar2D::disconnect_points);
  681. ClassDB::bind_method(D_METHOD("are_points_connected", "id", "to_id"), &AStar2D::are_points_connected);
  682. ClassDB::bind_method(D_METHOD("get_point_count"), &AStar2D::get_point_count);
  683. ClassDB::bind_method(D_METHOD("get_point_capacity"), &AStar2D::get_point_capacity);
  684. ClassDB::bind_method(D_METHOD("reserve_space", "num_nodes"), &AStar2D::reserve_space);
  685. ClassDB::bind_method(D_METHOD("clear"), &AStar2D::clear);
  686. ClassDB::bind_method(D_METHOD("get_closest_point", "to_position", "include_disabled"), &AStar2D::get_closest_point, DEFVAL(false));
  687. ClassDB::bind_method(D_METHOD("get_closest_position_in_segment", "to_position"), &AStar2D::get_closest_position_in_segment);
  688. ClassDB::bind_method(D_METHOD("get_point_path", "from_id", "to_id"), &AStar2D::get_point_path);
  689. ClassDB::bind_method(D_METHOD("get_id_path", "from_id", "to_id"), &AStar2D::get_id_path);
  690. BIND_VMETHOD(MethodInfo(Variant::REAL, "_estimate_cost", PropertyInfo(Variant::INT, "from_id"), PropertyInfo(Variant::INT, "to_id")));
  691. BIND_VMETHOD(MethodInfo(Variant::REAL, "_compute_cost", PropertyInfo(Variant::INT, "from_id"), PropertyInfo(Variant::INT, "to_id")));
  692. }
  693. AStar2D::AStar2D() {
  694. }
  695. AStar2D::~AStar2D() {
  696. }