CPreGame.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651
  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. bool network;
  109. bool chatOn; //if chat is shown, then description is hidden
  110. CTextBox *mapDescription;
  111. CChatBox *chat;
  112. CPicture *playerListBg;
  113. CToggleGroup *difficulty;
  114. void changeSelection(const CMapInfo *to);
  115. void showAll(SDL_Surface * to) override;
  116. void clickRight(tribool down, bool previousState) override;
  117. void showTeamsPopup();
  118. void toggleChat();
  119. void setChat(bool activateChat);
  120. InfoCard(bool Network = false);
  121. ~InfoCard();
  122. };
  123. /// The selection tab which is shown at the map selection screen
  124. class SelectionTab : public CIntObject
  125. {
  126. private:
  127. std::shared_ptr<CAnimation> formatIcons;
  128. void parseMaps(const std::unordered_set<ResourceID> &files);
  129. void parseGames(const std::unordered_set<ResourceID> &files, bool multi);
  130. void parseCampaigns(const std::unordered_set<ResourceID> & files );
  131. std::unordered_set<ResourceID> getFiles(std::string dirURI, int resType);
  132. CMenuScreen::EState tabType;
  133. public:
  134. int positions; //how many entries (games/maps) can be shown
  135. CPicture *bg; //general bg image
  136. CSlider *slider;
  137. std::vector<CMapInfo> allItems;
  138. std::vector<CMapInfo*> curItems;
  139. size_t selectionPos;
  140. std::function<void(CMapInfo *)> onSelect;
  141. ESortBy sortingBy;
  142. ESortBy generalSortingBy;
  143. bool ascending;
  144. CTextInput *txt;
  145. void filter(int size, bool selectFirst = false); //0 - all
  146. void select(int position); //position: <0 - positions> position on the screen
  147. void selectAbs(int position); //position: absolute position in curItems vector
  148. int getPosition(int x, int y); //convert mouse coords to entry position; -1 means none
  149. void sliderMove(int slidPos);
  150. void sortBy(int criteria);
  151. void sort();
  152. void printMaps(SDL_Surface *to);
  153. int getLine();
  154. void selectFName(std::string fname);
  155. const CMapInfo * getSelectedMapInfo() const;
  156. void showAll(SDL_Surface * to) override;
  157. void clickLeft(tribool down, bool previousState) override;
  158. void keyPressed(const SDL_KeyboardEvent & key) override;
  159. void onDoubleClick() override;
  160. SelectionTab(CMenuScreen::EState Type, const std::function<void(CMapInfo *)> &OnSelect, CMenuScreen::EMultiMode MultiPlayer = CMenuScreen::SINGLE_PLAYER);
  161. ~SelectionTab();
  162. };
  163. /// The options tab which is shown at the map selection phase.
  164. class OptionsTab : public CIntObject
  165. {
  166. CPicture *bg;
  167. public:
  168. enum SelType {TOWN, HERO, BONUS};
  169. struct CPlayerSettingsHelper
  170. {
  171. const PlayerSettings & settings;
  172. const SelType type;
  173. CPlayerSettingsHelper(const PlayerSettings & settings, SelType type):
  174. settings(settings),
  175. type(type)
  176. {}
  177. /// visible image settings
  178. size_t getImageIndex();
  179. std::string getImageName();
  180. std::string getName(); /// name visible in options dialog
  181. std::string getTitle(); /// title in popup box
  182. std::string getSubtitle(); /// popup box subtitle
  183. std::string getDescription();/// popup box description, not always present
  184. };
  185. class CPregameTooltipBox : public CWindowObject, public CPlayerSettingsHelper
  186. {
  187. void genHeader();
  188. void genTownWindow();
  189. void genHeroWindow();
  190. void genBonusWindow();
  191. public:
  192. CPregameTooltipBox(CPlayerSettingsHelper & helper);
  193. };
  194. struct SelectedBox : public CIntObject, public CPlayerSettingsHelper //img with current town/hero/bonus
  195. {
  196. CAnimImage * image;
  197. CLabel *subtitle;
  198. SelectedBox(Point position, PlayerSettings & settings, SelType type);
  199. void clickRight(tribool down, bool previousState) override;
  200. void update();
  201. };
  202. struct PlayerOptionsEntry : public CIntObject
  203. {
  204. PlayerInfo &pi;
  205. PlayerSettings &s;
  206. CPicture *bg;
  207. CButton *btns[6]; //left and right for town, hero, bonus
  208. CButton *flag;
  209. SelectedBox *town;
  210. SelectedBox *hero;
  211. SelectedBox *bonus;
  212. enum {HUMAN_OR_CPU, HUMAN, CPU} whoCanPlay;
  213. PlayerOptionsEntry(OptionsTab *owner, PlayerSettings &S);
  214. void selectButtons(); //hides unavailable buttons
  215. void showAll(SDL_Surface * to) override;
  216. void update();
  217. };
  218. CSlider *turnDuration;
  219. std::set<int> usedHeroes;
  220. struct PlayerToRestore
  221. {
  222. PlayerColor color;
  223. int id;
  224. void reset() { id = -1; color = PlayerColor::CANNOT_DETERMINE; }
  225. PlayerToRestore(){ reset(); }
  226. } playerToRestore;
  227. std::map<PlayerColor, PlayerOptionsEntry *> entries; //indexed by color
  228. void nextCastle(PlayerColor player, int dir); //dir == -1 or +1
  229. void nextHero(PlayerColor player, int dir); //dir == -1 or +1
  230. void nextBonus(PlayerColor player, int dir); //dir == -1 or +1
  231. void setTurnLength(int npos);
  232. void flagPressed(PlayerColor player);
  233. void recreate();
  234. OptionsTab();
  235. ~OptionsTab();
  236. void showAll(SDL_Surface * to) override;
  237. int nextAllowedHero(PlayerColor player, int min, int max, int incl, int dir );
  238. bool canUseThisHero(PlayerColor player, int ID );
  239. };
  240. /// The random map tab shows options for generating a random map.
  241. class CRandomMapTab : public CIntObject
  242. {
  243. public:
  244. CRandomMapTab();
  245. void showAll(SDL_Surface * to) override;
  246. void updateMapInfo();
  247. CFunctionList<void (const CMapInfo *)> & getMapInfoChanged();
  248. const CMapInfo * getMapInfo() const;
  249. const CMapGenOptions & getMapGenOptions() const;
  250. void setMapGenOptions(std::shared_ptr<CMapGenOptions> opts);
  251. private:
  252. void addButtonsToGroup(CToggleGroup * group, const std::vector<std::string> & defs, int startIndex, int endIndex, int btnWidth, int helpStartIndex) const;
  253. void addButtonsWithRandToGroup(CToggleGroup * group, const std::vector<std::string> & defs, int startIndex, int endIndex, int btnWidth, int helpStartIndex, int helpRandIndex) const;
  254. void deactivateButtonsFrom(CToggleGroup * group, int startId);
  255. void validatePlayersCnt(int playersCnt);
  256. void validateCompOnlyPlayersCnt(int compOnlyPlayersCnt);
  257. std::vector<int> getPossibleMapSizes();
  258. CPicture * bg;
  259. CToggleButton * twoLevelsBtn;
  260. CToggleGroup * mapSizeBtnGroup, * playersCntGroup, * teamsCntGroup, * compOnlyPlayersCntGroup,
  261. * compOnlyTeamsCntGroup, * waterContentGroup, * monsterStrengthGroup;
  262. CButton * showRandMaps;
  263. CMapGenOptions mapGenOptions;
  264. std::unique_ptr<CMapInfo> mapInfo;
  265. CFunctionList<void(const CMapInfo *)> mapInfoChanged;
  266. };
  267. /// Interface for selecting a map.
  268. class ISelectionScreenInfo
  269. {
  270. public:
  271. CMenuScreen::EMultiMode multiPlayer;
  272. CMenuScreen::EState screenType; //new/save/load#Game
  273. const CMapInfo *current;
  274. StartInfo sInfo;
  275. std::map<ui8, std::string> playerNames; // id of player <-> player name; 0 is reserved as ID of AI "players"
  276. ISelectionScreenInfo(const std::map<ui8, std::string> *Names = nullptr);
  277. virtual ~ISelectionScreenInfo();
  278. virtual void update(){};
  279. virtual void propagateOptions() {};
  280. virtual void postRequest(ui8 what, ui8 dir) {};
  281. virtual void postChatMessage(const std::string &txt){};
  282. void setPlayer(PlayerSettings &pset, ui8 player);
  283. void updateStartInfo( std::string filename, StartInfo & sInfo, const CMapHeader * mapHeader );
  284. ui8 getIdOfFirstUnallocatedPlayer(); //returns 0 if none
  285. bool isGuest() const;
  286. bool isHost() const;
  287. };
  288. /// The actual map selection screen which consists of the options and selection tab
  289. class CSelectionScreen : public CIntObject, public ISelectionScreenInfo
  290. {
  291. bool bordered;
  292. public:
  293. CPicture *bg; //general bg image
  294. InfoCard *card;
  295. OptionsTab *opt;
  296. CRandomMapTab * randMapTab;
  297. CButton *start, *back;
  298. SelectionTab *sel;
  299. CIntObject *curTab;
  300. boost::thread *serverHandlingThread;
  301. boost::recursive_mutex *mx;
  302. std::list<CPackForSelectionScreen *> upcomingPacks; //protected by mx
  303. CConnection *serv; //connection to server, used in MP mode
  304. bool ongoingClosing;
  305. ui8 myNameID; //used when networking - otherwise all player are "mine"
  306. 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 = "");
  307. ~CSelectionScreen();
  308. void toggleTab(CIntObject *tab);
  309. void changeSelection(const CMapInfo *to);
  310. void startCampaign();
  311. void startScenario();
  312. void difficultyChange(int to);
  313. void handleConnection();
  314. void processPacks();
  315. void setSInfo(const StartInfo &si);
  316. void update() override;
  317. void propagateOptions() override;
  318. void postRequest(ui8 what, ui8 dir) override;
  319. void postChatMessage(const std::string &txt) override;
  320. void propagateNames();
  321. void showAll(SDL_Surface *to) override;
  322. };
  323. /// Save game screen
  324. class CSavingScreen : public CSelectionScreen
  325. {
  326. public:
  327. const CMapInfo *ourGame;
  328. CSavingScreen(bool hotseat = false);
  329. ~CSavingScreen();
  330. };
  331. /// Scenario information screen shown during the game (thus not really a "pre-game" but fits here anyway)
  332. class CScenarioInfo : public CIntObject, public ISelectionScreenInfo
  333. {
  334. public:
  335. CButton *back;
  336. InfoCard *card;
  337. OptionsTab *opt;
  338. CScenarioInfo(const CMapHeader *mapHeader, const StartInfo *startInfo);
  339. ~CScenarioInfo();
  340. };
  341. /// Multiplayer mode
  342. class CMultiMode : public CIntObject
  343. {
  344. public:
  345. CPicture *bg;
  346. CTextInput *txt;
  347. CButton *btns[7]; //0 - hotseat, 6 - cancel
  348. CGStatusBar *bar;
  349. CMultiMode();
  350. void openHotseat();
  351. void hostTCP();
  352. void joinTCP();
  353. };
  354. /// Hot seat player window
  355. class CHotSeatPlayers : public CIntObject
  356. {
  357. CPicture *bg;
  358. CTextBox *title;
  359. CTextInput* txt[8];
  360. CButton *ok, *cancel;
  361. CGStatusBar *bar;
  362. void onChange(std::string newText);
  363. void enterSelectionScreen();
  364. public:
  365. CHotSeatPlayers(const std::string &firstPlayer);
  366. };
  367. class CPrologEpilogVideo : public CWindowObject
  368. {
  369. CCampaignScenario::SScenarioPrologEpilog spe;
  370. int positionCounter;
  371. int voiceSoundHandle;
  372. std::function<void()> exitCb;
  373. CMultiLineLabel * text;
  374. public:
  375. CPrologEpilogVideo(CCampaignScenario::SScenarioPrologEpilog _spe, std::function<void()> callback);
  376. void clickLeft(tribool down, bool previousState) override;
  377. void show(SDL_Surface * to) override;
  378. };
  379. /// Campaign screen where you can choose one out of three starting bonuses
  380. class CBonusSelection : public CIntObject
  381. {
  382. public:
  383. CBonusSelection(const std::string & campaignFName);
  384. CBonusSelection(std::shared_ptr<CCampaignState> _ourCampaign);
  385. ~CBonusSelection();
  386. void showAll(SDL_Surface * to) override;
  387. void show(SDL_Surface * to) override;
  388. private:
  389. struct SCampPositions
  390. {
  391. std::string campPrefix;
  392. int colorSuffixLength;
  393. struct SRegionDesc
  394. {
  395. std::string infix;
  396. int xpos, ypos;
  397. };
  398. std::vector<SRegionDesc> regions;
  399. };
  400. class CRegion : public CIntObject
  401. {
  402. CBonusSelection * owner;
  403. SDL_Surface* graphics[3]; //[0] - not selected, [1] - selected, [2] - striped
  404. bool accessible; //false if region should be striped
  405. bool selectable; //true if region should be selectable
  406. int myNumber; //number of region
  407. public:
  408. std::string rclickText;
  409. CRegion(CBonusSelection * _owner, bool _accessible, bool _selectable, int _myNumber);
  410. ~CRegion();
  411. void clickLeft(tribool down, bool previousState) override;
  412. void clickRight(tribool down, bool previousState) override;
  413. void show(SDL_Surface * to) override;
  414. };
  415. void init();
  416. void loadPositionsOfGraphics();
  417. void updateStartButtonState(int selected = -1); //-1 -- no bonus is selected
  418. void updateBonusSelection();
  419. void updateCampaignState();
  420. // Event handlers
  421. void goBack();
  422. void startMap();
  423. void restartMap();
  424. void selectMap(int mapNr, bool initialSelect);
  425. void selectBonus(int id);
  426. void increaseDifficulty();
  427. void decreaseDifficulty();
  428. // GUI components
  429. SDL_Surface * background;
  430. CButton * startB, * restartB, * backB;
  431. CTextBox * campaignDescription, * mapDescription;
  432. std::vector<SCampPositions> campDescriptions;
  433. std::vector<CRegion *> regions;
  434. CRegion * highlightedRegion;
  435. CToggleGroup * bonuses;
  436. std::array<CAnimImage *, 5> diffPics; //pictures of difficulties, user-selectable (or not if campaign locks this)
  437. CButton * diffLb, * diffRb; //buttons for changing difficulty
  438. CAnimImage * sizes;//icons of map sizes
  439. std::shared_ptr<CAnimation> sFlags;
  440. // Data
  441. std::shared_ptr<CCampaignState> ourCampaign;
  442. int selectedMap;
  443. boost::optional<int> selectedBonus;
  444. StartInfo startInfo;
  445. CMapHeader * ourHeader;
  446. };
  447. /// Campaign selection screen
  448. class CCampaignScreen : public CIntObject
  449. {
  450. public:
  451. enum CampaignStatus {DEFAULT = 0, ENABLED, DISABLED, COMPLETED}; // the status of the campaign
  452. private:
  453. /// A button which plays a video when you move the mouse cursor over it
  454. class CCampaignButton : public CIntObject
  455. {
  456. private:
  457. CLabel *hoverLabel;
  458. CampaignStatus status;
  459. std::string campFile; // the filename/resourcename of the campaign
  460. std::string video; // the resource name of the video
  461. std::string hoverText;
  462. void clickLeft(tribool down, bool previousState) override;
  463. void hover(bool on) override;
  464. public:
  465. CCampaignButton(const JsonNode &config );
  466. void show(SDL_Surface * to) override;
  467. };
  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) override;
  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. std::shared_ptr<CAnimation> victoryIcons, lossIcons;
  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) override;
  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(CMenuScreen::EMultiMode mode);
  524. void onChange(const std::string & newText);
  525. public:
  526. CSimpleJoinScreen(CMenuScreen::EMultiMode mode);
  527. };
  528. extern ISelectionScreenInfo *SEL;
  529. extern CGPreGame *CGP;