NetPacksClient.cpp 26 KB

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