CPlayerInterface.h 15 KB

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