2
0

NetPacksClient.cpp 13 KB

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