WaterAdopter.cpp 6.7 KB

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