Zone.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  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. void Zone::clearTiles()
  80. {
  81. dArea.clear();
  82. dAreaPossible.clear();
  83. dAreaFree.clear();
  84. }
  85. void Zone::initFreeTiles()
  86. {
  87. rmg::Tileset possibleTiles;
  88. vstd::copy_if(dArea.getTiles(), vstd::set_inserter(possibleTiles), [this](const int3 &tile) -> bool
  89. {
  90. return map.isPossible(tile);
  91. });
  92. dAreaPossible.assign(possibleTiles);
  93. if(dAreaFree.empty())
  94. {
  95. dAreaPossible.erase(pos);
  96. dAreaFree.add(pos); //zone must have at least one free tile where other paths go - for instance in the center
  97. }
  98. }
  99. rmg::Area & Zone::freePaths()
  100. {
  101. return dAreaFree;
  102. }
  103. si32 Zone::getTownType() const
  104. {
  105. return townType;
  106. }
  107. void Zone::setTownType(si32 town)
  108. {
  109. townType = town;
  110. }
  111. TerrainId Zone::getTerrainType() const
  112. {
  113. return terrainType;
  114. }
  115. void Zone::setTerrainType(TerrainId terrain)
  116. {
  117. terrainType = terrain;
  118. }
  119. rmg::Path Zone::searchPath(const rmg::Area & src, bool onlyStraight, const std::function<bool(const int3 &)> & areafilter) const
  120. ///connect current tile to any other free tile within zone
  121. {
  122. auto movementCost = [this](const int3 & s, const int3 & d)
  123. {
  124. if(map.isFree(d))
  125. return 1;
  126. else if (map.isPossible(d))
  127. return 2;
  128. return 3;
  129. };
  130. auto area = (dAreaPossible + dAreaFree).getSubarea(areafilter);
  131. rmg::Path freePath(area);
  132. rmg::Path resultPath(area);
  133. freePath.connect(dAreaFree);
  134. //connect to all pieces
  135. auto goals = connectedAreas(src, onlyStraight);
  136. for(auto & goal : goals)
  137. {
  138. auto path = freePath.search(goal, onlyStraight, movementCost);
  139. if(path.getPathArea().empty())
  140. return rmg::Path::invalid();
  141. freePath.connect(path.getPathArea());
  142. resultPath.connect(path.getPathArea());
  143. }
  144. return resultPath;
  145. }
  146. rmg::Path Zone::searchPath(const int3 & src, bool onlyStraight, const std::function<bool(const int3 &)> & areafilter) const
  147. ///connect current tile to any other free tile within zone
  148. {
  149. return searchPath(rmg::Area({src}), onlyStraight, areafilter);
  150. }
  151. void Zone::connectPath(const rmg::Path & path)
  152. ///connect current tile to any other free tile within zone
  153. {
  154. dAreaPossible.subtract(path.getPathArea());
  155. dAreaFree.unite(path.getPathArea());
  156. for(const auto & t : path.getPathArea().getTilesVector())
  157. map.setOccupied(t, ETileType::FREE);
  158. }
  159. void Zone::fractalize()
  160. {
  161. rmg::Area clearedTiles(dAreaFree);
  162. rmg::Area possibleTiles(dAreaPossible);
  163. rmg::Area tilesToIgnore; //will be erased in this iteration
  164. const float minDistance = 10 * 10; //squared
  165. if(type != ETemplateZoneType::JUNCTION)
  166. {
  167. //junction is not fractalized, has only one straight path
  168. //everything else remains blocked
  169. while(!possibleTiles.empty())
  170. {
  171. //link tiles in random order
  172. std::vector<int3> tilesToMakePath = possibleTiles.getTilesVector();
  173. RandomGeneratorUtil::randomShuffle(tilesToMakePath, generator.rand);
  174. int3 nodeFound(-1, -1, -1);
  175. for(const auto & tileToMakePath : tilesToMakePath)
  176. {
  177. //find closest free tile
  178. int3 closestTile = clearedTiles.nearest(tileToMakePath);
  179. if(closestTile.dist2dSQ(tileToMakePath) <= minDistance)
  180. tilesToIgnore.add(tileToMakePath);
  181. else
  182. {
  183. //if tiles are not close enough, make path to it
  184. nodeFound = tileToMakePath;
  185. clearedTiles.add(nodeFound); //from now on nearby tiles will be considered handled
  186. break; //next iteration - use already cleared tiles
  187. }
  188. }
  189. possibleTiles.subtract(tilesToIgnore);
  190. if(!nodeFound.valid()) //nothing else can be done (?)
  191. break;
  192. tilesToIgnore.clear();
  193. }
  194. }
  195. //cut straight paths towards the center. A* is too slow for that.
  196. auto areas = connectedAreas(clearedTiles, false);
  197. for(auto & area : areas)
  198. {
  199. if(dAreaFree.overlap(area))
  200. continue; //already found
  201. auto availableArea = dAreaPossible + dAreaFree;
  202. rmg::Path path(availableArea);
  203. path.connect(dAreaFree);
  204. auto res = path.search(area, false);
  205. if(res.getPathArea().empty())
  206. {
  207. dAreaPossible.subtract(area);
  208. dAreaFree.subtract(area);
  209. for(const auto & t : area.getTiles())
  210. map.setOccupied(t, ETileType::BLOCKED);
  211. }
  212. else
  213. {
  214. dAreaPossible.subtract(res.getPathArea());
  215. dAreaFree.unite(res.getPathArea());
  216. for(const auto & t : res.getPathArea().getTiles())
  217. map.setOccupied(t, ETileType::FREE);
  218. }
  219. }
  220. //now block most distant tiles away from passages
  221. float blockDistance = minDistance * 0.25f;
  222. auto areaToBlock = dArea.getSubarea([this, blockDistance](const int3 & t)
  223. {
  224. auto distance = static_cast<float>(dAreaFree.distanceSqr(t));
  225. return distance > blockDistance;
  226. });
  227. dAreaPossible.subtract(areaToBlock);
  228. dAreaFree.subtract(areaToBlock);
  229. for(const auto & t : areaToBlock.getTiles())
  230. map.setOccupied(t, ETileType::BLOCKED);
  231. }
  232. void Zone::initModificators()
  233. {
  234. for(auto & modificator : modificators)
  235. {
  236. modificator->init();
  237. }
  238. logGlobal->info("Zone %d modificators initialized", getId());
  239. }
  240. void Zone::processModificators()
  241. {
  242. for(auto & modificator : modificators)
  243. {
  244. try
  245. {
  246. modificator->run();
  247. }
  248. catch (const rmgException & e)
  249. {
  250. logGlobal->info("Zone %d, modificator %s - FAILED: %s", getId(), e.what());
  251. throw e;
  252. }
  253. }
  254. logGlobal->info("Zone %d filled successfully", getId());
  255. }
  256. Modificator::Modificator(Zone & zone, RmgMap & map, CMapGenerator & generator) : zone(zone), map(map), generator(generator)
  257. {
  258. }
  259. void Modificator::setName(const std::string & n)
  260. {
  261. name = n;
  262. }
  263. const std::string & Modificator::getName() const
  264. {
  265. return name;
  266. }
  267. bool Modificator::isFinished() const
  268. {
  269. return finished;
  270. }
  271. void Modificator::run()
  272. {
  273. started = true;
  274. if(!finished)
  275. {
  276. for(auto * modificator : preceeders)
  277. {
  278. if(!modificator->started)
  279. modificator->run();
  280. }
  281. logGlobal->info("Modificator zone %d - %s - started", zone.getId(), getName());
  282. CStopWatch processTime;
  283. try
  284. {
  285. process();
  286. }
  287. catch(rmgException &e)
  288. {
  289. logGlobal->error("Modificator %s, exception: %s", getName(), e.what());
  290. }
  291. #ifdef RMG_DUMP
  292. dump();
  293. #endif
  294. finished = true;
  295. logGlobal->info("Modificator zone %d - %s - done (%d ms)", zone.getId(), getName(), processTime.getDiff());
  296. }
  297. }
  298. void Modificator::dependency(Modificator * modificator)
  299. {
  300. if(modificator && modificator != this)
  301. {
  302. if(std::find(preceeders.begin(), preceeders.end(), modificator) == preceeders.end())
  303. preceeders.push_back(modificator);
  304. }
  305. }
  306. void Modificator::postfunction(Modificator * modificator)
  307. {
  308. if(modificator && modificator != this)
  309. {
  310. if(std::find(modificator->preceeders.begin(), modificator->preceeders.end(), this) == modificator->preceeders.end())
  311. modificator->preceeders.push_back(this);
  312. }
  313. }
  314. void Modificator::dump()
  315. {
  316. std::ofstream out(boost::to_string(boost::format("seed_%d_modzone_%d_%s.txt") % generator.getRandomSeed() % zone.getId() % getName()));
  317. auto & mapInstance = map.map();
  318. int levels = mapInstance.levels();
  319. int width = mapInstance.width;
  320. int height = mapInstance.height;
  321. for(int z = 0; z < levels; z++)
  322. {
  323. for(int j=0; j<height; j++)
  324. {
  325. for(int i=0; i<width; i++)
  326. {
  327. out << dump(int3(i, j, z));
  328. }
  329. out << std::endl;
  330. }
  331. out << std::endl;
  332. }
  333. out << std::endl;
  334. }
  335. char Modificator::dump(const int3 & t)
  336. {
  337. if(zone.freePaths().contains(t))
  338. return '.'; //free path
  339. if(zone.areaPossible().contains(t))
  340. return ' '; //possible
  341. if(zone.areaUsed().contains(t))
  342. return 'U'; //used
  343. if(zone.area().contains(t))
  344. {
  345. if(map.shouldBeBlocked(t))
  346. return '#'; //obstacle
  347. else
  348. return '^'; //visitable points?
  349. }
  350. return '?';
  351. }
  352. VCMI_LIB_NAMESPACE_END