NetPacksClient.cpp 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042
  1. /*
  2. * NetPacksClient.cpp, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #include "StdInc.h"
  11. #include "ClientNetPackVisitors.h"
  12. #include "Client.h"
  13. #include "CPlayerInterface.h"
  14. #include "CGameInfo.h"
  15. #include "windows/GUIClasses.h"
  16. #include "mapView/mapHandler.h"
  17. #include "adventureMap/CInGameConsole.h"
  18. #include "battle/BattleInterface.h"
  19. #include "battle/BattleWindow.h"
  20. #include "gui/CGuiHandler.h"
  21. #include "gui/WindowHandler.h"
  22. #include "widgets/MiscWidgets.h"
  23. #include "CMT.h"
  24. #include "CServerHandler.h"
  25. #include "../CCallback.h"
  26. #include "../lib/filesystem/Filesystem.h"
  27. #include "../lib/filesystem/FileInfo.h"
  28. #include "../lib/serializer/Connection.h"
  29. #include "../lib/serializer/BinarySerializer.h"
  30. #include "../lib/CGeneralTextHandler.h"
  31. #include "../lib/CHeroHandler.h"
  32. #include "../lib/VCMI_Lib.h"
  33. #include "../lib/mapping/CMap.h"
  34. #include "../lib/VCMIDirs.h"
  35. #include "../lib/spells/CSpellHandler.h"
  36. #include "../lib/CSoundBase.h"
  37. #include "../lib/StartInfo.h"
  38. #include "../lib/CConfigHandler.h"
  39. #include "../lib/mapObjects/CGMarket.h"
  40. #include "../lib/gameState/CGameState.h"
  41. #include "../lib/CStack.h"
  42. #include "../lib/battle/BattleInfo.h"
  43. #include "../lib/GameConstants.h"
  44. #include "../lib/CPlayerState.h"
  45. // TODO: as Tow suggested these template should all be part of CClient
  46. // This will require rework spectator interface properly though
  47. template<typename T, typename ... Args, typename ... Args2>
  48. bool callOnlyThatInterface(CClient & cl, PlayerColor player, void (T::*ptr)(Args...), Args2 && ...args)
  49. {
  50. if(vstd::contains(cl.playerint, player))
  51. {
  52. ((*cl.playerint[player]).*ptr)(std::forward<Args2>(args)...);
  53. return true;
  54. }
  55. return false;
  56. }
  57. template<typename T, typename ... Args, typename ... Args2>
  58. bool callInterfaceIfPresent(CClient & cl, PlayerColor player, void (T::*ptr)(Args...), Args2 && ...args)
  59. {
  60. bool called = callOnlyThatInterface(cl, player, ptr, std::forward<Args2>(args)...);
  61. return called;
  62. }
  63. template<typename T, typename ... Args, typename ... Args2>
  64. void callOnlyThatBattleInterface(CClient & cl, PlayerColor player, void (T::*ptr)(Args...), Args2 && ...args)
  65. {
  66. if(vstd::contains(cl.battleints,player))
  67. ((*cl.battleints[player]).*ptr)(std::forward<Args2>(args)...);
  68. if(cl.additionalBattleInts.count(player))
  69. {
  70. for(auto bInt : cl.additionalBattleInts[player])
  71. ((*bInt).*ptr)(std::forward<Args2>(args)...);
  72. }
  73. }
  74. template<typename T, typename ... Args, typename ... Args2>
  75. void callBattleInterfaceIfPresent(CClient & cl, PlayerColor player, void (T::*ptr)(Args...), Args2 && ...args)
  76. {
  77. callOnlyThatInterface(cl, player, ptr, std::forward<Args2>(args)...);
  78. }
  79. //calls all normal interfaces and privileged ones, playerints may be updated when iterating over it, so we need a copy
  80. template<typename T, typename ... Args, typename ... Args2>
  81. void callAllInterfaces(CClient & cl, void (T::*ptr)(Args...), Args2 && ...args)
  82. {
  83. for(auto pInt : cl.playerint)
  84. ((*pInt.second).*ptr)(std::forward<Args2>(args)...);
  85. }
  86. //calls all normal interfaces and privileged ones, playerints may be updated when iterating over it, so we need a copy
  87. template<typename T, typename ... Args, typename ... Args2>
  88. void callBattleInterfaceIfPresentForBothSides(CClient & cl, const BattleID & battleID, void (T::*ptr)(Args...), Args2 && ...args)
  89. {
  90. assert(cl.gameState()->getBattle(battleID));
  91. if (!cl.gameState()->getBattle(battleID))
  92. {
  93. logGlobal->error("Attempt to call battle interface without ongoing battle!");
  94. return;
  95. }
  96. callOnlyThatBattleInterface(cl, cl.gameState()->getBattle(battleID)->sides[0].color, ptr, std::forward<Args2>(args)...);
  97. callOnlyThatBattleInterface(cl, cl.gameState()->getBattle(battleID)->sides[1].color, ptr, std::forward<Args2>(args)...);
  98. if(settings["session"]["spectate"].Bool() && !settings["session"]["spectate-skip-battle"].Bool() && LOCPLINT->battleInt)
  99. {
  100. callOnlyThatBattleInterface(cl, PlayerColor::SPECTATOR, ptr, std::forward<Args2>(args)...);
  101. }
  102. }
  103. void ApplyClientNetPackVisitor::visitSetResources(SetResources & pack)
  104. {
  105. //todo: inform on actual resource set transfered
  106. callInterfaceIfPresent(cl, pack.player, &IGameEventsReceiver::receivedResource);
  107. }
  108. void ApplyClientNetPackVisitor::visitSetPrimSkill(SetPrimSkill & pack)
  109. {
  110. const CGHeroInstance * h = cl.getHero(pack.id);
  111. if(!h)
  112. {
  113. logNetwork->error("Cannot find hero with pack.id %d", pack.id.getNum());
  114. return;
  115. }
  116. callInterfaceIfPresent(cl, h->tempOwner, &IGameEventsReceiver::heroPrimarySkillChanged, h, pack.which, pack.val);
  117. }
  118. void ApplyClientNetPackVisitor::visitSetSecSkill(SetSecSkill & pack)
  119. {
  120. const CGHeroInstance *h = cl.getHero(pack.id);
  121. if(!h)
  122. {
  123. logNetwork->error("Cannot find hero with pack.id %d", pack.id.getNum());
  124. return;
  125. }
  126. callInterfaceIfPresent(cl, h->tempOwner, &IGameEventsReceiver::heroSecondarySkillChanged, h, pack.which, pack.val);
  127. }
  128. void ApplyClientNetPackVisitor::visitHeroVisitCastle(HeroVisitCastle & pack)
  129. {
  130. const CGHeroInstance *h = cl.getHero(pack.hid);
  131. if(pack.start())
  132. {
  133. callInterfaceIfPresent(cl, h->tempOwner, &IGameEventsReceiver::heroVisitsTown, h, gs.getTown(pack.tid));
  134. }
  135. }
  136. void ApplyClientNetPackVisitor::visitSetMana(SetMana & pack)
  137. {
  138. const CGHeroInstance *h = cl.getHero(pack.hid);
  139. callInterfaceIfPresent(cl, h->tempOwner, &IGameEventsReceiver::heroManaPointsChanged, h);
  140. for (auto window : GH.windows().findWindows<BattleWindow>())
  141. window->heroManaPointsChanged(h);
  142. }
  143. void ApplyClientNetPackVisitor::visitSetMovePoints(SetMovePoints & pack)
  144. {
  145. const CGHeroInstance *h = cl.getHero(pack.hid);
  146. cl.invalidatePaths();
  147. callInterfaceIfPresent(cl, h->tempOwner, &IGameEventsReceiver::heroMovePointsChanged, h);
  148. }
  149. void ApplyClientNetPackVisitor::visitFoWChange(FoWChange & pack)
  150. {
  151. for(auto &i : cl.playerint)
  152. {
  153. if(cl.getPlayerRelations(i.first, pack.player) == PlayerRelations::SAME_PLAYER && pack.waitForDialogs && LOCPLINT == i.second.get())
  154. {
  155. LOCPLINT->waitWhileDialog();
  156. }
  157. if(cl.getPlayerRelations(i.first, pack.player) != PlayerRelations::ENEMIES)
  158. {
  159. if(pack.mode)
  160. i.second->tileRevealed(pack.tiles);
  161. else
  162. i.second->tileHidden(pack.tiles);
  163. }
  164. }
  165. cl.invalidatePaths();
  166. }
  167. static void dispatchGarrisonChange(CClient & cl, ObjectInstanceID army1, ObjectInstanceID army2)
  168. {
  169. auto obj1 = cl.getObj(army1);
  170. if(!obj1)
  171. {
  172. logNetwork->error("Cannot find army with pack.id %d", army1.getNum());
  173. return;
  174. }
  175. callInterfaceIfPresent(cl, obj1->tempOwner, &IGameEventsReceiver::garrisonsChanged, army1, army2);
  176. if(army2 != ObjectInstanceID() && army2 != army1)
  177. {
  178. auto obj2 = cl.getObj(army2);
  179. if(!obj2)
  180. {
  181. logNetwork->error("Cannot find army with pack.id %d", army2.getNum());
  182. return;
  183. }
  184. if(obj1->tempOwner != obj2->tempOwner)
  185. callInterfaceIfPresent(cl, obj2->tempOwner, &IGameEventsReceiver::garrisonsChanged, army1, army2);
  186. }
  187. }
  188. void ApplyClientNetPackVisitor::visitChangeStackCount(ChangeStackCount & pack)
  189. {
  190. dispatchGarrisonChange(cl, pack.army, ObjectInstanceID());
  191. }
  192. void ApplyClientNetPackVisitor::visitSetStackType(SetStackType & pack)
  193. {
  194. dispatchGarrisonChange(cl, pack.army, ObjectInstanceID());
  195. }
  196. void ApplyClientNetPackVisitor::visitEraseStack(EraseStack & pack)
  197. {
  198. dispatchGarrisonChange(cl, pack.army, ObjectInstanceID());
  199. }
  200. void ApplyClientNetPackVisitor::visitSwapStacks(SwapStacks & pack)
  201. {
  202. dispatchGarrisonChange(cl, pack.srcArmy, pack.dstArmy);
  203. }
  204. void ApplyClientNetPackVisitor::visitInsertNewStack(InsertNewStack & pack)
  205. {
  206. dispatchGarrisonChange(cl, pack.army, ObjectInstanceID());
  207. }
  208. void ApplyClientNetPackVisitor::visitRebalanceStacks(RebalanceStacks & pack)
  209. {
  210. dispatchGarrisonChange(cl, pack.srcArmy, pack.dstArmy);
  211. }
  212. void ApplyClientNetPackVisitor::visitBulkRebalanceStacks(BulkRebalanceStacks & pack)
  213. {
  214. if(!pack.moves.empty())
  215. {
  216. auto destArmy = pack.moves[0].srcArmy == pack.moves[0].dstArmy
  217. ? ObjectInstanceID()
  218. : pack.moves[0].dstArmy;
  219. dispatchGarrisonChange(cl, pack.moves[0].srcArmy, destArmy);
  220. }
  221. }
  222. void ApplyClientNetPackVisitor::visitBulkSmartRebalanceStacks(BulkSmartRebalanceStacks & pack)
  223. {
  224. if(!pack.moves.empty())
  225. {
  226. assert(pack.moves[0].srcArmy == pack.moves[0].dstArmy);
  227. dispatchGarrisonChange(cl, pack.moves[0].srcArmy, ObjectInstanceID());
  228. }
  229. else if(!pack.changes.empty())
  230. {
  231. dispatchGarrisonChange(cl, pack.changes[0].army, ObjectInstanceID());
  232. }
  233. }
  234. void ApplyClientNetPackVisitor::visitPutArtifact(PutArtifact & pack)
  235. {
  236. callInterfaceIfPresent(cl, pack.al.owningPlayer(), &IGameEventsReceiver::artifactPut, pack.al);
  237. if(pack.askAssemble)
  238. callInterfaceIfPresent(cl, pack.al.owningPlayer(), &IGameEventsReceiver::askToAssembleArtifact, pack.al);
  239. }
  240. void ApplyClientNetPackVisitor::visitEraseArtifact(EraseArtifact & pack)
  241. {
  242. callInterfaceIfPresent(cl, pack.al.owningPlayer(), &IGameEventsReceiver::artifactRemoved, pack.al);
  243. }
  244. void ApplyClientNetPackVisitor::visitMoveArtifact(MoveArtifact & pack)
  245. {
  246. auto moveArtifact = [this, &pack](PlayerColor player) -> void
  247. {
  248. callInterfaceIfPresent(cl, player, &IGameEventsReceiver::artifactMoved, pack.src, pack.dst);
  249. if(pack.askAssemble)
  250. callInterfaceIfPresent(cl, player, &IGameEventsReceiver::askToAssembleArtifact, pack.dst);
  251. };
  252. moveArtifact(pack.src.owningPlayer());
  253. if(pack.src.owningPlayer() != pack.dst.owningPlayer())
  254. moveArtifact(pack.dst.owningPlayer());
  255. cl.invalidatePaths(); // hero might have equipped/unequipped Angel Wings
  256. }
  257. void ApplyClientNetPackVisitor::visitBulkMoveArtifacts(BulkMoveArtifacts & pack)
  258. {
  259. auto applyMove = [this, &pack](std::vector<BulkMoveArtifacts::LinkedSlots> & artsPack) -> void
  260. {
  261. for(auto & slotToMove : artsPack)
  262. {
  263. auto srcLoc = ArtifactLocation(pack.srcArtHolder, slotToMove.srcPos);
  264. auto dstLoc = ArtifactLocation(pack.dstArtHolder, slotToMove.dstPos);
  265. MoveArtifact ma(&srcLoc, &dstLoc, false);
  266. visitMoveArtifact(ma);
  267. }
  268. };
  269. auto srcOwner = std::get<ConstTransitivePtr<CGHeroInstance>>(pack.srcArtHolder)->tempOwner;
  270. auto dstOwner = std::get<ConstTransitivePtr<CGHeroInstance>>(pack.dstArtHolder)->tempOwner;
  271. // Begin a session of bulk movement of arts. It is not necessary but useful for the client optimization.
  272. callInterfaceIfPresent(cl, srcOwner, &IGameEventsReceiver::bulkArtMovementStart, pack.artsPack0.size() + pack.artsPack1.size());
  273. if(srcOwner != dstOwner)
  274. callInterfaceIfPresent(cl, dstOwner, &IGameEventsReceiver::bulkArtMovementStart, pack.artsPack0.size() + pack.artsPack1.size());
  275. applyMove(pack.artsPack0);
  276. if(pack.swap)
  277. applyMove(pack.artsPack1);
  278. }
  279. void ApplyClientNetPackVisitor::visitAssembledArtifact(AssembledArtifact & pack)
  280. {
  281. callInterfaceIfPresent(cl, pack.al.owningPlayer(), &IGameEventsReceiver::artifactAssembled, pack.al);
  282. cl.invalidatePaths(); // hero might have equipped/unequipped Angel Wings
  283. }
  284. void ApplyClientNetPackVisitor::visitDisassembledArtifact(DisassembledArtifact & pack)
  285. {
  286. callInterfaceIfPresent(cl, pack.al.owningPlayer(), &IGameEventsReceiver::artifactDisassembled, pack.al);
  287. cl.invalidatePaths(); // hero might have equipped/unequipped Angel Wings
  288. }
  289. void ApplyClientNetPackVisitor::visitHeroVisit(HeroVisit & pack)
  290. {
  291. auto hero = cl.getHero(pack.heroId);
  292. auto obj = cl.getObj(pack.objId, false);
  293. callInterfaceIfPresent(cl, pack.player, &IGameEventsReceiver::heroVisit, hero, obj, pack.starting);
  294. }
  295. void ApplyClientNetPackVisitor::visitNewTurn(NewTurn & pack)
  296. {
  297. cl.invalidatePaths();
  298. }
  299. void ApplyClientNetPackVisitor::visitGiveBonus(GiveBonus & pack)
  300. {
  301. cl.invalidatePaths();
  302. switch(pack.who)
  303. {
  304. case GiveBonus::ETarget::HERO:
  305. {
  306. const CGHeroInstance *h = gs.getHero(ObjectInstanceID(pack.id));
  307. callInterfaceIfPresent(cl, h->tempOwner, &IGameEventsReceiver::heroBonusChanged, h, pack.bonus, true);
  308. }
  309. break;
  310. case GiveBonus::ETarget::PLAYER:
  311. {
  312. callInterfaceIfPresent(cl, PlayerColor(pack.id), &IGameEventsReceiver::playerBonusChanged, pack.bonus, true);
  313. }
  314. break;
  315. }
  316. }
  317. void ApplyFirstClientNetPackVisitor::visitChangeObjPos(ChangeObjPos & pack)
  318. {
  319. CGObjectInstance *obj = gs.getObjInstance(pack.objid);
  320. if(CGI->mh)
  321. CGI->mh->onObjectFadeOut(obj, pack.initiator);
  322. CGI->mh->waitForOngoingAnimations();
  323. }
  324. void ApplyClientNetPackVisitor::visitChangeObjPos(ChangeObjPos & pack)
  325. {
  326. CGObjectInstance *obj = gs.getObjInstance(pack.objid);
  327. if(CGI->mh)
  328. CGI->mh->onObjectFadeIn(obj, pack.initiator);
  329. CGI->mh->waitForOngoingAnimations();
  330. cl.invalidatePaths();
  331. }
  332. void ApplyClientNetPackVisitor::visitPlayerEndsGame(PlayerEndsGame & pack)
  333. {
  334. callAllInterfaces(cl, &IGameEventsReceiver::gameOver, pack.player, pack.victoryLossCheckResult);
  335. // In auto testing pack.mode we always close client if red pack.player won or lose
  336. if(!settings["session"]["testmap"].isNull() && pack.player == PlayerColor(0))
  337. handleQuit(settings["session"]["spectate"].Bool()); // if spectator is active ask to close client or not
  338. }
  339. void ApplyClientNetPackVisitor::visitPlayerReinitInterface(PlayerReinitInterface & pack)
  340. {
  341. auto initInterfaces = [this]()
  342. {
  343. cl.initPlayerInterfaces();
  344. for (PlayerColor player(0); player < PlayerColor::PLAYER_LIMIT; ++player)
  345. {
  346. if (cl.gameState()->isPlayerMakingTurn(player))
  347. {
  348. callAllInterfaces(cl, &IGameEventsReceiver::playerStartsTurn, player);
  349. callOnlyThatInterface(cl, player, &CGameInterface::yourTurn, QueryID::NONE);
  350. }
  351. }
  352. };
  353. for(auto player : pack.players)
  354. {
  355. auto & plSettings = CSH->si->getIthPlayersSettings(player);
  356. if(pack.playerConnectionId == PlayerSettings::PLAYER_AI)
  357. {
  358. plSettings.connectedPlayerIDs.clear();
  359. cl.initPlayerEnvironments();
  360. initInterfaces();
  361. }
  362. else if(pack.playerConnectionId == CSH->c->connectionID)
  363. {
  364. plSettings.connectedPlayerIDs.insert(pack.playerConnectionId);
  365. cl.playerint.clear();
  366. initInterfaces();
  367. }
  368. }
  369. }
  370. void ApplyClientNetPackVisitor::visitRemoveBonus(RemoveBonus & pack)
  371. {
  372. cl.invalidatePaths();
  373. switch(pack.who)
  374. {
  375. case GiveBonus::ETarget::HERO:
  376. {
  377. const CGHeroInstance *h = gs.getHero(ObjectInstanceID(pack.id));
  378. callInterfaceIfPresent(cl, h->tempOwner, &IGameEventsReceiver::heroBonusChanged, h, pack.bonus, false);
  379. }
  380. break;
  381. case GiveBonus::ETarget::PLAYER:
  382. {
  383. //const PlayerState *p = gs.getPlayerState(pack.id);
  384. callInterfaceIfPresent(cl, PlayerColor(pack.id), &IGameEventsReceiver::playerBonusChanged, pack.bonus, false);
  385. }
  386. break;
  387. }
  388. }
  389. void ApplyFirstClientNetPackVisitor::visitRemoveObject(RemoveObject & pack)
  390. {
  391. const CGObjectInstance *o = cl.getObj(pack.objectID);
  392. if(CGI->mh)
  393. CGI->mh->onObjectFadeOut(o, pack.initiator);
  394. //notify interfaces about removal
  395. for(auto i=cl.playerint.begin(); i!=cl.playerint.end(); i++)
  396. {
  397. //below line contains little cheat for AI so it will be aware of deletion of enemy heroes that moved or got re-covered by FoW
  398. //TODO: loose requirements as next AI related crashes appear, for example another pack.player collects object that got re-covered by FoW, unsure if AI code workarounds this
  399. if(gs.isVisible(o, i->first) || (!cl.getPlayerState(i->first)->human && o->ID == Obj::HERO && o->tempOwner != i->first))
  400. i->second->objectRemoved(o, pack.initiator);
  401. }
  402. CGI->mh->waitForOngoingAnimations();
  403. }
  404. void ApplyClientNetPackVisitor::visitRemoveObject(RemoveObject & pack)
  405. {
  406. cl.invalidatePaths();
  407. for(auto i=cl.playerint.begin(); i!=cl.playerint.end(); i++)
  408. i->second->objectRemovedAfter();
  409. }
  410. void ApplyFirstClientNetPackVisitor::visitTryMoveHero(TryMoveHero & pack)
  411. {
  412. CGHeroInstance *h = gs.getHero(pack.id);
  413. if(CGI->mh)
  414. {
  415. switch (pack.result)
  416. {
  417. case TryMoveHero::EMBARK:
  418. CGI->mh->onBeforeHeroEmbark(h, pack.start, pack.end);
  419. break;
  420. case TryMoveHero::TELEPORTATION:
  421. CGI->mh->onBeforeHeroTeleported(h, pack.start, pack.end);
  422. break;
  423. case TryMoveHero::DISEMBARK:
  424. CGI->mh->onBeforeHeroDisembark(h, pack.start, pack.end);
  425. break;
  426. }
  427. CGI->mh->waitForOngoingAnimations();
  428. }
  429. }
  430. void ApplyClientNetPackVisitor::visitTryMoveHero(TryMoveHero & pack)
  431. {
  432. const CGHeroInstance *h = cl.getHero(pack.id);
  433. cl.invalidatePaths();
  434. if(CGI->mh)
  435. {
  436. switch(pack.result)
  437. {
  438. case TryMoveHero::SUCCESS:
  439. CGI->mh->onHeroMoved(h, pack.start, pack.end);
  440. break;
  441. case TryMoveHero::EMBARK:
  442. CGI->mh->onAfterHeroEmbark(h, pack.start, pack.end);
  443. break;
  444. case TryMoveHero::TELEPORTATION:
  445. CGI->mh->onAfterHeroTeleported(h, pack.start, pack.end);
  446. break;
  447. case TryMoveHero::DISEMBARK:
  448. CGI->mh->onAfterHeroDisembark(h, pack.start, pack.end);
  449. break;
  450. }
  451. }
  452. PlayerColor player = h->tempOwner;
  453. for(auto &i : cl.playerint)
  454. if(cl.getPlayerRelations(i.first, player) != PlayerRelations::ENEMIES)
  455. i.second->tileRevealed(pack.fowRevealed);
  456. for(auto i=cl.playerint.begin(); i!=cl.playerint.end(); i++)
  457. {
  458. if(i->first != PlayerColor::SPECTATOR && gs.checkForStandardLoss(i->first)) // Do not notify vanquished pack.player's interface
  459. continue;
  460. if(gs.isVisible(h->convertToVisitablePos(pack.start), i->first)
  461. || gs.isVisible(h->convertToVisitablePos(pack.end), i->first))
  462. {
  463. // pack.src and pack.dst of enemy hero move may be not visible => 'verbose' should be false
  464. const bool verbose = cl.getPlayerRelations(i->first, player) != PlayerRelations::ENEMIES;
  465. i->second->heroMoved(pack, verbose);
  466. }
  467. }
  468. }
  469. void ApplyClientNetPackVisitor::visitNewStructures(NewStructures & pack)
  470. {
  471. CGTownInstance *town = gs.getTown(pack.tid);
  472. for(const auto & id : pack.bid)
  473. {
  474. callInterfaceIfPresent(cl, town->getOwner(), &IGameEventsReceiver::buildChanged, town, id, 1);
  475. }
  476. // invalidate section of map view with our object and force an update
  477. CGI->mh->onObjectInstantRemove(town, town->getOwner());
  478. CGI->mh->onObjectInstantAdd(town, town->getOwner());
  479. }
  480. void ApplyClientNetPackVisitor::visitRazeStructures(RazeStructures & pack)
  481. {
  482. CGTownInstance * town = gs.getTown(pack.tid);
  483. for(const auto & id : pack.bid)
  484. {
  485. callInterfaceIfPresent(cl, town->getOwner(), &IGameEventsReceiver::buildChanged, town, id, 2);
  486. }
  487. // invalidate section of map view with our object and force an update
  488. CGI->mh->onObjectInstantRemove(town, town->getOwner());
  489. CGI->mh->onObjectInstantAdd(town, town->getOwner());
  490. }
  491. void ApplyClientNetPackVisitor::visitSetAvailableCreatures(SetAvailableCreatures & pack)
  492. {
  493. const CGDwelling * dw = static_cast<const CGDwelling*>(cl.getObj(pack.tid));
  494. PlayerColor p;
  495. if(dw->ID == Obj::WAR_MACHINE_FACTORY) //War Machines Factory is not flaggable, it's "owned" by visitor
  496. p = cl.getTile(dw->visitablePos())->visitableObjects.back()->tempOwner;
  497. else
  498. p = dw->tempOwner;
  499. callInterfaceIfPresent(cl, p, &IGameEventsReceiver::availableCreaturesChanged, dw);
  500. }
  501. void ApplyClientNetPackVisitor::visitSetHeroesInTown(SetHeroesInTown & pack)
  502. {
  503. CGTownInstance * t = gs.getTown(pack.tid);
  504. CGHeroInstance * hGarr = gs.getHero(pack.garrison);
  505. CGHeroInstance * hVisit = gs.getHero(pack.visiting);
  506. //inform all players that see this object
  507. for(auto i = cl.playerint.cbegin(); i != cl.playerint.cend(); ++i)
  508. {
  509. if(!i->first.isValidPlayer())
  510. continue;
  511. if(gs.isVisible(t, i->first) ||
  512. (hGarr && gs.isVisible(hGarr, i->first)) ||
  513. (hVisit && gs.isVisible(hVisit, i->first)))
  514. {
  515. cl.playerint[i->first]->heroInGarrisonChange(t);
  516. }
  517. }
  518. }
  519. void ApplyClientNetPackVisitor::visitHeroRecruited(HeroRecruited & pack)
  520. {
  521. CGHeroInstance *h = gs.map->heroesOnMap.back();
  522. if(h->subID != pack.hid)
  523. {
  524. logNetwork->error("Something wrong with hero recruited!");
  525. }
  526. if(callInterfaceIfPresent(cl, h->tempOwner, &IGameEventsReceiver::heroCreated, h))
  527. {
  528. if(const CGTownInstance *t = gs.getTown(pack.tid))
  529. callInterfaceIfPresent(cl, h->getOwner(), &IGameEventsReceiver::heroInGarrisonChange, t);
  530. }
  531. if(CGI->mh)
  532. CGI->mh->onObjectInstantAdd(h, h->getOwner());
  533. }
  534. void ApplyClientNetPackVisitor::visitGiveHero(GiveHero & pack)
  535. {
  536. CGHeroInstance *h = gs.getHero(pack.id);
  537. if(CGI->mh)
  538. CGI->mh->onObjectInstantAdd(h, h->getOwner());
  539. callInterfaceIfPresent(cl, h->tempOwner, &IGameEventsReceiver::heroCreated, h);
  540. }
  541. void ApplyFirstClientNetPackVisitor::visitGiveHero(GiveHero & pack)
  542. {
  543. }
  544. void ApplyClientNetPackVisitor::visitInfoWindow(InfoWindow & pack)
  545. {
  546. std::string str = pack.text.toString();
  547. if(!callInterfaceIfPresent(cl, pack.player, &CGameInterface::showInfoDialog, pack.type, str, pack.components,(soundBase::soundID)pack.soundID))
  548. logNetwork->warn("We received InfoWindow for not our player...");
  549. }
  550. void ApplyFirstClientNetPackVisitor::visitSetObjectProperty(SetObjectProperty & pack)
  551. {
  552. //inform all players that see this object
  553. for(auto it = cl.playerint.cbegin(); it != cl.playerint.cend(); ++it)
  554. {
  555. if(gs.isVisible(gs.getObjInstance(pack.id), it->first))
  556. callInterfaceIfPresent(cl, it->first, &IGameEventsReceiver::beforeObjectPropertyChanged, &pack);
  557. }
  558. // invalidate section of map view with our object and force an update with new flag color
  559. if (pack.what == ObjProperty::OWNER)
  560. {
  561. auto object = gs.getObjInstance(pack.id);
  562. CGI->mh->onObjectInstantRemove(object, object->getOwner());
  563. }
  564. }
  565. void ApplyClientNetPackVisitor::visitSetObjectProperty(SetObjectProperty & pack)
  566. {
  567. //inform all players that see this object
  568. for(auto it = cl.playerint.cbegin(); it != cl.playerint.cend(); ++it)
  569. {
  570. if(gs.isVisible(gs.getObjInstance(pack.id), it->first))
  571. callInterfaceIfPresent(cl, it->first, &IGameEventsReceiver::objectPropertyChanged, &pack);
  572. }
  573. // invalidate section of map view with our object and force an update with new flag color
  574. if (pack.what == ObjProperty::OWNER)
  575. {
  576. auto object = gs.getObjInstance(pack.id);
  577. CGI->mh->onObjectInstantAdd(object, object->getOwner());
  578. }
  579. }
  580. void ApplyClientNetPackVisitor::visitHeroLevelUp(HeroLevelUp & pack)
  581. {
  582. const CGHeroInstance * hero = cl.getHero(pack.heroId);
  583. assert(hero);
  584. callOnlyThatInterface(cl, pack.player, &CGameInterface::heroGotLevel, hero, pack.primskill, pack.skills, pack.queryID);
  585. }
  586. void ApplyClientNetPackVisitor::visitCommanderLevelUp(CommanderLevelUp & pack)
  587. {
  588. const CGHeroInstance * hero = cl.getHero(pack.heroId);
  589. assert(hero);
  590. const CCommanderInstance * commander = hero->commander;
  591. assert(commander);
  592. assert(commander->armyObj); //is it possible for Commander to exist beyond armed instance?
  593. callOnlyThatInterface(cl, pack.player, &CGameInterface::commanderGotLevel, commander, pack.skills, pack.queryID);
  594. }
  595. void ApplyClientNetPackVisitor::visitBlockingDialog(BlockingDialog & pack)
  596. {
  597. std::string str = pack.text.toString();
  598. if(!callOnlyThatInterface(cl, pack.player, &CGameInterface::showBlockingDialog, str, pack.components, pack.queryID, (soundBase::soundID)pack.soundID, pack.selection(), pack.cancel()))
  599. logNetwork->warn("We received YesNoDialog for not our player...");
  600. }
  601. void ApplyClientNetPackVisitor::visitGarrisonDialog(GarrisonDialog & pack)
  602. {
  603. const CGHeroInstance *h = cl.getHero(pack.hid);
  604. const CArmedInstance *obj = static_cast<const CArmedInstance*>(cl.getObj(pack.objid));
  605. callOnlyThatInterface(cl, h->getOwner(), &CGameInterface::showGarrisonDialog, obj, h, pack.removableUnits, pack.queryID);
  606. }
  607. void ApplyClientNetPackVisitor::visitExchangeDialog(ExchangeDialog & pack)
  608. {
  609. callInterfaceIfPresent(cl, pack.player, &IGameEventsReceiver::heroExchangeStarted, pack.hero1, pack.hero2, pack.queryID);
  610. }
  611. void ApplyClientNetPackVisitor::visitTeleportDialog(TeleportDialog & pack)
  612. {
  613. const CGHeroInstance *h = cl.getHero(pack.hero);
  614. callOnlyThatInterface(cl, h->getOwner(), &CGameInterface::showTeleportDialog, h, pack.channel, pack.exits, pack.impassable, pack.queryID);
  615. }
  616. void ApplyClientNetPackVisitor::visitMapObjectSelectDialog(MapObjectSelectDialog & pack)
  617. {
  618. callOnlyThatInterface(cl, pack.player, &CGameInterface::showMapObjectSelectDialog, pack.queryID, pack.icon, pack.title, pack.description, pack.objects);
  619. }
  620. void ApplyFirstClientNetPackVisitor::visitBattleStart(BattleStart & pack)
  621. {
  622. // Cannot use the usual code because curB is not set yet
  623. callOnlyThatBattleInterface(cl, pack.info->sides[0].color, &IBattleEventsReceiver::battleStartBefore, pack.battleID, pack.info->sides[0].armyObject, pack.info->sides[1].armyObject,
  624. pack.info->tile, pack.info->sides[0].hero, pack.info->sides[1].hero);
  625. callOnlyThatBattleInterface(cl, pack.info->sides[1].color, &IBattleEventsReceiver::battleStartBefore, pack.battleID, pack.info->sides[0].armyObject, pack.info->sides[1].armyObject,
  626. pack.info->tile, pack.info->sides[0].hero, pack.info->sides[1].hero);
  627. callOnlyThatBattleInterface(cl, PlayerColor::SPECTATOR, &IBattleEventsReceiver::battleStartBefore, pack.battleID, pack.info->sides[0].armyObject, pack.info->sides[1].armyObject,
  628. pack.info->tile, pack.info->sides[0].hero, pack.info->sides[1].hero);
  629. }
  630. void ApplyClientNetPackVisitor::visitBattleStart(BattleStart & pack)
  631. {
  632. cl.battleStarted(pack.info);
  633. }
  634. void ApplyFirstClientNetPackVisitor::visitBattleNextRound(BattleNextRound & pack)
  635. {
  636. callBattleInterfaceIfPresentForBothSides(cl, pack.battleID, &IBattleEventsReceiver::battleNewRoundFirst, pack.battleID);
  637. }
  638. void ApplyClientNetPackVisitor::visitBattleNextRound(BattleNextRound & pack)
  639. {
  640. callBattleInterfaceIfPresentForBothSides(cl, pack.battleID, &IBattleEventsReceiver::battleNewRound, pack.battleID);
  641. }
  642. void ApplyClientNetPackVisitor::visitBattleSetActiveStack(BattleSetActiveStack & pack)
  643. {
  644. if(!pack.askPlayerInterface)
  645. return;
  646. const CStack *activated = gs.getBattle(pack.battleID)->battleGetStackByID(pack.stack);
  647. PlayerColor playerToCall; //pack.player that will move activated stack
  648. if (activated->hasBonusOfType(BonusType::HYPNOTIZED))
  649. {
  650. playerToCall = (gs.getBattle(pack.battleID)->sides[0].color == activated->unitOwner()
  651. ? gs.getBattle(pack.battleID)->sides[1].color
  652. : gs.getBattle(pack.battleID)->sides[0].color);
  653. }
  654. else
  655. {
  656. playerToCall = activated->unitOwner();
  657. }
  658. cl.startPlayerBattleAction(pack.battleID, playerToCall);
  659. }
  660. void ApplyClientNetPackVisitor::visitBattleLogMessage(BattleLogMessage & pack)
  661. {
  662. callBattleInterfaceIfPresentForBothSides(cl, pack.battleID, &IBattleEventsReceiver::battleLogMessage, pack.battleID, pack.lines);
  663. }
  664. void ApplyClientNetPackVisitor::visitBattleTriggerEffect(BattleTriggerEffect & pack)
  665. {
  666. callBattleInterfaceIfPresentForBothSides(cl, pack.battleID, &IBattleEventsReceiver::battleTriggerEffect, pack.battleID, pack);
  667. }
  668. void ApplyFirstClientNetPackVisitor::visitBattleUpdateGateState(BattleUpdateGateState & pack)
  669. {
  670. callBattleInterfaceIfPresentForBothSides(cl, pack.battleID, &IBattleEventsReceiver::battleGateStateChanged, pack.battleID, pack.state);
  671. }
  672. void ApplyFirstClientNetPackVisitor::visitBattleResult(BattleResult & pack)
  673. {
  674. callBattleInterfaceIfPresentForBothSides(cl, pack.battleID, &IBattleEventsReceiver::battleEnd, pack.battleID, &pack, pack.queryID);
  675. cl.battleFinished(pack.battleID);
  676. }
  677. void ApplyFirstClientNetPackVisitor::visitBattleStackMoved(BattleStackMoved & pack)
  678. {
  679. const CStack * movedStack = gs.getBattle(pack.battleID)->battleGetStackByID(pack.stack);
  680. callBattleInterfaceIfPresentForBothSides(cl, pack.battleID, &IBattleEventsReceiver::battleStackMoved, pack.battleID, movedStack, pack.tilesToMove, pack.distance, pack.teleporting);
  681. }
  682. void ApplyFirstClientNetPackVisitor::visitBattleAttack(BattleAttack & pack)
  683. {
  684. callBattleInterfaceIfPresentForBothSides(cl, pack.battleID, &IBattleEventsReceiver::battleAttack, pack.battleID, &pack);
  685. // battleStacksAttacked should be excuted before BattleAttack.applyGs() to play animation before damaging unit
  686. // so this has to be here instead of ApplyClientNetPackVisitor::visitBattleAttack()
  687. callBattleInterfaceIfPresentForBothSides(cl, pack.battleID, &IBattleEventsReceiver::battleStacksAttacked, pack.battleID, pack.bsa, pack.shot());
  688. }
  689. void ApplyClientNetPackVisitor::visitBattleAttack(BattleAttack & pack)
  690. {
  691. }
  692. void ApplyFirstClientNetPackVisitor::visitStartAction(StartAction & pack)
  693. {
  694. cl.currentBattleAction = std::make_unique<BattleAction>(pack.ba);
  695. callBattleInterfaceIfPresentForBothSides(cl, pack.battleID, &IBattleEventsReceiver::actionStarted, pack.battleID, pack.ba);
  696. }
  697. void ApplyClientNetPackVisitor::visitBattleSpellCast(BattleSpellCast & pack)
  698. {
  699. callBattleInterfaceIfPresentForBothSides(cl, pack.battleID, &IBattleEventsReceiver::battleSpellCast, pack.battleID, &pack);
  700. }
  701. void ApplyClientNetPackVisitor::visitSetStackEffect(SetStackEffect & pack)
  702. {
  703. //informing about effects
  704. callBattleInterfaceIfPresentForBothSides(cl, pack.battleID, &IBattleEventsReceiver::battleStacksEffectsSet, pack.battleID, pack);
  705. }
  706. void ApplyClientNetPackVisitor::visitStacksInjured(StacksInjured & pack)
  707. {
  708. callBattleInterfaceIfPresentForBothSides(cl, pack.battleID, &IBattleEventsReceiver::battleStacksAttacked, pack.battleID, pack.stacks, false);
  709. }
  710. void ApplyClientNetPackVisitor::visitBattleResultsApplied(BattleResultsApplied & pack)
  711. {
  712. callInterfaceIfPresent(cl, pack.player1, &IGameEventsReceiver::battleResultsApplied);
  713. callInterfaceIfPresent(cl, pack.player2, &IGameEventsReceiver::battleResultsApplied);
  714. callInterfaceIfPresent(cl, PlayerColor::SPECTATOR, &IGameEventsReceiver::battleResultsApplied);
  715. }
  716. void ApplyClientNetPackVisitor::visitBattleUnitsChanged(BattleUnitsChanged & pack)
  717. {
  718. callBattleInterfaceIfPresentForBothSides(cl, pack.battleID, &IBattleEventsReceiver::battleUnitsChanged, pack.battleID, pack.changedStacks);
  719. }
  720. void ApplyClientNetPackVisitor::visitBattleObstaclesChanged(BattleObstaclesChanged & pack)
  721. {
  722. //inform interfaces about removed obstacles
  723. callBattleInterfaceIfPresentForBothSides(cl, pack.battleID, &IBattleEventsReceiver::battleObstaclesChanged, pack.battleID, pack.changes);
  724. }
  725. void ApplyClientNetPackVisitor::visitCatapultAttack(CatapultAttack & pack)
  726. {
  727. //inform interfaces about catapult attack
  728. callBattleInterfaceIfPresentForBothSides(cl, pack.battleID, &IBattleEventsReceiver::battleCatapultAttacked, pack.battleID, pack);
  729. }
  730. void ApplyClientNetPackVisitor::visitEndAction(EndAction & pack)
  731. {
  732. callBattleInterfaceIfPresentForBothSides(cl, pack.battleID, &IBattleEventsReceiver::actionFinished, pack.battleID, *cl.currentBattleAction);
  733. cl.currentBattleAction.reset();
  734. }
  735. void ApplyClientNetPackVisitor::visitPackageApplied(PackageApplied & pack)
  736. {
  737. callInterfaceIfPresent(cl, pack.player, &IGameEventsReceiver::requestRealized, &pack);
  738. if(!CClient::waitingRequest.tryRemovingElement(pack.requestID))
  739. logNetwork->warn("Surprising server message! PackageApplied for unknown requestID!");
  740. }
  741. void ApplyClientNetPackVisitor::visitSystemMessage(SystemMessage & pack)
  742. {
  743. std::ostringstream str;
  744. str << "System message: " << pack.text;
  745. logNetwork->error(str.str()); // usually used to receive error messages from server
  746. if(LOCPLINT && !settings["session"]["hideSystemMessages"].Bool())
  747. LOCPLINT->cingconsole->print(str.str());
  748. }
  749. void ApplyClientNetPackVisitor::visitPlayerBlocked(PlayerBlocked & pack)
  750. {
  751. callInterfaceIfPresent(cl, pack.player, &IGameEventsReceiver::playerBlocked, pack.reason, pack.startOrEnd == PlayerBlocked::BLOCKADE_STARTED);
  752. }
  753. void ApplyClientNetPackVisitor::visitPlayerStartsTurn(PlayerStartsTurn & pack)
  754. {
  755. logNetwork->debug("Server gives turn to %s", pack.player.toString());
  756. callAllInterfaces(cl, &IGameEventsReceiver::playerStartsTurn, pack.player);
  757. callOnlyThatInterface(cl, pack.player, &CGameInterface::yourTurn, pack.queryID);
  758. }
  759. void ApplyClientNetPackVisitor::visitPlayerEndsTurn(PlayerEndsTurn & pack)
  760. {
  761. logNetwork->debug("Server ends turn of %s", pack.player.toString());
  762. callAllInterfaces(cl, &IGameEventsReceiver::playerEndsTurn, pack.player);
  763. }
  764. void ApplyClientNetPackVisitor::visitTurnTimeUpdate(TurnTimeUpdate & pack)
  765. {
  766. logNetwork->debug("Server sets turn timer {turn: %d, base: %d, battle: %d, creature: %d} for %s", pack.turnTimer.turnTimer, pack.turnTimer.baseTimer, pack.turnTimer.battleTimer, pack.turnTimer.creatureTimer, pack.player.toString());
  767. }
  768. void ApplyClientNetPackVisitor::visitPlayerMessageClient(PlayerMessageClient & pack)
  769. {
  770. logNetwork->debug("pack.player %s sends a message: %s", pack.player.toString(), pack.text);
  771. std::ostringstream str;
  772. if(pack.player.isSpectator())
  773. str << "Spectator: " << pack.text;
  774. else
  775. str << cl.getPlayerState(pack.player)->nodeName() <<": " << pack.text;
  776. if(LOCPLINT)
  777. LOCPLINT->cingconsole->print(str.str());
  778. }
  779. void ApplyClientNetPackVisitor::visitAdvmapSpellCast(AdvmapSpellCast & pack)
  780. {
  781. cl.invalidatePaths();
  782. auto caster = cl.getHero(pack.casterID);
  783. if(caster)
  784. //consider notifying other interfaces that see hero?
  785. callInterfaceIfPresent(cl, caster->getOwner(), &IGameEventsReceiver::advmapSpellCast, caster, pack.spellID);
  786. else
  787. logNetwork->error("Invalid hero instance");
  788. }
  789. void ApplyClientNetPackVisitor::visitShowWorldViewEx(ShowWorldViewEx & pack)
  790. {
  791. callOnlyThatInterface(cl, pack.player, &CGameInterface::showWorldViewEx, pack.objectPositions, pack.showTerrain);
  792. }
  793. void ApplyClientNetPackVisitor::visitOpenWindow(OpenWindow & pack)
  794. {
  795. switch(pack.window)
  796. {
  797. case EOpenWindowMode::RECRUITMENT_FIRST:
  798. case EOpenWindowMode::RECRUITMENT_ALL:
  799. {
  800. const CGDwelling *dw = dynamic_cast<const CGDwelling*>(cl.getObj(ObjectInstanceID(pack.object)));
  801. const CArmedInstance *dst = dynamic_cast<const CArmedInstance*>(cl.getObj(ObjectInstanceID(pack.visitor)));
  802. callInterfaceIfPresent(cl, dst->tempOwner, &IGameEventsReceiver::showRecruitmentDialog, dw, dst, pack.window == EOpenWindowMode::RECRUITMENT_FIRST ? 0 : -1, pack.queryID);
  803. }
  804. break;
  805. case EOpenWindowMode::SHIPYARD_WINDOW:
  806. {
  807. assert(pack.queryID == QueryID::NONE);
  808. const IShipyard *sy = IShipyard::castFrom(cl.getObj(ObjectInstanceID(pack.object)));
  809. callInterfaceIfPresent(cl, sy->getObject()->getOwner(), &IGameEventsReceiver::showShipyardDialog, sy);
  810. }
  811. break;
  812. case EOpenWindowMode::THIEVES_GUILD:
  813. {
  814. assert(pack.queryID == QueryID::NONE);
  815. //displays Thieves' Guild window (when hero enters Den of Thieves)
  816. const CGObjectInstance *obj = cl.getObj(ObjectInstanceID(pack.object));
  817. const CGHeroInstance *hero = cl.getHero(ObjectInstanceID(pack.visitor));
  818. callInterfaceIfPresent(cl, hero->getOwner(), &IGameEventsReceiver::showThievesGuildWindow, obj);
  819. }
  820. break;
  821. case EOpenWindowMode::UNIVERSITY_WINDOW:
  822. {
  823. //displays University window (when hero enters University on adventure map)
  824. const IMarket *market = IMarket::castFrom(cl.getObj(ObjectInstanceID(pack.object)));
  825. const CGHeroInstance *hero = cl.getHero(ObjectInstanceID(pack.visitor));
  826. callInterfaceIfPresent(cl, hero->tempOwner, &IGameEventsReceiver::showUniversityWindow, market, hero, pack.queryID);
  827. }
  828. break;
  829. case EOpenWindowMode::MARKET_WINDOW:
  830. {
  831. //displays Thieves' Guild window (when hero enters Den of Thieves)
  832. const CGObjectInstance *obj = cl.getObj(ObjectInstanceID(pack.object));
  833. const CGHeroInstance *hero = cl.getHero(ObjectInstanceID(pack.visitor));
  834. const IMarket *market = IMarket::castFrom(obj);
  835. callInterfaceIfPresent(cl, cl.getTile(obj->visitablePos())->visitableObjects.back()->tempOwner, &IGameEventsReceiver::showMarketWindow, market, hero, pack.queryID);
  836. }
  837. break;
  838. case EOpenWindowMode::HILL_FORT_WINDOW:
  839. {
  840. assert(pack.queryID == QueryID::NONE);
  841. //displays Hill fort window
  842. const CGObjectInstance *obj = cl.getObj(ObjectInstanceID(pack.object));
  843. const CGHeroInstance *hero = cl.getHero(ObjectInstanceID(pack.visitor));
  844. callInterfaceIfPresent(cl, cl.getTile(obj->visitablePos())->visitableObjects.back()->tempOwner, &IGameEventsReceiver::showHillFortWindow, obj, hero);
  845. }
  846. break;
  847. case EOpenWindowMode::PUZZLE_MAP:
  848. {
  849. assert(pack.queryID == QueryID::NONE);
  850. const CGHeroInstance *hero = cl.getHero(ObjectInstanceID(pack.visitor));
  851. callInterfaceIfPresent(cl, hero->getOwner(), &IGameEventsReceiver::showPuzzleMap);
  852. }
  853. break;
  854. case EOpenWindowMode::TAVERN_WINDOW:
  855. {
  856. const CGObjectInstance *obj1 = cl.getObj(ObjectInstanceID(pack.object));
  857. const CGHeroInstance * hero = cl.getHero(ObjectInstanceID(pack.visitor));
  858. callInterfaceIfPresent(cl, hero->tempOwner, &IGameEventsReceiver::showTavernWindow, obj1, hero, pack.queryID);
  859. }
  860. break;
  861. }
  862. }
  863. void ApplyClientNetPackVisitor::visitCenterView(CenterView & pack)
  864. {
  865. callInterfaceIfPresent(cl, pack.player, &IGameEventsReceiver::centerView, pack.pos, pack.focusTime);
  866. }
  867. void ApplyClientNetPackVisitor::visitNewObject(NewObject & pack)
  868. {
  869. cl.invalidatePaths();
  870. const CGObjectInstance *obj = cl.getObj(pack.createdObjectID);
  871. if(CGI->mh)
  872. CGI->mh->onObjectFadeIn(obj, pack.initiator);
  873. for(auto i=cl.playerint.begin(); i!=cl.playerint.end(); i++)
  874. {
  875. if(gs.isVisible(obj, i->first))
  876. i->second->newObject(obj);
  877. }
  878. CGI->mh->waitForOngoingAnimations();
  879. }
  880. void ApplyClientNetPackVisitor::visitSetAvailableArtifacts(SetAvailableArtifacts & pack)
  881. {
  882. if(pack.id < 0) //artifact merchants globally
  883. {
  884. callAllInterfaces(cl, &IGameEventsReceiver::availableArtifactsChanged, nullptr);
  885. }
  886. else
  887. {
  888. const CGBlackMarket *bm = dynamic_cast<const CGBlackMarket *>(cl.getObj(ObjectInstanceID(pack.id)));
  889. assert(bm);
  890. callInterfaceIfPresent(cl, cl.getTile(bm->visitablePos())->visitableObjects.back()->tempOwner, &IGameEventsReceiver::availableArtifactsChanged, bm);
  891. }
  892. }
  893. void ApplyClientNetPackVisitor::visitEntitiesChanged(EntitiesChanged & pack)
  894. {
  895. cl.invalidatePaths();
  896. }