NetPacksClient.cpp 21 KB

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