2022-08-09 07:54:32 +02:00
|
|
|
/*
|
|
|
|
* RmgArea.cpp, 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
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "StdInc.h"
|
|
|
|
#include "RmgArea.h"
|
|
|
|
#include "CMapGenerator.h"
|
|
|
|
|
2022-07-26 15:07:42 +02:00
|
|
|
VCMI_LIB_NAMESPACE_BEGIN
|
|
|
|
|
2022-08-09 07:54:32 +02:00
|
|
|
namespace rmg
|
|
|
|
{
|
|
|
|
|
|
|
|
void toAbsolute(Tileset & tiles, const int3 & position)
|
|
|
|
{
|
|
|
|
Tileset temp;
|
2023-02-11 18:05:02 +02:00
|
|
|
for(const auto & tile : tiles)
|
2022-08-09 07:54:32 +02:00
|
|
|
{
|
|
|
|
temp.insert(tile + position);
|
|
|
|
}
|
|
|
|
tiles = std::move(temp);
|
|
|
|
}
|
|
|
|
|
|
|
|
void toRelative(Tileset & tiles, const int3 & position)
|
|
|
|
{
|
|
|
|
toAbsolute(tiles, -position);
|
|
|
|
}
|
|
|
|
|
|
|
|
Area::Area(const Area & area): dTiles(area.dTiles), dTotalShiftCache(area.dTotalShiftCache)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2023-02-11 18:05:02 +02:00
|
|
|
Area::Area(Area && area) noexcept: dTiles(std::move(area.dTiles)), dTotalShiftCache(area.dTotalShiftCache)
|
2022-08-09 07:54:32 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
Area & Area::operator=(const Area & area)
|
|
|
|
{
|
|
|
|
clear();
|
|
|
|
dTiles = area.dTiles;
|
|
|
|
dTotalShiftCache = area.dTotalShiftCache;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2023-02-11 18:05:02 +02:00
|
|
|
Area::Area(Tileset tiles): dTiles(std::move(tiles))
|
2022-08-09 07:54:32 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2023-02-11 18:05:02 +02:00
|
|
|
Area::Area(Tileset relative, const int3 & position): dTiles(std::move(relative)), dTotalShiftCache(position)
|
2022-08-09 07:54:32 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void Area::invalidate()
|
|
|
|
{
|
|
|
|
getTiles();
|
|
|
|
dTilesVectorCache.clear();
|
|
|
|
dBorderCache.clear();
|
|
|
|
dBorderOutsideCache.clear();
|
|
|
|
}
|
|
|
|
|
2023-06-10 14:56:03 +02:00
|
|
|
bool Area::connected(bool noDiagonals) const
|
2022-08-09 07:54:32 +02:00
|
|
|
{
|
|
|
|
std::list<int3> queue({*dTiles.begin()});
|
|
|
|
Tileset connected = dTiles; //use invalidated cache - ok
|
2023-06-10 14:56:03 +02:00
|
|
|
|
2022-08-09 07:54:32 +02:00
|
|
|
while(!queue.empty())
|
|
|
|
{
|
|
|
|
auto t = queue.front();
|
|
|
|
connected.erase(t);
|
|
|
|
queue.pop_front();
|
|
|
|
|
2023-06-10 14:56:03 +02:00
|
|
|
if (noDiagonals)
|
|
|
|
{
|
|
|
|
for (auto& i : dirs4)
|
|
|
|
{
|
|
|
|
if (connected.count(t + i))
|
|
|
|
{
|
|
|
|
queue.push_back(t + i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
2022-08-09 07:54:32 +02:00
|
|
|
{
|
2023-06-10 14:56:03 +02:00
|
|
|
for (auto& i : int3::getDirs())
|
2022-08-09 07:54:32 +02:00
|
|
|
{
|
2023-06-10 14:56:03 +02:00
|
|
|
if (connected.count(t + i))
|
|
|
|
{
|
|
|
|
queue.push_back(t + i);
|
|
|
|
}
|
2022-08-09 07:54:32 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return connected.empty();
|
|
|
|
}
|
|
|
|
|
2022-08-28 10:54:06 +02:00
|
|
|
std::list<Area> connectedAreas(const Area & area, bool disableDiagonalConnections)
|
2022-08-09 07:54:32 +02:00
|
|
|
{
|
2022-08-28 10:54:06 +02:00
|
|
|
auto allDirs = int3::getDirs();
|
|
|
|
std::vector<int3> dirs(allDirs.begin(), allDirs.end());
|
|
|
|
if(disableDiagonalConnections)
|
|
|
|
dirs.assign(rmg::dirs4.begin(), rmg::dirs4.end());
|
|
|
|
|
2022-08-09 07:54:32 +02:00
|
|
|
std::list<Area> result;
|
|
|
|
Tileset connected = area.getTiles();
|
|
|
|
while(!connected.empty())
|
|
|
|
{
|
|
|
|
result.emplace_back();
|
|
|
|
std::list<int3> queue({*connected.begin()});
|
|
|
|
std::set<int3> queueSet({*connected.begin()});
|
|
|
|
while(!queue.empty())
|
|
|
|
{
|
|
|
|
auto t = queue.front();
|
|
|
|
connected.erase(t);
|
|
|
|
result.back().add(t);
|
|
|
|
queue.pop_front();
|
|
|
|
|
2022-08-28 10:54:06 +02:00
|
|
|
for(auto & i : dirs)
|
2022-08-09 07:54:32 +02:00
|
|
|
{
|
|
|
|
auto tile = t + i;
|
|
|
|
if(!queueSet.count(tile) && connected.count(tile) && !result.back().contains(tile))
|
|
|
|
{
|
|
|
|
queueSet.insert(tile);
|
|
|
|
queue.push_back(tile);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
const Tileset & Area::getTiles() const
|
|
|
|
{
|
|
|
|
if(dTotalShiftCache != int3())
|
|
|
|
{
|
|
|
|
toAbsolute(dTiles, dTotalShiftCache);
|
|
|
|
dTotalShiftCache = int3();
|
|
|
|
}
|
|
|
|
return dTiles;
|
|
|
|
}
|
|
|
|
|
|
|
|
const std::vector<int3> & Area::getTilesVector() const
|
|
|
|
{
|
|
|
|
if(dTilesVectorCache.empty())
|
|
|
|
{
|
|
|
|
getTiles();
|
|
|
|
dTilesVectorCache.assign(dTiles.begin(), dTiles.end());
|
|
|
|
}
|
|
|
|
return dTilesVectorCache;
|
|
|
|
}
|
|
|
|
|
|
|
|
const Tileset & Area::getBorder() const
|
|
|
|
{
|
|
|
|
if(!dBorderCache.empty())
|
|
|
|
return dBorderCache;
|
|
|
|
|
|
|
|
//compute border cache
|
2023-02-11 18:05:02 +02:00
|
|
|
for(const auto & t : dTiles)
|
2022-08-09 07:54:32 +02:00
|
|
|
{
|
|
|
|
for(auto & i : int3::getDirs())
|
|
|
|
{
|
|
|
|
if(!dTiles.count(t + i))
|
|
|
|
{
|
|
|
|
dBorderCache.insert(t + dTotalShiftCache);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return dBorderCache;
|
|
|
|
}
|
|
|
|
|
|
|
|
const Tileset & Area::getBorderOutside() const
|
|
|
|
{
|
|
|
|
if(!dBorderOutsideCache.empty())
|
|
|
|
return dBorderOutsideCache;
|
|
|
|
|
|
|
|
//compute outside border cache
|
2023-02-11 18:05:02 +02:00
|
|
|
for(const auto & t : dTiles)
|
2022-08-09 07:54:32 +02:00
|
|
|
{
|
|
|
|
for(auto & i : int3::getDirs())
|
|
|
|
{
|
|
|
|
if(!dTiles.count(t + i))
|
|
|
|
dBorderOutsideCache.insert(t + i + dTotalShiftCache);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return dBorderOutsideCache;
|
|
|
|
}
|
|
|
|
|
|
|
|
DistanceMap Area::computeDistanceMap(std::map<int, Tileset> & reverseDistanceMap) const
|
|
|
|
{
|
|
|
|
reverseDistanceMap.clear();
|
|
|
|
DistanceMap result;
|
|
|
|
auto area = *this;
|
|
|
|
int distance = 0;
|
|
|
|
|
|
|
|
while(!area.empty())
|
|
|
|
{
|
2023-02-11 18:05:02 +02:00
|
|
|
for(const auto & tile : area.getBorder())
|
2022-08-09 07:54:32 +02:00
|
|
|
result[tile] = distance;
|
|
|
|
reverseDistanceMap[distance++] = area.getBorder();
|
|
|
|
area.subtract(area.getBorder());
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Area::empty() const
|
|
|
|
{
|
|
|
|
return dTiles.empty();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Area::contains(const int3 & tile) const
|
|
|
|
{
|
|
|
|
return dTiles.count(tile - dTotalShiftCache);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Area::contains(const std::vector<int3> & tiles) const
|
|
|
|
{
|
2023-02-11 18:05:02 +02:00
|
|
|
for(const auto & t : tiles)
|
2022-08-09 07:54:32 +02:00
|
|
|
{
|
|
|
|
if(!contains(t))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Area::contains(const Area & area) const
|
|
|
|
{
|
|
|
|
return contains(area.getTilesVector());
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Area::overlap(const std::vector<int3> & tiles) const
|
|
|
|
{
|
2023-02-11 18:05:02 +02:00
|
|
|
for(const auto & t : tiles)
|
2022-08-09 07:54:32 +02:00
|
|
|
{
|
|
|
|
if(contains(t))
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Area::overlap(const Area & area) const
|
|
|
|
{
|
|
|
|
return overlap(area.getTilesVector());
|
|
|
|
}
|
|
|
|
|
|
|
|
int Area::distanceSqr(const int3 & tile) const
|
|
|
|
{
|
|
|
|
return nearest(tile).dist2dSQ(tile);
|
|
|
|
}
|
|
|
|
|
|
|
|
int Area::distanceSqr(const Area & area) const
|
|
|
|
{
|
|
|
|
int dist = std::numeric_limits<int>::max();
|
|
|
|
int3 nearTile = *getTilesVector().begin();
|
|
|
|
int3 otherNearTile = area.nearest(nearTile);
|
|
|
|
|
|
|
|
while(dist != otherNearTile.dist2dSQ(nearTile))
|
|
|
|
{
|
|
|
|
dist = otherNearTile.dist2dSQ(nearTile);
|
|
|
|
nearTile = nearest(otherNearTile);
|
|
|
|
otherNearTile = area.nearest(nearTile);
|
|
|
|
}
|
|
|
|
|
|
|
|
return dist;
|
|
|
|
}
|
|
|
|
|
|
|
|
int3 Area::nearest(const int3 & tile) const
|
|
|
|
{
|
|
|
|
return findClosestTile(getTilesVector(), tile);
|
|
|
|
}
|
|
|
|
|
|
|
|
int3 Area::nearest(const Area & area) const
|
|
|
|
{
|
|
|
|
int dist = std::numeric_limits<int>::max();
|
|
|
|
int3 nearTile = *getTilesVector().begin();
|
|
|
|
int3 otherNearTile = area.nearest(nearTile);
|
|
|
|
|
|
|
|
while(dist != otherNearTile.dist2dSQ(nearTile))
|
|
|
|
{
|
|
|
|
dist = otherNearTile.dist2dSQ(nearTile);
|
|
|
|
nearTile = nearest(otherNearTile);
|
|
|
|
otherNearTile = area.nearest(nearTile);
|
|
|
|
}
|
|
|
|
|
|
|
|
return nearTile;
|
|
|
|
}
|
|
|
|
|
2023-02-11 18:05:02 +02:00
|
|
|
Area Area::getSubarea(const std::function<bool(const int3 &)> & filter) const
|
2022-08-09 07:54:32 +02:00
|
|
|
{
|
|
|
|
Area subset;
|
2023-02-11 18:05:02 +02:00
|
|
|
for(const auto & t : getTilesVector())
|
2022-08-09 07:54:32 +02:00
|
|
|
if(filter(t))
|
|
|
|
subset.add(t);
|
|
|
|
return subset;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Area::clear()
|
|
|
|
{
|
|
|
|
dTiles.clear();
|
|
|
|
dTotalShiftCache = int3();
|
|
|
|
invalidate();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Area::assign(const Tileset tiles)
|
|
|
|
{
|
|
|
|
clear();
|
|
|
|
dTiles = tiles;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Area::add(const int3 & tile)
|
|
|
|
{
|
|
|
|
invalidate();
|
|
|
|
dTiles.insert(tile);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Area::erase(const int3 & tile)
|
|
|
|
{
|
|
|
|
invalidate();
|
|
|
|
dTiles.erase(tile);
|
|
|
|
}
|
|
|
|
void Area::unite(const Area & area)
|
|
|
|
{
|
|
|
|
invalidate();
|
2023-02-11 18:05:02 +02:00
|
|
|
for(const auto & t : area.getTilesVector())
|
2022-08-09 07:54:32 +02:00
|
|
|
{
|
|
|
|
dTiles.insert(t);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
void Area::intersect(const Area & area)
|
|
|
|
{
|
|
|
|
invalidate();
|
|
|
|
Tileset result;
|
2023-02-11 18:05:02 +02:00
|
|
|
for(const auto & t : area.getTilesVector())
|
2022-08-09 07:54:32 +02:00
|
|
|
{
|
|
|
|
if(dTiles.count(t))
|
|
|
|
result.insert(t);
|
|
|
|
}
|
|
|
|
dTiles = result;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Area::subtract(const Area & area)
|
|
|
|
{
|
|
|
|
invalidate();
|
2023-02-11 18:05:02 +02:00
|
|
|
for(const auto & t : area.getTilesVector())
|
2022-08-09 07:54:32 +02:00
|
|
|
{
|
|
|
|
dTiles.erase(t);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Area::translate(const int3 & shift)
|
|
|
|
{
|
|
|
|
dBorderCache.clear();
|
|
|
|
dBorderOutsideCache.clear();
|
|
|
|
|
|
|
|
if(dTilesVectorCache.empty())
|
|
|
|
{
|
|
|
|
getTiles();
|
|
|
|
getTilesVector();
|
|
|
|
}
|
|
|
|
|
|
|
|
//avoid recomputation within std::set, use vector instead
|
|
|
|
dTotalShiftCache += shift;
|
|
|
|
|
|
|
|
for(auto & t : dTilesVectorCache)
|
|
|
|
{
|
|
|
|
t += shift;
|
|
|
|
}
|
|
|
|
//toAbsolute(dTiles, shift);
|
|
|
|
}
|
|
|
|
|
|
|
|
Area operator- (const Area & l, const int3 & r)
|
|
|
|
{
|
|
|
|
Area result(l);
|
|
|
|
result.translate(-r);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
Area operator+ (const Area & l, const int3 & r)
|
|
|
|
{
|
|
|
|
Area result(l);
|
|
|
|
result.translate(r);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
Area operator+ (const Area & l, const Area & r)
|
|
|
|
{
|
|
|
|
Area result(l);
|
|
|
|
result.unite(r);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
Area operator- (const Area & l, const Area & r)
|
|
|
|
{
|
|
|
|
Area result(l);
|
|
|
|
result.subtract(r);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
Area operator* (const Area & l, const Area & r)
|
|
|
|
{
|
|
|
|
Area result(l);
|
|
|
|
result.intersect(r);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator== (const Area & l, const Area & r)
|
|
|
|
{
|
|
|
|
return l.getTiles() == r.getTiles();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2022-07-26 15:07:42 +02:00
|
|
|
|
|
|
|
VCMI_LIB_NAMESPACE_END
|