CPreGame.h 18 KB

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