RmgArea.h 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. VCMI_LIB_NAMESPACE_BEGIN
  14. namespace rmg
  15. {
  16. static const std::array<int3, 4> dirs4 = { int3(0,1,0),int3(0,-1,0),int3(-1,0,0),int3(+1,0,0) };
  17. static const std::array<int3, 4> dirsDiagonal= { int3(1,1,0),int3(1,-1,0),int3(-1,1,0),int3(-1,-1,0) };
  18. using Tileset = std::unordered_set<int3>;
  19. using DistanceMap = std::map<int3, int>;
  20. void toAbsolute(Tileset & tiles, const int3 & position);
  21. void toRelative(Tileset & tiles, const int3 & position);
  22. class DLL_LINKAGE Area
  23. {
  24. public:
  25. Area() = default;
  26. Area(const Area &);
  27. Area(Area &&) noexcept;
  28. Area(Tileset tiles);
  29. Area(Tileset relative, const int3 & position); //create from relative positions
  30. Area & operator= (const Area &);
  31. const Tileset & getTiles() const;
  32. const std::vector<int3> & getTilesVector() const;
  33. const Tileset & getBorder() const; //lazy cache invalidation
  34. const Tileset & getBorderOutside() const; //lazy cache invalidation
  35. DistanceMap computeDistanceMap(std::map<int, Tileset> & reverseDistanceMap) const;
  36. Area getSubarea(const std::function<bool(const int3 &)> & filter) const;
  37. bool connected(bool noDiagonals = false) const; //is connected
  38. bool empty() const;
  39. bool contains(const int3 & tile) const;
  40. bool contains(const std::vector<int3> & tiles) const;
  41. bool contains(const Area & area) const;
  42. bool overlap(const Area & area) const;
  43. bool overlap(const std::vector<int3> & tiles) const;
  44. int distance(const int3 & tile) const;
  45. int distanceSqr(const int3 & tile) const;
  46. int distanceSqr(const Area & area) const;
  47. int3 nearest(const int3 & tile) const;
  48. int3 nearest(const Area & area) const;
  49. void clear();
  50. void assign(const Tileset tiles); //do not use reference to allow assigment of cached data
  51. void add(const int3 & tile);
  52. void erase(const int3 & tile);
  53. void unite(const Area & area);
  54. void intersect(const Area & area);
  55. void subtract(const Area & area);
  56. void translate(const int3 & shift);
  57. void erase_if(std::function<bool(const int3&)> predicate);
  58. friend Area operator+ (const Area & l, const int3 & r); //translation
  59. friend Area operator- (const Area & l, const int3 & r); //translation
  60. friend Area operator+ (const Area & l, const Area & r); //union
  61. friend Area operator* (const Area & l, const Area & r); //intersection
  62. friend Area operator- (const Area & l, const Area & r); //AreaL reduced by tiles from AreaR
  63. friend bool operator== (const Area & l, const Area & r);
  64. friend std::list<Area> connectedAreas(const Area & area, bool disableDiagonalConnections);
  65. private:
  66. void invalidate();
  67. void computeBorderCache();
  68. void computeBorderOutsideCache();
  69. mutable Tileset dTiles;
  70. mutable std::vector<int3> dTilesVectorCache;
  71. mutable Tileset dBorderCache;
  72. mutable Tileset dBorderOutsideCache;
  73. mutable int3 dTotalShiftCache;
  74. };
  75. }
  76. VCMI_LIB_NAMESPACE_END