CPreGame.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  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 mapSorter
  33. {
  34. public:
  35. ESortBy sortBy;
  36. bool operator()(const CMapInfo *aaa, const CMapInfo *bbb);
  37. mapSorter(ESortBy es):sortBy(es){};
  38. };
  39. class CMenuScreen : public CIntObject
  40. {
  41. public:
  42. enum EState { //where are we?
  43. mainMenu, newGame, loadGame, campaignMain, saveGame, scenarioInfo, campaignList
  44. };
  45. enum EMultiMode {
  46. SINGLE_PLAYER = 0, MULTI_HOT_SEAT, MULTI_NETWORK_HOST, MULTI_NETWORK_GUEST
  47. };
  48. CPicture *bgAd;
  49. AdventureMapButton *buttons[5];
  50. CMenuScreen(EState which);
  51. ~CMenuScreen();
  52. void showAll(SDL_Surface * to);
  53. void show(SDL_Surface * to);
  54. void moveTo(CMenuScreen *next);
  55. };
  56. struct FileInfo
  57. {
  58. std::string name; // file name with full path and extension
  59. std::time_t date;
  60. bool inLod; //tells if this file is located in Lod
  61. };
  62. class CChatBox : public CIntObject
  63. {
  64. public:
  65. CTextBox *chatHistory;
  66. CTextInput *inputBox;
  67. CChatBox(const Rect &rect);
  68. void keyPressed(const SDL_KeyboardEvent & key);
  69. void addNewMessage(const std::string &text);
  70. };
  71. class InfoCard : public CIntObject
  72. {
  73. CPicture *bg;
  74. public:
  75. CMenuScreen::EState type;
  76. bool network;
  77. bool chatOn; //if chat is shown, then description is hidden
  78. CTextBox *mapDescription;
  79. CChatBox *chat;
  80. CPicture *playerListBg;
  81. CHighlightableButtonsGroup *difficulty;
  82. CDefHandler *sizes, *sFlags;;
  83. void changeSelection(const CMapInfo *to);
  84. void showAll(SDL_Surface * to);
  85. void clickRight(tribool down, bool previousState);
  86. void showTeamsPopup();
  87. void toggleChat();
  88. void setChat(bool activateChat);
  89. InfoCard(bool Network = false);
  90. ~InfoCard();
  91. };
  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. class OptionsTab : public CIntObject
  130. {
  131. CPicture *bg;
  132. public:
  133. enum SelType {TOWN, HERO, BONUS};
  134. struct SelectedBox : public CIntObject //img with current town/hero/bonus
  135. {
  136. SelType which;
  137. ui8 player; //serial nr
  138. SDL_Surface *getImg() const;
  139. const std::string *getText() const;
  140. SelectedBox(SelType Which, ui8 Player);
  141. void showAll(SDL_Surface * to);
  142. void clickRight(tribool down, bool previousState);
  143. };
  144. struct PlayerOptionsEntry : public CIntObject
  145. {
  146. PlayerInfo &pi;
  147. PlayerSettings &s;
  148. CPicture *bg;
  149. AdventureMapButton *btns[6]; //left and right for town, hero, bonus
  150. AdventureMapButton *flag;
  151. SelectedBox *town;
  152. SelectedBox *hero;
  153. SelectedBox *bonus;
  154. enum {HUMAN_OR_CPU, HUMAN, CPU} whoCanPlay;
  155. PlayerOptionsEntry(OptionsTab *owner, PlayerSettings &S);
  156. void selectButtons(bool onlyHero = true); //hides unavailable buttons
  157. void showAll(SDL_Surface * to);
  158. };
  159. CSlider *turnDuration;
  160. std::set<int> usedHeroes;
  161. struct PlayerToRestore
  162. {
  163. int color, id;
  164. void reset() { color = id = -1; }
  165. PlayerToRestore(){ reset(); }
  166. } playerToRestore;
  167. std::map<int, PlayerOptionsEntry *> entries; //indexed by color
  168. void nextCastle(int player, int dir); //dir == -1 or +1
  169. void nextHero(int player, int dir); //dir == -1 or +1
  170. void nextBonus(int player, int dir); //dir == -1 or +1
  171. void setTurnLength(int npos);
  172. void flagPressed(int player);
  173. void recreate();
  174. OptionsTab();
  175. ~OptionsTab();
  176. void showAll(SDL_Surface * to);
  177. int nextAllowedHero( int min, int max, int incl, int dir );
  178. bool canUseThisHero( int ID );
  179. };
  180. class ISelectionScreenInfo
  181. {
  182. public:
  183. CMenuScreen::EMultiMode multiPlayer;
  184. CMenuScreen::EState screenType; //new/save/load#Game
  185. const CMapInfo *current;
  186. StartInfo sInfo;
  187. std::map<ui32, std::string> playerNames; // id of player <-> player name; 0 is reserved as ID of AI "players"
  188. ISelectionScreenInfo(const std::map<ui32, std::string> *Names = NULL);
  189. virtual ~ISelectionScreenInfo();
  190. virtual void update(){};
  191. virtual void propagateOptions() {};
  192. virtual void postRequest(ui8 what, ui8 dir) {};
  193. virtual void postChatMessage(const std::string &txt){};
  194. void setPlayer(PlayerSettings &pset, unsigned player);
  195. void updateStartInfo( std::string filename, StartInfo & sInfo, const CMapHeader * mapHeader );
  196. int getIdOfFirstUnallocatedPlayer(); //returns 0 if none
  197. bool isGuest() const;
  198. bool isHost() const;
  199. };
  200. class CSelectionScreen : public CIntObject, public ISelectionScreenInfo
  201. {
  202. public:
  203. CPicture *bg; //general bg image
  204. InfoCard *card;
  205. OptionsTab *opt;
  206. AdventureMapButton *start, *back;
  207. SelectionTab *sel;
  208. CIntObject *curTab;
  209. boost::thread *serverHandlingThread;
  210. boost::recursive_mutex *mx;
  211. std::list<CPackForSelectionScreen *> upcomingPacks; //protected by mx
  212. CConnection *serv; //connection to server, used in MP mode
  213. bool ongoingClosing;
  214. ui8 myNameID; //used when networking - otherwise all player are "mine"
  215. CSelectionScreen(CMenuScreen::EState Type, CMenuScreen::EMultiMode MultiPlayer = CMenuScreen::SINGLE_PLAYER, const std::map<ui32, std::string> *Names = NULL);
  216. ~CSelectionScreen();
  217. void toggleTab(CIntObject *tab);
  218. void changeSelection(const CMapInfo *to);
  219. void startCampaign();
  220. void startGame();
  221. void difficultyChange(int to);
  222. void handleConnection();
  223. void processPacks();
  224. void setSInfo(const StartInfo &si);
  225. void update() OVERRIDE;
  226. void propagateOptions() OVERRIDE;
  227. void postRequest(ui8 what, ui8 dir) OVERRIDE;
  228. void postChatMessage(const std::string &txt) OVERRIDE;
  229. void propagateNames();
  230. };
  231. class CSavingScreen : public CSelectionScreen
  232. {
  233. public:
  234. const CMapInfo *ourGame;
  235. CSavingScreen(bool hotseat = false);
  236. ~CSavingScreen();
  237. };
  238. //scenario information screen shown during the game (thus not really a "pre-game" but fits here anyway)
  239. class CScenarioInfo : public CIntObject, public ISelectionScreenInfo
  240. {
  241. public:
  242. AdventureMapButton *back;
  243. InfoCard *card;
  244. OptionsTab *opt;
  245. CScenarioInfo(const CMapHeader *mapHeader, const StartInfo *startInfo);
  246. ~CScenarioInfo();
  247. };
  248. class CMultiMode : public CIntObject
  249. {
  250. public:
  251. CPicture *bg;
  252. CTextInput *txt;
  253. AdventureMapButton *btns[7]; //0 - hotseat, 6 - cancel
  254. CGStatusBar *bar;
  255. CMultiMode();
  256. void openHotseat();
  257. void hostTCP();
  258. void joinTCP();
  259. };
  260. class CHotSeatPlayers : public CIntObject
  261. {
  262. public:
  263. CPicture *bg;
  264. CTextInput *txt[8];
  265. AdventureMapButton *ok, *cancel;
  266. CGStatusBar *bar;
  267. CHotSeatPlayers(const std::string &firstPlayer);
  268. void enterSelectionScreen();
  269. };
  270. class CBonusSelection : public CIntObject
  271. {
  272. SDL_Surface * background;
  273. AdventureMapButton * startB, * backB;
  274. //campaign & map descriptions:
  275. CTextBox * cmpgDesc, * mapDesc;
  276. struct SCampPositions
  277. {
  278. std::string campPrefix;
  279. int colorSuffixLength;
  280. struct SRegionDesc
  281. {
  282. std::string infix;
  283. int xpos, ypos;
  284. };
  285. std::vector<SRegionDesc> regions;
  286. };
  287. std::vector<SCampPositions> campDescriptions;
  288. class CRegion : public CIntObject
  289. {
  290. CBonusSelection * owner;
  291. SDL_Surface * graphics[3]; //[0] - not selected, [1] - selected, [2] - striped
  292. bool accessible; //false if region should be striped
  293. bool selectable; //true if region should be selectable
  294. int myNumber; //number of region
  295. public:
  296. std::string rclickText;
  297. CRegion(CBonusSelection * _owner, bool _accessible, bool _selectable, int _myNumber);
  298. ~CRegion();
  299. void clickLeft(tribool down, bool previousState);
  300. void clickRight(tribool down, bool previousState);
  301. void show(SDL_Surface * to);
  302. };
  303. std::vector<CRegion *> regions;
  304. CRegion * highlightedRegion;
  305. void loadPositionsOfGraphics();
  306. CCampaignState * ourCampaign;
  307. CMapHeader *ourHeader;
  308. CDefHandler *sizes; //icons of map sizes
  309. SDL_Surface * diffPics[5]; //pictures of difficulties, user-selectable (or not if campaign locks this)
  310. AdventureMapButton * diffLb, * diffRb; //buttons for changing difficulty
  311. void changeDiff(bool increase); //if false, then decrease
  312. //bonus selection
  313. void updateBonusSelection();
  314. CHighlightableButtonsGroup * bonuses;
  315. public:
  316. void bonusSelectionChanges(int choosenBonus);
  317. StartInfo sInfo;
  318. CDefHandler *sFlags;
  319. void selectMap(int whichOne);
  320. void selectBonus(int id);
  321. CBonusSelection(CCampaignState * _ourCampaign);
  322. ~CBonusSelection();
  323. void showAll(SDL_Surface * to);
  324. void show(SDL_Surface * to);
  325. void goBack();
  326. void startMap();
  327. };
  328. class CGPreGame : public CIntObject, public IUpdateable
  329. {
  330. public:
  331. SDL_Surface *mainbg;
  332. CMenuScreen *scrs[4];
  333. SDL_Surface *nHero, *rHero, *nTown, *rTown; // none/random hero/town imgs
  334. CDefHandler *bonuses;
  335. CDefHandler *victory, *loss;
  336. CGPreGame();
  337. ~CGPreGame();
  338. void update();
  339. void openSel(CMenuScreen::EState type, CMenuScreen::EMultiMode multi = CMenuScreen::SINGLE_PLAYER);
  340. void loadGraphics();
  341. void disposeGraphics();
  342. };
  343. extern CGPreGame *CGP;
  344. #endif // __CPREGAME_H__