a_star.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. #include "a_star.h"
  2. #include "geometry.h"
  3. int AStar::get_available_point_id() const {
  4. if (points.empty()) {
  5. return 1;
  6. }
  7. return points.back()->key()+1;
  8. }
  9. void AStar::add_point(int p_id, const Vector3 &p_pos, float p_weight_scale) {
  10. ERR_FAIL_COND(p_id<0);
  11. if (!points.has(p_id)) {
  12. Point *pt = memnew( Point );
  13. pt->id=p_id;
  14. pt->pos=p_pos;
  15. pt->weight_scale=p_weight_scale;
  16. pt->prev_point=NULL;
  17. pt->last_pass=0;
  18. points[p_id]=pt;
  19. } else {
  20. points[p_id]->pos=p_pos;
  21. points[p_id]->weight_scale=p_weight_scale;
  22. }
  23. }
  24. Vector3 AStar::get_point_pos(int p_id) const{
  25. ERR_FAIL_COND_V(!points.has(p_id),Vector3());
  26. return points[p_id]->pos;
  27. }
  28. float AStar::get_point_weight_scale(int p_id) const{
  29. ERR_FAIL_COND_V(!points.has(p_id),0);
  30. return points[p_id]->weight_scale;
  31. }
  32. void AStar::remove_point(int p_id){
  33. ERR_FAIL_COND(!points.has(p_id));
  34. Point* p = points[p_id];
  35. for(int i=0;i<p->neighbours.size();i++) {
  36. Segment s(p_id,p->neighbours[i]->id);
  37. segments.erase(s);
  38. p->neighbours[i]->neighbours.erase(p);
  39. }
  40. memdelete(p);
  41. points.erase(p_id);
  42. }
  43. void AStar::connect_points(int p_id,int p_with_id){
  44. ERR_FAIL_COND(!points.has(p_id));
  45. ERR_FAIL_COND(!points.has(p_with_id));
  46. ERR_FAIL_COND(p_id==p_with_id);
  47. Point* a = points[p_id];
  48. Point* b = points[p_with_id];
  49. a->neighbours.push_back(b);
  50. b->neighbours.push_back(a);
  51. Segment s(p_id,p_with_id);
  52. if (s.from==p_id) {
  53. s.from_point=a;
  54. s.to_point=b;
  55. } else {
  56. s.from_point=b;
  57. s.to_point=a;
  58. }
  59. segments.insert(s);
  60. }
  61. void AStar::disconnect_points(int p_id,int p_with_id){
  62. Segment s(p_id,p_with_id);
  63. ERR_FAIL_COND(!segments.has(s));
  64. segments.erase(s);
  65. Point *a = points[p_id];
  66. Point *b = points[p_with_id];
  67. a->neighbours.erase(b);
  68. b->neighbours.erase(a);
  69. }
  70. bool AStar::are_points_connected(int p_id,int p_with_id) const{
  71. Segment s(p_id,p_with_id);
  72. return segments.has(s);
  73. }
  74. void AStar::clear(){
  75. for (const Map<int,Point*>::Element *E=points.front();E;E=E->next()) {
  76. memdelete(E->get());
  77. }
  78. segments.clear();
  79. points.clear();
  80. }
  81. int AStar::get_closest_point(const Vector3& p_point) const{
  82. int closest_id=-1;
  83. float closest_dist=1e20;
  84. for (const Map<int,Point*>::Element *E=points.front();E;E=E->next()) {
  85. float d = p_point.distance_squared_to(E->get()->pos);
  86. if (closest_id<0 || d<closest_dist) {
  87. closest_dist=d;
  88. closest_id=E->key();
  89. }
  90. }
  91. return closest_id;
  92. }
  93. Vector3 AStar::get_closest_pos_in_segment(const Vector3& p_point) const {
  94. float closest_dist = 1e20;
  95. bool found=false;
  96. Vector3 closest_point;
  97. for (const Set<Segment>::Element *E=segments.front();E;E=E->next()) {
  98. Vector3 segment[2]={
  99. E->get().from_point->pos,
  100. E->get().to_point->pos,
  101. };
  102. Vector3 p = Geometry::get_closest_point_to_segment(p_point,segment);
  103. float d = p_point.distance_squared_to(p);
  104. if (!found || d<closest_dist) {
  105. closest_point=p;
  106. closest_dist=d;
  107. found=true;
  108. }
  109. }
  110. return closest_point;
  111. }
  112. bool AStar::_solve(Point* begin_point, Point* end_point) {
  113. pass++;
  114. SelfList<Point>::List open_list;
  115. bool found_route=false;
  116. for(int i=0;i<begin_point->neighbours.size();i++) {
  117. Point *n = begin_point->neighbours[i];
  118. n->prev_point=begin_point;
  119. n->distance=n->pos.distance_to(begin_point->pos);
  120. n->distance*=n->weight_scale;
  121. n->last_pass=pass;
  122. open_list.add(&n->list);
  123. if (end_point==n) {
  124. found_route=true;
  125. break;
  126. }
  127. }
  128. while(!found_route) {
  129. if (open_list.first()==NULL) {
  130. //could not find path sadly
  131. break;
  132. }
  133. //check open list
  134. SelfList<Point> *least_cost_point=NULL;
  135. float least_cost=1e30;
  136. //this could be faster (cache previous results)
  137. for (SelfList<Point> *E=open_list.first();E;E=E->next()) {
  138. Point *p=E->self();
  139. float cost=p->distance;
  140. cost+=p->pos.distance_to(end_point->pos);
  141. cost*=p->weight_scale;
  142. if (cost<least_cost) {
  143. least_cost_point=E;
  144. least_cost=cost;
  145. }
  146. }
  147. Point *p=least_cost_point->self();
  148. //open the neighbours for search
  149. int es = p->neighbours.size();
  150. for(int i=0;i<es;i++) {
  151. Point* e=p->neighbours[i];
  152. float distance = p->pos.distance_to(e->pos) + p->distance;
  153. distance*=e->weight_scale;
  154. if (e->last_pass==pass) {
  155. //oh this was visited already, can we win the cost?
  156. if (e->distance>distance) {
  157. e->prev_point=p;
  158. e->distance=distance;
  159. }
  160. } else {
  161. //add to open neighbours
  162. e->prev_point=p;
  163. e->distance=distance;
  164. e->last_pass=pass; //mark as used
  165. open_list.add(&e->list);
  166. if (e==end_point) {
  167. //oh my reached end! stop algorithm
  168. found_route=true;
  169. break;
  170. }
  171. }
  172. }
  173. if (found_route)
  174. break;
  175. open_list.remove(least_cost_point);
  176. }
  177. //clear the openf list
  178. while(open_list.first()) {
  179. open_list.remove( open_list.first() );
  180. }
  181. return found_route;
  182. }
  183. DVector<Vector3> AStar::get_point_path(int p_from_id, int p_to_id) {
  184. ERR_FAIL_COND_V(!points.has(p_from_id),DVector<Vector3>());
  185. ERR_FAIL_COND_V(!points.has(p_to_id),DVector<Vector3>());
  186. pass++;
  187. Point* a = points[p_from_id];
  188. Point* b = points[p_to_id];
  189. if (a==b) {
  190. DVector<Vector3> ret;
  191. ret.push_back(a->pos);
  192. return ret;
  193. }
  194. Point *begin_point=a;
  195. Point *end_point=b;
  196. bool found_route=_solve(begin_point,end_point);
  197. if (!found_route)
  198. return DVector<Vector3>();
  199. //midpoints
  200. Point *p=end_point;
  201. int pc=1; //begin point
  202. while(p!=begin_point) {
  203. pc++;
  204. p=p->prev_point;
  205. }
  206. DVector<Vector3> path;
  207. path.resize(pc);
  208. {
  209. DVector<Vector3>::Write w = path.write();
  210. Point *p=end_point;
  211. int idx=pc-1;
  212. while(p!=begin_point) {
  213. w[idx--]=p->pos;
  214. p=p->prev_point;
  215. }
  216. w[0]=p->pos; //assign first
  217. }
  218. return path;
  219. }
  220. DVector<int> AStar::get_id_path(int p_from_id, int p_to_id) {
  221. ERR_FAIL_COND_V(!points.has(p_from_id),DVector<int>());
  222. ERR_FAIL_COND_V(!points.has(p_to_id),DVector<int>());
  223. pass++;
  224. Point* a = points[p_from_id];
  225. Point* b = points[p_to_id];
  226. if (a==b) {
  227. DVector<int> ret;
  228. ret.push_back(a->id);
  229. return ret;
  230. }
  231. Point *begin_point=a;
  232. Point *end_point=b;
  233. bool found_route=_solve(begin_point,end_point);
  234. if (!found_route)
  235. return DVector<int>();
  236. //midpoints
  237. Point *p=end_point;
  238. int pc=1; //begin point
  239. while(p!=begin_point) {
  240. pc++;
  241. p=p->prev_point;
  242. }
  243. DVector<int> path;
  244. path.resize(pc);
  245. {
  246. DVector<int>::Write w = path.write();
  247. p=end_point;
  248. int idx=pc-1;
  249. while(p!=begin_point) {
  250. w[idx--]=p->id;
  251. p=p->prev_point;
  252. }
  253. w[0]=p->id; //assign first
  254. }
  255. return path;
  256. }
  257. void AStar::_bind_methods() {
  258. ObjectTypeDB::bind_method(_MD("get_available_point_id"),&AStar::get_available_point_id);
  259. ObjectTypeDB::bind_method(_MD("add_point","id","pos","weight_scale"),&AStar::add_point,DEFVAL(1.0));
  260. ObjectTypeDB::bind_method(_MD("get_point_pos","id"),&AStar::get_point_pos);
  261. ObjectTypeDB::bind_method(_MD("get_point_weight_scale","id"),&AStar::get_point_weight_scale);
  262. ObjectTypeDB::bind_method(_MD("remove_point","id"),&AStar::remove_point);
  263. ObjectTypeDB::bind_method(_MD("connect_points","id","to_id"),&AStar::connect_points);
  264. ObjectTypeDB::bind_method(_MD("disconnect_points","id","to_id"),&AStar::disconnect_points);
  265. ObjectTypeDB::bind_method(_MD("are_points_connected","id","to_id"),&AStar::are_points_connected);
  266. ObjectTypeDB::bind_method(_MD("clear"),&AStar::clear);
  267. ObjectTypeDB::bind_method(_MD("get_closest_point","to_pos"),&AStar::get_closest_point);
  268. ObjectTypeDB::bind_method(_MD("get_closest_pos_in_segment","to_pos"),&AStar::get_closest_pos_in_segment);
  269. ObjectTypeDB::bind_method(_MD("get_point_path","from_id","to_id"),&AStar::get_point_path);
  270. ObjectTypeDB::bind_method(_MD("get_id_path","from_id","to_id"),&AStar::get_id_path);
  271. }
  272. AStar::AStar() {
  273. pass=1;
  274. }
  275. AStar::~AStar() {
  276. pass=1;
  277. }