Zone.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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. #include "modificators/ObjectManager.h"
  18. VCMI_LIB_NAMESPACE_BEGIN
  19. std::function<bool(const int3 &)> AREA_NO_FILTER = [](const int3 & t)
  20. {
  21. return true;
  22. };
  23. Zone::Zone(RmgMap & map, CMapGenerator & generator, CRandomGenerator & r)
  24. : finished(false)
  25. , townType(ETownType::NEUTRAL)
  26. , terrainType(ETerrainId::GRASS)
  27. , map(map)
  28. , generator(generator)
  29. {
  30. rand.setSeed(r.nextInt());
  31. }
  32. bool Zone::isUnderground() const
  33. {
  34. return getPos().z;
  35. }
  36. void Zone::setOptions(const ZoneOptions& options)
  37. {
  38. ZoneOptions::operator=(options);
  39. }
  40. float3 Zone::getCenter() const
  41. {
  42. return center;
  43. }
  44. void Zone::setCenter(const float3 &f)
  45. {
  46. //limit boundaries to (0,1) square
  47. //alternate solution - wrap zone around unitary square. If it doesn't fit on one side, will come out on the opposite side
  48. center = f;
  49. center.x = static_cast<float>(std::fmod(center.x, 1));
  50. center.y = static_cast<float>(std::fmod(center.y, 1));
  51. if(center.x < 0) //fmod seems to work only for positive numbers? we want to stay positive
  52. center.x = 1 - std::abs(center.x);
  53. if(center.y < 0)
  54. center.y = 1 - std::abs(center.y);
  55. }
  56. int3 Zone::getPos() const
  57. {
  58. return pos;
  59. }
  60. void Zone::setPos(const int3 &Pos)
  61. {
  62. pos = Pos;
  63. }
  64. const rmg::Area & Zone::getArea() const
  65. {
  66. return dArea;
  67. }
  68. rmg::Area & Zone::area()
  69. {
  70. return dArea;
  71. }
  72. rmg::Area & Zone::areaPossible()
  73. {
  74. //FIXME: make const, only modify via mutex-protected interface
  75. return dAreaPossible;
  76. }
  77. rmg::Area & Zone::areaUsed()
  78. {
  79. return dAreaUsed;
  80. }
  81. void Zone::clearTiles()
  82. {
  83. //Lock lock(mx);
  84. dArea.clear();
  85. dAreaPossible.clear();
  86. dAreaFree.clear();
  87. }
  88. void Zone::initFreeTiles()
  89. {
  90. rmg::Tileset possibleTiles;
  91. //Lock lock(mx);
  92. vstd::copy_if(dArea.getTiles(), vstd::set_inserter(possibleTiles), [this](const int3 &tile) -> bool
  93. {
  94. return map.isPossible(tile);
  95. });
  96. dAreaPossible.assign(possibleTiles);
  97. if(dAreaFree.empty())
  98. {
  99. dAreaPossible.erase(pos);
  100. dAreaFree.add(pos); //zone must have at least one free tile where other paths go - for instance in the center
  101. }
  102. }
  103. rmg::Area & Zone::freePaths()
  104. {
  105. return dAreaFree;
  106. }
  107. FactionID Zone::getTownType() const
  108. {
  109. return townType;
  110. }
  111. void Zone::setTownType(FactionID town)
  112. {
  113. townType = town;
  114. }
  115. TerrainId Zone::getTerrainType() const
  116. {
  117. return terrainType;
  118. }
  119. void Zone::setTerrainType(TerrainId terrain)
  120. {
  121. terrainType = terrain;
  122. }
  123. rmg::Path Zone::searchPath(const rmg::Area & src, bool onlyStraight, const std::function<bool(const int3 &)> & areafilter) const
  124. ///connect current tile to any other free tile within zone
  125. {
  126. auto movementCost = [this](const int3 & s, const int3 & d)
  127. {
  128. if(map.isFree(d))
  129. return 1;
  130. else if (map.isPossible(d))
  131. return 2;
  132. return 3;
  133. };
  134. auto area = (dAreaPossible + dAreaFree).getSubarea(areafilter);
  135. rmg::Path freePath(area);
  136. rmg::Path resultPath(area);
  137. freePath.connect(dAreaFree);
  138. //connect to all pieces
  139. auto goals = connectedAreas(src, onlyStraight);
  140. for(auto & goal : goals)
  141. {
  142. auto path = freePath.search(goal, onlyStraight, movementCost);
  143. if(path.getPathArea().empty())
  144. return rmg::Path::invalid();
  145. freePath.connect(path.getPathArea());
  146. resultPath.connect(path.getPathArea());
  147. }
  148. return resultPath;
  149. }
  150. rmg::Path Zone::searchPath(const rmg::Area & src, bool onlyStraight, const rmg::Area & searchArea) const
  151. ///connect current tile to any other free tile within searchArea
  152. {
  153. auto movementCost = [this](const int3 & s, const int3 & d)
  154. {
  155. if(map.isFree(d))
  156. return 1;
  157. else if (map.isPossible(d))
  158. return 2;
  159. return 3;
  160. };
  161. rmg::Path freePath(searchArea);
  162. rmg::Path resultPath(searchArea);
  163. freePath.connect(dAreaFree);
  164. //connect to all pieces
  165. auto goals = connectedAreas(src, onlyStraight);
  166. for(auto & goal : goals)
  167. {
  168. auto path = freePath.search(goal, onlyStraight, movementCost);
  169. if(path.getPathArea().empty())
  170. return rmg::Path::invalid();
  171. freePath.connect(path.getPathArea());
  172. resultPath.connect(path.getPathArea());
  173. }
  174. return resultPath;
  175. }
  176. rmg::Path Zone::searchPath(const int3 & src, bool onlyStraight, const std::function<bool(const int3 &)> & areafilter) const
  177. ///connect current tile to any other free tile within zone
  178. {
  179. return searchPath(rmg::Area({ src }), onlyStraight, areafilter);
  180. }
  181. TModificators Zone::getModificators()
  182. {
  183. return modificators;
  184. }
  185. void Zone::connectPath(const rmg::Path & path)
  186. ///connect current tile to any other free tile within zone
  187. {
  188. dAreaPossible.subtract(path.getPathArea());
  189. dAreaFree.unite(path.getPathArea());
  190. for(const auto & t : path.getPathArea().getTilesVector())
  191. map.setOccupied(t, ETileType::FREE);
  192. }
  193. void Zone::fractalize()
  194. {
  195. rmg::Area clearedTiles(dAreaFree);
  196. rmg::Area possibleTiles(dAreaPossible);
  197. rmg::Area tilesToIgnore; //will be erased in this iteration
  198. //Squared
  199. float minDistance = 9 * 9;
  200. float freeDistance = pos.z ? (10 * 10) : 6 * 6;
  201. float spanFactor = (pos.z ? 0.25 : 0.5f); //Narrower passages in the Underground
  202. float marginFactor = 1.0f;
  203. int treasureValue = 0;
  204. int treasureDensity = 0;
  205. for (const auto & t : treasureInfo)
  206. {
  207. treasureValue += ((t.min + t.max) / 2) * t.density / 1000.f; //Thousands
  208. treasureDensity += t.density;
  209. }
  210. if (treasureValue > 400)
  211. {
  212. // A quater at max density
  213. marginFactor = (0.25f + ((std::max(0, (600 - treasureValue))) / (600.f - 400)) * 0.75f);
  214. }
  215. else if (treasureValue < 125)
  216. {
  217. //Dense obstacles
  218. spanFactor *= (treasureValue / 125.f);
  219. vstd::amax(spanFactor, 0.15f);
  220. }
  221. if (treasureDensity <= 10)
  222. {
  223. vstd::amin(spanFactor, 0.1f + 0.01f * treasureDensity); //Add extra obstacles to fill up space
  224. }
  225. float blockDistance = minDistance * spanFactor; //More obstacles in the Underground
  226. freeDistance = freeDistance * marginFactor;
  227. vstd::amax(freeDistance, 4 * 4);
  228. logGlobal->info("Zone %d: treasureValue %d blockDistance: %2.f, freeDistance: %2.f", getId(), treasureValue, blockDistance, freeDistance);
  229. if(type != ETemplateZoneType::JUNCTION)
  230. {
  231. //junction is not fractalized, has only one straight path
  232. //everything else remains blocked
  233. while(!possibleTiles.empty())
  234. {
  235. //link tiles in random order
  236. std::vector<int3> tilesToMakePath = possibleTiles.getTilesVector();
  237. // Do not fractalize tiles near the edge of the map to avoid paths adjacent to map edge
  238. const auto h = map.height();
  239. const auto w = map.width();
  240. const size_t MARGIN = 3;
  241. vstd::erase_if(tilesToMakePath, [&, h, w](const int3 & tile)
  242. {
  243. return tile.x < MARGIN || tile.x > (w - MARGIN) ||
  244. tile.y < MARGIN || tile.y > (h - MARGIN);
  245. });
  246. RandomGeneratorUtil::randomShuffle(tilesToMakePath, getRand());
  247. int3 nodeFound(-1, -1, -1);
  248. for(const auto & tileToMakePath : tilesToMakePath)
  249. {
  250. //find closest free tile
  251. int3 closestTile = clearedTiles.nearest(tileToMakePath);
  252. if(closestTile.dist2dSQ(tileToMakePath) <= freeDistance)
  253. tilesToIgnore.add(tileToMakePath);
  254. else
  255. {
  256. //if tiles are not close enough, make path to it
  257. nodeFound = tileToMakePath;
  258. clearedTiles.add(nodeFound); //from now on nearby tiles will be considered handled
  259. break; //next iteration - use already cleared tiles
  260. }
  261. }
  262. possibleTiles.subtract(tilesToIgnore);
  263. if(!nodeFound.valid()) //nothing else can be done (?)
  264. break;
  265. tilesToIgnore.clear();
  266. }
  267. }
  268. else
  269. {
  270. // Handle special case - place Monoliths at the edge of a zone
  271. auto objectManager = getModificator<ObjectManager>();
  272. if (objectManager)
  273. {
  274. objectManager->createMonoliths();
  275. }
  276. }
  277. Lock lock(areaMutex);
  278. //cut straight paths towards the center. A* is too slow for that.
  279. auto areas = connectedAreas(clearedTiles, false);
  280. for(auto & area : areas)
  281. {
  282. if(dAreaFree.overlap(area))
  283. continue; //already found
  284. auto availableArea = dAreaPossible + dAreaFree;
  285. rmg::Path path(availableArea);
  286. path.connect(dAreaFree);
  287. auto res = path.search(area, false);
  288. if(res.getPathArea().empty())
  289. {
  290. dAreaPossible.subtract(area);
  291. dAreaFree.subtract(area);
  292. for(const auto & t : area.getTiles())
  293. map.setOccupied(t, ETileType::BLOCKED);
  294. }
  295. else
  296. {
  297. dAreaPossible.subtract(res.getPathArea());
  298. dAreaFree.unite(res.getPathArea());
  299. for(const auto & t : res.getPathArea().getTiles())
  300. map.setOccupied(t, ETileType::FREE);
  301. }
  302. }
  303. //now block most distant tiles away from passages
  304. auto areaToBlock = dArea.getSubarea([this, blockDistance](const int3 & t)
  305. {
  306. auto distance = static_cast<float>(dAreaFree.distanceSqr(t));
  307. return distance > blockDistance;
  308. });
  309. dAreaPossible.subtract(areaToBlock);
  310. dAreaFree.subtract(areaToBlock);
  311. lock.unlock();
  312. for(const auto & t : areaToBlock.getTiles())
  313. map.setOccupied(t, ETileType::BLOCKED);
  314. }
  315. void Zone::initModificators()
  316. {
  317. for(auto & modificator : modificators)
  318. {
  319. modificator->init();
  320. }
  321. logGlobal->info("Zone %d modificators initialized", getId());
  322. }
  323. CRandomGenerator& Zone::getRand()
  324. {
  325. return rand;
  326. }
  327. VCMI_LIB_NAMESPACE_END