Client.cpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. #include "../CCallback.h"
  2. #include "../CConsoleHandler.h"
  3. #include "../CGameInfo.h"
  4. #include "../CGameState.h"
  5. #include "../CPlayerInterface.h"
  6. #include "../StartInfo.h"
  7. #include "../hch/CArtHandler.h"
  8. #include "../hch/CDefObjInfoHandler.h"
  9. #include "../hch/CGeneralTextHandler.h"
  10. #include "../hch/CHeroHandler.h"
  11. #include "../hch/CTownHandler.h"
  12. #include "../hch/CObjectHandler.h"
  13. #include "../hch/CBuildingHandler.h"
  14. #include "../hch/CSpellHandler.h"
  15. #include "../lib/Connection.h"
  16. #include "../lib/Interprocess.h"
  17. #include "../lib/NetPacks.h"
  18. #include "../lib/VCMI_Lib.h"
  19. #include "../map.h"
  20. #include "../mapHandler.h"
  21. #include "CConfigHandler.h"
  22. #include "Client.h"
  23. #include <boost/bind.hpp>
  24. #include <boost/foreach.hpp>
  25. #include <boost/thread.hpp>
  26. #include <boost/thread/shared_mutex.hpp>
  27. #include <sstream>
  28. #undef DLL_EXPORT
  29. #define DLL_EXPORT
  30. #include "../lib/RegisterTypes.h"
  31. extern std::string NAME;
  32. namespace intpr = boost::interprocess;
  33. class CBaseForCLApply
  34. {
  35. public:
  36. virtual void applyOnClAfter(CClient *cl, void *pack) const =0;
  37. virtual void applyOnClBefore(CClient *cl, void *pack) const =0;
  38. };
  39. template <typename T> class CApplyOnCL : public CBaseForCLApply
  40. {
  41. public:
  42. void applyOnClAfter(CClient *cl, void *pack) const
  43. {
  44. T *ptr = static_cast<T*>(pack);
  45. ptr->applyCl(cl);
  46. }
  47. void applyOnClBefore(CClient *cl, void *pack) const
  48. {
  49. T *ptr = static_cast<T*>(pack);
  50. ptr->applyFirstCl(cl);
  51. }
  52. };
  53. class CCLApplier
  54. {
  55. public:
  56. std::map<ui16,CBaseForCLApply*> apps;
  57. CCLApplier()
  58. {
  59. registerTypes2(*this);
  60. }
  61. template<typename T> void registerType(const T * t=NULL)
  62. {
  63. ui16 ID = typeList.registerType(&typeid(T));
  64. apps[ID] = new CApplyOnCL<T>;
  65. }
  66. } applier;
  67. void CClient::init()
  68. {
  69. IObjectInterface::cb = this;
  70. serv = NULL;
  71. gs = NULL;
  72. cb = NULL;
  73. try
  74. {
  75. shared = new SharedMem();
  76. } HANDLE_EXCEPTION
  77. }
  78. CClient::CClient(void)
  79. {
  80. init();
  81. }
  82. CClient::CClient(CConnection *con, StartInfo *si)
  83. {
  84. init();
  85. newGame(con,si);
  86. }
  87. CClient::~CClient(void)
  88. {
  89. delete shared;
  90. }
  91. void CClient::waitForMoveAndSend(int color)
  92. {
  93. try
  94. {
  95. BattleAction ba = playerint[color]->activeStack(gs->curB->activeStack);
  96. *serv << ui16(3002) << ba;
  97. return;
  98. }HANDLE_EXCEPTION
  99. tlog1 << "We should not be here!" << std::endl;
  100. }
  101. void CClient::run()
  102. {
  103. CPack *pack;
  104. try
  105. {
  106. *serv >> pack;
  107. CBaseForCLApply *apply = applier.apps[typeList.getTypeID(pack)];
  108. apply->applyOnClBefore(this,pack);
  109. gs->apply(pack);
  110. apply->applyOnClAfter(this,pack);
  111. delete pack;
  112. pack = NULL;
  113. } HANDLE_EXCEPTION
  114. }
  115. void CClient::close()
  116. {
  117. if(!serv)
  118. return;
  119. tlog3 << "Connection has been requested to be closed.\n";
  120. boost::unique_lock<boost::mutex>(*serv->wmx);
  121. *serv << ui16(99);
  122. tlog3 << "Sent closing signal to the server\n";
  123. serv->close();
  124. tlog3 << "Our socket has been closed.\n";
  125. }
  126. void CClient::save(const std::string & fname)
  127. {
  128. if(gs->curB)
  129. {
  130. tlog1 << "Game cannot be saved during battle!\n";
  131. return;
  132. }
  133. *serv << ui16(98) << fname;
  134. }
  135. void CClient::load( const std::string & fname )
  136. {
  137. tlog0 <<"\n\nLoading procedure started!\n\n";
  138. timeHandler tmh;
  139. close(); //kill server
  140. tlog0 <<"Sent kill signal to the server: "<<tmh.getDif()<<std::endl;
  141. VLC->clear(); //delete old handlers
  142. delete CGI->mh;
  143. delete CGI->state;
  144. //TODO: del callbacks
  145. for(std::map<ui8,CGameInterface *>::iterator i = playerint.begin(); i!=playerint.end(); i++)
  146. {
  147. delete i->second; //delete player interfaces
  148. }
  149. tlog0 <<"Deleting old data: "<<tmh.getDif()<<std::endl;
  150. char portc[10];
  151. SDL_itoa(conf.cc.port,portc,10);
  152. runServer(portc); //create new server
  153. tlog0 <<"Restarting server: "<<tmh.getDif()<<std::endl;
  154. {
  155. ui32 ver;
  156. char sig[8];
  157. CMapHeader dum;
  158. CGI->mh = new CMapHandler();
  159. CLoadFile lf(fname + ".vlgm1");
  160. lf >> sig >> ver >> dum >> *sig;
  161. tlog0 <<"Reading save signature: "<<tmh.getDif()<<std::endl;
  162. lf >> *VLC;
  163. CGI->setFromLib();
  164. tlog0 <<"Reading handlers: "<<tmh.getDif()<<std::endl;
  165. lf >> gs;
  166. tlog0 <<"Reading gamestate: "<<tmh.getDif()<<std::endl;
  167. CGI->state = gs;
  168. CGI->mh->map = gs->map;
  169. CGI->mh->init();
  170. tlog0 <<"Initing maphandler: "<<tmh.getDif()<<std::endl;
  171. }
  172. waitForServer();
  173. tlog0 <<"Waiting for server: "<<tmh.getDif()<<std::endl;
  174. serv = new CConnection(conf.cc.server,portc,NAME);
  175. tlog0 <<"Setting up connection: "<<tmh.getDif()<<std::endl;
  176. ui8 pom8;
  177. *serv << ui8(3) << ui8(1); //load game; one client
  178. *serv << fname;
  179. *serv >> pom8;
  180. if(pom8)
  181. throw "Server cannot open the savegame!";
  182. else
  183. tlog0 << "Server opened savegame properly.\n";
  184. *serv << ui8(gs->scenarioOps->playerInfos.size()+1); //number of players + neutral
  185. for(size_t i=0;i<gs->scenarioOps->playerInfos.size();i++)
  186. {
  187. *serv << ui8(gs->scenarioOps->playerInfos[i].color); //players
  188. }
  189. *serv << ui8(255); // neutrals
  190. tlog0 <<"Sent info to server: "<<tmh.getDif()<<std::endl;
  191. for (size_t i=0; i<gs->scenarioOps->playerInfos.size();++i) //initializing interfaces for players
  192. {
  193. ui8 color = gs->scenarioOps->playerInfos[i].color;
  194. CCallback *cb = new CCallback(gs,color,this);
  195. if(!gs->scenarioOps->playerInfos[i].human) {
  196. playerint[color] = static_cast<CGameInterface*>(CAIHandler::getNewAI(cb,conf.cc.defaultAI));
  197. }
  198. else {
  199. playerint[color] = new CPlayerInterface(color,i);
  200. }
  201. gs->currentPlayer = color;
  202. playerint[color]->init(cb);
  203. tlog0 <<"Setting up interface for player "<< (int)color <<": "<<tmh.getDif()<<std::endl;
  204. }
  205. playerint[255] = CAIHandler::getNewAI(cb,conf.cc.defaultAI);
  206. playerint[255]->init(new CCallback(gs,255,this));
  207. tlog0 <<"Setting up interface for neutral \"player\"" << tmh.getDif() << std::endl;
  208. }
  209. int CClient::getCurrentPlayer()
  210. {
  211. return gs->currentPlayer;
  212. }
  213. int CClient::getSelectedHero()
  214. {
  215. return IGameCallback::getSelectedHero(getCurrentPlayer())->id;
  216. }
  217. void CClient::newGame( CConnection *con, StartInfo *si )
  218. {
  219. timeHandler tmh;
  220. CGI->state = new CGameState();
  221. tlog0 <<"\tGamestate: "<<tmh.getDif()<<std::endl;
  222. serv = con;
  223. CConnection &c(*con);
  224. ////////////////////////////////////////////////////
  225. ui8 pom8;
  226. c << ui8(2) << ui8(1); //new game; one client
  227. c << *si;
  228. c >> pom8;
  229. if(pom8)
  230. throw "Server cannot open the map!";
  231. else
  232. tlog0 << "Server opened map properly.\n";
  233. c << ui8(si->playerInfos.size()+1); //number of players + neutral
  234. for(size_t i=0;i<si->playerInfos.size();i++)
  235. {
  236. c << ui8(si->playerInfos[i].color); //players
  237. }
  238. c << ui8(255); // neutrals
  239. ui32 seed, sum;
  240. std::string mapname;
  241. c >> mapname >> sum >> seed;
  242. tlog0 <<"\tSending/Getting info to/from the server: "<<tmh.getDif()<<std::endl;
  243. Mapa * mapa = new Mapa(mapname);
  244. tlog0 <<"Reading and detecting map file (together): "<<tmh.getDif()<<std::endl;
  245. tlog0 << "\tServer checksum for "<<mapname <<": "<<sum << std::endl;
  246. tlog0 << "\tOur checksum for the map: "<< mapa->checksum << std::endl;
  247. if(mapa->checksum != sum)
  248. {
  249. tlog1 << "Wrong map checksum!!!" << std::endl;
  250. throw std::string("Wrong checksum");
  251. }
  252. tlog0 << "\tUsing random seed: "<<seed << std::endl;
  253. gs = CGI->state;
  254. gs->scenarioOps = si;
  255. gs->init(si,mapa,seed);
  256. CGI->mh = new CMapHandler();
  257. tlog0 <<"Initializing GameState (together): "<<tmh.getDif()<<std::endl;
  258. CGI->mh->map = mapa;
  259. tlog0 <<"Creating mapHandler: "<<tmh.getDif()<<std::endl;
  260. CGI->mh->init();
  261. tlog0 <<"Initializing mapHandler (together): "<<tmh.getDif()<<std::endl;
  262. for (size_t i=0; i<CGI->state->scenarioOps->playerInfos.size();++i) //initializing interfaces for players
  263. {
  264. ui8 color = gs->scenarioOps->playerInfos[i].color;
  265. CCallback *cb = new CCallback(gs,color,this);
  266. if(!gs->scenarioOps->playerInfos[i].human) {
  267. playerint[color] = static_cast<CGameInterface*>(CAIHandler::getNewAI(cb,conf.cc.defaultAI));
  268. }
  269. else {
  270. playerint[color] = new CPlayerInterface(color,i);
  271. }
  272. gs->currentPlayer = color;
  273. playerint[color]->init(cb);
  274. }
  275. playerint[255] = CAIHandler::getNewAI(cb,conf.cc.defaultAI);
  276. playerint[255]->init(new CCallback(gs,255,this));
  277. }
  278. void CClient::runServer(const char * portc)
  279. {
  280. static std::string comm = std::string(SERVER_NAME) + " " + portc + " > server_log.txt"; //needs to be static, if not - will be probably destroyed before new thread reads it
  281. boost::thread servthr(boost::bind(system,comm.c_str())); //runs server executable; //TODO: will it work on non-windows platforms?
  282. }
  283. void CClient::waitForServer()
  284. {
  285. intpr::scoped_lock<intpr::interprocess_mutex> slock(shared->sr->mutex);
  286. while(!shared->sr->ready)
  287. {
  288. shared->sr->cond.wait(slock);
  289. }
  290. }