CPreGame.h 13 KB

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