CPreGame.h 13 KB

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