Client.h 7.6 KB

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