RmgArea.h 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /*
  2. * RmgArea.h, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #pragma once
  11. #include "../GameConstants.h"
  12. #include "../int3.h"
  13. namespace rmg
  14. {
  15. static const std::array<int3, 4> dirs4 = { int3(0,1,0),int3(0,-1,0),int3(-1,0,0),int3(+1,0,0) };
  16. static const std::array<int3, 4> dirsDiagonal= { int3(1,1,0),int3(1,-1,0),int3(-1,1,0),int3(-1,-1,0) };
  17. using Tileset = std::set<int3>;
  18. using DistanceMap = std::map<int3, int>;
  19. void toAbsolute(Tileset & tiles, const int3 & position);
  20. void toRelative(Tileset & tiles, const int3 & position);
  21. class Area
  22. {
  23. public:
  24. Area() = default;
  25. Area(const Area &);
  26. Area(const Area &&);
  27. Area(const Tileset & tiles);
  28. Area(const Tileset & relative, const int3 & position); //create from relative positions
  29. Area & operator= (const Area &);
  30. const Tileset & getTiles() const;
  31. const std::vector<int3> & getTilesVector() const;
  32. const Tileset & getBorder() const; //lazy cache invalidation
  33. const Tileset & getBorderOutside() const; //lazy cache invalidation
  34. DistanceMap computeDistanceMap(std::map<int, Tileset> & reverseDistanceMap) const;
  35. Area getSubarea(std::function<bool(const int3 &)> filter) const;
  36. bool connected() const; //is connected
  37. bool empty() const;
  38. bool contains(const int3 & tile) const;
  39. bool contains(const std::vector<int3> & tiles) const;
  40. bool contains(const Area & area) const;
  41. bool overlap(const Area & area) const;
  42. bool overlap(const std::vector<int3> & tiles) const;
  43. int distanceSqr(const int3 & tile) const;
  44. int distanceSqr(const Area & area) const;
  45. int3 nearest(const int3 & tile) const;
  46. int3 nearest(const Area & area) const;
  47. void clear();
  48. void assign(const Tileset tiles); //do not use reference to allow assigment of cached data
  49. void add(const int3 & tile);
  50. void erase(const int3 & tile);
  51. void unite(const Area & area);
  52. void intersect(const Area & area);
  53. void subtract(const Area & area);
  54. void translate(const int3 & shift);
  55. friend Area operator+ (const Area & l, const int3 & r); //translation
  56. friend Area operator- (const Area & l, const int3 & r); //translation
  57. friend Area operator+ (const Area & l, const Area & r); //union
  58. friend Area operator* (const Area & l, const Area & r); //intersection
  59. friend Area operator- (const Area & l, const Area & r); //AreaL reduced by tiles from AreaR
  60. friend bool operator== (const Area & l, const Area & r);
  61. friend std::list<Area> connectedAreas(const Area & area, bool disableDiagonalConnections);
  62. private:
  63. void invalidate();
  64. void computeBorderCache();
  65. void computeBorderOutsideCache();
  66. mutable Tileset dTiles;
  67. mutable std::vector<int3> dTilesVectorCache;
  68. mutable Tileset dBorderCache;
  69. mutable Tileset dBorderOutsideCache;
  70. mutable int3 dTotalShiftCache;
  71. };
  72. }