2
0

NetPacksClient.cpp 13 KB

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