RmgArea.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. int3 getCenterOfMass() const;
  37. Area getSubarea(const std::function<bool(const int3 &)> & filter) const;
  38. bool connected(bool noDiagonals = false) const; //is connected
  39. bool empty() const;
  40. bool contains(const int3 & tile) const;
  41. bool contains(const std::vector<int3> & tiles) const;
  42. bool contains(const Area & area) const;
  43. bool overlap(const Area & area) const;
  44. bool overlap(const std::vector<int3> & tiles) const;
  45. int distance(const int3 & tile) const;
  46. int distanceSqr(const int3 & tile) const;
  47. int distanceSqr(const Area & area) const;
  48. int3 nearest(const int3 & tile) const;
  49. int3 nearest(const Area & area) const;
  50. void clear();
  51. void assign(const Tileset tiles); //do not use reference to allow assignment of cached data
  52. void add(const int3 & tile);
  53. void erase(const int3 & tile);
  54. void unite(const Area & area);
  55. void intersect(const Area & area);
  56. void subtract(const Area & area);
  57. void translate(const int3 & shift);
  58. void erase_if(std::function<bool(const int3&)> predicate);
  59. friend Area operator+ (const Area & l, const int3 & r); //translation
  60. friend Area operator- (const Area & l, const int3 & r); //translation
  61. friend Area operator+ (const Area & l, const Area & r); //union
  62. friend Area operator* (const Area & l, const Area & r); //intersection
  63. friend Area operator- (const Area & l, const Area & r); //AreaL reduced by tiles from AreaR
  64. friend bool operator== (const Area & l, const Area & r);
  65. friend std::list<Area> connectedAreas(const Area & area, bool disableDiagonalConnections);
  66. private:
  67. void invalidate();
  68. void computeBorderCache();
  69. void computeBorderOutsideCache();
  70. mutable Tileset dTiles;
  71. mutable std::vector<int3> dTilesVectorCache;
  72. mutable Tileset dBorderCache;
  73. mutable Tileset dBorderOutsideCache;
  74. mutable int3 dTotalShiftCache;
  75. };
  76. }
  77. VCMI_LIB_NAMESPACE_END