RandomMapTab.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  1. /*
  2. * RandomMapTab.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 "RandomMapTab.h"
  12. #include "CSelectionBase.h"
  13. #include "../CGameInfo.h"
  14. #include "../CServerHandler.h"
  15. #include "../gui/CGuiHandler.h"
  16. #include "../gui/MouseButton.h"
  17. #include "../gui/WindowHandler.h"
  18. #include "../widgets/CComponent.h"
  19. #include "../widgets/ComboBox.h"
  20. #include "../widgets/Buttons.h"
  21. #include "../widgets/MiscWidgets.h"
  22. #include "../widgets/ObjectLists.h"
  23. #include "../widgets/Slider.h"
  24. #include "../widgets/TextControls.h"
  25. #include "../windows/GUIClasses.h"
  26. #include "../windows/InfoWindows.h"
  27. #include "../../lib/CGeneralTextHandler.h"
  28. #include "../../lib/mapping/CMapInfo.h"
  29. #include "../../lib/mapping/CMapHeader.h"
  30. #include "../../lib/mapping/MapFormat.h"
  31. #include "../../lib/rmg/CMapGenOptions.h"
  32. #include "../../lib/rmg/CRmgTemplateStorage.h"
  33. #include "../../lib/filesystem/Filesystem.h"
  34. #include "../../lib/RoadHandler.h"
  35. RandomMapTab::RandomMapTab():
  36. InterfaceObjectConfigurable()
  37. {
  38. recActions = 0;
  39. mapGenOptions = std::make_shared<CMapGenOptions>();
  40. addCallback("toggleMapSize", [&](int btnId)
  41. {
  42. auto mapSizeVal = getPossibleMapSizes();
  43. mapGenOptions->setWidth(mapSizeVal[btnId]);
  44. mapGenOptions->setHeight(mapSizeVal[btnId]);
  45. if(mapGenOptions->getMapTemplate())
  46. if(!mapGenOptions->getMapTemplate()->matchesSize(int3{mapGenOptions->getWidth(), mapGenOptions->getHeight(), 1 + mapGenOptions->getHasTwoLevels()}))
  47. setTemplate(nullptr);
  48. updateMapInfoByHost();
  49. });
  50. addCallback("toggleTwoLevels", [&](bool on)
  51. {
  52. mapGenOptions->setHasTwoLevels(on);
  53. if(mapGenOptions->getMapTemplate())
  54. if(!mapGenOptions->getMapTemplate()->matchesSize(int3{mapGenOptions->getWidth(), mapGenOptions->getHeight(), 1 + mapGenOptions->getHasTwoLevels()}))
  55. setTemplate(nullptr);
  56. updateMapInfoByHost();
  57. });
  58. addCallback("setPlayersCount", [&](int btnId)
  59. {
  60. mapGenOptions->setPlayerCount(btnId);
  61. setMapGenOptions(mapGenOptions);
  62. updateMapInfoByHost();
  63. });
  64. addCallback("setTeamsCount", [&](int btnId)
  65. {
  66. mapGenOptions->setTeamCount(btnId);
  67. updateMapInfoByHost();
  68. });
  69. addCallback("setCompOnlyPlayers", [&](int btnId)
  70. {
  71. mapGenOptions->setCompOnlyPlayerCount(btnId);
  72. setMapGenOptions(mapGenOptions);
  73. updateMapInfoByHost();
  74. });
  75. addCallback("setCompOnlyTeams", [&](int btnId)
  76. {
  77. mapGenOptions->setCompOnlyTeamCount(btnId);
  78. updateMapInfoByHost();
  79. });
  80. addCallback("setWaterContent", [&](int btnId)
  81. {
  82. mapGenOptions->setWaterContent(static_cast<EWaterContent::EWaterContent>(btnId));
  83. updateMapInfoByHost();
  84. });
  85. addCallback("setMonsterStrength", [&](int btnId)
  86. {
  87. if(btnId < 0)
  88. mapGenOptions->setMonsterStrength(EMonsterStrength::RANDOM);
  89. else
  90. mapGenOptions->setMonsterStrength(static_cast<EMonsterStrength::EMonsterStrength>(btnId)); //value 2 to 4
  91. updateMapInfoByHost();
  92. });
  93. //new callbacks available only from mod
  94. addCallback("teamAlignments", [&](int)
  95. {
  96. GH.windows().createAndPushWindow<TeamAlignmentsWidget>(*this);
  97. });
  98. for(auto road : VLC->roadTypeHandler->objects)
  99. {
  100. std::string cbRoadType = "selectRoad_" + road->getJsonKey();
  101. addCallback(cbRoadType, [&, road](bool on)
  102. {
  103. mapGenOptions->setRoadEnabled(road->getId(), on);
  104. updateMapInfoByHost();
  105. });
  106. }
  107. const JsonNode config(JsonPath::builtin("config/widgets/randomMapTab.json"));
  108. build(config);
  109. //set combo box callbacks
  110. if(auto w = widget<ComboBox>("templateList"))
  111. {
  112. w->onConstructItems = [](std::vector<const void *> & curItems){
  113. auto templates = VLC->tplh->getTemplates();
  114. boost::range::sort(templates, [](const CRmgTemplate * a, const CRmgTemplate * b){
  115. return a->getName() < b->getName();
  116. });
  117. curItems.push_back(nullptr); //default template
  118. for(auto & t : templates)
  119. curItems.push_back(t);
  120. };
  121. w->onSetItem = [&](const void * item){
  122. this->setTemplate(reinterpret_cast<const CRmgTemplate *>(item));
  123. };
  124. w->getItemText = [this](int idx, const void * item){
  125. if(item)
  126. return reinterpret_cast<const CRmgTemplate *>(item)->getName();
  127. if(idx == 0)
  128. return readText(variables["randomTemplate"]);
  129. return std::string("");
  130. };
  131. }
  132. updateMapInfoByHost();
  133. }
  134. void RandomMapTab::updateMapInfoByHost()
  135. {
  136. if(CSH->isGuest())
  137. return;
  138. // Generate header info
  139. mapInfo = std::make_shared<CMapInfo>();
  140. mapInfo->isRandomMap = true;
  141. mapInfo->mapHeader = std::make_unique<CMapHeader>();
  142. mapInfo->mapHeader->version = EMapFormat::VCMI;
  143. mapInfo->mapHeader->name = CGI->generaltexth->allTexts[740];
  144. mapInfo->mapHeader->description = CGI->generaltexth->allTexts[741];
  145. mapInfo->mapHeader->difficulty = 1; // Normal
  146. mapInfo->mapHeader->height = mapGenOptions->getHeight();
  147. mapInfo->mapHeader->width = mapGenOptions->getWidth();
  148. mapInfo->mapHeader->twoLevel = mapGenOptions->getHasTwoLevels();
  149. // Generate player information
  150. int playersToGen = PlayerColor::PLAYER_LIMIT_I;
  151. if(mapGenOptions->getPlayerCount() != CMapGenOptions::RANDOM_SIZE)
  152. {
  153. if(mapGenOptions->getCompOnlyPlayerCount() != CMapGenOptions::RANDOM_SIZE)
  154. playersToGen = mapGenOptions->getPlayerCount() + mapGenOptions->getCompOnlyPlayerCount();
  155. else
  156. playersToGen = mapGenOptions->getPlayerCount();
  157. }
  158. mapInfo->mapHeader->howManyTeams = playersToGen;
  159. std::set<TeamID> occupiedTeams;
  160. for(int i = 0; i < PlayerColor::PLAYER_LIMIT_I; ++i)
  161. {
  162. mapInfo->mapHeader->players[i].canComputerPlay = false;
  163. mapInfo->mapHeader->players[i].canHumanPlay = false;
  164. }
  165. for(int i = 0; i < playersToGen; ++i)
  166. {
  167. PlayerInfo player;
  168. player.isFactionRandom = true;
  169. player.canComputerPlay = true;
  170. if(mapGenOptions->getCompOnlyPlayerCount() != CMapGenOptions::RANDOM_SIZE && i >= mapGenOptions->getPlayerCount())
  171. {
  172. player.canHumanPlay = false;
  173. }
  174. else
  175. {
  176. player.canHumanPlay = true;
  177. }
  178. auto team = mapGenOptions->getPlayersSettings().at(PlayerColor(i)).getTeam();
  179. player.team = team;
  180. occupiedTeams.insert(team);
  181. player.hasMainTown = true;
  182. player.generateHeroAtMainTown = true;
  183. mapInfo->mapHeader->players[i] = player;
  184. }
  185. for(auto & player : mapInfo->mapHeader->players)
  186. {
  187. for(int i = 0; player.team == TeamID::NO_TEAM; ++i)
  188. {
  189. TeamID team(i);
  190. if(!occupiedTeams.count(team))
  191. {
  192. player.team = team;
  193. occupiedTeams.insert(team);
  194. }
  195. }
  196. }
  197. mapInfoChanged(mapInfo, mapGenOptions);
  198. }
  199. void RandomMapTab::setMapGenOptions(std::shared_ptr<CMapGenOptions> opts)
  200. {
  201. mapGenOptions = opts;
  202. //prepare allowed options
  203. for(int i = 0; i <= PlayerColor::PLAYER_LIMIT_I; ++i)
  204. {
  205. playerCountAllowed.insert(i);
  206. compCountAllowed.insert(i);
  207. playerTeamsAllowed.insert(i);
  208. compTeamsAllowed.insert(i);
  209. }
  210. auto * tmpl = mapGenOptions->getMapTemplate();
  211. if(tmpl)
  212. {
  213. playerCountAllowed = tmpl->getPlayers().getNumbers();
  214. compCountAllowed = tmpl->getCpuPlayers().getNumbers();
  215. }
  216. if(mapGenOptions->getPlayerCount() != CMapGenOptions::RANDOM_SIZE)
  217. {
  218. vstd::erase_if(compCountAllowed,
  219. [opts](int el){
  220. return PlayerColor::PLAYER_LIMIT_I - opts->getPlayerCount() < el;
  221. });
  222. vstd::erase_if(playerTeamsAllowed,
  223. [opts](int el){
  224. return opts->getPlayerCount() <= el;
  225. });
  226. if(!playerTeamsAllowed.count(opts->getTeamCount()))
  227. opts->setTeamCount(CMapGenOptions::RANDOM_SIZE);
  228. }
  229. if(mapGenOptions->getCompOnlyPlayerCount() != CMapGenOptions::RANDOM_SIZE)
  230. {
  231. vstd::erase_if(playerCountAllowed,
  232. [opts](int el){
  233. return PlayerColor::PLAYER_LIMIT_I - opts->getCompOnlyPlayerCount() < el;
  234. });
  235. vstd::erase_if(compTeamsAllowed,
  236. [opts](int el){
  237. return opts->getCompOnlyPlayerCount() <= el;
  238. });
  239. if(!compTeamsAllowed.count(opts->getCompOnlyTeamCount()))
  240. opts->setCompOnlyTeamCount(CMapGenOptions::RANDOM_SIZE);
  241. }
  242. if(auto w = widget<CToggleGroup>("groupMapSize"))
  243. {
  244. for(auto toggle : w->buttons)
  245. {
  246. if(auto button = std::dynamic_pointer_cast<CToggleButton>(toggle.second))
  247. {
  248. const auto & mapSizes = getPossibleMapSizes();
  249. int3 size( mapSizes[toggle.first], mapSizes[toggle.first], 1 + mapGenOptions->getHasTwoLevels());
  250. bool sizeAllowed = !mapGenOptions->getMapTemplate() || mapGenOptions->getMapTemplate()->matchesSize(size);
  251. button->block(!sizeAllowed);
  252. }
  253. }
  254. w->setSelected(vstd::find_pos(getPossibleMapSizes(), opts->getWidth()));
  255. }
  256. if(auto w = widget<CToggleButton>("buttonTwoLevels"))
  257. {
  258. int3 size( opts->getWidth(), opts->getWidth(), 2);
  259. bool undergoundAllowed = !mapGenOptions->getMapTemplate() || mapGenOptions->getMapTemplate()->matchesSize(size);
  260. w->setSelected(opts->getHasTwoLevels());
  261. w->block(!undergoundAllowed);
  262. }
  263. if(auto w = widget<CToggleGroup>("groupMaxPlayers"))
  264. {
  265. w->setSelected(opts->getPlayerCount());
  266. deactivateButtonsFrom(*w, playerCountAllowed);
  267. }
  268. if(auto w = widget<CToggleGroup>("groupMaxTeams"))
  269. {
  270. w->setSelected(opts->getTeamCount());
  271. deactivateButtonsFrom(*w, playerTeamsAllowed);
  272. }
  273. if(auto w = widget<CToggleGroup>("groupCompOnlyPlayers"))
  274. {
  275. w->setSelected(opts->getCompOnlyPlayerCount());
  276. deactivateButtonsFrom(*w, compCountAllowed);
  277. }
  278. if(auto w = widget<CToggleGroup>("groupCompOnlyTeams"))
  279. {
  280. w->setSelected(opts->getCompOnlyTeamCount());
  281. deactivateButtonsFrom(*w, compTeamsAllowed);
  282. }
  283. if(auto w = widget<CToggleGroup>("groupWaterContent"))
  284. {
  285. w->setSelected(opts->getWaterContent());
  286. if(opts->getMapTemplate())
  287. {
  288. std::set<int> allowedWater(opts->getMapTemplate()->getWaterContentAllowed().begin(), opts->getMapTemplate()->getWaterContentAllowed().end());
  289. deactivateButtonsFrom(*w, allowedWater);
  290. }
  291. else
  292. deactivateButtonsFrom(*w, {-1});
  293. }
  294. if(auto w = widget<CToggleGroup>("groupMonsterStrength"))
  295. w->setSelected(opts->getMonsterStrength());
  296. if(auto w = widget<CButton>("templateButton"))
  297. {
  298. if(tmpl)
  299. w->addTextOverlay(tmpl->getName(), EFonts::FONT_SMALL, Colors::WHITE);
  300. else
  301. w->addTextOverlay(readText(variables["randomTemplate"]), EFonts::FONT_SMALL, Colors::WHITE);
  302. }
  303. for(auto r : VLC->roadTypeHandler->objects)
  304. {
  305. if(auto w = widget<CToggleButton>(r->getJsonKey()))
  306. {
  307. w->setSelected(opts->isRoadEnabled(r->getId()));
  308. }
  309. }
  310. }
  311. void RandomMapTab::setTemplate(const CRmgTemplate * tmpl)
  312. {
  313. mapGenOptions->setMapTemplate(tmpl);
  314. setMapGenOptions(mapGenOptions);
  315. if(auto w = widget<CButton>("templateButton"))
  316. {
  317. if(tmpl)
  318. w->addTextOverlay(tmpl->getName(), EFonts::FONT_SMALL, Colors::WHITE);
  319. else
  320. w->addTextOverlay(readText(variables["randomTemplate"]), EFonts::FONT_SMALL, Colors::WHITE);
  321. }
  322. updateMapInfoByHost();
  323. }
  324. void RandomMapTab::deactivateButtonsFrom(CToggleGroup & group, const std::set<int> & allowed)
  325. {
  326. logGlobal->debug("Blocking buttons");
  327. for(auto toggle : group.buttons)
  328. {
  329. if(auto button = std::dynamic_pointer_cast<CToggleButton>(toggle.second))
  330. {
  331. if(allowed.count(CMapGenOptions::RANDOM_SIZE)
  332. || allowed.count(toggle.first)
  333. || toggle.first == CMapGenOptions::RANDOM_SIZE)
  334. {
  335. button->block(false);
  336. }
  337. else
  338. {
  339. button->block(true);
  340. }
  341. }
  342. }
  343. }
  344. std::vector<int> RandomMapTab::getPossibleMapSizes()
  345. {
  346. return {CMapHeader::MAP_SIZE_SMALL, CMapHeader::MAP_SIZE_MIDDLE, CMapHeader::MAP_SIZE_LARGE, CMapHeader::MAP_SIZE_XLARGE, CMapHeader::MAP_SIZE_HUGE, CMapHeader::MAP_SIZE_XHUGE, CMapHeader::MAP_SIZE_GIANT};
  347. }
  348. TeamAlignmentsWidget::TeamAlignmentsWidget(RandomMapTab & randomMapTab):
  349. InterfaceObjectConfigurable()
  350. {
  351. const JsonNode config(JsonPath::builtin("config/widgets/randomMapTeamsWidget.json"));
  352. variables = config["variables"];
  353. int humanPlayers = randomMapTab.obtainMapGenOptions().getPlayerCount();
  354. int cpuPlayers = randomMapTab.obtainMapGenOptions().getCompOnlyPlayerCount();
  355. int totalPlayers = humanPlayers == CMapGenOptions::RANDOM_SIZE || cpuPlayers == CMapGenOptions::RANDOM_SIZE
  356. ? PlayerColor::PLAYER_LIMIT_I : humanPlayers + cpuPlayers;
  357. assert(totalPlayers <= PlayerColor::PLAYER_LIMIT_I);
  358. auto settings = randomMapTab.obtainMapGenOptions().getPlayersSettings();
  359. variables["totalPlayers"].Integer() = totalPlayers;
  360. pos.w = variables["windowSize"]["x"].Integer() + totalPlayers * variables["cellMargin"]["x"].Integer();
  361. pos.h = variables["windowSize"]["y"].Integer() + totalPlayers * variables["cellMargin"]["y"].Integer();
  362. variables["backgroundRect"]["x"].Integer() = pos.x;
  363. variables["backgroundRect"]["y"].Integer() = pos.y;
  364. variables["backgroundRect"]["w"].Integer() = pos.w;
  365. variables["backgroundRect"]["h"].Integer() = pos.h;
  366. variables["okButtonPosition"]["x"].Integer() = variables["buttonsOffset"]["ok"]["x"].Integer();
  367. variables["okButtonPosition"]["y"].Integer() = variables["buttonsOffset"]["ok"]["y"].Integer() + totalPlayers * variables["cellMargin"]["y"].Integer();
  368. variables["cancelButtonPosition"]["x"].Integer() = variables["buttonsOffset"]["cancel"]["x"].Integer();
  369. variables["cancelButtonPosition"]["y"].Integer() = variables["buttonsOffset"]["cancel"]["y"].Integer() + totalPlayers * variables["cellMargin"]["y"].Integer();
  370. addCallback("ok", [&](int)
  371. {
  372. for(int plId = 0; plId < players.size(); ++plId)
  373. {
  374. randomMapTab.obtainMapGenOptions().setPlayerTeam(PlayerColor(plId), TeamID(players[plId]->getSelected()));
  375. }
  376. randomMapTab.updateMapInfoByHost();
  377. assert(GH.windows().isTopWindow(this));
  378. GH.windows().popWindows(1);
  379. });
  380. addCallback("cancel", [&](int)
  381. {
  382. assert(GH.windows().isTopWindow(this));
  383. GH.windows().popWindows(1);
  384. });
  385. build(config);
  386. center(pos);
  387. OBJ_CONSTRUCTION;
  388. for(int plId = 0; plId < totalPlayers; ++plId)
  389. {
  390. players.push_back(std::make_shared<CToggleGroup>([&, totalPlayers, plId](int sel)
  391. {
  392. variables["player_id"].Integer() = plId;
  393. OBJ_CONSTRUCTION_TARGETED(players[plId].get());
  394. for(int teamId = 0; teamId < totalPlayers; ++teamId)
  395. {
  396. auto button = std::dynamic_pointer_cast<CToggleButton>(players[plId]->buttons[teamId]);
  397. assert(button);
  398. if(sel == teamId)
  399. {
  400. button->addOverlay(buildWidget(variables["flagsAnimation"]));
  401. }
  402. else
  403. {
  404. button->addOverlay(nullptr);
  405. }
  406. }
  407. }));
  408. OBJ_CONSTRUCTION_TARGETED(players.back().get());
  409. for(int teamId = 0; teamId < totalPlayers; ++teamId)
  410. {
  411. variables["point"]["x"].Integer() = variables["cellOffset"]["x"].Integer() + plId * variables["cellMargin"]["x"].Integer();
  412. variables["point"]["y"].Integer() = variables["cellOffset"]["y"].Integer() + teamId * variables["cellMargin"]["y"].Integer();
  413. auto button = buildWidget(variables["button"]);
  414. players.back()->addToggle(teamId, std::dynamic_pointer_cast<CToggleBase>(button));
  415. }
  416. auto team = settings.at(PlayerColor(plId)).getTeam();
  417. if(team == TeamID::NO_TEAM)
  418. players.back()->setSelected(plId);
  419. else
  420. players.back()->setSelected(team.getNum());
  421. }
  422. }