CPreGame.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663
  1. #pragma once
  2. #include "../lib/Filesystem/CResourceLoader.h"
  3. #include <SDL.h>
  4. #include "../lib/StartInfo.h"
  5. #include "GUIClasses.h"
  6. #include "FunctionList.h"
  7. #include "../lib/Map/CMapInfo.h"
  8. #include "../lib/RMG/CMapGenOptions.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 RandomMapTab;
  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::vector<ResourceID> &files);
  120. void parseGames(const std::vector<ResourceID> &files, bool multi);
  121. void parseCampaigns(const std::vector<ResourceID> & files );
  122. std::vector<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. boost::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 boost::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 SelectedBox : public CIntObject //img with current town/hero/bonus
  160. {
  161. SelType which;
  162. ui8 player; //serial nr
  163. size_t getBonusImageIndex() const;
  164. SDL_Surface *getImg() const;
  165. const std::string *getText() const;
  166. SelectedBox(SelType Which, ui8 Player);
  167. void showAll(SDL_Surface * to);
  168. void clickRight(tribool down, bool previousState);
  169. };
  170. struct PlayerOptionsEntry : public CIntObject
  171. {
  172. PlayerInfo &pi;
  173. PlayerSettings &s;
  174. CPicture *bg;
  175. CAdventureMapButton *btns[6]; //left and right for town, hero, bonus
  176. CAdventureMapButton *flag;
  177. SelectedBox *town;
  178. SelectedBox *hero;
  179. SelectedBox *bonus;
  180. enum {HUMAN_OR_CPU, HUMAN, CPU} whoCanPlay;
  181. PlayerOptionsEntry(OptionsTab *owner, PlayerSettings &S);
  182. void selectButtons(); //hides unavailable buttons
  183. void showAll(SDL_Surface * to);
  184. };
  185. CSlider *turnDuration;
  186. std::set<int> usedHeroes;
  187. struct PlayerToRestore
  188. {
  189. int color, id;
  190. void reset() { color = id = -1; }
  191. PlayerToRestore(){ reset(); }
  192. } playerToRestore;
  193. std::map<int, PlayerOptionsEntry *> entries; //indexed by color
  194. void nextCastle(int player, int dir); //dir == -1 or +1
  195. void nextHero(int player, int dir); //dir == -1 or +1
  196. void nextBonus(int player, int dir); //dir == -1 or +1
  197. void setTurnLength(int npos);
  198. void flagPressed(int player);
  199. void recreate();
  200. OptionsTab();
  201. ~OptionsTab();
  202. void showAll(SDL_Surface * to);
  203. int nextAllowedHero( int min, int max, int incl, int dir );
  204. bool canUseThisHero( int ID );
  205. };
  206. /**
  207. * The random map tab shows options for generating a random map.
  208. */
  209. class RandomMapTab : public CIntObject
  210. {
  211. public:
  212. /**
  213. * C-tor.
  214. */
  215. RandomMapTab();
  216. /**
  217. * Shows the interface and the visual representation of this tab.
  218. *
  219. * @param to where the graphics should be inserted
  220. */
  221. void showAll(SDL_Surface * to);
  222. /**
  223. * Updates the map info object and fires the associated callback method.
  224. */
  225. void updateMapInfo();
  226. /**
  227. * Gets the map info changed callback method list object. This event
  228. * occurs when the updateMapInfo method has been called or the options
  229. * of this tab have been changed.
  230. *
  231. * @return the map info changed callback method list object
  232. */
  233. CFunctionList<void (const CMapInfo *)> & getMapInfoChanged();
  234. /**
  235. * Gets the created map info object.
  236. *
  237. * @return the created map info object
  238. */
  239. const CMapInfo & getMapInfo() const;
  240. /**
  241. * Gets the map generation options.
  242. *
  243. * @return the map generation options
  244. */
  245. const CMapGenOptions & getMapGenOptions() const;
  246. private:
  247. /**
  248. * Adds buttons specified by the defs list to the given buttons group.
  249. *
  250. * @param group the button group where the buttons should be added to
  251. * @param defs the names of the button defs
  252. * @param startIndex start index of the defs vector
  253. * @param endIndex end index of the defs vector
  254. * @param btnWidth width of one button(fixed width)
  255. * @param helpStartIndex the index of the first help msg entry
  256. */
  257. void addButtonsToGroup(CHighlightableButtonsGroup * group, const std::vector<std::string> & defs, int startIndex, int endIndex, int btnWidth, int helpStartIndex) const;
  258. /**
  259. * Adds buttons specified by the defs list and the random button to the given buttons group. Auto-selects the
  260. * random button.
  261. *
  262. * @param group the button group where the buttons should be added to
  263. * @param defs the names of the button defs
  264. * @param startIndex start index of the defs vector
  265. * @param endIndex end index of the defs vector
  266. * @param btnWidth width of one button(fixed width)
  267. * @param helpStartIndex the index of the first help msg entry
  268. * @param helpRandIndex the index of the random help msg entry
  269. */
  270. void addButtonsWithRandToGroup(CHighlightableButtonsGroup * group, const std::vector<std::string> & defs, int startIndex, int endIndex, int btnWidth, int helpStartIndex, int helpRandIndex) const;
  271. /**
  272. * Deactives buttons of a highlightable button group beginning from startId. Buttons with a id
  273. * lower than startId will be activated/reseted.
  274. *
  275. * @param group the associated highlightable button group
  276. * @param startId the id of the first button to deactivate
  277. */
  278. void deactivateButtonsFrom(CHighlightableButtonsGroup * group, int startId);
  279. /**
  280. * Validates players count and updates teams count, comp only players/teams count if necessary.
  281. *
  282. * @param playersCnt the players count to validate
  283. */
  284. void validatePlayersCnt(int playersCnt);
  285. /**
  286. * Validates computer only players count and updates comp only teams count if necessary.
  287. *
  288. * @param compOnlyPlayersCnt the computer only players count to validate
  289. */
  290. void validateCompOnlyPlayersCnt(int compOnlyPlayersCnt);
  291. /** the background image of the rmg options tab */
  292. CPicture * bg;
  293. /** the map size buttons group */
  294. CHighlightableButtonsGroup * mapSizeBtnGroup;
  295. /** the two levels highlightable button */
  296. CHighlightableButton * twoLevelsBtn;
  297. /** the players count group */
  298. CHighlightableButtonsGroup * playersCntGroup;
  299. /** the teams count group */
  300. CHighlightableButtonsGroup * teamsCntGroup;
  301. /** the computer only players count group */
  302. CHighlightableButtonsGroup * compOnlyPlayersCntGroup;
  303. /** the computer only teams count group */
  304. CHighlightableButtonsGroup * compOnlyTeamsCntGroup;
  305. /** the water content group */
  306. CHighlightableButtonsGroup * waterContentGroup;
  307. /** the monster strength group */
  308. CHighlightableButtonsGroup * monsterStrengthGroup;
  309. /** show previously created random maps button */
  310. CAdventureMapButton * showRandMaps;
  311. /** the map options selected by the user */
  312. CMapGenOptions mapGenOptions;
  313. /** map info object describing a randomly created map */
  314. CMapInfo mapInfo;
  315. /** callback method which gets called when the random options have been changed */
  316. CFunctionList<void(const CMapInfo *)> mapInfoChanged;
  317. };
  318. /// Interface for selecting a map.
  319. class ISelectionScreenInfo
  320. {
  321. public:
  322. CMenuScreen::EMultiMode multiPlayer;
  323. CMenuScreen::EState screenType; //new/save/load#Game
  324. const CMapInfo *current;
  325. StartInfo sInfo;
  326. std::map<TPlayerColor, std::string> playerNames; // id of player <-> player name; 0 is reserved as ID of AI "players"
  327. ISelectionScreenInfo(const std::map<TPlayerColor, std::string> *Names = NULL);
  328. virtual ~ISelectionScreenInfo();
  329. virtual void update(){};
  330. virtual void propagateOptions() {};
  331. virtual void postRequest(ui8 what, ui8 dir) {};
  332. virtual void postChatMessage(const std::string &txt){};
  333. void setPlayer(PlayerSettings &pset, TPlayerColor player);
  334. void updateStartInfo( std::string filename, StartInfo & sInfo, const CMapHeader * mapHeader );
  335. int getIdOfFirstUnallocatedPlayer(); //returns 0 if none
  336. bool isGuest() const;
  337. bool isHost() const;
  338. };
  339. /// The actual map selection screen which consists of the options and selection tab
  340. class CSelectionScreen : public CIntObject, public ISelectionScreenInfo
  341. {
  342. bool bordered;
  343. public:
  344. CPicture *bg; //general bg image
  345. InfoCard *card;
  346. OptionsTab *opt;
  347. RandomMapTab * randMapTab;
  348. CAdventureMapButton *start, *back;
  349. SelectionTab *sel;
  350. CIntObject *curTab;
  351. boost::thread *serverHandlingThread;
  352. boost::recursive_mutex *mx;
  353. std::list<CPackForSelectionScreen *> upcomingPacks; //protected by mx
  354. CConnection *serv; //connection to server, used in MP mode
  355. bool ongoingClosing;
  356. ui8 myNameID; //used when networking - otherwise all player are "mine"
  357. CSelectionScreen(CMenuScreen::EState Type, CMenuScreen::EMultiMode MultiPlayer = CMenuScreen::SINGLE_PLAYER, const std::map<TPlayerColor, std::string> *Names = NULL);
  358. ~CSelectionScreen();
  359. void toggleTab(CIntObject *tab);
  360. void changeSelection(const CMapInfo *to);
  361. void startCampaign();
  362. void startGame();
  363. void difficultyChange(int to);
  364. void handleConnection();
  365. void processPacks();
  366. void setSInfo(const StartInfo &si);
  367. void update() OVERRIDE;
  368. void propagateOptions() OVERRIDE;
  369. void postRequest(ui8 what, ui8 dir) OVERRIDE;
  370. void postChatMessage(const std::string &txt) OVERRIDE;
  371. void propagateNames();
  372. void showAll(SDL_Surface *to);
  373. };
  374. /// Save game screen
  375. class CSavingScreen : public CSelectionScreen
  376. {
  377. public:
  378. const CMapInfo *ourGame;
  379. CSavingScreen(bool hotseat = false);
  380. ~CSavingScreen();
  381. };
  382. /// Scenario information screen shown during the game (thus not really a "pre-game" but fits here anyway)
  383. class CScenarioInfo : public CIntObject, public ISelectionScreenInfo
  384. {
  385. public:
  386. CAdventureMapButton *back;
  387. InfoCard *card;
  388. OptionsTab *opt;
  389. CScenarioInfo(const CMapHeader *mapHeader, const StartInfo *startInfo);
  390. ~CScenarioInfo();
  391. };
  392. /// Multiplayer mode
  393. class CMultiMode : public CIntObject
  394. {
  395. public:
  396. CPicture *bg;
  397. CTextInput *txt;
  398. CAdventureMapButton *btns[7]; //0 - hotseat, 6 - cancel
  399. CGStatusBar *bar;
  400. CMultiMode();
  401. void openHotseat();
  402. void hostTCP();
  403. void joinTCP();
  404. };
  405. /// Hot seat player window
  406. class CHotSeatPlayers : public CIntObject
  407. {
  408. CPicture *bg;
  409. CTextBox *title;
  410. CTextInput* txt[8];
  411. CAdventureMapButton *ok, *cancel;
  412. CGStatusBar *bar;
  413. void onChange(std::string newText);
  414. void enterSelectionScreen();
  415. public:
  416. CHotSeatPlayers(const std::string &firstPlayer);
  417. };
  418. /// Campaign screen where you can choose one out of three starting bonuses
  419. class CBonusSelection : public CIntObject
  420. {
  421. SDL_Surface * background;
  422. CAdventureMapButton * startB, * backB;
  423. //campaign & map descriptions:
  424. CTextBox * cmpgDesc, * mapDesc;
  425. struct SCampPositions
  426. {
  427. std::string campPrefix;
  428. int colorSuffixLength;
  429. struct SRegionDesc
  430. {
  431. std::string infix;
  432. int xpos, ypos;
  433. };
  434. std::vector<SRegionDesc> regions;
  435. };
  436. std::vector<SCampPositions> campDescriptions;
  437. class CRegion : public CIntObject
  438. {
  439. CBonusSelection * owner;
  440. SDL_Surface* graphics[3]; //[0] - not selected, [1] - selected, [2] - striped
  441. bool accessible; //false if region should be striped
  442. bool selectable; //true if region should be selectable
  443. int myNumber; //number of region
  444. public:
  445. std::string rclickText;
  446. CRegion(CBonusSelection * _owner, bool _accessible, bool _selectable, int _myNumber);
  447. ~CRegion();
  448. void clickLeft(tribool down, bool previousState);
  449. void clickRight(tribool down, bool previousState);
  450. void show(SDL_Surface * to);
  451. };
  452. std::vector<CRegion *> regions;
  453. CRegion * highlightedRegion;
  454. void loadPositionsOfGraphics();
  455. shared_ptr<CCampaignState> ourCampaign;
  456. CMapHeader *ourHeader;
  457. CDefHandler *sizes; //icons of map sizes
  458. SDL_Surface* diffPics[5]; //pictures of difficulties, user-selectable (or not if campaign locks this)
  459. CAdventureMapButton * diffLb, * diffRb; //buttons for changing difficulty
  460. void changeDiff(bool increase); //if false, then decrease
  461. void updateStartButtonState(int selected = -1); //-1 -- no bonus is selected
  462. //bonus selection
  463. void updateBonusSelection();
  464. CHighlightableButtonsGroup * bonuses;
  465. public:
  466. StartInfo sInfo;
  467. CDefHandler *sFlags;
  468. void selectMap(int whichOne);
  469. void selectBonus(int id);
  470. void init();
  471. CBonusSelection(std::string campaignFName);
  472. CBonusSelection(shared_ptr<CCampaignState> _ourCampaign);
  473. ~CBonusSelection();
  474. void showAll(SDL_Surface * to);
  475. void show(SDL_Surface * to);
  476. void goBack();
  477. void startMap();
  478. };
  479. /// Campaign selection screen
  480. class CCampaignScreen : public CIntObject
  481. {
  482. public:
  483. enum CampaignStatus {DEFAULT = 0, ENABLED, DISABLED, COMPLETED}; // the status of the campaign
  484. private:
  485. /// A button which plays a video when you move the mouse cursor over it
  486. class CCampaignButton : public CIntObject
  487. {
  488. private:
  489. CPicture *image;
  490. CPicture *checkMark;
  491. CLabel *hoverLabel;
  492. CampaignStatus status;
  493. std::string campFile; // the filename/resourcename of the campaign
  494. std::string video; // the resource name of the video
  495. std::string hoverText;
  496. void clickLeft(tribool down, bool previousState);
  497. void hover(bool on);
  498. public:
  499. CCampaignButton(const JsonNode &config );
  500. void show(SDL_Surface * to);
  501. };
  502. CAdventureMapButton *back;
  503. std::vector<CCampaignButton*> campButtons;
  504. std::vector<CPicture*> images;
  505. CAdventureMapButton* createExitButton(const JsonNode& button);
  506. public:
  507. enum CampaignSet {ROE, AB, SOD, WOG};
  508. CCampaignScreen(const JsonNode &config);
  509. void showAll(SDL_Surface *to);
  510. };
  511. /// Handles background screen, loads graphics for victory/loss condition and random town or hero selection
  512. class CGPreGame : public CIntObject, public IUpdateable
  513. {
  514. void loadGraphics();
  515. void disposeGraphics();
  516. CGPreGame(); //Use createIfNotPresent
  517. public:
  518. const JsonNode * const pregameConfig;
  519. CMenuScreen* menu;
  520. SDL_Surface *nHero, *rHero, *nTown, *rTown; // none/random hero/town imgs
  521. CDefHandler *bonuses;
  522. CDefHandler *victory, *loss;
  523. ~CGPreGame();
  524. void update();
  525. void openSel(CMenuScreen::EState type, CMenuScreen::EMultiMode multi = CMenuScreen::SINGLE_PLAYER);
  526. void openCampaignScreen(std::string name);
  527. static CGPreGame * create();
  528. void removeFromGui();
  529. };
  530. extern ISelectionScreenInfo *SEL;
  531. extern CGPreGame *CGP;