1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
|
/*
* WaterAdopter.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 "WaterAdopter.h"
#include "CMapGenerator.h"
#include "RmgMap.h"
#include "../mapping/CMap.h"
#include "../mapping/CMapEditManager.h"
#include "../mapObjects/CObjectClassesHandler.h"
#include "RmgPath.h"
#include "RmgObject.h"
#include "ObjectManager.h"
#include "Functions.h"
#include "RoadPlacer.h"
#include "TreasurePlacer.h"
#include "TownPlacer.h"
#include "ConnectionsPlacer.h"
#include "TileInfo.h"
VCMI_LIB_NAMESPACE_BEGIN
void WaterAdopter::process()
{
createWater(map.getMapGenOptions().getWaterContent());
}
void WaterAdopter::init()
{
//make dependencies
DEPENDENCY_ALL(WaterAdopter);
DEPENDENCY(TownPlacer);
POSTFUNCTION(ConnectionsPlacer);
POSTFUNCTION(TreasurePlacer);
}
void WaterAdopter::createWater(EWaterContent::EWaterContent waterContent)
{
if(waterContent == EWaterContent::NONE || zone.isUnderground() || zone.getType() == ETemplateZoneType::WATER)
return; //do nothing
distanceMap = zone.area().computeDistanceMap(reverseDistanceMap);
//add border tiles as water for ISLANDS
if(waterContent == EWaterContent::ISLANDS)
{
waterArea.unite(collectDistantTiles(zone, zone.getSize() + 1));
waterArea.unite(zone.area().getBorder());
}
//protect some parts from water for NORMAL
if(waterContent == EWaterContent::NORMAL)
{
waterArea.unite(collectDistantTiles(zone, zone.getSize() - 1));
auto sliceStart = RandomGeneratorUtil::nextItem(reverseDistanceMap[0], generator.rand);
auto sliceEnd = RandomGeneratorUtil::nextItem(reverseDistanceMap[0], generator.rand);
//at least 25% without water
bool endPassed = false;
for(int counter = 0; counter < reverseDistanceMap[0].size() / 4 || !endPassed; ++sliceStart, ++counter)
{
if(sliceStart == reverseDistanceMap[0].end())
sliceStart = reverseDistanceMap[0].begin();
if(sliceStart == sliceEnd)
endPassed = true;
noWaterArea.add(*sliceStart);
}
rmg::Area noWaterSlice;
for(int i = 1; i < reverseDistanceMap.size(); ++i)
{
for(auto & t : reverseDistanceMap[i])
{
if(noWaterArea.distanceSqr(t) < 3)
noWaterSlice.add(t);
}
noWaterArea.unite(noWaterSlice);
}
}
//generating some irregularity of coast
int coastIdMax = sqrt(reverseDistanceMap.size()); //size of coastTilesMap shows the most distant tile from water
assert(coastIdMax > 0);
std::list<int3> tilesQueue;
rmg::Tileset tilesChecked;
for(int coastId = coastIdMax; coastId >= 0; --coastId)
{
//amount of iterations shall be proportion of coast perimeter
const int coastLength = reverseDistanceMap[coastId].size() / (coastId + 3);
for(int coastIter = 0; coastIter < coastLength; ++coastIter)
{
int3 tile = *RandomGeneratorUtil::nextItem(reverseDistanceMap[coastId], generator.rand);
if(tilesChecked.find(tile) != tilesChecked.end())
continue;
if(map.isUsed(tile) || map.isFree(tile)) //prevent placing water nearby town
continue;
tilesQueue.push_back(tile);
tilesChecked.insert(tile);
}
}
//if tile is marked as water - connect it with "big" water
while(!tilesQueue.empty())
{
int3 src = tilesQueue.front();
tilesQueue.pop_front();
if(waterArea.contains(src))
continue;
waterArea.add(src);
map.foreach_neighbour(src, [&src, this, &tilesChecked, &tilesQueue](const int3 & dst)
{
if(tilesChecked.count(dst))
return;
if(distanceMap[dst] >= 0 && distanceMap[src] - distanceMap[dst] == 1)
{
tilesQueue.push_back(dst);
tilesChecked.insert(dst);
}
});
}
waterArea.subtract(noWaterArea);
//start filtering of narrow places and coast atrifacts
rmg::Area waterAdd;
for(int coastId = 1; coastId <= coastIdMax; ++coastId)
{
for(auto& tile : reverseDistanceMap[coastId])
{
//collect neighbout water tiles
auto collectionLambda = [this](const int3 & t, std::set<int3> & outCollection)
{
if(waterArea.contains(t))
{
reverseDistanceMap[0].insert(t);
outCollection.insert(t);
}
};
std::set<int3> waterCoastDirect, waterCoastDiag;
map.foreachDirectNeighbour(tile, std::bind(collectionLambda, std::placeholders::_1, std::ref(waterCoastDirect)));
map.foreachDiagonalNeighbour(tile, std::bind(collectionLambda, std::placeholders::_1, std::ref(waterCoastDiag)));
int waterCoastDirectNum = waterCoastDirect.size();
int waterCoastDiagNum = waterCoastDiag.size();
//remove tiles which are mostly covered by water
if(waterCoastDirectNum >= 3)
{
waterAdd.add(tile);
continue;
}
if(waterCoastDiagNum == 4 && waterCoastDirectNum == 2)
{
waterAdd.add(tile);
continue;
}
if(waterCoastDirectNum == 2 && waterCoastDiagNum >= 2)
{
int3 diagSum, dirSum;
for(auto & i : waterCoastDiag)
diagSum += i - tile;
for(auto & i : waterCoastDirect)
dirSum += i - tile;
if(diagSum == int3() || dirSum == int3())
{
waterAdd.add(tile);
continue;
}
if(waterCoastDiagNum == 3 && diagSum != dirSum)
{
waterAdd.add(tile);
continue;
}
}
}
}
waterArea.unite(waterAdd);
//filtering tiny "lakes"
for(auto& tile : reverseDistanceMap[0]) //now it's only coast-water tiles
{
if(!waterArea.contains(tile)) //for ground tiles
continue;
std::vector<int3> groundCoast;
map.foreachDirectNeighbour(tile, [this, &groundCoast](const int3 & t)
{
if(!waterArea.contains(t) && zone.area().contains(t)) //for ground tiles of same zone
{
groundCoast.push_back(t);
}
});
if(groundCoast.size() >= 3)
{
waterArea.erase(tile);
}
else
{
if(groundCoast.size() == 2)
{
if(groundCoast[0] + groundCoast[1] == int3())
{
waterArea.erase(tile);
}
}
}
}
map.getZones()[waterZoneId]->area().unite(waterArea);
zone.area().subtract(waterArea);
zone.areaPossible().subtract(waterArea);
distanceMap = zone.area().computeDistanceMap(reverseDistanceMap);
}
void WaterAdopter::setWaterZone(TRmgTemplateZoneId water)
{
waterZoneId = water;
}
rmg::Area WaterAdopter::getCoastTiles() const
{
if(reverseDistanceMap.empty())
return rmg::Area();
return rmg::Area(reverseDistanceMap.at(0));
}
char WaterAdopter::dump(const int3 & t)
{
if(noWaterArea.contains(t))
return 'X';
if(waterArea.contains(t))
return '~';
auto distanceMapIter = distanceMap.find(t);
if(distanceMapIter != distanceMap.end())
{
if(distanceMapIter->second > 9)
return '%';
auto distStr = std::to_string(distanceMapIter->second);
if(distStr.length() > 0)
return distStr[0];
}
return Modificator::dump(t);
}
VCMI_LIB_NAMESPACE_END
|