CPlayerInterface.h 15 KB

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