Zone.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. /*
  2. * Zone.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 "Zone.h"
  12. #include "RmgMap.h"
  13. #include "Functions.h"
  14. #include "TileInfo.h"
  15. #include "CMapGenerator.h"
  16. #include "RmgPath.h"
  17. VCMI_LIB_NAMESPACE_BEGIN
  18. std::function<bool(const int3 &)> AREA_NO_FILTER = [](const int3 & t)
  19. {
  20. return true;
  21. };
  22. Zone::Zone(RmgMap & map, CMapGenerator & generator, CRandomGenerator & r)
  23. : finished(false)
  24. , townType(ETownType::NEUTRAL)
  25. , terrainType(ETerrainId::GRASS)
  26. , map(map)
  27. , generator(generator)
  28. {
  29. rand.setSeed(r.nextInt());
  30. }
  31. bool Zone::isUnderground() const
  32. {
  33. return getPos().z;
  34. }
  35. void Zone::setOptions(const ZoneOptions& options)
  36. {
  37. ZoneOptions::operator=(options);
  38. }
  39. float3 Zone::getCenter() const
  40. {
  41. return center;
  42. }
  43. void Zone::setCenter(const float3 &f)
  44. {
  45. //limit boundaries to (0,1) square
  46. //alternate solution - wrap zone around unitary square. If it doesn't fit on one side, will come out on the opposite side
  47. center = f;
  48. center.x = static_cast<float>(std::fmod(center.x, 1));
  49. center.y = static_cast<float>(std::fmod(center.y, 1));
  50. if(center.x < 0) //fmod seems to work only for positive numbers? we want to stay positive
  51. center.x = 1 - std::abs(center.x);
  52. if(center.y < 0)
  53. center.y = 1 - std::abs(center.y);
  54. }
  55. int3 Zone::getPos() const
  56. {
  57. return pos;
  58. }
  59. void Zone::setPos(const int3 &Pos)
  60. {
  61. pos = Pos;
  62. }
  63. const rmg::Area & Zone::getArea() const
  64. {
  65. return dArea;
  66. }
  67. rmg::Area & Zone::area()
  68. {
  69. return dArea;
  70. }
  71. rmg::Area & Zone::areaPossible()
  72. {
  73. //FIXME: make const, only modify via mutex-protected interface
  74. return dAreaPossible;
  75. }
  76. rmg::Area & Zone::areaUsed()
  77. {
  78. return dAreaUsed;
  79. }
  80. void Zone::clearTiles()
  81. {
  82. //Lock lock(mx);
  83. dArea.clear();
  84. dAreaPossible.clear();
  85. dAreaFree.clear();
  86. }
  87. void Zone::initFreeTiles()
  88. {
  89. rmg::Tileset possibleTiles;
  90. //Lock lock(mx);
  91. vstd::copy_if(dArea.getTiles(), vstd::set_inserter(possibleTiles), [this](const int3 &tile) -> bool
  92. {
  93. return map.isPossible(tile);
  94. });
  95. dAreaPossible.assign(possibleTiles);
  96. if(dAreaFree.empty())
  97. {
  98. dAreaPossible.erase(pos);
  99. dAreaFree.add(pos); //zone must have at least one free tile where other paths go - for instance in the center
  100. }
  101. }
  102. rmg::Area & Zone::freePaths()
  103. {
  104. return dAreaFree;
  105. }
  106. FactionID Zone::getTownType() const
  107. {
  108. return FactionID(townType);
  109. }
  110. void Zone::setTownType(si32 town)
  111. {
  112. townType = town;
  113. }
  114. TerrainId Zone::getTerrainType() const
  115. {
  116. return terrainType;
  117. }
  118. void Zone::setTerrainType(TerrainId terrain)
  119. {
  120. terrainType = terrain;
  121. }
  122. rmg::Path Zone::searchPath(const rmg::Area & src, bool onlyStraight, const std::function<bool(const int3 &)> & areafilter) const
  123. ///connect current tile to any other free tile within zone
  124. {
  125. auto movementCost = [this](const int3 & s, const int3 & d)
  126. {
  127. if(map.isFree(d))
  128. return 1;
  129. else if (map.isPossible(d))
  130. return 2;
  131. return 3;
  132. };
  133. auto area = (dAreaPossible + dAreaFree).getSubarea(areafilter);
  134. rmg::Path freePath(area);
  135. rmg::Path resultPath(area);
  136. freePath.connect(dAreaFree);
  137. //connect to all pieces
  138. auto goals = connectedAreas(src, onlyStraight);
  139. for(auto & goal : goals)
  140. {
  141. auto path = freePath.search(goal, onlyStraight, movementCost);
  142. if(path.getPathArea().empty())
  143. return rmg::Path::invalid();
  144. freePath.connect(path.getPathArea());
  145. resultPath.connect(path.getPathArea());
  146. }
  147. return resultPath;
  148. }
  149. rmg::Path Zone::searchPath(const int3 & src, bool onlyStraight, const std::function<bool(const int3 &)> & areafilter) const
  150. ///connect current tile to any other free tile within zone
  151. {
  152. return searchPath(rmg::Area({ src }), onlyStraight, areafilter);
  153. }
  154. TModificators Zone::getModificators()
  155. {
  156. return modificators;
  157. }
  158. void Zone::connectPath(const rmg::Path & path)
  159. ///connect current tile to any other free tile within zone
  160. {
  161. dAreaPossible.subtract(path.getPathArea());
  162. dAreaFree.unite(path.getPathArea());
  163. for(const auto & t : path.getPathArea().getTilesVector())
  164. map.setOccupied(t, ETileType::FREE);
  165. }
  166. void Zone::fractalize()
  167. {
  168. rmg::Area clearedTiles(dAreaFree);
  169. rmg::Area possibleTiles(dAreaPossible);
  170. rmg::Area tilesToIgnore; //will be erased in this iteration
  171. const float minDistance = 10 * 10; //squared
  172. float blockDistance = minDistance * 0.25f;
  173. if(type != ETemplateZoneType::JUNCTION)
  174. {
  175. //junction is not fractalized, has only one straight path
  176. //everything else remains blocked
  177. while(!possibleTiles.empty())
  178. {
  179. //link tiles in random order
  180. std::vector<int3> tilesToMakePath = possibleTiles.getTilesVector();
  181. RandomGeneratorUtil::randomShuffle(tilesToMakePath, getRand());
  182. int3 nodeFound(-1, -1, -1);
  183. for(const auto & tileToMakePath : tilesToMakePath)
  184. {
  185. //find closest free tile
  186. int3 closestTile = clearedTiles.nearest(tileToMakePath);
  187. if(closestTile.dist2dSQ(tileToMakePath) <= minDistance)
  188. tilesToIgnore.add(tileToMakePath);
  189. else
  190. {
  191. //if tiles are not close enough, make path to it
  192. nodeFound = tileToMakePath;
  193. clearedTiles.add(nodeFound); //from now on nearby tiles will be considered handled
  194. break; //next iteration - use already cleared tiles
  195. }
  196. }
  197. possibleTiles.subtract(tilesToIgnore);
  198. if(!nodeFound.valid()) //nothing else can be done (?)
  199. break;
  200. tilesToIgnore.clear();
  201. }
  202. }
  203. Lock lock(areaMutex);
  204. //cut straight paths towards the center. A* is too slow for that.
  205. auto areas = connectedAreas(clearedTiles, false);
  206. for(auto & area : areas)
  207. {
  208. if(dAreaFree.overlap(area))
  209. continue; //already found
  210. auto availableArea = dAreaPossible + dAreaFree;
  211. rmg::Path path(availableArea);
  212. path.connect(dAreaFree);
  213. auto res = path.search(area, false);
  214. if(res.getPathArea().empty())
  215. {
  216. dAreaPossible.subtract(area);
  217. dAreaFree.subtract(area);
  218. for(const auto & t : area.getTiles())
  219. map.setOccupied(t, ETileType::BLOCKED);
  220. }
  221. else
  222. {
  223. dAreaPossible.subtract(res.getPathArea());
  224. dAreaFree.unite(res.getPathArea());
  225. for(const auto & t : res.getPathArea().getTiles())
  226. map.setOccupied(t, ETileType::FREE);
  227. }
  228. }
  229. //now block most distant tiles away from passages
  230. auto areaToBlock = dArea.getSubarea([this, blockDistance](const int3 & t)
  231. {
  232. auto distance = static_cast<float>(dAreaFree.distanceSqr(t));
  233. return distance > blockDistance;
  234. });
  235. dAreaPossible.subtract(areaToBlock);
  236. dAreaFree.subtract(areaToBlock);
  237. lock.unlock();
  238. for(const auto & t : areaToBlock.getTiles())
  239. map.setOccupied(t, ETileType::BLOCKED);
  240. }
  241. void Zone::initModificators()
  242. {
  243. for(auto & modificator : modificators)
  244. {
  245. modificator->init();
  246. }
  247. logGlobal->info("Zone %d modificators initialized", getId());
  248. }
  249. CRandomGenerator& Zone::getRand()
  250. {
  251. return rand;
  252. }
  253. VCMI_LIB_NAMESPACE_END