CLobbyScreen.cpp 10 KB

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