CPlayerInterface.h 14 KB

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