Client.h 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /*
  2. * Client.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/IGameCallback.h"
  12. #include "../lib/battle/BattleAction.h"
  13. #include "../lib/CStopWatch.h"
  14. #include "../lib/int3.h"
  15. #include "../lib/CondSh.h"
  16. #include "../lib/CPathfinder.h"
  17. struct CPack;
  18. struct CPackForServer;
  19. class CCampaignState;
  20. class CBattleCallback;
  21. class IGameEventsReceiver;
  22. class IBattleEventsReceiver;
  23. class CBattleGameInterface;
  24. class CGameState;
  25. class CGameInterface;
  26. class CCallback;
  27. class BattleAction;
  28. class CClient;
  29. class CScriptingModule;
  30. struct CPathsInfo;
  31. class BinaryDeserializer;
  32. class BinarySerializer;
  33. namespace boost { class thread; }
  34. template<typename T> class CApplier;
  35. class CBaseForCLApply;
  36. template<typename T>
  37. class ThreadSafeVector
  38. {
  39. typedef std::vector<T> TVector;
  40. typedef boost::unique_lock<boost::mutex> TLock;
  41. TVector items;
  42. boost::mutex mx;
  43. boost::condition_variable cond;
  44. public:
  45. void clear()
  46. {
  47. TLock lock(mx);
  48. items.clear();
  49. cond.notify_all();
  50. }
  51. void pushBack(const T & item)
  52. {
  53. TLock lock(mx);
  54. items.push_back(item);
  55. cond.notify_all();
  56. }
  57. // //to access list, caller must present a lock used to lock mx
  58. // TVector &getList(TLock &lockedLock)
  59. // {
  60. // assert(lockedLock.owns_lock() && lockedLock.mutex() == &mx);
  61. // return items;
  62. // }
  63. TLock getLock()
  64. {
  65. return TLock(mx);
  66. }
  67. void waitWhileContains(const T & item)
  68. {
  69. auto lock = getLock();
  70. while(vstd::contains(items, item))
  71. cond.wait(lock);
  72. }
  73. bool tryRemovingElement(const T & item) //returns false if element was not present
  74. {
  75. auto lock = getLock();
  76. auto itr = vstd::find(items, item);
  77. if(itr == items.end()) //not in container
  78. {
  79. return false;
  80. }
  81. items.erase(itr);
  82. cond.notify_all();
  83. return true;
  84. }
  85. };
  86. /// Class which handles client - server logic
  87. class CClient : public IGameCallback
  88. {
  89. std::shared_ptr<CApplier<CBaseForCLApply>> applier;
  90. mutable boost::mutex pathCacheMutex;
  91. std::map<const CGHeroInstance *, std::shared_ptr<CPathsInfo>> pathCache;
  92. std::map<PlayerColor, std::shared_ptr<boost::thread>> playerActionThreads;
  93. void waitForMoveAndSend(PlayerColor color);
  94. public:
  95. std::map<PlayerColor, std::shared_ptr<CCallback>> callbacks; //callbacks given to player interfaces
  96. std::map<PlayerColor, std::shared_ptr<CBattleCallback>> battleCallbacks; //callbacks given to player interfaces
  97. std::vector<std::shared_ptr<IGameEventsReceiver>> privilegedGameEventReceivers; //scripting modules, spectator interfaces
  98. std::vector<std::shared_ptr<IBattleEventsReceiver>> privilegedBattleEventReceivers; //scripting modules, spectator interfaces
  99. std::map<PlayerColor, std::shared_ptr<CGameInterface>> playerint;
  100. std::map<PlayerColor, std::shared_ptr<CBattleGameInterface>> battleints;
  101. std::map<PlayerColor, std::vector<std::shared_ptr<IGameEventsReceiver>>> additionalPlayerInts;
  102. std::map<PlayerColor, std::vector<std::shared_ptr<IBattleEventsReceiver>>> additionalBattleInts;
  103. boost::optional<BattleAction> curbaction;
  104. CScriptingModule * erm;
  105. CClient();
  106. void newGame();
  107. void loadGame();
  108. void serialize(BinarySerializer & h, const int version);
  109. void serialize(BinaryDeserializer & h, const int version);
  110. void save(const std::string & fname);
  111. void endGame();
  112. void initMapHandler();
  113. void initPlayerInterfaces();
  114. std::string aiNameForPlayer(const PlayerSettings & ps, bool battleAI); //empty means no AI -> human
  115. std::string aiNameForPlayer(bool battleAI);
  116. void installNewPlayerInterface(std::shared_ptr<CGameInterface> gameInterface, boost::optional<PlayerColor> color, bool battlecb = false);
  117. void installNewBattleInterface(std::shared_ptr<CBattleGameInterface> battleInterface, boost::optional<PlayerColor> color, bool needCallback = true);
  118. static ThreadSafeVector<int> waitingRequest; //FIXME: make this normal field (need to join all threads before client destruction)
  119. void handlePack(CPack * pack); //applies the given pack and deletes it
  120. void commitPackage(CPackForClient * pack) override;
  121. int sendRequest(const CPackForServer * request, PlayerColor player); //returns ID given to that request
  122. void battleStarted(const BattleInfo * info);
  123. void commenceTacticPhaseForInt(std::shared_ptr<CBattleGameInterface> battleInt); //will be called as separate thread
  124. void battleFinished();
  125. void startPlayerBattleAction(PlayerColor color);
  126. void stopPlayerBattleAction(PlayerColor color);
  127. void stopAllBattleActions();
  128. void invalidatePaths();
  129. std::shared_ptr<const CPathsInfo> getPathsInfo(const CGHeroInstance * h);
  130. virtual PlayerColor getLocalPlayer() const override;
  131. friend class CCallback; //handling players actions
  132. friend class CBattleCallback; //handling players actions
  133. void changeSpells(const CGHeroInstance * hero, bool give, const std::set<SpellID> & spells) override {};
  134. bool removeObject(const CGObjectInstance * obj) override {return false;};
  135. void setBlockVis(ObjectInstanceID objid, bool bv) override {};
  136. void setOwner(const CGObjectInstance * obj, PlayerColor owner) override {};
  137. void changePrimSkill(const CGHeroInstance * hero, PrimarySkill::PrimarySkill which, si64 val, bool abs = false) override {};
  138. void changeSecSkill(const CGHeroInstance * hero, SecondarySkill which, int val, bool abs = false) override {};
  139. void showBlockingDialog(BlockingDialog * iw) override {};
  140. void showGarrisonDialog(ObjectInstanceID upobj, ObjectInstanceID hid, bool removableUnits) override {};
  141. void showTeleportDialog(TeleportDialog * iw) override {};
  142. void showThievesGuildWindow(PlayerColor player, ObjectInstanceID requestingObjId) override {};
  143. void giveResource(PlayerColor player, Res::ERes which, int val) override {};
  144. virtual void giveResources(PlayerColor player, TResources resources) override {};
  145. void giveCreatures(const CArmedInstance * objid, const CGHeroInstance * h, const CCreatureSet & creatures, bool remove) override {};
  146. void takeCreatures(ObjectInstanceID objid, const std::vector<CStackBasicDescriptor> & creatures) override {};
  147. bool changeStackType(const StackLocation & sl, const CCreature * c) override {return false;};
  148. bool changeStackCount(const StackLocation & sl, TQuantity count, bool absoluteValue = false) override {return false;};
  149. bool insertNewStack(const StackLocation & sl, const CCreature * c, TQuantity count) override {return false;};
  150. bool eraseStack(const StackLocation & sl, bool forceRemoval = false) override {return false;};
  151. bool swapStacks(const StackLocation & sl1, const StackLocation & sl2) override {return false;}
  152. bool addToSlot(const StackLocation & sl, const CCreature * c, TQuantity count) override {return false;}
  153. void tryJoiningArmy(const CArmedInstance * src, const CArmedInstance * dst, bool removeObjWhenFinished, bool allowMerging) override {}
  154. bool moveStack(const StackLocation & src, const StackLocation & dst, TQuantity count = -1) override {return false;}
  155. void removeAfterVisit(const CGObjectInstance * object) override {};
  156. void giveHeroNewArtifact(const CGHeroInstance * h, const CArtifact * artType, ArtifactPosition pos) override {};
  157. void giveHeroArtifact(const CGHeroInstance * h, const CArtifactInstance * a, ArtifactPosition pos) override {};
  158. void putArtifact(const ArtifactLocation & al, const CArtifactInstance * a) override {};
  159. void removeArtifact(const ArtifactLocation & al) override {};
  160. bool moveArtifact(const ArtifactLocation & al1, const ArtifactLocation & al2) override {return false;};
  161. void synchronizeArtifactHandlerLists() override {};
  162. void showCompInfo(ShowInInfobox * comp) override {};
  163. void heroVisitCastle(const CGTownInstance * obj, const CGHeroInstance * hero) override {};
  164. void visitCastleObjects(const CGTownInstance * obj, const CGHeroInstance * hero) override {};
  165. void stopHeroVisitCastle(const CGTownInstance * obj, const CGHeroInstance * hero) override {};
  166. void startBattlePrimary(const CArmedInstance * army1, const CArmedInstance * army2, int3 tile, const CGHeroInstance * hero1, const CGHeroInstance * hero2, bool creatureBank = false, const CGTownInstance * town = nullptr) override {}; //use hero=nullptr for no hero
  167. void startBattleI(const CArmedInstance * army1, const CArmedInstance * army2, int3 tile, bool creatureBank = false) override {}; //if any of armies is hero, hero will be used
  168. void startBattleI(const CArmedInstance * army1, const CArmedInstance * army2, bool creatureBank = false) override {}; //if any of armies is hero, hero will be used, visitable tile of second obj is place of battle
  169. void setAmount(ObjectInstanceID objid, ui32 val) override {};
  170. bool moveHero(ObjectInstanceID hid, int3 dst, ui8 teleporting, bool transit = false, PlayerColor asker = PlayerColor::NEUTRAL) override {return false;};
  171. void giveHeroBonus(GiveBonus * bonus) override {};
  172. void setMovePoints(SetMovePoints * smp) override {};
  173. void setManaPoints(ObjectInstanceID hid, int val) override {};
  174. void giveHero(ObjectInstanceID id, PlayerColor player) override {};
  175. void changeObjPos(ObjectInstanceID objid, int3 newPos, ui8 flags) override {};
  176. void sendAndApply(CPackForClient * pack) override {};
  177. void heroExchange(ObjectInstanceID hero1, ObjectInstanceID hero2) override {};
  178. void changeFogOfWar(int3 center, ui32 radius, PlayerColor player, bool hide) override {}
  179. void changeFogOfWar(std::unordered_set<int3, ShashInt3> & tiles, PlayerColor player, bool hide) override {}
  180. };