CPlayerInterface.h 13 KB

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