Pathfind 2D.h 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /******************************************************************************
  2. Use 'PathFind' for 2D map path finding.
  3. /******************************************************************************/
  4. enum PATH_FIND_PIXEL_FLAG // PathFind Pixel Flags
  5. {
  6. PFP_WALKABLE=0x1, // walkable pixel
  7. PFP_TARGET =0x2, // destination target
  8. PFP_START =0x4, // beginning
  9. };
  10. /******************************************************************************/
  11. struct PathFind // 2D Map Path Finder
  12. {
  13. // manage
  14. PathFind& create(Int w, Int h); // create and set map size to 'w*h'
  15. // get
  16. Int w()C {return _size.x;} // get map width
  17. Int h()C {return _size.y;} // get map height
  18. Bool validPos(Int x, Int y)C {return x<_border.max.x && y<_border.max.y && x>=_border.min.x && y>=_border.min.y;} // if selected position is valid (lies within allowed border)
  19. #if EE_PRIVATE
  20. struct Pixel
  21. {
  22. Byte flag;
  23. Int added_in_step;
  24. UInt length, iteration;
  25. VecI2 xy;
  26. Pixel *src;
  27. void create(Int x, Int y);
  28. };
  29. Pixel& pixel (Int x, Int y) {return _map[x + y*_size.x];}
  30. C Pixel& pixel (Int x, Int y)C {return _map[x + y*_size.x];}
  31. Pixel& pixel (C VecI2 &v ) {return pixel (v.x, v.y);}
  32. C Pixel& pixel (C VecI2 &v )C {return pixel (v.x, v.y);}
  33. Bool validPos(C VecI2 &v ) {return validPos(v.x, v.y);}
  34. Bool onTarget(C VecI2 &v, C VecI2 *target)C {return target ? v==*target : FlagTest(pixel(v).flag, PFP_TARGET);} // !! 'v' is assumed to be 'validPos' !!
  35. void step ();
  36. void zero ();
  37. #endif
  38. // set
  39. UInt pixelFlag (Int x, Int y)C; PathFind& pixelFlag (Int x, Int y, Byte flag); // get/set map pixel flag PATH_FIND_PIXEL_FLAG
  40. Bool pixelWalkable(Int x, Int y)C; PathFind& pixelWalkable(Int x, Int y, Bool on ); // get/set if map pixel is walkable
  41. Bool pixelTarget (Int x, Int y)C; PathFind& pixelTarget (Int x, Int y, Bool on ); // get/set if map pixel is a destination target by marking it with PFP_TARGET flag
  42. Bool pixelStart (Int x, Int y)C; PathFind& pixelStart (Int x, Int y, Bool on ); // get/set if map pixel is a beginning by marking it with PFP_START flag
  43. PathFind& border(Int min_x, Int min_y, Int max_x, Int max_y); // specify borders outside of which moving is forbidden, this allows to specify the walking rectangle area, default=(0, 0, x(), y())
  44. // operations
  45. Bool find(C VecI2 *start, C VecI2 *target, MemPtr<VecI2> path, Int max_steps=-1, Bool diagonal=true, Bool reversed=false); // find path from 'start' to 'target' (if 'start' is null then all pixels with PFP_START are treated as the starting points, if 'target' is null then all pixels with PFP_TARGET are treated as target points), 'max_steps'=maximum allowed steps (-1=unlimited) allows to limit the maximum number of pathfind iterations, 'diagonal'=if allow diagonal movements, 'reversed'=if reverse the path, false on fail
  46. void getWalkableNeighbors(C VecI2 &pos, MemPtr<VecI2> pixels, Bool diagonal=true); // get a list of all neighbor walkable pixels, 'diagonal'=if allow diagonal movements
  47. PathFind& del(); // delete manually
  48. ~PathFind() {del();}
  49. PathFind();
  50. private:
  51. VecI2 _size;
  52. RectI _border;
  53. UInt _iteration;
  54. #if EE_PRIVATE
  55. Pixel *_map;
  56. Memc<Pixel*> _active;
  57. #else
  58. Ptr _map;
  59. Memc<Ptr> _active;
  60. #endif
  61. NO_COPY_CONSTRUCTOR(PathFind);
  62. };
  63. /******************************************************************************/