CPlayerInterface.h 13 KB

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