Zone.cpp 6.6 KB

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