Zone.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  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 "../mapping/CMap.h"
  16. #include "../CStopWatch.h"
  17. #include "CMapGenerator.h"
  18. #include "RmgPath.h"
  19. VCMI_LIB_NAMESPACE_BEGIN
  20. std::function<bool(const int3 &)> AREA_NO_FILTER = [](const int3 & t)
  21. {
  22. return true;
  23. };
  24. Zone::Zone(RmgMap & map, CMapGenerator & generator)
  25. : townType(ETownType::NEUTRAL)
  26. , terrainType(ETerrainId::GRASS)
  27. , map(map)
  28. , generator(generator)
  29. {
  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. return dAreaPossible;
  74. }
  75. rmg::Area & Zone::areaUsed()
  76. {
  77. return dAreaUsed;
  78. }
  79. std::vector<int3> Zone::getPossibleQuestArtifactPos() const
  80. {
  81. return possibleQuestArtifactPos;
  82. }
  83. void Zone::clearTiles()
  84. {
  85. dArea.clear();
  86. dAreaPossible.clear();
  87. dAreaFree.clear();
  88. }
  89. void Zone::initFreeTiles()
  90. {
  91. rmg::Tileset possibleTiles;
  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 FactionID(townType);
  110. }
  111. void Zone::setTownType(si32 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 int3 & src, bool onlyStraight, const std::function<bool(const int3 &)> & areafilter) const
  151. ///connect current tile to any other free tile within zone
  152. {
  153. return searchPath(rmg::Area({src}), onlyStraight, areafilter);
  154. }
  155. void Zone::connectPath(const rmg::Path & path)
  156. ///connect current tile to any other free tile within zone
  157. {
  158. dAreaPossible.subtract(path.getPathArea());
  159. dAreaFree.unite(path.getPathArea());
  160. for(const auto & t : path.getPathArea().getTilesVector())
  161. map.setOccupied(t, ETileType::FREE);
  162. }
  163. void Zone::fractalize()
  164. {
  165. rmg::Area clearedTiles(dAreaFree);
  166. rmg::Area possibleTiles(dAreaPossible);
  167. rmg::Area tilesToIgnore; //will be erased in this iteration
  168. const float minDistance = 10 * 10; //squared
  169. if(type != ETemplateZoneType::JUNCTION)
  170. {
  171. //junction is not fractalized, has only one straight path
  172. //everything else remains blocked
  173. while(!possibleTiles.empty())
  174. {
  175. //link tiles in random order
  176. std::vector<int3> tilesToMakePath = possibleTiles.getTilesVector();
  177. RandomGeneratorUtil::randomShuffle(tilesToMakePath, generator.rand);
  178. int3 nodeFound(-1, -1, -1);
  179. for(const auto & tileToMakePath : tilesToMakePath)
  180. {
  181. //find closest free tile
  182. int3 closestTile = clearedTiles.nearest(tileToMakePath);
  183. if(closestTile.dist2dSQ(tileToMakePath) <= minDistance)
  184. tilesToIgnore.add(tileToMakePath);
  185. else
  186. {
  187. //if tiles are not close enough, make path to it
  188. nodeFound = tileToMakePath;
  189. clearedTiles.add(nodeFound); //from now on nearby tiles will be considered handled
  190. break; //next iteration - use already cleared tiles
  191. }
  192. }
  193. possibleTiles.subtract(tilesToIgnore);
  194. if(!nodeFound.valid()) //nothing else can be done (?)
  195. break;
  196. tilesToIgnore.clear();
  197. }
  198. }
  199. //cut straight paths towards the center. A* is too slow for that.
  200. auto areas = connectedAreas(clearedTiles, false);
  201. for(auto & area : areas)
  202. {
  203. if(dAreaFree.overlap(area))
  204. continue; //already found
  205. auto availableArea = dAreaPossible + dAreaFree;
  206. rmg::Path path(availableArea);
  207. path.connect(dAreaFree);
  208. auto res = path.search(area, false);
  209. if(res.getPathArea().empty())
  210. {
  211. dAreaPossible.subtract(area);
  212. dAreaFree.subtract(area);
  213. for(const auto & t : area.getTiles())
  214. map.setOccupied(t, ETileType::BLOCKED);
  215. }
  216. else
  217. {
  218. dAreaPossible.subtract(res.getPathArea());
  219. dAreaFree.unite(res.getPathArea());
  220. for(const auto & t : res.getPathArea().getTiles())
  221. map.setOccupied(t, ETileType::FREE);
  222. }
  223. }
  224. //now block most distant tiles away from passages
  225. float blockDistance = minDistance * 0.25f;
  226. auto areaToBlock = dArea.getSubarea([this, blockDistance](const int3 & t)
  227. {
  228. auto distance = static_cast<float>(dAreaFree.distanceSqr(t));
  229. return distance > blockDistance;
  230. });
  231. dAreaPossible.subtract(areaToBlock);
  232. dAreaFree.subtract(areaToBlock);
  233. for(const auto & t : areaToBlock.getTiles())
  234. map.setOccupied(t, ETileType::BLOCKED);
  235. }
  236. void Zone::initModificators()
  237. {
  238. for(auto & modificator : modificators)
  239. {
  240. modificator->init();
  241. }
  242. logGlobal->info("Zone %d modificators initialized", getId());
  243. }
  244. void Zone::processModificators()
  245. {
  246. for(auto & modificator : modificators)
  247. {
  248. try
  249. {
  250. modificator->run();
  251. }
  252. catch (const rmgException & e)
  253. {
  254. logGlobal->info("Zone %d, modificator %s - FAILED: %s", getId(), e.what());
  255. throw e;
  256. }
  257. }
  258. logGlobal->info("Zone %d filled successfully", getId());
  259. }
  260. Modificator::Modificator(Zone & zone, RmgMap & map, CMapGenerator & generator) : zone(zone), map(map), generator(generator)
  261. {
  262. }
  263. void Modificator::setName(const std::string & n)
  264. {
  265. name = n;
  266. }
  267. const std::string & Modificator::getName() const
  268. {
  269. return name;
  270. }
  271. bool Modificator::isFinished() const
  272. {
  273. return finished;
  274. }
  275. void Modificator::run()
  276. {
  277. started = true;
  278. if(!finished)
  279. {
  280. for(auto * modificator : preceeders)
  281. {
  282. if(!modificator->started)
  283. modificator->run();
  284. }
  285. logGlobal->info("Modificator zone %d - %s - started", zone.getId(), getName());
  286. CStopWatch processTime;
  287. try
  288. {
  289. process();
  290. }
  291. catch(rmgException &e)
  292. {
  293. logGlobal->error("Modificator %s, exception: %s", getName(), e.what());
  294. }
  295. #ifdef RMG_DUMP
  296. dump();
  297. #endif
  298. finished = true;
  299. logGlobal->info("Modificator zone %d - %s - done (%d ms)", zone.getId(), getName(), processTime.getDiff());
  300. }
  301. }
  302. void Modificator::dependency(Modificator * modificator)
  303. {
  304. if(modificator && modificator != this)
  305. {
  306. if(std::find(preceeders.begin(), preceeders.end(), modificator) == preceeders.end())
  307. preceeders.push_back(modificator);
  308. }
  309. }
  310. void Modificator::postfunction(Modificator * modificator)
  311. {
  312. if(modificator && modificator != this)
  313. {
  314. if(std::find(modificator->preceeders.begin(), modificator->preceeders.end(), this) == modificator->preceeders.end())
  315. modificator->preceeders.push_back(this);
  316. }
  317. }
  318. void Modificator::dump()
  319. {
  320. std::ofstream out(boost::to_string(boost::format("seed_%d_modzone_%d_%s.txt") % generator.getRandomSeed() % zone.getId() % getName()));
  321. auto & mapInstance = map.map();
  322. int levels = mapInstance.levels();
  323. int width = mapInstance.width;
  324. int height = mapInstance.height;
  325. for(int z = 0; z < levels; z++)
  326. {
  327. for(int j=0; j<height; j++)
  328. {
  329. for(int i=0; i<width; i++)
  330. {
  331. out << dump(int3(i, j, z));
  332. }
  333. out << std::endl;
  334. }
  335. out << std::endl;
  336. }
  337. out << std::endl;
  338. }
  339. char Modificator::dump(const int3 & t)
  340. {
  341. if(zone.freePaths().contains(t))
  342. return '.'; //free path
  343. if(zone.areaPossible().contains(t))
  344. return ' '; //possible
  345. if(zone.areaUsed().contains(t))
  346. return 'U'; //used
  347. if(zone.area().contains(t))
  348. {
  349. if(map.shouldBeBlocked(t))
  350. return '#'; //obstacle
  351. else
  352. return '^'; //visitable points?
  353. }
  354. return '?';
  355. }
  356. VCMI_LIB_NAMESPACE_END