CPlayerInterface.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. #ifndef __CPLAYERINTERFACE_H__
  2. #define __CPLAYERINTERFACE_H__
  3. #include "../global.h"
  4. #include "../lib/CGameInterface.h"
  5. #include "../lib/CondSh.h"
  6. #include <map>
  7. #include <list>
  8. #include <algorithm>
  9. #include "GUIBase.h"
  10. #include "FunctionList.h"
  11. #ifdef __GNUC__
  12. #define sprintf_s snprintf
  13. #endif
  14. #ifdef max
  15. #undef max
  16. #endif
  17. #ifdef min
  18. #undef min
  19. #endif
  20. /*
  21. * CPlayerInterface.h, part of VCMI engine
  22. *
  23. * Authors: listed in file AUTHORS in main folder
  24. *
  25. * License: GNU General Public License v2.0 or later
  26. * Full text of license available in license.txt file, in main folder
  27. *
  28. */
  29. class CDefEssential;
  30. class AdventureMapButton;
  31. class CHighlightableButtonsGroup;
  32. class CDefHandler;
  33. struct TryMoveHero;
  34. class CDefEssential;
  35. class CGHeroInstance;
  36. class CAdvMapInt;
  37. class CCastleInterface;
  38. class CBattleInterface;
  39. class CStack;
  40. class SComponent;
  41. class CCreature;
  42. struct SDL_Surface;
  43. struct CGPath;
  44. class CCreatureAnimation;
  45. class CSelectableComponent;
  46. class CCreatureSet;
  47. class CGObjectInstance;
  48. class CSlider;
  49. struct UpgradeInfo;
  50. template <typename T> struct CondSh;
  51. class CInGameConsole;
  52. class CGarrisonInt;
  53. class CInGameConsole;
  54. union SDL_Event;
  55. class IStatusBar;
  56. class CInfoWindow;
  57. class IShowActivable;
  58. class ClickableL;
  59. class ClickableR;
  60. class Hoverable;
  61. class KeyInterested;
  62. class MotionInterested;
  63. class TimeInterested;
  64. class IShowable;
  65. struct CPathsInfo;
  66. namespace boost
  67. {
  68. class mutex;
  69. class recursive_mutex;
  70. };
  71. /// Stores information about system options like hero move speed, map scrolling speed, creature animation speed,...
  72. struct SystemOptions
  73. {
  74. std::string playerName;
  75. ui8 heroMoveSpeed;/*, enemyMoveSpeed*/ //speed of player's hero movement
  76. ui8 mapScrollingSpeed; //map scrolling speed
  77. ui8 musicVolume, soundVolume;
  78. //TODO: rest of system options
  79. //battle settings
  80. ui8 printCellBorders; //if true, cell borders will be printed
  81. ui8 printStackRange; //if true,range of active stack will be printed
  82. ui8 animSpeed; //speed of animation; 1 - slowest, 2 - medium, 4 - fastest
  83. ui8 printMouseShadow; //if true, hex under mouse will be shaded
  84. ui8 showQueue;
  85. SystemOptions();
  86. void setHeroMoveSpeed(int newSpeed); //set for the member above
  87. void setMapScrollingSpeed(int newSpeed); //set the member above
  88. void setMusicVolume(int newVolume);
  89. void setSoundVolume(int newVolume);
  90. void setPlayerName(const std::string &newPlayerName);
  91. void settingsChanged(); //updates file with "default" settings for next running of application
  92. void apply();
  93. template <typename Handler> void serialize(Handler &h, const int version)
  94. {
  95. h & playerName;
  96. h & heroMoveSpeed & mapScrollingSpeed & musicVolume & soundVolume;
  97. h & printCellBorders & printStackRange & animSpeed & printMouseShadow & showQueue;
  98. }
  99. };
  100. extern SystemOptions GDefaultOptions; //defined and inited in CMT.cpp, stores default settings loaded with application
  101. /// Central class for managing user interface logic
  102. class CPlayerInterface : public CGameInterface, public IUpdateable
  103. {
  104. public:
  105. bool observerInDuelMode;
  106. //minor interfaces
  107. CondSh<bool> *showingDialog; //indicates if dialog box is displayed
  108. static boost::recursive_mutex *pim;
  109. bool makingTurn; //if player is already making his turn
  110. int firstCall; // -1 - just loaded game; 1 - just started game; 0 otherwise
  111. int autosaveCount;
  112. static const int SAVES_COUNT = 5;
  113. static int howManyPeople;
  114. SystemOptions sysOpts;
  115. CCastleInterface * castleInt; //NULL if castle window isn't opened
  116. static CBattleInterface * battleInt; //NULL if no battle
  117. CInGameConsole * cingconsole;
  118. CCallback * cb; //to communicate with engine
  119. const BattleAction *curAction; //during the battle - action currently performed by active stack (or NULL)
  120. std::list<CInfoWindow *> dialogs; //queue of dialogs awaiting to be shown (not currently shown!)
  121. std::vector<const CGHeroInstance *> wanderingHeroes; //our heroes on the adventure map (not the garrisoned ones)
  122. std::vector<const CGTownInstance *> towns; //our heroes on the adventure map (not the garrisoned ones)
  123. std::map<const CGHeroInstance *, CGPath> paths; //maps hero => selected path in adventure map
  124. std::vector<const CGHeroInstance *> sleepingHeroes;
  125. struct SpellbookLastSetting
  126. {
  127. int spellbookLastPageBattle, spellbokLastPageAdvmap; //on which page we left spellbook
  128. int spellbookLastTabBattle, spellbookLastTabAdvmap; //on which page we left spellbook
  129. SpellbookLastSetting();
  130. template <typename Handler> void serialize( Handler &h, const int version )
  131. {
  132. h & spellbookLastPageBattle & spellbokLastPageAdvmap & spellbookLastTabBattle & spellbookLastTabAdvmap;
  133. }
  134. } spellbookSettings;
  135. void update();
  136. void recreateHeroTownList();
  137. const CGHeroInstance *getWHero(int pos); //returns NULL if position is not valid
  138. int getLastIndex(std::string namePrefix);
  139. //overridden funcs from CGameInterface
  140. void buildChanged(const CGTownInstance *town, int buildingID, int what) OVERRIDE; //what: 1 - built, 2 - demolished
  141. void stackChagedCount(const StackLocation &location, const TQuantity &change, bool isAbsolute) OVERRIDE; //if absolute, change is the new count; otherwise count was modified by adding change
  142. void stackChangedType(const StackLocation &location, const CCreature &newType) OVERRIDE; //used eg. when upgrading creatures
  143. void stacksErased(const StackLocation &location) OVERRIDE; //stack removed from previously filled slot
  144. void stacksSwapped(const StackLocation &loc1, const StackLocation &loc2) OVERRIDE;
  145. void newStackInserted(const StackLocation &location, const CStackInstance &stack) OVERRIDE; //new stack inserted at given (previously empty position)
  146. void stacksRebalanced(const StackLocation &src, const StackLocation &dst, TQuantity count) OVERRIDE; //moves creatures from src stack to dst slot, may be used for merging/splittint/moving stacks
  147. void artifactPut(const ArtifactLocation &al);
  148. void artifactRemoved(const ArtifactLocation &al);
  149. void artifactMoved(const ArtifactLocation &src, const ArtifactLocation &dst);
  150. void artifactAssembled(const ArtifactLocation &al);
  151. void artifactDisassembled(const ArtifactLocation &al);
  152. void heroCreated(const CGHeroInstance* hero) OVERRIDE;
  153. void heroGotLevel(const CGHeroInstance *hero, int pskill, std::vector<ui16> &skills, boost::function<void(ui32)> &callback) OVERRIDE;
  154. void heroInGarrisonChange(const CGTownInstance *town) OVERRIDE;
  155. void heroMoved(const TryMoveHero & details) OVERRIDE;
  156. void heroPrimarySkillChanged(const CGHeroInstance * hero, int which, si64 val) OVERRIDE;
  157. void heroSecondarySkillChanged(const CGHeroInstance * hero, int which, int val) OVERRIDE;
  158. void heroManaPointsChanged(const CGHeroInstance * hero) OVERRIDE;
  159. void heroMovePointsChanged(const CGHeroInstance * hero) OVERRIDE;
  160. void heroVisitsTown(const CGHeroInstance* hero, const CGTownInstance * town) OVERRIDE;
  161. void receivedResource(int type, int val) OVERRIDE;
  162. void showInfoDialog(const std::string &text, const std::vector<Component*> &components, int soundID) OVERRIDE;
  163. void showRecruitmentDialog(const CGDwelling *dwelling, const CArmedInstance *dst, int level) OVERRIDE;
  164. void showShipyardDialog(const IShipyard *obj) OVERRIDE; //obj may be town or shipyard;
  165. void showBlockingDialog(const std::string &text, const std::vector<Component> &components, ui32 askID, int soundID, bool selection, bool cancel) OVERRIDE; //Show a dialog, player must take decision. If selection then he has to choose between one of given components, if cancel he is allowed to not choose. After making choice, CCallback::selectionMade should be called with number of selected component (1 - n) or 0 for cancel (if allowed) and askID.
  166. void showGarrisonDialog(const CArmedInstance *up, const CGHeroInstance *down, bool removableUnits, boost::function<void()> &onEnd) OVERRIDE;
  167. void showPuzzleMap() OVERRIDE;
  168. void showMarketWindow(const IMarket *market, const CGHeroInstance *visitor) OVERRIDE;
  169. void showUniversityWindow(const IMarket *market, const CGHeroInstance *visitor) OVERRIDE;
  170. void showHillFortWindow(const CGObjectInstance *object, const CGHeroInstance *visitor) OVERRIDE;
  171. void showTavernWindow(const CGObjectInstance *townOrTavern) OVERRIDE;
  172. void advmapSpellCast(const CGHeroInstance * caster, int spellID) OVERRIDE; //called when a hero casts a spell
  173. void tileHidden(const boost::unordered_set<int3, ShashInt3> &pos) OVERRIDE; //called when given tiles become hidden under fog of war
  174. void tileRevealed(const boost::unordered_set<int3, ShashInt3> &pos) OVERRIDE; //called when fog of war disappears from given tiles
  175. void newObject(const CGObjectInstance * obj) OVERRIDE;
  176. void availableArtifactsChanged(const CGBlackMarket *bm = NULL) OVERRIDE; //bm may be NULL, then artifacts are changed in the global pool (used by merchants in towns)
  177. void yourTurn() OVERRIDE;
  178. void availableCreaturesChanged(const CGDwelling *town) OVERRIDE;
  179. void heroBonusChanged(const CGHeroInstance *hero, const Bonus &bonus, bool gain) OVERRIDE;//if gain hero received bonus, else he lost it
  180. void playerBonusChanged(const Bonus &bonus, bool gain) OVERRIDE;
  181. void requestRealized(PackageApplied *pa) OVERRIDE;
  182. void heroExchangeStarted(si32 hero1, si32 hero2) OVERRIDE;
  183. void centerView (int3 pos, int focusTime) OVERRIDE;
  184. void objectPropertyChanged(const SetObjectProperty * sop) OVERRIDE;
  185. void objectRemoved(const CGObjectInstance *obj) OVERRIDE;
  186. void gameOver(ui8 player, bool victory) OVERRIDE;
  187. void serialize(COSer<CSaveFile> &h, const int version) OVERRIDE; //saving
  188. void serialize(CISer<CLoadFile> &h, const int version) OVERRIDE; //loading
  189. //for battles
  190. void actionFinished(const BattleAction* action) OVERRIDE;//occurs AFTER action taken by active stack or by the hero
  191. void actionStarted(const BattleAction* action) OVERRIDE;//occurs BEFORE action taken by active stack or by the hero
  192. BattleAction activeStack(const CStack * stack) OVERRIDE; //called when it's turn of that stack
  193. void battleAttack(const BattleAttack *ba) OVERRIDE; //stack performs attack
  194. void battleEnd(const BattleResult *br) OVERRIDE; //end of battle
  195. void battleNewRoundFirst(int round) OVERRIDE; //called at the beginning of each turn before changes are applied; used for HP regen handling
  196. void battleNewRound(int round) OVERRIDE; //called at the beginning of each turn, round=-1 is the tactic phase, round=0 is the first "normal" turn
  197. void battleStackMoved(const CStack * stack, std::vector<THex> dest, int distance) OVERRIDE;
  198. void battleSpellCast(const BattleSpellCast *sc) OVERRIDE;
  199. void battleStacksEffectsSet(const SetStackEffect & sse) OVERRIDE; //called when a specific effect is set to stacks
  200. void battleStacksAttacked(const std::vector<BattleStackAttacked> & bsa) OVERRIDE;
  201. void battleStart(const CCreatureSet *army1, const CCreatureSet *army2, int3 tile, const CGHeroInstance *hero1, const CGHeroInstance *hero2, bool side) OVERRIDE; //called by engine when battle starts; side=0 - left, side=1 - right
  202. void battleStacksHealedRes(const std::vector<std::pair<ui32, ui32> > & healedStacks, bool lifeDrain, bool tentHeal, si32 lifeDrainFrom) OVERRIDE; //called when stacks are healed / resurrected
  203. void battleNewStackAppeared(const CStack * stack) OVERRIDE; //not called at the beginning of a battle or by resurrection; called eg. when elemental is summoned
  204. void battleObstaclesRemoved(const std::set<si32> & removedObstacles) OVERRIDE; //called when a certain set of obstacles is removed from batlefield; IDs of them are given
  205. void battleCatapultAttacked(const CatapultAttack & ca) OVERRIDE; //called when catapult makes an attack
  206. void battleStacksRemoved(const BattleStacksRemoved & bsr) OVERRIDE; //called when certain stack is completely removed from battlefield
  207. void yourTacticPhase(int distance) OVERRIDE;
  208. //-------------//
  209. void showArtifactAssemblyDialog(ui32 artifactID, ui32 assembleTo, bool assemble, CFunctionList<void()> onYes, CFunctionList<void()> onNo);
  210. void garrisonChanged(const CGObjectInstance * obj, bool updateInfobox = true);
  211. void heroKilled(const CGHeroInstance* hero);
  212. void waitWhileDialog();
  213. bool shiftPressed() const; //determines if shift key is pressed (left or right or both)
  214. bool ctrlPressed() const; //determines if ctrl key is pressed (left or right or both)
  215. bool altPressed() const; //determines if alt key is pressed (left or right or both)
  216. void redrawHeroWin(const CGHeroInstance * hero);
  217. void showComp(SComponent comp); //TODO: comment me
  218. void openTownWindow(const CGTownInstance * town); //shows townscreen
  219. void openHeroWindow(const CGHeroInstance * hero); //shows hero window with given hero
  220. SDL_Surface * infoWin(const CGObjectInstance * specific); //specific=0 => draws info about selected town/hero
  221. void updateInfo(const CGObjectInstance * specific);
  222. void init(CCallback * CB);
  223. int3 repairScreenPos(int3 pos); //returns position closest to pos we can center screen on
  224. void showInfoDialog(const std::string &text, const std::vector<SComponent*> & components = std::vector<SComponent*>(), int soundID = 0, bool delComps = false);
  225. void showYesNoDialog(const std::string &text, const std::vector<SComponent*> & components, CFunctionList<void()> onYes, CFunctionList<void()> onNo, bool DelComps); //deactivateCur - whether current main interface should be deactivated; delComps - if components will be deleted on window close
  226. void stopMovement();
  227. bool moveHero(const CGHeroInstance *h, CGPath path);
  228. void initMovement(const TryMoveHero &details, const CGHeroInstance * ho, const int3 &hp );//initializing objects and performing first step of move
  229. void movementPxStep( const TryMoveHero &details, int i, const int3 &hp, const CGHeroInstance * ho );//performing step of movement
  230. void finishMovement( const TryMoveHero &details, const int3 &hp, const CGHeroInstance * ho ); //finish movement
  231. void eraseCurrentPathOf( const CGHeroInstance * ho, bool checkForExistanceOfPath = true );
  232. void removeLastNodeFromPath(const CGHeroInstance *ho);
  233. CGPath *getAndVerifyPath( const CGHeroInstance * h );
  234. void acceptTurn(); //used during hot seat after your turn message is close
  235. void tryDiggging(const CGHeroInstance *h);
  236. void showShipyardDialogOrProblemPopup(const IShipyard *obj); //obj may be town or shipyard;
  237. void requestReturningToMainMenu();
  238. void requestStoppingClient();
  239. void sendCustomEvent(int code);
  240. CPlayerInterface(int Player);//c-tor
  241. ~CPlayerInterface();//d-tor
  242. CondSh<bool> terminate_cond; // confirm termination
  243. //////////////////////////////////////////////////////////////////////////
  244. template <typename Handler> void serializeTempl(Handler &h, const int version);
  245. };
  246. extern CPlayerInterface * LOCPLINT;
  247. #endif // __CPLAYERINTERFACE_H__