NetPacksClient.cpp 37 KB

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