Zone.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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. //Squared
  172. float minDistance = 10 * 10;
  173. float spanFactor = (pos.z ? 0.25 : 0.5f); //Narrower passages in the Underground
  174. int treasureValue = 0;
  175. int treasureDensity = 0;
  176. for (auto t : treasureInfo)
  177. {
  178. treasureValue += ((t.min + t.max) / 2) * t.density / 1000.f; //Thousands
  179. treasureDensity += t.density;
  180. }
  181. if (treasureValue > 200)
  182. {
  183. //Less obstacles - max span is 1 (no obstacles)
  184. spanFactor = 1.0f - ((std::max(0, (1000 - treasureValue)) / (1000.f - 200)) * (1 - spanFactor));
  185. }
  186. else if (treasureValue < 100)
  187. {
  188. //Dense obstacles
  189. spanFactor *= (treasureValue / 100.f);
  190. vstd::amax(spanFactor, 0.2f);
  191. }
  192. if (treasureDensity <= 10)
  193. {
  194. vstd::amin(spanFactor, 0.25f); //Add extra obstacles to fill up space
  195. }
  196. float blockDistance = minDistance * spanFactor; //More obstacles in the Underground
  197. if(type != ETemplateZoneType::JUNCTION)
  198. {
  199. //junction is not fractalized, has only one straight path
  200. //everything else remains blocked
  201. while(!possibleTiles.empty())
  202. {
  203. //link tiles in random order
  204. std::vector<int3> tilesToMakePath = possibleTiles.getTilesVector();
  205. RandomGeneratorUtil::randomShuffle(tilesToMakePath, getRand());
  206. int3 nodeFound(-1, -1, -1);
  207. for(const auto & tileToMakePath : tilesToMakePath)
  208. {
  209. //find closest free tile
  210. int3 closestTile = clearedTiles.nearest(tileToMakePath);
  211. if(closestTile.dist2dSQ(tileToMakePath) <= minDistance)
  212. tilesToIgnore.add(tileToMakePath);
  213. else
  214. {
  215. //if tiles are not close enough, make path to it
  216. nodeFound = tileToMakePath;
  217. clearedTiles.add(nodeFound); //from now on nearby tiles will be considered handled
  218. break; //next iteration - use already cleared tiles
  219. }
  220. }
  221. possibleTiles.subtract(tilesToIgnore);
  222. if(!nodeFound.valid()) //nothing else can be done (?)
  223. break;
  224. tilesToIgnore.clear();
  225. }
  226. }
  227. Lock lock(areaMutex);
  228. //cut straight paths towards the center. A* is too slow for that.
  229. auto areas = connectedAreas(clearedTiles, false);
  230. for(auto & area : areas)
  231. {
  232. if(dAreaFree.overlap(area))
  233. continue; //already found
  234. auto availableArea = dAreaPossible + dAreaFree;
  235. rmg::Path path(availableArea);
  236. path.connect(dAreaFree);
  237. auto res = path.search(area, false);
  238. if(res.getPathArea().empty())
  239. {
  240. dAreaPossible.subtract(area);
  241. dAreaFree.subtract(area);
  242. for(const auto & t : area.getTiles())
  243. map.setOccupied(t, ETileType::BLOCKED);
  244. }
  245. else
  246. {
  247. dAreaPossible.subtract(res.getPathArea());
  248. dAreaFree.unite(res.getPathArea());
  249. for(const auto & t : res.getPathArea().getTiles())
  250. map.setOccupied(t, ETileType::FREE);
  251. }
  252. }
  253. //now block most distant tiles away from passages
  254. auto areaToBlock = dArea.getSubarea([this, blockDistance](const int3 & t)
  255. {
  256. auto distance = static_cast<float>(dAreaFree.distanceSqr(t));
  257. return distance > blockDistance;
  258. });
  259. dAreaPossible.subtract(areaToBlock);
  260. dAreaFree.subtract(areaToBlock);
  261. lock.unlock();
  262. for(const auto & t : areaToBlock.getTiles())
  263. map.setOccupied(t, ETileType::BLOCKED);
  264. }
  265. void Zone::initModificators()
  266. {
  267. for(auto & modificator : modificators)
  268. {
  269. modificator->init();
  270. }
  271. logGlobal->info("Zone %d modificators initialized", getId());
  272. }
  273. CRandomGenerator& Zone::getRand()
  274. {
  275. return rand;
  276. }
  277. VCMI_LIB_NAMESPACE_END