CPlayerInterface.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. #pragma once
  2. //#include "../lib/CondSh.h"
  3. #include "../lib/FunctionList.h"
  4. #include "../lib/CGameInterface.h"
  5. #include "../lib/NetPacksBase.h"
  6. #include "gui/CIntObject.h"
  7. //#include "../lib/CGameState.h"
  8. #ifdef __GNUC__
  9. #define sprintf_s snprintf
  10. #endif
  11. #ifdef max
  12. #undef max
  13. #endif
  14. #ifdef min
  15. #undef min
  16. #endif
  17. /*
  18. * CPlayerInterface.h, part of VCMI engine
  19. *
  20. * Authors: listed in file AUTHORS in main folder
  21. *
  22. * License: GNU General Public License v2.0 or later
  23. * Full text of license available in license.txt file, in main folder
  24. *
  25. */
  26. class CDefEssential;
  27. class CButton;
  28. class CToggleGroup;
  29. class CDefHandler;
  30. struct TryMoveHero;
  31. class CDefEssential;
  32. class CGHeroInstance;
  33. class CAdvMapInt;
  34. class CCastleInterface;
  35. class CBattleInterface;
  36. class CStack;
  37. class CComponent;
  38. class CCreature;
  39. struct SDL_Surface;
  40. struct CGPath;
  41. class CCreatureAnimation;
  42. class CSelectableComponent;
  43. class CCreatureSet;
  44. class CGObjectInstance;
  45. class CSlider;
  46. struct UpgradeInfo;
  47. template <typename T> struct CondSh;
  48. class CInGameConsole;
  49. class CGarrisonInt;
  50. class CInGameConsole;
  51. union SDL_Event;
  52. class CInfoWindow;
  53. class IShowActivatable;
  54. class ClickableL;
  55. class ClickableR;
  56. class Hoverable;
  57. class KeyInterested;
  58. class MotionInterested;
  59. class TimeInterested;
  60. class IShowable;
  61. struct CPathsInfo;
  62. namespace boost
  63. {
  64. class mutex;
  65. class recursive_mutex;
  66. }
  67. enum
  68. {
  69. /*CHANGE_SCREEN_RESOLUTION = 1,*/
  70. RETURN_TO_MAIN_MENU = 2,
  71. STOP_CLIENT = 3,
  72. RESTART_GAME,
  73. RETURN_TO_MENU_LOAD,
  74. FULLSCREEN_TOGGLED,
  75. PREPARE_RESTART_CAMPAIGN
  76. };
  77. /// Central class for managing user interface logic
  78. class CPlayerInterface : public CGameInterface, public ILockedUpdatable
  79. {
  80. public:
  81. bool observerInDuelMode;
  82. //minor interfaces
  83. CondSh<bool> *showingDialog; //indicates if dialog box is displayed
  84. static boost::recursive_mutex *pim;
  85. bool makingTurn; //if player is already making his turn
  86. int firstCall; // -1 - just loaded game; 1 - just started game; 0 otherwise
  87. int autosaveCount;
  88. static const int SAVES_COUNT = 5;
  89. static int howManyPeople;
  90. CCastleInterface * castleInt; //nullptr if castle window isn't opened
  91. static CBattleInterface * battleInt; //nullptr if no battle
  92. CInGameConsole * cingconsole;
  93. shared_ptr<CCallback> cb; //to communicate with engine
  94. const BattleAction *curAction; //during the battle - action currently performed by active stack (or nullptr)
  95. std::list<CInfoWindow *> dialogs; //queue of dialogs awaiting to be shown (not currently shown!)
  96. std::vector<const CGHeroInstance *> wanderingHeroes; //our heroes on the adventure map (not the garrisoned ones)
  97. std::vector<const CGTownInstance *> towns; //our towns on the adventure map
  98. std::map<const CGHeroInstance *, CGPath> paths; //maps hero => selected path in adventure map
  99. std::vector<const CGHeroInstance *> sleepingHeroes; //if hero is in here, he's sleeping
  100. //During battle is quick combat mode is used
  101. shared_ptr<CBattleGameInterface> autofightingAI; //AI that makes decisions
  102. bool isAutoFightOn; //Flag, switch it to stop quick combat. Don't touch if there is no battle interface.
  103. struct SpellbookLastSetting
  104. {
  105. int spellbookLastPageBattle, spellbokLastPageAdvmap; //on which page we left spellbook
  106. int spellbookLastTabBattle, spellbookLastTabAdvmap; //on which page we left spellbook
  107. SpellbookLastSetting();
  108. template <typename Handler> void serialize( Handler &h, const int version )
  109. {
  110. h & spellbookLastPageBattle & spellbokLastPageAdvmap & spellbookLastTabBattle & spellbookLastTabAdvmap;
  111. }
  112. } spellbookSettings;
  113. void update() override;
  114. void runLocked(std::function<void(IUpdateable * )> functor) override;
  115. void initializeHeroTownList();
  116. int getLastIndex(std::string namePrefix);
  117. //overridden funcs from CGameInterface
  118. void buildChanged(const CGTownInstance *town, BuildingID buildingID, int what) override; //what: 1 - built, 2 - demolished
  119. void stackChagedCount(const StackLocation &location, const TQuantity &change, bool isAbsolute) override; //if absolute, change is the new count; otherwise count was modified by adding change
  120. void stackChangedType(const StackLocation &location, const CCreature &newType) override; //used eg. when upgrading creatures
  121. void stacksErased(const StackLocation &location) override; //stack removed from previously filled slot
  122. void stacksSwapped(const StackLocation &loc1, const StackLocation &loc2) override;
  123. void newStackInserted(const StackLocation &location, const CStackInstance &stack) override; //new stack inserted at given (previously empty position)
  124. void stacksRebalanced(const StackLocation &src, const StackLocation &dst, TQuantity count) override; //moves creatures from src stack to dst slot, may be used for merging/splittint/moving stacks
  125. void artifactPut(const ArtifactLocation &al);
  126. void artifactRemoved(const ArtifactLocation &al);
  127. void artifactMoved(const ArtifactLocation &src, const ArtifactLocation &dst);
  128. void artifactAssembled(const ArtifactLocation &al);
  129. void artifactDisassembled(const ArtifactLocation &al);
  130. void heroCreated(const CGHeroInstance* hero) override;
  131. void heroGotLevel(const CGHeroInstance *hero, PrimarySkill::PrimarySkill pskill, std::vector<SecondarySkill> &skills, QueryID queryID) override;
  132. void commanderGotLevel (const CCommanderInstance * commander, std::vector<ui32> skills, QueryID queryID) override;
  133. void heroInGarrisonChange(const CGTownInstance *town) override;
  134. void heroMoved(const TryMoveHero & details) override;
  135. void heroPrimarySkillChanged(const CGHeroInstance * hero, int which, si64 val) override;
  136. void heroSecondarySkillChanged(const CGHeroInstance * hero, int which, int val) override;
  137. void heroManaPointsChanged(const CGHeroInstance * hero) override;
  138. void heroMovePointsChanged(const CGHeroInstance * hero) override;
  139. void heroVisitsTown(const CGHeroInstance* hero, const CGTownInstance * town) override;
  140. void receivedResource(int type, int val) override;
  141. void showInfoDialog(const std::string &text, const std::vector<Component*> &components, int soundID) override;
  142. void showRecruitmentDialog(const CGDwelling *dwelling, const CArmedInstance *dst, int level) override;
  143. void showShipyardDialog(const IShipyard *obj) override; //obj may be town or shipyard;
  144. void showBlockingDialog(const std::string &text, const std::vector<Component> &components, QueryID askID, 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.
  145. void showGarrisonDialog(const CArmedInstance *up, const CGHeroInstance *down, bool removableUnits, QueryID queryID) override;
  146. void showPuzzleMap() 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 gameOver(PlayerColor player, const EVictoryLossCheckResult & victoryLossCheckResult) override;
  168. void playerStartsTurn(PlayerColor player) override; //called before yourTurn on active itnerface
  169. void showComp(const Component &comp, std::string message) override; //display component in the advmapint infobox
  170. void saveGame(COSer<CSaveFile> &h, const int version) override; //saving
  171. void loadGame(CISer<CLoadFile> &h, const int version) override; //loading
  172. //for battles
  173. void actionFinished(const BattleAction& action) override;//occurs AFTER action taken by active stack or by the hero
  174. void actionStarted(const BattleAction& action) override;//occurs BEFORE action taken by active stack or by the hero
  175. BattleAction activeStack(const CStack * stack) override; //called when it's turn of that stack
  176. void battleAttack(const BattleAttack *ba) override; //stack performs attack
  177. void battleEnd(const BattleResult *br) override; //end of battle
  178. void battleNewRoundFirst(int round) override; //called at the beginning of each turn before changes are applied; used for HP regen handling
  179. 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
  180. void battleStackMoved(const CStack * stack, std::vector<BattleHex> dest, int distance) override;
  181. void battleSpellCast(const BattleSpellCast *sc) override;
  182. void battleStacksEffectsSet(const SetStackEffect & sse) override; //called when a specific effect is set to stacks
  183. void battleTriggerEffect(const BattleTriggerEffect & bte) override; //various one-shot effect
  184. void battleStacksAttacked(const std::vector<BattleStackAttacked> & bsa) override;
  185. 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
  186. 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
  187. void battleStacksHealedRes(const std::vector<std::pair<ui32, ui32> > & healedStacks, bool lifeDrain, bool tentHeal, si32 lifeDrainFrom) override; //called when stacks are healed / resurrected
  188. void battleNewStackAppeared(const CStack * stack) override; //not called at the beginning of a battle or by resurrection; called eg. when elemental is summoned
  189. void battleObstaclesRemoved(const std::set<si32> & removedObstacles) override; //called when a certain set of obstacles is removed from batlefield; IDs of them are given
  190. void battleCatapultAttacked(const CatapultAttack & ca) override; //called when catapult makes an attack
  191. void battleStacksRemoved(const BattleStacksRemoved & bsr) override; //called when certain stack is completely removed from battlefield
  192. void battleObstaclePlaced(const CObstacleInstance &obstacle) override;
  193. void yourTacticPhase(int distance) override;
  194. //-------------//
  195. void showArtifactAssemblyDialog(ui32 artifactID, ui32 assembleTo, bool assemble, CFunctionList<bool()> onYes, CFunctionList<bool()> onNo);
  196. void garrisonsChanged(std::vector<const CGObjectInstance *> objs);
  197. void garrisonChanged(const CGObjectInstance * obj);
  198. void heroKilled(const CGHeroInstance* hero);
  199. void waitWhileDialog(bool unlockPim = true);
  200. void waitForAllDialogs(bool unlockPim = true);
  201. bool shiftPressed() const; //determines if shift key is pressed (left or right or both)
  202. bool ctrlPressed() const; //determines if ctrl key is pressed (left or right or both)
  203. bool altPressed() const; //determines if alt key is pressed (left or right or both)
  204. void redrawHeroWin(const CGHeroInstance * hero);
  205. void openTownWindow(const CGTownInstance * town); //shows townscreen
  206. void openHeroWindow(const CGHeroInstance * hero); //shows hero window with given hero
  207. void updateInfo(const CGObjectInstance * specific);
  208. void init(shared_ptr<CCallback> CB);
  209. int3 repairScreenPos(int3 pos); //returns position closest to pos we can center screen on
  210. // show dialogs
  211. void showInfoDialog(const std::string &text, CComponent * component);
  212. void showInfoDialog(const std::string &text, const std::vector<CComponent*> & components = std::vector<CComponent*>(), int soundID = 0, bool delComps = false);
  213. void showInfoDialogAndWait(std::vector<Component> & components, const MetaString & text);
  214. void showOkDialog(std::vector<Component> & components, const MetaString & text, const std::function<void()> & onOk);
  215. void showYesNoDialog(const std::string &text, CFunctionList<void()> onYes, CFunctionList<void()> onNo, bool DelComps = false, const std::vector<CComponent*> & components = std::vector<CComponent*>()); //deactivateCur - whether current main interface should be deactivated; delComps - if components will be deleted on window close
  216. void stopMovement();
  217. void moveHero(const CGHeroInstance *h, CGPath path);
  218. void initMovement(const TryMoveHero &details, const CGHeroInstance * ho, const int3 &hp );//initializing objects and performing first step of move
  219. void movementPxStep( const TryMoveHero &details, int i, const int3 &hp, const CGHeroInstance * ho );//performing step of movement
  220. void finishMovement( const TryMoveHero &details, const int3 &hp, const CGHeroInstance * ho ); //finish movement
  221. void eraseCurrentPathOf( const CGHeroInstance * ho, bool checkForExistanceOfPath = true );
  222. void removeLastNodeFromPath(const CGHeroInstance *ho);
  223. CGPath *getAndVerifyPath( const CGHeroInstance * h );
  224. void acceptTurn(); //used during hot seat after your turn message is close
  225. void tryDiggging(const CGHeroInstance *h);
  226. void showShipyardDialogOrProblemPopup(const IShipyard *obj); //obj may be town or shipyard;
  227. void requestReturningToMainMenu();
  228. void requestStoppingClient();
  229. void sendCustomEvent(int code);
  230. void proposeLoadingGame();
  231. ///returns true if all events are processed internally
  232. bool capturedAllEvents();
  233. CPlayerInterface(PlayerColor Player);//c-tor
  234. ~CPlayerInterface();//d-tor
  235. static CondSh<bool> terminate_cond; // confirm termination
  236. private:
  237. template <typename Handler> void serializeTempl(Handler &h, const int version);
  238. private:
  239. struct IgnoreEvents
  240. {
  241. CPlayerInterface & owner;
  242. IgnoreEvents(CPlayerInterface & Owner):owner(Owner)
  243. {
  244. owner.ignoreEvents = true;
  245. };
  246. ~IgnoreEvents()
  247. {
  248. owner.ignoreEvents = false;
  249. };
  250. };
  251. bool duringMovement;
  252. bool ignoreEvents;
  253. bool locked;
  254. void doMoveHero(const CGHeroInstance *h, CGPath path);
  255. };
  256. extern CPlayerInterface * LOCPLINT;