NetPacksClient.cpp 36 KB

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