Client.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. #ifndef __CLIENT_H__
  2. #define __CLIENT_H__
  3. #include "../global.h"
  4. #include <boost/thread.hpp>
  5. #include "../lib/IGameCallback.h"
  6. #include "../lib/CondSh.h"
  7. #include <queue>
  8. /*
  9. * Client.h, part of VCMI engine
  10. *
  11. * Authors: listed in file AUTHORS in main folder
  12. *
  13. * License: GNU General Public License v2.0 or later
  14. * Full text of license available in license.txt file, in main folder
  15. *
  16. */
  17. class IGameEventsReceiver;
  18. class IBattleEventsReceiver;
  19. class CBattleGameInterface;
  20. struct StartInfo;
  21. class CGameState;
  22. class CGameInterface;
  23. class CConnection;
  24. class CCallback;
  25. struct BattleAction;
  26. struct SharedMem;
  27. class CClient;
  28. class CScriptingModule;
  29. struct CPathsInfo;
  30. namespace boost { class thread; }
  31. void processCommand(const std::string &message, CClient *&client);
  32. /// structure to handle running server and connecting to it
  33. class CServerHandler
  34. {
  35. private:
  36. void callServer(); //calls server via system(), should be called as thread
  37. public:
  38. timeHandler th;
  39. boost::thread *serverThread; //thread that called system to run server
  40. SharedMem *shared; //interprocess memory (for waiting for server)
  41. bool verbose; //whether to print log msgs
  42. std::string port; //port number in text form
  43. //functions setting up local server
  44. void startServer(); //creates a thread with callServer
  45. void waitForServer(); //waits till server is ready
  46. CConnection * connectToServer(); //connects to server
  47. //////////////////////////////////////////////////////////////////////////
  48. static CConnection * justConnectToServer(const std::string &host = "", const std::string &port = ""); //connects to given host without taking any other actions (like setting up server)
  49. CServerHandler(bool runServer = false);
  50. ~CServerHandler();
  51. };
  52. /// Class which handles client - server logic
  53. class CClient : public IGameCallback
  54. {
  55. public:
  56. CCallback *cb;
  57. std::set<CCallback*> callbacks; //callbacks given to player interfaces
  58. std::vector<IGameEventsReceiver*> privilagedGameEventReceivers; //scripting modules, spectator interfaces
  59. std::vector<IBattleEventsReceiver*> privilagedBattleEventReceivers; //scripting modules, spectator interfaces
  60. std::map<ui8,CGameInterface *> playerint;
  61. std::map<ui8,CBattleGameInterface *> battleints;
  62. bool hotSeat;
  63. CConnection *serv;
  64. BattleAction *curbaction;
  65. CPathsInfo *pathInfo;
  66. boost::mutex pathMx; //protects the variable above
  67. CScriptingModule *erm;
  68. CondSh<bool> waitingRequest;
  69. std::queue<CPack *> packs;
  70. boost::mutex packsM;
  71. void waitForMoveAndSend(int color);
  72. //void sendRequest(const CPackForServer *request, bool waitForRealization);
  73. CClient(void);
  74. CClient(CConnection *con, StartInfo *si);
  75. ~CClient(void);
  76. void init();
  77. void newGame(CConnection *con, StartInfo *si); //con - connection to server
  78. void loadNeutralBattleAI();
  79. void endGame(bool closeConnection = true);
  80. void stopConnection();
  81. void save(const std::string & fname);
  82. void loadGame(const std::string & fname);
  83. void run();
  84. void finishCampaign( CCampaignState * camp );
  85. void proposeNextMission( CCampaignState * camp );
  86. void calculatePaths(const CGHeroInstance *h);
  87. void updatePaths(); //calls calculatePaths for same hero for which we previously calculated paths
  88. bool terminate; // tell to terminate
  89. boost::thread *connectionHandler; //thread running run() method
  90. //////////////////////////////////////////////////////////////////////////
  91. virtual int getLocalPlayer() const OVERRIDE;
  92. //////////////////////////////////////////////////////////////////////////
  93. //not working yet, will be implement somewhen later with support for local-sim-based gameplay
  94. void changeSpells(int hid, bool give, const std::set<ui32> &spells) OVERRIDE {};
  95. bool removeObject(int objid) OVERRIDE {return false;};
  96. void setBlockVis(int objid, bool bv) OVERRIDE {};
  97. void setOwner(int objid, ui8 owner) OVERRIDE {};
  98. void setHoverName(int objid, MetaString * name) OVERRIDE {};
  99. void changePrimSkill(int ID, int which, si64 val, bool abs=false) OVERRIDE {};
  100. void changeSecSkill(int ID, int which, int val, bool abs=false) OVERRIDE {};
  101. void showBlockingDialog(BlockingDialog *iw, const CFunctionList<void(ui32)> &callback) OVERRIDE {};
  102. ui32 showBlockingDialog(BlockingDialog *iw) OVERRIDE {return 0;}; //synchronous version of above
  103. void showGarrisonDialog(int upobj, int hid, bool removableUnits, const boost::function<void()> &cb) OVERRIDE {};
  104. void showThievesGuildWindow(int requestingObjId) OVERRIDE {};
  105. void giveResource(int player, int which, int val) OVERRIDE {};
  106. void giveCreatures(const CArmedInstance * objid, const CGHeroInstance * h, const CCreatureSet &creatures, bool remove) OVERRIDE {};
  107. void takeCreatures(int objid, const std::vector<CStackBasicDescriptor> &creatures) OVERRIDE {};
  108. bool changeStackType(const StackLocation &sl, CCreature *c) OVERRIDE {return false;};
  109. bool changeStackCount(const StackLocation &sl, TQuantity count, bool absoluteValue = false) OVERRIDE {return false;};
  110. bool insertNewStack(const StackLocation &sl, const CCreature *c, TQuantity count) OVERRIDE {return false;};
  111. bool eraseStack(const StackLocation &sl, bool forceRemoval = false){return false;};
  112. bool swapStacks(const StackLocation &sl1, const StackLocation &sl2) OVERRIDE {return false;}
  113. bool addToSlot(const StackLocation &sl, const CCreature *c, TQuantity count) OVERRIDE {return false;}
  114. void tryJoiningArmy(const CArmedInstance *src, const CArmedInstance *dst, bool removeObjWhenFinished, bool allowMerging) OVERRIDE {}
  115. bool moveStack(const StackLocation &src, const StackLocation &dst, TQuantity count = -1) OVERRIDE {return false;}
  116. void giveHeroNewArtifact(const CGHeroInstance *h, const CArtifact *artType, int pos) OVERRIDE {};
  117. void giveHeroArtifact(const CGHeroInstance *h, const CArtifactInstance *a, int pos) OVERRIDE {};
  118. void putArtifact(const ArtifactLocation &al, const CArtifactInstance *a) OVERRIDE {};
  119. void removeArtifact(const ArtifactLocation &al) OVERRIDE {};
  120. void moveArtifact(const ArtifactLocation &al1, const ArtifactLocation &al2) OVERRIDE {};
  121. void showCompInfo(ShowInInfobox * comp) OVERRIDE {};
  122. void heroVisitCastle(int obj, int heroID) OVERRIDE {};
  123. void stopHeroVisitCastle(int obj, int heroID) OVERRIDE {};
  124. //void giveHeroArtifact(int artid, int hid, int position){};
  125. //void giveNewArtifact(int hid, int position){};
  126. void startBattleI(const CArmedInstance *army1, const CArmedInstance *army2, int3 tile, const CGHeroInstance *hero1, const CGHeroInstance *hero2, bool creatureBank = false, boost::function<void(BattleResult*)> cb = 0, const CGTownInstance *town = NULL) OVERRIDE {}; //use hero=NULL for no hero
  127. void startBattleI(const CArmedInstance *army1, const CArmedInstance *army2, int3 tile, boost::function<void(BattleResult*)> cb = 0, bool creatureBank = false) OVERRIDE {}; //if any of armies is hero, hero will be used
  128. void startBattleI(const CArmedInstance *army1, const CArmedInstance *army2, boost::function<void(BattleResult*)> cb = 0, bool creatureBank = false) OVERRIDE {}; //if any of armies is hero, hero will be used, visitable tile of second obj is place of battle
  129. void setAmount(int objid, ui32 val) OVERRIDE {};
  130. bool moveHero(si32 hid, int3 dst, ui8 instant, ui8 asker = 255) OVERRIDE {return false;};
  131. void giveHeroBonus(GiveBonus * bonus) OVERRIDE {};
  132. void setMovePoints(SetMovePoints * smp) OVERRIDE {};
  133. void setManaPoints(int hid, int val) OVERRIDE {};
  134. void giveHero(int id, int player) OVERRIDE {};
  135. void changeObjPos(int objid, int3 newPos, ui8 flags) OVERRIDE {};
  136. void sendAndApply(CPackForClient * info) OVERRIDE {};
  137. void heroExchange(si32 hero1, si32 hero2) OVERRIDE {};
  138. //////////////////////////////////////////////////////////////////////////
  139. friend class CCallback; //handling players actions
  140. friend void processCommand(const std::string &message, CClient *&client); //handling console
  141. void handlePack( CPack * pack ); //applies the given pack and deletes it
  142. void battleStarted(const BattleInfo * info);
  143. void commitPackage(CPackForClient *pack) OVERRIDE;
  144. //////////////////////////////////////////////////////////////////////////
  145. template <typename Handler> void serialize(Handler &h, const int version);
  146. };
  147. #endif // __CLIENT_H__