NetPacksClient.cpp 12 KB

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