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