NetPacksClient.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776
  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->visitablePos();
  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. //inform order about the change
  259. int p = -1;
  260. if(dw->ID == 106) //War Machines Factory is not flaggable, it's "owned" by visitor
  261. p = cl->getTile(dw->visitablePos())->visitableObjects.back()->tempOwner;
  262. else
  263. p = dw->tempOwner;
  264. INTERFACE_CALL_IF_PRESENT(p, availableCreaturesChanged, dw);
  265. }
  266. void SetHeroesInTown::applyCl( CClient *cl )
  267. {
  268. CGTownInstance *t = GS(cl)->getTown(tid);
  269. if(vstd::contains(cl->playerint,t->tempOwner))
  270. cl->playerint[t->tempOwner]->heroInGarrisonChange(t);
  271. }
  272. void SetHeroArtifacts::applyCl( CClient *cl )
  273. {
  274. CGHeroInstance *h = GS(cl)->getHero(hid);
  275. CGameInterface *player = (vstd::contains(cl->playerint,h->tempOwner) ? cl->playerint[h->tempOwner] : NULL);
  276. if(!player)
  277. return;
  278. h->recreateArtBonuses();
  279. player->heroArtifactSetChanged(h);
  280. BOOST_FOREACH(Bonus bonus, gained)
  281. {
  282. player->heroBonusChanged(h,bonus,true);
  283. }
  284. BOOST_FOREACH(Bonus bonus, lost)
  285. {
  286. player->heroBonusChanged(h,bonus,false);
  287. }
  288. }
  289. void HeroRecruited::applyCl( CClient *cl )
  290. {
  291. CGHeroInstance *h = GS(cl)->map->heroes.back();
  292. if(h->subID != hid)
  293. {
  294. tlog1 << "Something wrong with hero recruited!\n";
  295. }
  296. CGI->mh->initHeroDef(h);
  297. CGI->mh->printObject(h);
  298. if(vstd::contains(cl->playerint,h->tempOwner))
  299. {
  300. cl->playerint[h->tempOwner]->heroCreated(h);
  301. cl->playerint[h->tempOwner]->heroInGarrisonChange(GS(cl)->getTown(tid));
  302. }
  303. }
  304. void GiveHero::applyCl( CClient *cl )
  305. {
  306. CGHeroInstance *h = GS(cl)->getHero(id);
  307. CGI->mh->initHeroDef(h);
  308. CGI->mh->printObject(h);
  309. cl->playerint[h->tempOwner]->heroCreated(h);
  310. }
  311. void GiveHero::applyFirstCl( CClient *cl )
  312. {
  313. CGI->mh->hideObject(GS(cl)->getHero(id));
  314. }
  315. void InfoWindow::applyCl( CClient *cl )
  316. {
  317. std::vector<Component*> comps;
  318. for(size_t i=0;i<components.size();i++)
  319. {
  320. comps.push_back(&components[i]);
  321. }
  322. std::string str;
  323. text.toString(str);
  324. if(vstd::contains(cl->playerint,player))
  325. cl->playerint[player]->showInfoDialog(str,comps,(soundBase::soundID)soundID);
  326. else
  327. tlog2 << "We received InfoWindow for not our player...\n";
  328. }
  329. void SetObjectProperty::applyCl( CClient *cl )
  330. {
  331. //inform all players that see this object
  332. for(std::map<ui8,CGameInterface *>::const_iterator it = cl->playerint.begin(); it != cl->playerint.end(); ++it)
  333. {
  334. if(GS(cl)->isVisible(GS(cl)->map->objects[id], it->first))
  335. INTERFACE_CALL_IF_PRESENT(it->first, objectPropertyChanged, this);
  336. }
  337. }
  338. void HeroLevelUp::applyCl( CClient *cl )
  339. {
  340. CGHeroInstance *h = GS(cl)->getHero(heroid);
  341. if(vstd::contains(cl->playerint,h->tempOwner))
  342. {
  343. boost::function<void(ui32)> callback = boost::function<void(ui32)>(boost::bind(&CCallback::selectionMade,LOCPLINT->cb,_1,id));
  344. cl->playerint[h->tempOwner]->heroGotLevel(const_cast<const CGHeroInstance*>(h),static_cast<int>(primskill),skills, callback);
  345. }
  346. }
  347. void BlockingDialog::applyCl( CClient *cl )
  348. {
  349. std::string str;
  350. text.toString(str);
  351. if(vstd::contains(cl->playerint,player))
  352. cl->playerint[player]->showBlockingDialog(str,components,id,(soundBase::soundID)soundID,selection(),cancel());
  353. else
  354. tlog2 << "We received YesNoDialog for not our player...\n";
  355. }
  356. void GarrisonDialog::applyCl(CClient *cl)
  357. {
  358. const CGHeroInstance *h = cl->getHero(hid);
  359. const CArmedInstance *obj = static_cast<const CArmedInstance*>(cl->getObj(objid));
  360. if(!vstd::contains(cl->playerint,h->getOwner()))
  361. return;
  362. boost::function<void()> callback = boost::bind(&CCallback::selectionMade,LOCPLINT->cb,0,id);
  363. cl->playerint[h->getOwner()]->showGarrisonDialog(obj,h,removableUnits,callback);
  364. }
  365. void BattleStart::applyCl( CClient *cl )
  366. {
  367. CPlayerInterface * att, * def;
  368. if(vstd::contains(cl->playerint, info->side1) && cl->playerint[info->side1]->human)
  369. att = static_cast<CPlayerInterface*>( cl->playerint[info->side1] );
  370. else
  371. att = NULL;
  372. if(vstd::contains(cl->playerint, info->side2) && cl->playerint[info->side2]->human)
  373. def = static_cast<CPlayerInterface*>( cl->playerint[info->side2] );
  374. else
  375. def = NULL;
  376. new CBattleInterface(info->belligerents[0], info->belligerents[1], info->heroes[0], info->heroes[1], genRect(600, 800, (conf.cc.resx - 800)/2, (conf.cc.resy - 600)/2), att, def);
  377. if(vstd::contains(cl->playerint,info->side1))
  378. cl->playerint[info->side1]->battleStart(info->belligerents[0], info->belligerents[1], info->tile, info->heroes[0], info->heroes[1], 0);
  379. if(vstd::contains(cl->playerint,info->side2))
  380. cl->playerint[info->side2]->battleStart(info->belligerents[0], info->belligerents[1], info->tile, info->heroes[0], info->heroes[1], 1);
  381. }
  382. void BattleNextRound::applyFirstCl(CClient *cl)
  383. {
  384. if(cl->playerint.find(GS(cl)->curB->side1) != cl->playerint.end())
  385. cl->playerint[GS(cl)->curB->side1]->battleNewRoundFirst(round);
  386. if(cl->playerint.find(GS(cl)->curB->side2) != cl->playerint.end())
  387. cl->playerint[GS(cl)->curB->side2]->battleNewRoundFirst(round);
  388. }
  389. void BattleNextRound::applyCl( CClient *cl )
  390. {
  391. if(cl->playerint.find(GS(cl)->curB->side1) != cl->playerint.end())
  392. cl->playerint[GS(cl)->curB->side1]->battleNewRound(round);
  393. if(cl->playerint.find(GS(cl)->curB->side2) != cl->playerint.end())
  394. cl->playerint[GS(cl)->curB->side2]->battleNewRound(round);
  395. }
  396. void BattleSetActiveStack::applyCl( CClient *cl )
  397. {
  398. CStack * activated = GS(cl)->curB->getStack(stack);
  399. int playerToCall = -1; //player that will move activated stack
  400. if( activated->hasBonusOfType(Bonus::HYPNOTIZED) )
  401. {
  402. playerToCall = ( GS(cl)->curB->side1 == activated->owner ? GS(cl)->curB->side2 : GS(cl)->curB->side1 );
  403. }
  404. else
  405. {
  406. playerToCall = activated->owner;
  407. }
  408. if( vstd::contains(cl->playerint, playerToCall) )
  409. boost::thread( boost::bind(&CClient::waitForMoveAndSend, cl, playerToCall) );
  410. }
  411. void BattleResult::applyFirstCl( CClient *cl )
  412. {
  413. if(cl->playerint.find(GS(cl)->curB->side1) != cl->playerint.end())
  414. cl->playerint[GS(cl)->curB->side1]->battleEnd(this);
  415. if(cl->playerint.find(GS(cl)->curB->side2) != cl->playerint.end())
  416. cl->playerint[GS(cl)->curB->side2]->battleEnd(this);
  417. }
  418. void BattleStackMoved::applyFirstCl( CClient *cl )
  419. {
  420. INTERFACE_CALL_IF_PRESENT(GS(cl)->curB->side1,battleStackMoved,stack,tile,distance,ending);
  421. INTERFACE_CALL_IF_PRESENT(GS(cl)->curB->side2,battleStackMoved,stack,tile,distance,ending);
  422. }
  423. void BattleStackAttacked::applyCl( CClient *cl )
  424. {
  425. std::vector<BattleStackAttacked> bsa;
  426. bsa.push_back(*this);
  427. INTERFACE_CALL_IF_PRESENT(GS(cl)->curB->side1,battleStacksAttacked,bsa);
  428. INTERFACE_CALL_IF_PRESENT(GS(cl)->curB->side2,battleStacksAttacked,bsa);
  429. }
  430. void BattleAttack::applyFirstCl( CClient *cl )
  431. {
  432. if(cl->playerint.find(GS(cl)->curB->side1) != cl->playerint.end())
  433. cl->playerint[GS(cl)->curB->side1]->battleAttack(this);
  434. if(cl->playerint.find(GS(cl)->curB->side2) != cl->playerint.end())
  435. cl->playerint[GS(cl)->curB->side2]->battleAttack(this);
  436. for (int g=0; g<bsa.size(); ++g)
  437. {
  438. for (int z=0; z<bsa[g].healedStacks.size(); ++z)
  439. {
  440. bsa[g].healedStacks[z].applyCl(cl);
  441. }
  442. }
  443. }
  444. void BattleAttack::applyCl( CClient *cl )
  445. {
  446. INTERFACE_CALL_IF_PRESENT(GS(cl)->curB->side1,battleStacksAttacked,bsa);
  447. INTERFACE_CALL_IF_PRESENT(GS(cl)->curB->side2,battleStacksAttacked,bsa);
  448. }
  449. void StartAction::applyFirstCl( CClient *cl )
  450. {
  451. cl->curbaction = new BattleAction(ba);
  452. if(cl->playerint.find(GS(cl)->curB->side1) != cl->playerint.end())
  453. cl->playerint[GS(cl)->curB->side1]->actionStarted(&ba);
  454. if(cl->playerint.find(GS(cl)->curB->side2) != cl->playerint.end())
  455. cl->playerint[GS(cl)->curB->side2]->actionStarted(&ba);
  456. }
  457. void BattleSpellCast::applyCl( CClient *cl )
  458. {
  459. if(cl->playerint.find(GS(cl)->curB->side1) != cl->playerint.end())
  460. cl->playerint[GS(cl)->curB->side1]->battleSpellCast(this);
  461. if(cl->playerint.find(GS(cl)->curB->side2) != cl->playerint.end())
  462. cl->playerint[GS(cl)->curB->side2]->battleSpellCast(this);
  463. if(id >= 66 && id <= 69) //elemental summoning
  464. {
  465. if(cl->playerint.find(GS(cl)->curB->side1) != cl->playerint.end())
  466. cl->playerint[GS(cl)->curB->side1]->battleNewStackAppeared(GS(cl)->curB->stacks.size() - 1);
  467. if(cl->playerint.find(GS(cl)->curB->side2) != cl->playerint.end())
  468. cl->playerint[GS(cl)->curB->side2]->battleNewStackAppeared(GS(cl)->curB->stacks.size() - 1);
  469. }
  470. }
  471. void SetStackEffect::applyCl( CClient *cl )
  472. {
  473. BattleSpellCast sc;
  474. sc.id = effect.id;
  475. sc.side = 3; //doesn't matter
  476. sc.skill = effect.level;
  477. //informing about effects
  478. if(cl->playerint.find(GS(cl)->curB->side1) != cl->playerint.end())
  479. cl->playerint[GS(cl)->curB->side1]->battleStacksEffectsSet(*this);
  480. if(cl->playerint.find(GS(cl)->curB->side2) != cl->playerint.end())
  481. cl->playerint[GS(cl)->curB->side2]->battleStacksEffectsSet(*this);
  482. }
  483. void StacksInjured::applyCl( CClient *cl )
  484. {
  485. INTERFACE_CALL_IF_PRESENT(GS(cl)->curB->side1,battleStacksAttacked,stacks);
  486. INTERFACE_CALL_IF_PRESENT(GS(cl)->curB->side2,battleStacksAttacked,stacks);
  487. }
  488. void BattleResultsApplied::applyCl( CClient *cl )
  489. {
  490. INTERFACE_CALL_IF_PRESENT(player1,battleResultsApplied);
  491. INTERFACE_CALL_IF_PRESENT(player2,battleResultsApplied);
  492. }
  493. void StacksHealedOrResurrected::applyCl( CClient *cl )
  494. {
  495. std::vector<std::pair<ui32, ui32> > shiftedHealed;
  496. for(int v=0; v<healedStacks.size(); ++v)
  497. {
  498. shiftedHealed.push_back(std::make_pair(healedStacks[v].stackID, healedStacks[v].healedHP));
  499. }
  500. INTERFACE_CALL_IF_PRESENT(GS(cl)->curB->side1, battleStacksHealedRes, shiftedHealed, lifeDrain, drainedFrom);
  501. INTERFACE_CALL_IF_PRESENT(GS(cl)->curB->side2, battleStacksHealedRes, shiftedHealed, lifeDrain, drainedFrom);
  502. }
  503. void ObstaclesRemoved::applyCl( CClient *cl )
  504. {
  505. //inform interfaces about removed obstacles
  506. INTERFACE_CALL_IF_PRESENT(GS(cl)->curB->side1, battleObstaclesRemoved, obstacles);
  507. INTERFACE_CALL_IF_PRESENT(GS(cl)->curB->side2, battleObstaclesRemoved, obstacles);
  508. }
  509. void CatapultAttack::applyCl( CClient *cl )
  510. {
  511. //inform interfaces about catapult attack
  512. INTERFACE_CALL_IF_PRESENT(GS(cl)->curB->side1, battleCatapultAttacked, *this);
  513. INTERFACE_CALL_IF_PRESENT(GS(cl)->curB->side2, battleCatapultAttacked, *this);
  514. }
  515. void BattleStacksRemoved::applyCl( CClient *cl )
  516. {
  517. //inform interfaces about removed stacks
  518. INTERFACE_CALL_IF_PRESENT(GS(cl)->curB->side1, battleStacksRemoved, *this);
  519. INTERFACE_CALL_IF_PRESENT(GS(cl)->curB->side2, battleStacksRemoved, *this);
  520. }
  521. CGameState* CPackForClient::GS( CClient *cl )
  522. {
  523. return cl->gs;
  524. }
  525. void EndAction::applyCl( CClient *cl )
  526. {
  527. INTERFACE_CALL_IF_PRESENT(GS(cl)->curB->side1,actionFinished,cl->curbaction);
  528. INTERFACE_CALL_IF_PRESENT(GS(cl)->curB->side2,actionFinished,cl->curbaction);
  529. delete cl->curbaction;
  530. cl->curbaction = NULL;
  531. }
  532. void PackageApplied::applyCl( CClient *cl )
  533. {
  534. ui8 player = GS(cl)->currentPlayer;
  535. INTERFACE_CALL_IF_PRESENT(player, requestRealized, this);
  536. if(cl->waitingRequest.get())
  537. cl->waitingRequest.setn(false);
  538. }
  539. void SystemMessage::applyCl( CClient *cl )
  540. {
  541. std::ostringstream str;
  542. str << "System message: " << text;
  543. tlog4 << str.str() << std::endl;
  544. if(LOCPLINT)
  545. LOCPLINT->cingconsole->print(str.str());
  546. }
  547. void PlayerBlocked::applyCl( CClient *cl )
  548. {
  549. INTERFACE_CALL_IF_PRESENT(player,playerBlocked,reason);
  550. }
  551. void YourTurn::applyCl( CClient *cl )
  552. {
  553. INTERFACE_CALL_IF_PRESENT(player,yourTurn);
  554. }
  555. void SaveGame::applyCl(CClient *cl)
  556. {
  557. CSaveFile save(GVCMIDirs.UserPath + "/Games/" + fname + ".vcgm1");
  558. save << *cl;
  559. }
  560. void PlayerMessage::applyCl(CClient *cl)
  561. {
  562. std::ostringstream str;
  563. str << "Player "<<(int)player<<" sends a message: " << text;
  564. tlog4 << str.str() << std::endl;
  565. if(LOCPLINT)
  566. LOCPLINT->cingconsole->print(str.str());
  567. }
  568. void SetSelection::applyCl(CClient *cl)
  569. {
  570. const CGHeroInstance *h = cl->getHero(id);
  571. if(!h)
  572. return;
  573. //CPackForClient::GS(cl)->calculatePaths(h, *cl->pathInfo);
  574. }
  575. void ShowInInfobox::applyCl(CClient *cl)
  576. {
  577. SComponent sc(c);
  578. text.toString(sc.description);
  579. if(cl->playerint[player]->human)
  580. {
  581. static_cast<CPlayerInterface*>(cl->playerint[player])->showComp(sc);
  582. }
  583. }
  584. void AdvmapSpellCast::applyCl(CClient *cl)
  585. {
  586. cl->playerint[caster->getOwner()]->advmapSpellCast(caster, spellID);
  587. }
  588. void OpenWindow::applyCl(CClient *cl)
  589. {
  590. switch(window)
  591. {
  592. case EXCHANGE_WINDOW:
  593. {
  594. const CGHeroInstance *h = cl->getHero(id1);
  595. const CGObjectInstance *h2 = cl->getHero(id2);
  596. assert(h && h2);
  597. INTERFACE_CALL_IF_PRESENT(h->tempOwner,heroExchangeStarted, id1, id2);
  598. }
  599. break;
  600. case RECRUITMENT_FIRST:
  601. case RECRUITMENT_ALL:
  602. {
  603. const CGDwelling *dw = dynamic_cast<const CGDwelling*>(cl->getObj(id1));
  604. const CArmedInstance *dst = dynamic_cast<const CArmedInstance*>(cl->getObj(id2));
  605. INTERFACE_CALL_IF_PRESENT(dst->tempOwner,showRecruitmentDialog, dw, dst, window == RECRUITMENT_FIRST ? 0 : -1);
  606. }
  607. break;
  608. case SHIPYARD_WINDOW:
  609. {
  610. const IShipyard *sy = IShipyard::castFrom(cl->getObj(id1));
  611. INTERFACE_CALL_IF_PRESENT(sy->o->tempOwner, showShipyardDialog, sy);
  612. }
  613. break;
  614. case THIEVES_GUILD:
  615. {
  616. //displays Thieves' Guild window (when hero enters Den of Thieves)
  617. const CGObjectInstance *obj = cl->getObj(id1);
  618. GH.pushInt( new CThievesGuildWindow(obj) );
  619. }
  620. break;
  621. case MARKET_WINDOW:
  622. {
  623. //displays Thieves' Guild window (when hero enters Den of Thieves)
  624. const CGObjectInstance *obj = cl->getObj(id1);
  625. const CGHeroInstance *hero = cl->getHero(id2);
  626. const IMarket *market = IMarket::castFrom(obj);
  627. INTERFACE_CALL_IF_PRESENT(cl->getTile(obj->visitablePos())->visitableObjects.back()->tempOwner, showMarketWindow, market, hero);
  628. }
  629. break;
  630. case PUZZLE_MAP:
  631. {
  632. INTERFACE_CALL_IF_PRESENT(id1, showPuzzleMap);
  633. }
  634. break;
  635. }
  636. }
  637. void CenterView::applyCl(CClient *cl)
  638. {
  639. INTERFACE_CALL_IF_PRESENT (player, centerView, pos, focusTime);
  640. }
  641. void NewObject::applyCl(CClient *cl)
  642. {
  643. const CGObjectInstance *obj = cl->getObj(id);
  644. //notify interfaces about move
  645. for(std::map<ui8, CGameInterface*>::iterator i=cl->playerint.begin();i!=cl->playerint.end();i++)
  646. {
  647. //TODO: check if any covered tile is visible
  648. if(i->first >= PLAYER_LIMIT) continue;
  649. if(GS(cl)->players[i->first].fogOfWarMap[obj->pos.x][obj->pos.y][obj->pos.z])
  650. {
  651. i->second->newObject(obj);
  652. }
  653. }
  654. }
  655. void TradeComponents::applyCl(CClient *cl)
  656. {///Shop handler
  657. switch (CGI->mh->map->objects[objectid]->ID)
  658. {
  659. case 7: //Black Market
  660. break;
  661. case 95: //Tavern
  662. break;
  663. case 97: //Den of Thieves
  664. break;
  665. case 221: //Trading Post
  666. break;
  667. default:
  668. tlog2 << "Shop type not supported! \n";
  669. }
  670. }