CMapGenerator.cpp 19 KB

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