CLobbyScreen.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. /*
  2. * CLobbyScreen.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 "CLobbyScreen.h"
  12. #include "CBonusSelection.h"
  13. #include "TurnOptionsTab.h"
  14. #include "ExtraOptionsTab.h"
  15. #include "OptionsTab.h"
  16. #include "RandomMapTab.h"
  17. #include "SelectionTab.h"
  18. #include "../CServerHandler.h"
  19. #include "../GameEngine.h"
  20. #include "../GameInstance.h"
  21. #include "../gui/Shortcut.h"
  22. #include "../widgets/Buttons.h"
  23. #include "../widgets/GraphicalPrimitiveCanvas.h"
  24. #include "../windows/InfoWindows.h"
  25. #include "../render/Colors.h"
  26. #include "../globalLobby/GlobalLobbyClient.h"
  27. #include "../../lib/CConfigHandler.h"
  28. #include "../../lib/texts/CGeneralTextHandler.h"
  29. #include "../../lib/campaign/CampaignHandler.h"
  30. #include "../../lib/mapping/CMapInfo.h"
  31. #include "../../lib/networkPacks/PacksForLobby.h"
  32. #include "../../lib/rmg/CMapGenOptions.h"
  33. #include "../../lib/GameLibrary.h"
  34. CLobbyScreen::CLobbyScreen(ESelectionScreen screenType, bool hideScreen)
  35. : CSelectionBase(screenType), bonusSel(nullptr)
  36. {
  37. OBJECT_CONSTRUCTION;
  38. tabSel = std::make_shared<SelectionTab>(screenType);
  39. curTab = tabSel;
  40. auto initLobby = [&]()
  41. {
  42. tabSel->callOnSelect = std::bind(&IServerAPI::setMapInfo, &GAME->server(), _1, nullptr);
  43. buttonSelect = std::make_shared<CButton>(Point(411, 80), AnimationPath::builtin("GSPBUTT.DEF"), LIBRARY->generaltexth->zelp[45], 0, EShortcut::LOBBY_SELECT_SCENARIO);
  44. buttonSelect->addCallback([this]()
  45. {
  46. toggleTab(tabSel);
  47. if (getMapInfo() && getMapInfo()->isRandomMap)
  48. GAME->server().setMapInfo(tabSel->getSelectedMapInfo());
  49. });
  50. buttonOptions = std::make_shared<CButton>(Point(411, 510), AnimationPath::builtin("GSPBUTT.DEF"), LIBRARY->generaltexth->zelp[46], std::bind(&CLobbyScreen::toggleTab, this, tabOpt), EShortcut::LOBBY_ADDITIONAL_OPTIONS);
  51. if(settings["general"]["enableUiEnhancements"].Bool())
  52. {
  53. buttonTurnOptions = std::make_shared<CButton>(Point(619, 105), AnimationPath::builtin("GSPBUT2.DEF"), LIBRARY->generaltexth->zelp[46], std::bind(&CLobbyScreen::toggleTab, this, tabTurnOptions), EShortcut::LOBBY_TURN_OPTIONS);
  54. buttonExtraOptions = std::make_shared<CButton>(Point(619, 510), AnimationPath::builtin("GSPBUT2.DEF"), LIBRARY->generaltexth->zelp[46], std::bind(&CLobbyScreen::toggleTab, this, tabExtraOptions), EShortcut::LOBBY_EXTRA_OPTIONS);
  55. }
  56. };
  57. if(screenType != ESelectionScreen::campaignList)
  58. {
  59. buttonChat = std::make_shared<CButton>(Point(619, 80), AnimationPath::builtin("GSPBUT2.DEF"), LIBRARY->generaltexth->zelp[48], std::bind(&CLobbyScreen::toggleChat, this), EShortcut::LOBBY_TOGGLE_CHAT);
  60. buttonChat->setTextOverlay(LIBRARY->generaltexth->allTexts[532], FONT_SMALL, Colors::WHITE);
  61. }
  62. switch(screenType)
  63. {
  64. case ESelectionScreen::newGame:
  65. {
  66. tabOpt = std::make_shared<OptionsTab>();
  67. tabTurnOptions = std::make_shared<TurnOptionsTab>();
  68. tabExtraOptions = std::make_shared<ExtraOptionsTab>();
  69. tabRand = std::make_shared<RandomMapTab>();
  70. tabRand->mapInfoChanged += std::bind(&IServerAPI::setMapInfo, &GAME->server(), _1, _2);
  71. buttonRMG = std::make_shared<CButton>(Point(411, 105), AnimationPath::builtin("GSPBUTT.DEF"), LIBRARY->generaltexth->zelp[47], 0, EShortcut::LOBBY_RANDOM_MAP);
  72. buttonRMG->addCallback([this]()
  73. {
  74. toggleTab(tabRand);
  75. if (getMapInfo() && !getMapInfo()->isRandomMap)
  76. tabRand->updateMapInfoByHost();
  77. });
  78. card->iconDifficulty->addCallback(std::bind(&IServerAPI::setDifficulty, &GAME->server(), _1));
  79. buttonStart = std::make_shared<CButton>(Point(411, 535), AnimationPath::builtin("SCNRBEG.DEF"), LIBRARY->generaltexth->zelp[103], std::bind(&CLobbyScreen::startScenario, this, false), EShortcut::LOBBY_BEGIN_STANDARD_GAME);
  80. initLobby();
  81. break;
  82. }
  83. case ESelectionScreen::loadGame:
  84. {
  85. tabOpt = std::make_shared<OptionsTab>();
  86. tabTurnOptions = std::make_shared<TurnOptionsTab>();
  87. tabExtraOptions = std::make_shared<ExtraOptionsTab>();
  88. buttonStart = std::make_shared<CButton>(Point(411, 535), AnimationPath::builtin("SCNRLOD.DEF"), LIBRARY->generaltexth->zelp[103], std::bind(&CLobbyScreen::startScenario, this, false), EShortcut::LOBBY_LOAD_GAME);
  89. initLobby();
  90. break;
  91. }
  92. case ESelectionScreen::campaignList:
  93. tabSel->callOnSelect = std::bind(&IServerAPI::setMapInfo, &GAME->server(), _1, nullptr);
  94. buttonStart = std::make_shared<CButton>(Point(411, 535), AnimationPath::builtin("SCNRLOD.DEF"), CButton::tooltip(), std::bind(&CLobbyScreen::startCampaign, this), EShortcut::LOBBY_BEGIN_CAMPAIGN);
  95. break;
  96. }
  97. buttonStart->block(true); // to be unblocked after map list is ready
  98. buttonBack = std::make_shared<CButton>(Point(581, 535), AnimationPath::builtin("SCNRBACK.DEF"), LIBRARY->generaltexth->zelp[105], [&]()
  99. {
  100. bool wasInLobbyRoom = GAME->server().inLobbyRoom();
  101. GAME->server().sendClientDisconnecting();
  102. close();
  103. if (wasInLobbyRoom)
  104. GAME->server().getGlobalLobby().activateInterface();
  105. }, EShortcut::GLOBAL_CANCEL);
  106. if(hideScreen) // workaround to avoid confusing players by custom campaign list displaying for a few ms -> instead of this draw a black screen while "loading"
  107. {
  108. blackScreen = std::make_shared<GraphicalPrimitiveCanvas>(Rect(Point(0, 0), pos.dimensions()));
  109. blackScreen->addBox(Point(0, 0), pos.dimensions(), Colors::BLACK);
  110. }
  111. }
  112. CLobbyScreen::~CLobbyScreen()
  113. {
  114. // TODO: For now we always destroy whole lobby when leaving bonus selection screen
  115. if(GAME->server().getState() == EClientState::LOBBY_CAMPAIGN)
  116. GAME->server().sendClientDisconnecting();
  117. }
  118. void CLobbyScreen::toggleTab(std::shared_ptr<CIntObject> tab)
  119. {
  120. if(tab == curTab)
  121. GAME->server().sendGuiAction(LobbyGuiAction::NO_TAB);
  122. else if(tab == tabOpt)
  123. GAME->server().sendGuiAction(LobbyGuiAction::OPEN_OPTIONS);
  124. else if(tab == tabSel)
  125. GAME->server().sendGuiAction(LobbyGuiAction::OPEN_SCENARIO_LIST);
  126. else if(tab == tabRand)
  127. GAME->server().sendGuiAction(LobbyGuiAction::OPEN_RANDOM_MAP_OPTIONS);
  128. else if(tab == tabTurnOptions)
  129. GAME->server().sendGuiAction(LobbyGuiAction::OPEN_TURN_OPTIONS);
  130. else if(tab == tabExtraOptions)
  131. GAME->server().sendGuiAction(LobbyGuiAction::OPEN_EXTRA_OPTIONS);
  132. CSelectionBase::toggleTab(tab);
  133. }
  134. void CLobbyScreen::startCampaign()
  135. {
  136. if(!GAME->server().mi)
  137. return;
  138. try {
  139. auto ourCampaign = CampaignHandler::getCampaign(GAME->server().mi->fileURI);
  140. GAME->server().setCampaignState(ourCampaign);
  141. }
  142. catch (const std::runtime_error &e)
  143. {
  144. // handle possible exception on map loading. For example campaign that contains map in unsupported format
  145. // for example, wog campaigns or hota campaigns without hota map support mod
  146. MetaString message;
  147. message.appendTextID("vcmi.client.errors.invalidMap");
  148. message.replaceRawString(e.what());
  149. CInfoWindow::showInfoDialog(message.toString(), {});
  150. }
  151. }
  152. void CLobbyScreen::startScenario(bool allowOnlyAI)
  153. {
  154. if (tabRand && GAME->server().si->mapGenOptions)
  155. {
  156. // Save RMG settings at game start
  157. tabRand->saveOptions(*GAME->server().si->mapGenOptions);
  158. }
  159. // Save chosen difficulty
  160. Settings lastDifficulty = settings.write["general"]["lastDifficulty"];
  161. lastDifficulty->Integer() = getCurrentDifficulty();
  162. if (GAME->server().validateGameStart(allowOnlyAI))
  163. {
  164. GAME->server().sendStartGame(allowOnlyAI);
  165. buttonStart->block(true);
  166. }
  167. }
  168. void CLobbyScreen::toggleMode(bool host)
  169. {
  170. tabSel->toggleMode();
  171. buttonStart->block(!host);
  172. if(screenType == ESelectionScreen::campaignList)
  173. return;
  174. auto buttonColor = host ? Colors::WHITE : Colors::ORANGE;
  175. buttonSelect->setTextOverlay(" " + LIBRARY->generaltexth->allTexts[500], FONT_SMALL, buttonColor);
  176. buttonOptions->setTextOverlay(LIBRARY->generaltexth->allTexts[501], FONT_SMALL, buttonColor);
  177. if (buttonTurnOptions)
  178. buttonTurnOptions->setTextOverlay(LIBRARY->generaltexth->translate("vcmi.optionsTab.turnOptions.hover"), FONT_SMALL, buttonColor);
  179. if (buttonExtraOptions)
  180. buttonExtraOptions->setTextOverlay(LIBRARY->generaltexth->translate("vcmi.optionsTab.extraOptions.hover"), FONT_SMALL, buttonColor);
  181. if(buttonRMG)
  182. {
  183. buttonRMG->setTextOverlay(" " + LIBRARY->generaltexth->allTexts[740], FONT_SMALL, buttonColor);
  184. buttonRMG->block(!host);
  185. }
  186. buttonSelect->block(!host);
  187. buttonOptions->block(!host);
  188. if (buttonTurnOptions)
  189. buttonTurnOptions->block(!host);
  190. if (buttonExtraOptions)
  191. buttonExtraOptions->block(!host);
  192. if(GAME->server().mi)
  193. {
  194. tabOpt->recreate();
  195. tabTurnOptions->recreate();
  196. tabExtraOptions->recreate();
  197. }
  198. }
  199. void CLobbyScreen::toggleChat()
  200. {
  201. card->toggleChat();
  202. if(card->showChat)
  203. buttonChat->setTextOverlay(LIBRARY->generaltexth->allTexts[531], FONT_SMALL, Colors::WHITE);
  204. else
  205. buttonChat->setTextOverlay(LIBRARY->generaltexth->allTexts[532], FONT_SMALL, Colors::WHITE);
  206. }
  207. void CLobbyScreen::updateAfterStateChange()
  208. {
  209. if(GAME->server().isHost() && screenType == ESelectionScreen::newGame)
  210. {
  211. bool isMultiplayer = GAME->server().loadMode == ELoadMode::MULTI;
  212. ExtraOptionsInfo info = SEL->getStartInfo()->extraOptionsInfo;
  213. info.cheatsAllowed = isMultiplayer ? persistentStorage["startExtraOptions"]["multiPlayer"]["cheatsAllowed"].Bool() : !persistentStorage["startExtraOptions"]["singlePlayer"]["cheatsNotAllowed"].Bool();
  214. info.unlimitedReplay = persistentStorage["startExtraOptions"][isMultiplayer ? "multiPlayer" : "singlePlayer"]["unlimitedReplay"].Bool();
  215. if(info.cheatsAllowed != GAME->server().si->extraOptionsInfo.cheatsAllowed || info.unlimitedReplay != GAME->server().si->extraOptionsInfo.unlimitedReplay)
  216. GAME->server().setExtraOptionsInfo(info);
  217. }
  218. if(GAME->server().mi)
  219. {
  220. if (tabOpt)
  221. tabOpt->recreate();
  222. if (tabTurnOptions)
  223. tabTurnOptions->recreate();
  224. if (tabExtraOptions)
  225. tabExtraOptions->recreate();
  226. }
  227. buttonStart->block(GAME->server().mi == nullptr || GAME->server().isGuest());
  228. card->changeSelection();
  229. if (card->iconDifficulty)
  230. {
  231. if (screenType == ESelectionScreen::loadGame)
  232. {
  233. // When loading the game, only one button in the difficulty toggle group should be enabled, so here disable all other buttons first, then make selection
  234. card->iconDifficulty->setSelectedOnly(GAME->server().si->difficulty);
  235. }
  236. else
  237. {
  238. card->iconDifficulty->setSelected(GAME->server().si->difficulty);
  239. }
  240. }
  241. if(curTab && curTab == tabRand && GAME->server().si->mapGenOptions)
  242. tabRand->setMapGenOptions(GAME->server().si->mapGenOptions);
  243. }
  244. const StartInfo * CLobbyScreen::getStartInfo()
  245. {
  246. return GAME->server().si.get();
  247. }
  248. const CMapInfo * CLobbyScreen::getMapInfo()
  249. {
  250. return GAME->server().mi.get();
  251. }