CPreGame.h 17 KB

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