CPreGame.h 17 KB

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