WaterAdopter.cpp 6.7 KB

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