Client.h 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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 <memory>
  12. #include <vcmi/Environment.h>
  13. #include "../lib/IGameCallback.h"
  14. VCMI_LIB_NAMESPACE_BEGIN
  15. struct CPack;
  16. struct CPackForServer;
  17. class IBattleEventsReceiver;
  18. class CBattleGameInterface;
  19. class CGameInterface;
  20. class BinaryDeserializer;
  21. class BinarySerializer;
  22. class BattleAction;
  23. class BattleInfo;
  24. template<typename T> class CApplier;
  25. #if SCRIPTING_ENABLED
  26. namespace scripting
  27. {
  28. class PoolImpl;
  29. }
  30. #endif
  31. namespace events
  32. {
  33. class EventBus;
  34. }
  35. VCMI_LIB_NAMESPACE_END
  36. class CBattleCallback;
  37. class CCallback;
  38. class CClient;
  39. class CBaseForCLApply;
  40. namespace boost { class thread; }
  41. template<typename T>
  42. class ThreadSafeVector
  43. {
  44. using TLock = boost::unique_lock<boost::mutex>;
  45. std::vector<T> items;
  46. boost::mutex mx;
  47. boost::condition_variable cond;
  48. public:
  49. void clear()
  50. {
  51. TLock lock(mx);
  52. items.clear();
  53. cond.notify_all();
  54. }
  55. void pushBack(const T & item)
  56. {
  57. TLock lock(mx);
  58. items.push_back(item);
  59. cond.notify_all();
  60. }
  61. void waitWhileContains(const T & item)
  62. {
  63. TLock lock(mx);
  64. while(vstd::contains(items, item))
  65. cond.wait(lock);
  66. }
  67. bool tryRemovingElement(const T & item) //returns false if element was not present
  68. {
  69. TLock lock(mx);
  70. auto itr = vstd::find(items, item);
  71. if(itr == items.end()) //not in container
  72. {
  73. return false;
  74. }
  75. items.erase(itr);
  76. cond.notify_all();
  77. return true;
  78. }
  79. };
  80. class CPlayerEnvironment : public Environment
  81. {
  82. public:
  83. PlayerColor player;
  84. CClient * cl;
  85. std::shared_ptr<CCallback> mainCallback;
  86. CPlayerEnvironment(PlayerColor player_, CClient * cl_, std::shared_ptr<CCallback> mainCallback_);
  87. const Services * services() const override;
  88. vstd::CLoggerBase * logger() const override;
  89. events::EventBus * eventBus() const override;
  90. const BattleCb * battle(const BattleID & battle) const override;
  91. const GameCb * game() const override;
  92. };
  93. /// Class which handles client - server logic
  94. class CClient : public IGameCallback, public Environment
  95. {
  96. public:
  97. std::map<PlayerColor, std::shared_ptr<CGameInterface>> playerint;
  98. std::map<PlayerColor, std::shared_ptr<CBattleGameInterface>> battleints;
  99. std::map<PlayerColor, std::vector<std::shared_ptr<IBattleEventsReceiver>>> additionalBattleInts;
  100. std::unique_ptr<BattleAction> currentBattleAction;
  101. CClient();
  102. ~CClient();
  103. const Services * services() const override;
  104. const BattleCb * battle(const BattleID & battle) const override;
  105. const GameCb * game() const override;
  106. vstd::CLoggerBase * logger() const override;
  107. events::EventBus * eventBus() const override;
  108. void newGame(CGameState * gameState);
  109. void loadGame(CGameState * gameState);
  110. void save(const std::string & fname);
  111. void endNetwork();
  112. void endGame();
  113. void initMapHandler();
  114. void initPlayerEnvironments();
  115. void initPlayerInterfaces();
  116. std::string aiNameForPlayer(const PlayerSettings & ps, bool battleAI, bool alliedToHuman) const; //empty means no AI -> human
  117. std::string aiNameForPlayer(bool battleAI, bool alliedToHuman) const;
  118. void installNewPlayerInterface(std::shared_ptr<CGameInterface> gameInterface, PlayerColor color, bool battlecb = false);
  119. void installNewBattleInterface(std::shared_ptr<CBattleGameInterface> battleInterface, PlayerColor color, bool needCallback = true);
  120. static ThreadSafeVector<int> waitingRequest; //FIXME: make this normal field (need to join all threads before client destruction)
  121. void handlePack(CPack * pack); //applies the given pack and deletes it
  122. int sendRequest(const CPackForServer * request, PlayerColor player); //returns ID given to that request
  123. void battleStarted(const BattleInfo * info);
  124. void battleFinished(const BattleID & battleID);
  125. void startPlayerBattleAction(const BattleID & battleID, PlayerColor color);
  126. void invalidatePaths();
  127. std::shared_ptr<const CPathsInfo> getPathsInfo(const CGHeroInstance * h);
  128. friend class CCallback; //handling players actions
  129. friend class CBattleCallback; //handling players actions
  130. void changeSpells(const CGHeroInstance * hero, bool give, const std::set<SpellID> & spells) override {};
  131. bool removeObject(const CGObjectInstance * obj, const PlayerColor & initiator) override {return false;};
  132. void createObject(const int3 & visitablePosition, const PlayerColor & initiator, MapObjectID type, MapObjectSubID subtype) override {};
  133. void setOwner(const CGObjectInstance * obj, PlayerColor owner) override {};
  134. void giveExperience(const CGHeroInstance * hero, TExpType val) override {};
  135. void changePrimSkill(const CGHeroInstance * hero, PrimarySkill which, si64 val, bool abs = false) override {};
  136. void changeSecSkill(const CGHeroInstance * hero, SecondarySkill which, int val, bool abs = false) override {};
  137. void showBlockingDialog(BlockingDialog * iw) override {};
  138. void showGarrisonDialog(ObjectInstanceID upobj, ObjectInstanceID hid, bool removableUnits) override {};
  139. void showTeleportDialog(TeleportDialog * iw) override {};
  140. void showObjectWindow(const CGObjectInstance * object, EOpenWindowMode window, const CGHeroInstance * visitor, bool addQuery) override {};
  141. void giveResource(PlayerColor player, GameResID which, int val) override {};
  142. void giveResources(PlayerColor player, TResources resources) override {};
  143. void giveCreatures(const CArmedInstance * objid, const CGHeroInstance * h, const CCreatureSet & creatures, bool remove) override {};
  144. void takeCreatures(ObjectInstanceID objid, const std::vector<CStackBasicDescriptor> & creatures) override {};
  145. bool changeStackType(const StackLocation & sl, const CCreature * c) override {return false;};
  146. bool changeStackCount(const StackLocation & sl, TQuantity count, bool absoluteValue = false) override {return false;};
  147. bool insertNewStack(const StackLocation & sl, const CCreature * c, TQuantity count) override {return false;};
  148. bool eraseStack(const StackLocation & sl, bool forceRemoval = false) override {return false;};
  149. bool swapStacks(const StackLocation & sl1, const StackLocation & sl2) override {return false;}
  150. bool addToSlot(const StackLocation & sl, const CCreature * c, TQuantity count) override {return false;}
  151. void tryJoiningArmy(const CArmedInstance * src, const CArmedInstance * dst, bool removeObjWhenFinished, bool allowMerging) override {}
  152. bool moveStack(const StackLocation & src, const StackLocation & dst, TQuantity count = -1) override {return false;}
  153. void removeAfterVisit(const CGObjectInstance * object) override {};
  154. bool swapGarrisonOnSiege(ObjectInstanceID tid) override {return false;};
  155. bool giveHeroNewArtifact(const CGHeroInstance * h, const CArtifact * artType, ArtifactPosition pos) override {return false;}
  156. bool putArtifact(const ArtifactLocation & al, const CArtifactInstance * art, std::optional<bool> askAssemble) override {return false;};
  157. void removeArtifact(const ArtifactLocation & al) override {};
  158. bool moveArtifact(const PlayerColor & player, const ArtifactLocation & al1, const ArtifactLocation & al2) override {return false;};
  159. void heroVisitCastle(const CGTownInstance * obj, const CGHeroInstance * hero) override {};
  160. void visitCastleObjects(const CGTownInstance * obj, const CGHeroInstance * hero) override {};
  161. void stopHeroVisitCastle(const CGTownInstance * obj, const CGHeroInstance * hero) override {};
  162. 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
  163. void startBattleI(const CArmedInstance * army1, const CArmedInstance * army2, int3 tile, bool creatureBank = false) override {}; //if any of armies is hero, hero will be used
  164. 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
  165. bool moveHero(ObjectInstanceID hid, int3 dst, EMovementMode movementMode, bool transit = false, PlayerColor asker = PlayerColor::NEUTRAL) override {return false;};
  166. void giveHeroBonus(GiveBonus * bonus) override {};
  167. void setMovePoints(SetMovePoints * smp) override {};
  168. void setMovePoints(ObjectInstanceID hid, int val, bool absolute) override {};
  169. void setManaPoints(ObjectInstanceID hid, int val) override {};
  170. void giveHero(ObjectInstanceID id, PlayerColor player, ObjectInstanceID boatId = ObjectInstanceID()) override {};
  171. void changeObjPos(ObjectInstanceID objid, int3 newPos, const PlayerColor & initiator) override {};
  172. void sendAndApply(CPackForClient * pack) override {};
  173. void heroExchange(ObjectInstanceID hero1, ObjectInstanceID hero2) override {};
  174. void castSpell(const spells::Caster * caster, SpellID spellID, const int3 &pos) override {};
  175. void changeFogOfWar(int3 center, ui32 radius, PlayerColor player, ETileVisibility mode) override {}
  176. void changeFogOfWar(std::unordered_set<int3> & tiles, PlayerColor player, ETileVisibility mode) override {}
  177. void setObjPropertyValue(ObjectInstanceID objid, ObjProperty prop, int32_t value) override {};
  178. void setObjPropertyID(ObjectInstanceID objid, ObjProperty prop, ObjPropertyID identifier) override {};
  179. void showInfoDialog(InfoWindow * iw) override {};
  180. void removeGUI() const;
  181. vstd::RNG & getRandomGenerator() override;
  182. #if SCRIPTING_ENABLED
  183. scripting::Pool * getGlobalContextPool() const override;
  184. #endif
  185. private:
  186. std::map<PlayerColor, std::shared_ptr<CBattleCallback>> battleCallbacks; //callbacks given to player interfaces
  187. std::map<PlayerColor, std::shared_ptr<CPlayerEnvironment>> playerEnvironments;
  188. #if SCRIPTING_ENABLED
  189. std::shared_ptr<scripting::PoolImpl> clientScripts;
  190. #endif
  191. std::unique_ptr<events::EventBus> clientEventBus;
  192. std::shared_ptr<CApplier<CBaseForCLApply>> applier;
  193. mutable boost::mutex pathCacheMutex;
  194. std::map<const CGHeroInstance *, std::shared_ptr<CPathsInfo>> pathCache;
  195. void reinitScripting();
  196. };