RandomMapTab.cpp 18 KB

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