2
0

Client.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. #include "Client.h"
  2. #include "../lib/Connection.h"
  3. #include "../StartInfo.h"
  4. #include "../map.h"
  5. #include "../CGameState.h"
  6. #include "../CGameInfo.h"
  7. #include "../mapHandler.h"
  8. #include "../CCallback.h"
  9. #include "../CPlayerInterface.h"
  10. #include "../CConsoleHandler.h"
  11. #include "../lib/NetPacks.h"
  12. #include <boost/bind.hpp>
  13. #include <boost/thread.hpp>
  14. #include "../hch/CObjectHandler.h"
  15. #include "../hch/CGeneralTextHandler.h"
  16. #include "../hch/CArtHandler.h"
  17. #include <boost/thread/shared_mutex.hpp>
  18. CSharedCond<std::set<IPack*> > mess(new std::set<IPack*>);
  19. std::string toString(MetaString &ms)
  20. {
  21. std::string ret;
  22. for(int i=0;i<ms.message.size();i++)
  23. {
  24. if(ms.message[i]>0)
  25. {
  26. ret += ms.strings[ms.message[i]-1];
  27. }
  28. else
  29. {
  30. std::vector<std::string> *vec;
  31. int type = ms.texts[-ms.message[i]-1].first;
  32. if(type == 5)
  33. {
  34. ret += CGI->arth->artifacts[ms.texts[-ms.message[i]-1].second].name;
  35. continue;
  36. }
  37. else if(type == 7)
  38. {
  39. ret += CGI->creh->creatures[ms.texts[-ms.message[i]-1].second].namePl;
  40. continue;
  41. }
  42. else if(type == 9)
  43. {
  44. ret += CGI->objh->mines[ms.texts[-ms.message[i]-1].second].first;
  45. continue;
  46. }
  47. else
  48. {
  49. switch(type)
  50. {
  51. case 1:
  52. vec = &CGI->generaltexth->allTexts;
  53. break;
  54. case 2:
  55. vec = &CGI->objh->xtrainfo;
  56. break;
  57. case 3:
  58. vec = &CGI->objh->names;
  59. break;
  60. case 4:
  61. vec = &CGI->objh->restypes;
  62. break;
  63. case 6:
  64. vec = &CGI->generaltexth->arraytxt;
  65. break;
  66. case 8:
  67. vec = &CGI->objh->creGens;
  68. break;
  69. }
  70. ret += (*vec)[ms.texts[-ms.message[i]-1].second];
  71. }
  72. }
  73. }
  74. return ret;
  75. }
  76. CClient::CClient(void)
  77. {
  78. }
  79. CClient::CClient(CConnection *con, StartInfo *si)
  80. :serv(con)
  81. {
  82. timeHandler tmh;
  83. CGI->state = new CGameState();
  84. THC std::cout<<"\tGamestate: "<<tmh.getDif()<<std::endl;
  85. CConnection &c(*con);
  86. ////////////////////////////////////////////////////
  87. ui8 pom8;
  88. c << ui8(2) << ui8(1); //new game; one client
  89. c << *si;
  90. c >> pom8;
  91. if(pom8) throw "Server cannot open the map!";
  92. c << ui8(si->playerInfos.size());
  93. for(int i=0;i<si->playerInfos.size();i++)
  94. c << ui8(si->playerInfos[i].color);
  95. ui32 seed, sum;
  96. std::string mapname;
  97. c >> mapname >> sum >> seed;
  98. THC std::cout<<"\tSending/Getting info to/from the server: "<<tmh.getDif()<<std::endl;
  99. Mapa * mapa = new Mapa(mapname);
  100. THC std::cout<<"Reading and detecting map file (together): "<<tmh.getDif()<<std::endl;
  101. std::cout << "\tServer checksum for "<<mapname <<": "<<sum << std::endl;
  102. std::cout << "\tOur checksum for the map: "<< mapa->checksum << std::endl;
  103. if(mapa->checksum != sum)
  104. exit(-1);
  105. std::cout << "\tUsing random seed: "<<seed << std::endl;
  106. gs = CGI->state;
  107. gs->scenarioOps = si;
  108. gs->init(si,mapa,seed);
  109. CGI->mh = new CMapHandler();
  110. THC std::cout<<"Initializing GameState (together): "<<tmh.getDif()<<std::endl;
  111. CGI->mh->map = mapa;
  112. THC std::cout<<"Creating mapHandler: "<<tmh.getDif()<<std::endl;
  113. CGI->mh->init();
  114. THC std::cout<<"Initializing mapHandler (together): "<<tmh.getDif()<<std::endl;
  115. for (int i=0; i<CGI->state->scenarioOps->playerInfos.size();i++) //initializing interfaces
  116. {
  117. ui8 color = gs->scenarioOps->playerInfos[i].color;
  118. CCallback *cb = new CCallback(gs,color,this);
  119. if(!gs->scenarioOps->playerInfos[i].human)
  120. playerint[color] = static_cast<CGameInterface*>(CAIHandler::getNewAI(cb,"EmptyAI.dll"));
  121. else
  122. {
  123. gs->currentPlayer = color;
  124. playerint[color] = new CPlayerInterface(color,i);
  125. playerint[color]->init(cb);
  126. }
  127. }
  128. CGI->consoleh->cb = new CCallback(gs,-1,this);
  129. }
  130. CClient::~CClient(void)
  131. {
  132. }
  133. void CClient::process(int what)
  134. {
  135. switch (what)
  136. {
  137. case 100: //one of our interaces has turn
  138. {
  139. ui8 player;
  140. *serv >> player;//who?
  141. std::cout << "It's turn of "<<(unsigned)player<<" player."<<std::endl;
  142. boost::thread(boost::bind(&CGameInterface::yourTurn,playerint[player]));
  143. break;
  144. }
  145. case 101:
  146. {
  147. NewTurn n;
  148. *serv >> n;
  149. std::cout << "New day: "<<(unsigned)n.day<<". Applying changes... ";
  150. gs->apply(&n);
  151. std::cout << "done!"<<std::endl;
  152. break;
  153. }
  154. case 501: //hero movement response - we have to notify interfaces and callback
  155. {
  156. TryMoveHero *th = new TryMoveHero; //will be deleted by callback after processing
  157. *serv >> *th;
  158. std::cout << "HeroMove: id="<<th->id<<"\tResult: "<<(unsigned)th->result<<"\tPosition "<<th->end<<std::endl;
  159. gs->apply(th);
  160. int player = gs->map->objects[th->id]->getOwner();
  161. if(playerint[player])
  162. {
  163. for(std::set<int3>::iterator i=th->fowRevealed.begin(); i != th->fowRevealed.end(); i++)
  164. playerint[player]->tileRevealed(*i);
  165. //boost::function<void(int3)> tr = boost::bind(&CGameInterface::tileRevealed,playerint[player]);
  166. //std::for_each(th->fowRevealed.begin(),th->fowRevealed.end(),tr);
  167. }
  168. //notify interfacesabout move
  169. int nn=0; //number of interfece of currently browsed player
  170. for(std::map<ui8, CGameInterface*>::iterator i=playerint.begin();i!=playerint.end();i++)
  171. {
  172. if(gs->players[i->first].fogOfWarMap[th->start.x-1][th->start.y][th->start.z] || gs->players[i->first].fogOfWarMap[th->end.x-1][th->end.y][th->end.z])
  173. {
  174. HeroMoveDetails hmd(th->start,th->end,static_cast<CGHeroInstance*>(gs->map->objects[th->id]));
  175. hmd.successful = th->result;
  176. i->second->heroMoved(hmd);
  177. }
  178. }
  179. //add info for callback
  180. mess.mx->lock();
  181. mess.res->insert(th);
  182. mess.mx->unlock();
  183. mess.cv->notify_all();
  184. break;
  185. }
  186. case 1001:
  187. {
  188. SetObjectProperty sop;
  189. *serv >> sop;
  190. std::cout << "Setting " << (unsigned)sop.what << " property of " << sop.id <<" object to "<<sop.val<<std::endl;
  191. gs->apply(&sop);
  192. break;
  193. }
  194. case 1002:
  195. {
  196. SetHoverName shn;
  197. *serv >> shn;
  198. std::cout << "Setting a name of " << shn.id <<" object to "<< toString(shn.name) <<std::endl;
  199. gs->mx->lock();
  200. gs->map->objects[shn.id]->hoverName = toString(shn.name);
  201. gs->mx->unlock();
  202. break;
  203. }
  204. case 9999:
  205. break;
  206. default:
  207. throw std::exception("Not supported server message!");
  208. break;
  209. }
  210. }
  211. void CClient::run()
  212. {
  213. try
  214. {
  215. ui16 typ;
  216. while(1)
  217. {
  218. *serv >> typ;
  219. process(typ);
  220. }
  221. } HANDLE_EXCEPTION
  222. }