CMapGenerator.cpp 19 KB

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