CLobbyScreen.cpp 10 KB

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