Client.h 8.5 KB

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