RandomMapTab.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610
  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 "CLobbyScreen.h"
  14. #include "SelectionTab.h"
  15. #include "../CGameInfo.h"
  16. #include "../CServerHandler.h"
  17. #include "../gui/CGuiHandler.h"
  18. #include "../gui/MouseButton.h"
  19. #include "../gui/WindowHandler.h"
  20. #include "../widgets/CComponent.h"
  21. #include "../widgets/ComboBox.h"
  22. #include "../widgets/Buttons.h"
  23. #include "../widgets/MiscWidgets.h"
  24. #include "../widgets/ObjectLists.h"
  25. #include "../widgets/Slider.h"
  26. #include "../widgets/TextControls.h"
  27. #include "../windows/GUIClasses.h"
  28. #include "../windows/InfoWindows.h"
  29. #include "../../lib/CGeneralTextHandler.h"
  30. #include "../../lib/mapping/CMapInfo.h"
  31. #include "../../lib/mapping/CMapHeader.h"
  32. #include "../../lib/mapping/MapFormat.h"
  33. #include "../../lib/rmg/CMapGenOptions.h"
  34. #include "../../lib/rmg/CRmgTemplateStorage.h"
  35. #include "../../lib/filesystem/Filesystem.h"
  36. #include "../../lib/RoadHandler.h"
  37. //#include "../../lib/GameSettings.h"
  38. #include "../../lib/CConfigHandler.h"
  39. #include "../../lib/serializer/JsonSerializer.h"
  40. #include "../../lib/serializer/JsonDeserializer.h"
  41. RandomMapTab::RandomMapTab():
  42. InterfaceObjectConfigurable()
  43. {
  44. recActions = 0;
  45. mapGenOptions = std::make_shared<CMapGenOptions>();
  46. addCallback("toggleMapSize", [&](int btnId)
  47. {
  48. auto mapSizeVal = getPossibleMapSizes();
  49. mapGenOptions->setWidth(mapSizeVal[btnId]);
  50. mapGenOptions->setHeight(mapSizeVal[btnId]);
  51. if(mapGenOptions->getMapTemplate())
  52. if(!mapGenOptions->getMapTemplate()->matchesSize(int3{mapGenOptions->getWidth(), mapGenOptions->getHeight(), 1 + mapGenOptions->getHasTwoLevels()}))
  53. setTemplate(nullptr);
  54. updateMapInfoByHost();
  55. });
  56. addCallback("toggleTwoLevels", [&](bool on)
  57. {
  58. mapGenOptions->setHasTwoLevels(on);
  59. if(mapGenOptions->getMapTemplate())
  60. if(!mapGenOptions->getMapTemplate()->matchesSize(int3{mapGenOptions->getWidth(), mapGenOptions->getHeight(), 1 + mapGenOptions->getHasTwoLevels()}))
  61. setTemplate(nullptr);
  62. updateMapInfoByHost();
  63. });
  64. addCallback("setPlayersCount", [&](int btnId)
  65. {
  66. mapGenOptions->setHumanOrCpuPlayerCount(btnId);
  67. setMapGenOptions(mapGenOptions);
  68. updateMapInfoByHost();
  69. });
  70. addCallback("setTeamsCount", [&](int btnId)
  71. {
  72. mapGenOptions->setTeamCount(btnId);
  73. updateMapInfoByHost();
  74. });
  75. addCallback("setCompOnlyPlayers", [&](int btnId)
  76. {
  77. mapGenOptions->setCompOnlyPlayerCount(btnId);
  78. setMapGenOptions(mapGenOptions);
  79. updateMapInfoByHost();
  80. });
  81. addCallback("setCompOnlyTeams", [&](int btnId)
  82. {
  83. mapGenOptions->setCompOnlyTeamCount(btnId);
  84. updateMapInfoByHost();
  85. });
  86. addCallback("setWaterContent", [&](int btnId)
  87. {
  88. mapGenOptions->setWaterContent(static_cast<EWaterContent::EWaterContent>(btnId));
  89. updateMapInfoByHost();
  90. });
  91. addCallback("setMonsterStrength", [&](int btnId)
  92. {
  93. if(btnId < 0)
  94. mapGenOptions->setMonsterStrength(EMonsterStrength::RANDOM);
  95. else
  96. mapGenOptions->setMonsterStrength(static_cast<EMonsterStrength::EMonsterStrength>(btnId)); //value 2 to 4
  97. updateMapInfoByHost();
  98. });
  99. //new callbacks available only from mod
  100. addCallback("teamAlignments", [&](int)
  101. {
  102. GH.windows().createAndPushWindow<TeamAlignments>(*this);
  103. });
  104. for(auto road : VLC->roadTypeHandler->objects)
  105. {
  106. std::string cbRoadType = "selectRoad_" + road->getJsonKey();
  107. addCallback(cbRoadType, [&, road](bool on)
  108. {
  109. mapGenOptions->setRoadEnabled(road->getId(), on);
  110. updateMapInfoByHost();
  111. });
  112. }
  113. const JsonNode config(JsonPath::builtin("config/widgets/randomMapTab.json"));
  114. build(config);
  115. if(auto w = widget<CButton>("buttonShowRandomMaps"))
  116. {
  117. w->addCallback([&]()
  118. {
  119. (static_cast<CLobbyScreen *>(parent))->toggleTab((static_cast<CLobbyScreen *>(parent))->tabSel);
  120. (static_cast<CLobbyScreen *>(parent))->tabSel->showRandom = true;
  121. (static_cast<CLobbyScreen *>(parent))->tabSel->filter(0, true);
  122. });
  123. }
  124. //set combo box callbacks
  125. if(auto w = widget<ComboBox>("templateList"))
  126. {
  127. w->onConstructItems = [](std::vector<const void *> & curItems){
  128. auto templates = VLC->tplh->getTemplates();
  129. boost::range::sort(templates, [](const CRmgTemplate * a, const CRmgTemplate * b){
  130. return a->getName() < b->getName();
  131. });
  132. curItems.push_back(nullptr); //default template
  133. for(auto & t : templates)
  134. curItems.push_back(t);
  135. };
  136. w->onSetItem = [&](const void * item){
  137. this->setTemplate(reinterpret_cast<const CRmgTemplate *>(item));
  138. };
  139. w->getItemText = [this](int idx, const void * item){
  140. if(item)
  141. return reinterpret_cast<const CRmgTemplate *>(item)->getName();
  142. if(idx == 0)
  143. return readText(variables["randomTemplate"]);
  144. return std::string("");
  145. };
  146. }
  147. loadOptions();
  148. //updateMapInfoByHost();
  149. }
  150. void RandomMapTab::updateMapInfoByHost()
  151. {
  152. if(CSH->isGuest())
  153. return;
  154. // Generate header info
  155. mapInfo = std::make_shared<CMapInfo>();
  156. mapInfo->isRandomMap = true;
  157. mapInfo->mapHeader = std::make_unique<CMapHeader>();
  158. mapInfo->mapHeader->version = EMapFormat::VCMI;
  159. mapInfo->mapHeader->name.appendLocalString(EMetaText::GENERAL_TXT, 740);
  160. mapInfo->mapHeader->description.appendLocalString(EMetaText::GENERAL_TXT, 741);
  161. const auto * temp = mapGenOptions->getMapTemplate();
  162. if (temp)
  163. {
  164. auto randomTemplateDescription = temp->getDescription();
  165. if (!randomTemplateDescription.empty())
  166. {
  167. auto description = std::string("\n\n") + randomTemplateDescription;
  168. mapInfo->mapHeader->description.appendRawString(description);
  169. }
  170. }
  171. mapInfo->mapHeader->difficulty = EMapDifficulty::NORMAL;
  172. mapInfo->mapHeader->height = mapGenOptions->getHeight();
  173. mapInfo->mapHeader->width = mapGenOptions->getWidth();
  174. mapInfo->mapHeader->twoLevel = mapGenOptions->getHasTwoLevels();
  175. // Generate player information
  176. int playersToGen = mapGenOptions->getMaxPlayersCount();
  177. mapInfo->mapHeader->howManyTeams = playersToGen;
  178. //TODO: Assign all human-controlled colors in first place
  179. for(int i = 0; i < PlayerColor::PLAYER_LIMIT_I; ++i)
  180. {
  181. mapInfo->mapHeader->players[i].canComputerPlay = false;
  182. mapInfo->mapHeader->players[i].canHumanPlay = false;
  183. }
  184. std::vector<PlayerColor> availableColors;
  185. for (ui8 color = 0; color < PlayerColor::PLAYER_LIMIT_I; color++)
  186. {
  187. availableColors.push_back(PlayerColor(color));
  188. }
  189. //First restore known players
  190. for (auto& player : mapGenOptions->getPlayersSettings())
  191. {
  192. PlayerInfo playerInfo;
  193. playerInfo.isFactionRandom = (player.second.getStartingTown() == FactionID::RANDOM);
  194. playerInfo.canComputerPlay = (player.second.getPlayerType() != EPlayerType::HUMAN);
  195. playerInfo.canHumanPlay = (player.second.getPlayerType() != EPlayerType::COMP_ONLY);
  196. auto team = player.second.getTeam();
  197. playerInfo.team = team;
  198. playerInfo.hasMainTown = true;
  199. playerInfo.generateHeroAtMainTown = true;
  200. mapInfo->mapHeader->players[player.first] = playerInfo;
  201. vstd::erase(availableColors, player.first);
  202. }
  203. mapInfoChanged(mapInfo, mapGenOptions);
  204. }
  205. void RandomMapTab::setMapGenOptions(std::shared_ptr<CMapGenOptions> opts)
  206. {
  207. mapGenOptions = opts;
  208. //Prepare allowed options - add all, then erase the ones above the limit
  209. for(int i = 0; i <= PlayerColor::PLAYER_LIMIT_I; ++i)
  210. {
  211. playerCountAllowed.insert(i);
  212. compCountAllowed.insert(i);
  213. if (i >= 2)
  214. {
  215. playerTeamsAllowed.insert(i);
  216. }
  217. if (i >= 1)
  218. {
  219. compTeamsAllowed.insert(i);
  220. }
  221. }
  222. std::set<int> humanCountAllowed;
  223. auto * tmpl = mapGenOptions->getMapTemplate();
  224. if(tmpl)
  225. {
  226. playerCountAllowed = tmpl->getPlayers().getNumbers();
  227. humanCountAllowed = tmpl->getHumanPlayers().getNumbers(); // Unused now?
  228. }
  229. si8 playerLimit = opts->getMaxPlayersCount();
  230. si8 humanOrCpuPlayerCount = opts->getHumanOrCpuPlayerCount();
  231. si8 compOnlyPlayersCount = opts->getCompOnlyPlayerCount();
  232. if(humanOrCpuPlayerCount != CMapGenOptions::RANDOM_SIZE)
  233. {
  234. vstd::erase_if(compCountAllowed, [playerLimit, humanOrCpuPlayerCount](int el)
  235. {
  236. return (playerLimit - humanOrCpuPlayerCount) < el;
  237. });
  238. vstd::erase_if(playerTeamsAllowed, [humanOrCpuPlayerCount](int el)
  239. {
  240. return humanOrCpuPlayerCount <= el;
  241. });
  242. }
  243. else // Random
  244. {
  245. vstd::erase_if(compCountAllowed, [playerLimit](int el)
  246. {
  247. return (playerLimit - 1) < el; // Must leave at least 1 human player
  248. });
  249. vstd::erase_if(playerTeamsAllowed, [playerLimit](int el)
  250. {
  251. return playerLimit <= el;
  252. });
  253. }
  254. if(!playerTeamsAllowed.count(opts->getTeamCount()))
  255. {
  256. opts->setTeamCount(CMapGenOptions::RANDOM_SIZE);
  257. }
  258. if(compOnlyPlayersCount != CMapGenOptions::RANDOM_SIZE)
  259. {
  260. // This setting doesn't impact total number of players
  261. vstd::erase_if(compTeamsAllowed, [compOnlyPlayersCount](int el)
  262. {
  263. return compOnlyPlayersCount<= el;
  264. });
  265. if(!compTeamsAllowed.count(opts->getCompOnlyTeamCount()))
  266. {
  267. opts->setCompOnlyTeamCount(CMapGenOptions::RANDOM_SIZE);
  268. }
  269. }
  270. if(auto w = widget<CToggleGroup>("groupMapSize"))
  271. {
  272. for(auto toggle : w->buttons)
  273. {
  274. if(auto button = std::dynamic_pointer_cast<CToggleButton>(toggle.second))
  275. {
  276. const auto & mapSizes = getPossibleMapSizes();
  277. int3 size( mapSizes[toggle.first], mapSizes[toggle.first], 1 + mapGenOptions->getHasTwoLevels());
  278. bool sizeAllowed = !mapGenOptions->getMapTemplate() || mapGenOptions->getMapTemplate()->matchesSize(size);
  279. button->block(!sizeAllowed);
  280. }
  281. }
  282. w->setSelected(vstd::find_pos(getPossibleMapSizes(), opts->getWidth()));
  283. }
  284. if(auto w = widget<CToggleButton>("buttonTwoLevels"))
  285. {
  286. int3 size( opts->getWidth(), opts->getWidth(), 2);
  287. bool undergoundAllowed = !mapGenOptions->getMapTemplate() || mapGenOptions->getMapTemplate()->matchesSize(size);
  288. w->setSelected(opts->getHasTwoLevels());
  289. w->block(!undergoundAllowed);
  290. }
  291. if(auto w = widget<CToggleGroup>("groupMaxPlayers"))
  292. {
  293. w->setSelected(opts->getHumanOrCpuPlayerCount());
  294. deactivateButtonsFrom(*w, playerCountAllowed);
  295. }
  296. if(auto w = widget<CToggleGroup>("groupMaxTeams"))
  297. {
  298. w->setSelected(opts->getTeamCount());
  299. deactivateButtonsFrom(*w, playerTeamsAllowed);
  300. }
  301. if(auto w = widget<CToggleGroup>("groupCompOnlyPlayers"))
  302. {
  303. w->setSelected(opts->getCompOnlyPlayerCount());
  304. deactivateButtonsFrom(*w, compCountAllowed);
  305. }
  306. if(auto w = widget<CToggleGroup>("groupCompOnlyTeams"))
  307. {
  308. w->setSelected(opts->getCompOnlyTeamCount());
  309. deactivateButtonsFrom(*w, compTeamsAllowed);
  310. }
  311. if(auto w = widget<CToggleGroup>("groupWaterContent"))
  312. {
  313. w->setSelected(opts->getWaterContent());
  314. if(opts->getMapTemplate())
  315. {
  316. std::set<int> allowedWater(opts->getMapTemplate()->getWaterContentAllowed().begin(), opts->getMapTemplate()->getWaterContentAllowed().end());
  317. deactivateButtonsFrom(*w, allowedWater);
  318. }
  319. else
  320. deactivateButtonsFrom(*w, {-1});
  321. }
  322. if(auto w = widget<CToggleGroup>("groupMonsterStrength"))
  323. w->setSelected(opts->getMonsterStrength());
  324. if(auto w = widget<CButton>("templateButton"))
  325. {
  326. if(tmpl)
  327. w->addTextOverlay(tmpl->getName(), EFonts::FONT_SMALL, Colors::WHITE);
  328. else
  329. w->addTextOverlay(readText(variables["randomTemplate"]), EFonts::FONT_SMALL, Colors::WHITE);
  330. }
  331. for(auto r : VLC->roadTypeHandler->objects)
  332. {
  333. // Workaround for vcmi-extras bug
  334. std::string jsonKey = r->getJsonKey();
  335. std::string identifier = jsonKey.substr(jsonKey.find(':')+1);
  336. if(auto w = widget<CToggleButton>(identifier))
  337. {
  338. w->setSelected(opts->isRoadEnabled(r->getId()));
  339. }
  340. }
  341. }
  342. void RandomMapTab::setTemplate(const CRmgTemplate * tmpl)
  343. {
  344. mapGenOptions->setMapTemplate(tmpl);
  345. setMapGenOptions(mapGenOptions);
  346. if(auto w = widget<CButton>("templateButton"))
  347. {
  348. if(tmpl)
  349. w->addTextOverlay(tmpl->getName(), EFonts::FONT_SMALL, Colors::WHITE);
  350. else
  351. w->addTextOverlay(readText(variables["randomTemplate"]), EFonts::FONT_SMALL, Colors::WHITE);
  352. }
  353. updateMapInfoByHost();
  354. }
  355. void RandomMapTab::deactivateButtonsFrom(CToggleGroup & group, const std::set<int> & allowed)
  356. {
  357. logGlobal->debug("Blocking buttons");
  358. for(auto toggle : group.buttons)
  359. {
  360. if(auto button = std::dynamic_pointer_cast<CToggleButton>(toggle.second))
  361. {
  362. if(allowed.count(CMapGenOptions::RANDOM_SIZE)
  363. || allowed.count(toggle.first)
  364. || toggle.first == CMapGenOptions::RANDOM_SIZE)
  365. {
  366. button->block(false);
  367. }
  368. else
  369. {
  370. button->block(true);
  371. }
  372. }
  373. }
  374. }
  375. std::vector<int> RandomMapTab::getPossibleMapSizes()
  376. {
  377. 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};
  378. }
  379. void TeamAlignmentsWidget::checkTeamCount()
  380. {
  381. //Do not allow to select one team only
  382. std::set<TeamID> teams;
  383. for (int plId = 0; plId < players.size(); ++plId)
  384. {
  385. teams.insert(TeamID(players[plId]->getSelected()));
  386. }
  387. if (teams.size() < 2)
  388. {
  389. //Do not let player close the window
  390. buttonOk->block(true);
  391. }
  392. else
  393. {
  394. buttonOk->block(false);
  395. }
  396. }
  397. TeamAlignments::TeamAlignments(RandomMapTab & randomMapTab)
  398. : CWindowObject(BORDERED)
  399. {
  400. OBJ_CONSTRUCTION_CAPTURING_ALL_NO_DISPOSE;
  401. widget = std::make_shared<TeamAlignmentsWidget>(randomMapTab);
  402. pos = widget->pos;
  403. updateShadow();
  404. center();
  405. }
  406. TeamAlignmentsWidget::TeamAlignmentsWidget(RandomMapTab & randomMapTab):
  407. InterfaceObjectConfigurable()
  408. {
  409. const JsonNode config(JsonPath::builtin("config/widgets/randomMapTeamsWidget.json"));
  410. variables = config["variables"];
  411. //int totalPlayers = randomMapTab.obtainMapGenOptions().getPlayerLimit();
  412. int totalPlayers = randomMapTab.obtainMapGenOptions().getMaxPlayersCount();
  413. assert(totalPlayers <= PlayerColor::PLAYER_LIMIT_I);
  414. auto settings = randomMapTab.obtainMapGenOptions().getPlayersSettings();
  415. variables["totalPlayers"].Integer() = totalPlayers;
  416. pos.w = variables["windowSize"]["x"].Integer() + totalPlayers * variables["cellMargin"]["x"].Integer();
  417. pos.h = variables["windowSize"]["y"].Integer() + totalPlayers * variables["cellMargin"]["y"].Integer();
  418. variables["backgroundRect"]["x"].Integer() = 0;
  419. variables["backgroundRect"]["y"].Integer() = 0;
  420. variables["backgroundRect"]["w"].Integer() = pos.w;
  421. variables["backgroundRect"]["h"].Integer() = pos.h;
  422. variables["okButtonPosition"]["x"].Integer() = variables["buttonsOffset"]["ok"]["x"].Integer();
  423. variables["okButtonPosition"]["y"].Integer() = variables["buttonsOffset"]["ok"]["y"].Integer() + totalPlayers * variables["cellMargin"]["y"].Integer();
  424. variables["cancelButtonPosition"]["x"].Integer() = variables["buttonsOffset"]["cancel"]["x"].Integer();
  425. variables["cancelButtonPosition"]["y"].Integer() = variables["buttonsOffset"]["cancel"]["y"].Integer() + totalPlayers * variables["cellMargin"]["y"].Integer();
  426. addCallback("ok", [&](int)
  427. {
  428. for(int plId = 0; plId < players.size(); ++plId)
  429. {
  430. randomMapTab.obtainMapGenOptions().setPlayerTeam(PlayerColor(plId), TeamID(players[plId]->getSelected()));
  431. }
  432. randomMapTab.updateMapInfoByHost();
  433. for(auto & window : GH.windows().findWindows<TeamAlignments>())
  434. GH.windows().popWindow(window);
  435. });
  436. addCallback("cancel", [&](int)
  437. {
  438. for(auto & window : GH.windows().findWindows<TeamAlignments>())
  439. GH.windows().popWindow(window);
  440. });
  441. build(config);
  442. center(pos);
  443. OBJ_CONSTRUCTION;
  444. // Window should have X * X columns, where X is max players allowed for current settings
  445. // For random player count, X is 8
  446. if (totalPlayers > settings.size())
  447. {
  448. auto savedPlayers = randomMapTab.obtainMapGenOptions().getSavedPlayersMap();
  449. for (const auto & player : savedPlayers)
  450. {
  451. if (!vstd::contains(settings, player.first))
  452. {
  453. settings[player.first] = player.second;
  454. }
  455. }
  456. }
  457. std::vector<CMapGenOptions::CPlayerSettings> settingsVec;
  458. for (const auto & player : settings)
  459. {
  460. settingsVec.push_back(player.second);
  461. }
  462. for(int plId = 0; plId < totalPlayers; ++plId)
  463. {
  464. players.push_back(std::make_shared<CToggleGroup>([&, totalPlayers, plId](int sel)
  465. {
  466. variables["player_id"].Integer() = plId;
  467. OBJ_CONSTRUCTION_TARGETED(players[plId].get());
  468. for(int teamId = 0; teamId < totalPlayers; ++teamId)
  469. {
  470. auto button = std::dynamic_pointer_cast<CToggleButton>(players[plId]->buttons[teamId]);
  471. assert(button);
  472. if(sel == teamId)
  473. {
  474. button->addOverlay(buildWidget(variables["flagsAnimation"]));
  475. }
  476. else
  477. {
  478. button->addOverlay(nullptr);
  479. }
  480. button->addCallback([this](bool)
  481. {
  482. checkTeamCount();
  483. });
  484. }
  485. }));
  486. OBJ_CONSTRUCTION_TARGETED(players.back().get());
  487. for(int teamId = 0; teamId < totalPlayers; ++teamId)
  488. {
  489. variables["point"]["x"].Integer() = variables["cellOffset"]["x"].Integer() + plId * variables["cellMargin"]["x"].Integer();
  490. variables["point"]["y"].Integer() = variables["cellOffset"]["y"].Integer() + teamId * variables["cellMargin"]["y"].Integer();
  491. auto button = buildWidget(variables["button"]);
  492. players.back()->addToggle(teamId, std::dynamic_pointer_cast<CToggleBase>(button));
  493. }
  494. // plId is not neccessarily player color, just an index
  495. auto team = settingsVec.at(plId).getTeam();
  496. if(team == TeamID::NO_TEAM)
  497. {
  498. logGlobal->warn("Player %d (id %d) has uninitialized team", settingsVec.at(plId).getColor(), plId);
  499. players.back()->setSelected(plId);
  500. }
  501. else
  502. players.back()->setSelected(team.getNum());
  503. }
  504. buttonOk = widget<CButton>("buttonOK");
  505. buttonCancel = widget<CButton>("buttonCancel");
  506. }
  507. void RandomMapTab::saveOptions(const CMapGenOptions & options)
  508. {
  509. JsonNode data;
  510. JsonSerializer ser(nullptr, data);
  511. ser.serializeStruct("lastSettings", const_cast<CMapGenOptions & >(options));
  512. // FIXME: Do not nest fields
  513. Settings rmgSettings = persistentStorage.write["rmg"];
  514. rmgSettings["rmg"] = data;
  515. }
  516. void RandomMapTab::loadOptions()
  517. {
  518. auto rmgSettings = persistentStorage["rmg"]["rmg"];
  519. if (!rmgSettings.Struct().empty())
  520. {
  521. mapGenOptions.reset(new CMapGenOptions());
  522. JsonDeserializer handler(nullptr, rmgSettings);
  523. handler.serializeStruct("lastSettings", *mapGenOptions);
  524. // Will check template and set other options as well
  525. setTemplate(mapGenOptions->getMapTemplate());
  526. if(auto w = widget<ComboBox>("templateList"))
  527. {
  528. w->setItem(mapGenOptions->getMapTemplate());
  529. }
  530. }
  531. updateMapInfoByHost();
  532. // TODO: Save & load difficulty?
  533. }