a_star.cpp 12 KB

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