a_star.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886
  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_3d.h"
  32. #include "core/object/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 = nullptr;
  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) {
  130. s.direction = Segment::BIDIRECTIONAL;
  131. }
  132. Set<Segment>::Element *element = segments.find(s);
  133. if (element != nullptr) {
  134. s.direction |= element->get().direction;
  135. if (s.direction == Segment::BIDIRECTIONAL) {
  136. // Both are neighbours of each other now
  137. a->unlinked_neighbours.remove(b->id);
  138. b->unlinked_neighbours.remove(a->id);
  139. }
  140. segments.erase(element);
  141. }
  142. segments.insert(s);
  143. }
  144. void AStar::disconnect_points(int p_id, int p_with_id, bool bidirectional) {
  145. Point *a;
  146. bool a_exists = points.lookup(p_id, a);
  147. ERR_FAIL_COND(!a_exists);
  148. Point *b;
  149. bool b_exists = points.lookup(p_with_id, b);
  150. ERR_FAIL_COND(!b_exists);
  151. Segment s(p_id, p_with_id);
  152. int remove_direction = bidirectional ? (int)Segment::BIDIRECTIONAL : s.direction;
  153. Set<Segment>::Element *element = segments.find(s);
  154. if (element != nullptr) {
  155. // s is the new segment
  156. // Erase the directions to be removed
  157. s.direction = (element->get().direction & ~remove_direction);
  158. a->neighbours.remove(b->id);
  159. if (bidirectional) {
  160. b->neighbours.remove(a->id);
  161. if (element->get().direction != Segment::BIDIRECTIONAL) {
  162. a->unlinked_neighbours.remove(b->id);
  163. b->unlinked_neighbours.remove(a->id);
  164. }
  165. } else {
  166. if (s.direction == Segment::NONE) {
  167. b->unlinked_neighbours.remove(a->id);
  168. } else {
  169. a->unlinked_neighbours.set(b->id, b);
  170. }
  171. }
  172. segments.erase(element);
  173. if (s.direction != Segment::NONE) {
  174. segments.insert(s);
  175. }
  176. }
  177. }
  178. bool AStar::has_point(int p_id) const {
  179. return points.has(p_id);
  180. }
  181. Array AStar::get_points() {
  182. Array point_list;
  183. for (OAHashMap<int, Point *>::Iterator it = points.iter(); it.valid; it = points.next_iter(it)) {
  184. point_list.push_back(*(it.key));
  185. }
  186. return point_list;
  187. }
  188. Vector<int> AStar::get_point_connections(int p_id) {
  189. Point *p;
  190. bool p_exists = points.lookup(p_id, p);
  191. ERR_FAIL_COND_V(!p_exists, Vector<int>());
  192. Vector<int> point_list;
  193. for (OAHashMap<int, Point *>::Iterator it = p->neighbours.iter(); it.valid; it = p->neighbours.next_iter(it)) {
  194. point_list.push_back((*it.key));
  195. }
  196. return point_list;
  197. }
  198. bool AStar::are_points_connected(int p_id, int p_with_id, bool bidirectional) const {
  199. Segment s(p_id, p_with_id);
  200. const Set<Segment>::Element *element = segments.find(s);
  201. return element != nullptr &&
  202. (bidirectional || (element->get().direction & s.direction) == s.direction);
  203. }
  204. void AStar::clear() {
  205. last_free_id = 0;
  206. for (OAHashMap<int, Point *>::Iterator it = points.iter(); it.valid; it = points.next_iter(it)) {
  207. memdelete(*(it.value));
  208. }
  209. segments.clear();
  210. points.clear();
  211. }
  212. int AStar::get_point_count() const {
  213. return points.get_num_elements();
  214. }
  215. int AStar::get_point_capacity() const {
  216. return points.get_capacity();
  217. }
  218. void AStar::reserve_space(int p_num_nodes) {
  219. ERR_FAIL_COND_MSG(p_num_nodes <= 0, "New capacity must be greater than 0, was: " + itos(p_num_nodes) + ".");
  220. 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) + ".");
  221. points.reserve(p_num_nodes);
  222. }
  223. int AStar::get_closest_point(const Vector3 &p_point, bool p_include_disabled) const {
  224. int closest_id = -1;
  225. real_t closest_dist = 1e20;
  226. for (OAHashMap<int, Point *>::Iterator it = points.iter(); it.valid; it = points.next_iter(it)) {
  227. if (!p_include_disabled && !(*it.value)->enabled) {
  228. continue; // Disabled points should not be considered.
  229. }
  230. // Keep the closest point's ID, and in case of multiple closest IDs,
  231. // the smallest one (makes it deterministic).
  232. real_t d = p_point.distance_squared_to((*it.value)->pos);
  233. int id = *(it.key);
  234. if (d <= closest_dist) {
  235. if (d == closest_dist && id > closest_id) { // Keep lowest ID.
  236. continue;
  237. }
  238. closest_dist = d;
  239. closest_id = id;
  240. }
  241. }
  242. return closest_id;
  243. }
  244. Vector3 AStar::get_closest_position_in_segment(const Vector3 &p_point) const {
  245. real_t closest_dist = 1e20;
  246. Vector3 closest_point;
  247. for (const Set<Segment>::Element *E = segments.front(); E; E = E->next()) {
  248. Point *from_point = nullptr, *to_point = nullptr;
  249. points.lookup(E->get().u, from_point);
  250. points.lookup(E->get().v, to_point);
  251. if (!(from_point->enabled && to_point->enabled)) {
  252. continue;
  253. }
  254. Vector3 segment[2] = {
  255. from_point->pos,
  256. to_point->pos,
  257. };
  258. Vector3 p = Geometry3D::get_closest_point_to_segment(p_point, segment);
  259. real_t d = p_point.distance_squared_to(p);
  260. if (d < closest_dist) {
  261. closest_point = p;
  262. closest_dist = d;
  263. }
  264. }
  265. return closest_point;
  266. }
  267. bool AStar::_solve(Point *begin_point, Point *end_point) {
  268. pass++;
  269. if (!end_point->enabled) {
  270. return false;
  271. }
  272. bool found_route = false;
  273. Vector<Point *> open_list;
  274. SortArray<Point *, SortPoints> sorter;
  275. begin_point->g_score = 0;
  276. begin_point->f_score = _estimate_cost(begin_point->id, end_point->id);
  277. open_list.push_back(begin_point);
  278. while (!open_list.empty()) {
  279. Point *p = open_list[0]; // The currently processed point
  280. if (p == end_point) {
  281. found_route = true;
  282. break;
  283. }
  284. sorter.pop_heap(0, open_list.size(), open_list.ptrw()); // Remove the current point from the open list
  285. open_list.remove(open_list.size() - 1);
  286. p->closed_pass = pass; // Mark the point as closed
  287. for (OAHashMap<int, Point *>::Iterator it = p->neighbours.iter(); it.valid; it = p->neighbours.next_iter(it)) {
  288. Point *e = *(it.value); // The neighbour point
  289. if (!e->enabled || e->closed_pass == pass) {
  290. continue;
  291. }
  292. real_t tentative_g_score = p->g_score + _compute_cost(p->id, e->id) * e->weight_scale;
  293. bool new_point = false;
  294. if (e->open_pass != pass) { // The point wasn't inside the open list.
  295. e->open_pass = pass;
  296. open_list.push_back(e);
  297. new_point = true;
  298. } else if (tentative_g_score >= e->g_score) { // The new path is worse than the previous.
  299. continue;
  300. }
  301. e->prev_point = p;
  302. e->g_score = tentative_g_score;
  303. e->f_score = e->g_score + _estimate_cost(e->id, end_point->id);
  304. if (new_point) { // The position of the new points is already known.
  305. sorter.push_heap(0, open_list.size() - 1, 0, e, open_list.ptrw());
  306. } else {
  307. sorter.push_heap(0, open_list.find(e), 0, e, open_list.ptrw());
  308. }
  309. }
  310. }
  311. return found_route;
  312. }
  313. real_t AStar::_estimate_cost(int p_from_id, int p_to_id) {
  314. if (get_script_instance() && get_script_instance()->has_method(SceneStringNames::get_singleton()->_estimate_cost)) {
  315. return get_script_instance()->call(SceneStringNames::get_singleton()->_estimate_cost, p_from_id, p_to_id);
  316. }
  317. Point *from_point;
  318. bool from_exists = points.lookup(p_from_id, from_point);
  319. ERR_FAIL_COND_V(!from_exists, 0);
  320. Point *to_point;
  321. bool to_exists = points.lookup(p_to_id, to_point);
  322. ERR_FAIL_COND_V(!to_exists, 0);
  323. return from_point->pos.distance_to(to_point->pos);
  324. }
  325. real_t AStar::_compute_cost(int p_from_id, int p_to_id) {
  326. if (get_script_instance() && get_script_instance()->has_method(SceneStringNames::get_singleton()->_compute_cost)) {
  327. return get_script_instance()->call(SceneStringNames::get_singleton()->_compute_cost, p_from_id, p_to_id);
  328. }
  329. Point *from_point;
  330. bool from_exists = points.lookup(p_from_id, from_point);
  331. ERR_FAIL_COND_V(!from_exists, 0);
  332. Point *to_point;
  333. bool to_exists = points.lookup(p_to_id, to_point);
  334. ERR_FAIL_COND_V(!to_exists, 0);
  335. return from_point->pos.distance_to(to_point->pos);
  336. }
  337. Vector<Vector3> AStar::get_point_path(int p_from_id, int p_to_id) {
  338. Point *a;
  339. bool from_exists = points.lookup(p_from_id, a);
  340. ERR_FAIL_COND_V(!from_exists, Vector<Vector3>());
  341. Point *b;
  342. bool to_exists = points.lookup(p_to_id, b);
  343. ERR_FAIL_COND_V(!to_exists, Vector<Vector3>());
  344. if (a == b) {
  345. Vector<Vector3> ret;
  346. ret.push_back(a->pos);
  347. return ret;
  348. }
  349. Point *begin_point = a;
  350. Point *end_point = b;
  351. bool found_route = _solve(begin_point, end_point);
  352. if (!found_route) {
  353. return Vector<Vector3>();
  354. }
  355. Point *p = end_point;
  356. int pc = 1; // Begin point
  357. while (p != begin_point) {
  358. pc++;
  359. p = p->prev_point;
  360. }
  361. Vector<Vector3> path;
  362. path.resize(pc);
  363. {
  364. Vector3 *w = path.ptrw();
  365. Point *p2 = end_point;
  366. int idx = pc - 1;
  367. while (p2 != begin_point) {
  368. w[idx--] = p2->pos;
  369. p2 = p2->prev_point;
  370. }
  371. w[0] = p2->pos; // Assign first
  372. }
  373. return path;
  374. }
  375. Vector<int> AStar::get_id_path(int p_from_id, int p_to_id) {
  376. Point *a;
  377. bool from_exists = points.lookup(p_from_id, a);
  378. ERR_FAIL_COND_V(!from_exists, Vector<int>());
  379. Point *b;
  380. bool to_exists = points.lookup(p_to_id, b);
  381. ERR_FAIL_COND_V(!to_exists, Vector<int>());
  382. if (a == b) {
  383. Vector<int> ret;
  384. ret.push_back(a->id);
  385. return ret;
  386. }
  387. Point *begin_point = a;
  388. Point *end_point = b;
  389. bool found_route = _solve(begin_point, end_point);
  390. if (!found_route) {
  391. return Vector<int>();
  392. }
  393. Point *p = end_point;
  394. int pc = 1; // Begin point
  395. while (p != begin_point) {
  396. pc++;
  397. p = p->prev_point;
  398. }
  399. Vector<int> path;
  400. path.resize(pc);
  401. {
  402. int *w = path.ptrw();
  403. p = end_point;
  404. int idx = pc - 1;
  405. while (p != begin_point) {
  406. w[idx--] = p->id;
  407. p = p->prev_point;
  408. }
  409. w[0] = p->id; // Assign first
  410. }
  411. return path;
  412. }
  413. void AStar::set_point_disabled(int p_id, bool p_disabled) {
  414. Point *p;
  415. bool p_exists = points.lookup(p_id, p);
  416. ERR_FAIL_COND(!p_exists);
  417. p->enabled = !p_disabled;
  418. }
  419. bool AStar::is_point_disabled(int p_id) const {
  420. Point *p;
  421. bool p_exists = points.lookup(p_id, p);
  422. ERR_FAIL_COND_V(!p_exists, false);
  423. return !p->enabled;
  424. }
  425. void AStar::_bind_methods() {
  426. ClassDB::bind_method(D_METHOD("get_available_point_id"), &AStar::get_available_point_id);
  427. ClassDB::bind_method(D_METHOD("add_point", "id", "position", "weight_scale"), &AStar::add_point, DEFVAL(1.0));
  428. ClassDB::bind_method(D_METHOD("get_point_position", "id"), &AStar::get_point_position);
  429. ClassDB::bind_method(D_METHOD("set_point_position", "id", "position"), &AStar::set_point_position);
  430. ClassDB::bind_method(D_METHOD("get_point_weight_scale", "id"), &AStar::get_point_weight_scale);
  431. ClassDB::bind_method(D_METHOD("set_point_weight_scale", "id", "weight_scale"), &AStar::set_point_weight_scale);
  432. ClassDB::bind_method(D_METHOD("remove_point", "id"), &AStar::remove_point);
  433. ClassDB::bind_method(D_METHOD("has_point", "id"), &AStar::has_point);
  434. ClassDB::bind_method(D_METHOD("get_point_connections", "id"), &AStar::get_point_connections);
  435. ClassDB::bind_method(D_METHOD("get_points"), &AStar::get_points);
  436. ClassDB::bind_method(D_METHOD("set_point_disabled", "id", "disabled"), &AStar::set_point_disabled, DEFVAL(true));
  437. ClassDB::bind_method(D_METHOD("is_point_disabled", "id"), &AStar::is_point_disabled);
  438. ClassDB::bind_method(D_METHOD("connect_points", "id", "to_id", "bidirectional"), &AStar::connect_points, DEFVAL(true));
  439. ClassDB::bind_method(D_METHOD("disconnect_points", "id", "to_id", "bidirectional"), &AStar::disconnect_points, DEFVAL(true));
  440. ClassDB::bind_method(D_METHOD("are_points_connected", "id", "to_id", "bidirectional"), &AStar::are_points_connected, DEFVAL(true));
  441. ClassDB::bind_method(D_METHOD("get_point_count"), &AStar::get_point_count);
  442. ClassDB::bind_method(D_METHOD("get_point_capacity"), &AStar::get_point_capacity);
  443. ClassDB::bind_method(D_METHOD("reserve_space", "num_nodes"), &AStar::reserve_space);
  444. ClassDB::bind_method(D_METHOD("clear"), &AStar::clear);
  445. ClassDB::bind_method(D_METHOD("get_closest_point", "to_position", "include_disabled"), &AStar::get_closest_point, DEFVAL(false));
  446. ClassDB::bind_method(D_METHOD("get_closest_position_in_segment", "to_position"), &AStar::get_closest_position_in_segment);
  447. ClassDB::bind_method(D_METHOD("get_point_path", "from_id", "to_id"), &AStar::get_point_path);
  448. ClassDB::bind_method(D_METHOD("get_id_path", "from_id", "to_id"), &AStar::get_id_path);
  449. BIND_VMETHOD(MethodInfo(Variant::FLOAT, "_estimate_cost", PropertyInfo(Variant::INT, "from_id"), PropertyInfo(Variant::INT, "to_id")));
  450. BIND_VMETHOD(MethodInfo(Variant::FLOAT, "_compute_cost", PropertyInfo(Variant::INT, "from_id"), PropertyInfo(Variant::INT, "to_id")));
  451. }
  452. AStar::~AStar() {
  453. clear();
  454. }
  455. /////////////////////////////////////////////////////////////
  456. int AStar2D::get_available_point_id() const {
  457. return astar.get_available_point_id();
  458. }
  459. void AStar2D::add_point(int p_id, const Vector2 &p_pos, real_t p_weight_scale) {
  460. astar.add_point(p_id, Vector3(p_pos.x, p_pos.y, 0), p_weight_scale);
  461. }
  462. Vector2 AStar2D::get_point_position(int p_id) const {
  463. Vector3 p = astar.get_point_position(p_id);
  464. return Vector2(p.x, p.y);
  465. }
  466. void AStar2D::set_point_position(int p_id, const Vector2 &p_pos) {
  467. astar.set_point_position(p_id, Vector3(p_pos.x, p_pos.y, 0));
  468. }
  469. real_t AStar2D::get_point_weight_scale(int p_id) const {
  470. return astar.get_point_weight_scale(p_id);
  471. }
  472. void AStar2D::set_point_weight_scale(int p_id, real_t p_weight_scale) {
  473. astar.set_point_weight_scale(p_id, p_weight_scale);
  474. }
  475. void AStar2D::remove_point(int p_id) {
  476. astar.remove_point(p_id);
  477. }
  478. bool AStar2D::has_point(int p_id) const {
  479. return astar.has_point(p_id);
  480. }
  481. Vector<int> AStar2D::get_point_connections(int p_id) {
  482. return astar.get_point_connections(p_id);
  483. }
  484. Array AStar2D::get_points() {
  485. return astar.get_points();
  486. }
  487. void AStar2D::set_point_disabled(int p_id, bool p_disabled) {
  488. astar.set_point_disabled(p_id, p_disabled);
  489. }
  490. bool AStar2D::is_point_disabled(int p_id) const {
  491. return astar.is_point_disabled(p_id);
  492. }
  493. void AStar2D::connect_points(int p_id, int p_with_id, bool p_bidirectional) {
  494. astar.connect_points(p_id, p_with_id, p_bidirectional);
  495. }
  496. void AStar2D::disconnect_points(int p_id, int p_with_id) {
  497. astar.disconnect_points(p_id, p_with_id);
  498. }
  499. bool AStar2D::are_points_connected(int p_id, int p_with_id) const {
  500. return astar.are_points_connected(p_id, p_with_id);
  501. }
  502. int AStar2D::get_point_count() const {
  503. return astar.get_point_count();
  504. }
  505. int AStar2D::get_point_capacity() const {
  506. return astar.get_point_capacity();
  507. }
  508. void AStar2D::clear() {
  509. astar.clear();
  510. }
  511. void AStar2D::reserve_space(int p_num_nodes) {
  512. astar.reserve_space(p_num_nodes);
  513. }
  514. int AStar2D::get_closest_point(const Vector2 &p_point, bool p_include_disabled) const {
  515. return astar.get_closest_point(Vector3(p_point.x, p_point.y, 0), p_include_disabled);
  516. }
  517. Vector2 AStar2D::get_closest_position_in_segment(const Vector2 &p_point) const {
  518. Vector3 p = astar.get_closest_position_in_segment(Vector3(p_point.x, p_point.y, 0));
  519. return Vector2(p.x, p.y);
  520. }
  521. real_t AStar2D::_estimate_cost(int p_from_id, int p_to_id) {
  522. if (get_script_instance() && get_script_instance()->has_method(SceneStringNames::get_singleton()->_estimate_cost)) {
  523. return get_script_instance()->call(SceneStringNames::get_singleton()->_estimate_cost, p_from_id, p_to_id);
  524. }
  525. AStar::Point *from_point;
  526. bool from_exists = astar.points.lookup(p_from_id, from_point);
  527. ERR_FAIL_COND_V(!from_exists, 0);
  528. AStar::Point *to_point;
  529. bool to_exists = astar.points.lookup(p_to_id, to_point);
  530. ERR_FAIL_COND_V(!to_exists, 0);
  531. return from_point->pos.distance_to(to_point->pos);
  532. }
  533. real_t AStar2D::_compute_cost(int p_from_id, int p_to_id) {
  534. if (get_script_instance() && get_script_instance()->has_method(SceneStringNames::get_singleton()->_compute_cost)) {
  535. return get_script_instance()->call(SceneStringNames::get_singleton()->_compute_cost, p_from_id, p_to_id);
  536. }
  537. AStar::Point *from_point;
  538. bool from_exists = astar.points.lookup(p_from_id, from_point);
  539. ERR_FAIL_COND_V(!from_exists, 0);
  540. AStar::Point *to_point;
  541. bool to_exists = astar.points.lookup(p_to_id, to_point);
  542. ERR_FAIL_COND_V(!to_exists, 0);
  543. return from_point->pos.distance_to(to_point->pos);
  544. }
  545. Vector<Vector2> AStar2D::get_point_path(int p_from_id, int p_to_id) {
  546. AStar::Point *a;
  547. bool from_exists = astar.points.lookup(p_from_id, a);
  548. ERR_FAIL_COND_V(!from_exists, Vector<Vector2>());
  549. AStar::Point *b;
  550. bool to_exists = astar.points.lookup(p_to_id, b);
  551. ERR_FAIL_COND_V(!to_exists, Vector<Vector2>());
  552. if (a == b) {
  553. Vector<Vector2> ret;
  554. ret.push_back(Vector2(a->pos.x, a->pos.y));
  555. return ret;
  556. }
  557. AStar::Point *begin_point = a;
  558. AStar::Point *end_point = b;
  559. bool found_route = _solve(begin_point, end_point);
  560. if (!found_route) {
  561. return Vector<Vector2>();
  562. }
  563. AStar::Point *p = end_point;
  564. int pc = 1; // Begin point
  565. while (p != begin_point) {
  566. pc++;
  567. p = p->prev_point;
  568. }
  569. Vector<Vector2> path;
  570. path.resize(pc);
  571. {
  572. Vector2 *w = path.ptrw();
  573. AStar::Point *p2 = end_point;
  574. int idx = pc - 1;
  575. while (p2 != begin_point) {
  576. w[idx--] = Vector2(p2->pos.x, p2->pos.y);
  577. p2 = p2->prev_point;
  578. }
  579. w[0] = Vector2(p2->pos.x, p2->pos.y); // Assign first
  580. }
  581. return path;
  582. }
  583. Vector<int> AStar2D::get_id_path(int p_from_id, int p_to_id) {
  584. AStar::Point *a;
  585. bool from_exists = astar.points.lookup(p_from_id, a);
  586. ERR_FAIL_COND_V(!from_exists, Vector<int>());
  587. AStar::Point *b;
  588. bool to_exists = astar.points.lookup(p_to_id, b);
  589. ERR_FAIL_COND_V(!to_exists, Vector<int>());
  590. if (a == b) {
  591. Vector<int> ret;
  592. ret.push_back(a->id);
  593. return ret;
  594. }
  595. AStar::Point *begin_point = a;
  596. AStar::Point *end_point = b;
  597. bool found_route = _solve(begin_point, end_point);
  598. if (!found_route) {
  599. return Vector<int>();
  600. }
  601. AStar::Point *p = end_point;
  602. int pc = 1; // Begin point
  603. while (p != begin_point) {
  604. pc++;
  605. p = p->prev_point;
  606. }
  607. Vector<int> path;
  608. path.resize(pc);
  609. {
  610. int *w = path.ptrw();
  611. p = end_point;
  612. int idx = pc - 1;
  613. while (p != begin_point) {
  614. w[idx--] = p->id;
  615. p = p->prev_point;
  616. }
  617. w[0] = p->id; // Assign first
  618. }
  619. return path;
  620. }
  621. bool AStar2D::_solve(AStar::Point *begin_point, AStar::Point *end_point) {
  622. astar.pass++;
  623. if (!end_point->enabled) {
  624. return false;
  625. }
  626. bool found_route = false;
  627. Vector<AStar::Point *> open_list;
  628. SortArray<AStar::Point *, AStar::SortPoints> sorter;
  629. begin_point->g_score = 0;
  630. begin_point->f_score = _estimate_cost(begin_point->id, end_point->id);
  631. open_list.push_back(begin_point);
  632. while (!open_list.empty()) {
  633. AStar::Point *p = open_list[0]; // The currently processed point
  634. if (p == end_point) {
  635. found_route = true;
  636. break;
  637. }
  638. sorter.pop_heap(0, open_list.size(), open_list.ptrw()); // Remove the current point from the open list
  639. open_list.remove(open_list.size() - 1);
  640. p->closed_pass = astar.pass; // Mark the point as closed
  641. for (OAHashMap<int, AStar::Point *>::Iterator it = p->neighbours.iter(); it.valid; it = p->neighbours.next_iter(it)) {
  642. AStar::Point *e = *(it.value); // The neighbour point
  643. if (!e->enabled || e->closed_pass == astar.pass) {
  644. continue;
  645. }
  646. real_t tentative_g_score = p->g_score + _compute_cost(p->id, e->id) * e->weight_scale;
  647. bool new_point = false;
  648. if (e->open_pass != astar.pass) { // The point wasn't inside the open list.
  649. e->open_pass = astar.pass;
  650. open_list.push_back(e);
  651. new_point = true;
  652. } else if (tentative_g_score >= e->g_score) { // The new path is worse than the previous.
  653. continue;
  654. }
  655. e->prev_point = p;
  656. e->g_score = tentative_g_score;
  657. e->f_score = e->g_score + _estimate_cost(e->id, end_point->id);
  658. if (new_point) { // The position of the new points is already known.
  659. sorter.push_heap(0, open_list.size() - 1, 0, e, open_list.ptrw());
  660. } else {
  661. sorter.push_heap(0, open_list.find(e), 0, e, open_list.ptrw());
  662. }
  663. }
  664. }
  665. return found_route;
  666. }
  667. void AStar2D::_bind_methods() {
  668. ClassDB::bind_method(D_METHOD("get_available_point_id"), &AStar2D::get_available_point_id);
  669. ClassDB::bind_method(D_METHOD("add_point", "id", "position", "weight_scale"), &AStar2D::add_point, DEFVAL(1.0));
  670. ClassDB::bind_method(D_METHOD("get_point_position", "id"), &AStar2D::get_point_position);
  671. ClassDB::bind_method(D_METHOD("set_point_position", "id", "position"), &AStar2D::set_point_position);
  672. ClassDB::bind_method(D_METHOD("get_point_weight_scale", "id"), &AStar2D::get_point_weight_scale);
  673. ClassDB::bind_method(D_METHOD("set_point_weight_scale", "id", "weight_scale"), &AStar2D::set_point_weight_scale);
  674. ClassDB::bind_method(D_METHOD("remove_point", "id"), &AStar2D::remove_point);
  675. ClassDB::bind_method(D_METHOD("has_point", "id"), &AStar2D::has_point);
  676. ClassDB::bind_method(D_METHOD("get_point_connections", "id"), &AStar2D::get_point_connections);
  677. ClassDB::bind_method(D_METHOD("get_points"), &AStar2D::get_points);
  678. ClassDB::bind_method(D_METHOD("set_point_disabled", "id", "disabled"), &AStar2D::set_point_disabled, DEFVAL(true));
  679. ClassDB::bind_method(D_METHOD("is_point_disabled", "id"), &AStar2D::is_point_disabled);
  680. ClassDB::bind_method(D_METHOD("connect_points", "id", "to_id", "bidirectional"), &AStar2D::connect_points, DEFVAL(true));
  681. ClassDB::bind_method(D_METHOD("disconnect_points", "id", "to_id"), &AStar2D::disconnect_points);
  682. ClassDB::bind_method(D_METHOD("are_points_connected", "id", "to_id"), &AStar2D::are_points_connected);
  683. ClassDB::bind_method(D_METHOD("get_point_count"), &AStar2D::get_point_count);
  684. ClassDB::bind_method(D_METHOD("get_point_capacity"), &AStar2D::get_point_capacity);
  685. ClassDB::bind_method(D_METHOD("reserve_space", "num_nodes"), &AStar2D::reserve_space);
  686. ClassDB::bind_method(D_METHOD("clear"), &AStar2D::clear);
  687. ClassDB::bind_method(D_METHOD("get_closest_point", "to_position", "include_disabled"), &AStar2D::get_closest_point, DEFVAL(false));
  688. ClassDB::bind_method(D_METHOD("get_closest_position_in_segment", "to_position"), &AStar2D::get_closest_position_in_segment);
  689. ClassDB::bind_method(D_METHOD("get_point_path", "from_id", "to_id"), &AStar2D::get_point_path);
  690. ClassDB::bind_method(D_METHOD("get_id_path", "from_id", "to_id"), &AStar2D::get_id_path);
  691. BIND_VMETHOD(MethodInfo(Variant::FLOAT, "_estimate_cost", PropertyInfo(Variant::INT, "from_id"), PropertyInfo(Variant::INT, "to_id")));
  692. BIND_VMETHOD(MethodInfo(Variant::FLOAT, "_compute_cost", PropertyInfo(Variant::INT, "from_id"), PropertyInfo(Variant::INT, "to_id")));
  693. }