Client.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. #ifndef __CLIENT_H__
  2. #define __CLIENT_H__
  3. #include "../global.h"
  4. #include "../lib/IGameCallback.h"
  5. #include <queue>
  6. #include "../lib/CBattleCallback.h"
  7. /*
  8. * Client.h, part of VCMI engine
  9. *
  10. * Authors: listed in file AUTHORS in main folder
  11. *
  12. * License: GNU General Public License v2.0 or later
  13. * Full text of license available in license.txt file, in main folder
  14. *
  15. */
  16. class IGameEventsReceiver;
  17. class IBattleEventsReceiver;
  18. class CBattleGameInterface;
  19. struct StartInfo;
  20. class CGameState;
  21. class CGameInterface;
  22. class CConnection;
  23. class CCallback;
  24. struct BattleAction;
  25. struct SharedMem;
  26. class CClient;
  27. class CScriptingModule;
  28. struct CPathsInfo;
  29. namespace boost { class thread; }
  30. class CLoadFile;
  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, public IConnectionHandler
  54. {
  55. public:
  56. CCallback *cb;
  57. std::map<ui8,boost::shared_ptr<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. BattleAction *curbaction;
  64. CPathsInfo *pathInfo;
  65. boost::mutex pathMx; //protects the variable above
  66. CScriptingModule *erm;
  67. std::queue<CPack *> packs;
  68. boost::mutex packsM;
  69. void waitForMoveAndSend(int color);
  70. //void sendRequest(const CPackForServer *request, bool waitForRealization);
  71. CClient(void);
  72. CClient(CConnection *con, StartInfo *si);
  73. ~CClient(void);
  74. void init();
  75. void newGame(CConnection *con, StartInfo *si); //con - connection to server
  76. void newDuel(CConnection *con, StartInfo *si); //con - connection to server
  77. void loadNeutralBattleAI();
  78. void endGame(bool closeConnection = true);
  79. void stopConnection();
  80. void save(const std::string & fname);
  81. void loadGame(const std::string & fname);
  82. void run();
  83. void runReplay(CLoadFile *f);
  84. void finishCampaign( CCampaignState * camp );
  85. void proposeNextMission( CCampaignState * camp );
  86. void invalidatePaths(const CGHeroInstance *h = NULL); //invalidates paths for hero h or for any hero if h is NULL => they'll got recalculated when the next query comes
  87. void calculatePaths(const CGHeroInstance *h);
  88. void updatePaths(); //calls calculatePaths for same hero for which we previously calculated paths
  89. bool terminate; // tell to terminate
  90. boost::thread *connectionHandler; //thread running run() method
  91. //////////////////////////////////////////////////////////////////////////
  92. virtual int getLocalPlayer() const OVERRIDE;
  93. //////////////////////////////////////////////////////////////////////////
  94. //not working yet, will be implement somewhen later with support for local-sim-based gameplay
  95. void changeSpells(int hid, bool give, const std::set<ui32> &spells) OVERRIDE {};
  96. bool removeObject(int objid) OVERRIDE {return false;};
  97. void setBlockVis(int objid, bool bv) OVERRIDE {};
  98. void setOwner(int objid, ui8 owner) OVERRIDE {};
  99. void setHoverName(int objid, MetaString * name) OVERRIDE {};
  100. void changePrimSkill(int ID, int which, si64 val, bool abs=false) OVERRIDE {};
  101. void changeSecSkill(int ID, int which, int val, bool abs=false) OVERRIDE {};
  102. void showBlockingDialog(BlockingDialog *iw, const CFunctionList<void(ui32)> &callback) OVERRIDE {};
  103. ui32 showBlockingDialog(BlockingDialog *iw) OVERRIDE {return 0;}; //synchronous version of above
  104. void showGarrisonDialog(int upobj, int hid, bool removableUnits, const boost::function<void()> &cb) OVERRIDE {};
  105. void showThievesGuildWindow(int requestingObjId) OVERRIDE {};
  106. void giveResource(int player, int which, int val) OVERRIDE {};
  107. void giveCreatures(const CArmedInstance * objid, const CGHeroInstance * h, const CCreatureSet &creatures, bool remove) OVERRIDE {};
  108. void takeCreatures(int objid, const std::vector<CStackBasicDescriptor> &creatures) OVERRIDE {};
  109. bool changeStackType(const StackLocation &sl, CCreature *c) OVERRIDE {return false;};
  110. bool changeStackCount(const StackLocation &sl, TQuantity count, bool absoluteValue = false) OVERRIDE {return false;};
  111. bool insertNewStack(const StackLocation &sl, const CCreature *c, TQuantity count) OVERRIDE {return false;};
  112. bool eraseStack(const StackLocation &sl, bool forceRemoval = false){return false;};
  113. bool swapStacks(const StackLocation &sl1, const StackLocation &sl2) OVERRIDE {return false;}
  114. bool addToSlot(const StackLocation &sl, const CCreature *c, TQuantity count) OVERRIDE {return false;}
  115. void tryJoiningArmy(const CArmedInstance *src, const CArmedInstance *dst, bool removeObjWhenFinished, bool allowMerging) OVERRIDE {}
  116. bool moveStack(const StackLocation &src, const StackLocation &dst, TQuantity count = -1) OVERRIDE {return false;}
  117. void giveHeroNewArtifact(const CGHeroInstance *h, const CArtifact *artType, int pos) OVERRIDE {};
  118. void giveHeroArtifact(const CGHeroInstance *h, const CArtifactInstance *a, int pos) OVERRIDE {};
  119. void putArtifact(const ArtifactLocation &al, const CArtifactInstance *a) OVERRIDE {};
  120. void removeArtifact(const ArtifactLocation &al) OVERRIDE {};
  121. void moveArtifact(const ArtifactLocation &al1, const ArtifactLocation &al2) OVERRIDE {};
  122. void showCompInfo(ShowInInfobox * comp) OVERRIDE {};
  123. void heroVisitCastle(int obj, int heroID) OVERRIDE {};
  124. void stopHeroVisitCastle(int obj, int heroID) OVERRIDE {};
  125. //void giveHeroArtifact(int artid, int hid, int position){};
  126. //void giveNewArtifact(int hid, int position){};
  127. 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
  128. 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
  129. 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
  130. void setAmount(int objid, ui32 val) OVERRIDE {};
  131. bool moveHero(si32 hid, int3 dst, ui8 instant, ui8 asker = 255) OVERRIDE {return false;};
  132. void giveHeroBonus(GiveBonus * bonus) OVERRIDE {};
  133. void setMovePoints(SetMovePoints * smp) OVERRIDE {};
  134. void setManaPoints(int hid, int val) OVERRIDE {};
  135. void giveHero(int id, int player) OVERRIDE {};
  136. void changeObjPos(int objid, int3 newPos, ui8 flags) OVERRIDE {};
  137. void sendAndApply(CPackForClient * info) OVERRIDE {};
  138. void heroExchange(si32 hero1, si32 hero2) OVERRIDE {};
  139. //////////////////////////////////////////////////////////////////////////
  140. friend class CCallback; //handling players actions
  141. friend class CBattleCallback; //handling players actions
  142. friend void processCommand(const std::string &message, CClient *&client); //handling console
  143. void handlePack( CPack * pack ); //applies the given pack and deletes it
  144. void battleStarted(const BattleInfo * info);
  145. void commenceTacticPhaseForInt(CBattleGameInterface *battleInt); //will be called as separate thread
  146. void commitPackage(CPackForClient *pack) OVERRIDE;
  147. //////////////////////////////////////////////////////////////////////////
  148. template <typename Handler> void serialize(Handler &h, const int version);
  149. };
  150. #endif // __CLIENT_H__