a_star.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737
  1. /*************************************************************************/
  2. /* a_star.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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.empty()) {
  36. return 1;
  37. }
  38. // calculate our new next available point id if bigger than before or next id already contained in set of points.
  39. if (points.has(last_free_id)) {
  40. int cur_new_id = last_free_id;
  41. while (points.has(cur_new_id)) {
  42. cur_new_id++;
  43. }
  44. int &non_const = const_cast<int &>(last_free_id);
  45. non_const = cur_new_id;
  46. }
  47. return last_free_id;
  48. }
  49. void AStar::add_point(int p_id, const Vector3 &p_pos, real_t p_weight_scale) {
  50. ERR_FAIL_COND(p_id < 0);
  51. ERR_FAIL_COND(p_weight_scale < 1);
  52. Point *found_pt;
  53. bool p_exists = points.lookup(p_id, found_pt);
  54. if (!p_exists) {
  55. Point *pt = memnew(Point);
  56. pt->id = p_id;
  57. pt->pos = p_pos;
  58. pt->weight_scale = p_weight_scale;
  59. pt->prev_point = NULL;
  60. pt->open_pass = 0;
  61. pt->closed_pass = 0;
  62. pt->enabled = true;
  63. points.set(p_id, pt);
  64. } else {
  65. found_pt->pos = p_pos;
  66. found_pt->weight_scale = p_weight_scale;
  67. }
  68. }
  69. Vector3 AStar::get_point_position(int p_id) const {
  70. Point *p;
  71. bool p_exists = points.lookup(p_id, p);
  72. ERR_FAIL_COND_V(!p_exists, Vector3());
  73. return p->pos;
  74. }
  75. void AStar::set_point_position(int p_id, const Vector3 &p_pos) {
  76. Point *p;
  77. bool p_exists = points.lookup(p_id, p);
  78. ERR_FAIL_COND(!p_exists);
  79. p->pos = p_pos;
  80. }
  81. real_t AStar::get_point_weight_scale(int p_id) const {
  82. Point *p;
  83. bool p_exists = points.lookup(p_id, p);
  84. ERR_FAIL_COND_V(!p_exists, 0);
  85. return p->weight_scale;
  86. }
  87. void AStar::set_point_weight_scale(int p_id, real_t p_weight_scale) {
  88. Point *p;
  89. bool p_exists = points.lookup(p_id, p);
  90. ERR_FAIL_COND(!p_exists);
  91. ERR_FAIL_COND(p_weight_scale < 1);
  92. p->weight_scale = p_weight_scale;
  93. }
  94. void AStar::remove_point(int p_id) {
  95. Point *p;
  96. bool p_exists = points.lookup(p_id, p);
  97. ERR_FAIL_COND(!p_exists);
  98. for (OAHashMap<int, Point *>::Iterator it = p->neighbours.iter(); it.valid; it = p->neighbours.next_iter(it)) {
  99. Segment s(p_id, (*it.key));
  100. segments.erase(s);
  101. (*it.value)->neighbours.remove(p->id);
  102. (*it.value)->unlinked_neighbours.remove(p->id);
  103. }
  104. for (OAHashMap<int, Point *>::Iterator it = p->unlinked_neighbours.iter(); it.valid; it = p->unlinked_neighbours.next_iter(it)) {
  105. Segment s(p_id, (*it.key));
  106. segments.erase(s);
  107. (*it.value)->neighbours.remove(p->id);
  108. (*it.value)->unlinked_neighbours.remove(p->id);
  109. }
  110. memdelete(p);
  111. points.remove(p_id);
  112. last_free_id = p_id;
  113. }
  114. void AStar::connect_points(int p_id, int p_with_id, bool bidirectional) {
  115. ERR_FAIL_COND(p_id == p_with_id);
  116. Point *a;
  117. bool from_exists = points.lookup(p_id, a);
  118. ERR_FAIL_COND(!from_exists);
  119. Point *b;
  120. bool to_exists = points.lookup(p_with_id, b);
  121. ERR_FAIL_COND(!to_exists);
  122. a->neighbours.set(b->id, b);
  123. if (bidirectional) {
  124. b->neighbours.set(a->id, a);
  125. } else {
  126. b->unlinked_neighbours.set(a->id, a);
  127. }
  128. Segment s(p_id, p_with_id);
  129. if (bidirectional) s.direction = Segment::BIDIRECTIONAL;
  130. Set<Segment>::Element *element = segments.find(s);
  131. if (element != NULL) {
  132. s.direction |= element->get().direction;
  133. if (s.direction == Segment::BIDIRECTIONAL) {
  134. // Both are neighbours of each other now
  135. a->unlinked_neighbours.remove(b->id);
  136. b->unlinked_neighbours.remove(a->id);
  137. }
  138. segments.erase(element);
  139. }
  140. segments.insert(s);
  141. }
  142. void AStar::disconnect_points(int p_id, int p_with_id, bool bidirectional) {
  143. Point *a;
  144. bool a_exists = points.lookup(p_id, a);
  145. ERR_FAIL_COND(!a_exists);
  146. Point *b;
  147. bool b_exists = points.lookup(p_with_id, b);
  148. ERR_FAIL_COND(!b_exists);
  149. Segment s(p_id, p_with_id);
  150. int remove_direction = bidirectional ? (int)Segment::BIDIRECTIONAL : s.direction;
  151. Set<Segment>::Element *element = segments.find(s);
  152. if (element != NULL) {
  153. // s is the new segment
  154. // Erase the directions to be removed
  155. s.direction = (element->get().direction & ~remove_direction);
  156. a->neighbours.remove(b->id);
  157. if (bidirectional) {
  158. b->neighbours.remove(a->id);
  159. if (element->get().direction != Segment::BIDIRECTIONAL) {
  160. a->unlinked_neighbours.remove(b->id);
  161. b->unlinked_neighbours.remove(a->id);
  162. }
  163. } else {
  164. if (s.direction == Segment::NONE)
  165. b->unlinked_neighbours.remove(a->id);
  166. else
  167. a->unlinked_neighbours.set(b->id, b);
  168. }
  169. segments.erase(element);
  170. if (s.direction != Segment::NONE)
  171. segments.insert(s);
  172. }
  173. }
  174. bool AStar::has_point(int p_id) const {
  175. return points.has(p_id);
  176. }
  177. Array AStar::get_points() {
  178. Array point_list;
  179. for (OAHashMap<int, Point *>::Iterator it = points.iter(); it.valid; it = points.next_iter(it)) {
  180. point_list.push_back(*(it.key));
  181. }
  182. return point_list;
  183. }
  184. Vector<int> AStar::get_point_connections(int p_id) {
  185. Point *p;
  186. bool p_exists = points.lookup(p_id, p);
  187. ERR_FAIL_COND_V(!p_exists, Vector<int>());
  188. Vector<int> point_list;
  189. for (OAHashMap<int, Point *>::Iterator it = p->neighbours.iter(); it.valid; it = p->neighbours.next_iter(it)) {
  190. point_list.push_back((*it.key));
  191. }
  192. return point_list;
  193. }
  194. bool AStar::are_points_connected(int p_id, int p_with_id, bool bidirectional) const {
  195. Segment s(p_id, p_with_id);
  196. const Set<Segment>::Element *element = segments.find(s);
  197. return element != NULL &&
  198. (bidirectional || (element->get().direction & s.direction) == s.direction);
  199. }
  200. void AStar::clear() {
  201. last_free_id = 0;
  202. for (OAHashMap<int, Point *>::Iterator it = points.iter(); it.valid; it = points.next_iter(it)) {
  203. memdelete(*(it.value));
  204. }
  205. segments.clear();
  206. points.clear();
  207. }
  208. int AStar::get_point_count() const {
  209. return points.get_num_elements();
  210. }
  211. int AStar::get_point_capacity() const {
  212. return points.get_capacity();
  213. }
  214. void AStar::reserve_space(int p_num_nodes) {
  215. ERR_FAIL_COND_MSG(p_num_nodes <= 0, "New capacity must be greater than 0, was: " + itos(p_num_nodes) + ".");
  216. ERR_FAIL_COND_MSG((uint32_t)p_num_nodes < points.get_capacity(), "New capacity must be greater than current capacity: " + itos(points.get_capacity()) + ", new was: " + itos(p_num_nodes) + ".");
  217. points.reserve(p_num_nodes);
  218. }
  219. int AStar::get_closest_point(const Vector3 &p_point, bool p_include_disabled) const {
  220. int closest_id = -1;
  221. real_t closest_dist = 1e20;
  222. for (OAHashMap<int, Point *>::Iterator it = points.iter(); it.valid; it = points.next_iter(it)) {
  223. if (!p_include_disabled && !(*it.value)->enabled) continue; // Disabled points should not be considered.
  224. real_t d = p_point.distance_squared_to((*it.value)->pos);
  225. if (closest_id < 0 || d < closest_dist) {
  226. closest_dist = d;
  227. closest_id = *(it.key);
  228. }
  229. }
  230. return closest_id;
  231. }
  232. Vector3 AStar::get_closest_position_in_segment(const Vector3 &p_point) const {
  233. bool found = false;
  234. real_t closest_dist = 1e20;
  235. Vector3 closest_point;
  236. for (const Set<Segment>::Element *E = segments.front(); E; E = E->next()) {
  237. Point *from_point = nullptr, *to_point = nullptr;
  238. points.lookup(E->get().u, from_point);
  239. points.lookup(E->get().v, to_point);
  240. if (!(from_point->enabled && to_point->enabled)) {
  241. continue;
  242. }
  243. Vector3 segment[2] = {
  244. from_point->pos,
  245. to_point->pos,
  246. };
  247. Vector3 p = Geometry::get_closest_point_to_segment(p_point, segment);
  248. real_t d = p_point.distance_squared_to(p);
  249. if (!found || d < closest_dist) {
  250. closest_point = p;
  251. closest_dist = d;
  252. found = true;
  253. }
  254. }
  255. return closest_point;
  256. }
  257. bool AStar::_solve(Point *begin_point, Point *end_point) {
  258. pass++;
  259. if (!end_point->enabled) return false;
  260. bool found_route = false;
  261. Vector<Point *> open_list;
  262. SortArray<Point *, SortPoints> sorter;
  263. begin_point->g_score = 0;
  264. begin_point->f_score = _estimate_cost(begin_point->id, end_point->id);
  265. open_list.push_back(begin_point);
  266. while (!open_list.empty()) {
  267. Point *p = open_list[0]; // The currently processed point
  268. if (p == end_point) {
  269. found_route = true;
  270. break;
  271. }
  272. sorter.pop_heap(0, open_list.size(), open_list.ptrw()); // Remove the current point from the open list
  273. open_list.remove(open_list.size() - 1);
  274. p->closed_pass = pass; // Mark the point as closed
  275. for (OAHashMap<int, Point *>::Iterator it = p->neighbours.iter(); it.valid; it = p->neighbours.next_iter(it)) {
  276. Point *e = *(it.value); // The neighbour point
  277. if (!e->enabled || e->closed_pass == pass) {
  278. continue;
  279. }
  280. real_t tentative_g_score = p->g_score + _compute_cost(p->id, e->id) * e->weight_scale;
  281. bool new_point = false;
  282. if (e->open_pass != pass) { // The point wasn't inside the open list.
  283. e->open_pass = pass;
  284. open_list.push_back(e);
  285. new_point = true;
  286. } else if (tentative_g_score >= e->g_score) { // The new path is worse than the previous.
  287. continue;
  288. }
  289. e->prev_point = p;
  290. e->g_score = tentative_g_score;
  291. e->f_score = e->g_score + _estimate_cost(e->id, end_point->id);
  292. if (new_point) { // The position of the new points is already known.
  293. sorter.push_heap(0, open_list.size() - 1, 0, e, open_list.ptrw());
  294. } else {
  295. sorter.push_heap(0, open_list.find(e), 0, e, open_list.ptrw());
  296. }
  297. }
  298. }
  299. return found_route;
  300. }
  301. float AStar::_estimate_cost(int p_from_id, int p_to_id) {
  302. if (get_script_instance() && get_script_instance()->has_method(SceneStringNames::get_singleton()->_estimate_cost))
  303. return get_script_instance()->call(SceneStringNames::get_singleton()->_estimate_cost, p_from_id, p_to_id);
  304. Point *from_point;
  305. bool from_exists = points.lookup(p_from_id, from_point);
  306. ERR_FAIL_COND_V(!from_exists, 0);
  307. Point *to_point;
  308. bool to_exists = points.lookup(p_to_id, to_point);
  309. ERR_FAIL_COND_V(!to_exists, 0);
  310. return from_point->pos.distance_to(to_point->pos);
  311. }
  312. float AStar::_compute_cost(int p_from_id, int p_to_id) {
  313. if (get_script_instance() && get_script_instance()->has_method(SceneStringNames::get_singleton()->_compute_cost))
  314. return get_script_instance()->call(SceneStringNames::get_singleton()->_compute_cost, p_from_id, p_to_id);
  315. Point *from_point;
  316. bool from_exists = points.lookup(p_from_id, from_point);
  317. ERR_FAIL_COND_V(!from_exists, 0);
  318. Point *to_point;
  319. bool to_exists = points.lookup(p_to_id, to_point);
  320. ERR_FAIL_COND_V(!to_exists, 0);
  321. return from_point->pos.distance_to(to_point->pos);
  322. }
  323. Vector<Vector3> AStar::get_point_path(int p_from_id, int p_to_id) {
  324. Point *a;
  325. bool from_exists = points.lookup(p_from_id, a);
  326. ERR_FAIL_COND_V(!from_exists, Vector<Vector3>());
  327. Point *b;
  328. bool to_exists = points.lookup(p_to_id, b);
  329. ERR_FAIL_COND_V(!to_exists, Vector<Vector3>());
  330. if (a == b) {
  331. Vector<Vector3> ret;
  332. ret.push_back(a->pos);
  333. return ret;
  334. }
  335. Point *begin_point = a;
  336. Point *end_point = b;
  337. bool found_route = _solve(begin_point, end_point);
  338. if (!found_route) return Vector<Vector3>();
  339. Point *p = end_point;
  340. int pc = 1; // Begin point
  341. while (p != begin_point) {
  342. pc++;
  343. p = p->prev_point;
  344. }
  345. Vector<Vector3> path;
  346. path.resize(pc);
  347. {
  348. Vector3 *w = path.ptrw();
  349. Point *p2 = end_point;
  350. int idx = pc - 1;
  351. while (p2 != begin_point) {
  352. w[idx--] = p2->pos;
  353. p2 = p2->prev_point;
  354. }
  355. w[0] = p2->pos; // Assign first
  356. }
  357. return path;
  358. }
  359. Vector<int> AStar::get_id_path(int p_from_id, int p_to_id) {
  360. Point *a;
  361. bool from_exists = points.lookup(p_from_id, a);
  362. ERR_FAIL_COND_V(!from_exists, Vector<int>());
  363. Point *b;
  364. bool to_exists = points.lookup(p_to_id, b);
  365. ERR_FAIL_COND_V(!to_exists, Vector<int>());
  366. if (a == b) {
  367. Vector<int> ret;
  368. ret.push_back(a->id);
  369. return ret;
  370. }
  371. Point *begin_point = a;
  372. Point *end_point = b;
  373. bool found_route = _solve(begin_point, end_point);
  374. if (!found_route) return Vector<int>();
  375. Point *p = end_point;
  376. int pc = 1; // Begin point
  377. while (p != begin_point) {
  378. pc++;
  379. p = p->prev_point;
  380. }
  381. Vector<int> path;
  382. path.resize(pc);
  383. {
  384. int *w = path.ptrw();
  385. p = end_point;
  386. int idx = pc - 1;
  387. while (p != begin_point) {
  388. w[idx--] = p->id;
  389. p = p->prev_point;
  390. }
  391. w[0] = p->id; // Assign first
  392. }
  393. return path;
  394. }
  395. void AStar::set_point_disabled(int p_id, bool p_disabled) {
  396. Point *p;
  397. bool p_exists = points.lookup(p_id, p);
  398. ERR_FAIL_COND(!p_exists);
  399. p->enabled = !p_disabled;
  400. }
  401. bool AStar::is_point_disabled(int p_id) const {
  402. Point *p;
  403. bool p_exists = points.lookup(p_id, p);
  404. ERR_FAIL_COND_V(!p_exists, false);
  405. return !p->enabled;
  406. }
  407. void AStar::_bind_methods() {
  408. ClassDB::bind_method(D_METHOD("get_available_point_id"), &AStar::get_available_point_id);
  409. ClassDB::bind_method(D_METHOD("add_point", "id", "position", "weight_scale"), &AStar::add_point, DEFVAL(1.0));
  410. ClassDB::bind_method(D_METHOD("get_point_position", "id"), &AStar::get_point_position);
  411. ClassDB::bind_method(D_METHOD("set_point_position", "id", "position"), &AStar::set_point_position);
  412. ClassDB::bind_method(D_METHOD("get_point_weight_scale", "id"), &AStar::get_point_weight_scale);
  413. ClassDB::bind_method(D_METHOD("set_point_weight_scale", "id", "weight_scale"), &AStar::set_point_weight_scale);
  414. ClassDB::bind_method(D_METHOD("remove_point", "id"), &AStar::remove_point);
  415. ClassDB::bind_method(D_METHOD("has_point", "id"), &AStar::has_point);
  416. ClassDB::bind_method(D_METHOD("get_point_connections", "id"), &AStar::get_point_connections);
  417. ClassDB::bind_method(D_METHOD("get_points"), &AStar::get_points);
  418. ClassDB::bind_method(D_METHOD("set_point_disabled", "id", "disabled"), &AStar::set_point_disabled, DEFVAL(true));
  419. ClassDB::bind_method(D_METHOD("is_point_disabled", "id"), &AStar::is_point_disabled);
  420. ClassDB::bind_method(D_METHOD("connect_points", "id", "to_id", "bidirectional"), &AStar::connect_points, DEFVAL(true));
  421. ClassDB::bind_method(D_METHOD("disconnect_points", "id", "to_id", "bidirectional"), &AStar::disconnect_points, DEFVAL(true));
  422. ClassDB::bind_method(D_METHOD("are_points_connected", "id", "to_id", "bidirectional"), &AStar::are_points_connected, DEFVAL(true));
  423. ClassDB::bind_method(D_METHOD("get_point_count"), &AStar::get_point_count);
  424. ClassDB::bind_method(D_METHOD("get_point_capacity"), &AStar::get_point_capacity);
  425. ClassDB::bind_method(D_METHOD("reserve_space", "num_nodes"), &AStar::reserve_space);
  426. ClassDB::bind_method(D_METHOD("clear"), &AStar::clear);
  427. ClassDB::bind_method(D_METHOD("get_closest_point", "to_position", "include_disabled"), &AStar::get_closest_point, DEFVAL(false));
  428. ClassDB::bind_method(D_METHOD("get_closest_position_in_segment", "to_position"), &AStar::get_closest_position_in_segment);
  429. ClassDB::bind_method(D_METHOD("get_point_path", "from_id", "to_id"), &AStar::get_point_path);
  430. ClassDB::bind_method(D_METHOD("get_id_path", "from_id", "to_id"), &AStar::get_id_path);
  431. BIND_VMETHOD(MethodInfo(Variant::REAL, "_estimate_cost", PropertyInfo(Variant::INT, "from_id"), PropertyInfo(Variant::INT, "to_id")));
  432. BIND_VMETHOD(MethodInfo(Variant::REAL, "_compute_cost", PropertyInfo(Variant::INT, "from_id"), PropertyInfo(Variant::INT, "to_id")));
  433. }
  434. AStar::AStar() {
  435. last_free_id = 0;
  436. pass = 1;
  437. }
  438. AStar::~AStar() {
  439. clear();
  440. }
  441. /////////////////////////////////////////////////////////////
  442. int AStar2D::get_available_point_id() const {
  443. return astar.get_available_point_id();
  444. }
  445. void AStar2D::add_point(int p_id, const Vector2 &p_pos, real_t p_weight_scale) {
  446. astar.add_point(p_id, Vector3(p_pos.x, p_pos.y, 0), p_weight_scale);
  447. }
  448. Vector2 AStar2D::get_point_position(int p_id) const {
  449. Vector3 p = astar.get_point_position(p_id);
  450. return Vector2(p.x, p.y);
  451. }
  452. void AStar2D::set_point_position(int p_id, const Vector2 &p_pos) {
  453. astar.set_point_position(p_id, Vector3(p_pos.x, p_pos.y, 0));
  454. }
  455. real_t AStar2D::get_point_weight_scale(int p_id) const {
  456. return astar.get_point_weight_scale(p_id);
  457. }
  458. void AStar2D::set_point_weight_scale(int p_id, real_t p_weight_scale) {
  459. astar.set_point_weight_scale(p_id, p_weight_scale);
  460. }
  461. void AStar2D::remove_point(int p_id) {
  462. astar.remove_point(p_id);
  463. }
  464. bool AStar2D::has_point(int p_id) const {
  465. return astar.has_point(p_id);
  466. }
  467. Vector<int> AStar2D::get_point_connections(int p_id) {
  468. return astar.get_point_connections(p_id);
  469. }
  470. Array AStar2D::get_points() {
  471. return astar.get_points();
  472. }
  473. void AStar2D::set_point_disabled(int p_id, bool p_disabled) {
  474. astar.set_point_disabled(p_id, p_disabled);
  475. }
  476. bool AStar2D::is_point_disabled(int p_id) const {
  477. return astar.is_point_disabled(p_id);
  478. }
  479. void AStar2D::connect_points(int p_id, int p_with_id, bool p_bidirectional) {
  480. astar.connect_points(p_id, p_with_id, p_bidirectional);
  481. }
  482. void AStar2D::disconnect_points(int p_id, int p_with_id) {
  483. astar.disconnect_points(p_id, p_with_id);
  484. }
  485. bool AStar2D::are_points_connected(int p_id, int p_with_id) const {
  486. return astar.are_points_connected(p_id, p_with_id);
  487. }
  488. int AStar2D::get_point_count() const {
  489. return astar.get_point_count();
  490. }
  491. int AStar2D::get_point_capacity() const {
  492. return astar.get_point_capacity();
  493. }
  494. void AStar2D::clear() {
  495. astar.clear();
  496. }
  497. void AStar2D::reserve_space(int p_num_nodes) {
  498. astar.reserve_space(p_num_nodes);
  499. }
  500. int AStar2D::get_closest_point(const Vector2 &p_point, bool p_include_disabled) const {
  501. return astar.get_closest_point(Vector3(p_point.x, p_point.y, 0), p_include_disabled);
  502. }
  503. Vector2 AStar2D::get_closest_position_in_segment(const Vector2 &p_point) const {
  504. Vector3 p = astar.get_closest_position_in_segment(Vector3(p_point.x, p_point.y, 0));
  505. return Vector2(p.x, p.y);
  506. }
  507. Vector<Vector2> AStar2D::get_point_path(int p_from_id, int p_to_id) {
  508. PackedVector3Array pv = astar.get_point_path(p_from_id, p_to_id);
  509. int size = pv.size();
  510. PackedVector2Array path;
  511. path.resize(size);
  512. {
  513. const Vector3 *r = pv.ptr();
  514. Vector2 *w = path.ptrw();
  515. for (int i = 0; i < size; i++) {
  516. Vector3 p = r[i];
  517. w[i] = Vector2(p.x, p.y);
  518. }
  519. }
  520. return path;
  521. }
  522. Vector<int> AStar2D::get_id_path(int p_from_id, int p_to_id) {
  523. return astar.get_id_path(p_from_id, p_to_id);
  524. }
  525. void AStar2D::_bind_methods() {
  526. ClassDB::bind_method(D_METHOD("get_available_point_id"), &AStar2D::get_available_point_id);
  527. ClassDB::bind_method(D_METHOD("add_point", "id", "position", "weight_scale"), &AStar2D::add_point, DEFVAL(1.0));
  528. ClassDB::bind_method(D_METHOD("get_point_position", "id"), &AStar2D::get_point_position);
  529. ClassDB::bind_method(D_METHOD("set_point_position", "id", "position"), &AStar2D::set_point_position);
  530. ClassDB::bind_method(D_METHOD("get_point_weight_scale", "id"), &AStar2D::get_point_weight_scale);
  531. ClassDB::bind_method(D_METHOD("set_point_weight_scale", "id", "weight_scale"), &AStar2D::set_point_weight_scale);
  532. ClassDB::bind_method(D_METHOD("remove_point", "id"), &AStar2D::remove_point);
  533. ClassDB::bind_method(D_METHOD("has_point", "id"), &AStar2D::has_point);
  534. ClassDB::bind_method(D_METHOD("get_point_connections", "id"), &AStar2D::get_point_connections);
  535. ClassDB::bind_method(D_METHOD("get_points"), &AStar2D::get_points);
  536. ClassDB::bind_method(D_METHOD("set_point_disabled", "id", "disabled"), &AStar2D::set_point_disabled, DEFVAL(true));
  537. ClassDB::bind_method(D_METHOD("is_point_disabled", "id"), &AStar2D::is_point_disabled);
  538. ClassDB::bind_method(D_METHOD("connect_points", "id", "to_id", "bidirectional"), &AStar2D::connect_points, DEFVAL(true));
  539. ClassDB::bind_method(D_METHOD("disconnect_points", "id", "to_id"), &AStar2D::disconnect_points);
  540. ClassDB::bind_method(D_METHOD("are_points_connected", "id", "to_id"), &AStar2D::are_points_connected);
  541. ClassDB::bind_method(D_METHOD("get_point_count"), &AStar2D::get_point_count);
  542. ClassDB::bind_method(D_METHOD("get_point_capacity"), &AStar2D::get_point_capacity);
  543. ClassDB::bind_method(D_METHOD("reserve_space", "num_nodes"), &AStar2D::reserve_space);
  544. ClassDB::bind_method(D_METHOD("clear"), &AStar2D::clear);
  545. ClassDB::bind_method(D_METHOD("get_closest_point", "to_position", "include_disabled"), &AStar2D::get_closest_point, DEFVAL(false));
  546. ClassDB::bind_method(D_METHOD("get_closest_position_in_segment", "to_position"), &AStar2D::get_closest_position_in_segment);
  547. ClassDB::bind_method(D_METHOD("get_point_path", "from_id", "to_id"), &AStar2D::get_point_path);
  548. ClassDB::bind_method(D_METHOD("get_id_path", "from_id", "to_id"), &AStar2D::get_id_path);
  549. }
  550. AStar2D::AStar2D() {
  551. }
  552. AStar2D::~AStar2D() {
  553. }