CPreGame.h 13 KB

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