NetPacksClient.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. #include "../lib/NetPacks.h"
  2. #include "../CCallback.h"
  3. #include "../client/Client.h"
  4. #include "../CPlayerInterface.h"
  5. #include "../CGameInfo.h"
  6. #include "../lib/Connection.h"
  7. #include "../hch/CGeneralTextHandler.h"
  8. #include "../hch/CDefObjInfoHandler.h"
  9. #include "../hch/CHeroHandler.h"
  10. #include "../hch/CObjectHandler.h"
  11. #include "../lib/VCMI_Lib.h"
  12. #include "../map.h"
  13. #include "../hch/CSpellHandler.h"
  14. #include "../mapHandler.h"
  15. #include <boost/bind.hpp>
  16. #include <boost/foreach.hpp>
  17. #include <boost/thread.hpp>
  18. #include <boost/thread/shared_mutex.hpp>
  19. //macro to avoid code duplication - calls given method with given arguments if interface for specific player is present
  20. #define INTERFACE_CALL_IF_PRESENT(player,function,...) \
  21. if(vstd::contains(cl->playerint,player)) \
  22. cl->playerint[player]->function(__VA_ARGS__);
  23. CSharedCond<std::set<CPack*> > mess(new std::set<CPack*>);
  24. void SetResources::applyCl( CClient *cl )
  25. {
  26. cl->playerint[player]->receivedResource(-1,-1);
  27. }
  28. void SetResource::applyCl( CClient *cl )
  29. {
  30. cl->playerint[player]->receivedResource(resid,val);
  31. }
  32. void SetPrimSkill::applyCl( CClient *cl )
  33. {
  34. const CGHeroInstance *h = GS(cl)->getHero(id);
  35. if(!h)
  36. {
  37. tlog1 << "Cannot find hero with ID " << id << std::endl;
  38. return;
  39. }
  40. INTERFACE_CALL_IF_PRESENT(h->tempOwner,heroPrimarySkillChanged,h,which,val);
  41. }
  42. void SetSecSkill::applyCl( CClient *cl )
  43. {
  44. //TODO: inform interface?
  45. }
  46. void HeroVisitCastle::applyCl( CClient *cl )
  47. {
  48. if(start() && !garrison() && vstd::contains(cl->playerint,GS(cl)->getHero(hid)->tempOwner))
  49. {
  50. cl->playerint[GS(cl)->getHero(hid)->tempOwner]->heroVisitsTown(GS(cl)->getHero(hid),GS(cl)->getTown(tid));
  51. }
  52. }
  53. void ChangeSpells::applyCl( CClient *cl )
  54. {
  55. //TODO: inform interface?
  56. }
  57. void SetMana::applyCl( CClient *cl )
  58. {
  59. CGHeroInstance *h = GS(cl)->getHero(hid);
  60. if(vstd::contains(cl->playerint,h->tempOwner))
  61. cl->playerint[h->tempOwner]->heroManaPointsChanged(h);
  62. }
  63. void SetMovePoints::applyCl( CClient *cl )
  64. {
  65. CGHeroInstance *h = GS(cl)->getHero(hid);
  66. if(vstd::contains(cl->playerint,h->tempOwner))
  67. cl->playerint[h->tempOwner]->heroMovePointsChanged(h);
  68. }
  69. void FoWChange::applyCl( CClient *cl )
  70. {
  71. if(!vstd::contains(cl->playerint,player))
  72. return;
  73. if(mode)
  74. cl->playerint[player]->tileRevealed(tiles);
  75. else
  76. cl->playerint[player]->tileHidden(tiles);
  77. }
  78. void SetAvailableHeroes::applyCl( CClient *cl )
  79. {
  80. //TODO: inform interface?
  81. }
  82. void GiveBonus::applyCl( CClient *cl )
  83. {
  84. CGHeroInstance *h = GS(cl)->getHero(hid);
  85. if(vstd::contains(cl->playerint,h->tempOwner))
  86. cl->playerint[h->tempOwner]->heroBonusChanged(h,h->bonuses.back(),true);
  87. }
  88. void ChangeObjPos::applyFirstCl( CClient *cl )
  89. {
  90. CGObjectInstance *obj = GS(cl)->map->objects[objid];
  91. if(flags & 1)
  92. CGI->mh->hideObject(obj);
  93. }
  94. void ChangeObjPos::applyCl( CClient *cl )
  95. {
  96. CGObjectInstance *obj = GS(cl)->map->objects[objid];
  97. if(flags & 1)
  98. CGI->mh->printObject(obj);
  99. }
  100. void RemoveObject::applyFirstCl( CClient *cl )
  101. {
  102. CGI->mh->hideObject(cl->getObj(id));
  103. CGHeroInstance *h = GS(cl)->getHero(id);
  104. if(h)
  105. {
  106. if(vstd::contains(cl->playerint,h->tempOwner))
  107. cl->playerint[h->tempOwner]->heroKilled(h);
  108. }
  109. }
  110. void RemoveObject::applyCl( CClient *cl )
  111. {
  112. }
  113. void TryMoveHero::applyFirstCl( CClient *cl )
  114. {
  115. if(result>1)
  116. CGI->mh->removeObject(GS(cl)->getHero(id));
  117. }
  118. void TryMoveHero::applyCl( CClient *cl )
  119. {
  120. HeroMoveDetails hmd(start,end,GS(cl)->getHero(id));
  121. hmd.style = result-1;
  122. hmd.successful = result;
  123. if(result>1)
  124. CGI->mh->printObject(hmd.ho);
  125. int player = hmd.ho->tempOwner;
  126. if(vstd::contains(cl->playerint,player))
  127. {
  128. cl->playerint[player]->tileRevealed(fowRevealed);
  129. }
  130. //notify interfaces about move
  131. for(std::map<ui8, CGameInterface*>::iterator i=cl->playerint.begin();i!=cl->playerint.end();i++)
  132. {
  133. if(i->first >= PLAYER_LIMIT) continue;
  134. if(GS(cl)->players[i->first].fogOfWarMap[start.x-1][start.y][start.z] || GS(cl)->players[i->first].fogOfWarMap[end.x-1][end.y][end.z])
  135. {
  136. i->second->heroMoved(hmd);
  137. }
  138. }
  139. //add info for callback
  140. if(result<2)
  141. {
  142. mess.mx->lock();
  143. mess.res->insert(new TryMoveHero(*this));
  144. mess.mx->unlock();
  145. mess.cv->notify_all();
  146. }
  147. }
  148. void SetGarrisons::applyCl( CClient *cl )
  149. {
  150. for(std::map<ui32,CCreatureSet>::iterator i = garrs.begin(); i!=garrs.end(); i++)
  151. if(vstd::contains(cl->playerint,cl->getOwner(i->first)))
  152. cl->playerint[cl->getOwner(i->first)]->garrisonChanged(cl->getObj(i->first));
  153. }
  154. void NewStructures::applyCl( CClient *cl )
  155. {
  156. CGTownInstance *town = GS(cl)->getTown(tid);
  157. BOOST_FOREACH(si32 id, bid)
  158. {
  159. if(id==13) //fort or capitol
  160. {
  161. town->defInfo = GS(cl)->capitols[town->subID];
  162. }
  163. if(id ==7)
  164. {
  165. town->defInfo = GS(cl)->forts[town->subID];
  166. }
  167. if(vstd::contains(cl->playerint,town->tempOwner))
  168. cl->playerint[town->tempOwner]->buildChanged(town,id,1);
  169. }
  170. }
  171. void SetAvailableCreatures::applyCl( CClient *cl )
  172. {
  173. CGTownInstance *t = GS(cl)->getTown(tid);
  174. if(vstd::contains(cl->playerint,t->tempOwner))
  175. cl->playerint[t->tempOwner]->availableCreaturesChanged(t);
  176. }
  177. void SetHeroesInTown::applyCl( CClient *cl )
  178. {
  179. CGTownInstance *t = GS(cl)->getTown(tid);
  180. if(vstd::contains(cl->playerint,t->tempOwner))
  181. cl->playerint[t->tempOwner]->heroInGarrisonChange(t);
  182. }
  183. void SetHeroArtifacts::applyCl( CClient *cl )
  184. {
  185. CGHeroInstance *t = GS(cl)->getHero(hid);
  186. if(vstd::contains(cl->playerint,t->tempOwner))
  187. cl->playerint[t->tempOwner]->heroArtifactSetChanged(t);
  188. }
  189. void HeroRecruited::applyCl( CClient *cl )
  190. {
  191. CGHeroInstance *h = GS(cl)->map->heroes.back();
  192. if(h->subID != hid)
  193. {
  194. tlog1 << "Something wrong with hero recruited!\n";
  195. }
  196. CGI->mh->initHeroDef(h);
  197. if(vstd::contains(cl->playerint,h->tempOwner))
  198. {
  199. cl->playerint[h->tempOwner]->heroCreated(h);
  200. cl->playerint[h->tempOwner]->heroInGarrisonChange(GS(cl)->getTown(tid));
  201. }
  202. }
  203. void GiveHero::applyCl( CClient *cl )
  204. {
  205. CGHeroInstance *h = GS(cl)->getHero(id);
  206. CGI->mh->initHeroDef(h);
  207. CGI->mh->printObject(h);
  208. cl->playerint[h->tempOwner]->heroCreated(h);
  209. }
  210. void GiveHero::applyFirstCl( CClient *cl )
  211. {
  212. CGI->mh->hideObject(GS(cl)->getHero(id));
  213. }
  214. void InfoWindow::applyCl( CClient *cl )
  215. {
  216. std::vector<Component*> comps;
  217. for(size_t i=0;i<components.size();i++)
  218. {
  219. comps.push_back(&components[i]);
  220. }
  221. std::string str = toString(text);
  222. if(vstd::contains(cl->playerint,player))
  223. cl->playerint[player]->showInfoDialog(str,comps);
  224. else
  225. tlog2 << "We received InfoWindow for not our player...\n";
  226. }
  227. void HeroLevelUp::applyCl( CClient *cl )
  228. {
  229. CGHeroInstance *h = GS(cl)->getHero(heroid);
  230. if(vstd::contains(cl->playerint,h->tempOwner))
  231. {
  232. boost::function<void(ui32)> callback = boost::function<void(ui32)>(boost::bind(&CCallback::selectionMade,LOCPLINT->cb,_1,id));
  233. cl->playerint[h->tempOwner]->heroGotLevel((const CGHeroInstance *)h,(int)primskill,skills, callback);
  234. }
  235. }
  236. void SelectionDialog::applyCl( CClient *cl )
  237. {
  238. std::vector<Component*> comps;
  239. for(size_t i=0; i < components.size(); ++i) {
  240. comps.push_back(&components[i]);
  241. }
  242. std::string str = toString(text);
  243. if(vstd::contains(cl->playerint,player))
  244. cl->playerint[player]->showSelDialog(str,comps,id);
  245. else
  246. tlog2 << "We received SelectionDialog for not our player...\n";
  247. }
  248. void YesNoDialog::applyCl( CClient *cl )
  249. {
  250. std::vector<Component*> comps;
  251. for(size_t i=0; i < components.size(); ++i) {
  252. comps.push_back(&components[i]);
  253. }
  254. std::string str = toString(text);
  255. if(vstd::contains(cl->playerint,player))
  256. cl->playerint[player]->showYesNoDialog(str,comps,id);
  257. else
  258. tlog2 << "We received YesNoDialog for not our player...\n";
  259. }
  260. void BattleStart::applyCl( CClient *cl )
  261. {
  262. if(vstd::contains(cl->playerint,info->side1))
  263. cl->playerint[info->side1]->battleStart(&info->army1, &info->army2, info->tile, GS(cl)->getHero(info->hero1), GS(cl)->getHero(info->hero2), 0);
  264. if(vstd::contains(cl->playerint,info->side2))
  265. cl->playerint[info->side2]->battleStart(&info->army1, &info->army2, info->tile, GS(cl)->getHero(info->hero1), GS(cl)->getHero(info->hero2), 1);
  266. }
  267. void BattleNextRound::applyCl( CClient *cl )
  268. {
  269. if(cl->playerint.find(GS(cl)->curB->side1) != cl->playerint.end())
  270. cl->playerint[GS(cl)->curB->side1]->battleNewRound(round);
  271. if(cl->playerint.find(GS(cl)->curB->side2) != cl->playerint.end())
  272. cl->playerint[GS(cl)->curB->side2]->battleNewRound(round);
  273. }
  274. void BattleSetActiveStack::applyCl( CClient *cl )
  275. {
  276. int owner = GS(cl)->curB->getStack(stack)->owner;
  277. if(vstd::contains(cl->playerint,owner))
  278. boost::thread(boost::bind(&CClient::waitForMoveAndSend,cl,owner));
  279. }
  280. void BattleResult::applyFirstCl( CClient *cl )
  281. {
  282. if(cl->playerint.find(GS(cl)->curB->side1) != cl->playerint.end())
  283. cl->playerint[GS(cl)->curB->side1]->battleEnd(this);
  284. if(cl->playerint.find(GS(cl)->curB->side2) != cl->playerint.end())
  285. cl->playerint[GS(cl)->curB->side2]->battleEnd(this);
  286. }
  287. void BattleStackMoved::applyFirstCl( CClient *cl )
  288. {
  289. INTERFACE_CALL_IF_PRESENT(GS(cl)->curB->side1,battleStackMoved,stack,tile,distance,ending);
  290. INTERFACE_CALL_IF_PRESENT(GS(cl)->curB->side2,battleStackMoved,stack,tile,distance,ending);
  291. }
  292. void BattleStackAttacked::applyCl( CClient *cl )
  293. {
  294. std::set<BattleStackAttacked> bsa;
  295. bsa.insert(*this);
  296. INTERFACE_CALL_IF_PRESENT(GS(cl)->curB->side1,battleStacksAttacked,bsa);
  297. INTERFACE_CALL_IF_PRESENT(GS(cl)->curB->side2,battleStacksAttacked,bsa);
  298. }
  299. void BattleAttack::applyFirstCl( CClient *cl )
  300. {
  301. if(cl->playerint.find(GS(cl)->curB->side1) != cl->playerint.end())
  302. cl->playerint[GS(cl)->curB->side1]->battleAttack(this);
  303. if(cl->playerint.find(GS(cl)->curB->side2) != cl->playerint.end())
  304. cl->playerint[GS(cl)->curB->side2]->battleAttack(this);
  305. }
  306. void BattleAttack::applyCl( CClient *cl )
  307. {
  308. INTERFACE_CALL_IF_PRESENT(GS(cl)->curB->side1,battleStacksAttacked,bsa);
  309. INTERFACE_CALL_IF_PRESENT(GS(cl)->curB->side2,battleStacksAttacked,bsa);
  310. }
  311. void StartAction::applyFirstCl( CClient *cl )
  312. {
  313. cl->curbaction = new BattleAction(ba);
  314. if(cl->playerint.find(GS(cl)->curB->side1) != cl->playerint.end())
  315. cl->playerint[GS(cl)->curB->side1]->actionStarted(&ba);
  316. if(cl->playerint.find(GS(cl)->curB->side2) != cl->playerint.end())
  317. cl->playerint[GS(cl)->curB->side2]->actionStarted(&ba);
  318. }
  319. void SpellCasted::applyCl( CClient *cl )
  320. {
  321. if(cl->playerint.find(GS(cl)->curB->side1) != cl->playerint.end())
  322. cl->playerint[GS(cl)->curB->side1]->battleSpellCasted(this);
  323. if(cl->playerint.find(GS(cl)->curB->side2) != cl->playerint.end())
  324. cl->playerint[GS(cl)->curB->side2]->battleSpellCasted(this);
  325. }
  326. void SetStackEffect::applyCl( CClient *cl )
  327. {
  328. SpellCasted sc;
  329. sc.id = effect.id;
  330. sc.side = 3; //doesn't matter
  331. sc.skill = effect.level;
  332. //informing about effects
  333. if(cl->playerint.find(GS(cl)->curB->side1) != cl->playerint.end())
  334. cl->playerint[GS(cl)->curB->side1]->battleStacksEffectsSet(*this);
  335. if(cl->playerint.find(GS(cl)->curB->side2) != cl->playerint.end())
  336. cl->playerint[GS(cl)->curB->side2]->battleStacksEffectsSet(*this);
  337. }
  338. CGameState* CPackForClient::GS( CClient *cl )
  339. {
  340. return cl->gs;
  341. }
  342. void EndAction::applyCl( CClient *cl )
  343. {
  344. INTERFACE_CALL_IF_PRESENT(GS(cl)->curB->side1,actionFinished,cl->curbaction);
  345. INTERFACE_CALL_IF_PRESENT(GS(cl)->curB->side2,actionFinished,cl->curbaction);
  346. delete cl->curbaction;
  347. cl->curbaction = NULL;
  348. }
  349. void SystemMessage::applyCl( CClient *cl )
  350. {
  351. tlog4 << "System message from server: " << text << std::endl;
  352. }
  353. void YourTurn::applyCl( CClient *cl )
  354. {
  355. boost::thread(boost::bind(&CGameInterface::yourTurn,cl->playerint[player]));
  356. }
  357. void SaveGame::applyCl(CClient *cl)
  358. {
  359. CSaveFile save("Games" PATHSEPARATOR + fname + ".vcgm1");
  360. save << *cl;
  361. }
  362. void PlayerMessage::applyCl(CClient *cl)
  363. {
  364. tlog4 << "Player "<<(int)player<<" sends a message: " << text << std::endl;
  365. }
  366. void ShowInInfobox::applyCl(CClient *cl)
  367. {
  368. SComponent sc(c);
  369. sc.description = toString(text);
  370. if(cl->playerint[player]->human)
  371. {
  372. static_cast<CPlayerInterface*>(cl->playerint[player])->showComp(sc);
  373. }
  374. }