CPlayerInterface.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. #pragma once
  2. #include "../lib/FunctionList.h"
  3. #include "../lib/CGameInterface.h"
  4. #include "../lib/NetPacksBase.h"
  5. #include "gui/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 CButton;
  26. class CToggleGroup;
  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 CInfoWindow;
  51. class IShowActivatable;
  52. class ClickableL;
  53. class ClickableR;
  54. class Hoverable;
  55. class KeyInterested;
  56. class MotionInterested;
  57. class TimeInterested;
  58. class IShowable;
  59. struct CPathsInfo;
  60. namespace boost
  61. {
  62. class mutex;
  63. class recursive_mutex;
  64. }
  65. enum
  66. {
  67. /*CHANGE_SCREEN_RESOLUTION = 1,*/
  68. RETURN_TO_MAIN_MENU = 2,
  69. STOP_CLIENT = 3,
  70. RESTART_GAME,
  71. RETURN_TO_MENU_LOAD,
  72. FULLSCREEN_TOGGLED,
  73. PREPARE_RESTART_CAMPAIGN,
  74. FORCE_QUIT //quit client without question
  75. };
  76. /// Central class for managing user interface logic
  77. class CPlayerInterface : public CGameInterface, public IUpdateable
  78. {
  79. const CArmedInstance * currentSelection;
  80. public:
  81. bool observerInDuelMode;
  82. ObjectInstanceID destinationTeleport; //contain -1 or object id if teleportation
  83. int3 destinationTeleportPos;
  84. //minor interfaces
  85. CondSh<bool> *showingDialog; //indicates if dialog box is displayed
  86. static boost::recursive_mutex *pim;
  87. bool makingTurn; //if player is already making his turn
  88. int firstCall; // -1 - just loaded game; 1 - just started game; 0 otherwise
  89. int autosaveCount;
  90. static const int SAVES_COUNT = 5;
  91. static int howManyPeople;
  92. CCastleInterface * castleInt; //nullptr if castle window isn't opened
  93. static CBattleInterface * battleInt; //nullptr if no battle
  94. CInGameConsole * cingconsole;
  95. std::shared_ptr<CCallback> cb; //to communicate with engine
  96. const BattleAction *curAction; //during the battle - action currently performed by active stack (or nullptr)
  97. std::list<CInfoWindow *> dialogs; //queue of dialogs awaiting to be shown (not currently shown!)
  98. std::vector<const CGHeroInstance *> wanderingHeroes; //our heroes on the adventure map (not the garrisoned ones)
  99. std::vector<const CGTownInstance *> towns; //our towns on the adventure map
  100. std::map<const CGHeroInstance *, CGPath> paths; //maps hero => selected path in adventure map
  101. std::vector<const CGHeroInstance *> sleepingHeroes; //if hero is in here, he's sleeping
  102. //During battle is quick combat mode is used
  103. std::shared_ptr<CBattleGameInterface> autofightingAI; //AI that makes decisions
  104. bool isAutoFightOn; //Flag, switch it to stop quick combat. Don't touch if there is no battle interface.
  105. const CArmedInstance * getSelection();
  106. void setSelection(const CArmedInstance * obj);
  107. struct SpellbookLastSetting
  108. {
  109. int spellbookLastPageBattle, spellbokLastPageAdvmap; //on which page we left spellbook
  110. int spellbookLastTabBattle, spellbookLastTabAdvmap; //on which page we left spellbook
  111. SpellbookLastSetting();
  112. template <typename Handler> void serialize( Handler &h, const int version )
  113. {
  114. h & spellbookLastPageBattle & spellbokLastPageAdvmap & spellbookLastTabBattle & spellbookLastTabAdvmap;
  115. }
  116. } spellbookSettings;
  117. void update() override;
  118. void initializeHeroTownList();
  119. int getLastIndex(std::string namePrefix);
  120. //overridden funcs from CGameInterface
  121. void buildChanged(const CGTownInstance *town, BuildingID buildingID, int what) override; //what: 1 - built, 2 - demolished
  122. 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
  123. void stackChangedType(const StackLocation &location, const CCreature &newType) override; //used eg. when upgrading creatures
  124. void stacksErased(const StackLocation &location) override; //stack removed from previously filled slot
  125. void stacksSwapped(const StackLocation &loc1, const StackLocation &loc2) override;
  126. void newStackInserted(const StackLocation &location, const CStackInstance &stack) override; //new stack inserted at given (previously empty position)
  127. 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
  128. void artifactPut(const ArtifactLocation &al) override;
  129. void artifactRemoved(const ArtifactLocation &al) override;
  130. void artifactMoved(const ArtifactLocation &src, const ArtifactLocation &dst) override;
  131. void artifactAssembled(const ArtifactLocation &al) override;
  132. void artifactDisassembled(const ArtifactLocation &al) override;
  133. void heroCreated(const CGHeroInstance* hero) override;
  134. void heroGotLevel(const CGHeroInstance *hero, PrimarySkill::PrimarySkill pskill, std::vector<SecondarySkill> &skills, QueryID queryID) override;
  135. void commanderGotLevel (const CCommanderInstance * commander, std::vector<ui32> skills, QueryID queryID) override;
  136. void heroInGarrisonChange(const CGTownInstance *town) override;
  137. void heroMoved(const TryMoveHero & details) override;
  138. void heroPrimarySkillChanged(const CGHeroInstance * hero, int which, si64 val) override;
  139. void heroSecondarySkillChanged(const CGHeroInstance * hero, int which, int val) override;
  140. void heroManaPointsChanged(const CGHeroInstance * hero) override;
  141. void heroMovePointsChanged(const CGHeroInstance * hero) override;
  142. void heroVisitsTown(const CGHeroInstance* hero, const CGTownInstance * town) override;
  143. void receivedResource(int type, int val) override;
  144. void showInfoDialog(const std::string &text, const std::vector<Component*> &components, int soundID) override;
  145. void showRecruitmentDialog(const CGDwelling *dwelling, const CArmedInstance *dst, int level) override;
  146. void showShipyardDialog(const IShipyard *obj) override; //obj may be town or shipyard;
  147. 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.
  148. void showTeleportDialog(TeleportChannelID channel, TTeleportExitsList exits, bool impassable, QueryID askID) override;
  149. void showGarrisonDialog(const CArmedInstance *up, const CGHeroInstance *down, bool removableUnits, QueryID queryID) override;
  150. void showPuzzleMap() override;
  151. void viewWorldMap() override;
  152. void showMarketWindow(const IMarket *market, const CGHeroInstance *visitor) override;
  153. void showUniversityWindow(const IMarket *market, const CGHeroInstance *visitor) override;
  154. void showHillFortWindow(const CGObjectInstance *object, const CGHeroInstance *visitor) override;
  155. void showTavernWindow(const CGObjectInstance *townOrTavern) override;
  156. void showThievesGuildWindow (const CGObjectInstance * obj) override;
  157. void showQuestLog() override;
  158. void advmapSpellCast(const CGHeroInstance * caster, int spellID) override; //called when a hero casts a spell
  159. void tileHidden(const std::unordered_set<int3, ShashInt3> &pos) override; //called when given tiles become hidden under fog of war
  160. void tileRevealed(const std::unordered_set<int3, ShashInt3> &pos) override; //called when fog of war disappears from given tiles
  161. void newObject(const CGObjectInstance * obj) override;
  162. void availableArtifactsChanged(const CGBlackMarket *bm = nullptr) override; //bm may be nullptr, then artifacts are changed in the global pool (used by merchants in towns)
  163. void yourTurn() override;
  164. void availableCreaturesChanged(const CGDwelling *town) override;
  165. void heroBonusChanged(const CGHeroInstance *hero, const Bonus &bonus, bool gain) override;//if gain hero received bonus, else he lost it
  166. void playerBonusChanged(const Bonus &bonus, bool gain) override;
  167. void requestRealized(PackageApplied *pa) override;
  168. void heroExchangeStarted(ObjectInstanceID hero1, ObjectInstanceID hero2, QueryID query) override;
  169. void centerView (int3 pos, int focusTime) override;
  170. void objectPropertyChanged(const SetObjectProperty * sop) override;
  171. void objectRemoved(const CGObjectInstance *obj) override;
  172. void gameOver(PlayerColor player, const EVictoryLossCheckResult & victoryLossCheckResult) override;
  173. void playerStartsTurn(PlayerColor player) override; //called before yourTurn on active itnerface
  174. void showComp(const Component &comp, std::string message) override; //display component in the advmapint infobox
  175. void saveGame(COSer & h, const int version) override; //saving
  176. void loadGame(CISer & h, const int version) override; //loading
  177. void showWorldViewEx(const std::vector<ObjectPosInfo> & objectPositions) override;
  178. //for battles
  179. void actionFinished(const BattleAction& action) override;//occurs AFTER action taken by active stack or by the hero
  180. void actionStarted(const BattleAction& action) override;//occurs BEFORE action taken by active stack or by the hero
  181. BattleAction activeStack(const CStack * stack) override; //called when it's turn of that stack
  182. void battleAttack(const BattleAttack *ba) override; //stack performs attack
  183. void battleEnd(const BattleResult *br) override; //end of battle
  184. void battleNewRoundFirst(int round) override; //called at the beginning of each turn before changes are applied; used for HP regen handling
  185. 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
  186. void battleStackMoved(const CStack * stack, std::vector<BattleHex> dest, int distance) override;
  187. void battleSpellCast(const BattleSpellCast *sc) override;
  188. void battleStacksEffectsSet(const SetStackEffect & sse) override; //called when a specific effect is set to stacks
  189. void battleTriggerEffect(const BattleTriggerEffect & bte) override; //various one-shot effect
  190. void battleStacksAttacked(const std::vector<BattleStackAttacked> & bsa) override;
  191. 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
  192. 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
  193. void battleStacksHealedRes(const std::vector<std::pair<ui32, ui32> > & healedStacks, bool lifeDrain, bool tentHeal, si32 lifeDrainFrom) override; //called when stacks are healed / resurrected
  194. void battleNewStackAppeared(const CStack * stack) override; //not called at the beginning of a battle or by resurrection; called eg. when elemental is summoned
  195. void battleObstaclesRemoved(const std::set<si32> & removedObstacles) override; //called when a certain set of obstacles is removed from batlefield; IDs of them are given
  196. void battleCatapultAttacked(const CatapultAttack & ca) override; //called when catapult makes an attack
  197. void battleStacksRemoved(const BattleStacksRemoved & bsr) override; //called when certain stack is completely removed from battlefield
  198. void battleObstaclePlaced(const CObstacleInstance &obstacle) override;
  199. void yourTacticPhase(int distance) override;
  200. //-------------//
  201. void showArtifactAssemblyDialog(ui32 artifactID, ui32 assembleTo, bool assemble, CFunctionList<bool()> onYes, CFunctionList<bool()> onNo);
  202. void garrisonsChanged(std::vector<const CGObjectInstance *> objs);
  203. void garrisonChanged(const CGObjectInstance * obj);
  204. void heroKilled(const CGHeroInstance* hero);
  205. void waitWhileDialog(bool unlockPim = true);
  206. void waitForAllDialogs(bool unlockPim = true);
  207. bool shiftPressed() const; //determines if shift key is pressed (left or right or both)
  208. bool ctrlPressed() const; //determines if ctrl key is pressed (left or right or both)
  209. bool altPressed() const; //determines if alt key is pressed (left or right or both)
  210. void redrawHeroWin(const CGHeroInstance * hero);
  211. void openTownWindow(const CGTownInstance * town); //shows townscreen
  212. void openHeroWindow(const CGHeroInstance * hero); //shows hero window with given hero
  213. void updateInfo(const CGObjectInstance * specific);
  214. void init(std::shared_ptr<CCallback> CB) override;
  215. int3 repairScreenPos(int3 pos); //returns position closest to pos we can center screen on
  216. // show dialogs
  217. void showInfoDialog(const std::string &text, CComponent * component);
  218. void showInfoDialog(const std::string &text, const std::vector<CComponent*> & components = std::vector<CComponent*>(), int soundID = 0, bool delComps = false);
  219. void showInfoDialogAndWait(std::vector<Component> & components, const MetaString & text);
  220. void showOkDialog(std::vector<Component> & components, const MetaString & text, const std::function<void()> & onOk);
  221. 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
  222. void stopMovement();
  223. void 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. void proposeLoadingGame();
  237. ///returns true if all events are processed internally
  238. bool capturedAllEvents();
  239. CPlayerInterface(PlayerColor Player);//c-tor
  240. ~CPlayerInterface();//d-tor
  241. private:
  242. template <typename Handler> void serializeTempl(Handler &h, const int version);
  243. private:
  244. struct IgnoreEvents
  245. {
  246. CPlayerInterface & owner;
  247. IgnoreEvents(CPlayerInterface & Owner):owner(Owner)
  248. {
  249. owner.ignoreEvents = true;
  250. };
  251. ~IgnoreEvents()
  252. {
  253. owner.ignoreEvents = false;
  254. };
  255. };
  256. bool duringMovement;
  257. bool ignoreEvents;
  258. void doMoveHero(const CGHeroInstance *h, CGPath path);
  259. };
  260. extern CPlayerInterface * LOCPLINT;