Client.h 6.8 KB

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