Client.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  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 <boost/foreach.hpp>
  15. #include "../hch/CObjectHandler.h"
  16. #include "../hch/CGeneralTextHandler.h"
  17. #include "../hch/CArtHandler.h"
  18. #include <boost/thread/shared_mutex.hpp>
  19. #include "../lib/VCMI_Lib.h"
  20. CSharedCond<std::set<IPack*> > mess(new std::set<IPack*>);
  21. std::string toString(MetaString &ms)
  22. {
  23. std::string ret;
  24. for(int i=0;i<ms.message.size();i++)
  25. {
  26. if(ms.message[i]>0)
  27. {
  28. ret += ms.strings[ms.message[i]-1];
  29. }
  30. else
  31. {
  32. std::vector<std::string> *vec;
  33. int type = ms.texts[-ms.message[i]-1].first,
  34. ser = ms.texts[-ms.message[i]-1].second;
  35. if(type == 5)
  36. {
  37. ret += CGI->arth->artifacts[ser].name;
  38. continue;
  39. }
  40. else if(type == 7)
  41. {
  42. ret += CGI->creh->creatures[ser].namePl;
  43. continue;
  44. }
  45. else if(type == 9)
  46. {
  47. ret += CGI->objh->mines[ser].first;
  48. continue;
  49. }
  50. else if(type == 10)
  51. {
  52. ret += CGI->objh->mines[ser].second;
  53. continue;
  54. }
  55. else
  56. {
  57. switch(type)
  58. {
  59. case 1:
  60. vec = &CGI->generaltexth->allTexts;
  61. break;
  62. case 2:
  63. vec = &CGI->objh->xtrainfo;
  64. break;
  65. case 3:
  66. vec = &CGI->objh->names;
  67. break;
  68. case 4:
  69. vec = &CGI->objh->restypes;
  70. break;
  71. case 6:
  72. vec = &CGI->generaltexth->arraytxt;
  73. break;
  74. case 8:
  75. vec = &CGI->objh->creGens;
  76. break;
  77. case 11:
  78. vec = &CGI->objh->advobtxt;
  79. }
  80. ret += (*vec)[ser];
  81. }
  82. }
  83. }
  84. for(int i=0;i<ms.replacements.size();i++)
  85. {
  86. ret.replace(ret.find("%s"),2,ms.replacements[i]);
  87. }
  88. return ret;
  89. }
  90. CClient::CClient(void)
  91. {
  92. }
  93. CClient::CClient(CConnection *con, StartInfo *si)
  94. :serv(con)
  95. {
  96. timeHandler tmh;
  97. CGI->state = new CGameState();
  98. THC std::cout<<"\tGamestate: "<<tmh.getDif()<<std::endl;
  99. CConnection &c(*con);
  100. ////////////////////////////////////////////////////
  101. ui8 pom8;
  102. c << ui8(2) << ui8(1); //new game; one client
  103. c << *si;
  104. c >> pom8;
  105. if(pom8) throw "Server cannot open the map!";
  106. c << ui8(si->playerInfos.size());
  107. for(int i=0;i<si->playerInfos.size();i++)
  108. c << ui8(si->playerInfos[i].color);
  109. ui32 seed, sum;
  110. std::string mapname;
  111. c >> mapname >> sum >> seed;
  112. THC std::cout<<"\tSending/Getting info to/from the server: "<<tmh.getDif()<<std::endl;
  113. Mapa * mapa = new Mapa(mapname);
  114. THC std::cout<<"Reading and detecting map file (together): "<<tmh.getDif()<<std::endl;
  115. std::cout << "\tServer checksum for "<<mapname <<": "<<sum << std::endl;
  116. std::cout << "\tOur checksum for the map: "<< mapa->checksum << std::endl;
  117. if(mapa->checksum != sum)
  118. {
  119. #ifndef __GNUC__
  120. throw std::exception("Wrong checksum");
  121. #else
  122. throw std::exception();
  123. #endif
  124. exit(-1);
  125. }
  126. std::cout << "\tUsing random seed: "<<seed << std::endl;
  127. gs = CGI->state;
  128. gs->scenarioOps = si;
  129. gs->init(si,mapa,seed);
  130. CGI->mh = new CMapHandler();
  131. THC std::cout<<"Initializing GameState (together): "<<tmh.getDif()<<std::endl;
  132. CGI->mh->map = mapa;
  133. THC std::cout<<"Creating mapHandler: "<<tmh.getDif()<<std::endl;
  134. CGI->mh->init();
  135. THC std::cout<<"Initializing mapHandler (together): "<<tmh.getDif()<<std::endl;
  136. for (int i=0; i<CGI->state->scenarioOps->playerInfos.size();i++) //initializing interfaces
  137. {
  138. ui8 color = gs->scenarioOps->playerInfos[i].color;
  139. CCallback *cb = new CCallback(gs,color,this);
  140. if(!gs->scenarioOps->playerInfos[i].human)
  141. {
  142. playerint[color] = static_cast<CGameInterface*>(CAIHandler::getNewAI(cb,"EmptyAI.dll"));
  143. }
  144. else
  145. {
  146. gs->currentPlayer = color;
  147. playerint[color] = new CPlayerInterface(color,i);
  148. playerint[color]->init(cb);
  149. }
  150. }
  151. CGI->consoleh->cb = new CCallback(gs,-1,this);
  152. }
  153. CClient::~CClient(void)
  154. {
  155. }
  156. void CClient::process(int what)
  157. {
  158. switch (what)
  159. {
  160. case 100: //one of our interaces has turn
  161. {
  162. ui8 player;
  163. *serv >> player;//who?
  164. std::cout << "It's turn of "<<(unsigned)player<<" player."<<std::endl;
  165. boost::thread(boost::bind(&CGameInterface::yourTurn,playerint[player]));
  166. break;
  167. }
  168. case 101:
  169. {
  170. NewTurn n;
  171. *serv >> n;
  172. std::cout << "New day: "<<(unsigned)n.day<<". Applying changes... ";
  173. gs->apply(&n);
  174. std::cout << "done!"<<std::endl;
  175. break;
  176. }
  177. case 102: //set resource amount
  178. {
  179. SetResource sr;
  180. *serv >> sr;
  181. std::cout << "Set amount of "<<CGI->objh->restypes[sr.resid]
  182. << " of player "<<(unsigned)sr.player <<" to "<<sr.val<<std::endl;
  183. gs->apply(&sr);
  184. playerint[sr.player]->receivedResource(sr.resid,sr.val);
  185. break;
  186. }
  187. case 103: //show info dialog
  188. {
  189. InfoWindow iw;
  190. *serv >> iw;
  191. std::vector<Component*> comps;
  192. for(int i=0;i<iw.components.size();i++)
  193. comps.push_back(&iw.components[i]);
  194. playerint[iw.player]->showInfoDialog(toString(iw.text),comps);
  195. break;
  196. }
  197. case 104:
  198. {
  199. SetResources sr;
  200. *serv >> sr;
  201. std::cout << "Set amount of resources of player "<<(unsigned)sr.player<<std::endl;
  202. gs->apply(&sr);
  203. playerint[sr.player]->receivedResource(-1,-1);
  204. break;
  205. }
  206. case 105:
  207. {
  208. SetPrimSkill sps;
  209. *serv >> sps;
  210. std::cout << "Changing hero primary skill"<<std::endl;
  211. gs->apply(&sps);
  212. playerint[gs->getHero(sps.id)->tempOwner]->heroPrimarySkillChanged(gs->getHero(sps.id),sps.which,sps.val);
  213. break;
  214. }
  215. case 107:
  216. {
  217. ShowInInfobox sii;
  218. *serv >> sii;
  219. SComponent sc(sii.c);
  220. sc.description = toString(sii.text);
  221. if(playerint[sii.player]->human)
  222. static_cast<CPlayerInterface*>(playerint[sii.player])->showComp(sc);
  223. break;
  224. }
  225. case 500:
  226. {
  227. RemoveHero rh;
  228. *serv >> rh;
  229. CGHeroInstance *h = static_cast<CGHeroInstance*>(gs->map->objects[rh.id]);
  230. std::cout << "Removing hero with id = "<<(unsigned)rh.id<<std::endl;
  231. CGI->mh->removeObject(h);
  232. gs->apply(&rh);
  233. playerint[h->tempOwner]->heroKilled(h);
  234. break;
  235. }
  236. case 501: //hero movement response - we have to notify interfaces and callback
  237. {
  238. TryMoveHero *th = new TryMoveHero; //will be deleted by callback after processing
  239. *serv >> *th;
  240. std::cout << "HeroMove: id="<<th->id<<"\tResult: "<<(unsigned)th->result<<"\tPosition "<<th->end<<std::endl;
  241. gs->apply(th);
  242. int player = gs->map->objects[th->id]->getOwner();
  243. if(playerint[player])
  244. {
  245. for(std::set<int3>::iterator i=th->fowRevealed.begin(); i != th->fowRevealed.end(); i++)
  246. playerint[player]->tileRevealed(*i);
  247. //std::for_each(th->fowRevealed.begin(),th->fowRevealed.end(),boost::bind(&CGameInterface::tileRevealed,playerint[player],_1));
  248. }
  249. //notify interfacesabout move
  250. int nn=0; //number of interfece of currently browsed player
  251. for(std::map<ui8, CGameInterface*>::iterator i=playerint.begin();i!=playerint.end();i++)
  252. {
  253. 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])
  254. {
  255. HeroMoveDetails hmd(th->start,th->end,static_cast<CGHeroInstance*>(gs->map->objects[th->id]));
  256. hmd.successful = th->result;
  257. i->second->heroMoved(hmd);
  258. }
  259. }
  260. //add info for callback
  261. mess.mx->lock();
  262. mess.res->insert(th);
  263. mess.mx->unlock();
  264. mess.cv->notify_all();
  265. break;
  266. }
  267. case 502:
  268. {
  269. SetGarrisons sg;
  270. *serv >> sg;
  271. std::cout << "Setting garrisons." << std::endl;
  272. gs->apply(&sg);
  273. for(std::map<ui32,CCreatureSet>::iterator i = sg.garrs.begin(); i!=sg.garrs.end(); i++)
  274. playerint[gs->map->objects[i->first]->tempOwner]->garrisonChanged(gs->map->objects[i->first]);
  275. break;
  276. }
  277. case 503:
  278. {
  279. //SetStrInfo ssi;
  280. //*serv >> ssi;
  281. //gs->apply(&ssi);
  282. //TODO: notify interfaces
  283. break;
  284. }
  285. case 504:
  286. {
  287. NewStructures ns;
  288. *serv >> ns;
  289. std::cout << "New structure(s) in " << ns.tid << " - " << *ns.bid.begin() << std::endl;
  290. gs->apply(&ns);
  291. BOOST_FOREACH(si32 bid, ns.bid)
  292. playerint[gs->map->objects[ns.tid]->tempOwner]->buildChanged(static_cast<CGTownInstance*>(gs->map->objects[ns.tid]),bid,1);
  293. break;
  294. }
  295. case 506:
  296. {
  297. SetAvailableCreatures ns;
  298. *serv >> ns;
  299. std::cout << "Setting available creatures in " << ns.tid << std::endl;
  300. gs->apply(&ns);
  301. //TODO: do we need to inform interface?
  302. break;
  303. }
  304. case 1001:
  305. {
  306. SetObjectProperty sop;
  307. *serv >> sop;
  308. std::cout << "Setting " << (unsigned)sop.what << " property of " << sop.id <<" object to "<<sop.val<<std::endl;
  309. gs->apply(&sop);
  310. break;
  311. }
  312. case 1002:
  313. {
  314. SetHoverName shn;
  315. *serv >> shn;
  316. std::cout << "Setting a name of " << shn.id <<" object to "<< toString(shn.name) <<std::endl;
  317. gs->mx->lock();
  318. gs->map->objects[shn.id]->hoverName = toString(shn.name);
  319. gs->mx->unlock();
  320. break;
  321. }
  322. case 3000:
  323. {
  324. BattleStart bs;
  325. *serv >> bs; //uses new to allocate memory for battleInfo - must be deleted when battle is over
  326. std::cout << "Starting battle!" <<std::endl;
  327. gs->apply(&bs);
  328. if(playerint.find(gs->curB->side1) != playerint.end())
  329. playerint[gs->curB->side1]->battleStart(&gs->curB->army1, &gs->curB->army2, gs->curB->tile, gs->getHero(gs->curB->hero1), gs->getHero(gs->curB->hero2), 0);
  330. if(playerint.find(gs->curB->side2) != playerint.end())
  331. playerint[gs->curB->side2]->battleStart(&gs->curB->army1, &gs->curB->army2, gs->curB->tile, gs->getHero(gs->curB->hero1), gs->getHero(gs->curB->hero2), 1);
  332. break;
  333. }
  334. case 3001:
  335. {
  336. BattleNextRound bnr;
  337. *serv >> bnr;
  338. std::cout << "Round nr " << bnr.round <<std::endl;
  339. gs->apply(&bnr);
  340. //tell players about next round
  341. if(playerint.find(gs->curB->side1) != playerint.end())
  342. playerint[gs->curB->side1]->battleNewRound(bnr.round);
  343. if(playerint.find(gs->curB->side2) != playerint.end())
  344. playerint[gs->curB->side2]->battleNewRound(bnr.round);
  345. break;
  346. }
  347. case 3002:
  348. {
  349. BattleSetActiveStack sas;
  350. *serv >> sas;
  351. std::cout << "Active stack: " << sas.stack <<std::endl;
  352. gs->apply(&sas);
  353. int owner = gs->curB->getStack(sas.stack)->owner;
  354. if(owner >= PLAYER_LIMIT) //ugly workaround to skip neutral creatures - should be replaced with AI
  355. {
  356. BattleAction ba;
  357. ba.stackNumber = sas.stack;
  358. ba.actionType = 3;
  359. *serv << ui16(3002) << ba;
  360. }
  361. else
  362. {
  363. boost::thread(boost::bind(&CClient::waitForMoveAndSend,this,owner));
  364. }
  365. break;
  366. }
  367. case 3003:
  368. {
  369. BattleResult br;
  370. *serv >> br;
  371. std::cout << "Battle ends. Winner: " << (unsigned)br.winner<< ". Type of end: "<< (unsigned)br.result <<std::endl;
  372. if(playerint.find(gs->curB->side1) != playerint.end())
  373. playerint[gs->curB->side1]->battleEnd(&br);
  374. if(playerint.find(gs->curB->side2) != playerint.end())
  375. playerint[gs->curB->side2]->battleEnd(&br);
  376. gs->apply(&br);
  377. break;
  378. }
  379. case 3004:
  380. {
  381. BattleStackMoved br;
  382. *serv >> br;
  383. std::cout << "Stack "<<br.stack <<" moves to the tile "<<br.tile<<std::endl;
  384. if(playerint.find(gs->curB->side1) != playerint.end())
  385. playerint[gs->curB->side1]->battleStackMoved(br.stack,br.tile,br.flags&1,br.flags&2);
  386. if(playerint.find(gs->curB->side2) != playerint.end())
  387. playerint[gs->curB->side2]->battleStackMoved(br.stack,br.tile,br.flags&1,br.flags&2);
  388. gs->apply(&br);
  389. break;
  390. }
  391. case 3006:
  392. {
  393. BattleAttack ba;
  394. *serv >> ba;
  395. std::cout << "Stack: " << ba.stackAttacking << " is attacking stack "<< ba.bsa.stackAttacked <<std::endl;
  396. gs->apply(&ba);
  397. LOCPLINT->battleAttack(&ba);
  398. break;
  399. }
  400. case 9999:
  401. break;
  402. default:
  403. #ifndef __GNUC__
  404. throw std::exception("Not supported server message!");
  405. #else
  406. throw std::exception();
  407. #endif
  408. break;
  409. }
  410. }
  411. void CClient::waitForMoveAndSend(int color)
  412. {
  413. BattleAction ba = playerint[color]->activeStack(gs->curB->activeStack);
  414. *serv << ui16(3002) << ba;
  415. }
  416. void CClient::run()
  417. {
  418. try
  419. {
  420. ui16 typ;
  421. while(1)
  422. {
  423. *serv >> typ;
  424. process(typ);
  425. }
  426. } HANDLE_EXCEPTION
  427. }