CPreGame.h 18 KB

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