CPreGame.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626
  1. #pragma once
  2. #include "../lib/filesystem/Filesystem.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/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}; //_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. CTextBox* credits;
  76. public:
  77. CreditsScreen();
  78. void show(SDL_Surface * to);
  79. void showAll(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. 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(SDL_Surface * to);
  147. void clickLeft(tribool down, bool previousState);
  148. void keyPressed(const SDL_KeyboardEvent & key);
  149. void onDoubleClick();
  150. SelectionTab(CMenuScreen::EState Type, const std::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(SDL_Surface * to);
  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(SDL_Surface * to);
  227. int nextAllowedHero(PlayerColor player, int min, int max, int incl, int dir );
  228. bool canUseThisHero(PlayerColor player, int ID );
  229. };
  230. /// The random map tab shows options for generating a random map.
  231. class CRandomMapTab : public CIntObject
  232. {
  233. public:
  234. CRandomMapTab();
  235. void showAll(SDL_Surface * to);
  236. void updateMapInfo();
  237. CFunctionList<void (const CMapInfo *)> & getMapInfoChanged();
  238. const CMapInfo * getMapInfo() const;
  239. const CMapGenOptions & getMapGenOptions() const;
  240. private:
  241. void addButtonsToGroup(CHighlightableButtonsGroup * group, const std::vector<std::string> & defs, int startIndex, int endIndex, int btnWidth, int helpStartIndex) const;
  242. void addButtonsWithRandToGroup(CHighlightableButtonsGroup * group, const std::vector<std::string> & defs, int startIndex, int endIndex, int btnWidth, int helpStartIndex, int helpRandIndex) const;
  243. void deactivateButtonsFrom(CHighlightableButtonsGroup * group, int startId);
  244. void validatePlayersCnt(int playersCnt);
  245. void validateCompOnlyPlayersCnt(int compOnlyPlayersCnt);
  246. CPicture * bg;
  247. CHighlightableButton * twoLevelsBtn;
  248. CHighlightableButtonsGroup * mapSizeBtnGroup, * playersCntGroup, * teamsCntGroup, * compOnlyPlayersCntGroup,
  249. * compOnlyTeamsCntGroup, * waterContentGroup, * monsterStrengthGroup;
  250. CAdventureMapButton * showRandMaps;
  251. CMapGenOptions mapGenOptions;
  252. unique_ptr<CMapInfo> mapInfo;
  253. CFunctionList<void(const CMapInfo *)> mapInfoChanged;
  254. };
  255. /// Interface for selecting a map.
  256. class ISelectionScreenInfo
  257. {
  258. public:
  259. CMenuScreen::EMultiMode multiPlayer;
  260. CMenuScreen::EState screenType; //new/save/load#Game
  261. const CMapInfo *current;
  262. StartInfo sInfo;
  263. std::map<ui8, std::string> playerNames; // id of player <-> player name; 0 is reserved as ID of AI "players"
  264. ISelectionScreenInfo(const std::map<ui8, std::string> *Names = nullptr);
  265. virtual ~ISelectionScreenInfo();
  266. virtual void update(){};
  267. virtual void propagateOptions() {};
  268. virtual void postRequest(ui8 what, ui8 dir) {};
  269. virtual void postChatMessage(const std::string &txt){};
  270. void setPlayer(PlayerSettings &pset, ui8 player);
  271. void updateStartInfo( std::string filename, StartInfo & sInfo, const CMapHeader * mapHeader );
  272. ui8 getIdOfFirstUnallocatedPlayer(); //returns 0 if none
  273. bool isGuest() const;
  274. bool isHost() const;
  275. };
  276. /// The actual map selection screen which consists of the options and selection tab
  277. class CSelectionScreen : public CIntObject, public ISelectionScreenInfo
  278. {
  279. bool bordered;
  280. public:
  281. CPicture *bg; //general bg image
  282. InfoCard *card;
  283. OptionsTab *opt;
  284. CRandomMapTab * randMapTab;
  285. CAdventureMapButton *start, *back;
  286. SelectionTab *sel;
  287. CIntObject *curTab;
  288. boost::thread *serverHandlingThread;
  289. boost::recursive_mutex *mx;
  290. std::list<CPackForSelectionScreen *> upcomingPacks; //protected by mx
  291. CConnection *serv; //connection to server, used in MP mode
  292. bool ongoingClosing;
  293. ui8 myNameID; //used when networking - otherwise all player are "mine"
  294. 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 = "");
  295. ~CSelectionScreen();
  296. void toggleTab(CIntObject *tab);
  297. void changeSelection(const CMapInfo *to);
  298. void startCampaign();
  299. void startScenario();
  300. void difficultyChange(int to);
  301. void handleConnection();
  302. void processPacks();
  303. void setSInfo(const StartInfo &si);
  304. void update() override;
  305. void propagateOptions() override;
  306. void postRequest(ui8 what, ui8 dir) override;
  307. void postChatMessage(const std::string &txt) override;
  308. void propagateNames();
  309. void showAll(SDL_Surface *to);
  310. };
  311. /// Save game screen
  312. class CSavingScreen : public CSelectionScreen
  313. {
  314. public:
  315. const CMapInfo *ourGame;
  316. CSavingScreen(bool hotseat = false);
  317. ~CSavingScreen();
  318. };
  319. /// Scenario information screen shown during the game (thus not really a "pre-game" but fits here anyway)
  320. class CScenarioInfo : public CIntObject, public ISelectionScreenInfo
  321. {
  322. public:
  323. CAdventureMapButton *back;
  324. InfoCard *card;
  325. OptionsTab *opt;
  326. CScenarioInfo(const CMapHeader *mapHeader, const StartInfo *startInfo);
  327. ~CScenarioInfo();
  328. };
  329. /// Multiplayer mode
  330. class CMultiMode : public CIntObject
  331. {
  332. public:
  333. CPicture *bg;
  334. CTextInput *txt;
  335. CAdventureMapButton *btns[7]; //0 - hotseat, 6 - cancel
  336. CGStatusBar *bar;
  337. CMultiMode();
  338. void openHotseat();
  339. void hostTCP();
  340. void joinTCP();
  341. };
  342. /// Hot seat player window
  343. class CHotSeatPlayers : public CIntObject
  344. {
  345. CPicture *bg;
  346. CTextBox *title;
  347. CTextInput* txt[8];
  348. CAdventureMapButton *ok, *cancel;
  349. CGStatusBar *bar;
  350. void onChange(std::string newText);
  351. void enterSelectionScreen();
  352. public:
  353. CHotSeatPlayers(const std::string &firstPlayer);
  354. };
  355. class CPrologEpilogVideo : public CIntObject
  356. {
  357. CCampaignScenario::SScenarioPrologEpilog spe;
  358. SDL_Surface * txt;
  359. int curTxtH, decrementDelayCounter;
  360. std::function<void()> exitCb;
  361. public:
  362. CPrologEpilogVideo(CCampaignScenario::SScenarioPrologEpilog _spe, std::function<void()> callback);
  363. void clickLeft(tribool down, bool previousState);
  364. void show(SDL_Surface * to);
  365. };
  366. /// Campaign screen where you can choose one out of three starting bonuses
  367. class CBonusSelection : public CIntObject
  368. {
  369. SDL_Surface * background;
  370. CAdventureMapButton * startB, * backB;
  371. //campaign & map descriptions:
  372. CTextBox * cmpgDesc, * mapDesc;
  373. struct SCampPositions
  374. {
  375. std::string campPrefix;
  376. int colorSuffixLength;
  377. struct SRegionDesc
  378. {
  379. std::string infix;
  380. int xpos, ypos;
  381. };
  382. std::vector<SRegionDesc> regions;
  383. };
  384. std::vector<SCampPositions> campDescriptions;
  385. class CRegion : public CIntObject
  386. {
  387. CBonusSelection * owner;
  388. SDL_Surface* graphics[3]; //[0] - not selected, [1] - selected, [2] - striped
  389. bool accessible; //false if region should be striped
  390. bool selectable; //true if region should be selectable
  391. int myNumber; //number of region
  392. public:
  393. std::string rclickText;
  394. CRegion(CBonusSelection * _owner, bool _accessible, bool _selectable, int _myNumber);
  395. ~CRegion();
  396. void clickLeft(tribool down, bool previousState);
  397. void clickRight(tribool down, bool previousState);
  398. void show(SDL_Surface * to);
  399. };
  400. std::vector<CRegion *> regions;
  401. CRegion * highlightedRegion;
  402. void loadPositionsOfGraphics();
  403. shared_ptr<CCampaignState> ourCampaign;
  404. CMapHeader *ourHeader;
  405. CDefHandler *sizes; //icons of map sizes
  406. SDL_Surface* diffPics[5]; //pictures of difficulties, user-selectable (or not if campaign locks this)
  407. CAdventureMapButton * diffLb, * diffRb; //buttons for changing difficulty
  408. void changeDiff(bool increase); //if false, then decrease
  409. void updateStartButtonState(int selected = -1); //-1 -- no bonus is selected
  410. //bonus selection
  411. void updateBonusSelection();
  412. CHighlightableButtonsGroup * bonuses;
  413. public:
  414. StartInfo sInfo;
  415. CDefHandler *sFlags;
  416. void selectMap(int whichOne, bool initialSelect);
  417. void selectBonus(int id);
  418. void init();
  419. CBonusSelection(std::string campaignFName);
  420. CBonusSelection(shared_ptr<CCampaignState> _ourCampaign);
  421. ~CBonusSelection();
  422. void showAll(SDL_Surface * to);
  423. void show(SDL_Surface * to);
  424. void goBack();
  425. void startMap();
  426. };
  427. /// Campaign selection screen
  428. class CCampaignScreen : public CIntObject
  429. {
  430. public:
  431. enum CampaignStatus {DEFAULT = 0, ENABLED, DISABLED, COMPLETED}; // the status of the campaign
  432. private:
  433. /// A button which plays a video when you move the mouse cursor over it
  434. class CCampaignButton : public CIntObject
  435. {
  436. private:
  437. CPicture *image;
  438. CPicture *checkMark;
  439. CLabel *hoverLabel;
  440. CampaignStatus status;
  441. std::string campFile; // the filename/resourcename of the campaign
  442. std::string video; // the resource name of the video
  443. std::string hoverText;
  444. void clickLeft(tribool down, bool previousState);
  445. void hover(bool on);
  446. public:
  447. CCampaignButton(const JsonNode &config );
  448. void show(SDL_Surface * to);
  449. };
  450. CAdventureMapButton *back;
  451. std::vector<CCampaignButton*> campButtons;
  452. std::vector<CPicture*> images;
  453. CAdventureMapButton* createExitButton(const JsonNode& button);
  454. public:
  455. enum CampaignSet {ROE, AB, SOD, WOG};
  456. CCampaignScreen(const JsonNode &config);
  457. void showAll(SDL_Surface *to);
  458. };
  459. /// Handles background screen, loads graphics for victory/loss condition and random town or hero selection
  460. class CGPreGame : public CIntObject, public IUpdateable
  461. {
  462. void loadGraphics();
  463. void disposeGraphics();
  464. CGPreGame(); //Use createIfNotPresent
  465. public:
  466. const JsonNode * const pregameConfig;
  467. CMenuScreen* menu;
  468. CDefHandler *victory, *loss;
  469. ~CGPreGame();
  470. void update();
  471. void openSel(CMenuScreen::EState type, CMenuScreen::EMultiMode multi = CMenuScreen::SINGLE_PLAYER);
  472. void openCampaignScreen(std::string name);
  473. static CGPreGame * create();
  474. void removeFromGui();
  475. static void showLoadingScreen(std::function<void()> loader);
  476. };
  477. class CLoadingScreen : public CWindowObject
  478. {
  479. boost::thread loadingThread;
  480. std::string getBackground();
  481. public:
  482. CLoadingScreen(std::function<void()> loader);
  483. ~CLoadingScreen();
  484. void showAll(SDL_Surface *to);
  485. };
  486. /// Simple window to enter the server's address.
  487. class CSimpleJoinScreen : public CIntObject
  488. {
  489. CPicture * bg;
  490. CTextBox * title;
  491. CAdventureMapButton * ok, * cancel;
  492. CGStatusBar * bar;
  493. CTextInput * address;
  494. CTextInput * port;
  495. void enterSelectionScreen();
  496. void onChange(const std::string & newText);
  497. public:
  498. CSimpleJoinScreen();
  499. };
  500. extern ISelectionScreenInfo *SEL;
  501. extern CGPreGame *CGP;