CPreGame.h 12 KB

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