RmgArea.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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::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 distanceSqr(const int3 & tile) const;
  45. int distanceSqr(const Area & area) const;
  46. int3 nearest(const int3 & tile) const;
  47. int3 nearest(const Area & area) const;
  48. void clear();
  49. void assign(const Tileset tiles); //do not use reference to allow assigment of cached data
  50. void add(const int3 & tile);
  51. void erase(const int3 & tile);
  52. void unite(const Area & area);
  53. void intersect(const Area & area);
  54. void subtract(const Area & area);
  55. void translate(const int3 & shift);
  56. friend Area operator+ (const Area & l, const int3 & r); //translation
  57. friend Area operator- (const Area & l, const int3 & r); //translation
  58. friend Area operator+ (const Area & l, const Area & r); //union
  59. friend Area operator* (const Area & l, const Area & r); //intersection
  60. friend Area operator- (const Area & l, const Area & r); //AreaL reduced by tiles from AreaR
  61. friend bool operator== (const Area & l, const Area & r);
  62. friend std::list<Area> connectedAreas(const Area & area, bool disableDiagonalConnections);
  63. private:
  64. void invalidate();
  65. void computeBorderCache();
  66. void computeBorderOutsideCache();
  67. mutable Tileset dTiles;
  68. mutable std::vector<int3> dTilesVectorCache;
  69. mutable Tileset dBorderCache;
  70. mutable Tileset dBorderOutsideCache;
  71. mutable int3 dTotalShiftCache;
  72. };
  73. }
  74. VCMI_LIB_NAMESPACE_END