a_star.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  1. /*************************************************************************/
  2. /* a_star.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2017 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2017 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 "geometry.h"
  32. #include "scene/scene_string_names.h"
  33. #include "script_language.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 (int i = 0; i < p->neighbours.size(); i++) {
  77. Segment s(p_id, p->neighbours[i]->id);
  78. segments.erase(s);
  79. p->neighbours[i]->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.push_back(b);
  91. if (bidirectional)
  92. b->neighbours.push_back(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 (int i = 0; i < p->neighbours.size(); i++) {
  127. point_list.push_back(p->neighbours[i]->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 (int i = 0; i < begin_point->neighbours.size(); i++) {
  178. Point *n = begin_point->neighbours[i];
  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. if (end_point == n) {
  184. found_route = true;
  185. break;
  186. }
  187. }
  188. while (!found_route) {
  189. if (open_list.first() == NULL) {
  190. // No path found
  191. break;
  192. }
  193. // Check open list
  194. SelfList<Point> *least_cost_point = NULL;
  195. real_t least_cost = 1e30;
  196. // TODO: Cache previous results
  197. for (SelfList<Point> *E = open_list.first(); E; E = E->next()) {
  198. Point *p = E->self();
  199. real_t cost = p->distance;
  200. cost += _estimate_cost(p->id, end_point->id);
  201. if (cost < least_cost) {
  202. least_cost_point = E;
  203. least_cost = cost;
  204. }
  205. }
  206. Point *p = least_cost_point->self();
  207. // Open the neighbours for search
  208. int es = p->neighbours.size();
  209. for (int i = 0; i < es; i++) {
  210. Point *e = p->neighbours[i];
  211. real_t distance = _compute_cost(p->id, e->id) * e->weight_scale + p->distance;
  212. if (e->last_pass == pass) {
  213. // Already visited, is this cheaper?
  214. if (e->distance > distance) {
  215. e->prev_point = p;
  216. e->distance = distance;
  217. }
  218. } else {
  219. // Add to open neighbours
  220. e->prev_point = p;
  221. e->distance = distance;
  222. e->last_pass = pass; // Mark as used
  223. open_list.add(&e->list);
  224. if (e == end_point) {
  225. // End reached; stop algorithm
  226. found_route = true;
  227. break;
  228. }
  229. }
  230. }
  231. if (found_route)
  232. break;
  233. open_list.remove(least_cost_point);
  234. }
  235. // Clear the openf list
  236. while (open_list.first()) {
  237. open_list.remove(open_list.first());
  238. }
  239. return found_route;
  240. }
  241. float AStar::_estimate_cost(int p_from_id, int p_to_id) {
  242. if (get_script_instance() && get_script_instance()->has_method(SceneStringNames::get_singleton()->_estimate_cost))
  243. return get_script_instance()->call(SceneStringNames::get_singleton()->_estimate_cost, p_from_id, p_to_id);
  244. return points[p_from_id]->pos.distance_to(points[p_to_id]->pos);
  245. }
  246. float AStar::_compute_cost(int p_from_id, int p_to_id) {
  247. if (get_script_instance() && get_script_instance()->has_method(SceneStringNames::get_singleton()->_compute_cost))
  248. return get_script_instance()->call(SceneStringNames::get_singleton()->_compute_cost, p_from_id, p_to_id);
  249. return points[p_from_id]->pos.distance_to(points[p_to_id]->pos);
  250. }
  251. PoolVector<Vector3> AStar::get_point_path(int p_from_id, int p_to_id) {
  252. ERR_FAIL_COND_V(!points.has(p_from_id), PoolVector<Vector3>());
  253. ERR_FAIL_COND_V(!points.has(p_to_id), PoolVector<Vector3>());
  254. pass++;
  255. Point *a = points[p_from_id];
  256. Point *b = points[p_to_id];
  257. if (a == b) {
  258. PoolVector<Vector3> ret;
  259. ret.push_back(a->pos);
  260. return ret;
  261. }
  262. Point *begin_point = a;
  263. Point *end_point = b;
  264. bool found_route = _solve(begin_point, end_point);
  265. if (!found_route)
  266. return PoolVector<Vector3>();
  267. // Midpoints
  268. Point *p = end_point;
  269. int pc = 1; // Begin point
  270. while (p != begin_point) {
  271. pc++;
  272. p = p->prev_point;
  273. }
  274. PoolVector<Vector3> path;
  275. path.resize(pc);
  276. {
  277. PoolVector<Vector3>::Write w = path.write();
  278. Point *p = end_point;
  279. int idx = pc - 1;
  280. while (p != begin_point) {
  281. w[idx--] = p->pos;
  282. p = p->prev_point;
  283. }
  284. w[0] = p->pos; // Assign first
  285. }
  286. return path;
  287. }
  288. PoolVector<int> AStar::get_id_path(int p_from_id, int p_to_id) {
  289. ERR_FAIL_COND_V(!points.has(p_from_id), PoolVector<int>());
  290. ERR_FAIL_COND_V(!points.has(p_to_id), PoolVector<int>());
  291. pass++;
  292. Point *a = points[p_from_id];
  293. Point *b = points[p_to_id];
  294. if (a == b) {
  295. PoolVector<int> ret;
  296. ret.push_back(a->id);
  297. return ret;
  298. }
  299. Point *begin_point = a;
  300. Point *end_point = b;
  301. bool found_route = _solve(begin_point, end_point);
  302. if (!found_route)
  303. return PoolVector<int>();
  304. // Midpoints
  305. Point *p = end_point;
  306. int pc = 1; // Begin point
  307. while (p != begin_point) {
  308. pc++;
  309. p = p->prev_point;
  310. }
  311. PoolVector<int> path;
  312. path.resize(pc);
  313. {
  314. PoolVector<int>::Write w = path.write();
  315. p = end_point;
  316. int idx = pc - 1;
  317. while (p != begin_point) {
  318. w[idx--] = p->id;
  319. p = p->prev_point;
  320. }
  321. w[0] = p->id; // Assign first
  322. }
  323. return path;
  324. }
  325. void AStar::_bind_methods() {
  326. ClassDB::bind_method(D_METHOD("get_available_point_id"), &AStar::get_available_point_id);
  327. ClassDB::bind_method(D_METHOD("add_point", "id", "position", "weight_scale"), &AStar::add_point, DEFVAL(1.0));
  328. ClassDB::bind_method(D_METHOD("get_point_position", "id"), &AStar::get_point_position);
  329. ClassDB::bind_method(D_METHOD("set_point_position", "id", "position"), &AStar::set_point_position);
  330. ClassDB::bind_method(D_METHOD("get_point_weight_scale", "id"), &AStar::get_point_weight_scale);
  331. ClassDB::bind_method(D_METHOD("set_point_weight_scale", "id", "weight_scale"), &AStar::set_point_weight_scale);
  332. ClassDB::bind_method(D_METHOD("remove_point", "id"), &AStar::remove_point);
  333. ClassDB::bind_method(D_METHOD("has_point", "id"), &AStar::has_point);
  334. ClassDB::bind_method(D_METHOD("get_points"), &AStar::get_points);
  335. ClassDB::bind_method(D_METHOD("get_point_connections"), &AStar::get_point_connections);
  336. ClassDB::bind_method(D_METHOD("connect_points", "id", "to_id", "bidirectional"), &AStar::connect_points, DEFVAL(true));
  337. ClassDB::bind_method(D_METHOD("disconnect_points", "id", "to_id"), &AStar::disconnect_points);
  338. ClassDB::bind_method(D_METHOD("are_points_connected", "id", "to_id"), &AStar::are_points_connected);
  339. ClassDB::bind_method(D_METHOD("clear"), &AStar::clear);
  340. ClassDB::bind_method(D_METHOD("get_closest_point", "to_position"), &AStar::get_closest_point);
  341. ClassDB::bind_method(D_METHOD("get_closest_position_in_segment", "to_position"), &AStar::get_closest_position_in_segment);
  342. ClassDB::bind_method(D_METHOD("get_point_path", "from_id", "to_id"), &AStar::get_point_path);
  343. ClassDB::bind_method(D_METHOD("get_id_path", "from_id", "to_id"), &AStar::get_id_path);
  344. BIND_VMETHOD(MethodInfo("_estimate_cost", PropertyInfo(Variant::INT, "from_id"), PropertyInfo(Variant::INT, "to_id")));
  345. BIND_VMETHOD(MethodInfo("_compute_cost", PropertyInfo(Variant::INT, "from_id"), PropertyInfo(Variant::INT, "to_id")));
  346. }
  347. AStar::AStar() {
  348. pass = 1;
  349. }
  350. AStar::~AStar() {
  351. pass = 1;
  352. clear();
  353. }