CPreGame.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652
  1. #pragma once
  2. //#include "../lib/filesystem/Filesystem.h"
  3. #include "../lib/StartInfo.h"
  4. #include "../lib/FunctionList.h"
  5. #include "../lib/mapping/CMapInfo.h"
  6. #include "../lib/rmg/CMapGenerator.h"
  7. #include "windows/CWindowObject.h"
  8. /*
  9. * CPreGame.h, part of VCMI engine
  10. *
  11. * Authors: listed in file AUTHORS in main folder
  12. *
  13. * License: GNU General Public License v2.0 or later
  14. * Full text of license available in license.txt file, in main folder
  15. *
  16. */
  17. class CMapInfo;
  18. class CMusicHandler;
  19. class CMapHeader;
  20. class CCampaignHeader;
  21. class CTextInput;
  22. class CCampaign;
  23. class CGStatusBar;
  24. class CTextBox;
  25. class CCampaignState;
  26. class CConnection;
  27. class JsonNode;
  28. class CMapGenOptions;
  29. class CRandomMapTab;
  30. struct CPackForSelectionScreen;
  31. struct PlayerInfo;
  32. class CMultiLineLabel;
  33. class CToggleButton;
  34. class CToggleGroup;
  35. class CTabbedInt;
  36. class CButton;
  37. class CSlider;
  38. namespace boost{ class thread; class recursive_mutex;}
  39. enum ESortBy{_playerAm, _size, _format, _name, _viccon, _loscon, _numOfMaps, _fileName}; //_numOfMaps is for campaigns
  40. /// Class which handles map sorting by different criteria
  41. class mapSorter
  42. {
  43. public:
  44. ESortBy sortBy;
  45. bool operator()(const CMapInfo *aaa, const CMapInfo *bbb);
  46. mapSorter(ESortBy es):sortBy(es){};
  47. };
  48. /// The main menu screens listed in the EState enum
  49. class CMenuScreen : public CIntObject
  50. {
  51. const JsonNode& config;
  52. CTabbedInt *tabs;
  53. CPicture * background;
  54. std::vector<CPicture*> images;
  55. CIntObject *createTab(size_t index);
  56. public:
  57. std::vector<std::string> menuNameToEntry;
  58. enum EState { //where are we?
  59. mainMenu, newGame, loadGame, campaignMain, saveGame, scenarioInfo, campaignList
  60. };
  61. enum EMultiMode {
  62. SINGLE_PLAYER = 0, MULTI_HOT_SEAT, MULTI_NETWORK_HOST, MULTI_NETWORK_GUEST
  63. };
  64. CMenuScreen(const JsonNode& configNode);
  65. void showAll(SDL_Surface * to);
  66. void show(SDL_Surface * to);
  67. void activate();
  68. void deactivate();
  69. void switchToTab(size_t index);
  70. };
  71. class CMenuEntry : public CIntObject
  72. {
  73. std::vector<CPicture*> images;
  74. std::vector<CButton*> buttons;
  75. CButton* createButton(CMenuScreen* parent, const JsonNode& button);
  76. public:
  77. CMenuEntry(CMenuScreen* parent, const JsonNode &config);
  78. };
  79. class CreditsScreen : public CIntObject
  80. {
  81. int positionCounter;
  82. CMultiLineLabel* credits;
  83. public:
  84. CreditsScreen();
  85. void show(SDL_Surface * to);
  86. void clickLeft(tribool down, bool previousState);
  87. void clickRight(tribool down, bool previousState);
  88. };
  89. /// Implementation of the chat box
  90. class CChatBox : public CIntObject
  91. {
  92. public:
  93. CTextBox *chatHistory;
  94. CTextInput *inputBox;
  95. CChatBox(const Rect &rect);
  96. void keyPressed(const SDL_KeyboardEvent & key);
  97. void addNewMessage(const std::string &text);
  98. };
  99. class InfoCard : public CIntObject
  100. {
  101. public:
  102. CPicture *bg;
  103. CMenuScreen::EState type;
  104. bool network;
  105. bool chatOn; //if chat is shown, then description is hidden
  106. CTextBox *mapDescription;
  107. CChatBox *chat;
  108. CPicture *playerListBg;
  109. CToggleGroup *difficulty;
  110. CDefHandler *sizes, *sFlags;
  111. void changeSelection(const CMapInfo *to);
  112. void showAll(SDL_Surface * to);
  113. void clickRight(tribool down, bool previousState);
  114. void showTeamsPopup();
  115. void toggleChat();
  116. void setChat(bool activateChat);
  117. InfoCard(bool Network = false);
  118. ~InfoCard();
  119. };
  120. /// The selection tab which is shown at the map selection screen
  121. class SelectionTab : public CIntObject
  122. {
  123. private:
  124. CDefHandler *format; //map size
  125. void parseMaps(const std::unordered_set<ResourceID> &files);
  126. void parseGames(const std::unordered_set<ResourceID> &files, bool multi);
  127. void parseCampaigns(const std::unordered_set<ResourceID> & files );
  128. std::unordered_set<ResourceID> getFiles(std::string dirURI, int resType);
  129. CMenuScreen::EState tabType;
  130. public:
  131. int positions; //how many entries (games/maps) can be shown
  132. CPicture *bg; //general bg image
  133. CSlider *slider;
  134. std::vector<CMapInfo> allItems;
  135. std::vector<CMapInfo*> curItems;
  136. size_t selectionPos;
  137. std::function<void(CMapInfo *)> onSelect;
  138. ESortBy sortingBy;
  139. ESortBy generalSortingBy;
  140. bool ascending;
  141. CTextInput *txt;
  142. void filter(int size, bool selectFirst = false); //0 - all
  143. void select(int position); //position: <0 - positions> position on the screen
  144. void selectAbs(int position); //position: absolute position in curItems vector
  145. int getPosition(int x, int y); //convert mouse coords to entry position; -1 means none
  146. void sliderMove(int slidPos);
  147. void sortBy(int criteria);
  148. void sort();
  149. void printMaps(SDL_Surface *to);
  150. int getLine();
  151. void selectFName(std::string fname);
  152. const CMapInfo * getSelectedMapInfo() const;
  153. void showAll(SDL_Surface * to);
  154. void clickLeft(tribool down, bool previousState);
  155. void keyPressed(const SDL_KeyboardEvent & key);
  156. void onDoubleClick();
  157. SelectionTab(CMenuScreen::EState Type, const std::function<void(CMapInfo *)> &OnSelect, CMenuScreen::EMultiMode MultiPlayer = CMenuScreen::SINGLE_PLAYER);
  158. ~SelectionTab();
  159. };
  160. /// The options tab which is shown at the map selection phase.
  161. class OptionsTab : public CIntObject
  162. {
  163. CPicture *bg;
  164. public:
  165. enum SelType {TOWN, HERO, BONUS};
  166. struct CPlayerSettingsHelper
  167. {
  168. const PlayerSettings & settings;
  169. const SelType type;
  170. CPlayerSettingsHelper(const PlayerSettings & settings, SelType type):
  171. settings(settings),
  172. type(type)
  173. {}
  174. /// visible image settings
  175. size_t getImageIndex();
  176. std::string getImageName();
  177. std::string getName(); /// name visible in options dialog
  178. std::string getTitle(); /// title in popup box
  179. std::string getSubtitle(); /// popup box subtitle
  180. std::string getDescription();/// popup box description, not always present
  181. };
  182. class CPregameTooltipBox : public CWindowObject, public CPlayerSettingsHelper
  183. {
  184. void genHeader();
  185. void genTownWindow();
  186. void genHeroWindow();
  187. void genBonusWindow();
  188. public:
  189. CPregameTooltipBox(CPlayerSettingsHelper & helper);
  190. };
  191. struct SelectedBox : public CIntObject, public CPlayerSettingsHelper //img with current town/hero/bonus
  192. {
  193. CAnimImage * image;
  194. CLabel *subtitle;
  195. SelectedBox(Point position, PlayerSettings & settings, SelType type);
  196. void clickRight(tribool down, bool previousState);
  197. void update();
  198. };
  199. struct PlayerOptionsEntry : public CIntObject
  200. {
  201. PlayerInfo &pi;
  202. PlayerSettings &s;
  203. CPicture *bg;
  204. CButton *btns[6]; //left and right for town, hero, bonus
  205. CButton *flag;
  206. SelectedBox *town;
  207. SelectedBox *hero;
  208. SelectedBox *bonus;
  209. enum {HUMAN_OR_CPU, HUMAN, CPU} whoCanPlay;
  210. PlayerOptionsEntry(OptionsTab *owner, PlayerSettings &S);
  211. void selectButtons(); //hides unavailable buttons
  212. void showAll(SDL_Surface * to);
  213. void update();
  214. };
  215. CSlider *turnDuration;
  216. std::set<int> usedHeroes;
  217. struct PlayerToRestore
  218. {
  219. PlayerColor color;
  220. int id;
  221. void reset() { id = -1; color = PlayerColor::CANNOT_DETERMINE; }
  222. PlayerToRestore(){ reset(); }
  223. } playerToRestore;
  224. std::map<PlayerColor, PlayerOptionsEntry *> entries; //indexed by color
  225. void nextCastle(PlayerColor player, int dir); //dir == -1 or +1
  226. void nextHero(PlayerColor player, int dir); //dir == -1 or +1
  227. void nextBonus(PlayerColor player, int dir); //dir == -1 or +1
  228. void setTurnLength(int npos);
  229. void flagPressed(PlayerColor player);
  230. void recreate();
  231. OptionsTab();
  232. ~OptionsTab();
  233. void showAll(SDL_Surface * to);
  234. int nextAllowedHero(PlayerColor player, int min, int max, int incl, int dir );
  235. bool canUseThisHero(PlayerColor player, int ID );
  236. };
  237. /// The random map tab shows options for generating a random map.
  238. class CRandomMapTab : public CIntObject
  239. {
  240. public:
  241. CRandomMapTab();
  242. void showAll(SDL_Surface * to);
  243. void updateMapInfo();
  244. CFunctionList<void (const CMapInfo *)> & getMapInfoChanged();
  245. const CMapInfo * getMapInfo() const;
  246. const CMapGenOptions & getMapGenOptions() const;
  247. void setMapGenOptions(shared_ptr<CMapGenOptions> opts);
  248. private:
  249. void addButtonsToGroup(CToggleGroup * group, const std::vector<std::string> & defs, int startIndex, int endIndex, int btnWidth, int helpStartIndex) const;
  250. void addButtonsWithRandToGroup(CToggleGroup * group, const std::vector<std::string> & defs, int startIndex, int endIndex, int btnWidth, int helpStartIndex, int helpRandIndex) const;
  251. void deactivateButtonsFrom(CToggleGroup * group, int startId);
  252. void validatePlayersCnt(int playersCnt);
  253. void validateCompOnlyPlayersCnt(int compOnlyPlayersCnt);
  254. std::vector<int> getPossibleMapSizes();
  255. CPicture * bg;
  256. CToggleButton * twoLevelsBtn;
  257. CToggleGroup * mapSizeBtnGroup, * playersCntGroup, * teamsCntGroup, * compOnlyPlayersCntGroup,
  258. * compOnlyTeamsCntGroup, * waterContentGroup, * monsterStrengthGroup;
  259. CButton * showRandMaps;
  260. CMapGenOptions mapGenOptions;
  261. unique_ptr<CMapInfo> mapInfo;
  262. CFunctionList<void(const CMapInfo *)> mapInfoChanged;
  263. };
  264. /// Interface for selecting a map.
  265. class ISelectionScreenInfo
  266. {
  267. public:
  268. CMenuScreen::EMultiMode multiPlayer;
  269. CMenuScreen::EState screenType; //new/save/load#Game
  270. const CMapInfo *current;
  271. StartInfo sInfo;
  272. std::map<ui8, std::string> playerNames; // id of player <-> player name; 0 is reserved as ID of AI "players"
  273. ISelectionScreenInfo(const std::map<ui8, std::string> *Names = nullptr);
  274. virtual ~ISelectionScreenInfo();
  275. virtual void update(){};
  276. virtual void propagateOptions() {};
  277. virtual void postRequest(ui8 what, ui8 dir) {};
  278. virtual void postChatMessage(const std::string &txt){};
  279. void setPlayer(PlayerSettings &pset, ui8 player);
  280. void updateStartInfo( std::string filename, StartInfo & sInfo, const CMapHeader * mapHeader );
  281. ui8 getIdOfFirstUnallocatedPlayer(); //returns 0 if none
  282. bool isGuest() const;
  283. bool isHost() const;
  284. };
  285. /// The actual map selection screen which consists of the options and selection tab
  286. class CSelectionScreen : public CIntObject, public ISelectionScreenInfo
  287. {
  288. bool bordered;
  289. public:
  290. CPicture *bg; //general bg image
  291. InfoCard *card;
  292. OptionsTab *opt;
  293. CRandomMapTab * randMapTab;
  294. CButton *start, *back;
  295. SelectionTab *sel;
  296. CIntObject *curTab;
  297. boost::thread *serverHandlingThread;
  298. boost::recursive_mutex *mx;
  299. std::list<CPackForSelectionScreen *> upcomingPacks; //protected by mx
  300. CConnection *serv; //connection to server, used in MP mode
  301. bool ongoingClosing;
  302. ui8 myNameID; //used when networking - otherwise all player are "mine"
  303. CSelectionScreen(CMenuScreen::EState Type, CMenuScreen::EMultiMode MultiPlayer = CMenuScreen::SINGLE_PLAYER, const std::map<ui8, std::string> * Names = nullptr, const std::string & Address = "", const std::string & Port = "");
  304. ~CSelectionScreen();
  305. void toggleTab(CIntObject *tab);
  306. void changeSelection(const CMapInfo *to);
  307. void startCampaign();
  308. void startScenario();
  309. void difficultyChange(int to);
  310. void handleConnection();
  311. void processPacks();
  312. void setSInfo(const StartInfo &si);
  313. void update() override;
  314. void propagateOptions() override;
  315. void postRequest(ui8 what, ui8 dir) override;
  316. void postChatMessage(const std::string &txt) override;
  317. void propagateNames();
  318. void showAll(SDL_Surface *to);
  319. };
  320. /// Save game screen
  321. class CSavingScreen : public CSelectionScreen
  322. {
  323. public:
  324. const CMapInfo *ourGame;
  325. CSavingScreen(bool hotseat = false);
  326. ~CSavingScreen();
  327. };
  328. /// Scenario information screen shown during the game (thus not really a "pre-game" but fits here anyway)
  329. class CScenarioInfo : public CIntObject, public ISelectionScreenInfo
  330. {
  331. public:
  332. CButton *back;
  333. InfoCard *card;
  334. OptionsTab *opt;
  335. CScenarioInfo(const CMapHeader *mapHeader, const StartInfo *startInfo);
  336. ~CScenarioInfo();
  337. };
  338. /// Multiplayer mode
  339. class CMultiMode : public CIntObject
  340. {
  341. public:
  342. CPicture *bg;
  343. CTextInput *txt;
  344. CButton *btns[7]; //0 - hotseat, 6 - cancel
  345. CGStatusBar *bar;
  346. CMultiMode();
  347. void openHotseat();
  348. void hostTCP();
  349. void joinTCP();
  350. };
  351. /// Hot seat player window
  352. class CHotSeatPlayers : public CIntObject
  353. {
  354. CPicture *bg;
  355. CTextBox *title;
  356. CTextInput* txt[8];
  357. CButton *ok, *cancel;
  358. CGStatusBar *bar;
  359. void onChange(std::string newText);
  360. void enterSelectionScreen();
  361. public:
  362. CHotSeatPlayers(const std::string &firstPlayer);
  363. };
  364. class CPrologEpilogVideo : public CWindowObject
  365. {
  366. CCampaignScenario::SScenarioPrologEpilog spe;
  367. int positionCounter;
  368. int voiceSoundHandle;
  369. std::function<void()> exitCb;
  370. CMultiLineLabel * text;
  371. public:
  372. CPrologEpilogVideo(CCampaignScenario::SScenarioPrologEpilog _spe, std::function<void()> callback);
  373. void clickLeft(tribool down, bool previousState);
  374. void show(SDL_Surface * to);
  375. };
  376. /// Campaign screen where you can choose one out of three starting bonuses
  377. class CBonusSelection : public CIntObject
  378. {
  379. public:
  380. CBonusSelection(const std::string & campaignFName);
  381. CBonusSelection(shared_ptr<CCampaignState> _ourCampaign);
  382. ~CBonusSelection();
  383. void showAll(SDL_Surface * to) override;
  384. void show(SDL_Surface * to) override;
  385. private:
  386. struct SCampPositions
  387. {
  388. std::string campPrefix;
  389. int colorSuffixLength;
  390. struct SRegionDesc
  391. {
  392. std::string infix;
  393. int xpos, ypos;
  394. };
  395. std::vector<SRegionDesc> regions;
  396. };
  397. class CRegion : public CIntObject
  398. {
  399. CBonusSelection * owner;
  400. SDL_Surface* graphics[3]; //[0] - not selected, [1] - selected, [2] - striped
  401. bool accessible; //false if region should be striped
  402. bool selectable; //true if region should be selectable
  403. int myNumber; //number of region
  404. public:
  405. std::string rclickText;
  406. CRegion(CBonusSelection * _owner, bool _accessible, bool _selectable, int _myNumber);
  407. ~CRegion();
  408. void clickLeft(tribool down, bool previousState);
  409. void clickRight(tribool down, bool previousState);
  410. void show(SDL_Surface * to);
  411. };
  412. void init();
  413. void loadPositionsOfGraphics();
  414. void updateStartButtonState(int selected = -1); //-1 -- no bonus is selected
  415. void updateBonusSelection();
  416. void updateCampaignState();
  417. // Event handlers
  418. void goBack();
  419. void startMap();
  420. void restartMap();
  421. void selectMap(int mapNr, bool initialSelect);
  422. void selectBonus(int id);
  423. void increaseDifficulty();
  424. void decreaseDifficulty();
  425. // GUI components
  426. SDL_Surface * background;
  427. CButton * startB, * restartB, * backB;
  428. CTextBox * campaignDescription, * mapDescription;
  429. std::vector<SCampPositions> campDescriptions;
  430. std::vector<CRegion *> regions;
  431. CRegion * highlightedRegion;
  432. CToggleGroup * bonuses;
  433. SDL_Surface * diffPics[5]; //pictures of difficulties, user-selectable (or not if campaign locks this)
  434. CButton * diffLb, * diffRb; //buttons for changing difficulty
  435. CDefHandler * sizes; //icons of map sizes
  436. CDefHandler * sFlags;
  437. // Data
  438. shared_ptr<CCampaignState> ourCampaign;
  439. int selectedMap;
  440. boost::optional<int> selectedBonus;
  441. StartInfo startInfo;
  442. CMapHeader * ourHeader;
  443. };
  444. /// Campaign selection screen
  445. class CCampaignScreen : public CIntObject
  446. {
  447. public:
  448. enum CampaignStatus {DEFAULT = 0, ENABLED, DISABLED, COMPLETED}; // the status of the campaign
  449. private:
  450. /// A button which plays a video when you move the mouse cursor over it
  451. class CCampaignButton : public CIntObject
  452. {
  453. private:
  454. CPicture *image;
  455. CPicture *checkMark;
  456. CLabel *hoverLabel;
  457. CampaignStatus status;
  458. std::string campFile; // the filename/resourcename of the campaign
  459. std::string video; // the resource name of the video
  460. std::string hoverText;
  461. void clickLeft(tribool down, bool previousState);
  462. void hover(bool on);
  463. public:
  464. CCampaignButton(const JsonNode &config );
  465. void show(SDL_Surface * to);
  466. };
  467. CButton *back;
  468. std::vector<CCampaignButton*> campButtons;
  469. std::vector<CPicture*> images;
  470. CButton* createExitButton(const JsonNode& button);
  471. public:
  472. enum CampaignSet {ROE, AB, SOD, WOG};
  473. CCampaignScreen(const JsonNode &config);
  474. void showAll(SDL_Surface *to);
  475. };
  476. /// Manages the configuration of pregame GUI elements like campaign screen, main menu, loading screen,...
  477. class CGPreGameConfig
  478. {
  479. public:
  480. static CGPreGameConfig & get();
  481. const JsonNode & getConfig() const;
  482. const JsonNode & getCampaigns() const;
  483. private:
  484. CGPreGameConfig();
  485. const JsonNode campaignSets;
  486. const JsonNode config;
  487. };
  488. /// Handles background screen, loads graphics for victory/loss condition and random town or hero selection
  489. class CGPreGame : public CIntObject, public IUpdateable
  490. {
  491. void loadGraphics();
  492. void disposeGraphics();
  493. CGPreGame(); //Use createIfNotPresent
  494. public:
  495. CMenuScreen * menu;
  496. CDefHandler *victory, *loss;
  497. ~CGPreGame();
  498. void update() override;
  499. void openSel(CMenuScreen::EState type, CMenuScreen::EMultiMode multi = CMenuScreen::SINGLE_PLAYER);
  500. void openCampaignScreen(std::string name);
  501. static CGPreGame * create();
  502. void removeFromGui();
  503. static void showLoadingScreen(std::function<void()> loader);
  504. };
  505. class CLoadingScreen : public CWindowObject
  506. {
  507. boost::thread loadingThread;
  508. std::string getBackground();
  509. public:
  510. CLoadingScreen(std::function<void()> loader);
  511. ~CLoadingScreen();
  512. void showAll(SDL_Surface *to);
  513. };
  514. /// Simple window to enter the server's address.
  515. class CSimpleJoinScreen : public CIntObject
  516. {
  517. CPicture * bg;
  518. CTextBox * title;
  519. CButton * ok, * cancel;
  520. CGStatusBar * bar;
  521. CTextInput * address;
  522. CTextInput * port;
  523. void enterSelectionScreen();
  524. void onChange(const std::string & newText);
  525. public:
  526. CSimpleJoinScreen();
  527. };
  528. extern ISelectionScreenInfo *SEL;
  529. extern CGPreGame *CGP;