CLobbyScreen.cpp 11 KB

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