CPlayerInterface.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. /*
  2. * CPlayerInterface.h, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #pragma once
  11. #include "../lib/FunctionList.h"
  12. #include "../lib/CGameInterface.h"
  13. #include "../lib/NetPacksBase.h"
  14. #include "gui/CIntObject.h"
  15. #ifdef __GNUC__
  16. #define sprintf_s snprintf
  17. #endif
  18. #ifdef max
  19. #undef max
  20. #endif
  21. #ifdef min
  22. #undef min
  23. #endif
  24. class CButton;
  25. class CToggleGroup;
  26. struct TryMoveHero;
  27. class CGHeroInstance;
  28. class CAdvMapInt;
  29. class CCastleInterface;
  30. class CBattleInterface;
  31. class CStack;
  32. class CComponent;
  33. class CCreature;
  34. struct SDL_Surface;
  35. struct CGPath;
  36. class CCreatureAnimation;
  37. class CSelectableComponent;
  38. class CCreatureSet;
  39. class CGObjectInstance;
  40. class CSlider;
  41. struct UpgradeInfo;
  42. template <typename T> struct CondSh;
  43. class CInGameConsole;
  44. class CGarrisonInt;
  45. class CInGameConsole;
  46. union SDL_Event;
  47. class CInfoWindow;
  48. class IShowActivatable;
  49. class ClickableL;
  50. class ClickableR;
  51. class Hoverable;
  52. class KeyInterested;
  53. class MotionInterested;
  54. class TimeInterested;
  55. class IShowable;
  56. struct CPathsInfo;
  57. namespace boost
  58. {
  59. class mutex;
  60. class recursive_mutex;
  61. }
  62. enum
  63. {
  64. /*CHANGE_SCREEN_RESOLUTION = 1,*/
  65. RETURN_TO_MAIN_MENU = 2,
  66. //STOP_CLIENT = 3,
  67. RESTART_GAME = 4,
  68. RETURN_TO_MENU_LOAD,
  69. FULLSCREEN_TOGGLED,
  70. PREPARE_RESTART_CAMPAIGN,
  71. FORCE_QUIT //quit client without question
  72. };
  73. /// Central class for managing user interface logic
  74. class CPlayerInterface : public CGameInterface, public IUpdateable
  75. {
  76. const CArmedInstance * currentSelection;
  77. public:
  78. ObjectInstanceID destinationTeleport; //contain -1 or object id if teleportation
  79. int3 destinationTeleportPos;
  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. std::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. std::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. const CArmedInstance * getSelection();
  102. void setSelection(const CArmedInstance * obj);
  103. struct SpellbookLastSetting
  104. {
  105. int spellbookLastPageBattle, spellbokLastPageAdvmap; //on which page we left spellbook
  106. int spellbookLastTabBattle, spellbookLastTabAdvmap; //on which page we left spellbook
  107. SpellbookLastSetting();
  108. template <typename Handler> void serialize( Handler &h, const int version )
  109. {
  110. h & spellbookLastPageBattle;
  111. h & spellbokLastPageAdvmap;
  112. h & spellbookLastTabBattle;
  113. h & spellbookLastTabAdvmap;
  114. }
  115. } spellbookSettings;
  116. void update() override;
  117. void initializeHeroTownList();
  118. int getLastIndex(std::string namePrefix);
  119. //overridden funcs from CGameInterface
  120. void buildChanged(const CGTownInstance *town, BuildingID buildingID, int what) override; //what: 1 - built, 2 - demolished
  121. 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
  122. void stackChangedType(const StackLocation &location, const CCreature &newType) override; //used eg. when upgrading creatures
  123. void stacksErased(const StackLocation &location) override; //stack removed from previously filled slot
  124. void stacksSwapped(const StackLocation &loc1, const StackLocation &loc2) override;
  125. void newStackInserted(const StackLocation &location, const CStackInstance &stack) override; //new stack inserted at given (previously empty position)
  126. 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
  127. void artifactPut(const ArtifactLocation &al) override;
  128. void artifactRemoved(const ArtifactLocation &al) override;
  129. void artifactMoved(const ArtifactLocation &src, const ArtifactLocation &dst) override;
  130. void artifactAssembled(const ArtifactLocation &al) override;
  131. void artifactDisassembled(const ArtifactLocation &al) override;
  132. void heroCreated(const CGHeroInstance* hero) override;
  133. void heroGotLevel(const CGHeroInstance *hero, PrimarySkill::PrimarySkill pskill, std::vector<SecondarySkill> &skills, QueryID queryID) override;
  134. void commanderGotLevel (const CCommanderInstance * commander, std::vector<ui32> skills, QueryID queryID) override;
  135. void heroInGarrisonChange(const CGTownInstance *town) override;
  136. void heroMoved(const TryMoveHero & details) override;
  137. void heroPrimarySkillChanged(const CGHeroInstance * hero, int which, si64 val) override;
  138. void heroSecondarySkillChanged(const CGHeroInstance * hero, int which, int val) override;
  139. void heroManaPointsChanged(const CGHeroInstance * hero) override;
  140. void heroMovePointsChanged(const CGHeroInstance * hero) override;
  141. void heroVisitsTown(const CGHeroInstance* hero, const CGTownInstance * town) override;
  142. void receivedResource() override;
  143. void showInfoDialog(const std::string &text, const std::vector<Component*> &components, int soundID) override;
  144. void showRecruitmentDialog(const CGDwelling *dwelling, const CArmedInstance *dst, int level) override;
  145. void showShipyardDialog(const IShipyard *obj) override; //obj may be town or shipyard;
  146. void showBlockingDialog(const std::string &text, const std::vector<Component> &components, QueryID askID, const 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.
  147. void showTeleportDialog(TeleportChannelID channel, TTeleportExitsList exits, bool impassable, QueryID askID) override;
  148. void showGarrisonDialog(const CArmedInstance *up, const CGHeroInstance *down, bool removableUnits, QueryID queryID) override;
  149. void showMapObjectSelectDialog(QueryID askID, const Component & icon, const MetaString & title, const MetaString & description, const std::vector<ObjectInstanceID> & objects) 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(BinarySerializer & h, const int version) override; //saving
  176. void loadGame(BinaryDeserializer & 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 battleGateStateChanged(const EGateState state) override;
  200. void yourTacticPhase(int distance) override;
  201. //-------------//
  202. void showArtifactAssemblyDialog(ui32 artifactID, ui32 assembleTo, bool assemble, CFunctionList<bool()> onYes, CFunctionList<bool()> onNo);
  203. void garrisonsChanged(std::vector<const CGObjectInstance *> objs);
  204. void garrisonChanged(const CGObjectInstance * obj);
  205. void heroKilled(const CGHeroInstance* hero);
  206. void waitWhileDialog(bool unlockPim = true);
  207. void waitForAllDialogs(bool unlockPim = true);
  208. bool shiftPressed() const; //determines if shift key is pressed (left or right or both)
  209. bool ctrlPressed() const; //determines if ctrl key is pressed (left or right or both)
  210. bool altPressed() const; //determines if alt key is pressed (left or right or both)
  211. void redrawHeroWin(const CGHeroInstance * hero);
  212. void openTownWindow(const CGTownInstance * town); //shows townscreen
  213. void openHeroWindow(const CGHeroInstance * hero); //shows hero window with given hero
  214. void updateInfo(const CGObjectInstance * specific);
  215. void init(std::shared_ptr<CCallback> CB) override;
  216. int3 repairScreenPos(int3 pos); //returns position closest to pos we can center screen on
  217. void activateForSpectator(); // TODO: spectator probably need own player interface class
  218. // show dialogs
  219. void showInfoDialog(const std::string &text, CComponent * component);
  220. void showInfoDialog(const std::string &text, const std::vector<CComponent*> & components = std::vector<CComponent*>(), int soundID = 0, bool delComps = false);
  221. void showInfoDialogAndWait(std::vector<Component> & components, const MetaString & text);
  222. void showOkDialog(std::vector<Component> & components, const MetaString & text, const std::function<void()> & onOk);
  223. 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
  224. void stopMovement();
  225. void moveHero(const CGHeroInstance *h, CGPath path);
  226. void initMovement(const TryMoveHero &details, const CGHeroInstance * ho, const int3 &hp );//initializing objects and performing first step of move
  227. void movementPxStep( const TryMoveHero &details, int i, const int3 &hp, const CGHeroInstance * ho );//performing step of movement
  228. void finishMovement( const TryMoveHero &details, const int3 &hp, const CGHeroInstance * ho ); //finish movement
  229. void eraseCurrentPathOf( const CGHeroInstance * ho, bool checkForExistanceOfPath = true );
  230. void removeLastNodeFromPath(const CGHeroInstance *ho);
  231. CGPath *getAndVerifyPath( const CGHeroInstance * h );
  232. void acceptTurn(); //used during hot seat after your turn message is close
  233. void tryDiggging(const CGHeroInstance *h);
  234. void showShipyardDialogOrProblemPopup(const IShipyard *obj); //obj may be town or shipyard;
  235. void requestReturningToMainMenu();
  236. void sendCustomEvent(int code);
  237. void proposeLoadingGame();
  238. ///returns true if all events are processed internally
  239. bool capturedAllEvents();
  240. CPlayerInterface(PlayerColor Player);
  241. ~CPlayerInterface();
  242. private:
  243. template <typename Handler> void serializeTempl(Handler &h, const int version);
  244. private:
  245. struct IgnoreEvents
  246. {
  247. CPlayerInterface & owner;
  248. IgnoreEvents(CPlayerInterface & Owner):owner(Owner)
  249. {
  250. owner.ignoreEvents = true;
  251. };
  252. ~IgnoreEvents()
  253. {
  254. owner.ignoreEvents = false;
  255. };
  256. };
  257. bool duringMovement;
  258. bool ignoreEvents;
  259. void doMoveHero(const CGHeroInstance *h, CGPath path);
  260. void setMovementStatus(bool value);
  261. void askToAssembleArtifact(const ArtifactLocation &al);
  262. };
  263. extern CPlayerInterface * LOCPLINT;