CMapGenerator.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786
  1. #include "StdInc.h"
  2. #include "CMapGenerator.h"
  3. #include "../mapping/CMap.h"
  4. #include "../VCMI_Lib.h"
  5. #include "../CGeneralTextHandler.h"
  6. #include "../mapping/CMapEditManager.h"
  7. #include "../CTownHandler.h"
  8. #include "../StringConstants.h"
  9. #include "../filesystem/Filesystem.h"
  10. #include "CRmgTemplate.h"
  11. #include "CRmgTemplateZone.h"
  12. #include "CZonePlacer.h"
  13. #include "../mapObjects/CObjectClassesHandler.h"
  14. static const int3 dirs4[] = {int3(0,1,0),int3(0,-1,0),int3(-1,0,0),int3(+1,0,0)};
  15. void CMapGenerator::foreach_neighbour(const int3 &pos, std::function<void(int3& pos)> foo)
  16. {
  17. for(const int3 &dir : int3::getDirs())
  18. {
  19. int3 n = pos + dir;
  20. /*important notice: perform any translation before this function is called,
  21. so the actual map position is checked*/
  22. if(map->isInTheMap(n))
  23. foo(n);
  24. }
  25. }
  26. void CMapGenerator::foreachDirectNeighbour(const int3& pos, std::function<void(int3& pos)> foo)
  27. {
  28. for(const int3 &dir : dirs4)
  29. {
  30. int3 n = pos + dir;
  31. if(map->isInTheMap(n))
  32. foo(n);
  33. }
  34. }
  35. CMapGenerator::CMapGenerator() :
  36. zonesTotal(0), monolithIndex(0)
  37. {
  38. }
  39. void CMapGenerator::initTiles()
  40. {
  41. map->initTerrain();
  42. int width = map->width;
  43. int height = map->height;
  44. int level = map->twoLevel ? 2 : 1;
  45. tiles = new CTileInfo**[width];
  46. for (int i = 0; i < width; ++i)
  47. {
  48. tiles[i] = new CTileInfo*[height];
  49. for (int j = 0; j < height; ++j)
  50. {
  51. tiles[i][j] = new CTileInfo[level];
  52. }
  53. }
  54. zoneColouring.resize(boost::extents[map->twoLevel ? 2 : 1][map->width][map->height]);
  55. }
  56. CMapGenerator::~CMapGenerator()
  57. {
  58. if (tiles)
  59. {
  60. int width = mapGenOptions->getWidth();
  61. int height = mapGenOptions->getHeight();
  62. for (int i=0; i < width; i++)
  63. {
  64. for(int j=0; j < height; j++)
  65. {
  66. delete [] tiles[i][j];
  67. }
  68. delete [] tiles[i];
  69. }
  70. delete [] tiles;
  71. }
  72. }
  73. void CMapGenerator::initPrisonsRemaining()
  74. {
  75. prisonsRemaining = 0;
  76. for (auto isAllowed : map->allowedHeroes)
  77. {
  78. if (isAllowed)
  79. prisonsRemaining++;
  80. }
  81. prisonsRemaining = std::max<int> (0, prisonsRemaining - 16 * mapGenOptions->getPlayerCount()); //so at least 16 heroes will be available for every player
  82. }
  83. void CMapGenerator::initQuestArtsRemaining()
  84. {
  85. for (auto art : VLC->arth->artifacts)
  86. {
  87. if (art->aClass == CArtifact::ART_TREASURE && VLC->arth->legalArtifact(art->id) && art->constituentOf.empty()) //don't use parts of combined artifacts
  88. questArtifacts.push_back(art->id);
  89. }
  90. }
  91. std::unique_ptr<CMap> CMapGenerator::generate(CMapGenOptions * mapGenOptions, int randomSeed /*= std::time(nullptr)*/)
  92. {
  93. this->mapGenOptions = mapGenOptions;
  94. this->randomSeed = randomSeed;
  95. assert(mapGenOptions);
  96. rand.setSeed(this->randomSeed);
  97. mapGenOptions->finalize(rand);
  98. map = make_unique<CMap>();
  99. editManager = map->getEditManager();
  100. try
  101. {
  102. editManager->getUndoManager().setUndoRedoLimit(0);
  103. //FIXME: somehow mapGenOption is nullptr at this point :?
  104. addHeaderInfo();
  105. initTiles();
  106. initPrisonsRemaining();
  107. initQuestArtsRemaining();
  108. genZones();
  109. map->calculateGuardingGreaturePositions(); //clear map so that all tiles are unguarded
  110. fillZones();
  111. //updated guarded tiles will be calculated in CGameState::initMapObjects()
  112. }
  113. catch (rmgException &e)
  114. {
  115. logGlobal->errorStream() << "Random map generation received exception: " << e.what();
  116. }
  117. return std::move(map);
  118. }
  119. std::string CMapGenerator::getMapDescription() const
  120. {
  121. assert(mapGenOptions);
  122. assert(map);
  123. const std::string waterContentStr[3] = { "none", "normal", "islands" };
  124. const std::string monsterStrengthStr[3] = { "weak", "normal", "strong" };
  125. int monsterStrengthIndex = mapGenOptions->getMonsterStrength() - EMonsterStrength::GLOBAL_WEAK; //does not start from 0
  126. std::stringstream ss;
  127. ss << boost::str(boost::format(std::string("Map created by the Random Map Generator.\nTemplate was %s, Random seed was %d, size %dx%d") +
  128. ", levels %s, players %d, computers %d, water %s, monster %s, VCMI map") % mapGenOptions->getMapTemplate()->getName() %
  129. randomSeed % map->width % map->height % (map->twoLevel ? "2" : "1") % static_cast<int>(mapGenOptions->getPlayerCount()) %
  130. static_cast<int>(mapGenOptions->getCompOnlyPlayerCount()) % waterContentStr[mapGenOptions->getWaterContent()] %
  131. monsterStrengthStr[monsterStrengthIndex]);
  132. for(const auto & pair : mapGenOptions->getPlayersSettings())
  133. {
  134. const auto & pSettings = pair.second;
  135. if(pSettings.getPlayerType() == EPlayerType::HUMAN)
  136. {
  137. ss << ", " << GameConstants::PLAYER_COLOR_NAMES[pSettings.getColor().getNum()] << " is human";
  138. }
  139. if(pSettings.getStartingTown() != CMapGenOptions::CPlayerSettings::RANDOM_TOWN)
  140. {
  141. ss << ", " << GameConstants::PLAYER_COLOR_NAMES[pSettings.getColor().getNum()]
  142. << " town choice is " << VLC->townh->factions[pSettings.getStartingTown()]->name;
  143. }
  144. }
  145. return ss.str();
  146. }
  147. void CMapGenerator::addPlayerInfo()
  148. {
  149. // Calculate which team numbers exist
  150. enum ETeams {CPHUMAN = 0, CPUONLY = 1, AFTER_LAST = 2};
  151. std::array<std::list<int>, 2> teamNumbers;
  152. int teamOffset = 0;
  153. int playerCount = 0;
  154. int teamCount = 0;
  155. for (int i = CPHUMAN; i < AFTER_LAST; ++i)
  156. {
  157. if (i == CPHUMAN)
  158. {
  159. playerCount = mapGenOptions->getPlayerCount();
  160. teamCount = mapGenOptions->getTeamCount();
  161. }
  162. else
  163. {
  164. playerCount = mapGenOptions->getCompOnlyPlayerCount();
  165. teamCount = mapGenOptions->getCompOnlyTeamCount();
  166. }
  167. if(playerCount == 0)
  168. {
  169. continue;
  170. }
  171. int playersPerTeam = playerCount / (teamCount == 0 ? playerCount : teamCount);
  172. int teamCountNorm = teamCount;
  173. if(teamCountNorm == 0)
  174. {
  175. teamCountNorm = playerCount;
  176. }
  177. for(int j = 0; j < teamCountNorm; ++j)
  178. {
  179. for(int k = 0; k < playersPerTeam; ++k)
  180. {
  181. teamNumbers[i].push_back(j + teamOffset);
  182. }
  183. }
  184. for(int j = 0; j < playerCount - teamCountNorm * playersPerTeam; ++j)
  185. {
  186. teamNumbers[i].push_back(j + teamOffset);
  187. }
  188. teamOffset += teamCountNorm;
  189. }
  190. // Team numbers are assigned randomly to every player
  191. //TODO: allow customize teams in rmg template
  192. for(const auto & pair : mapGenOptions->getPlayersSettings())
  193. {
  194. const auto & pSettings = pair.second;
  195. PlayerInfo player;
  196. player.canComputerPlay = true;
  197. int j = (pSettings.getPlayerType() == EPlayerType::COMP_ONLY) ? CPUONLY : CPHUMAN;
  198. if (j == CPHUMAN)
  199. {
  200. player.canHumanPlay = true;
  201. }
  202. if (teamNumbers[j].empty())
  203. {
  204. logGlobal->errorStream() << boost::format("Not enough places in team for %s player") % ((j == CPUONLY) ? "CPU" : "CPU or human");
  205. assert (teamNumbers[j].size());
  206. }
  207. auto itTeam = RandomGeneratorUtil::nextItem(teamNumbers[j], rand);
  208. player.team = TeamID(*itTeam);
  209. teamNumbers[j].erase(itTeam);
  210. map->players[pSettings.getColor().getNum()] = player;
  211. }
  212. map->howManyTeams = (mapGenOptions->getTeamCount() == 0 ? mapGenOptions->getPlayerCount() : mapGenOptions->getTeamCount())
  213. + (mapGenOptions->getCompOnlyTeamCount() == 0 ? mapGenOptions->getCompOnlyPlayerCount() : mapGenOptions->getCompOnlyTeamCount());
  214. }
  215. void CMapGenerator::genZones()
  216. {
  217. editManager->clearTerrain(&rand);
  218. editManager->getTerrainSelection().selectRange(MapRect(int3(0, 0, 0), mapGenOptions->getWidth(), mapGenOptions->getHeight()));
  219. editManager->drawTerrain(ETerrainType::GRASS, &rand);
  220. auto tmpl = mapGenOptions->getMapTemplate();
  221. zones = tmpl->getZones(); //copy from template (refactor?)
  222. CZonePlacer placer(this);
  223. placer.placeZones(mapGenOptions, &rand);
  224. placer.assignZones(mapGenOptions);
  225. logGlobal->infoStream() << "Zones generated successfully";
  226. }
  227. void CMapGenerator::fillZones()
  228. {
  229. //init native town count with 0
  230. for (auto faction : VLC->townh->getAllowedFactions())
  231. zonesPerFaction[faction] = 0;
  232. logGlobal->infoStream() << "Started filling zones";
  233. //initialize possible tiles before any object is actually placed
  234. for (auto it : zones)
  235. {
  236. it.second->initFreeTiles(this);
  237. }
  238. findZonesForQuestArts();
  239. createConnections();
  240. //make sure all connections are passable before creating borders
  241. for (auto it : zones)
  242. {
  243. it.second->createBorder(this);
  244. //we need info about all town types to evaluate dwellings and pandoras with creatures properly
  245. it.second->initTownType(this);
  246. }
  247. std::vector<CRmgTemplateZone*> treasureZones;
  248. for (auto it : zones)
  249. {
  250. it.second->fill(this);
  251. if (it.second->getType() == ETemplateZoneType::TREASURE)
  252. treasureZones.push_back(it.second);
  253. }
  254. //set apriopriate free/occupied tiles, including blocked underground rock
  255. createObstaclesCommon1();
  256. //set back original terrain for underground zones
  257. for (auto it : zones)
  258. it.second->createObstacles1(this);
  259. createObstaclesCommon2();
  260. //place actual obstacles matching zone terrain
  261. for (auto it : zones)
  262. {
  263. it.second->createObstacles2(this);
  264. }
  265. #define PRINT_MAP_BEFORE_ROADS true
  266. if (PRINT_MAP_BEFORE_ROADS) //enable to debug
  267. {
  268. std::ofstream out("road debug");
  269. int levels = map->twoLevel ? 2 : 1;
  270. int width = map->width;
  271. int height = map->height;
  272. for (int k = 0; k < levels; k++)
  273. {
  274. for (int j = 0; j<height; j++)
  275. {
  276. for (int i = 0; i<width; i++)
  277. {
  278. char t = '?';
  279. switch (getTile(int3(i, j, k)).getTileType())
  280. {
  281. case ETileType::FREE:
  282. t = ' '; break;
  283. case ETileType::BLOCKED:
  284. t = '#'; break;
  285. case ETileType::POSSIBLE:
  286. t = '-'; break;
  287. case ETileType::USED:
  288. t = 'O'; break;
  289. }
  290. out << t;
  291. }
  292. out << std::endl;
  293. }
  294. out << std::endl;
  295. }
  296. out << std::endl;
  297. }
  298. for (auto it : zones)
  299. {
  300. it.second->connectRoads(this); //draw roads after everything else has been placed
  301. }
  302. //find place for Grail
  303. if (treasureZones.empty())
  304. {
  305. for (auto it : zones)
  306. treasureZones.push_back(it.second);
  307. }
  308. auto grailZone = *RandomGeneratorUtil::nextItem(treasureZones, rand);
  309. map->grailPos = *RandomGeneratorUtil::nextItem(*grailZone->getFreePaths(), rand);
  310. logGlobal->infoStream() << "Zones filled successfully";
  311. }
  312. void CMapGenerator::createObstaclesCommon1()
  313. {
  314. if (map->twoLevel) //underground
  315. {
  316. //negative approach - create rock tiles first, then make sure all accessible tiles have no rock
  317. std::vector<int3> rockTiles;
  318. for (int x = 0; x < map->width; x++)
  319. {
  320. for (int y = 0; y < map->height; y++)
  321. {
  322. int3 tile(x, y, 1);
  323. if (shouldBeBlocked(tile))
  324. {
  325. rockTiles.push_back(tile);
  326. }
  327. }
  328. }
  329. editManager->getTerrainSelection().setSelection(rockTiles);
  330. editManager->drawTerrain(ETerrainType::ROCK, &rand);
  331. }
  332. }
  333. void CMapGenerator::createObstaclesCommon2()
  334. {
  335. if (map->twoLevel)
  336. {
  337. //finally mark rock tiles as occupied, spawn no obstacles there
  338. for (int x = 0; x < map->width; x++)
  339. {
  340. for (int y = 0; y < map->height; y++)
  341. {
  342. int3 tile(x, y, 1);
  343. if (map->getTile(tile).terType == ETerrainType::ROCK)
  344. {
  345. setOccupied(tile, ETileType::USED);
  346. }
  347. }
  348. }
  349. }
  350. //tighten obstacles to improve visuals
  351. for (int i = 0; i < 3; ++i)
  352. {
  353. int blockedTiles = 0;
  354. int freeTiles = 0;
  355. for (int z = 0; z < (map->twoLevel ? 2 : 1); z++)
  356. {
  357. for (int x = 0; x < map->width; x++)
  358. {
  359. for (int y = 0; y < map->height; y++)
  360. {
  361. int3 tile(x, y, z);
  362. if (!isPossible(tile)) //only possible tiles can change
  363. continue;
  364. int blockedNeighbours = 0;
  365. int freeNeighbours = 0;
  366. foreach_neighbour(tile, [this, &blockedNeighbours, &freeNeighbours](int3 &pos)
  367. {
  368. if (this->isBlocked(pos))
  369. blockedNeighbours++;
  370. if (this->isFree(pos))
  371. freeNeighbours++;
  372. });
  373. if (blockedNeighbours > 4)
  374. {
  375. setOccupied(tile, ETileType::BLOCKED);
  376. blockedTiles++;
  377. }
  378. else if (freeNeighbours > 4)
  379. {
  380. setOccupied(tile, ETileType::FREE);
  381. freeTiles++;
  382. }
  383. }
  384. }
  385. }
  386. logGlobal->traceStream() << boost::format("Set %d tiles to BLOCKED and %d tiles to FREE") % blockedTiles % freeTiles;
  387. }
  388. }
  389. void CMapGenerator::findZonesForQuestArts()
  390. {
  391. //we want to place arties in zones that were not yet filled (higher index)
  392. for (auto connection : mapGenOptions->getMapTemplate()->getConnections())
  393. {
  394. auto zoneA = connection.getZoneA();
  395. auto zoneB = connection.getZoneB();
  396. if (zoneA->getId() > zoneB->getId())
  397. {
  398. zoneB->setQuestArtZone(zoneA);
  399. }
  400. else if (zoneA->getId() < zoneB->getId())
  401. {
  402. zoneA->setQuestArtZone(zoneB);
  403. }
  404. }
  405. }
  406. void CMapGenerator::createConnections()
  407. {
  408. for (auto connection : mapGenOptions->getMapTemplate()->getConnections())
  409. {
  410. auto zoneA = connection.getZoneA();
  411. auto zoneB = connection.getZoneB();
  412. //rearrange tiles in random order
  413. auto tilesCopy = zoneA->getTileInfo();
  414. std::vector<int3> tiles(tilesCopy.begin(), tilesCopy.end());
  415. int3 guardPos(-1,-1,-1);
  416. auto otherZoneTiles = zoneB->getTileInfo();
  417. int3 posA = zoneA->getPos();
  418. int3 posB = zoneB->getPos();
  419. auto zoneAid = zoneA->getId();
  420. auto zoneBid = zoneB->getId();
  421. if (posA.z == posB.z)
  422. {
  423. std::vector<int3> middleTiles;
  424. for (auto tile : tilesCopy)
  425. {
  426. if (isBlocked(tile)) //tiles may be occupied by subterranean gates already placed
  427. continue;
  428. foreachDirectNeighbour (tile, [&guardPos, tile, &otherZoneTiles, &middleTiles, this, zoneBid](int3 &pos) //must be direct since paths also also generated between direct neighbours
  429. {
  430. if (getZoneID(pos) == zoneBid)
  431. middleTiles.push_back(tile);
  432. });
  433. }
  434. //find tiles with minimum manhattan distance from center of the mass of zone border
  435. size_t tilesCount = middleTiles.size() ? middleTiles.size() : 1;
  436. int3 middleTile = std::accumulate(middleTiles.begin(), middleTiles.end(), int3(0, 0, 0));
  437. middleTile.x /= tilesCount;
  438. middleTile.y /= tilesCount;
  439. middleTile.z /= tilesCount; //TODO: implement division operator for int3?
  440. boost::sort(middleTiles, [middleTile](const int3 &lhs, const int3 &rhs) -> bool
  441. {
  442. //choose tiles with both corrdinates in the middle
  443. return lhs.mandist2d(middleTile) < rhs.mandist2d(middleTile);
  444. });
  445. //remove 1/4 tiles from each side - path should cross zone borders at smooth angle
  446. size_t removedCount = tilesCount / 4; //rounded down
  447. middleTiles.erase(middleTiles.end() - removedCount, middleTiles.end());
  448. middleTiles.erase(middleTiles.begin(), middleTiles.begin() + removedCount);
  449. RandomGeneratorUtil::randomShuffle(middleTiles, rand);
  450. for (auto tile : middleTiles)
  451. {
  452. guardPos = tile;
  453. if (guardPos.valid())
  454. {
  455. setOccupied(guardPos, ETileType::FREE); //just in case monster is too weak to spawn
  456. zoneA->addMonster(this, guardPos, connection.getGuardStrength(), false, true);
  457. //zones can make paths only in their own area
  458. zoneA->crunchPath(this, guardPos, posA, true, zoneA->getFreePaths()); //make connection towards our zone center
  459. zoneB->crunchPath(this, guardPos, posB, true, zoneB->getFreePaths()); //make connection towards other zone center
  460. zoneA->addRoadNode(guardPos);
  461. zoneB->addRoadNode(guardPos);
  462. break; //we're done with this connection
  463. }
  464. }
  465. }
  466. else //create subterranean gates between two zones
  467. {
  468. //find common tiles for both zones
  469. std::vector<int3> commonTiles;
  470. auto tileSetA = zoneA->getTileInfo(),
  471. tileSetB = zoneB->getTileInfo();
  472. std::vector<int3> tilesA (tileSetA.begin(), tileSetA.end()),
  473. tilesB (tileSetB.begin(), tileSetB.end());
  474. boost::sort(tilesA),
  475. boost::sort(tilesB);
  476. boost::set_intersection(tilesA, tilesB, std::back_inserter(commonTiles), [](const int3 &lhs, const int3 &rhs) -> bool
  477. {
  478. //ignore z coordinate
  479. if (lhs.x < rhs.x)
  480. return true;
  481. else
  482. return lhs.y < rhs.y;
  483. });
  484. boost::sort(commonTiles, [posA, posB](const int3 &lhs, const int3 &rhs) -> bool
  485. {
  486. //choose tiles which are equidistant to zone centers
  487. return (std::abs<double>(posA.dist2dSQ(lhs) - posB.dist2dSQ(lhs)) < std::abs<double>((posA.dist2dSQ(rhs) - posB.dist2dSQ(rhs))));
  488. });
  489. auto sgt = VLC->objtypeh->getHandlerFor(Obj::SUBTERRANEAN_GATE, 0)->getTemplates().front();
  490. for (auto tile : commonTiles)
  491. {
  492. tile.z = posA.z;
  493. int3 otherTile = tile;
  494. otherTile.z = posB.z;
  495. float distanceFromA = posA.dist2d(tile);
  496. float distanceFromB = posB.dist2d(otherTile);
  497. if (distanceFromA > 5 && distanceFromB > 5)
  498. {
  499. //all neightbouring tiles also belong to zone
  500. if (getZoneID(tile) == zoneAid && getZoneID(otherTile) == zoneBid)
  501. {
  502. bool withinZone = true;
  503. foreach_neighbour (tile, [&withinZone, &tiles, zoneAid, this](int3 &pos)
  504. {
  505. if (getZoneID(pos) != zoneAid)
  506. withinZone = false;
  507. });
  508. foreach_neighbour (otherTile, [&withinZone, &otherZoneTiles, zoneBid, this](int3 &pos)
  509. {
  510. if (getZoneID(pos) != zoneBid)
  511. withinZone = false;
  512. });
  513. //make sure both gates has some free tiles below them
  514. if (zoneA->getAccessibleOffset(this, sgt, tile).valid() && zoneB->getAccessibleOffset(this, sgt, otherTile).valid())
  515. {
  516. if (withinZone)
  517. {
  518. zoneA->placeSubterraneanGate(this, tile, connection.getGuardStrength());
  519. zoneB->placeSubterraneanGate(this, otherTile, connection.getGuardStrength());
  520. guardPos = tile; //just any valid value
  521. break; //we're done
  522. }
  523. }
  524. }
  525. }
  526. }
  527. }
  528. if (!guardPos.valid())
  529. {
  530. auto factory = VLC->objtypeh->getHandlerFor(Obj::MONOLITH_TWO_WAY, getNextMonlithIndex());
  531. auto teleport1 = factory->create(ObjectTemplate());
  532. auto teleport2 = factory->create(ObjectTemplate());
  533. zoneA->addRequiredObject (teleport1, connection.getGuardStrength());
  534. zoneB->addRequiredObject (teleport2, connection.getGuardStrength());
  535. }
  536. }
  537. }
  538. void CMapGenerator::addHeaderInfo()
  539. {
  540. map->version = EMapFormat::VCMI;
  541. map->width = mapGenOptions->getWidth();
  542. map->height = mapGenOptions->getHeight();
  543. map->twoLevel = mapGenOptions->getHasTwoLevels();
  544. map->name = VLC->generaltexth->allTexts[740];
  545. map->description = getMapDescription();
  546. map->difficulty = 1;
  547. addPlayerInfo();
  548. }
  549. void CMapGenerator::checkIsOnMap(const int3& tile) const
  550. {
  551. if (!map->isInTheMap(tile))
  552. throw rmgException(boost::to_string(boost::format("Tile %s is outside the map") % tile));
  553. }
  554. std::map<TRmgTemplateZoneId, CRmgTemplateZone*> CMapGenerator::getZones() const
  555. {
  556. return zones;
  557. }
  558. bool CMapGenerator::isBlocked(const int3 &tile) const
  559. {
  560. checkIsOnMap(tile);
  561. return tiles[tile.x][tile.y][tile.z].isBlocked();
  562. }
  563. bool CMapGenerator::shouldBeBlocked(const int3 &tile) const
  564. {
  565. checkIsOnMap(tile);
  566. return tiles[tile.x][tile.y][tile.z].shouldBeBlocked();
  567. }
  568. bool CMapGenerator::isPossible(const int3 &tile) const
  569. {
  570. checkIsOnMap(tile);
  571. return tiles[tile.x][tile.y][tile.z].isPossible();
  572. }
  573. bool CMapGenerator::isFree(const int3 &tile) const
  574. {
  575. checkIsOnMap(tile);
  576. return tiles[tile.x][tile.y][tile.z].isFree();
  577. }
  578. bool CMapGenerator::isUsed(const int3 &tile) const
  579. {
  580. checkIsOnMap(tile);
  581. return tiles[tile.x][tile.y][tile.z].isUsed();
  582. }
  583. bool CMapGenerator::isRoad(const int3& tile) const
  584. {
  585. checkIsOnMap(tile);
  586. return tiles[tile.x][tile.y][tile.z].isRoad();
  587. }
  588. void CMapGenerator::setOccupied(const int3 &tile, ETileType::ETileType state)
  589. {
  590. checkIsOnMap(tile);
  591. tiles[tile.x][tile.y][tile.z].setOccupied(state);
  592. }
  593. void CMapGenerator::setRoad(const int3& tile, ERoadType::ERoadType roadType)
  594. {
  595. checkIsOnMap(tile);
  596. tiles[tile.x][tile.y][tile.z].setRoadType(roadType);
  597. }
  598. CTileInfo CMapGenerator::getTile(const int3& tile) const
  599. {
  600. checkIsOnMap(tile);
  601. return tiles[tile.x][tile.y][tile.z];
  602. }
  603. TRmgTemplateZoneId CMapGenerator::getZoneID(const int3& tile) const
  604. {
  605. checkIsOnMap(tile);
  606. return zoneColouring[tile.z][tile.x][tile.y];
  607. }
  608. void CMapGenerator::setZoneID(const int3& tile, TRmgTemplateZoneId zid)
  609. {
  610. checkIsOnMap(tile);
  611. zoneColouring[tile.z][tile.x][tile.y] = zid;
  612. }
  613. bool CMapGenerator::isAllowedSpell(SpellID sid) const
  614. {
  615. assert(sid >= 0);
  616. if (sid < map->allowedSpell.size())
  617. {
  618. return map->allowedSpell[sid];
  619. }
  620. else
  621. return false;
  622. }
  623. void CMapGenerator::setNearestObjectDistance(int3 &tile, float value)
  624. {
  625. checkIsOnMap(tile);
  626. tiles[tile.x][tile.y][tile.z].setNearestObjectDistance(value);
  627. }
  628. float CMapGenerator::getNearestObjectDistance(const int3 &tile) const
  629. {
  630. checkIsOnMap(tile);
  631. return tiles[tile.x][tile.y][tile.z].getNearestObjectDistance();
  632. }
  633. int CMapGenerator::getNextMonlithIndex()
  634. {
  635. if (monolithIndex >= VLC->objtypeh->knownSubObjects(Obj::MONOLITH_TWO_WAY).size())
  636. {
  637. //logGlobal->errorStream() << boost::to_string(boost::format("RMG Error! There is no Monolith Two Way with index %d available!") % monolithIndex);
  638. //monolithIndex++;
  639. //return VLC->objtypeh->knownSubObjects(Obj::MONOLITH_TWO_WAY).size() - 1;
  640. //TODO: interrupt map generation and report error
  641. throw rmgException(boost::to_string(boost::format("There is no Monolith Two Way with index %d available!") % monolithIndex));
  642. }
  643. else
  644. return monolithIndex++;
  645. }
  646. int CMapGenerator::getPrisonsRemaning() const
  647. {
  648. return prisonsRemaining;
  649. }
  650. void CMapGenerator::decreasePrisonsRemaining()
  651. {
  652. prisonsRemaining = std::max (0, prisonsRemaining - 1);
  653. }
  654. std::vector<ArtifactID> CMapGenerator::getQuestArtsRemaning() const
  655. {
  656. return questArtifacts;
  657. }
  658. void CMapGenerator::banQuestArt(ArtifactID id)
  659. {
  660. map->allowedArtifact[id] = false;
  661. vstd::erase_if_present (questArtifacts, id);
  662. }
  663. void CMapGenerator::registerZone (TFaction faction)
  664. {
  665. zonesPerFaction[faction]++;
  666. zonesTotal++;
  667. }
  668. ui32 CMapGenerator::getZoneCount(TFaction faction)
  669. {
  670. return zonesPerFaction[faction];
  671. }
  672. ui32 CMapGenerator::getTotalZoneCount() const
  673. {
  674. return zonesTotal;
  675. }