CPreGame.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  1. #ifndef __CPREGAME_H__
  2. #define __CPREGAME_H__
  3. #include "../global.h"
  4. #include <set>
  5. #include <SDL.h>
  6. #include "../StartInfo.h"
  7. #include "GUIBase.h"
  8. #include "GUIClasses.h"
  9. #include "FunctionList.h"
  10. #include "../lib/CMapInfo.h"
  11. /*
  12. * CPreGame.h, part of VCMI engine
  13. *
  14. * Authors: listed in file AUTHORS in main folder
  15. *
  16. * License: GNU General Public License v2.0 or later
  17. * Full text of license available in license.txt file, in main folder
  18. *
  19. */
  20. class CMusicHandler;
  21. class CMapHeader;
  22. class CCampaignHeader;
  23. class CTextInput;
  24. class CCampaign;
  25. class CGStatusBar;
  26. class CTextBox;
  27. class CCampaignState;
  28. class CConnection;
  29. struct CPackForSelectionScreen;
  30. struct PlayerInfo;
  31. namespace boost{ class thread; class recursive_mutex;}
  32. enum ESortBy{_playerAm, _size, _format, _name, _viccon, _loscon, _numOfMaps}; //_numOfMaps is for campaigns
  33. /// Class which handles map sorting by different criteria
  34. class mapSorter
  35. {
  36. public:
  37. ESortBy sortBy;
  38. bool operator()(const CMapInfo *aaa, const CMapInfo *bbb);
  39. mapSorter(ESortBy es):sortBy(es){};
  40. };
  41. /// The main menu screens listed in the EState enum
  42. class CMenuScreen : public CIntObject
  43. {
  44. public:
  45. enum EState { //where are we?
  46. mainMenu, newGame, loadGame, campaignMain, saveGame, scenarioInfo, campaignList
  47. };
  48. enum EMultiMode {
  49. SINGLE_PLAYER = 0, MULTI_HOT_SEAT, MULTI_NETWORK_HOST, MULTI_NETWORK_GUEST
  50. };
  51. CPicture *bgAd;
  52. AdventureMapButton *buttons[5];
  53. CMenuScreen(EState which);
  54. ~CMenuScreen();
  55. void showAll(SDL_Surface * to);
  56. void show(SDL_Surface * to);
  57. void moveTo(CMenuScreen *next);
  58. };
  59. /// Struct which stores name, date and a value which says if the file is located in LOD
  60. struct FileInfo
  61. {
  62. std::string name; // file name with full path and extension
  63. std::time_t date;
  64. bool inLod; //tells if this file is located in Lod
  65. };
  66. /// Implementation of the chat box
  67. class CChatBox : public CIntObject
  68. {
  69. public:
  70. CTextBox *chatHistory;
  71. CTextInput *inputBox;
  72. CChatBox(const Rect &rect);
  73. void keyPressed(const SDL_KeyboardEvent & key);
  74. void addNewMessage(const std::string &text);
  75. };
  76. class InfoCard : public CIntObject
  77. {
  78. CPicture *bg;
  79. public:
  80. CMenuScreen::EState type;
  81. bool network;
  82. bool chatOn; //if chat is shown, then description is hidden
  83. CTextBox *mapDescription;
  84. CChatBox *chat;
  85. CPicture *playerListBg;
  86. CHighlightableButtonsGroup *difficulty;
  87. CDefHandler *sizes, *sFlags;;
  88. void changeSelection(const CMapInfo *to);
  89. void showAll(SDL_Surface * to);
  90. void clickRight(tribool down, bool previousState);
  91. void showTeamsPopup();
  92. void toggleChat();
  93. void setChat(bool activateChat);
  94. InfoCard(bool Network = false);
  95. ~InfoCard();
  96. };
  97. /// The selection tab which is shown at the map selection screen
  98. class SelectionTab : public CIntObject
  99. {
  100. private:
  101. CDefHandler *format; //map size
  102. void parseMaps(std::vector<FileInfo> &files, int start = 0, int threads = 1);
  103. void parseGames(std::vector<FileInfo> &files, bool multi);
  104. void parseCampaigns( std::vector<FileInfo> & files );
  105. void getFiles(std::vector<FileInfo> &out, const std::string &dirname, const std::string &ext);
  106. CMenuScreen::EState tabType;
  107. public:
  108. int positions; //how many entries (games/maps) can be shown
  109. CPicture *bg; //general bg image
  110. CSlider *slider;
  111. std::vector<CMapInfo> allItems;
  112. std::vector<CMapInfo*> curItems;
  113. size_t selectionPos;
  114. boost::function<void(CMapInfo *)> onSelect;
  115. ESortBy sortingBy;
  116. bool ascending;
  117. CTextInput *txt;
  118. void filter(int size, bool selectFirst = false); //0 - all
  119. void select(int position); //position: <0 - positions> position on the screen
  120. void selectAbs(int position); //position: absolute position in curItems vector
  121. int getPosition(int x, int y); //convert mouse coords to entry position; -1 means none
  122. void sliderMove(int slidPos);
  123. void sortBy(int criteria);
  124. void sort();
  125. void printMaps(SDL_Surface *to);
  126. int getLine();
  127. void selectFName(const std::string &fname);
  128. void showAll(SDL_Surface * to);
  129. void clickLeft(tribool down, bool previousState);
  130. void keyPressed(const SDL_KeyboardEvent & key);
  131. void onDoubleClick();
  132. SelectionTab(CMenuScreen::EState Type, const boost::function<void(CMapInfo *)> &OnSelect, CMenuScreen::EMultiMode MultiPlayer = CMenuScreen::SINGLE_PLAYER);
  133. ~SelectionTab();
  134. };
  135. /// The options tab which is shown at the map selection phase.
  136. class OptionsTab : public CIntObject
  137. {
  138. CPicture *bg;
  139. public:
  140. enum SelType {TOWN, HERO, BONUS};
  141. struct SelectedBox : public CIntObject //img with current town/hero/bonus
  142. {
  143. SelType which;
  144. ui8 player; //serial nr
  145. SDL_Surface *getImg() const;
  146. const std::string *getText() const;
  147. SelectedBox(SelType Which, ui8 Player);
  148. void showAll(SDL_Surface * to);
  149. void clickRight(tribool down, bool previousState);
  150. };
  151. struct PlayerOptionsEntry : public CIntObject
  152. {
  153. PlayerInfo &pi;
  154. PlayerSettings &s;
  155. CPicture *bg;
  156. AdventureMapButton *btns[6]; //left and right for town, hero, bonus
  157. AdventureMapButton *flag;
  158. SelectedBox *town;
  159. SelectedBox *hero;
  160. SelectedBox *bonus;
  161. enum {HUMAN_OR_CPU, HUMAN, CPU} whoCanPlay;
  162. PlayerOptionsEntry(OptionsTab *owner, PlayerSettings &S);
  163. void selectButtons(bool onlyHero = true); //hides unavailable buttons
  164. void showAll(SDL_Surface * to);
  165. };
  166. CSlider *turnDuration;
  167. std::set<int> usedHeroes;
  168. struct PlayerToRestore
  169. {
  170. int color, id;
  171. void reset() { color = id = -1; }
  172. PlayerToRestore(){ reset(); }
  173. } playerToRestore;
  174. std::map<int, PlayerOptionsEntry *> entries; //indexed by color
  175. void nextCastle(int player, int dir); //dir == -1 or +1
  176. void nextHero(int player, int dir); //dir == -1 or +1
  177. void nextBonus(int player, int dir); //dir == -1 or +1
  178. void setTurnLength(int npos);
  179. void flagPressed(int player);
  180. void recreate();
  181. OptionsTab();
  182. ~OptionsTab();
  183. void showAll(SDL_Surface * to);
  184. int nextAllowedHero( int min, int max, int incl, int dir );
  185. bool canUseThisHero( int ID );
  186. };
  187. /// Interface for selecting a map.
  188. class ISelectionScreenInfo
  189. {
  190. public:
  191. CMenuScreen::EMultiMode multiPlayer;
  192. CMenuScreen::EState screenType; //new/save/load#Game
  193. const CMapInfo *current;
  194. StartInfo sInfo;
  195. std::map<ui32, std::string> playerNames; // id of player <-> player name; 0 is reserved as ID of AI "players"
  196. ISelectionScreenInfo(const std::map<ui32, std::string> *Names = NULL);
  197. virtual ~ISelectionScreenInfo();
  198. virtual void update(){};
  199. virtual void propagateOptions() {};
  200. virtual void postRequest(ui8 what, ui8 dir) {};
  201. virtual void postChatMessage(const std::string &txt){};
  202. void setPlayer(PlayerSettings &pset, unsigned player);
  203. void updateStartInfo( std::string filename, StartInfo & sInfo, const CMapHeader * mapHeader );
  204. int getIdOfFirstUnallocatedPlayer(); //returns 0 if none
  205. bool isGuest() const;
  206. bool isHost() const;
  207. };
  208. /// The actual map selection screen which consists of the options and selection tab
  209. class CSelectionScreen : public CIntObject, public ISelectionScreenInfo
  210. {
  211. public:
  212. CPicture *bg; //general bg image
  213. InfoCard *card;
  214. OptionsTab *opt;
  215. AdventureMapButton *start, *back;
  216. SelectionTab *sel;
  217. CIntObject *curTab;
  218. boost::thread *serverHandlingThread;
  219. boost::recursive_mutex *mx;
  220. std::list<CPackForSelectionScreen *> upcomingPacks; //protected by mx
  221. CConnection *serv; //connection to server, used in MP mode
  222. bool ongoingClosing;
  223. ui8 myNameID; //used when networking - otherwise all player are "mine"
  224. CSelectionScreen(CMenuScreen::EState Type, CMenuScreen::EMultiMode MultiPlayer = CMenuScreen::SINGLE_PLAYER, const std::map<ui32, std::string> *Names = NULL);
  225. ~CSelectionScreen();
  226. void toggleTab(CIntObject *tab);
  227. void changeSelection(const CMapInfo *to);
  228. void startCampaign();
  229. void startGame();
  230. void difficultyChange(int to);
  231. void handleConnection();
  232. void processPacks();
  233. void setSInfo(const StartInfo &si);
  234. void update() OVERRIDE;
  235. void propagateOptions() OVERRIDE;
  236. void postRequest(ui8 what, ui8 dir) OVERRIDE;
  237. void postChatMessage(const std::string &txt) OVERRIDE;
  238. void propagateNames();
  239. };
  240. /// Save game screen
  241. class CSavingScreen : public CSelectionScreen
  242. {
  243. public:
  244. const CMapInfo *ourGame;
  245. CSavingScreen(bool hotseat = false);
  246. ~CSavingScreen();
  247. };
  248. /// Scenario information screen shown during the game (thus not really a "pre-game" but fits here anyway)
  249. class CScenarioInfo : public CIntObject, public ISelectionScreenInfo
  250. {
  251. public:
  252. AdventureMapButton *back;
  253. InfoCard *card;
  254. OptionsTab *opt;
  255. CScenarioInfo(const CMapHeader *mapHeader, const StartInfo *startInfo);
  256. ~CScenarioInfo();
  257. };
  258. /// Multiplayer mode
  259. class CMultiMode : public CIntObject
  260. {
  261. public:
  262. CPicture *bg;
  263. CTextInput *txt;
  264. AdventureMapButton *btns[7]; //0 - hotseat, 6 - cancel
  265. CGStatusBar *bar;
  266. CMultiMode();
  267. void openHotseat();
  268. void hostTCP();
  269. void joinTCP();
  270. };
  271. /// Hot seat player window
  272. class CHotSeatPlayers : public CIntObject
  273. {
  274. public:
  275. CPicture *bg;
  276. CTextInput *txt[8];
  277. AdventureMapButton *ok, *cancel;
  278. CGStatusBar *bar;
  279. CHotSeatPlayers(const std::string &firstPlayer);
  280. void enterSelectionScreen();
  281. };
  282. /// Campaign screen where you can choose one out of three starting bonuses
  283. class CBonusSelection : public CIntObject
  284. {
  285. SDL_Surface * background;
  286. AdventureMapButton * startB, * backB;
  287. //campaign & map descriptions:
  288. CTextBox * cmpgDesc, * mapDesc;
  289. struct SCampPositions
  290. {
  291. std::string campPrefix;
  292. int colorSuffixLength;
  293. struct SRegionDesc
  294. {
  295. std::string infix;
  296. int xpos, ypos;
  297. };
  298. std::vector<SRegionDesc> regions;
  299. };
  300. std::vector<SCampPositions> campDescriptions;
  301. class CRegion : public CIntObject
  302. {
  303. CBonusSelection * owner;
  304. SDL_Surface * graphics[3]; //[0] - not selected, [1] - selected, [2] - striped
  305. bool accessible; //false if region should be striped
  306. bool selectable; //true if region should be selectable
  307. int myNumber; //number of region
  308. public:
  309. std::string rclickText;
  310. CRegion(CBonusSelection * _owner, bool _accessible, bool _selectable, int _myNumber);
  311. ~CRegion();
  312. void clickLeft(tribool down, bool previousState);
  313. void clickRight(tribool down, bool previousState);
  314. void show(SDL_Surface * to);
  315. };
  316. std::vector<CRegion *> regions;
  317. CRegion * highlightedRegion;
  318. void loadPositionsOfGraphics();
  319. CCampaignState * ourCampaign;
  320. CMapHeader *ourHeader;
  321. CDefHandler *sizes; //icons of map sizes
  322. SDL_Surface * diffPics[5]; //pictures of difficulties, user-selectable (or not if campaign locks this)
  323. AdventureMapButton * diffLb, * diffRb; //buttons for changing difficulty
  324. void changeDiff(bool increase); //if false, then decrease
  325. //bonus selection
  326. void updateBonusSelection();
  327. CHighlightableButtonsGroup * bonuses;
  328. public:
  329. void bonusSelectionChanges(int choosenBonus);
  330. StartInfo sInfo;
  331. CDefHandler *sFlags;
  332. void selectMap(int whichOne);
  333. void selectBonus(int id);
  334. CBonusSelection(CCampaignState * _ourCampaign);
  335. ~CBonusSelection();
  336. void showAll(SDL_Surface * to);
  337. void show(SDL_Surface * to);
  338. void goBack();
  339. void startMap();
  340. };
  341. /// Campaign selection screen
  342. class CCampaignScreen : public CIntObject
  343. {
  344. public:
  345. enum CampaignStatus {DEFAULT = 0, ENABLED, DISABLED, COMPLETED}; // the status of the campaign
  346. private:
  347. SDL_Surface *bg; // background image
  348. SDL_Surface *noCamp; // no campaign placeholder
  349. AdventureMapButton *back; // back button
  350. /// A button which plays a video when you move the mouse cursor over it
  351. class CCampaignButton : public CIntObject
  352. {
  353. private:
  354. std::string image;
  355. SDL_Surface *bg;
  356. SDL_Surface *button;
  357. SDL_Surface *checked;
  358. CLabel *hoverLabel;
  359. CampaignStatus status;
  360. void clickLeft(tribool down, bool previousState);
  361. void hover(bool on);
  362. public:
  363. std::string campFile; // the filename/resourcename of the campaign
  364. std::string video; // the resource name of the video
  365. std::string hoverText; // the text which gets shown when you move the mouse cursor over the button
  366. CCampaignButton(SDL_Surface *bg, const std::string image, const int x, const int y, CampaignStatus status); // c-tor
  367. ~CCampaignButton(); // d-tor
  368. void show(SDL_Surface *to);
  369. };
  370. std::vector<CCampaignButton*> campButtons; // a container which holds all buttons where you can start a campaign
  371. void drawCampaignPlaceholder(); // draws the no campaign placeholder at the lower right position
  372. std::string getMapText(int index);
  373. void createButtons(const int buttonCords[7][2], const std::string campFiles[],
  374. const std::string campImages[], const std::string campVideos[], const std::string campTexts[], std::map<std::string, CampaignStatus>& camps, const CampaignStatus campDefaults[]);
  375. public:
  376. enum CampaignSet {ROE, AB, SOD, WOG};
  377. CCampaignScreen(CampaignSet campaigns, std::map<std::string, CampaignStatus>& camps);
  378. ~CCampaignScreen();
  379. void show(SDL_Surface *to);
  380. };
  381. /// Handles background screen, loads graphics for victory/loss condition and random town or hero selection
  382. class CGPreGame : public CIntObject, public IUpdateable
  383. {
  384. public:
  385. SDL_Surface *mainbg;
  386. CMenuScreen *scrs[4];
  387. SDL_Surface *nHero, *rHero, *nTown, *rTown; // none/random hero/town imgs
  388. CDefHandler *bonuses;
  389. CDefHandler *victory, *loss;
  390. CGPreGame();
  391. ~CGPreGame();
  392. void update();
  393. void openSel(CMenuScreen::EState type, CMenuScreen::EMultiMode multi = CMenuScreen::SINGLE_PLAYER);
  394. void openCampaignScreen(CCampaignScreen::CampaignSet campaigns);
  395. void loadGraphics();
  396. void disposeGraphics();
  397. };
  398. extern CGPreGame *CGP;
  399. #endif // __CPREGAME_H__