CPlayerInterface.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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 "gui/CIntObject.h"
  14. VCMI_LIB_NAMESPACE_BEGIN
  15. class Artifact;
  16. struct TryMoveHero;
  17. class CGHeroInstance;
  18. class CStack;
  19. class CCreature;
  20. struct CGPath;
  21. class CCreatureSet;
  22. class CGObjectInstance;
  23. struct UpgradeInfo;
  24. template <typename T> struct CondSh;
  25. struct CPathsInfo;
  26. VCMI_LIB_NAMESPACE_END
  27. class CButton;
  28. class CAdventureMapInterface;
  29. class CCastleInterface;
  30. class BattleInterface;
  31. class CComponent;
  32. class CSelectableComponent;
  33. class CSlider;
  34. class CInGameConsole;
  35. class CInfoWindow;
  36. class IShowActivatable;
  37. class ClickableL;
  38. class ClickableR;
  39. class Hoverable;
  40. class KeyInterested;
  41. class MotionInterested;
  42. class PlayerLocalState;
  43. class TimeInterested;
  44. class IShowable;
  45. namespace boost
  46. {
  47. class mutex;
  48. class recursive_mutex;
  49. }
  50. /// Central class for managing user interface logic
  51. class CPlayerInterface : public CGameInterface, public IUpdateable
  52. {
  53. bool duringMovement;
  54. bool ignoreEvents;
  55. size_t numOfMovedArts;
  56. // -1 - just loaded game; 1 - just started game; 0 otherwise
  57. int firstCall;
  58. int autosaveCount;
  59. static const int SAVES_COUNT = 5;
  60. std::pair<const CCreatureSet *, const CCreatureSet *> lastBattleArmies;
  61. bool allowBattleReplay = false;
  62. std::list<std::shared_ptr<CInfoWindow>> dialogs; //queue of dialogs awaiting to be shown (not currently shown!)
  63. const BattleAction *curAction; //during the battle - action currently performed by active stack (or nullptr)
  64. ObjectInstanceID destinationTeleport; //contain -1 or object id if teleportation
  65. int3 destinationTeleportPos;
  66. public: // TODO: make private
  67. std::shared_ptr<Environment> env;
  68. std::unique_ptr<PlayerLocalState> localState;
  69. //minor interfaces
  70. CondSh<bool> *showingDialog; //indicates if dialog box is displayed
  71. static boost::recursive_mutex *pim;
  72. bool makingTurn; //if player is already making his turn
  73. CCastleInterface * castleInt; //nullptr if castle window isn't opened
  74. static std::shared_ptr<BattleInterface> battleInt; //nullptr if no battle
  75. CInGameConsole * cingconsole;
  76. std::shared_ptr<CCallback> cb; //to communicate with engine
  77. //During battle is quick combat mode is used
  78. std::shared_ptr<CBattleGameInterface> autofightingAI; //AI that makes decisions
  79. bool isAutoFightOn; //Flag, switch it to stop quick combat. Don't touch if there is no battle interface.
  80. protected: // Call-ins from server, should not be called directly, but only via GameInterface
  81. void update() override;
  82. void initGameInterface(std::shared_ptr<Environment> ENV, std::shared_ptr<CCallback> CB) override;
  83. void garrisonsChanged(ObjectInstanceID id1, ObjectInstanceID id2) override;
  84. void buildChanged(const CGTownInstance *town, BuildingID buildingID, int what) override; //what: 1 - built, 2 - demolished
  85. void artifactPut(const ArtifactLocation &al) override;
  86. void artifactRemoved(const ArtifactLocation &al) override;
  87. void artifactMoved(const ArtifactLocation &src, const ArtifactLocation &dst) override;
  88. void bulkArtMovementStart(size_t numOfArts) override;
  89. void artifactAssembled(const ArtifactLocation &al) override;
  90. void askToAssembleArtifact(const ArtifactLocation & dst) override;
  91. void artifactDisassembled(const ArtifactLocation &al) override;
  92. void heroVisit(const CGHeroInstance * visitor, const CGObjectInstance * visitedObj, bool start) override;
  93. void heroCreated(const CGHeroInstance* hero) override;
  94. void heroGotLevel(const CGHeroInstance *hero, PrimarySkill::PrimarySkill pskill, std::vector<SecondarySkill> &skills, QueryID queryID) override;
  95. void commanderGotLevel (const CCommanderInstance * commander, std::vector<ui32> skills, QueryID queryID) override;
  96. void heroInGarrisonChange(const CGTownInstance *town) override;
  97. void heroMoved(const TryMoveHero & details, bool verbose = true) override;
  98. void heroPrimarySkillChanged(const CGHeroInstance * hero, int which, si64 val) override;
  99. void heroSecondarySkillChanged(const CGHeroInstance * hero, int which, int val) override;
  100. void heroManaPointsChanged(const CGHeroInstance * hero) override;
  101. void heroMovePointsChanged(const CGHeroInstance * hero) override;
  102. void heroVisitsTown(const CGHeroInstance* hero, const CGTownInstance * town) override;
  103. void receivedResource() override;
  104. void showInfoDialog(EInfoWindowMode type, const std::string & text, const std::vector<Component> & components, int soundID) override;
  105. void showRecruitmentDialog(const CGDwelling *dwelling, const CArmedInstance *dst, int level) override;
  106. 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.
  107. void showTeleportDialog(TeleportChannelID channel, TTeleportExitsList exits, bool impassable, QueryID askID) override;
  108. void showGarrisonDialog(const CArmedInstance *up, const CGHeroInstance *down, bool removableUnits, QueryID queryID) override;
  109. void showMapObjectSelectDialog(QueryID askID, const Component & icon, const MetaString & title, const MetaString & description, const std::vector<ObjectInstanceID> & objects) override;
  110. void showMarketWindow(const IMarket *market, const CGHeroInstance *visitor) override;
  111. void showUniversityWindow(const IMarket *market, const CGHeroInstance *visitor) override;
  112. void showHillFortWindow(const CGObjectInstance *object, const CGHeroInstance *visitor) override;
  113. void advmapSpellCast(const CGHeroInstance * caster, int spellID) override; //called when a hero casts a spell
  114. void tileHidden(const std::unordered_set<int3> &pos) override; //called when given tiles become hidden under fog of war
  115. void tileRevealed(const std::unordered_set<int3> &pos) override; //called when fog of war disappears from given tiles
  116. void newObject(const CGObjectInstance * obj) override;
  117. void availableArtifactsChanged(const CGBlackMarket *bm = nullptr) override; //bm may be nullptr, then artifacts are changed in the global pool (used by merchants in towns)
  118. void yourTurn() override;
  119. void availableCreaturesChanged(const CGDwelling *town) override;
  120. void heroBonusChanged(const CGHeroInstance *hero, const Bonus &bonus, bool gain) override;//if gain hero received bonus, else he lost it
  121. void playerBonusChanged(const Bonus &bonus, bool gain) override;
  122. void requestRealized(PackageApplied *pa) override;
  123. void heroExchangeStarted(ObjectInstanceID hero1, ObjectInstanceID hero2, QueryID query) override;
  124. void centerView (int3 pos, int focusTime) override;
  125. void objectPropertyChanged(const SetObjectProperty * sop) override;
  126. void objectRemoved(const CGObjectInstance *obj) override;
  127. void objectRemovedAfter() override;
  128. void playerBlocked(int reason, bool start) override;
  129. void gameOver(PlayerColor player, const EVictoryLossCheckResult & victoryLossCheckResult) override;
  130. void playerStartsTurn(PlayerColor player) override; //called before yourTurn on active itnerface
  131. void saveGame(BinarySerializer & h, const int version) override; //saving
  132. void loadGame(BinaryDeserializer & h, const int version) override; //loading
  133. void showWorldViewEx(const std::vector<ObjectPosInfo> & objectPositions, bool showTerrain) override;
  134. //for battles
  135. void actionFinished(const BattleAction& action) override;//occurs AFTER action taken by active stack or by the hero
  136. void actionStarted(const BattleAction& action) override;//occurs BEFORE action taken by active stack or by the hero
  137. BattleAction activeStack(const CStack * stack) override; //called when it's turn of that stack
  138. void battleAttack(const BattleAttack *ba) override; //stack performs attack
  139. void battleEnd(const BattleResult *br, QueryID queryID) override; //end of battle
  140. void battleNewRoundFirst(int round) override; //called at the beginning of each turn before changes are applied; used for HP regen handling
  141. 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
  142. void battleLogMessage(const std::vector<MetaString> & lines) override;
  143. void battleStackMoved(const CStack * stack, std::vector<BattleHex> dest, int distance, bool teleport) override;
  144. void battleSpellCast(const BattleSpellCast *sc) override;
  145. void battleStacksEffectsSet(const SetStackEffect & sse) override; //called when a specific effect is set to stacks
  146. void battleTriggerEffect(const BattleTriggerEffect & bte) override; //various one-shot effect
  147. void battleStacksAttacked(const std::vector<BattleStackAttacked> & bsa, bool ranged) override;
  148. 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
  149. 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
  150. void battleUnitsChanged(const std::vector<UnitChanges> & units) override;
  151. void battleObstaclesChanged(const std::vector<ObstacleChanges> & obstacles) override;
  152. void battleCatapultAttacked(const CatapultAttack & ca) override; //called when catapult makes an attack
  153. void battleGateStateChanged(const EGateState state) override;
  154. void yourTacticPhase(int distance) override;
  155. void forceEndTacticPhase() override;
  156. public: // public interface for use by client via LOCPLINT access
  157. // part of GameInterface that is also used by client code
  158. void showPuzzleMap() override;
  159. void viewWorldMap() override;
  160. void showQuestLog() override;
  161. void showThievesGuildWindow (const CGObjectInstance * obj) override;
  162. void showTavernWindow(const CGObjectInstance *townOrTavern) override;
  163. void showShipyardDialog(const IShipyard *obj) override; //obj may be town or shipyard;
  164. void showHeroExchange(ObjectInstanceID hero1, ObjectInstanceID hero2);
  165. void showArtifactAssemblyDialog(const Artifact * artifact, const Artifact * assembledArtifact, CFunctionList<bool()> onYes);
  166. void waitWhileDialog(bool unlockPim = true);
  167. void waitForAllDialogs(bool unlockPim = true);
  168. void openTownWindow(const CGTownInstance * town); //shows townscreen
  169. void openHeroWindow(const CGHeroInstance * hero); //shows hero window with given hero
  170. void showInfoDialog(const std::string &text, std::shared_ptr<CComponent> component);
  171. void showInfoDialog(const std::string &text, const std::vector<std::shared_ptr<CComponent>> & components = std::vector<std::shared_ptr<CComponent>>(), int soundID = 0);
  172. void showInfoDialogAndWait(std::vector<Component> & components, const MetaString & text);
  173. 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>>());
  174. void stopMovement();
  175. void moveHero(const CGHeroInstance *h, const CGPath& path);
  176. void tryDiggging(const CGHeroInstance *h);
  177. void showShipyardDialogOrProblemPopup(const IShipyard *obj); //obj may be town or shipyard;
  178. void proposeLoadingGame();
  179. ///returns true if all events are processed internally
  180. bool capturedAllEvents();
  181. CPlayerInterface(PlayerColor Player);
  182. ~CPlayerInterface();
  183. private:
  184. struct IgnoreEvents
  185. {
  186. CPlayerInterface & owner;
  187. IgnoreEvents(CPlayerInterface & Owner):owner(Owner)
  188. {
  189. owner.ignoreEvents = true;
  190. };
  191. ~IgnoreEvents()
  192. {
  193. owner.ignoreEvents = false;
  194. };
  195. };
  196. void heroKilled(const CGHeroInstance* hero);
  197. void garrisonsChanged(std::vector<const CGObjectInstance *> objs);
  198. void requestReturningToMainMenu(bool won);
  199. void acceptTurn(); //used during hot seat after your turn message is close
  200. void initializeHeroTownList();
  201. int getLastIndex(std::string namePrefix);
  202. void doMoveHero(const CGHeroInstance *h, CGPath path);
  203. void setMovementStatus(bool value);
  204. /// Performs autosave, if needed according to settings
  205. void performAutosave();
  206. };
  207. /// Provides global access to instance of interface of currently active player
  208. extern CPlayerInterface * LOCPLINT;