CMapGenerator.cpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  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 "../CObjectHandler.h"
  8. #include "../CDefObjInfoHandler.h"
  9. #include "../CTownHandler.h"
  10. #include "../StringConstants.h"
  11. #include "../filesystem/Filesystem.h"
  12. #include "CRmgTemplate.h"
  13. #include "CRmgTemplateZone.h"
  14. #include "CZonePlacer.h"
  15. void CMapGenerator::foreach_neighbour(const int3 &pos, std::function<void(int3& pos)> foo)
  16. {
  17. for(const int3 &dir : dirs)
  18. {
  19. int3 n = pos + dir;
  20. if(map->isInTheMap(n))
  21. foo(pos+dir);
  22. }
  23. }
  24. CMapGenerator::CMapGenerator(shared_ptr<CMapGenOptions> mapGenOptions, int randomSeed /*= std::time(nullptr)*/) :
  25. mapGenOptions(mapGenOptions), randomSeed(randomSeed), monolithIndex(0)
  26. {
  27. rand.setSeed(randomSeed);
  28. }
  29. void CMapGenerator::initTiles()
  30. {
  31. map->initTerrain();
  32. int width = map->width;
  33. int height = map->height;
  34. int level = map->twoLevel ? 2 : 1;
  35. tiles = new CTileInfo**[width];
  36. for (int i = 0; i < width; ++i)
  37. {
  38. tiles[i] = new CTileInfo*[height];
  39. for (int j = 0; j < height; ++j)
  40. {
  41. tiles[i][j] = new CTileInfo[level];
  42. }
  43. }
  44. }
  45. CMapGenerator::~CMapGenerator()
  46. {
  47. //FIXME: what if map is not present anymore?
  48. if (tiles && map)
  49. {
  50. for (int i=0; i < map->width; i++)
  51. {
  52. for(int j=0; j < map->height; j++)
  53. {
  54. delete [] tiles[i][j];
  55. }
  56. delete [] tiles[i];
  57. }
  58. delete [] tiles;
  59. }
  60. }
  61. std::unique_ptr<CMap> CMapGenerator::generate()
  62. {
  63. mapGenOptions->finalize(rand);
  64. map = make_unique<CMap>();
  65. editManager = map->getEditManager();
  66. try
  67. {
  68. editManager->getUndoManager().setUndoRedoLimit(0);
  69. addHeaderInfo();
  70. initTiles();
  71. genZones();
  72. map->calculateGuardingGreaturePositions(); //clear map so that all tiles are unguarded
  73. fillZones();
  74. }
  75. catch (rmgException &e)
  76. {
  77. logGlobal->errorStream() << "Random map generation received exception: " << e.what();
  78. }
  79. return std::move(map);
  80. }
  81. std::string CMapGenerator::getMapDescription() const
  82. {
  83. const std::string waterContentStr[3] = { "none", "normal", "islands" };
  84. const std::string monsterStrengthStr[3] = { "weak", "normal", "strong" };
  85. std::stringstream ss;
  86. ss << boost::str(boost::format(std::string("Map created by the Random Map Generator.\nTemplate was %s, Random seed was %d, size %dx%d") +
  87. ", levels %s, humans %d, computers %d, water %s, monster %s, second expansion map") % mapGenOptions->getMapTemplate()->getName() %
  88. randomSeed % map->width % map->height % (map->twoLevel ? "2" : "1") % static_cast<int>(mapGenOptions->getPlayerCount()) %
  89. static_cast<int>(mapGenOptions->getCompOnlyPlayerCount()) % waterContentStr[mapGenOptions->getWaterContent()] %
  90. monsterStrengthStr[mapGenOptions->getMonsterStrength()]);
  91. for(const auto & pair : mapGenOptions->getPlayersSettings())
  92. {
  93. const auto & pSettings = pair.second;
  94. if(pSettings.getPlayerType() == EPlayerType::HUMAN)
  95. {
  96. ss << ", " << GameConstants::PLAYER_COLOR_NAMES[pSettings.getColor().getNum()] << " is human";
  97. }
  98. if(pSettings.getStartingTown() != CMapGenOptions::CPlayerSettings::RANDOM_TOWN)
  99. {
  100. ss << ", " << GameConstants::PLAYER_COLOR_NAMES[pSettings.getColor().getNum()]
  101. << " town choice is " << VLC->townh->factions[pSettings.getStartingTown()]->name;
  102. }
  103. }
  104. return ss.str();
  105. }
  106. void CMapGenerator::addPlayerInfo()
  107. {
  108. // Calculate which team numbers exist
  109. std::array<std::list<int>, 2> teamNumbers; // 0= cpu/human, 1= cpu only
  110. int teamOffset = 0;
  111. for(int i = 0; i < 2; ++i)
  112. {
  113. int playerCount = i == 0 ? mapGenOptions->getPlayerCount() : mapGenOptions->getCompOnlyPlayerCount();
  114. int teamCount = i == 0 ? mapGenOptions->getTeamCount() : mapGenOptions->getCompOnlyTeamCount();
  115. if(playerCount == 0)
  116. {
  117. continue;
  118. }
  119. int playersPerTeam = playerCount /
  120. (teamCount == 0 ? playerCount : teamCount);
  121. int teamCountNorm = teamCount;
  122. if(teamCountNorm == 0)
  123. {
  124. teamCountNorm = playerCount;
  125. }
  126. for(int j = 0; j < teamCountNorm; ++j)
  127. {
  128. for(int k = 0; k < playersPerTeam; ++k)
  129. {
  130. teamNumbers[i].push_back(j + teamOffset);
  131. }
  132. }
  133. for(int j = 0; j < playerCount - teamCountNorm * playersPerTeam; ++j)
  134. {
  135. teamNumbers[i].push_back(j + teamOffset);
  136. }
  137. teamOffset += teamCountNorm;
  138. }
  139. // Team numbers are assigned randomly to every player
  140. for(const auto & pair : mapGenOptions->getPlayersSettings())
  141. {
  142. const auto & pSettings = pair.second;
  143. PlayerInfo player;
  144. player.canComputerPlay = true;
  145. int j = pSettings.getPlayerType() == EPlayerType::COMP_ONLY ? 1 : 0;
  146. if(j == 0)
  147. {
  148. player.canHumanPlay = true;
  149. }
  150. auto itTeam = RandomGeneratorUtil::nextItem(teamNumbers[j], rand);
  151. player.team = TeamID(*itTeam);
  152. teamNumbers[j].erase(itTeam);
  153. map->players[pSettings.getColor().getNum()] = player;
  154. }
  155. map->howManyTeams = (mapGenOptions->getTeamCount() == 0 ? mapGenOptions->getPlayerCount() : mapGenOptions->getTeamCount())
  156. + (mapGenOptions->getCompOnlyTeamCount() == 0 ? mapGenOptions->getCompOnlyPlayerCount() : mapGenOptions->getCompOnlyTeamCount());
  157. }
  158. void CMapGenerator::genZones()
  159. {
  160. editManager->clearTerrain(&rand);
  161. editManager->getTerrainSelection().selectRange(MapRect(int3(0, 0, 0), mapGenOptions->getWidth(), mapGenOptions->getHeight()));
  162. editManager->drawTerrain(ETerrainType::GRASS, &rand);
  163. auto pcnt = mapGenOptions->getPlayerCount();
  164. auto w = mapGenOptions->getWidth();
  165. auto h = mapGenOptions->getHeight();
  166. auto tmpl = mapGenOptions->getMapTemplate();
  167. zones = tmpl->getZones(); //copy from template (refactor?)
  168. int player_per_side = zones.size() > 4 ? 3 : 2;
  169. int zones_cnt = zones.size() > 4 ? 9 : 4;
  170. logGlobal->infoStream() << boost::format("Map size %d %d, players per side %d") % w % h % player_per_side;
  171. CZonePlacer placer(this);
  172. placer.placeZones(mapGenOptions, &rand);
  173. placer.assignZones(mapGenOptions);
  174. int i = 0;
  175. int part_w = w/player_per_side;
  176. int part_h = h/player_per_side;
  177. for(auto const it : zones)
  178. {
  179. CRmgTemplateZone * zone = it.second;
  180. zone->setType(i < pcnt ? ETemplateZoneType::PLAYER_START : ETemplateZoneType::TREASURE);
  181. this->zones[it.first] = zone;
  182. ++i;
  183. }
  184. logGlobal->infoStream() << "Zones generated successfully";
  185. }
  186. void CMapGenerator::fillZones()
  187. {
  188. logGlobal->infoStream() << "Started filling zones";
  189. for (auto it : zones)
  190. {
  191. it.second->createConnections(this);
  192. }
  193. for (auto it : zones)
  194. {
  195. //make sure all connections are passable before creating borders
  196. it.second->createBorder(this);
  197. it.second->fill(this);
  198. }
  199. logGlobal->infoStream() << "Zones filled successfully";
  200. }
  201. void CMapGenerator::addHeaderInfo()
  202. {
  203. map->version = EMapFormat::SOD;
  204. map->width = mapGenOptions->getWidth();
  205. map->height = mapGenOptions->getHeight();
  206. map->twoLevel = mapGenOptions->getHasTwoLevels();
  207. map->name = VLC->generaltexth->allTexts[740];
  208. map->description = getMapDescription();
  209. map->difficulty = 1;
  210. addPlayerInfo();
  211. }
  212. std::map<TRmgTemplateZoneId, CRmgTemplateZone*> CMapGenerator::getZones() const
  213. {
  214. return zones;
  215. }
  216. bool CMapGenerator::isBlocked(const int3 &tile) const
  217. {
  218. if (!map->isInTheMap(tile))
  219. throw rmgException(boost::to_string(boost::format("Tile %s is outside the map") % tile));
  220. return tiles[tile.x][tile.y][tile.z].isBlocked();
  221. }
  222. bool CMapGenerator::shouldBeBlocked(const int3 &tile) const
  223. {
  224. if (!map->isInTheMap(tile))
  225. throw rmgException(boost::to_string(boost::format("Tile %s is outside the map") % tile));
  226. return tiles[tile.x][tile.y][tile.z].shouldBeBlocked();
  227. }
  228. bool CMapGenerator::isPossible(const int3 &tile) const
  229. {
  230. if (!map->isInTheMap(tile))
  231. throw rmgException(boost::to_string(boost::format("Tile %s is outside the map") % tile));
  232. return tiles[tile.x][tile.y][tile.z].isPossible();
  233. }
  234. bool CMapGenerator::isFree(const int3 &tile) const
  235. {
  236. if (!map->isInTheMap(tile))
  237. throw rmgException(boost::to_string(boost::format("Tile %s is outside the map") % tile));
  238. return tiles[tile.x][tile.y][tile.z].isFree();
  239. }
  240. void CMapGenerator::setOccupied(int3 &tile, ETileType::ETileType state)
  241. {
  242. if (!map->isInTheMap(tile))
  243. throw rmgException(boost::to_string(boost::format("Tile %s is outside the map") % tile));
  244. tiles[tile.x][tile.y][tile.z].setOccupied(state);
  245. }
  246. CTileInfo CMapGenerator::getTile(int3 tile) const
  247. {
  248. if (!map->isInTheMap(tile))
  249. throw rmgException(boost::to_string(boost::format("Tile %s is outside the map") % tile));
  250. return tiles[tile.x][tile.y][tile.z];
  251. }
  252. void CMapGenerator::setNearestObjectDistance(int3 &tile, int value)
  253. {
  254. if (!map->isInTheMap(tile))
  255. throw rmgException(boost::to_string(boost::format("Tile %s is outside the map") % tile));
  256. tiles[tile.x][tile.y][tile.z].setNearestObjectDistance(value);
  257. }
  258. int CMapGenerator::getNearestObjectDistance(const int3 &tile) const
  259. {
  260. if (!map->isInTheMap(tile))
  261. throw rmgException(boost::to_string(boost::format("Tile %s is outside the map") % tile));
  262. return tiles[tile.x][tile.y][tile.z].getNearestObjectDistance();
  263. }
  264. int CMapGenerator::getNextMonlithIndex()
  265. {
  266. return monolithIndex++;
  267. }