CPlayerInterface.h 14 KB

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