CPlayerInterface.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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 "ArtifactsUIController.h"
  12. #include "../lib/callback/CGameInterface.h"
  13. #include "../lib/FunctionList.h"
  14. #include "gui/CIntObject.h"
  15. VCMI_LIB_NAMESPACE_BEGIN
  16. class Artifact;
  17. struct TryMoveHero;
  18. class CStack;
  19. class CCreature;
  20. struct CGPath;
  21. class CCreatureSet;
  22. class CGObjectInstance;
  23. class UpgradeInfo;
  24. class ConditionalWait;
  25. struct CPathsInfo;
  26. class PathfinderCache;
  27. VCMI_LIB_NAMESPACE_END
  28. class CButton;
  29. class AdventureMapInterface;
  30. class CCastleInterface;
  31. class BattleInterface;
  32. class CComponent;
  33. class CSelectableComponent;
  34. class CSlider;
  35. class CInGameConsole;
  36. class CInfoWindow;
  37. class IShowActivatable;
  38. class ClickableL;
  39. class ClickableR;
  40. class Hoverable;
  41. class KeyInterested;
  42. class MotionInterested;
  43. class PlayerLocalState;
  44. class TimeInterested;
  45. class HeroMovementController;
  46. namespace boost
  47. {
  48. class mutex;
  49. class recursive_mutex;
  50. }
  51. /// Central class for managing user interface logic
  52. class CPlayerInterface : public CGameInterface
  53. {
  54. bool ignoreEvents;
  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. std::unique_ptr<PathfinderCache> pathfinderCache;
  59. public: // TODO: make private
  60. std::unique_ptr<ArtifactsUIController> artifactController;
  61. std::shared_ptr<Environment> env;
  62. std::unique_ptr<PlayerLocalState> localState;
  63. //minor interfaces
  64. ConditionalWait * showingDialog; //indicates if dialog box is displayed
  65. bool makingTurn; //if player is already making his turn
  66. CCastleInterface * castleInt; //nullptr if castle window isn't opened
  67. static std::shared_ptr<BattleInterface> battleInt; //nullptr if no battle
  68. CInGameConsole * cingconsole;
  69. std::shared_ptr<CCallback> cb; //to communicate with engine
  70. //During battle is quick combat mode is used
  71. std::shared_ptr<CBattleGameInterface> autofightingAI; //AI that makes decisions
  72. bool isAutoFightOn; //Flag, switch it to stop quick combat. Don't touch if there is no battle interface.
  73. bool isAutoFightEndBattle; //Flag, if battle forced to end with autocombat
  74. protected: // Call-ins from server, should not be called directly, but only via GameInterface
  75. void initGameInterface(std::shared_ptr<Environment> ENV, std::shared_ptr<CCallback> CB) override;
  76. void garrisonsChanged(ObjectInstanceID id1, ObjectInstanceID id2) override;
  77. void buildChanged(const CGTownInstance *town, BuildingID buildingID, int what) override; //what: 1 - built, 2 - demolished
  78. void artifactPut(const ArtifactLocation &al) override;
  79. void artifactRemoved(const ArtifactLocation &al) override;
  80. void artifactMoved(const ArtifactLocation &src, const ArtifactLocation &dst) override;
  81. void bulkArtMovementStart(size_t totalNumOfArts, size_t possibleAssemblyNumOfArts) override;
  82. void artifactAssembled(const ArtifactLocation &al) override;
  83. void askToAssembleArtifact(const ArtifactLocation & dst) override;
  84. void artifactDisassembled(const ArtifactLocation &al) override;
  85. void heroVisit(const CGHeroInstance * visitor, const CGObjectInstance * visitedObj, bool start) override;
  86. void heroCreated(const CGHeroInstance* hero) override;
  87. void heroGotLevel(const CGHeroInstance *hero, PrimarySkill pskill, std::vector<SecondarySkill> &skills, QueryID queryID) override;
  88. void commanderGotLevel (const CCommanderInstance * commander, std::vector<ui32> skills, QueryID queryID) override;
  89. void heroInGarrisonChange(const CGTownInstance *town) override;
  90. void heroMoved(const TryMoveHero & details, bool verbose = true) override;
  91. void heroExperienceChanged(const CGHeroInstance * hero, si64 val) 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, QueryID queryID) override;
  100. 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.
  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, QueryID queryID) override;
  105. void showUniversityWindow(const IMarket *market, const CGHeroInstance *visitor, QueryID queryID) 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 FowTilesType &pos) override; //called when given tiles become hidden under fog of war
  109. void tileRevealed(const FowTilesType &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, const PlayerColor & initiator) 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 interface
  126. void playerEndsTurn(PlayerColor player) override;
  127. void showWorldViewEx(const std::vector<ObjectPosInfo> & objectPositions, bool showTerrain) override;
  128. void setColorScheme(ColorScheme scheme) 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, const BattleHexArray & 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, BattleSide 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 GAME->interface() access
  152. // part of GameInterface that is also used by client code
  153. void update();
  154. void showPuzzleMap() override;
  155. void viewWorldMap() override;
  156. void showQuestLog() override;
  157. void showThievesGuildWindow (const CGObjectInstance * obj) override;
  158. void showTavernWindow(const CGObjectInstance * object, const CGHeroInstance * visitor, QueryID queryID) override;
  159. void showShipyardDialog(const IShipyard *obj) override; //obj may be town or shipyard;
  160. void showHeroExchange(ObjectInstanceID hero1, ObjectInstanceID hero2);
  161. void waitWhileDialog();
  162. void waitForAllDialogs();
  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. void gamePause(bool pause);
  175. void endNetwork();
  176. void closeAllDialogs();
  177. std::shared_ptr<const CPathsInfo> getPathsInfo(const CGHeroInstance * h);
  178. void invalidatePaths() override;
  179. ///returns true if all events are processed internally
  180. bool capturedAllEvents();
  181. void registerBattleInterface(std::shared_ptr<CBattleGameInterface> battleEvents);
  182. void unregisterBattleInterface(std::shared_ptr<CBattleGameInterface> battleEvents);
  183. CPlayerInterface(PlayerColor Player);
  184. ~CPlayerInterface();
  185. private:
  186. struct IgnoreEvents
  187. {
  188. CPlayerInterface & owner;
  189. IgnoreEvents(CPlayerInterface & Owner):owner(Owner)
  190. {
  191. owner.ignoreEvents = true;
  192. };
  193. ~IgnoreEvents()
  194. {
  195. owner.ignoreEvents = false;
  196. };
  197. };
  198. void heroKilled(const CGHeroInstance* hero);
  199. void townRemoved(const CGTownInstance* town);
  200. void garrisonsChanged(std::vector<const CArmedInstance *> objs);
  201. void requestReturningToMainMenu(bool won);
  202. void acceptTurn(QueryID queryID, bool hotseatWait); //used during hot seat after your turn message is close
  203. void initializeHeroTownList();
  204. int getLastIndex(std::string namePrefix);
  205. };