Zone.cpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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. : townType(ETownType::NEUTRAL)
  24. , terrainType(ETerrainId::GRASS)
  25. , map(map)
  26. , generator(generator)
  27. {
  28. }
  29. bool Zone::isUnderground() const
  30. {
  31. return getPos().z;
  32. }
  33. void Zone::setOptions(const ZoneOptions& options)
  34. {
  35. ZoneOptions::operator=(options);
  36. }
  37. float3 Zone::getCenter() const
  38. {
  39. return center;
  40. }
  41. void Zone::setCenter(const float3 &f)
  42. {
  43. //limit boundaries to (0,1) square
  44. //alternate solution - wrap zone around unitary square. If it doesn't fit on one side, will come out on the opposite side
  45. center = f;
  46. center.x = static_cast<float>(std::fmod(center.x, 1));
  47. center.y = static_cast<float>(std::fmod(center.y, 1));
  48. if(center.x < 0) //fmod seems to work only for positive numbers? we want to stay positive
  49. center.x = 1 - std::abs(center.x);
  50. if(center.y < 0)
  51. center.y = 1 - std::abs(center.y);
  52. }
  53. int3 Zone::getPos() const
  54. {
  55. return pos;
  56. }
  57. void Zone::setPos(const int3 &Pos)
  58. {
  59. pos = Pos;
  60. }
  61. const rmg::Area & Zone::getArea() const
  62. {
  63. return dArea;
  64. }
  65. rmg::Area & Zone::area()
  66. {
  67. return dArea;
  68. }
  69. rmg::Area & Zone::areaPossible()
  70. {
  71. return dAreaPossible;
  72. }
  73. rmg::Area & Zone::areaUsed()
  74. {
  75. return dAreaUsed;
  76. }
  77. std::vector<int3> Zone::getPossibleQuestArtifactPos() const
  78. {
  79. return possibleQuestArtifactPos;
  80. }
  81. void Zone::clearTiles()
  82. {
  83. dArea.clear();
  84. dAreaPossible.clear();
  85. dAreaFree.clear();
  86. }
  87. void Zone::initFreeTiles()
  88. {
  89. rmg::Tileset possibleTiles;
  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. void Zone::connectPath(const rmg::Path & path)
  154. ///connect current tile to any other free tile within zone
  155. {
  156. dAreaPossible.subtract(path.getPathArea());
  157. dAreaFree.unite(path.getPathArea());
  158. for(const auto & t : path.getPathArea().getTilesVector())
  159. map.setOccupied(t, ETileType::FREE);
  160. }
  161. void Zone::fractalize()
  162. {
  163. rmg::Area clearedTiles(dAreaFree);
  164. rmg::Area possibleTiles(dAreaPossible);
  165. rmg::Area tilesToIgnore; //will be erased in this iteration
  166. const float minDistance = 10 * 10; //squared
  167. if(type != ETemplateZoneType::JUNCTION)
  168. {
  169. //junction is not fractalized, has only one straight path
  170. //everything else remains blocked
  171. while(!possibleTiles.empty())
  172. {
  173. //link tiles in random order
  174. std::vector<int3> tilesToMakePath = possibleTiles.getTilesVector();
  175. RandomGeneratorUtil::randomShuffle(tilesToMakePath, generator.rand);
  176. int3 nodeFound(-1, -1, -1);
  177. for(const auto & tileToMakePath : tilesToMakePath)
  178. {
  179. //find closest free tile
  180. int3 closestTile = clearedTiles.nearest(tileToMakePath);
  181. if(closestTile.dist2dSQ(tileToMakePath) <= minDistance)
  182. tilesToIgnore.add(tileToMakePath);
  183. else
  184. {
  185. //if tiles are not close enough, make path to it
  186. nodeFound = tileToMakePath;
  187. clearedTiles.add(nodeFound); //from now on nearby tiles will be considered handled
  188. break; //next iteration - use already cleared tiles
  189. }
  190. }
  191. possibleTiles.subtract(tilesToIgnore);
  192. if(!nodeFound.valid()) //nothing else can be done (?)
  193. break;
  194. tilesToIgnore.clear();
  195. }
  196. }
  197. //cut straight paths towards the center. A* is too slow for that.
  198. auto areas = connectedAreas(clearedTiles, false);
  199. for(auto & area : areas)
  200. {
  201. if(dAreaFree.overlap(area))
  202. continue; //already found
  203. auto availableArea = dAreaPossible + dAreaFree;
  204. rmg::Path path(availableArea);
  205. path.connect(dAreaFree);
  206. auto res = path.search(area, false);
  207. if(res.getPathArea().empty())
  208. {
  209. dAreaPossible.subtract(area);
  210. dAreaFree.subtract(area);
  211. for(const auto & t : area.getTiles())
  212. map.setOccupied(t, ETileType::BLOCKED);
  213. }
  214. else
  215. {
  216. dAreaPossible.subtract(res.getPathArea());
  217. dAreaFree.unite(res.getPathArea());
  218. for(const auto & t : res.getPathArea().getTiles())
  219. map.setOccupied(t, ETileType::FREE);
  220. }
  221. }
  222. //now block most distant tiles away from passages
  223. float blockDistance = minDistance * 0.25f;
  224. auto areaToBlock = dArea.getSubarea([this, blockDistance](const int3 & t)
  225. {
  226. auto distance = static_cast<float>(dAreaFree.distanceSqr(t));
  227. return distance > blockDistance;
  228. });
  229. dAreaPossible.subtract(areaToBlock);
  230. dAreaFree.subtract(areaToBlock);
  231. for(const auto & t : areaToBlock.getTiles())
  232. map.setOccupied(t, ETileType::BLOCKED);
  233. }
  234. void Zone::initModificators()
  235. {
  236. for(auto & modificator : modificators)
  237. {
  238. modificator->init();
  239. }
  240. logGlobal->info("Zone %d modificators initialized", getId());
  241. }
  242. void Zone::processModificators()
  243. {
  244. for(auto & modificator : modificators)
  245. {
  246. try
  247. {
  248. modificator->run();
  249. }
  250. catch (const rmgException & e)
  251. {
  252. logGlobal->info("Zone %d, modificator %s - FAILED: %s", getId(), e.what());
  253. throw e;
  254. }
  255. }
  256. logGlobal->info("Zone %d filled successfully", getId());
  257. }
  258. VCMI_LIB_NAMESPACE_END