NetPacksClient.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713
  1. #include "../lib/NetPacks.h"
  2. #include "../CCallback.h"
  3. #include "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 "../lib/map.h"
  13. #include "../lib/VCMIDirs.h"
  14. #include "../hch/CSpellHandler.h"
  15. #include "../hch/CSoundBase.h"
  16. #include "../mapHandler.h"
  17. #include "GUIClasses.h"
  18. #include <boost/bind.hpp>
  19. #include <boost/foreach.hpp>
  20. #include <boost/thread.hpp>
  21. #include <boost/thread/shared_mutex.hpp>
  22. //macro to avoid code duplication - calls given method with given arguments if interface for specific player is present
  23. #define INTERFACE_CALL_IF_PRESENT(player,function,...) \
  24. if(vstd::contains(cl->playerint,player)) \
  25. cl->playerint[player]->function(__VA_ARGS__);
  26. /*
  27. * NetPacksClient.cpp, part of VCMI engine
  28. *
  29. * Authors: listed in file AUTHORS in main folder
  30. *
  31. * License: GNU General Public License v2.0 or later
  32. * Full text of license available in license.txt file, in main folder
  33. *
  34. */
  35. void SetResources::applyCl( CClient *cl )
  36. {
  37. cl->playerint[player]->receivedResource(-1,-1);
  38. }
  39. void SetResource::applyCl( CClient *cl )
  40. {
  41. cl->playerint[player]->receivedResource(resid,val);
  42. }
  43. void SetPrimSkill::applyCl( CClient *cl )
  44. {
  45. const CGHeroInstance *h = GS(cl)->getHero(id);
  46. if(!h)
  47. {
  48. tlog1 << "Cannot find hero with ID " << id << std::endl;
  49. return;
  50. }
  51. INTERFACE_CALL_IF_PRESENT(h->tempOwner,heroPrimarySkillChanged,h,which,val);
  52. }
  53. void SetSecSkill::applyCl( CClient *cl )
  54. {
  55. //TODO: inform interface?
  56. }
  57. void HeroVisitCastle::applyCl( CClient *cl )
  58. {
  59. if(start() && !garrison() && vstd::contains(cl->playerint,GS(cl)->getHero(hid)->tempOwner))
  60. {
  61. cl->playerint[GS(cl)->getHero(hid)->tempOwner]->heroVisitsTown(GS(cl)->getHero(hid),GS(cl)->getTown(tid));
  62. }
  63. }
  64. void ChangeSpells::applyCl( CClient *cl )
  65. {
  66. //TODO: inform interface?
  67. }
  68. void SetMana::applyCl( CClient *cl )
  69. {
  70. CGHeroInstance *h = GS(cl)->getHero(hid);
  71. if(vstd::contains(cl->playerint,h->tempOwner))
  72. cl->playerint[h->tempOwner]->heroManaPointsChanged(h);
  73. }
  74. void SetMovePoints::applyCl( CClient *cl )
  75. {
  76. CGHeroInstance *h = GS(cl)->getHero(hid);
  77. if(vstd::contains(cl->playerint,h->tempOwner))
  78. cl->playerint[h->tempOwner]->heroMovePointsChanged(h);
  79. }
  80. void FoWChange::applyCl( CClient *cl )
  81. {
  82. if(!vstd::contains(cl->playerint,player))
  83. return;
  84. if(mode)
  85. cl->playerint[player]->tileRevealed(tiles);
  86. else
  87. cl->playerint[player]->tileHidden(tiles);
  88. if (cl->IGameCallback::getSelectedHero(player))//if we have selected hero...
  89. GS(cl)->calculatePaths(cl->IGameCallback::getSelectedHero(player), *cl->pathInfo);
  90. }
  91. void SetAvailableHeroes::applyCl( CClient *cl )
  92. {
  93. //TODO: inform interface?
  94. }
  95. void GiveBonus::applyCl( CClient *cl )
  96. {
  97. switch(who)
  98. {
  99. case HERO:
  100. {
  101. const CGHeroInstance *h = GS(cl)->getHero(id);
  102. INTERFACE_CALL_IF_PRESENT(h->tempOwner, heroBonusChanged, h, h->bonuses.back(),true);
  103. }
  104. break;
  105. case PLAYER:
  106. {
  107. const PlayerState *p = GS(cl)->getPlayer(id);
  108. INTERFACE_CALL_IF_PRESENT(id, playerBonusChanged, p->bonuses.back(), true);
  109. }
  110. break;
  111. }
  112. }
  113. void ChangeObjPos::applyFirstCl( CClient *cl )
  114. {
  115. CGObjectInstance *obj = GS(cl)->map->objects[objid];
  116. if(flags & 1)
  117. CGI->mh->hideObject(obj);
  118. }
  119. void ChangeObjPos::applyCl( CClient *cl )
  120. {
  121. CGObjectInstance *obj = GS(cl)->map->objects[objid];
  122. if(flags & 1)
  123. CGI->mh->printObject(obj);
  124. }
  125. void PlayerEndsGame::applyCl( CClient *cl )
  126. {
  127. for(std::map<ui8, CGameInterface*>::iterator i=cl->playerint.begin();i!=cl->playerint.end();i++)
  128. i->second->gameOver(player, victory);
  129. }
  130. void RemoveBonus::applyCl( CClient *cl )
  131. {
  132. switch(who)
  133. {
  134. case HERO:
  135. {
  136. const CGHeroInstance *h = GS(cl)->getHero(id);
  137. INTERFACE_CALL_IF_PRESENT(h->tempOwner, heroBonusChanged, h, bonus,false);
  138. }
  139. break;
  140. case PLAYER:
  141. {
  142. const PlayerState *p = GS(cl)->getPlayer(id);
  143. INTERFACE_CALL_IF_PRESENT(id, playerBonusChanged, bonus, false);
  144. }
  145. break;
  146. }
  147. }
  148. void RemoveObject::applyFirstCl( CClient *cl )
  149. {
  150. const CGObjectInstance *o = cl->getObj(id);
  151. CGI->mh->hideObject(o);
  152. int3 pos = o->pos - o->getVisitableOffset();
  153. //notify interfaces about removal
  154. for(std::map<ui8, CGameInterface*>::iterator i=cl->playerint.begin();i!=cl->playerint.end();i++)
  155. {
  156. if(i->first >= PLAYER_LIMIT) continue;
  157. if(GS(cl)->players[i->first].fogOfWarMap[pos.x][pos.y][pos.z])
  158. {
  159. i->second->objectRemoved(o);
  160. }
  161. }
  162. }
  163. void RemoveObject::applyCl( CClient *cl )
  164. {
  165. if(cl->pathInfo->hero && cl->pathInfo->hero->id != id)
  166. GS(cl)->calculatePaths(cl->pathInfo->hero, *cl->pathInfo);
  167. }
  168. void TryMoveHero::applyFirstCl( CClient *cl )
  169. {
  170. CGHeroInstance *h = GS(cl)->getHero(id);
  171. //check if playerint will have the knowledge about movement - if not, directly update maphandler
  172. for(std::map<ui8, CGameInterface*>::iterator i=cl->playerint.begin();i!=cl->playerint.end();i++)
  173. {
  174. if(i->first >= PLAYER_LIMIT)
  175. continue;
  176. PlayerState &p = GS(cl)->players[i->first];
  177. if((p.fogOfWarMap[start.x-1][start.y][start.z] || p.fogOfWarMap[end.x-1][end.y][end.z]) && p.human)
  178. humanKnows = true;
  179. }
  180. if(result == TELEPORTATION || result == EMBARK || result == DISEMBARK || !humanKnows)
  181. CGI->mh->removeObject(h);
  182. if(result == DISEMBARK)
  183. CGI->mh->printObject(h->boat);
  184. }
  185. void TryMoveHero::applyCl( CClient *cl )
  186. {
  187. const CGHeroInstance *h = cl->getHero(id);
  188. if(result == TELEPORTATION || result == EMBARK || result == DISEMBARK)
  189. CGI->mh->printObject(h);
  190. if(result == EMBARK)
  191. CGI->mh->hideObject(h->boat);
  192. int player = h->tempOwner;
  193. if(vstd::contains(cl->playerint,player))
  194. {
  195. cl->playerint[player]->tileRevealed(fowRevealed);
  196. }
  197. //notify interfaces about move
  198. for(std::map<ui8, CGameInterface*>::iterator i=cl->playerint.begin();i!=cl->playerint.end();i++)
  199. {
  200. if(i->first >= PLAYER_LIMIT) continue;
  201. PlayerState &p = GS(cl)->players[i->first];
  202. if(p.fogOfWarMap[start.x-1][start.y][start.z] || p.fogOfWarMap[end.x-1][end.y][end.z])
  203. {
  204. i->second->heroMoved(*this);
  205. }
  206. }
  207. if(!humanKnows) //maphandler didn't get update from playerint, do it now
  208. { //TODO: restructure nicely
  209. CGI->mh->printObject(h);
  210. }
  211. }
  212. void SetGarrisons::applyCl( CClient *cl )
  213. {
  214. for(std::map<ui32,CCreatureSet>::iterator i = garrs.begin(); i!=garrs.end(); i++)
  215. if(vstd::contains(cl->playerint,cl->getOwner(i->first)))
  216. cl->playerint[cl->getOwner(i->first)]->garrisonChanged(cl->getObj(i->first));
  217. }
  218. void NewStructures::applyCl( CClient *cl )
  219. {
  220. CGTownInstance *town = GS(cl)->getTown(tid);
  221. BOOST_FOREACH(si32 id, bid)
  222. {
  223. if(id==13) //fort or capitol
  224. {
  225. town->defInfo = GS(cl)->capitols[town->subID];
  226. }
  227. if(id ==7)
  228. {
  229. town->defInfo = GS(cl)->forts[town->subID];
  230. }
  231. if(vstd::contains(cl->playerint,town->tempOwner))
  232. cl->playerint[town->tempOwner]->buildChanged(town,id,1);
  233. }
  234. }
  235. void RazeStructures::applyCl (CClient *cl)
  236. {
  237. CGTownInstance *town = GS(cl)->getTown(tid);
  238. BOOST_FOREACH(si32 id, bid)
  239. {
  240. if (id == 13) //fort or capitol
  241. {
  242. town->defInfo = GS(cl)->forts[town->subID];
  243. }
  244. if(vstd::contains (cl->playerint,town->tempOwner))
  245. cl->playerint[town->tempOwner]->buildChanged (town,id,2);
  246. }
  247. }
  248. void SetAvailableCreatures::applyCl( CClient *cl )
  249. {
  250. const CGDwelling *dw = static_cast<const CGDwelling*>(cl->getObj(tid));
  251. if(vstd::contains(cl->playerint,dw->tempOwner))
  252. cl->playerint[dw->tempOwner]->availableCreaturesChanged(dw);
  253. }
  254. void SetHeroesInTown::applyCl( CClient *cl )
  255. {
  256. CGTownInstance *t = GS(cl)->getTown(tid);
  257. if(vstd::contains(cl->playerint,t->tempOwner))
  258. cl->playerint[t->tempOwner]->heroInGarrisonChange(t);
  259. }
  260. void SetHeroArtifacts::applyCl( CClient *cl )
  261. {
  262. CGHeroInstance *h = GS(cl)->getHero(hid);
  263. CGameInterface *player = (vstd::contains(cl->playerint,h->tempOwner) ? cl->playerint[h->tempOwner] : NULL);
  264. if(!player)
  265. return;
  266. player->heroArtifactSetChanged(h);
  267. BOOST_FOREACH(HeroBonus *bonus, gained)
  268. {
  269. player->heroBonusChanged(h,*bonus,true);
  270. }
  271. BOOST_FOREACH(HeroBonus *bonus, lost)
  272. {
  273. player->heroBonusChanged(h,*bonus,false);
  274. }
  275. }
  276. void HeroRecruited::applyCl( CClient *cl )
  277. {
  278. CGHeroInstance *h = GS(cl)->map->heroes.back();
  279. if(h->subID != hid)
  280. {
  281. tlog1 << "Something wrong with hero recruited!\n";
  282. }
  283. CGI->mh->initHeroDef(h);
  284. CGI->mh->printObject(h);
  285. if(vstd::contains(cl->playerint,h->tempOwner))
  286. {
  287. cl->playerint[h->tempOwner]->heroCreated(h);
  288. cl->playerint[h->tempOwner]->heroInGarrisonChange(GS(cl)->getTown(tid));
  289. }
  290. }
  291. void GiveHero::applyCl( CClient *cl )
  292. {
  293. CGHeroInstance *h = GS(cl)->getHero(id);
  294. CGI->mh->initHeroDef(h);
  295. CGI->mh->printObject(h);
  296. cl->playerint[h->tempOwner]->heroCreated(h);
  297. }
  298. void GiveHero::applyFirstCl( CClient *cl )
  299. {
  300. CGI->mh->hideObject(GS(cl)->getHero(id));
  301. }
  302. void InfoWindow::applyCl( CClient *cl )
  303. {
  304. std::vector<Component*> comps;
  305. for(size_t i=0;i<components.size();i++)
  306. {
  307. comps.push_back(&components[i]);
  308. }
  309. std::string str;
  310. text.toString(str);
  311. if(vstd::contains(cl->playerint,player))
  312. cl->playerint[player]->showInfoDialog(str,comps,(soundBase::soundID)soundID);
  313. else
  314. tlog2 << "We received InfoWindow for not our player...\n";
  315. }
  316. void SetObjectProperty::applyCl( CClient *cl )
  317. {
  318. //inform all players that see this object
  319. for(std::map<ui8,CGameInterface *>::const_iterator it = cl->playerint.begin(); it != cl->playerint.end(); ++it)
  320. {
  321. if(GS(cl)->isVisible(GS(cl)->map->objects[id], it->first))
  322. INTERFACE_CALL_IF_PRESENT(it->first, objectPropertyChanged, this);
  323. }
  324. }
  325. void HeroLevelUp::applyCl( CClient *cl )
  326. {
  327. CGHeroInstance *h = GS(cl)->getHero(heroid);
  328. if(vstd::contains(cl->playerint,h->tempOwner))
  329. {
  330. boost::function<void(ui32)> callback = boost::function<void(ui32)>(boost::bind(&CCallback::selectionMade,LOCPLINT->cb,_1,id));
  331. cl->playerint[h->tempOwner]->heroGotLevel(const_cast<const CGHeroInstance*>(h),static_cast<int>(primskill),skills, callback);
  332. }
  333. }
  334. void BlockingDialog::applyCl( CClient *cl )
  335. {
  336. std::string str;
  337. text.toString(str);
  338. if(vstd::contains(cl->playerint,player))
  339. cl->playerint[player]->showBlockingDialog(str,components,id,(soundBase::soundID)soundID,selection(),cancel());
  340. else
  341. tlog2 << "We received YesNoDialog for not our player...\n";
  342. }
  343. void GarrisonDialog::applyCl(CClient *cl)
  344. {
  345. const CGHeroInstance *h = cl->getHero(hid);
  346. const CArmedInstance *obj = static_cast<const CArmedInstance*>(cl->getObj(objid));
  347. if(!vstd::contains(cl->playerint,h->getOwner()))
  348. return;
  349. boost::function<void()> callback = boost::bind(&CCallback::selectionMade,LOCPLINT->cb,0,id);
  350. cl->playerint[h->getOwner()]->showGarrisonDialog(obj,h,removableUnits,callback);
  351. }
  352. void BattleStart::applyCl( CClient *cl )
  353. {
  354. if(vstd::contains(cl->playerint,info->side1))
  355. cl->playerint[info->side1]->battleStart(&info->army1, &info->army2, info->tile, info->heroes[0], info->heroes[1], 0);
  356. if(vstd::contains(cl->playerint,info->side2))
  357. cl->playerint[info->side2]->battleStart(&info->army1, &info->army2, info->tile, info->heroes[0], info->heroes[1], 1);
  358. }
  359. void BattleNextRound::applyCl( CClient *cl )
  360. {
  361. if(cl->playerint.find(GS(cl)->curB->side1) != cl->playerint.end())
  362. cl->playerint[GS(cl)->curB->side1]->battleNewRound(round);
  363. if(cl->playerint.find(GS(cl)->curB->side2) != cl->playerint.end())
  364. cl->playerint[GS(cl)->curB->side2]->battleNewRound(round);
  365. }
  366. void BattleSetActiveStack::applyCl( CClient *cl )
  367. {
  368. CStack * activated = GS(cl)->curB->getStack(stack);
  369. int playerToCall = -1; //player that will move activated stack
  370. if( activated->hasFeatureOfType(StackFeature::HYPNOTIZED) )
  371. {
  372. playerToCall = ( GS(cl)->curB->side1 == activated->owner ? GS(cl)->curB->side2 : GS(cl)->curB->side1 );
  373. }
  374. else
  375. {
  376. playerToCall = activated->owner;
  377. }
  378. if( vstd::contains(cl->playerint, playerToCall) )
  379. boost::thread( boost::bind(&CClient::waitForMoveAndSend, cl, playerToCall) );
  380. }
  381. void BattleResult::applyFirstCl( CClient *cl )
  382. {
  383. if(cl->playerint.find(GS(cl)->curB->side1) != cl->playerint.end())
  384. cl->playerint[GS(cl)->curB->side1]->battleEnd(this);
  385. if(cl->playerint.find(GS(cl)->curB->side2) != cl->playerint.end())
  386. cl->playerint[GS(cl)->curB->side2]->battleEnd(this);
  387. }
  388. void BattleStackMoved::applyFirstCl( CClient *cl )
  389. {
  390. INTERFACE_CALL_IF_PRESENT(GS(cl)->curB->side1,battleStackMoved,stack,tile,distance,ending);
  391. INTERFACE_CALL_IF_PRESENT(GS(cl)->curB->side2,battleStackMoved,stack,tile,distance,ending);
  392. }
  393. void BattleStackAttacked::applyCl( CClient *cl )
  394. {
  395. std::set<BattleStackAttacked> bsa;
  396. bsa.insert(*this);
  397. INTERFACE_CALL_IF_PRESENT(GS(cl)->curB->side1,battleStacksAttacked,bsa);
  398. INTERFACE_CALL_IF_PRESENT(GS(cl)->curB->side2,battleStacksAttacked,bsa);
  399. }
  400. void BattleAttack::applyFirstCl( CClient *cl )
  401. {
  402. if(cl->playerint.find(GS(cl)->curB->side1) != cl->playerint.end())
  403. cl->playerint[GS(cl)->curB->side1]->battleAttack(this);
  404. if(cl->playerint.find(GS(cl)->curB->side2) != cl->playerint.end())
  405. cl->playerint[GS(cl)->curB->side2]->battleAttack(this);
  406. }
  407. void BattleAttack::applyCl( CClient *cl )
  408. {
  409. INTERFACE_CALL_IF_PRESENT(GS(cl)->curB->side1,battleStacksAttacked,bsa);
  410. INTERFACE_CALL_IF_PRESENT(GS(cl)->curB->side2,battleStacksAttacked,bsa);
  411. }
  412. void StartAction::applyFirstCl( CClient *cl )
  413. {
  414. cl->curbaction = new BattleAction(ba);
  415. if(cl->playerint.find(GS(cl)->curB->side1) != cl->playerint.end())
  416. cl->playerint[GS(cl)->curB->side1]->actionStarted(&ba);
  417. if(cl->playerint.find(GS(cl)->curB->side2) != cl->playerint.end())
  418. cl->playerint[GS(cl)->curB->side2]->actionStarted(&ba);
  419. }
  420. void SpellCast::applyCl( CClient *cl )
  421. {
  422. if(cl->playerint.find(GS(cl)->curB->side1) != cl->playerint.end())
  423. cl->playerint[GS(cl)->curB->side1]->battleSpellCast(this);
  424. if(cl->playerint.find(GS(cl)->curB->side2) != cl->playerint.end())
  425. cl->playerint[GS(cl)->curB->side2]->battleSpellCast(this);
  426. if(id >= 66 && id <= 69) //elemental summoning
  427. {
  428. if(cl->playerint.find(GS(cl)->curB->side1) != cl->playerint.end())
  429. cl->playerint[GS(cl)->curB->side1]->battleNewStackAppeared(GS(cl)->curB->stacks.size() - 1);
  430. if(cl->playerint.find(GS(cl)->curB->side2) != cl->playerint.end())
  431. cl->playerint[GS(cl)->curB->side2]->battleNewStackAppeared(GS(cl)->curB->stacks.size() - 1);
  432. }
  433. }
  434. void SetStackEffect::applyCl( CClient *cl )
  435. {
  436. SpellCast sc;
  437. sc.id = effect.id;
  438. sc.side = 3; //doesn't matter
  439. sc.skill = effect.level;
  440. //informing about effects
  441. if(cl->playerint.find(GS(cl)->curB->side1) != cl->playerint.end())
  442. cl->playerint[GS(cl)->curB->side1]->battleStacksEffectsSet(*this);
  443. if(cl->playerint.find(GS(cl)->curB->side2) != cl->playerint.end())
  444. cl->playerint[GS(cl)->curB->side2]->battleStacksEffectsSet(*this);
  445. }
  446. void StacksInjured::applyCl( CClient *cl )
  447. {
  448. INTERFACE_CALL_IF_PRESENT(GS(cl)->curB->side1,battleStacksAttacked,stacks);
  449. INTERFACE_CALL_IF_PRESENT(GS(cl)->curB->side2,battleStacksAttacked,stacks);
  450. }
  451. void BattleResultsApplied::applyCl( CClient *cl )
  452. {
  453. INTERFACE_CALL_IF_PRESENT(player1,battleResultsApplied);
  454. INTERFACE_CALL_IF_PRESENT(player2,battleResultsApplied);
  455. }
  456. void StacksHealedOrResurrected::applyCl( CClient *cl )
  457. {
  458. std::vector<std::pair<ui32, ui32> > shiftedHealed;
  459. for(int v=0; v<healedStacks.size(); ++v)
  460. {
  461. shiftedHealed.push_back(std::make_pair(healedStacks[v].stackID, healedStacks[v].healedHP));
  462. }
  463. INTERFACE_CALL_IF_PRESENT(GS(cl)->curB->side1, battleStacksHealedRes, shiftedHealed);
  464. INTERFACE_CALL_IF_PRESENT(GS(cl)->curB->side2, battleStacksHealedRes, shiftedHealed);
  465. }
  466. void ObstaclesRemoved::applyCl( CClient *cl )
  467. {
  468. //inform interfaces about removed obstacles
  469. INTERFACE_CALL_IF_PRESENT(GS(cl)->curB->side1, battleObstaclesRemoved, obstacles);
  470. INTERFACE_CALL_IF_PRESENT(GS(cl)->curB->side2, battleObstaclesRemoved, obstacles);
  471. }
  472. void CatapultAttack::applyCl( CClient *cl )
  473. {
  474. //inform interfaces about catapult attack
  475. INTERFACE_CALL_IF_PRESENT(GS(cl)->curB->side1, battleCatapultAttacked, *this);
  476. INTERFACE_CALL_IF_PRESENT(GS(cl)->curB->side2, battleCatapultAttacked, *this);
  477. }
  478. void BattleStacksRemoved::applyCl( CClient *cl )
  479. {
  480. //inform interfaces about removed stacks
  481. INTERFACE_CALL_IF_PRESENT(GS(cl)->curB->side1, battleStacksRemoved, *this);
  482. INTERFACE_CALL_IF_PRESENT(GS(cl)->curB->side2, battleStacksRemoved, *this);
  483. }
  484. CGameState* CPackForClient::GS( CClient *cl )
  485. {
  486. return cl->gs;
  487. }
  488. void EndAction::applyCl( CClient *cl )
  489. {
  490. INTERFACE_CALL_IF_PRESENT(GS(cl)->curB->side1,actionFinished,cl->curbaction);
  491. INTERFACE_CALL_IF_PRESENT(GS(cl)->curB->side2,actionFinished,cl->curbaction);
  492. delete cl->curbaction;
  493. cl->curbaction = NULL;
  494. }
  495. void PackageApplied::applyCl( CClient *cl )
  496. {
  497. ui8 player = GS(cl)->currentPlayer;
  498. INTERFACE_CALL_IF_PRESENT(player, requestRealized, this);
  499. if(cl->waitingRequest.get())
  500. cl->waitingRequest.setn(false);
  501. }
  502. void SystemMessage::applyCl( CClient *cl )
  503. {
  504. std::ostringstream str;
  505. str << "System message: " << text;
  506. tlog4 << str.str() << std::endl;
  507. if(LOCPLINT)
  508. LOCPLINT->cingconsole->print(str.str());
  509. }
  510. void PlayerBlocked::applyCl( CClient *cl )
  511. {
  512. INTERFACE_CALL_IF_PRESENT(player,playerBlocked,reason);
  513. }
  514. void YourTurn::applyCl( CClient *cl )
  515. {
  516. INTERFACE_CALL_IF_PRESENT(player,yourTurn);
  517. }
  518. void SaveGame::applyCl(CClient *cl)
  519. {
  520. CSaveFile save(GVCMIDirs.UserPath + "/Games/" + fname + ".vcgm1");
  521. save << *cl;
  522. }
  523. void PlayerMessage::applyCl(CClient *cl)
  524. {
  525. std::ostringstream str;
  526. str << "Player "<<(int)player<<" sends a message: " << text;
  527. tlog4 << str.str() << std::endl;
  528. if(LOCPLINT)
  529. LOCPLINT->cingconsole->print(str.str());
  530. }
  531. void SetSelection::applyCl(CClient *cl)
  532. {
  533. const CGHeroInstance *h = cl->getHero(id);
  534. if(!h)
  535. return;
  536. //CPackForClient::GS(cl)->calculatePaths(h, *cl->pathInfo);
  537. }
  538. void ShowInInfobox::applyCl(CClient *cl)
  539. {
  540. SComponent sc(c);
  541. text.toString(sc.description);
  542. if(cl->playerint[player]->human)
  543. {
  544. static_cast<CPlayerInterface*>(cl->playerint[player])->showComp(sc);
  545. }
  546. }
  547. void OpenWindow::applyCl(CClient *cl)
  548. {
  549. switch(window)
  550. {
  551. case EXCHANGE_WINDOW:
  552. {
  553. const CGHeroInstance *h = cl->getHero(id1);
  554. const CGObjectInstance *h2 = cl->getHero(id2);
  555. assert(h && h2);
  556. INTERFACE_CALL_IF_PRESENT(h->tempOwner,heroExchangeStarted, id1, id2);
  557. }
  558. break;
  559. case RECRUITMENT_FIRST:
  560. case RECRUITMENT_ALL:
  561. {
  562. const CGDwelling *dw = dynamic_cast<const CGDwelling*>(cl->getObj(id1));
  563. const CArmedInstance *dst = dynamic_cast<const CArmedInstance*>(cl->getObj(id2));
  564. INTERFACE_CALL_IF_PRESENT(dw->tempOwner,showRecruitmentDialog, dw, dst, window == RECRUITMENT_FIRST ? 0 : -1);
  565. }
  566. break;
  567. case SHIPYARD_WINDOW:
  568. {
  569. const IShipyard *sy = IShipyard::castFrom(cl->getObj(id1));
  570. INTERFACE_CALL_IF_PRESENT(sy->o->tempOwner, showShipyardDialog, sy);
  571. }
  572. break;
  573. case THIEVES_GUILD:
  574. {
  575. //displays Thieves' Guild window (when hero enters Den of Thieves)
  576. const CGObjectInstance *obj = cl->getObj(id1);
  577. GH.pushInt( new CThievesGuildWindow(obj) );
  578. }
  579. break;
  580. case PUZZLE_MAP:
  581. {
  582. INTERFACE_CALL_IF_PRESENT(id1, showPuzzleMap);
  583. }
  584. break;
  585. }
  586. }
  587. void CenterView::applyCl(CClient *cl)
  588. {
  589. INTERFACE_CALL_IF_PRESENT (player, centerView, pos, focusTime);
  590. }
  591. void NewObject::applyCl(CClient *cl)
  592. {
  593. const CGObjectInstance *obj = cl->getObj(id);
  594. //notify interfaces about move
  595. for(std::map<ui8, CGameInterface*>::iterator i=cl->playerint.begin();i!=cl->playerint.end();i++)
  596. {
  597. //TODO: check if any covered tile is visible
  598. if(i->first >= PLAYER_LIMIT) continue;
  599. if(GS(cl)->players[i->first].fogOfWarMap[obj->pos.x][obj->pos.y][obj->pos.z])
  600. {
  601. i->second->newObject(obj);
  602. }
  603. }
  604. }
  605. void TradeComponents::applyCl(CClient *cl)
  606. {///Shop handler
  607. switch (CGI->mh->map->objects[objectid]->ID)
  608. {
  609. case 7: //Black Market
  610. break;
  611. case 95: //Tavern
  612. break;
  613. case 97: //Den of Thieves
  614. break;
  615. case 221: //Trading Post
  616. break;
  617. default:
  618. tlog2 << "Shop type not supported! \n";
  619. }
  620. }