2
0

CMapGenOptions.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  1. /*
  2. * CMapGenOptions.cpp, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #include "StdInc.h"
  11. #include "CMapGenOptions.h"
  12. #include "../mapping/CMap.h"
  13. #include "CRmgTemplateStorage.h"
  14. #include "CRmgTemplate.h"
  15. #include "../VCMI_Lib.h"
  16. #include "../CTownHandler.h"
  17. CMapGenOptions::CMapGenOptions()
  18. : width(CMapHeader::MAP_SIZE_MIDDLE), height(CMapHeader::MAP_SIZE_MIDDLE), hasTwoLevels(true),
  19. playerCount(RANDOM_SIZE), teamCount(RANDOM_SIZE), compOnlyPlayerCount(RANDOM_SIZE), compOnlyTeamCount(RANDOM_SIZE), humanPlayersCount(0),
  20. waterContent(EWaterContent::RANDOM), monsterStrength(EMonsterStrength::RANDOM), mapTemplate(nullptr)
  21. {
  22. resetPlayersMap();
  23. }
  24. si32 CMapGenOptions::getWidth() const
  25. {
  26. return width;
  27. }
  28. void CMapGenOptions::setWidth(si32 value)
  29. {
  30. assert(value >= 1);
  31. width = value;
  32. }
  33. si32 CMapGenOptions::getHeight() const
  34. {
  35. return height;
  36. }
  37. void CMapGenOptions::setHeight(si32 value)
  38. {
  39. assert(value >= 1);
  40. height = value;
  41. }
  42. bool CMapGenOptions::getHasTwoLevels() const
  43. {
  44. return hasTwoLevels;
  45. }
  46. void CMapGenOptions::setHasTwoLevels(bool value)
  47. {
  48. hasTwoLevels = value;
  49. }
  50. si8 CMapGenOptions::getPlayerCount() const
  51. {
  52. return playerCount;
  53. }
  54. void CMapGenOptions::setPlayerCount(si8 value)
  55. {
  56. assert((value >= 1 && value <= PlayerColor::PLAYER_LIMIT_I) || value == RANDOM_SIZE);
  57. playerCount = value;
  58. auto possibleCompPlayersCount = value;
  59. if (compOnlyPlayerCount > possibleCompPlayersCount)
  60. setCompOnlyPlayerCount(possibleCompPlayersCount);
  61. if (getPlayerCount() != RANDOM_SIZE)
  62. {
  63. if (getCompOnlyPlayerCount() != RANDOM_SIZE)
  64. humanPlayersCount = getPlayerCount() - getCompOnlyPlayerCount();
  65. else
  66. humanPlayersCount = getPlayerCount();
  67. }
  68. resetPlayersMap();
  69. }
  70. si8 CMapGenOptions::getHumanOnlyPlayerCount() const
  71. {
  72. return humanPlayersCount;
  73. }
  74. si8 CMapGenOptions::getTeamCount() const
  75. {
  76. return teamCount;
  77. }
  78. void CMapGenOptions::setTeamCount(si8 value)
  79. {
  80. assert(getPlayerCount() == RANDOM_SIZE || (value >= 0 && value < getPlayerCount()) || value == RANDOM_SIZE);
  81. teamCount = value;
  82. }
  83. si8 CMapGenOptions::getCompOnlyPlayerCount() const
  84. {
  85. return compOnlyPlayerCount;
  86. }
  87. void CMapGenOptions::setCompOnlyPlayerCount(si8 value)
  88. {
  89. assert(value == RANDOM_SIZE || (getPlayerCount() == RANDOM_SIZE || (value >= 0 && value <= getPlayerCount())));
  90. compOnlyPlayerCount = value;
  91. if (getPlayerCount() != RANDOM_SIZE && getCompOnlyPlayerCount() != RANDOM_SIZE)
  92. humanPlayersCount = getPlayerCount() - getCompOnlyPlayerCount();
  93. resetPlayersMap();
  94. }
  95. si8 CMapGenOptions::getCompOnlyTeamCount() const
  96. {
  97. return compOnlyTeamCount;
  98. }
  99. void CMapGenOptions::setCompOnlyTeamCount(si8 value)
  100. {
  101. assert(value == RANDOM_SIZE || compOnlyPlayerCount == RANDOM_SIZE || (value >= 0 && value <= std::max(compOnlyPlayerCount - 1, 0)));
  102. compOnlyTeamCount = value;
  103. }
  104. EWaterContent::EWaterContent CMapGenOptions::getWaterContent() const
  105. {
  106. return waterContent;
  107. }
  108. void CMapGenOptions::setWaterContent(EWaterContent::EWaterContent value)
  109. {
  110. waterContent = value;
  111. }
  112. EMonsterStrength::EMonsterStrength CMapGenOptions::getMonsterStrength() const
  113. {
  114. return monsterStrength;
  115. }
  116. void CMapGenOptions::setMonsterStrength(EMonsterStrength::EMonsterStrength value)
  117. {
  118. monsterStrength = value;
  119. }
  120. void CMapGenOptions::resetPlayersMap()
  121. {
  122. std::map<PlayerColor, TFaction> rememberTownTypes;
  123. for (auto p : players)
  124. {
  125. auto town = p.second.getStartingTown();
  126. if (town != RANDOM_SIZE)
  127. rememberTownTypes[p.first] = town;
  128. }
  129. players.clear();
  130. int realPlayersCnt = humanPlayersCount;
  131. int realCompOnlyPlayersCnt = (compOnlyPlayerCount == RANDOM_SIZE) ? (PlayerColor::PLAYER_LIMIT_I - realPlayersCnt) : compOnlyPlayerCount;
  132. int totalPlayersLimit = realPlayersCnt + realCompOnlyPlayersCnt;
  133. if (getPlayerCount() == RANDOM_SIZE || compOnlyPlayerCount == RANDOM_SIZE)
  134. totalPlayersLimit = static_cast<int>(PlayerColor::PLAYER_LIMIT_I);
  135. //FIXME: what happens with human players here?
  136. for(int color = 0; color < totalPlayersLimit; ++color)
  137. {
  138. CPlayerSettings player;
  139. auto pc = PlayerColor(color);
  140. player.setColor(pc);
  141. auto playerType = EPlayerType::AI;
  142. if (getPlayerCount() != RANDOM_SIZE && color < realPlayersCnt)
  143. {
  144. playerType = EPlayerType::HUMAN;
  145. }
  146. else if((getPlayerCount() != RANDOM_SIZE && color >= realPlayersCnt)
  147. || (compOnlyPlayerCount != RANDOM_SIZE && color >= (PlayerColor::PLAYER_LIMIT_I-compOnlyPlayerCount)))
  148. {
  149. playerType = EPlayerType::COMP_ONLY;
  150. }
  151. player.setPlayerType(playerType);
  152. players[pc] = player;
  153. if (vstd::contains(rememberTownTypes, pc))
  154. players[pc].setStartingTown(rememberTownTypes[pc]);
  155. }
  156. }
  157. const std::map<PlayerColor, CMapGenOptions::CPlayerSettings> & CMapGenOptions::getPlayersSettings() const
  158. {
  159. return players;
  160. }
  161. void CMapGenOptions::setStartingTownForPlayer(PlayerColor color, si32 town)
  162. {
  163. auto it = players.find(color);
  164. if(it == players.end()) assert(0);
  165. it->second.setStartingTown(town);
  166. }
  167. void CMapGenOptions::setPlayerTypeForStandardPlayer(PlayerColor color, EPlayerType::EPlayerType playerType)
  168. {
  169. assert(playerType != EPlayerType::COMP_ONLY);
  170. auto it = players.find(color);
  171. if(it == players.end()) assert(0);
  172. it->second.setPlayerType(playerType);
  173. }
  174. const CRmgTemplate * CMapGenOptions::getMapTemplate() const
  175. {
  176. return mapTemplate;
  177. }
  178. void CMapGenOptions::setMapTemplate(const CRmgTemplate * value)
  179. {
  180. mapTemplate = value;
  181. //TODO validate & adapt options according to template
  182. assert(0);
  183. }
  184. const std::map<std::string, CRmgTemplate *> & CMapGenOptions::getAvailableTemplates() const
  185. {
  186. return VLC->tplh->getTemplates();
  187. }
  188. void CMapGenOptions::finalize(CRandomGenerator & rand)
  189. {
  190. logGlobal->info("RMG settings: players %d, teams %d, computer players %d, computer teams %d, water %d, monsters %d",
  191. static_cast<int>(getPlayerCount()), static_cast<int>(getTeamCount()), static_cast<int>(getCompOnlyPlayerCount()),
  192. static_cast<int>(getCompOnlyTeamCount()), static_cast<int>(getWaterContent()), static_cast<int>(getMonsterStrength()));
  193. if(!mapTemplate)
  194. {
  195. mapTemplate = getPossibleTemplate(rand);
  196. }
  197. assert(mapTemplate);
  198. if (getPlayerCount() == RANDOM_SIZE)
  199. {
  200. auto possiblePlayers = mapTemplate->getPlayers().getNumbers();
  201. //ignore all non-randomized players, make sure these players will not be missing after roll
  202. possiblePlayers.erase(possiblePlayers.begin(), possiblePlayers.lower_bound(countHumanPlayers() + countCompOnlyPlayers()));
  203. assert(!possiblePlayers.empty());
  204. setPlayerCount (*RandomGeneratorUtil::nextItem(possiblePlayers, rand));
  205. updatePlayers();
  206. }
  207. if(teamCount == RANDOM_SIZE)
  208. {
  209. teamCount = rand.nextInt(getPlayerCount() - 1);
  210. if (teamCount == 1)
  211. teamCount = 0;
  212. }
  213. if(compOnlyPlayerCount == RANDOM_SIZE)
  214. {
  215. auto possiblePlayers = mapTemplate->getCpuPlayers().getNumbers();
  216. compOnlyPlayerCount = *RandomGeneratorUtil::nextItem(possiblePlayers, rand);
  217. updateCompOnlyPlayers();
  218. }
  219. if(compOnlyTeamCount == RANDOM_SIZE)
  220. {
  221. compOnlyTeamCount = rand.nextInt(std::max(compOnlyPlayerCount - 1, 0));
  222. }
  223. if(waterContent == EWaterContent::RANDOM)
  224. {
  225. waterContent = static_cast<EWaterContent::EWaterContent>(rand.nextInt(EWaterContent::NONE, EWaterContent::ISLANDS));
  226. }
  227. if(monsterStrength == EMonsterStrength::RANDOM)
  228. {
  229. monsterStrength = static_cast<EMonsterStrength::EMonsterStrength>(rand.nextInt(EMonsterStrength::GLOBAL_WEAK, EMonsterStrength::GLOBAL_STRONG));
  230. }
  231. assert (vstd::iswithin(waterContent, EWaterContent::NONE, EWaterContent::ISLANDS));
  232. assert (vstd::iswithin(monsterStrength, EMonsterStrength::GLOBAL_WEAK, EMonsterStrength::GLOBAL_STRONG));
  233. //rectangular maps are the future of gaming
  234. //setHeight(20);
  235. //setWidth(50);
  236. logGlobal->trace("Player config:");
  237. int humanPlayers = 0, cpuOnlyPlayers = 0, AIplayers = 0;
  238. for (auto player : players)
  239. {
  240. std::string playerType;
  241. switch (player.second.getPlayerType())
  242. {
  243. case EPlayerType::AI:
  244. playerType = "AI";
  245. AIplayers++;
  246. break;
  247. case EPlayerType::COMP_ONLY:
  248. playerType = "computer only";
  249. cpuOnlyPlayers++;
  250. break;
  251. case EPlayerType::HUMAN:
  252. playerType = "human only";
  253. humanPlayers++;
  254. break;
  255. default:
  256. assert(false);
  257. }
  258. logGlobal->trace("Player %d: %s", player.second.getColor(), playerType);
  259. }
  260. setCompOnlyPlayerCount(cpuOnlyPlayers); //human players are set automaticlaly (?)
  261. logGlobal->info("Final player config: %d total, %d cpu-only", players.size(), (int)getCompOnlyPlayerCount());
  262. }
  263. void CMapGenOptions::updatePlayers()
  264. {
  265. // Remove AI players only from the end of the players map if necessary
  266. for(auto itrev = players.end(); itrev != players.begin();)
  267. {
  268. auto it = itrev;
  269. --it;
  270. if (players.size() == getPlayerCount()) break;
  271. if(it->second.getPlayerType() == EPlayerType::AI)
  272. {
  273. players.erase(it);
  274. }
  275. else
  276. {
  277. --itrev;
  278. }
  279. }
  280. }
  281. void CMapGenOptions::updateCompOnlyPlayers()
  282. {
  283. // Remove comp only players only from the end of the players map if necessary
  284. for(auto itrev = players.end(); itrev != players.begin();)
  285. {
  286. auto it = itrev;
  287. --it;
  288. if (players.size() <= getPlayerCount()) break;
  289. if(it->second.getPlayerType() == EPlayerType::COMP_ONLY)
  290. {
  291. players.erase(it);
  292. }
  293. else
  294. {
  295. --itrev;
  296. }
  297. }
  298. // Add some comp only players if necessary
  299. int compOnlyPlayersToAdd = getPlayerCount() - players.size();
  300. if (compOnlyPlayersToAdd < 0)
  301. {
  302. logGlobal->error("Incorrect number of players to add. Requested players %d, current players %d", playerCount, players.size());
  303. assert (compOnlyPlayersToAdd < 0);
  304. }
  305. for(int i = 0; i < compOnlyPlayersToAdd; ++i)
  306. {
  307. CPlayerSettings pSettings;
  308. pSettings.setPlayerType(EPlayerType::COMP_ONLY);
  309. pSettings.setColor(getNextPlayerColor());
  310. players[pSettings.getColor()] = pSettings;
  311. }
  312. }
  313. int CMapGenOptions::countHumanPlayers() const
  314. {
  315. return static_cast<int>(boost::count_if(players, [](const std::pair<PlayerColor, CPlayerSettings> & pair)
  316. {
  317. return pair.second.getPlayerType() == EPlayerType::HUMAN;
  318. }));
  319. }
  320. int CMapGenOptions::countCompOnlyPlayers() const
  321. {
  322. return static_cast<int>(boost::count_if(players, [](const std::pair<PlayerColor, CPlayerSettings> & pair)
  323. {
  324. return pair.second.getPlayerType() == EPlayerType::COMP_ONLY;
  325. }));
  326. }
  327. PlayerColor CMapGenOptions::getNextPlayerColor() const
  328. {
  329. for(PlayerColor i = PlayerColor(0); i < PlayerColor::PLAYER_LIMIT; i.advance(1))
  330. {
  331. if(!players.count(i))
  332. {
  333. return i;
  334. }
  335. }
  336. logGlobal->error("Failed to get next player color");
  337. assert(false);
  338. return PlayerColor(0);
  339. }
  340. bool CMapGenOptions::checkOptions() const
  341. {
  342. assert(countHumanPlayers() > 0);
  343. if(mapTemplate)
  344. {
  345. return true;
  346. }
  347. else
  348. {
  349. CRandomGenerator gen;
  350. return getPossibleTemplate(gen) != nullptr;
  351. }
  352. }
  353. const CRmgTemplate * CMapGenOptions::getPossibleTemplate(CRandomGenerator & rand) const
  354. {
  355. // Find potential templates
  356. const auto & tpls = getAvailableTemplates();
  357. std::list<const CRmgTemplate *> potentialTpls;
  358. for(const auto & tplPair : tpls)
  359. {
  360. const auto & tpl = tplPair.second;
  361. int3 tplSize(width, height, (hasTwoLevels ? 2 : 1));
  362. if(tpl->matchesSize(tplSize))
  363. {
  364. bool isPlayerCountValid = false;
  365. if (getPlayerCount() != RANDOM_SIZE)
  366. {
  367. if (tpl->getPlayers().isInRange(getPlayerCount()))
  368. isPlayerCountValid = true;
  369. }
  370. else
  371. {
  372. // Human players shouldn't be banned when playing with random player count
  373. auto playerNumbers = tpl->getPlayers().getNumbers();
  374. if(countHumanPlayers() <= *boost::min_element(playerNumbers))
  375. {
  376. isPlayerCountValid = true;
  377. }
  378. }
  379. if (isPlayerCountValid)
  380. {
  381. bool isCpuPlayerCountValid = false;
  382. if(compOnlyPlayerCount != RANDOM_SIZE)
  383. {
  384. if (tpl->getCpuPlayers().isInRange(compOnlyPlayerCount))
  385. isCpuPlayerCountValid = true;
  386. }
  387. else
  388. {
  389. isCpuPlayerCountValid = true;
  390. }
  391. if(isCpuPlayerCountValid)
  392. potentialTpls.push_back(tpl);
  393. }
  394. }
  395. }
  396. // Select tpl
  397. if(potentialTpls.empty())
  398. {
  399. return nullptr;
  400. }
  401. else
  402. {
  403. return *RandomGeneratorUtil::nextItem(potentialTpls, rand);
  404. }
  405. }
  406. CMapGenOptions::CPlayerSettings::CPlayerSettings() : color(0), startingTown(RANDOM_TOWN), playerType(EPlayerType::AI)
  407. {
  408. }
  409. PlayerColor CMapGenOptions::CPlayerSettings::getColor() const
  410. {
  411. return color;
  412. }
  413. void CMapGenOptions::CPlayerSettings::setColor(PlayerColor value)
  414. {
  415. assert(value >= PlayerColor(0) && value < PlayerColor::PLAYER_LIMIT);
  416. color = value;
  417. }
  418. si32 CMapGenOptions::CPlayerSettings::getStartingTown() const
  419. {
  420. return startingTown;
  421. }
  422. void CMapGenOptions::CPlayerSettings::setStartingTown(si32 value)
  423. {
  424. assert(value >= -1);
  425. if(value >= 0)
  426. {
  427. assert(value < static_cast<int>(VLC->townh->factions.size()));
  428. assert(VLC->townh->factions[value]->town != nullptr);
  429. }
  430. startingTown = value;
  431. }
  432. EPlayerType::EPlayerType CMapGenOptions::CPlayerSettings::getPlayerType() const
  433. {
  434. return playerType;
  435. }
  436. void CMapGenOptions::CPlayerSettings::setPlayerType(EPlayerType::EPlayerType value)
  437. {
  438. playerType = value;
  439. }