CPreGame.h 19 KB

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