CPlayerInterface.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. #pragma once
  2. #include "../lib/CondSh.h"
  3. #include "FunctionList.h"
  4. #include "../lib/CGameInterface.h"
  5. #include "gui/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; //nullptr if castle window isn't opened
  89. static CBattleInterface * battleInt; //nullptr if no battle
  90. CInGameConsole * cingconsole;
  91. shared_ptr<CCallback> cb; //to communicate with engine
  92. const BattleAction *curAction; //during the battle - action currently performed by active stack (or nullptr)
  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. //During battle is quick combat mode is used
  99. shared_ptr<CBattleGameInterface> autofightingAI; //AI that makes decisions
  100. bool isAutoFightOn; //Flag, switch it to stop quick combat. Don't touch if there is no battle interface.
  101. struct SpellbookLastSetting
  102. {
  103. int spellbookLastPageBattle, spellbokLastPageAdvmap; //on which page we left spellbook
  104. int spellbookLastTabBattle, spellbookLastTabAdvmap; //on which page we left spellbook
  105. SpellbookLastSetting();
  106. template <typename Handler> void serialize( Handler &h, const int version )
  107. {
  108. h & spellbookLastPageBattle & spellbokLastPageAdvmap & spellbookLastTabBattle & spellbookLastTabAdvmap;
  109. }
  110. } spellbookSettings;
  111. void update();
  112. void initializeHeroTownList();
  113. int getLastIndex(std::string namePrefix);
  114. //overridden funcs from CGameInterface
  115. void buildChanged(const CGTownInstance *town, BuildingID buildingID, int what) override; //what: 1 - built, 2 - demolished
  116. 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
  117. void stackChangedType(const StackLocation &location, const CCreature &newType) override; //used eg. when upgrading creatures
  118. void stacksErased(const StackLocation &location) override; //stack removed from previously filled slot
  119. void stacksSwapped(const StackLocation &loc1, const StackLocation &loc2) override;
  120. void newStackInserted(const StackLocation &location, const CStackInstance &stack) override; //new stack inserted at given (previously empty position)
  121. 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
  122. void artifactPut(const ArtifactLocation &al);
  123. void artifactRemoved(const ArtifactLocation &al);
  124. void artifactMoved(const ArtifactLocation &src, const ArtifactLocation &dst);
  125. void artifactAssembled(const ArtifactLocation &al);
  126. void artifactDisassembled(const ArtifactLocation &al);
  127. void heroCreated(const CGHeroInstance* hero) override;
  128. void heroGotLevel(const CGHeroInstance *hero, PrimarySkill::PrimarySkill pskill, std::vector<SecondarySkill> &skills, QueryID queryID) override;
  129. void commanderGotLevel (const CCommanderInstance * commander, std::vector<ui32> skills, QueryID queryID) override;
  130. void heroInGarrisonChange(const CGTownInstance *town) override;
  131. void heroMoved(const TryMoveHero & details) override;
  132. void heroPrimarySkillChanged(const CGHeroInstance * hero, int which, si64 val) override;
  133. void heroSecondarySkillChanged(const CGHeroInstance * hero, int which, int val) override;
  134. void heroManaPointsChanged(const CGHeroInstance * hero) override;
  135. void heroMovePointsChanged(const CGHeroInstance * hero) override;
  136. void heroVisitsTown(const CGHeroInstance* hero, const CGTownInstance * town) override;
  137. void receivedResource(int type, int val) override;
  138. void showInfoDialog(const std::string &text, const std::vector<Component*> &components, int soundID) override;
  139. void showRecruitmentDialog(const CGDwelling *dwelling, const CArmedInstance *dst, int level) override;
  140. void showShipyardDialog(const IShipyard *obj) override; //obj may be town or shipyard;
  141. void showBlockingDialog(const std::string &text, const std::vector<Component> &components, QueryID 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.
  142. void showGarrisonDialog(const CArmedInstance *up, const CGHeroInstance *down, bool removableUnits, QueryID queryID) override;
  143. void showPuzzleMap() override;
  144. void showMarketWindow(const IMarket *market, const CGHeroInstance *visitor) override;
  145. void showUniversityWindow(const IMarket *market, const CGHeroInstance *visitor) override;
  146. void showHillFortWindow(const CGObjectInstance *object, const CGHeroInstance *visitor) override;
  147. void showTavernWindow(const CGObjectInstance *townOrTavern) override;
  148. void showThievesGuildWindow (const CGObjectInstance * obj) override;
  149. void showQuestLog() override;
  150. void advmapSpellCast(const CGHeroInstance * caster, int spellID) override; //called when a hero casts a spell
  151. void tileHidden(const boost::unordered_set<int3, ShashInt3> &pos) override; //called when given tiles become hidden under fog of war
  152. void tileRevealed(const boost::unordered_set<int3, ShashInt3> &pos) override; //called when fog of war disappears from given tiles
  153. void newObject(const CGObjectInstance * obj) override;
  154. void availableArtifactsChanged(const CGBlackMarket *bm = nullptr) override; //bm may be nullptr, then artifacts are changed in the global pool (used by merchants in towns)
  155. void yourTurn() override;
  156. void availableCreaturesChanged(const CGDwelling *town) override;
  157. void heroBonusChanged(const CGHeroInstance *hero, const Bonus &bonus, bool gain) override;//if gain hero received bonus, else he lost it
  158. void playerBonusChanged(const Bonus &bonus, bool gain) override;
  159. void requestRealized(PackageApplied *pa) override;
  160. void heroExchangeStarted(ObjectInstanceID hero1, ObjectInstanceID hero2, QueryID query) override;
  161. void centerView (int3 pos, int focusTime) override;
  162. void objectPropertyChanged(const SetObjectProperty * sop) override;
  163. void objectRemoved(const CGObjectInstance *obj) override;
  164. void gameOver(PlayerColor player, bool victory) override;
  165. void playerStartsTurn(PlayerColor player) override; //called before yourTurn on active itnerface
  166. void showComp(const Component &comp, std::string message) override; //display component in the advmapint infobox
  167. void saveGame(COSer<CSaveFile> &h, const int version) override; //saving
  168. void loadGame(CISer<CLoadFile> &h, const int version) override; //loading
  169. //for battles
  170. void actionFinished(const BattleAction& action) override;//occurs AFTER action taken by active stack or by the hero
  171. void actionStarted(const BattleAction& action) override;//occurs BEFORE action taken by active stack or by the hero
  172. BattleAction activeStack(const CStack * stack) override; //called when it's turn of that stack
  173. void battleAttack(const BattleAttack *ba) override; //stack performs attack
  174. void battleEnd(const BattleResult *br) override; //end of battle
  175. void battleNewRoundFirst(int round) override; //called at the beginning of each turn before changes are applied; used for HP regen handling
  176. 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
  177. void battleStackMoved(const CStack * stack, std::vector<BattleHex> dest, int distance) override;
  178. void battleSpellCast(const BattleSpellCast *sc) override;
  179. void battleStacksEffectsSet(const SetStackEffect & sse) override; //called when a specific effect is set to stacks
  180. void battleTriggerEffect(const BattleTriggerEffect & bte) override; //various one-shot effect
  181. void battleStacksAttacked(const std::vector<BattleStackAttacked> & bsa) override;
  182. 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
  183. void battleStacksHealedRes(const std::vector<std::pair<ui32, ui32> > & healedStacks, bool lifeDrain, bool tentHeal, si32 lifeDrainFrom) override; //called when stacks are healed / resurrected
  184. void battleNewStackAppeared(const CStack * stack) override; //not called at the beginning of a battle or by resurrection; called eg. when elemental is summoned
  185. void battleObstaclesRemoved(const std::set<si32> & removedObstacles) override; //called when a certain set of obstacles is removed from batlefield; IDs of them are given
  186. void battleCatapultAttacked(const CatapultAttack & ca) override; //called when catapult makes an attack
  187. void battleStacksRemoved(const BattleStacksRemoved & bsr) override; //called when certain stack is completely removed from battlefield
  188. void battleObstaclePlaced(const CObstacleInstance &obstacle) override;
  189. void yourTacticPhase(int distance) override;
  190. //-------------//
  191. void showArtifactAssemblyDialog(ui32 artifactID, ui32 assembleTo, bool assemble, CFunctionList<void()> onYes, CFunctionList<void()> onNo);
  192. void garrisonsChanged(std::vector<const CGObjectInstance *> objs);
  193. void garrisonChanged(const CGObjectInstance * obj);
  194. void heroKilled(const CGHeroInstance* hero);
  195. void waitWhileDialog(bool unlockPim = true);
  196. void waitForAllDialogs(bool unlockPim = true);
  197. bool shiftPressed() const; //determines if shift key is pressed (left or right or both)
  198. bool ctrlPressed() const; //determines if ctrl key is pressed (left or right or both)
  199. bool altPressed() const; //determines if alt key is pressed (left or right or both)
  200. void redrawHeroWin(const CGHeroInstance * hero);
  201. void openTownWindow(const CGTownInstance * town); //shows townscreen
  202. void openHeroWindow(const CGHeroInstance * hero); //shows hero window with given hero
  203. void updateInfo(const CGObjectInstance * specific);
  204. void init(shared_ptr<CCallback> CB);
  205. int3 repairScreenPos(int3 pos); //returns position closest to pos we can center screen on
  206. void showInfoDialog(const std::string &text, CComponent * component);
  207. void showInfoDialog(const std::string &text, const std::vector<CComponent*> & components = std::vector<CComponent*>(), int soundID = 0, bool delComps = false);
  208. 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
  209. void stopMovement();
  210. bool moveHero(const CGHeroInstance *h, CGPath path);
  211. void initMovement(const TryMoveHero &details, const CGHeroInstance * ho, const int3 &hp );//initializing objects and performing first step of move
  212. void movementPxStep( const TryMoveHero &details, int i, const int3 &hp, const CGHeroInstance * ho );//performing step of movement
  213. void finishMovement( const TryMoveHero &details, const int3 &hp, const CGHeroInstance * ho ); //finish movement
  214. void eraseCurrentPathOf( const CGHeroInstance * ho, bool checkForExistanceOfPath = true );
  215. void removeLastNodeFromPath(const CGHeroInstance *ho);
  216. CGPath *getAndVerifyPath( const CGHeroInstance * h );
  217. void acceptTurn(); //used during hot seat after your turn message is close
  218. void tryDiggging(const CGHeroInstance *h);
  219. void showShipyardDialogOrProblemPopup(const IShipyard *obj); //obj may be town or shipyard;
  220. void requestReturningToMainMenu();
  221. void requestStoppingClient();
  222. void sendCustomEvent(int code);
  223. void proposeLoadingGame();
  224. CPlayerInterface(PlayerColor Player);//c-tor
  225. ~CPlayerInterface();//d-tor
  226. static CondSh<bool> terminate_cond; // confirm termination
  227. //////////////////////////////////////////////////////////////////////////
  228. template <typename Handler> void serializeTempl(Handler &h, const int version);
  229. };
  230. extern CPlayerInterface * LOCPLINT;