Pathfind 2D.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /******************************************************************************/
  2. #include "stdafx.h"
  3. namespace EE{
  4. /******************************************************************************/
  5. void PathFind::Pixel::create(Int x, Int y) {xy.set(x, y); flag=0; iteration=0;}
  6. /******************************************************************************/
  7. PathFind::PathFind() {zero();}
  8. void PathFind::zero()
  9. {
  10. _size .zero();
  11. _border.zero();
  12. _iteration=0;
  13. _map=null;
  14. }
  15. PathFind& PathFind::del()
  16. {
  17. Free(_map);
  18. _active.del();
  19. zero(); return T;
  20. }
  21. PathFind& PathFind::create(Int w, Int h)
  22. {
  23. if(w<0)w=_size.x;
  24. if(h<0)h=_size.y;
  25. if(_size.x!=w || _size.y!=h)
  26. {
  27. Realloc(_map, w*h, 0);
  28. _size.x=w;
  29. _size.y=h;
  30. REPD(y, h)
  31. REPD(x, w)pixel(x, y).create(x, y);
  32. }
  33. return border(0, 0, _size.x, _size.y);
  34. }
  35. /******************************************************************************/
  36. UInt PathFind::pixelFlag(Int x, Int y)C
  37. {
  38. return validPos(x, y) ? pixel(x, y).flag : 0;
  39. }
  40. Bool PathFind::pixelWalkable(Int x, Int y)C {return FlagTest(pixelFlag(x, y), PFP_WALKABLE);}
  41. Bool PathFind::pixelTarget (Int x, Int y)C {return FlagTest(pixelFlag(x, y), PFP_TARGET );}
  42. Bool PathFind::pixelStart (Int x, Int y)C {return FlagTest(pixelFlag(x, y), PFP_START );}
  43. PathFind& PathFind::pixelFlag(Int x, Int y, Byte flag)
  44. {
  45. if(validPos(x, y))pixel(x, y).flag=flag;
  46. return T;
  47. }
  48. PathFind& PathFind::pixelWalkable(Int x, Int y, Bool on)
  49. {
  50. if(validPos(x, y))FlagSet(pixel(x, y).flag, PFP_WALKABLE, on);
  51. return T;
  52. }
  53. PathFind& PathFind::pixelTarget(Int x, Int y, Bool on)
  54. {
  55. if(validPos(x, y))FlagSet(pixel(x, y).flag, PFP_TARGET, on);
  56. return T;
  57. }
  58. PathFind& PathFind::pixelStart(Int x, Int y, Bool on)
  59. {
  60. if(validPos(x, y))FlagSet(pixel(x, y).flag, PFP_START, on);
  61. return T;
  62. }
  63. PathFind& PathFind::border(Int min_x, Int min_y, Int max_x, Int max_y)
  64. {
  65. _border.min.x=Max(0, min_x); _border.max.x=Min(_size.x, max_x);
  66. _border.min.y=Max(0, min_y); _border.max.y=Min(_size.y, max_y);
  67. return T;
  68. }
  69. /******************************************************************************/
  70. static struct MOVE
  71. {
  72. VecSB2 dir;
  73. Byte length;
  74. }const Move[8]=
  75. {
  76. {VecSB2( 0, 1), 5},
  77. {VecSB2( 0,-1), 5},
  78. {VecSB2( 1, 0), 5},
  79. {VecSB2(-1, 0), 5},
  80. {VecSB2( 1, 1), 7},
  81. {VecSB2( 1,-1), 7},
  82. {VecSB2(-1, 1), 7},
  83. {VecSB2(-1,-1), 7},
  84. };
  85. void PathFind::step()
  86. {
  87. // update iterations (reset pixels if needed)
  88. if(++_iteration==0) // we've reset the counter (got back to zero again)
  89. {
  90. REP(_size.x*_size.y) // reset pixels
  91. {
  92. Pixel &pixel=_map[i];
  93. pixel.iteration=0; // reset iterations
  94. }
  95. _iteration=1;
  96. }
  97. }
  98. Bool PathFind::find(C VecI2 *start, C VecI2 *target, MemPtr<VecI2> path, Int max_steps, Bool diagonal, Bool reversed)
  99. {
  100. const Int moves=(diagonal ? 8 : 4);
  101. // clear path
  102. path.clear();
  103. if(!start || validPos(*start)) // if valid start
  104. {
  105. // if already on target
  106. if(start && onTarget(*start, target))return true; // if there's only one 'start' point then there's no need for setting 'path'
  107. // no steps left
  108. if(!max_steps)return false;
  109. step();
  110. // setup first step
  111. _active.clear();
  112. if(start)
  113. {
  114. Pixel &pix=pixel(*start);
  115. pix.iteration=_iteration;
  116. pix.length =0;
  117. pix.src =null;
  118. _active.add(&pix);
  119. }else
  120. {
  121. for(Int y=_border.min.y; y<_border.max.y; y++)
  122. for(Int x=_border.min.x; x<_border.max.x; x++)
  123. {
  124. Pixel &pix=pixel(x, y); if(pix.flag&PFP_START)
  125. {
  126. VecI2 pos(x, y); if(onTarget(pos, target)){if(path)path.add(pos); return true;} // if already on target, in case where there are multiple start points ('start' is null) then add 'path' so we can know which one is on target
  127. pix.iteration=_iteration;
  128. pix.length =0;
  129. pix.src =null;
  130. _active.add(&pix);
  131. }
  132. }
  133. }
  134. // start searching
  135. UInt found_length;
  136. Pixel *found=null;
  137. for(Int step=0; ; step++)
  138. {
  139. REPA(_active) // order is important
  140. {
  141. Pixel *p=_active[i]; // for each walker
  142. UInt cur_length=p->length; // path travelled by walker
  143. REPD(m, moves) // try going in each direction
  144. {
  145. C MOVE &move=Move[m];
  146. VecI2 v=p->xy+move.dir; if(validPos(v)) // target position
  147. {
  148. UInt new_length=cur_length+move.length;
  149. Pixel &to =pixel(v);
  150. // zero if not used
  151. if(to.iteration!=_iteration)
  152. {
  153. to.iteration =_iteration;
  154. to.added_in_step=-1;
  155. to.length =UINT_MAX;
  156. }
  157. if(new_length<to.length)
  158. {
  159. if(move.dir.x && move.dir.y) // diagonal move
  160. if(pixel(p->xy).flag&PFP_WALKABLE) // test only if we're starting from 'walkable' pixel
  161. if(!(pixel(p->xy.x, v.y).flag&PFP_WALKABLE)
  162. || !(pixel(v.x, p->xy.y).flag&PFP_WALKABLE))continue;
  163. if(onTarget(v, target))
  164. {
  165. to.length=new_length;
  166. to.src =p;
  167. if(!found || new_length<found_length)
  168. {
  169. found =&to;
  170. found_length=new_length;
  171. }
  172. }else
  173. if(to.flag&PFP_WALKABLE)
  174. {
  175. to.length=new_length;
  176. to.src =p;
  177. if(to.added_in_step!=step)
  178. {
  179. to.added_in_step=step;
  180. _active.add(&to);
  181. }
  182. }
  183. }
  184. }
  185. }
  186. _active.remove(i);
  187. }
  188. if(found) // found path
  189. {
  190. if(path) // build path
  191. {
  192. if(!(found->flag&PFP_WALKABLE))found=found->src; // if the last is not walkable then remove it
  193. if(found)for(;;)
  194. {
  195. if(!start)path.add(found->xy); // this includes the starting position in the 'path'
  196. Pixel *src=found->src; if(!src)break;
  197. if( start)path.add(found->xy); // this does not include the starting position in the 'path'
  198. found=src;
  199. }
  200. if(!reversed)path.reverseOrder(); // 'path' is reversed by default when it's created, but if the user doesn't want that, then we need to reverse it
  201. }
  202. return true;
  203. }
  204. if(!_active.elms())return false; // no more paths left to follow
  205. if(!--max_steps )return false; // ran out of steps
  206. }
  207. }
  208. return false;
  209. }
  210. /******************************************************************************/
  211. void PathFind::getWalkableNeighbors(C VecI2 &pos, MemPtr<VecI2> pixels, Bool diagonal)
  212. {
  213. pixels.clear();
  214. if(pixelWalkable(pos.x, pos.y))
  215. {
  216. const Int moves=(diagonal ? 8 : 4);
  217. step();
  218. pixels.add(pos); pixel(pos).iteration=_iteration;
  219. FREPAD(processed, pixels)
  220. {
  221. VecI2 pos=pixels[processed]; // don't use reference because 'pixels' may get modified later
  222. REP(moves)
  223. {
  224. VecI2 next=pos+Move[i].dir; if(validPos(next))
  225. {
  226. Pixel &pix=pixel(next); if(FlagTest(pix.flag, PFP_WALKABLE) && pix.iteration!=_iteration)
  227. {
  228. pixels.add(next); pix.iteration=_iteration;
  229. }
  230. }
  231. }
  232. }
  233. }
  234. }
  235. /******************************************************************************/
  236. }
  237. /******************************************************************************/