RandomMapTab.cpp 18 KB

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