CMapGenerator.cpp 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. CMapGenerator::CMapGenerator(shared_ptr<CMapGenOptions> mapGenOptions, int randomSeed /*= std::time(nullptr)*/) :
  15. mapGenOptions(mapGenOptions), randomSeed(randomSeed)
  16. {
  17. rand.setSeed(randomSeed);
  18. }
  19. CMapGenerator::~CMapGenerator()
  20. {
  21. }
  22. std::unique_ptr<CMap> CMapGenerator::generate()
  23. {
  24. mapGenOptions->finalize(rand);
  25. map = make_unique<CMap>();
  26. editManager = map->getEditManager();
  27. try
  28. {
  29. editManager->getUndoManager().setUndoRedoLimit(0);
  30. addHeaderInfo();
  31. genZones();
  32. fillZones();
  33. }
  34. catch (rmgException &e)
  35. {
  36. logGlobal->infoStream() << "Random map generation received exception: " << e.what();
  37. }
  38. return std::move(map);
  39. }
  40. std::string CMapGenerator::getMapDescription() const
  41. {
  42. const std::string waterContentStr[3] = { "none", "normal", "islands" };
  43. const std::string monsterStrengthStr[3] = { "weak", "normal", "strong" };
  44. std::stringstream ss;
  45. ss << boost::str(boost::format(std::string("Map created by the Random Map Generator.\nTemplate was %s, Random seed was %d, size %dx%d") +
  46. ", levels %s, humans %d, computers %d, water %s, monster %s, second expansion map") % mapGenOptions->getMapTemplate()->getName() %
  47. randomSeed % map->width % map->height % (map->twoLevel ? "2" : "1") % static_cast<int>(mapGenOptions->getPlayerCount()) %
  48. static_cast<int>(mapGenOptions->getCompOnlyPlayerCount()) % waterContentStr[mapGenOptions->getWaterContent()] %
  49. monsterStrengthStr[mapGenOptions->getMonsterStrength()]);
  50. for(const auto & pair : mapGenOptions->getPlayersSettings())
  51. {
  52. const auto & pSettings = pair.second;
  53. if(pSettings.getPlayerType() == EPlayerType::HUMAN)
  54. {
  55. ss << ", " << GameConstants::PLAYER_COLOR_NAMES[pSettings.getColor().getNum()] << " is human";
  56. }
  57. if(pSettings.getStartingTown() != CMapGenOptions::CPlayerSettings::RANDOM_TOWN)
  58. {
  59. ss << ", " << GameConstants::PLAYER_COLOR_NAMES[pSettings.getColor().getNum()]
  60. << " town choice is " << VLC->townh->factions[pSettings.getStartingTown()]->name;
  61. }
  62. }
  63. return ss.str();
  64. }
  65. void CMapGenerator::addPlayerInfo()
  66. {
  67. // Calculate which team numbers exist
  68. std::array<std::list<int>, 2> teamNumbers; // 0= cpu/human, 1= cpu only
  69. int teamOffset = 0;
  70. for(int i = 0; i < 2; ++i)
  71. {
  72. int playerCount = i == 0 ? mapGenOptions->getPlayerCount() : mapGenOptions->getCompOnlyPlayerCount();
  73. int teamCount = i == 0 ? mapGenOptions->getTeamCount() : mapGenOptions->getCompOnlyTeamCount();
  74. if(playerCount == 0)
  75. {
  76. continue;
  77. }
  78. int playersPerTeam = playerCount /
  79. (teamCount == 0 ? playerCount : teamCount);
  80. int teamCountNorm = teamCount;
  81. if(teamCountNorm == 0)
  82. {
  83. teamCountNorm = playerCount;
  84. }
  85. for(int j = 0; j < teamCountNorm; ++j)
  86. {
  87. for(int k = 0; k < playersPerTeam; ++k)
  88. {
  89. teamNumbers[i].push_back(j + teamOffset);
  90. }
  91. }
  92. for(int j = 0; j < playerCount - teamCountNorm * playersPerTeam; ++j)
  93. {
  94. teamNumbers[i].push_back(j + teamOffset);
  95. }
  96. teamOffset += teamCountNorm;
  97. }
  98. // Team numbers are assigned randomly to every player
  99. for(const auto & pair : mapGenOptions->getPlayersSettings())
  100. {
  101. const auto & pSettings = pair.second;
  102. PlayerInfo player;
  103. player.canComputerPlay = true;
  104. int j = pSettings.getPlayerType() == EPlayerType::COMP_ONLY ? 1 : 0;
  105. if(j == 0)
  106. {
  107. player.canHumanPlay = true;
  108. }
  109. auto itTeam = std::next(teamNumbers[j].begin(), rand.nextInt (teamNumbers[j].size()));
  110. player.team = TeamID(*itTeam);
  111. teamNumbers[j].erase(itTeam);
  112. map->players[pSettings.getColor().getNum()] = player;
  113. }
  114. map->howManyTeams = (mapGenOptions->getTeamCount() == 0 ? mapGenOptions->getPlayerCount() : mapGenOptions->getTeamCount())
  115. + (mapGenOptions->getCompOnlyTeamCount() == 0 ? mapGenOptions->getCompOnlyPlayerCount() : mapGenOptions->getCompOnlyTeamCount());
  116. }
  117. void CMapGenerator::genZones()
  118. {
  119. map->initTerrain();
  120. editManager->clearTerrain(&rand);
  121. editManager->getTerrainSelection().selectRange(MapRect(int3(0, 0, 0), mapGenOptions->getWidth(), mapGenOptions->getHeight()));
  122. editManager->drawTerrain(ETerrainType::GRASS, &rand);
  123. auto pcnt = mapGenOptions->getPlayerCount();
  124. auto w = mapGenOptions->getWidth();
  125. auto h = mapGenOptions->getHeight();
  126. auto tmpl = mapGenOptions->getMapTemplate();
  127. auto zones = tmpl->getZones();
  128. int player_per_side = zones.size() > 4 ? 3 : 2;
  129. int zones_cnt = zones.size() > 4 ? 9 : 4;
  130. logGlobal->infoStream() << boost::format("Map size %d %d, players per side %d") % w % h % player_per_side;
  131. int i = 0;
  132. int part_w = w/player_per_side;
  133. int part_h = h/player_per_side;
  134. for(auto const it : zones)
  135. {
  136. CRmgTemplateZone * zone = it.second;
  137. std::vector<int3> shape;
  138. int left = part_w*(i%player_per_side);
  139. int top = part_h*(i/player_per_side);
  140. shape.push_back(int3(left, top, 0));
  141. shape.push_back(int3(left + part_w, top, 0));
  142. shape.push_back(int3(left + part_w, top + part_h, 0));
  143. shape.push_back(int3(left, top + part_h, 0));
  144. zone->setShape(shape);
  145. zone->setType(i < pcnt ? ETemplateZoneType::PLAYER_START : ETemplateZoneType::TREASURE);
  146. this->zones[it.first] = zone;
  147. ++i;
  148. }
  149. logGlobal->infoStream() << "Zones generated successfully";
  150. }
  151. void CMapGenerator::fillZones()
  152. {
  153. logGlobal->infoStream() << "Started filling zones";
  154. for(auto it : zones)
  155. {
  156. it.second->fill(this);
  157. }
  158. logGlobal->infoStream() << "Zones filled successfully";
  159. }
  160. void CMapGenerator::addHeaderInfo()
  161. {
  162. map->version = EMapFormat::SOD;
  163. map->width = mapGenOptions->getWidth();
  164. map->height = mapGenOptions->getHeight();
  165. map->twoLevel = mapGenOptions->getHasTwoLevels();
  166. map->name = VLC->generaltexth->allTexts[740];
  167. map->description = getMapDescription();
  168. map->difficulty = 1;
  169. addPlayerInfo();
  170. }