NetPacksClient.cpp 24 KB

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