a_star.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  1. /*************************************************************************/
  2. /* a_star.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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. return points.back()->key() + 1;
  39. }
  40. void AStar::add_point(int p_id, const Vector3 &p_pos, real_t p_weight_scale) {
  41. ERR_FAIL_COND(p_id < 0);
  42. ERR_FAIL_COND(p_weight_scale < 1);
  43. if (!points.has(p_id)) {
  44. Point *pt = memnew(Point);
  45. pt->id = p_id;
  46. pt->pos = p_pos;
  47. pt->weight_scale = p_weight_scale;
  48. pt->prev_point = NULL;
  49. pt->open_pass = 0;
  50. pt->closed_pass = 0;
  51. pt->enabled = true;
  52. points[p_id] = pt;
  53. } else {
  54. points[p_id]->pos = p_pos;
  55. points[p_id]->weight_scale = p_weight_scale;
  56. }
  57. }
  58. Vector3 AStar::get_point_position(int p_id) const {
  59. ERR_FAIL_COND_V(!points.has(p_id), Vector3());
  60. return points[p_id]->pos;
  61. }
  62. void AStar::set_point_position(int p_id, const Vector3 &p_pos) {
  63. ERR_FAIL_COND(!points.has(p_id));
  64. points[p_id]->pos = p_pos;
  65. }
  66. real_t AStar::get_point_weight_scale(int p_id) const {
  67. ERR_FAIL_COND_V(!points.has(p_id), 0);
  68. return points[p_id]->weight_scale;
  69. }
  70. void AStar::set_point_weight_scale(int p_id, real_t p_weight_scale) {
  71. ERR_FAIL_COND(!points.has(p_id));
  72. ERR_FAIL_COND(p_weight_scale < 1);
  73. points[p_id]->weight_scale = p_weight_scale;
  74. }
  75. void AStar::remove_point(int p_id) {
  76. ERR_FAIL_COND(!points.has(p_id));
  77. Point *p = points[p_id];
  78. for (Set<Point *>::Element *E = p->neighbours.front(); E; E = E->next()) {
  79. Segment s(p_id, E->get()->id);
  80. segments.erase(s);
  81. E->get()->neighbours.erase(p);
  82. E->get()->unlinked_neighbours.erase(p);
  83. }
  84. for (Set<Point *>::Element *E = p->unlinked_neighbours.front(); E; E = E->next()) {
  85. Segment s(p_id, E->get()->id);
  86. segments.erase(s);
  87. E->get()->neighbours.erase(p);
  88. E->get()->unlinked_neighbours.erase(p);
  89. }
  90. memdelete(p);
  91. points.erase(p_id);
  92. }
  93. void AStar::connect_points(int p_id, int p_with_id, bool bidirectional) {
  94. ERR_FAIL_COND(!points.has(p_id));
  95. ERR_FAIL_COND(!points.has(p_with_id));
  96. ERR_FAIL_COND(p_id == p_with_id);
  97. Point *a = points[p_id];
  98. Point *b = points[p_with_id];
  99. a->neighbours.insert(b);
  100. if (bidirectional)
  101. b->neighbours.insert(a);
  102. else
  103. b->unlinked_neighbours.insert(a);
  104. Segment s(p_id, p_with_id);
  105. if (s.from == p_id) {
  106. s.from_point = a;
  107. s.to_point = b;
  108. } else {
  109. s.from_point = b;
  110. s.to_point = a;
  111. }
  112. segments.insert(s);
  113. }
  114. void AStar::disconnect_points(int p_id, int p_with_id) {
  115. Segment s(p_id, p_with_id);
  116. ERR_FAIL_COND(!segments.has(s));
  117. segments.erase(s);
  118. Point *a = points[p_id];
  119. Point *b = points[p_with_id];
  120. a->neighbours.erase(b);
  121. a->unlinked_neighbours.erase(b);
  122. b->neighbours.erase(a);
  123. b->unlinked_neighbours.erase(a);
  124. }
  125. bool AStar::has_point(int p_id) const {
  126. return points.has(p_id);
  127. }
  128. Array AStar::get_points() {
  129. Array point_list;
  130. for (const Map<int, Point *>::Element *E = points.front(); E; E = E->next()) {
  131. point_list.push_back(E->key());
  132. }
  133. return point_list;
  134. }
  135. PoolVector<int> AStar::get_point_connections(int p_id) {
  136. ERR_FAIL_COND_V(!points.has(p_id), PoolVector<int>());
  137. PoolVector<int> point_list;
  138. Point *p = points[p_id];
  139. for (Set<Point *>::Element *E = p->neighbours.front(); E; E = E->next()) {
  140. point_list.push_back(E->get()->id);
  141. }
  142. return point_list;
  143. }
  144. bool AStar::are_points_connected(int p_id, int p_with_id) const {
  145. Segment s(p_id, p_with_id);
  146. return segments.has(s);
  147. }
  148. void AStar::clear() {
  149. for (const Map<int, Point *>::Element *E = points.front(); E; E = E->next()) {
  150. memdelete(E->get());
  151. }
  152. segments.clear();
  153. points.clear();
  154. }
  155. int AStar::get_closest_point(const Vector3 &p_point) const {
  156. int closest_id = -1;
  157. real_t closest_dist = 1e20;
  158. for (const Map<int, Point *>::Element *E = points.front(); E; E = E->next()) {
  159. real_t d = p_point.distance_squared_to(E->get()->pos);
  160. if (closest_id < 0 || d < closest_dist) {
  161. closest_dist = d;
  162. closest_id = E->key();
  163. }
  164. }
  165. return closest_id;
  166. }
  167. Vector3 AStar::get_closest_position_in_segment(const Vector3 &p_point) const {
  168. real_t closest_dist = 1e20;
  169. bool found = false;
  170. Vector3 closest_point;
  171. for (const Set<Segment>::Element *E = segments.front(); E; E = E->next()) {
  172. Vector3 segment[2] = {
  173. E->get().from_point->pos,
  174. E->get().to_point->pos,
  175. };
  176. Vector3 p = Geometry::get_closest_point_to_segment(p_point, segment);
  177. real_t d = p_point.distance_squared_to(p);
  178. if (!found || d < closest_dist) {
  179. closest_point = p;
  180. closest_dist = d;
  181. found = true;
  182. }
  183. }
  184. return closest_point;
  185. }
  186. bool AStar::_solve(Point *begin_point, Point *end_point) {
  187. pass++;
  188. if (!end_point->enabled)
  189. return false;
  190. bool found_route = false;
  191. Vector<Point *> open_list;
  192. SortArray<Point *, SortPoints> sorter;
  193. begin_point->g_score = 0;
  194. begin_point->f_score = _estimate_cost(begin_point->id, end_point->id);
  195. open_list.push_back(begin_point);
  196. while (true) {
  197. if (open_list.size() == 0) // No path found
  198. break;
  199. Point *p = open_list[0]; // The currently processed point
  200. if (p == end_point) {
  201. found_route = true;
  202. break;
  203. }
  204. sorter.pop_heap(0, open_list.size(), open_list.ptrw()); // Remove the current point from the open list
  205. open_list.remove(open_list.size() - 1);
  206. p->closed_pass = pass; // Mark the point as closed
  207. for (Set<Point *>::Element *E = p->neighbours.front(); E; E = E->next()) {
  208. Point *e = E->get(); // The neighbour point
  209. if (!e->enabled || e->closed_pass == pass)
  210. continue;
  211. real_t tentative_g_score = p->g_score + _compute_cost(p->id, e->id) * e->weight_scale;
  212. bool new_point = false;
  213. if (e->open_pass != pass) { // The point wasn't inside the open list
  214. e->open_pass = pass;
  215. open_list.push_back(e);
  216. new_point = true;
  217. } else if (tentative_g_score >= e->g_score) { // The new path is worse than the previous
  218. continue;
  219. }
  220. e->prev_point = p;
  221. e->g_score = tentative_g_score;
  222. e->f_score = e->g_score + _estimate_cost(e->id, end_point->id);
  223. if (new_point) // The position of the new points is already known
  224. sorter.push_heap(0, open_list.size() - 1, 0, e, open_list.ptrw());
  225. else
  226. sorter.push_heap(0, open_list.find(e), 0, e, open_list.ptrw());
  227. }
  228. }
  229. return found_route;
  230. }
  231. float AStar::_estimate_cost(int p_from_id, int p_to_id) {
  232. if (get_script_instance() && get_script_instance()->has_method(SceneStringNames::get_singleton()->_estimate_cost))
  233. return get_script_instance()->call(SceneStringNames::get_singleton()->_estimate_cost, p_from_id, p_to_id);
  234. return points[p_from_id]->pos.distance_to(points[p_to_id]->pos);
  235. }
  236. float AStar::_compute_cost(int p_from_id, int p_to_id) {
  237. if (get_script_instance() && get_script_instance()->has_method(SceneStringNames::get_singleton()->_compute_cost))
  238. return get_script_instance()->call(SceneStringNames::get_singleton()->_compute_cost, p_from_id, p_to_id);
  239. return points[p_from_id]->pos.distance_to(points[p_to_id]->pos);
  240. }
  241. PoolVector<Vector3> AStar::get_point_path(int p_from_id, int p_to_id) {
  242. ERR_FAIL_COND_V(!points.has(p_from_id), PoolVector<Vector3>());
  243. ERR_FAIL_COND_V(!points.has(p_to_id), PoolVector<Vector3>());
  244. Point *a = points[p_from_id];
  245. Point *b = points[p_to_id];
  246. if (a == b) {
  247. PoolVector<Vector3> ret;
  248. ret.push_back(a->pos);
  249. return ret;
  250. }
  251. Point *begin_point = a;
  252. Point *end_point = b;
  253. bool found_route = _solve(begin_point, end_point);
  254. if (!found_route)
  255. return PoolVector<Vector3>();
  256. // Midpoints
  257. Point *p = end_point;
  258. int pc = 1; // Begin point
  259. while (p != begin_point) {
  260. pc++;
  261. p = p->prev_point;
  262. }
  263. PoolVector<Vector3> path;
  264. path.resize(pc);
  265. {
  266. PoolVector<Vector3>::Write w = path.write();
  267. Point *p2 = end_point;
  268. int idx = pc - 1;
  269. while (p2 != begin_point) {
  270. w[idx--] = p2->pos;
  271. p2 = p2->prev_point;
  272. }
  273. w[0] = p2->pos; // Assign first
  274. }
  275. return path;
  276. }
  277. PoolVector<int> AStar::get_id_path(int p_from_id, int p_to_id) {
  278. ERR_FAIL_COND_V(!points.has(p_from_id), PoolVector<int>());
  279. ERR_FAIL_COND_V(!points.has(p_to_id), PoolVector<int>());
  280. Point *a = points[p_from_id];
  281. Point *b = points[p_to_id];
  282. if (a == b) {
  283. PoolVector<int> ret;
  284. ret.push_back(a->id);
  285. return ret;
  286. }
  287. Point *begin_point = a;
  288. Point *end_point = b;
  289. bool found_route = _solve(begin_point, end_point);
  290. if (!found_route)
  291. return PoolVector<int>();
  292. // Midpoints
  293. Point *p = end_point;
  294. int pc = 1; // Begin point
  295. while (p != begin_point) {
  296. pc++;
  297. p = p->prev_point;
  298. }
  299. PoolVector<int> path;
  300. path.resize(pc);
  301. {
  302. PoolVector<int>::Write w = path.write();
  303. p = end_point;
  304. int idx = pc - 1;
  305. while (p != begin_point) {
  306. w[idx--] = p->id;
  307. p = p->prev_point;
  308. }
  309. w[0] = p->id; // Assign first
  310. }
  311. return path;
  312. }
  313. void AStar::set_point_disabled(int p_id, bool p_disabled) {
  314. ERR_FAIL_COND(!points.has(p_id));
  315. points[p_id]->enabled = !p_disabled;
  316. }
  317. bool AStar::is_point_disabled(int p_id) const {
  318. ERR_FAIL_COND_V(!points.has(p_id), false);
  319. return !points[p_id]->enabled;
  320. }
  321. void AStar::_bind_methods() {
  322. ClassDB::bind_method(D_METHOD("get_available_point_id"), &AStar::get_available_point_id);
  323. ClassDB::bind_method(D_METHOD("add_point", "id", "position", "weight_scale"), &AStar::add_point, DEFVAL(1.0));
  324. ClassDB::bind_method(D_METHOD("get_point_position", "id"), &AStar::get_point_position);
  325. ClassDB::bind_method(D_METHOD("set_point_position", "id", "position"), &AStar::set_point_position);
  326. ClassDB::bind_method(D_METHOD("get_point_weight_scale", "id"), &AStar::get_point_weight_scale);
  327. ClassDB::bind_method(D_METHOD("set_point_weight_scale", "id", "weight_scale"), &AStar::set_point_weight_scale);
  328. ClassDB::bind_method(D_METHOD("remove_point", "id"), &AStar::remove_point);
  329. ClassDB::bind_method(D_METHOD("has_point", "id"), &AStar::has_point);
  330. ClassDB::bind_method(D_METHOD("get_points"), &AStar::get_points);
  331. ClassDB::bind_method(D_METHOD("set_point_disabled", "id", "disabled"), &AStar::set_point_disabled, DEFVAL(true));
  332. ClassDB::bind_method(D_METHOD("is_point_disabled", "id"), &AStar::is_point_disabled);
  333. ClassDB::bind_method(D_METHOD("get_point_connections", "id"), &AStar::get_point_connections);
  334. ClassDB::bind_method(D_METHOD("connect_points", "id", "to_id", "bidirectional"), &AStar::connect_points, DEFVAL(true));
  335. ClassDB::bind_method(D_METHOD("disconnect_points", "id", "to_id"), &AStar::disconnect_points);
  336. ClassDB::bind_method(D_METHOD("are_points_connected", "id", "to_id"), &AStar::are_points_connected);
  337. ClassDB::bind_method(D_METHOD("clear"), &AStar::clear);
  338. ClassDB::bind_method(D_METHOD("get_closest_point", "to_position"), &AStar::get_closest_point);
  339. ClassDB::bind_method(D_METHOD("get_closest_position_in_segment", "to_position"), &AStar::get_closest_position_in_segment);
  340. ClassDB::bind_method(D_METHOD("get_point_path", "from_id", "to_id"), &AStar::get_point_path);
  341. ClassDB::bind_method(D_METHOD("get_id_path", "from_id", "to_id"), &AStar::get_id_path);
  342. BIND_VMETHOD(MethodInfo(Variant::REAL, "_estimate_cost", PropertyInfo(Variant::INT, "from_id"), PropertyInfo(Variant::INT, "to_id")));
  343. BIND_VMETHOD(MethodInfo(Variant::REAL, "_compute_cost", PropertyInfo(Variant::INT, "from_id"), PropertyInfo(Variant::INT, "to_id")));
  344. }
  345. AStar::AStar() {
  346. pass = 1;
  347. }
  348. AStar::~AStar() {
  349. pass = 1;
  350. clear();
  351. }