Client.h 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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. template<typename T>
  38. class ThreadSafeVector
  39. {
  40. using TLock = std::unique_lock<std::mutex>;
  41. std::vector<T> items;
  42. std::mutex mx;
  43. std::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. void waitWhileContains(const T & item)
  58. {
  59. //FIXME: should throw exception on destruction
  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. friend class CCallback; //handling players actions
  124. friend class CBattleCallback; //handling players actions
  125. void changeSpells(const CGHeroInstance * hero, bool give, const std::set<SpellID> & spells) override {};
  126. void setResearchedSpells(const CGTownInstance * town, int level, const std::vector<SpellID> & spells, bool accepted) override {};
  127. bool removeObject(const CGObjectInstance * obj, const PlayerColor & initiator) override {return false;};
  128. void createBoat(const int3 & visitablePosition, BoatId type, PlayerColor initiator) override {};
  129. void setOwner(const CGObjectInstance * obj, PlayerColor owner) override {};
  130. void giveExperience(const CGHeroInstance * hero, TExpType val) override {};
  131. void changePrimSkill(const CGHeroInstance * hero, PrimarySkill which, si64 val, bool abs = false) override {};
  132. void changeSecSkill(const CGHeroInstance * hero, SecondarySkill which, int val, bool abs = false) override {};
  133. void showBlockingDialog(const IObjectInterface * caller, BlockingDialog * iw) override {};
  134. void showGarrisonDialog(ObjectInstanceID upobj, ObjectInstanceID hid, bool removableUnits) override {};
  135. void showTeleportDialog(TeleportDialog * iw) override {};
  136. void showObjectWindow(const CGObjectInstance * object, EOpenWindowMode window, const CGHeroInstance * visitor, bool addQuery) override {};
  137. void giveResource(PlayerColor player, GameResID which, int val) override {};
  138. void giveResources(PlayerColor player, TResources resources) override {};
  139. void giveCreatures(const CArmedInstance * objid, const CGHeroInstance * h, const CCreatureSet & creatures, bool remove) override {};
  140. void takeCreatures(ObjectInstanceID objid, const std::vector<CStackBasicDescriptor> & creatures) override {};
  141. bool changeStackType(const StackLocation & sl, const CCreature * c) override {return false;};
  142. bool changeStackCount(const StackLocation & sl, TQuantity count, bool absoluteValue = false) override {return false;};
  143. bool insertNewStack(const StackLocation & sl, const CCreature * c, TQuantity count) override {return false;};
  144. bool eraseStack(const StackLocation & sl, bool forceRemoval = false) override {return false;};
  145. bool swapStacks(const StackLocation & sl1, const StackLocation & sl2) override {return false;}
  146. bool addToSlot(const StackLocation & sl, const CCreature * c, TQuantity count) override {return false;}
  147. void tryJoiningArmy(const CArmedInstance * src, const CArmedInstance * dst, bool removeObjWhenFinished, bool allowMerging) override {}
  148. bool moveStack(const StackLocation & src, const StackLocation & dst, TQuantity count = -1) override {return false;}
  149. void removeAfterVisit(const CGObjectInstance * object) override {};
  150. bool swapGarrisonOnSiege(ObjectInstanceID tid) override {return false;};
  151. bool giveHeroNewArtifact(const CGHeroInstance * h, const ArtifactID & artId, const ArtifactPosition & pos) override {return false;};
  152. bool giveHeroNewScroll(const CGHeroInstance * h, const SpellID & spellId, const ArtifactPosition & pos) override {return false;};
  153. bool putArtifact(const ArtifactLocation & al, const ArtifactInstanceID & id, std::optional<bool> askAssemble) override {return false;};
  154. void removeArtifact(const ArtifactLocation & al) override {};
  155. bool moveArtifact(const PlayerColor & player, const ArtifactLocation & al1, const ArtifactLocation & al2) override {return false;};
  156. void heroVisitCastle(const CGTownInstance * obj, const CGHeroInstance * hero) override {};
  157. void visitCastleObjects(const CGTownInstance * obj, const CGHeroInstance * hero) override {};
  158. void stopHeroVisitCastle(const CGTownInstance * obj, const CGHeroInstance * hero) override {};
  159. 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
  160. void startBattle(const CArmedInstance * army1, const CArmedInstance * army2) override {}; //if any of armies is hero, hero will be used
  161. bool moveHero(ObjectInstanceID hid, int3 dst, EMovementMode movementMode, bool transit = false, PlayerColor asker = PlayerColor::NEUTRAL) override {return false;};
  162. void giveHeroBonus(GiveBonus * bonus) override {};
  163. void setMovePoints(SetMovePoints * smp) override {};
  164. void setMovePoints(ObjectInstanceID hid, int val, bool absolute) override {};
  165. void setManaPoints(ObjectInstanceID hid, int val) override {};
  166. void giveHero(ObjectInstanceID id, PlayerColor player, ObjectInstanceID boatId = ObjectInstanceID()) override {};
  167. void changeObjPos(ObjectInstanceID objid, int3 newPos, const PlayerColor & initiator) override {};
  168. void sendAndApply(CPackForClient & pack) override {};
  169. void heroExchange(ObjectInstanceID hero1, ObjectInstanceID hero2) override {};
  170. void castSpell(const spells::Caster * caster, SpellID spellID, const int3 &pos) override {};
  171. void changeFogOfWar(int3 center, ui32 radius, PlayerColor player, ETileVisibility mode) override {}
  172. void changeFogOfWar(const std::unordered_set<int3> & tiles, PlayerColor player, ETileVisibility mode) override {}
  173. void setObjPropertyValue(ObjectInstanceID objid, ObjProperty prop, int32_t value) override {};
  174. void setObjPropertyID(ObjectInstanceID objid, ObjProperty prop, ObjPropertyID identifier) override {};
  175. void setBankObjectConfiguration(ObjectInstanceID objid, const BankConfig & configuration) override {};
  176. void setRewardableObjectConfiguration(ObjectInstanceID objid, const Rewardable::Configuration & configuration) override {};
  177. void setRewardableObjectConfiguration(ObjectInstanceID townInstanceID, BuildingID buildingID, const Rewardable::Configuration & configuration) override{};
  178. void showInfoDialog(InfoWindow * iw) override {};
  179. void removeGUI() const;
  180. vstd::RNG & getRandomGenerator() override;
  181. #if SCRIPTING_ENABLED
  182. scripting::Pool * getGlobalContextPool() const override;
  183. #endif
  184. private:
  185. std::map<PlayerColor, std::shared_ptr<CBattleCallback>> battleCallbacks; //callbacks given to player interfaces
  186. std::map<PlayerColor, std::shared_ptr<CPlayerEnvironment>> playerEnvironments;
  187. #if SCRIPTING_ENABLED
  188. std::shared_ptr<scripting::PoolImpl> clientScripts;
  189. #endif
  190. std::unique_ptr<events::EventBus> clientEventBus;
  191. void reinitScripting();
  192. };