NetPacksClient.cpp 24 KB

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