CPlayerInterface.h 14 KB

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