CPlayerInterface.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. #ifndef __CPLAYERINTERFACE_H__
  2. #define __CPLAYERINTERFACE_H__
  3. #include "../global.h"
  4. #include "../CGameInterface.h"
  5. #include "../lib/CondSh.h"
  6. #include "SDL_framerate.h"
  7. #include <map>
  8. #include <list>
  9. #include <algorithm>
  10. #include "GUIBase.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. struct SystemOptions
  72. {
  73. std::string playerName;
  74. ui8 heroMoveSpeed;/*, enemyMoveSpeed*/ //speed of player's hero movement
  75. ui8 mapScrollingSpeed; //map scrolling speed
  76. ui8 musicVolume, soundVolume;
  77. //TODO: rest of system options
  78. //battle settings
  79. ui8 printCellBorders; //if true, cell borders will be printed
  80. ui8 printStackRange; //if true,range of active stack will be printed
  81. ui8 animSpeed; //speed of animation; 1 - slowest, 2 - medium, 4 - fastest
  82. ui8 printMouseShadow; //if true, hex under mouse will be shaded
  83. ui8 showQueue;
  84. SystemOptions();
  85. void setHeroMoveSpeed(int newSpeed); //set for the member above
  86. void setMapScrollingSpeed(int newSpeed); //set the member above
  87. void setMusicVolume(int newVolume);
  88. void setSoundVolume(int newVolume);
  89. void setPlayerName(const std::string &newPlayerName);
  90. void settingsChanged(); //updates file with "default" settings for next running of application
  91. void apply();
  92. template <typename Handler> void serialize(Handler &h, const int version)
  93. {
  94. h & playerName;
  95. h & heroMoveSpeed & mapScrollingSpeed & musicVolume & soundVolume;
  96. h & printCellBorders & printStackRange & animSpeed & printMouseShadow & showQueue;
  97. }
  98. };
  99. extern SystemOptions GDefaultOptions; //defined and inited in CMT.cpp, stores default settings loaded with application
  100. class CPlayerInterface : public CGameInterface, public IUpdateable
  101. {
  102. public:
  103. bool observerInDuelMode;
  104. //minor interfaces
  105. CondSh<bool> *showingDialog; //indicates if dialog box is displayed
  106. boost::recursive_mutex *pim;
  107. bool makingTurn; //if player is already making his turn
  108. int firstCall; // -1 - just loaded game; 1 - just started game; 0 otherwise
  109. int autosaveCount;
  110. static const int SAVES_COUNT = 5;
  111. static int howManyPeople;
  112. SystemOptions sysOpts;
  113. CCastleInterface * castleInt; //NULL if castle window isn't opened
  114. static CBattleInterface * battleInt; //NULL if no battle
  115. FPSmanager * mainFPSmng; //to keep const framerate
  116. CInGameConsole * cingconsole;
  117. CCallback * cb; //to communicate with engine
  118. const BattleAction *curAction; //during the battle - action currently performed by active stack (or NULL)
  119. std::list<CInfoWindow *> dialogs; //queue of dialogs awaiting to be shown (not currently shown!)
  120. std::vector<const CGHeroInstance *> wanderingHeroes; //our heroes on the adventure map (not the garrisoned ones)
  121. std::vector<const CGTownInstance *> towns; //our heroes on the adventure map (not the garrisoned ones)
  122. std::map<const CGHeroInstance *, CGPath> paths; //maps hero => selected path in adventure map
  123. struct SpellbookLastSetting
  124. {
  125. int spellbookLastPageBattle, spellbokLastPageAdvmap; //on which page we left spellbook
  126. int spellbookLastTabBattle, spellbookLastTabAdvmap; //on which page we left spellbook
  127. SpellbookLastSetting();
  128. template <typename Handler> void serialize( Handler &h, const int version )
  129. {
  130. h & spellbookLastPageBattle & spellbokLastPageAdvmap & spellbookLastTabBattle & spellbookLastTabAdvmap;
  131. }
  132. } spellbookSettings;
  133. void update();
  134. void recreateHeroTownList();
  135. const CGHeroInstance *getWHero(int pos); //returns NULL if position is not valid
  136. int getLastIndex(std::string namePrefix);
  137. //overridden funcs from CGameInterface
  138. void buildChanged(const CGTownInstance *town, int buildingID, int what) OVERRIDE; //what: 1 - built, 2 - demolished
  139. 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
  140. void stackChangedType(const StackLocation &location, const CCreature &newType) OVERRIDE; //used eg. when upgrading creatures
  141. void stacksErased(const StackLocation &location) OVERRIDE; //stack removed from previously filled slot
  142. void stacksSwapped(const StackLocation &loc1, const StackLocation &loc2) OVERRIDE;
  143. void newStackInserted(const StackLocation &location, const CStackInstance &stack) OVERRIDE; //new stack inserted at given (previously empty position)
  144. 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
  145. void artifactPut(const ArtifactLocation &al);
  146. void artifactRemoved(const ArtifactLocation &al);
  147. void artifactMoved(const ArtifactLocation &src, const ArtifactLocation &dst);
  148. void artifactAssembled(const ArtifactLocation &al);
  149. void artifactDisassembled(const ArtifactLocation &al);
  150. void heroCreated(const CGHeroInstance* hero) OVERRIDE;
  151. void heroGotLevel(const CGHeroInstance *hero, int pskill, std::vector<ui16> &skills, boost::function<void(ui32)> &callback) OVERRIDE;
  152. void heroInGarrisonChange(const CGTownInstance *town) OVERRIDE;
  153. void heroMoved(const TryMoveHero & details) OVERRIDE;
  154. void heroPrimarySkillChanged(const CGHeroInstance * hero, int which, si64 val) OVERRIDE;
  155. void heroSecondarySkillChanged(const CGHeroInstance * hero, int which, int val) OVERRIDE;
  156. void heroManaPointsChanged(const CGHeroInstance * hero) OVERRIDE;
  157. void heroMovePointsChanged(const CGHeroInstance * hero) OVERRIDE;
  158. void heroVisitsTown(const CGHeroInstance* hero, const CGTownInstance * town) OVERRIDE;
  159. void receivedResource(int type, int val) OVERRIDE;
  160. void showInfoDialog(const std::string &text, const std::vector<Component*> &components, int soundID) OVERRIDE;
  161. void showRecruitmentDialog(const CGDwelling *dwelling, const CArmedInstance *dst, int level) OVERRIDE;
  162. void showShipyardDialog(const IShipyard *obj) OVERRIDE; //obj may be town or shipyard;
  163. 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.
  164. void showGarrisonDialog(const CArmedInstance *up, const CGHeroInstance *down, bool removableUnits, boost::function<void()> &onEnd) OVERRIDE;
  165. void showPuzzleMap() OVERRIDE;
  166. void showMarketWindow(const IMarket *market, const CGHeroInstance *visitor) OVERRIDE;
  167. void showUniversityWindow(const IMarket *market, const CGHeroInstance *visitor) OVERRIDE;
  168. void showHillFortWindow(const CGObjectInstance *object, const CGHeroInstance *visitor) OVERRIDE;
  169. void showTavernWindow(const CGObjectInstance *townOrTavern) OVERRIDE;
  170. void advmapSpellCast(const CGHeroInstance * caster, int spellID) OVERRIDE; //called when a hero casts a spell
  171. void tileHidden(const boost::unordered_set<int3, ShashInt3> &pos) OVERRIDE; //called when given tiles become hidden under fog of war
  172. void tileRevealed(const boost::unordered_set<int3, ShashInt3> &pos) OVERRIDE; //called when fog of war disappears from given tiles
  173. void newObject(const CGObjectInstance * obj) OVERRIDE;
  174. void availableArtifactsChanged(const CGBlackMarket *bm = NULL) OVERRIDE; //bm may be NULL, then artifacts are changed in the global pool (used by merchants in towns)
  175. void yourTurn() OVERRIDE;
  176. void availableCreaturesChanged(const CGDwelling *town) OVERRIDE;
  177. void heroBonusChanged(const CGHeroInstance *hero, const Bonus &bonus, bool gain) OVERRIDE;//if gain hero received bonus, else he lost it
  178. void playerBonusChanged(const Bonus &bonus, bool gain) OVERRIDE;
  179. void requestRealized(PackageApplied *pa) OVERRIDE;
  180. void heroExchangeStarted(si32 hero1, si32 hero2) OVERRIDE;
  181. void centerView (int3 pos, int focusTime) OVERRIDE;
  182. void objectPropertyChanged(const SetObjectProperty * sop) OVERRIDE;
  183. void objectRemoved(const CGObjectInstance *obj) OVERRIDE;
  184. void gameOver(ui8 player, bool victory) OVERRIDE;
  185. void serialize(COSer<CSaveFile> &h, const int version) OVERRIDE; //saving
  186. void serialize(CISer<CLoadFile> &h, const int version) OVERRIDE; //loading
  187. //for battles
  188. void actionFinished(const BattleAction* action) OVERRIDE;//occurs AFTER action taken by active stack or by the hero
  189. void actionStarted(const BattleAction* action) OVERRIDE;//occurs BEFORE action taken by active stack or by the hero
  190. BattleAction activeStack(const CStack * stack) OVERRIDE; //called when it's turn of that stack
  191. void battleAttack(const BattleAttack *ba) OVERRIDE; //stack performs attack
  192. void battleEnd(const BattleResult *br) OVERRIDE; //end of battle
  193. void battleNewRoundFirst(int round) OVERRIDE; //called at the beginning of each turn before changes are applied; used for HP regen handling
  194. void battleNewRound(int round) OVERRIDE; //called at the beggining of each turn, round=-1 is the tactic phase, round=0 is the first "normal" turn
  195. void battleStackMoved(const CStack * stack, THex dest, int distance, bool end) OVERRIDE;
  196. void battleSpellCast(const BattleSpellCast *sc) OVERRIDE;
  197. void battleStacksEffectsSet(const SetStackEffect & sse) OVERRIDE; //called when a specific effect is set to stacks
  198. void battleStacksAttacked(const std::vector<BattleStackAttacked> & bsa) OVERRIDE;
  199. 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
  200. void battleStacksHealedRes(const std::vector<std::pair<ui32, ui32> > & healedStacks, bool lifeDrain, si32 lifeDrainFrom) OVERRIDE; //called when stacks are healed / resurrected
  201. void battleNewStackAppeared(const CStack * stack) OVERRIDE; //not called at the beginning of a battle or by resurrection; called eg. when elemental is summoned
  202. void battleObstaclesRemoved(const std::set<si32> & removedObstacles) OVERRIDE; //called when a certain set of obstacles is removed from batlefield; IDs of them are given
  203. void battleCatapultAttacked(const CatapultAttack & ca) OVERRIDE; //called when catapult makes an attack
  204. void battleStacksRemoved(const BattleStacksRemoved & bsr) OVERRIDE; //called when certain stack is completely removed from battlefield
  205. //-------------//
  206. void showArtifactAssemblyDialog(ui32 artifactID, ui32 assembleTo, bool assemble, CFunctionList<void()> onYes, CFunctionList<void()> onNo);
  207. void garrisonChanged(const CGObjectInstance * obj);
  208. void heroKilled(const CGHeroInstance* hero);
  209. void waitWhileDialog();
  210. bool shiftPressed() const; //determines if shift key is pressed (left or right or both)
  211. bool ctrlPressed() const; //determines if ctrl key is pressed (left or right or both)
  212. bool altPressed() const; //determines if alt key is pressed (left or right or both)
  213. void redrawHeroWin(const CGHeroInstance * hero);
  214. void showComp(SComponent comp); //TODO: comment me
  215. void openTownWindow(const CGTownInstance * town); //shows townscreen
  216. void openHeroWindow(const CGHeroInstance * hero); //shows hero window with given hero
  217. SDL_Surface * infoWin(const CGObjectInstance * specific); //specific=0 => draws info about selected town/hero
  218. void updateInfo(const CGObjectInstance * specific);
  219. void init(ICallback * CB);
  220. int3 repairScreenPos(int3 pos); //returns position closest to pos we can center screen on
  221. void showInfoDialog(const std::string &text, const std::vector<SComponent*> & components = std::vector<SComponent*>(), int soundID = 0, bool delComps = false);
  222. 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
  223. void stopMovement();
  224. bool moveHero(const CGHeroInstance *h, CGPath path);
  225. void initMovement(const TryMoveHero &details, const CGHeroInstance * ho, const int3 &hp );//initializing objects and performing first step of move
  226. void movementPxStep( const TryMoveHero &details, int i, const int3 &hp, const CGHeroInstance * ho );//performing step of movement
  227. void finishMovement( const TryMoveHero &details, const int3 &hp, const CGHeroInstance * ho ); //finish movement
  228. void eraseCurrentPathOf( const CGHeroInstance * ho, bool checkForExistanceOfPath = true );
  229. CGPath *getAndVerifyPath( const CGHeroInstance * h );
  230. void acceptTurn(); //used during hot seat after your turn message is close
  231. void tryDiggging(const CGHeroInstance *h);
  232. void showShipyardDialogOrProblemPopup(const IShipyard *obj); //obj may be town or shipyard;
  233. void requestReturningToMainMenu();
  234. void requestStoppingClient();
  235. void sendCustomEvent(int code);
  236. CPlayerInterface(int Player);//c-tor
  237. ~CPlayerInterface();//d-tor
  238. CondSh<bool> terminate_cond; // confirm termination
  239. //////////////////////////////////////////////////////////////////////////
  240. template <typename Handler> void serializeTempl(Handler &h, const int version);
  241. };
  242. extern CPlayerInterface * LOCPLINT;
  243. #endif // __CPLAYERINTERFACE_H__