NetPacksClient.cpp 21 KB

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