Client.h 10 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 CPackForServer;
  16. class IBattleEventsReceiver;
  17. class CBattleGameInterface;
  18. class CGameInterface;
  19. class BattleAction;
  20. class BattleInfo;
  21. struct BankConfig;
  22. #if SCRIPTING_ENABLED
  23. namespace scripting
  24. {
  25. class PoolImpl;
  26. }
  27. #endif
  28. namespace events
  29. {
  30. class EventBus;
  31. }
  32. VCMI_LIB_NAMESPACE_END
  33. class CBattleCallback;
  34. class CCallback;
  35. class CClient;
  36. class CBaseForCLApply;
  37. namespace boost { class thread; }
  38. template<typename T>
  39. class ThreadSafeVector
  40. {
  41. using TLock = boost::unique_lock<boost::mutex>;
  42. std::vector<T> items;
  43. boost::mutex mx;
  44. boost::condition_variable cond;
  45. public:
  46. void clear()
  47. {
  48. TLock lock(mx);
  49. items.clear();
  50. cond.notify_all();
  51. }
  52. void pushBack(const T & item)
  53. {
  54. TLock lock(mx);
  55. items.push_back(item);
  56. cond.notify_all();
  57. }
  58. void waitWhileContains(const T & item)
  59. {
  60. TLock lock(mx);
  61. while(vstd::contains(items, item))
  62. cond.wait(lock);
  63. }
  64. bool tryRemovingElement(const T & item) //returns false if element was not present
  65. {
  66. TLock lock(mx);
  67. auto itr = vstd::find(items, item);
  68. if(itr == items.end()) //not in container
  69. {
  70. return false;
  71. }
  72. items.erase(itr);
  73. cond.notify_all();
  74. return true;
  75. }
  76. };
  77. class CPlayerEnvironment : public Environment
  78. {
  79. public:
  80. PlayerColor player;
  81. CClient * cl;
  82. std::shared_ptr<CCallback> mainCallback;
  83. CPlayerEnvironment(PlayerColor player_, CClient * cl_, std::shared_ptr<CCallback> mainCallback_);
  84. const Services * services() const override;
  85. vstd::CLoggerBase * logger() const override;
  86. events::EventBus * eventBus() const override;
  87. const BattleCb * battle(const BattleID & battle) const override;
  88. const GameCb * game() const override;
  89. };
  90. /// Class which handles client - server logic
  91. class CClient : public IGameCallback, public Environment
  92. {
  93. public:
  94. std::map<PlayerColor, std::shared_ptr<CGameInterface>> playerint;
  95. std::map<PlayerColor, std::shared_ptr<CBattleGameInterface>> battleints;
  96. std::map<PlayerColor, std::vector<std::shared_ptr<IBattleEventsReceiver>>> additionalBattleInts;
  97. std::unique_ptr<BattleAction> currentBattleAction;
  98. CClient();
  99. ~CClient();
  100. const Services * services() const override;
  101. const BattleCb * battle(const BattleID & battle) const override;
  102. const GameCb * game() const override;
  103. vstd::CLoggerBase * logger() const override;
  104. events::EventBus * eventBus() const override;
  105. void newGame(CGameState * gameState);
  106. void loadGame(CGameState * gameState);
  107. void save(const std::string & fname);
  108. void endNetwork();
  109. void endGame();
  110. void initMapHandler();
  111. void initPlayerEnvironments();
  112. void initPlayerInterfaces();
  113. std::string aiNameForPlayer(const PlayerSettings & ps, bool battleAI, bool alliedToHuman) const; //empty means no AI -> human
  114. std::string aiNameForPlayer(bool battleAI, bool alliedToHuman) const;
  115. void installNewPlayerInterface(std::shared_ptr<CGameInterface> gameInterface, PlayerColor color, bool battlecb = false);
  116. void installNewBattleInterface(std::shared_ptr<CBattleGameInterface> battleInterface, PlayerColor color, bool needCallback = true);
  117. static ThreadSafeVector<int> waitingRequest; //FIXME: make this normal field (need to join all threads before client destruction)
  118. void handlePack(CPackForClient & pack); //applies the given pack and deletes it
  119. int sendRequest(const CPackForServer & request, PlayerColor player); //returns ID given to that request
  120. void battleStarted(const BattleInfo * info);
  121. void battleFinished(const BattleID & battleID);
  122. void startPlayerBattleAction(const BattleID & battleID, PlayerColor color);
  123. void invalidatePaths(); // clears this->pathCache()
  124. void updatePath(const ObjectInstanceID & heroID); // invalidatePaths and update displayed hero path
  125. void updatePath(const CGHeroInstance * hero);
  126. std::shared_ptr<const CPathsInfo> getPathsInfo(const CGHeroInstance * h);
  127. friend class CCallback; //handling players actions
  128. friend class CBattleCallback; //handling players actions
  129. void changeSpells(const CGHeroInstance * hero, bool give, const std::set<SpellID> & spells) override {};
  130. void setResearchedSpells(const CGTownInstance * town, int level, const std::vector<SpellID> & spells, bool accepted) override {};
  131. bool removeObject(const CGObjectInstance * obj, const PlayerColor & initiator) override {return false;};
  132. void createBoat(const int3 & visitablePosition, BoatId type, PlayerColor initiator) 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(const IObjectInterface * caller, 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 ArtifactID & artId, const ArtifactPosition & pos) override {return false;};
  156. bool giveHeroNewScroll(const CGHeroInstance * h, const SpellID & spellId, const ArtifactPosition & pos) override {return false;};
  157. bool putArtifact(const ArtifactLocation & al, const ArtifactInstanceID & id, std::optional<bool> askAssemble) override {return false;};
  158. void removeArtifact(const ArtifactLocation & al) override {};
  159. bool moveArtifact(const PlayerColor & player, const ArtifactLocation & al1, const ArtifactLocation & al2) override {return false;};
  160. void heroVisitCastle(const CGTownInstance * obj, const CGHeroInstance * hero) override {};
  161. void visitCastleObjects(const CGTownInstance * obj, const CGHeroInstance * hero) override {};
  162. void stopHeroVisitCastle(const CGTownInstance * obj, const CGHeroInstance * hero) override {};
  163. void startBattle(const CArmedInstance * army1, const CArmedInstance * army2, int3 tile, const CGHeroInstance * hero1, const CGHeroInstance * hero2, const BattleLayout & layout, const CGTownInstance * town) override {}; //use hero=nullptr for no hero
  164. void startBattle(const CArmedInstance * army1, const CArmedInstance * army2) override {}; //if any of armies is hero, hero will be used
  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(const 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 setBankObjectConfiguration(ObjectInstanceID objid, const BankConfig & configuration) override {};
  180. void setRewardableObjectConfiguration(ObjectInstanceID objid, const Rewardable::Configuration & configuration) override {};
  181. void setRewardableObjectConfiguration(ObjectInstanceID townInstanceID, BuildingID buildingID, const Rewardable::Configuration & configuration) override{};
  182. void showInfoDialog(InfoWindow * iw) override {};
  183. void removeGUI() const;
  184. vstd::RNG & getRandomGenerator() override;
  185. #if SCRIPTING_ENABLED
  186. scripting::Pool * getGlobalContextPool() const override;
  187. #endif
  188. private:
  189. std::map<PlayerColor, std::shared_ptr<CBattleCallback>> battleCallbacks; //callbacks given to player interfaces
  190. std::map<PlayerColor, std::shared_ptr<CPlayerEnvironment>> playerEnvironments;
  191. #if SCRIPTING_ENABLED
  192. std::shared_ptr<scripting::PoolImpl> clientScripts;
  193. #endif
  194. std::unique_ptr<events::EventBus> clientEventBus;
  195. mutable boost::mutex pathCacheMutex;
  196. std::map<const CGHeroInstance *, std::shared_ptr<CPathsInfo>> pathCache;
  197. void reinitScripting();
  198. };