2022-08-09 07:54:32 +02:00
|
|
|
/*
|
|
|
|
* RmgArea.h, part of VCMI engine
|
|
|
|
*
|
|
|
|
* Authors: listed in file AUTHORS in main folder
|
|
|
|
*
|
|
|
|
* License: GNU General Public License v2.0 or later
|
|
|
|
* Full text of license available in license.txt file, in main folder
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include "../GameConstants.h"
|
|
|
|
#include "../int3.h"
|
|
|
|
|
2022-07-26 15:07:42 +02:00
|
|
|
VCMI_LIB_NAMESPACE_BEGIN
|
|
|
|
|
2022-08-09 07:54:32 +02:00
|
|
|
namespace rmg
|
|
|
|
{
|
2022-08-28 10:54:06 +02:00
|
|
|
static const std::array<int3, 4> dirs4 = { int3(0,1,0),int3(0,-1,0),int3(-1,0,0),int3(+1,0,0) };
|
|
|
|
static const std::array<int3, 4> dirsDiagonal= { int3(1,1,0),int3(1,-1,0),int3(-1,1,0),int3(-1,-1,0) };
|
|
|
|
|
2023-12-18 14:52:03 +02:00
|
|
|
using Tileset = std::unordered_set<int3>;
|
2022-08-09 07:54:32 +02:00
|
|
|
using DistanceMap = std::map<int3, int>;
|
|
|
|
void toAbsolute(Tileset & tiles, const int3 & position);
|
|
|
|
void toRelative(Tileset & tiles, const int3 & position);
|
|
|
|
|
2022-09-17 13:04:01 +02:00
|
|
|
class DLL_LINKAGE Area
|
2022-08-09 07:54:32 +02:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
Area() = default;
|
|
|
|
Area(const Area &);
|
2023-02-11 18:05:02 +02:00
|
|
|
Area(Area &&) noexcept;
|
|
|
|
Area(Tileset tiles);
|
|
|
|
Area(Tileset relative, const int3 & position); //create from relative positions
|
2022-08-09 07:54:32 +02:00
|
|
|
Area & operator= (const Area &);
|
|
|
|
|
|
|
|
const Tileset & getTiles() const;
|
|
|
|
const std::vector<int3> & getTilesVector() const;
|
|
|
|
const Tileset & getBorder() const; //lazy cache invalidation
|
|
|
|
const Tileset & getBorderOutside() const; //lazy cache invalidation
|
|
|
|
|
|
|
|
DistanceMap computeDistanceMap(std::map<int, Tileset> & reverseDistanceMap) const;
|
2023-02-11 18:05:02 +02:00
|
|
|
|
|
|
|
Area getSubarea(const std::function<bool(const int3 &)> & filter) const;
|
|
|
|
|
2023-06-10 14:56:03 +02:00
|
|
|
bool connected(bool noDiagonals = false) const; //is connected
|
2022-08-09 07:54:32 +02:00
|
|
|
bool empty() const;
|
|
|
|
bool contains(const int3 & tile) const;
|
|
|
|
bool contains(const std::vector<int3> & tiles) const;
|
|
|
|
bool contains(const Area & area) const;
|
|
|
|
bool overlap(const Area & area) const;
|
|
|
|
bool overlap(const std::vector<int3> & tiles) const;
|
2024-05-10 15:35:33 +02:00
|
|
|
int distance(const int3 & tile) const;
|
2022-08-09 07:54:32 +02:00
|
|
|
int distanceSqr(const int3 & tile) const;
|
|
|
|
int distanceSqr(const Area & area) const;
|
|
|
|
int3 nearest(const int3 & tile) const;
|
|
|
|
int3 nearest(const Area & area) const;
|
|
|
|
|
|
|
|
void clear();
|
|
|
|
void assign(const Tileset tiles); //do not use reference to allow assigment of cached data
|
|
|
|
void add(const int3 & tile);
|
|
|
|
void erase(const int3 & tile);
|
|
|
|
void unite(const Area & area);
|
|
|
|
void intersect(const Area & area);
|
|
|
|
void subtract(const Area & area);
|
|
|
|
void translate(const int3 & shift);
|
2023-08-06 21:14:16 +02:00
|
|
|
void erase_if(std::function<bool(const int3&)> predicate);
|
2022-08-09 07:54:32 +02:00
|
|
|
|
|
|
|
friend Area operator+ (const Area & l, const int3 & r); //translation
|
|
|
|
friend Area operator- (const Area & l, const int3 & r); //translation
|
|
|
|
friend Area operator+ (const Area & l, const Area & r); //union
|
|
|
|
friend Area operator* (const Area & l, const Area & r); //intersection
|
|
|
|
friend Area operator- (const Area & l, const Area & r); //AreaL reduced by tiles from AreaR
|
|
|
|
friend bool operator== (const Area & l, const Area & r);
|
2022-08-28 10:54:06 +02:00
|
|
|
friend std::list<Area> connectedAreas(const Area & area, bool disableDiagonalConnections);
|
2022-08-09 07:54:32 +02:00
|
|
|
|
|
|
|
private:
|
|
|
|
|
|
|
|
void invalidate();
|
|
|
|
void computeBorderCache();
|
|
|
|
void computeBorderOutsideCache();
|
|
|
|
|
|
|
|
mutable Tileset dTiles;
|
|
|
|
mutable std::vector<int3> dTilesVectorCache;
|
|
|
|
mutable Tileset dBorderCache;
|
|
|
|
mutable Tileset dBorderOutsideCache;
|
|
|
|
mutable int3 dTotalShiftCache;
|
|
|
|
};
|
|
|
|
}
|
2022-07-26 15:07:42 +02:00
|
|
|
|
|
|
|
VCMI_LIB_NAMESPACE_END
|