2
0

a_star.cpp 12 KB

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