a_star.cpp 11 KB

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