Client.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. #pragma once
  2. #include "../lib/IGameCallback.h"
  3. #include "../lib/CondSh.h"
  4. #include "../lib/CStopWatch.h"
  5. /*
  6. * Client.h, part of VCMI engine
  7. *
  8. * Authors: listed in file AUTHORS in main folder
  9. *
  10. * License: GNU General Public License v2.0 or later
  11. * Full text of license available in license.txt file, in main folder
  12. *
  13. */
  14. class IGameEventsReceiver;
  15. class IBattleEventsReceiver;
  16. class CBattleGameInterface;
  17. struct StartInfo;
  18. class CGameState;
  19. class CGameInterface;
  20. class CConnection;
  21. class CCallback;
  22. struct BattleAction;
  23. struct SharedMem;
  24. class CClient;
  25. class CScriptingModule;
  26. struct CPathsInfo;
  27. namespace boost { class thread; }
  28. /// structure to handle running server and connecting to it
  29. class CServerHandler
  30. {
  31. private:
  32. void callServer(); //calls server via system(), should be called as thread
  33. public:
  34. CStopWatch th;
  35. boost::thread *serverThread; //thread that called system to run server
  36. SharedMem *shared; //interprocess memory (for waiting for server)
  37. bool verbose; //whether to print log msgs
  38. std::string port; //port number in text form
  39. //functions setting up local server
  40. void startServer(); //creates a thread with callServer
  41. void waitForServer(); //waits till server is ready
  42. CConnection * connectToServer(); //connects to server
  43. //////////////////////////////////////////////////////////////////////////
  44. static CConnection * justConnectToServer(const std::string &host = "", const std::string &port = ""); //connects to given host without taking any other actions (like setting up server)
  45. CServerHandler(bool runServer = false);
  46. ~CServerHandler();
  47. };
  48. template<typename T>
  49. class ThreadSafeVector
  50. {
  51. typedef std::vector<T> TVector;
  52. typedef boost::unique_lock<boost::mutex> TLock;
  53. TVector items;
  54. boost::mutex mx;
  55. boost::condition_variable cond;
  56. public:
  57. void pushBack(const T &item)
  58. {
  59. TLock lock(mx);
  60. items.push_back(item);
  61. cond.notify_all();
  62. }
  63. // //to access list, caller must present a lock used to lock mx
  64. // TVector &getList(TLock &lockedLock)
  65. // {
  66. // assert(lockedLock.owns_lock() && lockedLock.mutex() == &mx);
  67. // return items;
  68. // }
  69. TLock getLock()
  70. {
  71. return TLock(mx);
  72. }
  73. void waitWhileContains(const T &item)
  74. {
  75. auto lock = getLock();
  76. while(vstd::contains(items, item))
  77. cond.wait(lock);
  78. }
  79. bool tryRemovingElement(const T&item) //returns false if element was not present
  80. {
  81. auto lock = getLock();
  82. auto itr = vstd::find(items, item);
  83. if(itr == items.end()) //not in container
  84. {
  85. return false;
  86. }
  87. items.erase(itr);
  88. cond.notify_all();
  89. return true;
  90. }
  91. };
  92. /// Class which handles client - server logic
  93. class CClient : public IGameCallback
  94. {
  95. public:
  96. std::map<PlayerColor,shared_ptr<CCallback> > callbacks; //callbacks given to player interfaces
  97. std::map<PlayerColor,shared_ptr<CBattleCallback> > battleCallbacks; //callbacks given to player interfaces
  98. std::vector<shared_ptr<IGameEventsReceiver>> privilagedGameEventReceivers; //scripting modules, spectator interfaces
  99. std::vector<shared_ptr<IBattleEventsReceiver>> privilagedBattleEventReceivers; //scripting modules, spectator interfaces
  100. std::map<PlayerColor, shared_ptr<CGameInterface>> playerint;
  101. std::map<PlayerColor, shared_ptr<CBattleGameInterface>> battleints;
  102. std::map<PlayerColor,std::vector<shared_ptr<IGameEventsReceiver>>> additionalPlayerInts;
  103. std::map<PlayerColor,std::vector<shared_ptr<IBattleEventsReceiver>>> additionalBattleInts;
  104. bool hotSeat;
  105. CConnection *serv;
  106. boost::optional<BattleAction> curbaction;
  107. unique_ptr<CPathsInfo> pathInfo;
  108. boost::mutex pathMx; //protects the variable above
  109. CScriptingModule *erm;
  110. ThreadSafeVector<int> waitingRequest;
  111. void waitForMoveAndSend(PlayerColor color);
  112. //void sendRequest(const CPackForServer *request, bool waitForRealization);
  113. CClient(void);
  114. CClient(CConnection *con, StartInfo *si);
  115. ~CClient(void);
  116. void init();
  117. void newGame(CConnection *con, StartInfo *si); //con - connection to server
  118. void loadNeutralBattleAI();
  119. void installNewPlayerInterface(shared_ptr<CGameInterface> gameInterface, boost::optional<PlayerColor> color);
  120. void installNewBattleInterface(shared_ptr<CBattleGameInterface> battleInterface, boost::optional<PlayerColor> color, bool needCallback = true);
  121. std::string aiNameForPlayer(const PlayerSettings &ps, bool battleAI); //empty means no AI -> human
  122. void endGame(bool closeConnection = true);
  123. void stopConnection();
  124. void save(const std::string & fname);
  125. void loadGame(const std::string & fname);
  126. void run();
  127. void campaignMapFinished( shared_ptr<CCampaignState> camp );
  128. void finishCampaign( shared_ptr<CCampaignState> camp );
  129. void proposeNextMission(shared_ptr<CCampaignState> camp);
  130. void invalidatePaths(const CGHeroInstance *h = nullptr); //invalidates paths for hero h or for any hero if h is nullptr => they'll got recalculated when the next query comes
  131. void calculatePaths(const CGHeroInstance *h);
  132. void updatePaths(); //calls calculatePaths for same hero for which we previously calculated paths
  133. bool terminate; // tell to terminate
  134. boost::thread *connectionHandler; //thread running run() method
  135. //////////////////////////////////////////////////////////////////////////
  136. virtual PlayerColor getLocalPlayer() const override;
  137. //////////////////////////////////////////////////////////////////////////
  138. //not working yet, will be implement somewhen later with support for local-sim-based gameplay
  139. void changeSpells(const CGHeroInstance * hero, bool give, const std::set<SpellID> &spells) override {};
  140. bool removeObject(const CGObjectInstance * obj) override {return false;};
  141. void setBlockVis(ObjectInstanceID objid, bool bv) override {};
  142. void setOwner(const CGObjectInstance * obj, PlayerColor owner) override {};
  143. void setHoverName(const CGObjectInstance * obj, MetaString * name) override {};
  144. void changePrimSkill(const CGHeroInstance * hero, PrimarySkill::PrimarySkill which, si64 val, bool abs=false) override {};
  145. void changeSecSkill(const CGHeroInstance * hero, SecondarySkill which, int val, bool abs=false) override {};
  146. void showBlockingDialog(BlockingDialog *iw) override {};
  147. void showGarrisonDialog(ObjectInstanceID upobj, ObjectInstanceID hid, bool removableUnits) override {};
  148. void showThievesGuildWindow(PlayerColor player, ObjectInstanceID requestingObjId) override {};
  149. void giveResource(PlayerColor player, Res::ERes which, int val) override {};
  150. virtual void giveResources(PlayerColor player, TResources resources) override {};
  151. void giveCreatures(const CArmedInstance * objid, const CGHeroInstance * h, const CCreatureSet &creatures, bool remove) override {};
  152. void takeCreatures(ObjectInstanceID objid, const std::vector<CStackBasicDescriptor> &creatures) override {};
  153. bool changeStackType(const StackLocation &sl, CCreature *c) override {return false;};
  154. bool changeStackCount(const StackLocation &sl, TQuantity count, bool absoluteValue = false) override {return false;};
  155. bool insertNewStack(const StackLocation &sl, const CCreature *c, TQuantity count) override {return false;};
  156. bool eraseStack(const StackLocation &sl, bool forceRemoval = false){return false;};
  157. bool swapStacks(const StackLocation &sl1, const StackLocation &sl2) override {return false;}
  158. bool addToSlot(const StackLocation &sl, const CCreature *c, TQuantity count) override {return false;}
  159. void tryJoiningArmy(const CArmedInstance *src, const CArmedInstance *dst, bool removeObjWhenFinished, bool allowMerging) override {}
  160. bool moveStack(const StackLocation &src, const StackLocation &dst, TQuantity count = -1) override {return false;}
  161. void removeAfterVisit(const CGObjectInstance *object) override {};
  162. void giveHeroNewArtifact(const CGHeroInstance *h, const CArtifact *artType, ArtifactPosition pos) override {};
  163. void giveHeroArtifact(const CGHeroInstance *h, const CArtifactInstance *a, ArtifactPosition pos) override {};
  164. void putArtifact(const ArtifactLocation &al, const CArtifactInstance *a) override {};
  165. void removeArtifact(const ArtifactLocation &al) override {};
  166. bool moveArtifact(const ArtifactLocation &al1, const ArtifactLocation &al2) override {return false;};
  167. void synchronizeArtifactHandlerLists() override {};
  168. void showCompInfo(ShowInInfobox * comp) override {};
  169. void heroVisitCastle(const CGTownInstance * obj, const CGHeroInstance * hero) override {};
  170. void stopHeroVisitCastle(const CGTownInstance * obj, const CGHeroInstance * hero) override {};
  171. //void giveHeroArtifact(int artid, int hid, int position){};
  172. //void giveNewArtifact(int hid, int position){};
  173. 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
  174. void startBattleI(const CArmedInstance *army1, const CArmedInstance *army2, int3 tile, bool creatureBank = false) override {}; //if any of armies is hero, hero will be used
  175. 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
  176. void setAmount(ObjectInstanceID objid, ui32 val) override {};
  177. bool moveHero(ObjectInstanceID hid, int3 dst, ui8 teleporting, PlayerColor asker = PlayerColor::NEUTRAL) override {return false;};
  178. void giveHeroBonus(GiveBonus * bonus) override {};
  179. void setMovePoints(SetMovePoints * smp) override {};
  180. void setManaPoints(ObjectInstanceID hid, int val) override {};
  181. void giveHero(ObjectInstanceID id, PlayerColor player) override {};
  182. void changeObjPos(ObjectInstanceID objid, int3 newPos, ui8 flags) override {};
  183. void sendAndApply(CPackForClient * info) override {};
  184. void heroExchange(ObjectInstanceID hero1, ObjectInstanceID hero2) override {};
  185. //////////////////////////////////////////////////////////////////////////
  186. friend class CCallback; //handling players actions
  187. friend class CBattleCallback; //handling players actions
  188. int sendRequest(const CPack *request, PlayerColor player); //returns ID given to that request
  189. void handlePack( CPack * pack ); //applies the given pack and deletes it
  190. void battleStarted(const BattleInfo * info);
  191. void commenceTacticPhaseForInt(shared_ptr<CBattleGameInterface> battleInt); //will be called as separate thread
  192. void commitPackage(CPackForClient *pack) override;
  193. //////////////////////////////////////////////////////////////////////////
  194. template <typename Handler> void serialize(Handler &h, const int version);
  195. void battleFinished();
  196. };