Client.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. void startServer(); //creates a thread with callServer
  40. void waitForServer(); //waits till server is ready
  41. CConnection * connectToServer(); //connects to server
  42. CServerHandler();
  43. ~CServerHandler();
  44. };
  45. class CClient : public IGameCallback
  46. {
  47. public:
  48. CCallback *cb;
  49. std::set<CCallback*> callbacks; //callbacks given to player interfaces
  50. std::map<ui8,CGameInterface *> playerint;
  51. bool hotSeat;
  52. CConnection *serv;
  53. BattleAction *curbaction;
  54. CPathsInfo *pathInfo;
  55. CondSh<bool> waitingRequest;
  56. std::queue<CPack *> packs;
  57. boost::mutex packsM;
  58. void waitForMoveAndSend(int color);
  59. //void sendRequest(const CPackForServer *request, bool waitForRealization);
  60. CClient(void);
  61. CClient(CConnection *con, StartInfo *si);
  62. ~CClient(void);
  63. void init();
  64. void newGame(CConnection *con, StartInfo *si); //con - connection to server
  65. void endGame(bool closeConnection = true);
  66. void stopConnection();
  67. void save(const std::string & fname);
  68. void loadGame(const std::string & fname);
  69. void run();
  70. void finishCampaign( CCampaignState * camp );
  71. void proposeNextMission( CCampaignState * camp );
  72. bool terminate; // tell to terminate
  73. boost::thread *connectionHandler; //thread running run() method
  74. //////////////////////////////////////////////////////////////////////////
  75. //from IGameCallback
  76. int getCurrentPlayer();
  77. int getSelectedHero();
  78. //not working yet, will be implement somewhen later with support for local-sim-based gameplay
  79. void changeSpells(int hid, bool give, const std::set<ui32> &spells){};
  80. bool removeObject(int objid){return false;};
  81. void setBlockVis(int objid, bool bv){};
  82. void setOwner(int objid, ui8 owner){};
  83. void setHoverName(int objid, MetaString * name){};
  84. void setObjProperty(int objid, int prop, si64 val){};
  85. void changePrimSkill(int ID, int which, si64 val, bool abs=false){};
  86. void changeSecSkill(int ID, int which, int val, bool abs=false){};
  87. void showInfoDialog(InfoWindow *iw){};
  88. void showBlockingDialog(BlockingDialog *iw, const CFunctionList<void(ui32)> &callback){};
  89. ui32 showBlockingDialog(BlockingDialog *iw){return 0;}; //synchronous version of above
  90. void showGarrisonDialog(int upobj, int hid, bool removableUnits, const boost::function<void()> &cb){};
  91. void showThievesGuildWindow(int requestingObjId){};
  92. void giveResource(int player, int which, int val){};
  93. void giveCreatures (int objid, const CGHeroInstance * h, CCreatureSet creatures) {};
  94. void takeCreatures (int objid, TSlots creatures){};
  95. void changeCreatureType (int objid, TSlot slot, TCreature creature){};
  96. void showCompInfo(ShowInInfobox * comp){};
  97. void heroVisitCastle(int obj, int heroID){};
  98. void stopHeroVisitCastle(int obj, int heroID){};
  99. void giveHeroArtifact(int artid, int hid, int position){}; //pos==-1 - first free slot in backpack=0; pos==-2 - default if available or backpack
  100. bool removeArtifact(int artid, int hid){return false;};
  101. 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
  102. 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
  103. 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
  104. void setAmount(int objid, ui32 val){};
  105. bool moveHero(si32 hid, int3 dst, ui8 instant, ui8 asker = 255){return false;};
  106. void giveHeroBonus(GiveBonus * bonus){};
  107. void setMovePoints(SetMovePoints * smp){};
  108. void setManaPoints(int hid, int val){};
  109. void giveHero(int id, int player){};
  110. void changeObjPos(int objid, int3 newPos, ui8 flags){};
  111. void sendAndApply(CPackForClient * info){};
  112. void heroExchange(si32 hero1, si32 hero2){};
  113. //////////////////////////////////////////////////////////////////////////
  114. friend class CCallback; //handling players actions
  115. friend void processCommand(const std::string &message, CClient *&client); //handling console
  116. void handlePack( CPack * pack ); //applies the given pack and deletes it
  117. void updatePaths();
  118. //////////////////////////////////////////////////////////////////////////
  119. template <typename Handler> void serialize(Handler &h, const int version);
  120. };
  121. #endif // __CLIENT_H__