CPlayerInterface.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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. class Artifact;
  19. class CButton;
  20. class CToggleGroup;
  21. struct TryMoveHero;
  22. class CGHeroInstance;
  23. class CAdvMapInt;
  24. class CCastleInterface;
  25. class CBattleInterface;
  26. class CStack;
  27. class CComponent;
  28. class CCreature;
  29. struct SDL_Surface;
  30. struct CGPath;
  31. class CCreatureAnimation;
  32. class CSelectableComponent;
  33. class CCreatureSet;
  34. class CGObjectInstance;
  35. class CSlider;
  36. struct UpgradeInfo;
  37. template <typename T> struct CondSh;
  38. class CInGameConsole;
  39. class CInGameConsole;
  40. union SDL_Event;
  41. class CInfoWindow;
  42. class IShowActivatable;
  43. class ClickableL;
  44. class ClickableR;
  45. class Hoverable;
  46. class KeyInterested;
  47. class MotionInterested;
  48. class TimeInterested;
  49. class IShowable;
  50. struct CPathsInfo;
  51. namespace boost
  52. {
  53. class mutex;
  54. class recursive_mutex;
  55. }
  56. /// Central class for managing user interface logic
  57. class CPlayerInterface : public CGameInterface, public IUpdateable
  58. {
  59. const CArmedInstance * currentSelection;
  60. public:
  61. std::shared_ptr<Environment> env;
  62. ObjectInstanceID destinationTeleport; //contain -1 or object id if teleportation
  63. int3 destinationTeleportPos;
  64. //minor interfaces
  65. CondSh<bool> *showingDialog; //indicates if dialog box is displayed
  66. static boost::recursive_mutex *pim;
  67. bool makingTurn; //if player is already making his turn
  68. int firstCall; // -1 - just loaded game; 1 - just started game; 0 otherwise
  69. int autosaveCount;
  70. static const int SAVES_COUNT = 5;
  71. CCastleInterface * castleInt; //nullptr if castle window isn't opened
  72. static CBattleInterface * battleInt; //nullptr if no battle
  73. CInGameConsole * cingconsole;
  74. std::shared_ptr<CCallback> cb; //to communicate with engine
  75. const BattleAction *curAction; //during the battle - action currently performed by active stack (or nullptr)
  76. std::list<std::shared_ptr<CInfoWindow>> dialogs; //queue of dialogs awaiting to be shown (not currently shown!)
  77. std::vector<const CGHeroInstance *> wanderingHeroes; //our heroes on the adventure map (not the garrisoned ones)
  78. std::vector<const CGTownInstance *> towns; //our towns on the adventure map
  79. std::map<const CGHeroInstance *, CGPath> paths; //maps hero => selected path in adventure map
  80. std::vector<const CGHeroInstance *> sleepingHeroes; //if hero is in here, he's sleeping
  81. //During battle is quick combat mode is used
  82. std::shared_ptr<CBattleGameInterface> autofightingAI; //AI that makes decisions
  83. bool isAutoFightOn; //Flag, switch it to stop quick combat. Don't touch if there is no battle interface.
  84. const CArmedInstance * getSelection();
  85. void setSelection(const CArmedInstance * obj);
  86. struct SpellbookLastSetting
  87. {
  88. int spellbookLastPageBattle, spellbokLastPageAdvmap; //on which page we left spellbook
  89. int spellbookLastTabBattle, spellbookLastTabAdvmap; //on which page we left spellbook
  90. SpellbookLastSetting();
  91. template <typename Handler> void serialize( Handler &h, const int version )
  92. {
  93. h & spellbookLastPageBattle;
  94. h & spellbokLastPageAdvmap;
  95. h & spellbookLastTabBattle;
  96. h & spellbookLastTabAdvmap;
  97. }
  98. } spellbookSettings;
  99. void update() override;
  100. void initializeHeroTownList();
  101. int getLastIndex(std::string namePrefix);
  102. //overridden funcs from CGameInterface
  103. void garrisonsChanged(ObjectInstanceID id1, ObjectInstanceID id2) override;
  104. void buildChanged(const CGTownInstance *town, BuildingID buildingID, int what) override; //what: 1 - built, 2 - demolished
  105. void artifactPut(const ArtifactLocation &al) override;
  106. void artifactRemoved(const ArtifactLocation &al) override;
  107. void artifactMoved(const ArtifactLocation &src, const ArtifactLocation &dst) override;
  108. void artifactAssembled(const ArtifactLocation &al) override;
  109. void artifactDisassembled(const ArtifactLocation &al) override;
  110. void heroVisit(const CGHeroInstance * visitor, const CGObjectInstance * visitedObj, bool start) override;
  111. void heroCreated(const CGHeroInstance* hero) override;
  112. void heroGotLevel(const CGHeroInstance *hero, PrimarySkill::PrimarySkill pskill, std::vector<SecondarySkill> &skills, QueryID queryID) override;
  113. void commanderGotLevel (const CCommanderInstance * commander, std::vector<ui32> skills, QueryID queryID) override;
  114. void heroInGarrisonChange(const CGTownInstance *town) override;
  115. void heroMoved(const TryMoveHero & details) override;
  116. void heroPrimarySkillChanged(const CGHeroInstance * hero, int which, si64 val) override;
  117. void heroSecondarySkillChanged(const CGHeroInstance * hero, int which, int val) override;
  118. void heroManaPointsChanged(const CGHeroInstance * hero) override;
  119. void heroMovePointsChanged(const CGHeroInstance * hero) override;
  120. void heroVisitsTown(const CGHeroInstance* hero, const CGTownInstance * town) override;
  121. void receivedResource() override;
  122. void showInfoDialog(const std::string & text, const std::vector<Component> & components, int soundID) override;
  123. void showRecruitmentDialog(const CGDwelling *dwelling, const CArmedInstance *dst, int level) override;
  124. void showShipyardDialog(const IShipyard *obj) override; //obj may be town or shipyard;
  125. 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.
  126. void showTeleportDialog(TeleportChannelID channel, TTeleportExitsList exits, bool impassable, QueryID askID) override;
  127. void showGarrisonDialog(const CArmedInstance *up, const CGHeroInstance *down, bool removableUnits, QueryID queryID) override;
  128. void showMapObjectSelectDialog(QueryID askID, const Component & icon, const MetaString & title, const MetaString & description, const std::vector<ObjectInstanceID> & objects) override;
  129. void showPuzzleMap() override;
  130. void viewWorldMap() override;
  131. void showMarketWindow(const IMarket *market, const CGHeroInstance *visitor) override;
  132. void showUniversityWindow(const IMarket *market, const CGHeroInstance *visitor) override;
  133. void showHillFortWindow(const CGObjectInstance *object, const CGHeroInstance *visitor) override;
  134. void showTavernWindow(const CGObjectInstance *townOrTavern) override;
  135. void showThievesGuildWindow (const CGObjectInstance * obj) override;
  136. void showQuestLog() override;
  137. void advmapSpellCast(const CGHeroInstance * caster, int spellID) override; //called when a hero casts a spell
  138. void tileHidden(const std::unordered_set<int3, ShashInt3> &pos) override; //called when given tiles become hidden under fog of war
  139. void tileRevealed(const std::unordered_set<int3, ShashInt3> &pos) override; //called when fog of war disappears from given tiles
  140. void newObject(const CGObjectInstance * obj) override;
  141. void availableArtifactsChanged(const CGBlackMarket *bm = nullptr) override; //bm may be nullptr, then artifacts are changed in the global pool (used by merchants in towns)
  142. void yourTurn() override;
  143. void availableCreaturesChanged(const CGDwelling *town) override;
  144. void heroBonusChanged(const CGHeroInstance *hero, const Bonus &bonus, bool gain) override;//if gain hero received bonus, else he lost it
  145. void playerBonusChanged(const Bonus &bonus, bool gain) override;
  146. void requestRealized(PackageApplied *pa) override;
  147. void heroExchangeStarted(ObjectInstanceID hero1, ObjectInstanceID hero2, QueryID query) override;
  148. void centerView (int3 pos, int focusTime) override;
  149. void objectPropertyChanged(const SetObjectProperty * sop) override;
  150. void objectRemoved(const CGObjectInstance *obj) override;
  151. void playerBlocked(int reason, bool start) override;
  152. void gameOver(PlayerColor player, const EVictoryLossCheckResult & victoryLossCheckResult) override;
  153. void playerStartsTurn(PlayerColor player) override; //called before yourTurn on active itnerface
  154. void showComp(const Component &comp, std::string message) override; //display component in the advmapint infobox
  155. void saveGame(BinarySerializer & h, const int version) override; //saving
  156. void loadGame(BinaryDeserializer & h, const int version) override; //loading
  157. void showWorldViewEx(const std::vector<ObjectPosInfo> & objectPositions) override;
  158. //for battles
  159. void actionFinished(const BattleAction& action) override;//occurs AFTER action taken by active stack or by the hero
  160. void actionStarted(const BattleAction& action) override;//occurs BEFORE action taken by active stack or by the hero
  161. BattleAction activeStack(const CStack * stack) override; //called when it's turn of that stack
  162. void battleAttack(const BattleAttack *ba) override; //stack performs attack
  163. void battleEnd(const BattleResult *br) override; //end of battle
  164. void battleNewRoundFirst(int round) override; //called at the beginning of each turn before changes are applied; used for HP regen handling
  165. 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
  166. void battleLogMessage(const std::vector<MetaString> & lines) override;
  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 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
  173. 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
  174. void battleUnitsChanged(const std::vector<UnitChanges> & units, const std::vector<CustomEffectInfo> & customEffects) override;
  175. void battleObstaclesChanged(const std::vector<ObstacleChanges> & obstacles) override;
  176. void battleCatapultAttacked(const CatapultAttack & ca) override; //called when catapult makes an attack
  177. void battleGateStateChanged(const EGateState state) override;
  178. void yourTacticPhase(int distance) override;
  179. //-------------//
  180. void showArtifactAssemblyDialog(const Artifact * artifact, const Artifact * assembledArtifact, CFunctionList<bool()> onYes);
  181. void garrisonsChanged(std::vector<const CGObjectInstance *> objs);
  182. void garrisonChanged(const CGObjectInstance * obj);
  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. void updateInfo(const CGObjectInstance * specific);
  193. void init(std::shared_ptr<Environment> ENV, std::shared_ptr<CCallback> CB) override;
  194. int3 repairScreenPos(int3 pos); //returns position closest to pos we can center screen on
  195. void activateForSpectator(); // TODO: spectator probably need own player interface class
  196. // show dialogs
  197. void showInfoDialog(const std::string &text, std::shared_ptr<CComponent> component);
  198. void showInfoDialog(const std::string &text, const std::vector<std::shared_ptr<CComponent>> & components = std::vector<std::shared_ptr<CComponent>>(), int soundID = 0);
  199. void showInfoDialogAndWait(std::vector<Component> & components, const MetaString & text);
  200. void showYesNoDialog(const std::string &text, CFunctionList<void()> onYes, CFunctionList<void()> onNo, const std::vector<std::shared_ptr<CComponent>> & components = std::vector<std::shared_ptr<CComponent>>());
  201. void stopMovement();
  202. void moveHero(const CGHeroInstance *h, CGPath path);
  203. void initMovement(const TryMoveHero &details, const CGHeroInstance * ho, const int3 &hp );//initializing objects and performing first step of move
  204. void movementPxStep( const TryMoveHero &details, int i, const int3 &hp, const CGHeroInstance * ho );//performing step of movement
  205. void finishMovement( const TryMoveHero &details, const int3 &hp, const CGHeroInstance * ho ); //finish movement
  206. void eraseCurrentPathOf( const CGHeroInstance * ho, bool checkForExistanceOfPath = true );
  207. void removeLastNodeFromPath(const CGHeroInstance *ho);
  208. CGPath *getAndVerifyPath( const CGHeroInstance * h );
  209. void acceptTurn(); //used during hot seat after your turn message is close
  210. void tryDiggging(const CGHeroInstance *h);
  211. void showShipyardDialogOrProblemPopup(const IShipyard *obj); //obj may be town or shipyard;
  212. void requestReturningToMainMenu(bool won);
  213. void sendCustomEvent(int code);
  214. void proposeLoadingGame();
  215. // Ambient sounds
  216. void updateAmbientSounds(bool resetAll = false);
  217. ///returns true if all events are processed internally
  218. bool capturedAllEvents();
  219. CPlayerInterface(PlayerColor Player);
  220. ~CPlayerInterface();
  221. private:
  222. template <typename Handler> void serializeTempl(Handler &h, const int version);
  223. private:
  224. struct IgnoreEvents
  225. {
  226. CPlayerInterface & owner;
  227. IgnoreEvents(CPlayerInterface & Owner):owner(Owner)
  228. {
  229. owner.ignoreEvents = true;
  230. };
  231. ~IgnoreEvents()
  232. {
  233. owner.ignoreEvents = false;
  234. };
  235. };
  236. bool duringMovement;
  237. bool ignoreEvents;
  238. void doMoveHero(const CGHeroInstance *h, CGPath path);
  239. void setMovementStatus(bool value);
  240. void askToAssembleArtifact(const ArtifactLocation &al);
  241. };
  242. extern CPlayerInterface * LOCPLINT;