NetPacksClient.cpp 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027
  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);
  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);
  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, -1);
  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.id);
  392. if(CGI->mh)
  393. CGI->mh->onObjectFadeOut(o);
  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);
  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->tempOwner, &IGameEventsReceiver::buildChanged, town, id, 1);
  475. }
  476. // invalidate section of map view with our object and force an update
  477. CGI->mh->onObjectInstantRemove(town);
  478. CGI->mh->onObjectInstantAdd(town);
  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->tempOwner, &IGameEventsReceiver::buildChanged, town, id, 2);
  486. }
  487. // invalidate section of map view with our object and force an update
  488. CGI->mh->onObjectInstantRemove(town);
  489. CGI->mh->onObjectInstantAdd(town);
  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->tempOwner, &IGameEventsReceiver::heroInGarrisonChange, t);
  530. }
  531. if(CGI->mh)
  532. CGI->mh->onObjectInstantAdd(h);
  533. }
  534. void ApplyClientNetPackVisitor::visitGiveHero(GiveHero & pack)
  535. {
  536. CGHeroInstance *h = gs.getHero(pack.id);
  537. if(CGI->mh)
  538. CGI->mh->onObjectInstantAdd(h);
  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. CGI->mh->onObjectInstantRemove(gs.getObjInstance(pack.id));
  561. }
  562. void ApplyClientNetPackVisitor::visitSetObjectProperty(SetObjectProperty & pack)
  563. {
  564. //inform all players that see this object
  565. for(auto it = cl.playerint.cbegin(); it != cl.playerint.cend(); ++it)
  566. {
  567. if(gs.isVisible(gs.getObjInstance(pack.id), it->first))
  568. callInterfaceIfPresent(cl, it->first, &IGameEventsReceiver::objectPropertyChanged, &pack);
  569. }
  570. // invalidate section of map view with our object and force an update with new flag color
  571. if (pack.what == ObjProperty::OWNER)
  572. CGI->mh->onObjectInstantAdd(gs.getObjInstance(pack.id));
  573. }
  574. void ApplyClientNetPackVisitor::visitHeroLevelUp(HeroLevelUp & pack)
  575. {
  576. const CGHeroInstance * hero = cl.getHero(pack.heroId);
  577. assert(hero);
  578. callOnlyThatInterface(cl, pack.player, &CGameInterface::heroGotLevel, hero, pack.primskill, pack.skills, pack.queryID);
  579. }
  580. void ApplyClientNetPackVisitor::visitCommanderLevelUp(CommanderLevelUp & pack)
  581. {
  582. const CGHeroInstance * hero = cl.getHero(pack.heroId);
  583. assert(hero);
  584. const CCommanderInstance * commander = hero->commander;
  585. assert(commander);
  586. assert(commander->armyObj); //is it possible for Commander to exist beyond armed instance?
  587. callOnlyThatInterface(cl, pack.player, &CGameInterface::commanderGotLevel, commander, pack.skills, pack.queryID);
  588. }
  589. void ApplyClientNetPackVisitor::visitBlockingDialog(BlockingDialog & pack)
  590. {
  591. std::string str = pack.text.toString();
  592. if(!callOnlyThatInterface(cl, pack.player, &CGameInterface::showBlockingDialog, str, pack.components, pack.queryID, (soundBase::soundID)pack.soundID, pack.selection(), pack.cancel()))
  593. logNetwork->warn("We received YesNoDialog for not our player...");
  594. }
  595. void ApplyClientNetPackVisitor::visitGarrisonDialog(GarrisonDialog & pack)
  596. {
  597. const CGHeroInstance *h = cl.getHero(pack.hid);
  598. const CArmedInstance *obj = static_cast<const CArmedInstance*>(cl.getObj(pack.objid));
  599. callOnlyThatInterface(cl, h->getOwner(), &CGameInterface::showGarrisonDialog, obj, h, pack.removableUnits, pack.queryID);
  600. }
  601. void ApplyClientNetPackVisitor::visitExchangeDialog(ExchangeDialog & pack)
  602. {
  603. callInterfaceIfPresent(cl, pack.player, &IGameEventsReceiver::heroExchangeStarted, pack.hero1, pack.hero2, pack.queryID);
  604. }
  605. void ApplyClientNetPackVisitor::visitTeleportDialog(TeleportDialog & pack)
  606. {
  607. const CGHeroInstance *h = cl.getHero(pack.hero);
  608. callOnlyThatInterface(cl, h->getOwner(), &CGameInterface::showTeleportDialog, h, pack.channel, pack.exits, pack.impassable, pack.queryID);
  609. }
  610. void ApplyClientNetPackVisitor::visitMapObjectSelectDialog(MapObjectSelectDialog & pack)
  611. {
  612. callOnlyThatInterface(cl, pack.player, &CGameInterface::showMapObjectSelectDialog, pack.queryID, pack.icon, pack.title, pack.description, pack.objects);
  613. }
  614. void ApplyFirstClientNetPackVisitor::visitBattleStart(BattleStart & pack)
  615. {
  616. // Cannot use the usual code because curB is not set yet
  617. callOnlyThatBattleInterface(cl, pack.info->sides[0].color, &IBattleEventsReceiver::battleStartBefore, pack.battleID, pack.info->sides[0].armyObject, pack.info->sides[1].armyObject,
  618. pack.info->tile, pack.info->sides[0].hero, pack.info->sides[1].hero);
  619. callOnlyThatBattleInterface(cl, pack.info->sides[1].color, &IBattleEventsReceiver::battleStartBefore, pack.battleID, pack.info->sides[0].armyObject, pack.info->sides[1].armyObject,
  620. pack.info->tile, pack.info->sides[0].hero, pack.info->sides[1].hero);
  621. callOnlyThatBattleInterface(cl, PlayerColor::SPECTATOR, &IBattleEventsReceiver::battleStartBefore, pack.battleID, pack.info->sides[0].armyObject, pack.info->sides[1].armyObject,
  622. pack.info->tile, pack.info->sides[0].hero, pack.info->sides[1].hero);
  623. }
  624. void ApplyClientNetPackVisitor::visitBattleStart(BattleStart & pack)
  625. {
  626. cl.battleStarted(pack.info);
  627. }
  628. void ApplyFirstClientNetPackVisitor::visitBattleNextRound(BattleNextRound & pack)
  629. {
  630. callBattleInterfaceIfPresentForBothSides(cl, pack.battleID, &IBattleEventsReceiver::battleNewRoundFirst, pack.battleID);
  631. }
  632. void ApplyClientNetPackVisitor::visitBattleNextRound(BattleNextRound & pack)
  633. {
  634. callBattleInterfaceIfPresentForBothSides(cl, pack.battleID, &IBattleEventsReceiver::battleNewRound, pack.battleID);
  635. }
  636. void ApplyClientNetPackVisitor::visitBattleSetActiveStack(BattleSetActiveStack & pack)
  637. {
  638. if(!pack.askPlayerInterface)
  639. return;
  640. const CStack *activated = gs.getBattle(pack.battleID)->battleGetStackByID(pack.stack);
  641. PlayerColor playerToCall; //pack.player that will move activated stack
  642. if (activated->hasBonusOfType(BonusType::HYPNOTIZED))
  643. {
  644. playerToCall = (gs.getBattle(pack.battleID)->sides[0].color == activated->unitOwner()
  645. ? gs.getBattle(pack.battleID)->sides[1].color
  646. : gs.getBattle(pack.battleID)->sides[0].color);
  647. }
  648. else
  649. {
  650. playerToCall = activated->unitOwner();
  651. }
  652. cl.startPlayerBattleAction(pack.battleID, playerToCall);
  653. }
  654. void ApplyClientNetPackVisitor::visitBattleLogMessage(BattleLogMessage & pack)
  655. {
  656. callBattleInterfaceIfPresentForBothSides(cl, pack.battleID, &IBattleEventsReceiver::battleLogMessage, pack.battleID, pack.lines);
  657. }
  658. void ApplyClientNetPackVisitor::visitBattleTriggerEffect(BattleTriggerEffect & pack)
  659. {
  660. callBattleInterfaceIfPresentForBothSides(cl, pack.battleID, &IBattleEventsReceiver::battleTriggerEffect, pack.battleID, pack);
  661. }
  662. void ApplyFirstClientNetPackVisitor::visitBattleUpdateGateState(BattleUpdateGateState & pack)
  663. {
  664. callBattleInterfaceIfPresentForBothSides(cl, pack.battleID, &IBattleEventsReceiver::battleGateStateChanged, pack.battleID, pack.state);
  665. }
  666. void ApplyFirstClientNetPackVisitor::visitBattleResult(BattleResult & pack)
  667. {
  668. callBattleInterfaceIfPresentForBothSides(cl, pack.battleID, &IBattleEventsReceiver::battleEnd, pack.battleID, &pack, pack.queryID);
  669. cl.battleFinished(pack.battleID);
  670. }
  671. void ApplyFirstClientNetPackVisitor::visitBattleStackMoved(BattleStackMoved & pack)
  672. {
  673. const CStack * movedStack = gs.getBattle(pack.battleID)->battleGetStackByID(pack.stack);
  674. callBattleInterfaceIfPresentForBothSides(cl, pack.battleID, &IBattleEventsReceiver::battleStackMoved, pack.battleID, movedStack, pack.tilesToMove, pack.distance, pack.teleporting);
  675. }
  676. void ApplyFirstClientNetPackVisitor::visitBattleAttack(BattleAttack & pack)
  677. {
  678. callBattleInterfaceIfPresentForBothSides(cl, pack.battleID, &IBattleEventsReceiver::battleAttack, pack.battleID, &pack);
  679. // battleStacksAttacked should be excuted before BattleAttack.applyGs() to play animation before damaging unit
  680. // so this has to be here instead of ApplyClientNetPackVisitor::visitBattleAttack()
  681. callBattleInterfaceIfPresentForBothSides(cl, pack.battleID, &IBattleEventsReceiver::battleStacksAttacked, pack.battleID, pack.bsa, pack.shot());
  682. }
  683. void ApplyClientNetPackVisitor::visitBattleAttack(BattleAttack & pack)
  684. {
  685. }
  686. void ApplyFirstClientNetPackVisitor::visitStartAction(StartAction & pack)
  687. {
  688. cl.currentBattleAction = std::make_unique<BattleAction>(pack.ba);
  689. callBattleInterfaceIfPresentForBothSides(cl, pack.battleID, &IBattleEventsReceiver::actionStarted, pack.battleID, pack.ba);
  690. }
  691. void ApplyClientNetPackVisitor::visitBattleSpellCast(BattleSpellCast & pack)
  692. {
  693. callBattleInterfaceIfPresentForBothSides(cl, pack.battleID, &IBattleEventsReceiver::battleSpellCast, pack.battleID, &pack);
  694. }
  695. void ApplyClientNetPackVisitor::visitSetStackEffect(SetStackEffect & pack)
  696. {
  697. //informing about effects
  698. callBattleInterfaceIfPresentForBothSides(cl, pack.battleID, &IBattleEventsReceiver::battleStacksEffectsSet, pack.battleID, pack);
  699. }
  700. void ApplyClientNetPackVisitor::visitStacksInjured(StacksInjured & pack)
  701. {
  702. callBattleInterfaceIfPresentForBothSides(cl, pack.battleID, &IBattleEventsReceiver::battleStacksAttacked, pack.battleID, pack.stacks, false);
  703. }
  704. void ApplyClientNetPackVisitor::visitBattleResultsApplied(BattleResultsApplied & pack)
  705. {
  706. callInterfaceIfPresent(cl, pack.player1, &IGameEventsReceiver::battleResultsApplied);
  707. callInterfaceIfPresent(cl, pack.player2, &IGameEventsReceiver::battleResultsApplied);
  708. callInterfaceIfPresent(cl, PlayerColor::SPECTATOR, &IGameEventsReceiver::battleResultsApplied);
  709. }
  710. void ApplyClientNetPackVisitor::visitBattleUnitsChanged(BattleUnitsChanged & pack)
  711. {
  712. callBattleInterfaceIfPresentForBothSides(cl, pack.battleID, &IBattleEventsReceiver::battleUnitsChanged, pack.battleID, pack.changedStacks);
  713. }
  714. void ApplyClientNetPackVisitor::visitBattleObstaclesChanged(BattleObstaclesChanged & pack)
  715. {
  716. //inform interfaces about removed obstacles
  717. callBattleInterfaceIfPresentForBothSides(cl, pack.battleID, &IBattleEventsReceiver::battleObstaclesChanged, pack.battleID, pack.changes);
  718. }
  719. void ApplyClientNetPackVisitor::visitCatapultAttack(CatapultAttack & pack)
  720. {
  721. //inform interfaces about catapult attack
  722. callBattleInterfaceIfPresentForBothSides(cl, pack.battleID, &IBattleEventsReceiver::battleCatapultAttacked, pack.battleID, pack);
  723. }
  724. void ApplyClientNetPackVisitor::visitEndAction(EndAction & pack)
  725. {
  726. callBattleInterfaceIfPresentForBothSides(cl, pack.battleID, &IBattleEventsReceiver::actionFinished, pack.battleID, *cl.currentBattleAction);
  727. cl.currentBattleAction.reset();
  728. }
  729. void ApplyClientNetPackVisitor::visitPackageApplied(PackageApplied & pack)
  730. {
  731. callInterfaceIfPresent(cl, pack.player, &IGameEventsReceiver::requestRealized, &pack);
  732. if(!CClient::waitingRequest.tryRemovingElement(pack.requestID))
  733. logNetwork->warn("Surprising server message! PackageApplied for unknown requestID!");
  734. }
  735. void ApplyClientNetPackVisitor::visitSystemMessage(SystemMessage & pack)
  736. {
  737. std::ostringstream str;
  738. str << "System message: " << pack.text;
  739. logNetwork->error(str.str()); // usually used to receive error messages from server
  740. if(LOCPLINT && !settings["session"]["hideSystemMessages"].Bool())
  741. LOCPLINT->cingconsole->print(str.str());
  742. }
  743. void ApplyClientNetPackVisitor::visitPlayerBlocked(PlayerBlocked & pack)
  744. {
  745. callInterfaceIfPresent(cl, pack.player, &IGameEventsReceiver::playerBlocked, pack.reason, pack.startOrEnd == PlayerBlocked::BLOCKADE_STARTED);
  746. }
  747. void ApplyClientNetPackVisitor::visitPlayerStartsTurn(PlayerStartsTurn & pack)
  748. {
  749. logNetwork->debug("Server gives turn to %s", pack.player.toString());
  750. callAllInterfaces(cl, &IGameEventsReceiver::playerStartsTurn, pack.player);
  751. callOnlyThatInterface(cl, pack.player, &CGameInterface::yourTurn, pack.queryID);
  752. }
  753. void ApplyClientNetPackVisitor::visitPlayerEndsTurn(PlayerEndsTurn & pack)
  754. {
  755. logNetwork->debug("Server ends turn of %s", pack.player.toString());
  756. callAllInterfaces(cl, &IGameEventsReceiver::playerEndsTurn, pack.player);
  757. }
  758. void ApplyClientNetPackVisitor::visitTurnTimeUpdate(TurnTimeUpdate & pack)
  759. {
  760. 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());
  761. }
  762. void ApplyClientNetPackVisitor::visitPlayerMessageClient(PlayerMessageClient & pack)
  763. {
  764. logNetwork->debug("pack.player %s sends a message: %s", pack.player.toString(), pack.text);
  765. std::ostringstream str;
  766. if(pack.player.isSpectator())
  767. str << "Spectator: " << pack.text;
  768. else
  769. str << cl.getPlayerState(pack.player)->nodeName() <<": " << pack.text;
  770. if(LOCPLINT)
  771. LOCPLINT->cingconsole->print(str.str());
  772. }
  773. void ApplyClientNetPackVisitor::visitAdvmapSpellCast(AdvmapSpellCast & pack)
  774. {
  775. cl.invalidatePaths();
  776. auto caster = cl.getHero(pack.casterID);
  777. if(caster)
  778. //consider notifying other interfaces that see hero?
  779. callInterfaceIfPresent(cl, caster->getOwner(), &IGameEventsReceiver::advmapSpellCast, caster, pack.spellID);
  780. else
  781. logNetwork->error("Invalid hero instance");
  782. }
  783. void ApplyClientNetPackVisitor::visitShowWorldViewEx(ShowWorldViewEx & pack)
  784. {
  785. callOnlyThatInterface(cl, pack.player, &CGameInterface::showWorldViewEx, pack.objectPositions, pack.showTerrain);
  786. }
  787. void ApplyClientNetPackVisitor::visitOpenWindow(OpenWindow & pack)
  788. {
  789. switch(pack.window)
  790. {
  791. case EOpenWindowMode::RECRUITMENT_FIRST:
  792. case EOpenWindowMode::RECRUITMENT_ALL:
  793. {
  794. const CGDwelling *dw = dynamic_cast<const CGDwelling*>(cl.getObj(ObjectInstanceID(pack.id1)));
  795. const CArmedInstance *dst = dynamic_cast<const CArmedInstance*>(cl.getObj(ObjectInstanceID(pack.id2)));
  796. callInterfaceIfPresent(cl, dst->tempOwner, &IGameEventsReceiver::showRecruitmentDialog, dw, dst, pack.window == EOpenWindowMode::RECRUITMENT_FIRST ? 0 : -1);
  797. }
  798. break;
  799. case EOpenWindowMode::SHIPYARD_WINDOW:
  800. {
  801. const IShipyard *sy = IShipyard::castFrom(cl.getObj(ObjectInstanceID(pack.id1)));
  802. callInterfaceIfPresent(cl, sy->getObject()->getOwner(), &IGameEventsReceiver::showShipyardDialog, sy);
  803. }
  804. break;
  805. case EOpenWindowMode::THIEVES_GUILD:
  806. {
  807. //displays Thieves' Guild window (when hero enters Den of Thieves)
  808. const CGObjectInstance *obj = cl.getObj(ObjectInstanceID(pack.id2));
  809. callInterfaceIfPresent(cl, PlayerColor(pack.id1), &IGameEventsReceiver::showThievesGuildWindow, obj);
  810. }
  811. break;
  812. case EOpenWindowMode::UNIVERSITY_WINDOW:
  813. {
  814. //displays University window (when hero enters University on adventure map)
  815. const IMarket *market = IMarket::castFrom(cl.getObj(ObjectInstanceID(pack.id1)));
  816. const CGHeroInstance *hero = cl.getHero(ObjectInstanceID(pack.id2));
  817. callInterfaceIfPresent(cl, hero->tempOwner, &IGameEventsReceiver::showUniversityWindow, market, hero);
  818. }
  819. break;
  820. case EOpenWindowMode::MARKET_WINDOW:
  821. {
  822. //displays Thieves' Guild window (when hero enters Den of Thieves)
  823. const CGObjectInstance *obj = cl.getObj(ObjectInstanceID(pack.id1));
  824. const CGHeroInstance *hero = cl.getHero(ObjectInstanceID(pack.id2));
  825. const IMarket *market = IMarket::castFrom(obj);
  826. callInterfaceIfPresent(cl, cl.getTile(obj->visitablePos())->visitableObjects.back()->tempOwner, &IGameEventsReceiver::showMarketWindow, market, hero);
  827. }
  828. break;
  829. case EOpenWindowMode::HILL_FORT_WINDOW:
  830. {
  831. //displays Hill fort window
  832. const CGObjectInstance *obj = cl.getObj(ObjectInstanceID(pack.id1));
  833. const CGHeroInstance *hero = cl.getHero(ObjectInstanceID(pack.id2));
  834. callInterfaceIfPresent(cl, cl.getTile(obj->visitablePos())->visitableObjects.back()->tempOwner, &IGameEventsReceiver::showHillFortWindow, obj, hero);
  835. }
  836. break;
  837. case EOpenWindowMode::PUZZLE_MAP:
  838. {
  839. callInterfaceIfPresent(cl, PlayerColor(pack.id1), &IGameEventsReceiver::showPuzzleMap);
  840. }
  841. break;
  842. case EOpenWindowMode::TAVERN_WINDOW:
  843. const CGObjectInstance *obj1 = cl.getObj(ObjectInstanceID(pack.id1)),
  844. *obj2 = cl.getObj(ObjectInstanceID(pack.id2));
  845. callInterfaceIfPresent(cl, obj1->tempOwner, &IGameEventsReceiver::showTavernWindow, obj2);
  846. break;
  847. }
  848. }
  849. void ApplyClientNetPackVisitor::visitCenterView(CenterView & pack)
  850. {
  851. callInterfaceIfPresent(cl, pack.player, &IGameEventsReceiver::centerView, pack.pos, pack.focusTime);
  852. }
  853. void ApplyClientNetPackVisitor::visitNewObject(NewObject & pack)
  854. {
  855. cl.invalidatePaths();
  856. const CGObjectInstance *obj = cl.getObj(pack.createdObjectID);
  857. if(CGI->mh)
  858. CGI->mh->onObjectFadeIn(obj);
  859. for(auto i=cl.playerint.begin(); i!=cl.playerint.end(); i++)
  860. {
  861. if(gs.isVisible(obj, i->first))
  862. i->second->newObject(obj);
  863. }
  864. CGI->mh->waitForOngoingAnimations();
  865. }
  866. void ApplyClientNetPackVisitor::visitSetAvailableArtifacts(SetAvailableArtifacts & pack)
  867. {
  868. if(pack.id < 0) //artifact merchants globally
  869. {
  870. callAllInterfaces(cl, &IGameEventsReceiver::availableArtifactsChanged, nullptr);
  871. }
  872. else
  873. {
  874. const CGBlackMarket *bm = dynamic_cast<const CGBlackMarket *>(cl.getObj(ObjectInstanceID(pack.id)));
  875. assert(bm);
  876. callInterfaceIfPresent(cl, cl.getTile(bm->visitablePos())->visitableObjects.back()->tempOwner, &IGameEventsReceiver::availableArtifactsChanged, bm);
  877. }
  878. }
  879. void ApplyClientNetPackVisitor::visitEntitiesChanged(EntitiesChanged & pack)
  880. {
  881. cl.invalidatePaths();
  882. }