Client.h 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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){};
  85. bool removeObject(int objid){return false;};
  86. void setBlockVis(int objid, bool bv){};
  87. void setOwner(int objid, ui8 owner){};
  88. void setHoverName(int objid, MetaString * name){};
  89. void setObjProperty(int objid, int prop, si64 val){};
  90. void changePrimSkill(int ID, int which, si64 val, bool abs=false){};
  91. void changeSecSkill(int ID, int which, int val, bool abs=false){};
  92. void showInfoDialog(InfoWindow *iw){};
  93. void showBlockingDialog(BlockingDialog *iw, const CFunctionList<void(ui32)> &callback){};
  94. ui32 showBlockingDialog(BlockingDialog *iw){return 0;}; //synchronous version of above
  95. void showGarrisonDialog(int upobj, int hid, bool removableUnits, const boost::function<void()> &cb){};
  96. void showThievesGuildWindow(int requestingObjId){};
  97. void giveResource(int player, int which, int val){};
  98. void giveCreatures (const CArmedInstance * objid, const CGHeroInstance * h, const CCreatureSet &creatures, bool remove) {};
  99. void takeCreatures (int objid, TSlots creatures){};
  100. void takeCreatures (int objid, std::vector<CStackBasicDescriptor> creatures){};
  101. bool changeStackType(const StackLocation &sl, CCreature *c){return false;};
  102. bool changeStackCount(const StackLocation &sl, TQuantity count, bool absoluteValue = false){return false;};
  103. bool insertNewStack(const StackLocation &sl, const CCreature *c, TQuantity count){return false;};
  104. bool eraseStack(const StackLocation &sl, bool forceRemoval = false){return false;};
  105. bool swapStacks(const StackLocation &sl1, const StackLocation &sl2){return false;}
  106. bool addToSlot(const StackLocation &sl, const CCreature *c, TQuantity count){return false;}
  107. void tryJoiningArmy(const CArmedInstance *src, const CArmedInstance *dst, bool removeObjWhenFinished, bool allowMerging){}
  108. bool moveStack(const StackLocation &src, const StackLocation &dst, TQuantity count = -1){return false;}
  109. void showCompInfo(ShowInInfobox * comp){};
  110. void heroVisitCastle(int obj, int heroID){};
  111. void stopHeroVisitCastle(int obj, int heroID){};
  112. void giveHeroArtifact(int artid, int hid, int position){}; //pos==-1 - first free slot in backpack=0; pos==-2 - default if available or backpack
  113. void giveNewArtifact(int hid, int position){};
  114. bool removeArtifact(const CArtifact* art, int hid){return false;};
  115. 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){}; //use hero=NULL for no hero
  116. void startBattleI(const CArmedInstance *army1, const CArmedInstance *army2, int3 tile, boost::function<void(BattleResult*)> cb = 0, bool creatureBank = false){}; //if any of armies is hero, hero will be used
  117. void startBattleI(const CArmedInstance *army1, const CArmedInstance *army2, boost::function<void(BattleResult*)> cb = 0, bool creatureBank = false){}; //if any of armies is hero, hero will be used, visitable tile of second obj is place of battle
  118. void setAmount(int objid, ui32 val){};
  119. bool moveHero(si32 hid, int3 dst, ui8 instant, ui8 asker = 255){return false;};
  120. void giveHeroBonus(GiveBonus * bonus){};
  121. void setMovePoints(SetMovePoints * smp){};
  122. void setManaPoints(int hid, int val){};
  123. void giveHero(int id, int player){};
  124. void changeObjPos(int objid, int3 newPos, ui8 flags){};
  125. void sendAndApply(CPackForClient * info){};
  126. void heroExchange(si32 hero1, si32 hero2){};
  127. //////////////////////////////////////////////////////////////////////////
  128. friend class CCallback; //handling players actions
  129. friend void processCommand(const std::string &message, CClient *&client); //handling console
  130. void handlePack( CPack * pack ); //applies the given pack and deletes it
  131. void updatePaths();
  132. //////////////////////////////////////////////////////////////////////////
  133. template <typename Handler> void serialize(Handler &h, const int version);
  134. };
  135. #endif // __CLIENT_H__