NetPacksClient.cpp 12 KB

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