NetPacksClient.cpp 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044
  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 == ETileVisibility::REVEALED)
  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 && CGI->mh)
  321. {
  322. CGI->mh->onObjectFadeOut(obj, pack.initiator);
  323. CGI->mh->waitForOngoingAnimations();
  324. }
  325. }
  326. void ApplyClientNetPackVisitor::visitChangeObjPos(ChangeObjPos & pack)
  327. {
  328. CGObjectInstance *obj = gs.getObjInstance(pack.objid);
  329. if(CGI && CGI->mh)
  330. {
  331. CGI->mh->onObjectFadeIn(obj, pack.initiator);
  332. CGI->mh->waitForOngoingAnimations();
  333. }
  334. cl.invalidatePaths();
  335. }
  336. void ApplyClientNetPackVisitor::visitPlayerEndsGame(PlayerEndsGame & pack)
  337. {
  338. callAllInterfaces(cl, &IGameEventsReceiver::gameOver, pack.player, pack.victoryLossCheckResult);
  339. // In auto testing pack.mode we always close client if red pack.player won or lose
  340. if(!settings["session"]["testmap"].isNull() && pack.player == PlayerColor(0))
  341. handleQuit(settings["session"]["spectate"].Bool()); // if spectator is active ask to close client or not
  342. }
  343. void ApplyClientNetPackVisitor::visitPlayerReinitInterface(PlayerReinitInterface & pack)
  344. {
  345. auto initInterfaces = [this]()
  346. {
  347. cl.initPlayerInterfaces();
  348. for (PlayerColor player(0); player < PlayerColor::PLAYER_LIMIT; ++player)
  349. {
  350. if (cl.gameState()->isPlayerMakingTurn(player))
  351. {
  352. callAllInterfaces(cl, &IGameEventsReceiver::playerStartsTurn, player);
  353. callOnlyThatInterface(cl, player, &CGameInterface::yourTurn, QueryID::NONE);
  354. }
  355. }
  356. };
  357. for(auto player : pack.players)
  358. {
  359. auto & plSettings = CSH->si->getIthPlayersSettings(player);
  360. if(pack.playerConnectionId == PlayerSettings::PLAYER_AI)
  361. {
  362. plSettings.connectedPlayerIDs.clear();
  363. cl.initPlayerEnvironments();
  364. initInterfaces();
  365. }
  366. else if(pack.playerConnectionId == CSH->c->connectionID)
  367. {
  368. plSettings.connectedPlayerIDs.insert(pack.playerConnectionId);
  369. cl.playerint.clear();
  370. initInterfaces();
  371. }
  372. }
  373. }
  374. void ApplyClientNetPackVisitor::visitRemoveBonus(RemoveBonus & pack)
  375. {
  376. cl.invalidatePaths();
  377. switch(pack.who)
  378. {
  379. case GiveBonus::ETarget::HERO:
  380. {
  381. const CGHeroInstance *h = gs.getHero(ObjectInstanceID(pack.whoID));
  382. callInterfaceIfPresent(cl, h->tempOwner, &IGameEventsReceiver::heroBonusChanged, h, pack.bonus, false);
  383. }
  384. break;
  385. case GiveBonus::ETarget::PLAYER:
  386. {
  387. //const PlayerState *p = gs.getPlayerState(pack.id);
  388. callInterfaceIfPresent(cl, PlayerColor(pack.whoID), &IGameEventsReceiver::playerBonusChanged, pack.bonus, false);
  389. }
  390. break;
  391. }
  392. }
  393. void ApplyFirstClientNetPackVisitor::visitRemoveObject(RemoveObject & pack)
  394. {
  395. const CGObjectInstance *o = cl.getObj(pack.objectID);
  396. if(CGI->mh)
  397. CGI->mh->onObjectFadeOut(o, pack.initiator);
  398. //notify interfaces about removal
  399. for(auto i=cl.playerint.begin(); i!=cl.playerint.end(); i++)
  400. {
  401. //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
  402. //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
  403. if(gs.isVisible(o, i->first) || (!cl.getPlayerState(i->first)->human && o->ID == Obj::HERO && o->tempOwner != i->first))
  404. i->second->objectRemoved(o, pack.initiator);
  405. }
  406. CGI->mh->waitForOngoingAnimations();
  407. }
  408. void ApplyClientNetPackVisitor::visitRemoveObject(RemoveObject & pack)
  409. {
  410. cl.invalidatePaths();
  411. for(auto i=cl.playerint.begin(); i!=cl.playerint.end(); i++)
  412. i->second->objectRemovedAfter();
  413. }
  414. void ApplyFirstClientNetPackVisitor::visitTryMoveHero(TryMoveHero & pack)
  415. {
  416. CGHeroInstance *h = gs.getHero(pack.id);
  417. if(CGI->mh)
  418. {
  419. switch (pack.result)
  420. {
  421. case TryMoveHero::EMBARK:
  422. CGI->mh->onBeforeHeroEmbark(h, pack.start, pack.end);
  423. break;
  424. case TryMoveHero::TELEPORTATION:
  425. CGI->mh->onBeforeHeroTeleported(h, pack.start, pack.end);
  426. break;
  427. case TryMoveHero::DISEMBARK:
  428. CGI->mh->onBeforeHeroDisembark(h, pack.start, pack.end);
  429. break;
  430. }
  431. CGI->mh->waitForOngoingAnimations();
  432. }
  433. }
  434. void ApplyClientNetPackVisitor::visitTryMoveHero(TryMoveHero & pack)
  435. {
  436. const CGHeroInstance *h = cl.getHero(pack.id);
  437. cl.invalidatePaths();
  438. if(CGI->mh)
  439. {
  440. switch(pack.result)
  441. {
  442. case TryMoveHero::SUCCESS:
  443. CGI->mh->onHeroMoved(h, pack.start, pack.end);
  444. break;
  445. case TryMoveHero::EMBARK:
  446. CGI->mh->onAfterHeroEmbark(h, pack.start, pack.end);
  447. break;
  448. case TryMoveHero::TELEPORTATION:
  449. CGI->mh->onAfterHeroTeleported(h, pack.start, pack.end);
  450. break;
  451. case TryMoveHero::DISEMBARK:
  452. CGI->mh->onAfterHeroDisembark(h, pack.start, pack.end);
  453. break;
  454. }
  455. }
  456. PlayerColor player = h->tempOwner;
  457. for(auto &i : cl.playerint)
  458. if(cl.getPlayerRelations(i.first, player) != PlayerRelations::ENEMIES)
  459. i.second->tileRevealed(pack.fowRevealed);
  460. for(auto i=cl.playerint.begin(); i!=cl.playerint.end(); i++)
  461. {
  462. if(i->first != PlayerColor::SPECTATOR && gs.checkForStandardLoss(i->first)) // Do not notify vanquished pack.player's interface
  463. continue;
  464. if(gs.isVisible(h->convertToVisitablePos(pack.start), i->first)
  465. || gs.isVisible(h->convertToVisitablePos(pack.end), i->first))
  466. {
  467. // pack.src and pack.dst of enemy hero move may be not visible => 'verbose' should be false
  468. const bool verbose = cl.getPlayerRelations(i->first, player) != PlayerRelations::ENEMIES;
  469. i->second->heroMoved(pack, verbose);
  470. }
  471. }
  472. }
  473. void ApplyClientNetPackVisitor::visitNewStructures(NewStructures & pack)
  474. {
  475. CGTownInstance *town = gs.getTown(pack.tid);
  476. for(const auto & id : pack.bid)
  477. {
  478. callInterfaceIfPresent(cl, town->getOwner(), &IGameEventsReceiver::buildChanged, town, id, 1);
  479. }
  480. // invalidate section of map view with our object and force an update
  481. CGI->mh->onObjectInstantRemove(town, town->getOwner());
  482. CGI->mh->onObjectInstantAdd(town, town->getOwner());
  483. }
  484. void ApplyClientNetPackVisitor::visitRazeStructures(RazeStructures & pack)
  485. {
  486. CGTownInstance * town = gs.getTown(pack.tid);
  487. for(const auto & id : pack.bid)
  488. {
  489. callInterfaceIfPresent(cl, town->getOwner(), &IGameEventsReceiver::buildChanged, town, id, 2);
  490. }
  491. // invalidate section of map view with our object and force an update
  492. CGI->mh->onObjectInstantRemove(town, town->getOwner());
  493. CGI->mh->onObjectInstantAdd(town, town->getOwner());
  494. }
  495. void ApplyClientNetPackVisitor::visitSetAvailableCreatures(SetAvailableCreatures & pack)
  496. {
  497. const CGDwelling * dw = static_cast<const CGDwelling*>(cl.getObj(pack.tid));
  498. PlayerColor p;
  499. if(dw->ID == Obj::WAR_MACHINE_FACTORY) //War Machines Factory is not flaggable, it's "owned" by visitor
  500. p = cl.getTile(dw->visitablePos())->visitableObjects.back()->tempOwner;
  501. else
  502. p = dw->tempOwner;
  503. callInterfaceIfPresent(cl, p, &IGameEventsReceiver::availableCreaturesChanged, dw);
  504. }
  505. void ApplyClientNetPackVisitor::visitSetHeroesInTown(SetHeroesInTown & pack)
  506. {
  507. CGTownInstance * t = gs.getTown(pack.tid);
  508. CGHeroInstance * hGarr = gs.getHero(pack.garrison);
  509. CGHeroInstance * hVisit = gs.getHero(pack.visiting);
  510. //inform all players that see this object
  511. for(auto i = cl.playerint.cbegin(); i != cl.playerint.cend(); ++i)
  512. {
  513. if(!i->first.isValidPlayer())
  514. continue;
  515. if(gs.isVisible(t, i->first) ||
  516. (hGarr && gs.isVisible(hGarr, i->first)) ||
  517. (hVisit && gs.isVisible(hVisit, i->first)))
  518. {
  519. cl.playerint[i->first]->heroInGarrisonChange(t);
  520. }
  521. }
  522. }
  523. void ApplyClientNetPackVisitor::visitHeroRecruited(HeroRecruited & pack)
  524. {
  525. CGHeroInstance *h = gs.map->heroesOnMap.back();
  526. if(h->subID != pack.hid)
  527. {
  528. logNetwork->error("Something wrong with hero recruited!");
  529. }
  530. if(callInterfaceIfPresent(cl, h->tempOwner, &IGameEventsReceiver::heroCreated, h))
  531. {
  532. if(const CGTownInstance *t = gs.getTown(pack.tid))
  533. callInterfaceIfPresent(cl, h->getOwner(), &IGameEventsReceiver::heroInGarrisonChange, t);
  534. }
  535. if(CGI->mh)
  536. CGI->mh->onObjectInstantAdd(h, h->getOwner());
  537. }
  538. void ApplyClientNetPackVisitor::visitGiveHero(GiveHero & pack)
  539. {
  540. CGHeroInstance *h = gs.getHero(pack.id);
  541. if(CGI->mh)
  542. CGI->mh->onObjectInstantAdd(h, h->getOwner());
  543. callInterfaceIfPresent(cl, h->tempOwner, &IGameEventsReceiver::heroCreated, h);
  544. }
  545. void ApplyFirstClientNetPackVisitor::visitGiveHero(GiveHero & pack)
  546. {
  547. }
  548. void ApplyClientNetPackVisitor::visitInfoWindow(InfoWindow & pack)
  549. {
  550. std::string str = pack.text.toString();
  551. if(!callInterfaceIfPresent(cl, pack.player, &CGameInterface::showInfoDialog, pack.type, str, pack.components,(soundBase::soundID)pack.soundID))
  552. logNetwork->warn("We received InfoWindow for not our player...");
  553. }
  554. void ApplyFirstClientNetPackVisitor::visitSetObjectProperty(SetObjectProperty & pack)
  555. {
  556. //inform all players that see this object
  557. for(auto it = cl.playerint.cbegin(); it != cl.playerint.cend(); ++it)
  558. {
  559. if(gs.isVisible(gs.getObjInstance(pack.id), it->first))
  560. callInterfaceIfPresent(cl, it->first, &IGameEventsReceiver::beforeObjectPropertyChanged, &pack);
  561. }
  562. // invalidate section of map view with our object and force an update with new flag color
  563. if (pack.what == ObjProperty::OWNER)
  564. {
  565. auto object = gs.getObjInstance(pack.id);
  566. CGI->mh->onObjectInstantRemove(object, object->getOwner());
  567. }
  568. }
  569. void ApplyClientNetPackVisitor::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::objectPropertyChanged, &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)
  579. {
  580. auto object = gs.getObjInstance(pack.id);
  581. CGI->mh->onObjectInstantAdd(object, object->getOwner());
  582. }
  583. }
  584. void ApplyClientNetPackVisitor::visitHeroLevelUp(HeroLevelUp & pack)
  585. {
  586. const CGHeroInstance * hero = cl.getHero(pack.heroId);
  587. assert(hero);
  588. callOnlyThatInterface(cl, pack.player, &CGameInterface::heroGotLevel, hero, pack.primskill, pack.skills, pack.queryID);
  589. }
  590. void ApplyClientNetPackVisitor::visitCommanderLevelUp(CommanderLevelUp & pack)
  591. {
  592. const CGHeroInstance * hero = cl.getHero(pack.heroId);
  593. assert(hero);
  594. const CCommanderInstance * commander = hero->commander;
  595. assert(commander);
  596. assert(commander->armyObj); //is it possible for Commander to exist beyond armed instance?
  597. callOnlyThatInterface(cl, pack.player, &CGameInterface::commanderGotLevel, commander, pack.skills, pack.queryID);
  598. }
  599. void ApplyClientNetPackVisitor::visitBlockingDialog(BlockingDialog & pack)
  600. {
  601. std::string str = pack.text.toString();
  602. if(!callOnlyThatInterface(cl, pack.player, &CGameInterface::showBlockingDialog, str, pack.components, pack.queryID, (soundBase::soundID)pack.soundID, pack.selection(), pack.cancel()))
  603. logNetwork->warn("We received YesNoDialog for not our player...");
  604. }
  605. void ApplyClientNetPackVisitor::visitGarrisonDialog(GarrisonDialog & pack)
  606. {
  607. const CGHeroInstance *h = cl.getHero(pack.hid);
  608. const CArmedInstance *obj = static_cast<const CArmedInstance*>(cl.getObj(pack.objid));
  609. callOnlyThatInterface(cl, h->getOwner(), &CGameInterface::showGarrisonDialog, obj, h, pack.removableUnits, pack.queryID);
  610. }
  611. void ApplyClientNetPackVisitor::visitExchangeDialog(ExchangeDialog & pack)
  612. {
  613. callInterfaceIfPresent(cl, pack.player, &IGameEventsReceiver::heroExchangeStarted, pack.hero1, pack.hero2, pack.queryID);
  614. }
  615. void ApplyClientNetPackVisitor::visitTeleportDialog(TeleportDialog & pack)
  616. {
  617. const CGHeroInstance *h = cl.getHero(pack.hero);
  618. callOnlyThatInterface(cl, h->getOwner(), &CGameInterface::showTeleportDialog, h, pack.channel, pack.exits, pack.impassable, pack.queryID);
  619. }
  620. void ApplyClientNetPackVisitor::visitMapObjectSelectDialog(MapObjectSelectDialog & pack)
  621. {
  622. callOnlyThatInterface(cl, pack.player, &CGameInterface::showMapObjectSelectDialog, pack.queryID, pack.icon, pack.title, pack.description, pack.objects);
  623. }
  624. void ApplyFirstClientNetPackVisitor::visitBattleStart(BattleStart & pack)
  625. {
  626. // Cannot use the usual code because curB is not set yet
  627. callOnlyThatBattleInterface(cl, pack.info->sides[0].color, &IBattleEventsReceiver::battleStartBefore, pack.battleID, pack.info->sides[0].armyObject, pack.info->sides[1].armyObject,
  628. pack.info->tile, pack.info->sides[0].hero, pack.info->sides[1].hero);
  629. callOnlyThatBattleInterface(cl, pack.info->sides[1].color, &IBattleEventsReceiver::battleStartBefore, pack.battleID, pack.info->sides[0].armyObject, pack.info->sides[1].armyObject,
  630. pack.info->tile, pack.info->sides[0].hero, pack.info->sides[1].hero);
  631. callOnlyThatBattleInterface(cl, PlayerColor::SPECTATOR, &IBattleEventsReceiver::battleStartBefore, pack.battleID, pack.info->sides[0].armyObject, pack.info->sides[1].armyObject,
  632. pack.info->tile, pack.info->sides[0].hero, pack.info->sides[1].hero);
  633. }
  634. void ApplyClientNetPackVisitor::visitBattleStart(BattleStart & pack)
  635. {
  636. cl.battleStarted(pack.info);
  637. }
  638. void ApplyFirstClientNetPackVisitor::visitBattleNextRound(BattleNextRound & pack)
  639. {
  640. callBattleInterfaceIfPresentForBothSides(cl, pack.battleID, &IBattleEventsReceiver::battleNewRoundFirst, pack.battleID);
  641. }
  642. void ApplyClientNetPackVisitor::visitBattleNextRound(BattleNextRound & pack)
  643. {
  644. callBattleInterfaceIfPresentForBothSides(cl, pack.battleID, &IBattleEventsReceiver::battleNewRound, pack.battleID);
  645. }
  646. void ApplyClientNetPackVisitor::visitBattleSetActiveStack(BattleSetActiveStack & pack)
  647. {
  648. if(!pack.askPlayerInterface)
  649. return;
  650. const CStack *activated = gs.getBattle(pack.battleID)->battleGetStackByID(pack.stack);
  651. PlayerColor playerToCall; //pack.player that will move activated stack
  652. if (activated->hasBonusOfType(BonusType::HYPNOTIZED))
  653. {
  654. playerToCall = (gs.getBattle(pack.battleID)->sides[0].color == activated->unitOwner()
  655. ? gs.getBattle(pack.battleID)->sides[1].color
  656. : gs.getBattle(pack.battleID)->sides[0].color);
  657. }
  658. else
  659. {
  660. playerToCall = activated->unitOwner();
  661. }
  662. cl.startPlayerBattleAction(pack.battleID, playerToCall);
  663. }
  664. void ApplyClientNetPackVisitor::visitBattleLogMessage(BattleLogMessage & pack)
  665. {
  666. callBattleInterfaceIfPresentForBothSides(cl, pack.battleID, &IBattleEventsReceiver::battleLogMessage, pack.battleID, pack.lines);
  667. }
  668. void ApplyClientNetPackVisitor::visitBattleTriggerEffect(BattleTriggerEffect & pack)
  669. {
  670. callBattleInterfaceIfPresentForBothSides(cl, pack.battleID, &IBattleEventsReceiver::battleTriggerEffect, pack.battleID, pack);
  671. }
  672. void ApplyFirstClientNetPackVisitor::visitBattleUpdateGateState(BattleUpdateGateState & pack)
  673. {
  674. callBattleInterfaceIfPresentForBothSides(cl, pack.battleID, &IBattleEventsReceiver::battleGateStateChanged, pack.battleID, pack.state);
  675. }
  676. void ApplyFirstClientNetPackVisitor::visitBattleResult(BattleResult & pack)
  677. {
  678. callBattleInterfaceIfPresentForBothSides(cl, pack.battleID, &IBattleEventsReceiver::battleEnd, pack.battleID, &pack, pack.queryID);
  679. cl.battleFinished(pack.battleID);
  680. }
  681. void ApplyFirstClientNetPackVisitor::visitBattleStackMoved(BattleStackMoved & pack)
  682. {
  683. const CStack * movedStack = gs.getBattle(pack.battleID)->battleGetStackByID(pack.stack);
  684. callBattleInterfaceIfPresentForBothSides(cl, pack.battleID, &IBattleEventsReceiver::battleStackMoved, pack.battleID, movedStack, pack.tilesToMove, pack.distance, pack.teleporting);
  685. }
  686. void ApplyFirstClientNetPackVisitor::visitBattleAttack(BattleAttack & pack)
  687. {
  688. callBattleInterfaceIfPresentForBothSides(cl, pack.battleID, &IBattleEventsReceiver::battleAttack, pack.battleID, &pack);
  689. // battleStacksAttacked should be excuted before BattleAttack.applyGs() to play animation before damaging unit
  690. // so this has to be here instead of ApplyClientNetPackVisitor::visitBattleAttack()
  691. callBattleInterfaceIfPresentForBothSides(cl, pack.battleID, &IBattleEventsReceiver::battleStacksAttacked, pack.battleID, pack.bsa, pack.shot());
  692. }
  693. void ApplyClientNetPackVisitor::visitBattleAttack(BattleAttack & pack)
  694. {
  695. }
  696. void ApplyFirstClientNetPackVisitor::visitStartAction(StartAction & pack)
  697. {
  698. cl.currentBattleAction = std::make_unique<BattleAction>(pack.ba);
  699. callBattleInterfaceIfPresentForBothSides(cl, pack.battleID, &IBattleEventsReceiver::actionStarted, pack.battleID, pack.ba);
  700. }
  701. void ApplyClientNetPackVisitor::visitBattleSpellCast(BattleSpellCast & pack)
  702. {
  703. callBattleInterfaceIfPresentForBothSides(cl, pack.battleID, &IBattleEventsReceiver::battleSpellCast, pack.battleID, &pack);
  704. }
  705. void ApplyClientNetPackVisitor::visitSetStackEffect(SetStackEffect & pack)
  706. {
  707. //informing about effects
  708. callBattleInterfaceIfPresentForBothSides(cl, pack.battleID, &IBattleEventsReceiver::battleStacksEffectsSet, pack.battleID, pack);
  709. }
  710. void ApplyClientNetPackVisitor::visitStacksInjured(StacksInjured & pack)
  711. {
  712. callBattleInterfaceIfPresentForBothSides(cl, pack.battleID, &IBattleEventsReceiver::battleStacksAttacked, pack.battleID, pack.stacks, false);
  713. }
  714. void ApplyClientNetPackVisitor::visitBattleResultsApplied(BattleResultsApplied & pack)
  715. {
  716. callInterfaceIfPresent(cl, pack.player1, &IGameEventsReceiver::battleResultsApplied);
  717. callInterfaceIfPresent(cl, pack.player2, &IGameEventsReceiver::battleResultsApplied);
  718. callInterfaceIfPresent(cl, PlayerColor::SPECTATOR, &IGameEventsReceiver::battleResultsApplied);
  719. }
  720. void ApplyClientNetPackVisitor::visitBattleUnitsChanged(BattleUnitsChanged & pack)
  721. {
  722. callBattleInterfaceIfPresentForBothSides(cl, pack.battleID, &IBattleEventsReceiver::battleUnitsChanged, pack.battleID, pack.changedStacks);
  723. }
  724. void ApplyClientNetPackVisitor::visitBattleObstaclesChanged(BattleObstaclesChanged & pack)
  725. {
  726. //inform interfaces about removed obstacles
  727. callBattleInterfaceIfPresentForBothSides(cl, pack.battleID, &IBattleEventsReceiver::battleObstaclesChanged, pack.battleID, pack.changes);
  728. }
  729. void ApplyClientNetPackVisitor::visitCatapultAttack(CatapultAttack & pack)
  730. {
  731. //inform interfaces about catapult attack
  732. callBattleInterfaceIfPresentForBothSides(cl, pack.battleID, &IBattleEventsReceiver::battleCatapultAttacked, pack.battleID, pack);
  733. }
  734. void ApplyClientNetPackVisitor::visitEndAction(EndAction & pack)
  735. {
  736. callBattleInterfaceIfPresentForBothSides(cl, pack.battleID, &IBattleEventsReceiver::actionFinished, pack.battleID, *cl.currentBattleAction);
  737. cl.currentBattleAction.reset();
  738. }
  739. void ApplyClientNetPackVisitor::visitPackageApplied(PackageApplied & pack)
  740. {
  741. callInterfaceIfPresent(cl, pack.player, &IGameEventsReceiver::requestRealized, &pack);
  742. if(!CClient::waitingRequest.tryRemovingElement(pack.requestID))
  743. logNetwork->warn("Surprising server message! PackageApplied for unknown requestID!");
  744. }
  745. void ApplyClientNetPackVisitor::visitSystemMessage(SystemMessage & pack)
  746. {
  747. std::ostringstream str;
  748. str << "System message: " << pack.text;
  749. logNetwork->error(str.str()); // usually used to receive error messages from server
  750. if(LOCPLINT && !settings["session"]["hideSystemMessages"].Bool())
  751. LOCPLINT->cingconsole->print(str.str());
  752. }
  753. void ApplyClientNetPackVisitor::visitPlayerBlocked(PlayerBlocked & pack)
  754. {
  755. callInterfaceIfPresent(cl, pack.player, &IGameEventsReceiver::playerBlocked, pack.reason, pack.startOrEnd == PlayerBlocked::BLOCKADE_STARTED);
  756. }
  757. void ApplyClientNetPackVisitor::visitPlayerStartsTurn(PlayerStartsTurn & pack)
  758. {
  759. logNetwork->debug("Server gives turn to %s", pack.player.toString());
  760. callAllInterfaces(cl, &IGameEventsReceiver::playerStartsTurn, pack.player);
  761. callOnlyThatInterface(cl, pack.player, &CGameInterface::yourTurn, pack.queryID);
  762. }
  763. void ApplyClientNetPackVisitor::visitPlayerEndsTurn(PlayerEndsTurn & pack)
  764. {
  765. logNetwork->debug("Server ends turn of %s", pack.player.toString());
  766. callAllInterfaces(cl, &IGameEventsReceiver::playerEndsTurn, pack.player);
  767. }
  768. void ApplyClientNetPackVisitor::visitTurnTimeUpdate(TurnTimeUpdate & pack)
  769. {
  770. 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());
  771. }
  772. void ApplyClientNetPackVisitor::visitPlayerMessageClient(PlayerMessageClient & pack)
  773. {
  774. logNetwork->debug("pack.player %s sends a message: %s", pack.player.toString(), pack.text);
  775. std::ostringstream str;
  776. if(pack.player.isSpectator())
  777. str << "Spectator: " << pack.text;
  778. else
  779. str << cl.getPlayerState(pack.player)->nodeName() <<": " << pack.text;
  780. if(LOCPLINT)
  781. LOCPLINT->cingconsole->print(str.str());
  782. }
  783. void ApplyClientNetPackVisitor::visitAdvmapSpellCast(AdvmapSpellCast & pack)
  784. {
  785. cl.invalidatePaths();
  786. auto caster = cl.getHero(pack.casterID);
  787. if(caster)
  788. //consider notifying other interfaces that see hero?
  789. callInterfaceIfPresent(cl, caster->getOwner(), &IGameEventsReceiver::advmapSpellCast, caster, pack.spellID);
  790. else
  791. logNetwork->error("Invalid hero instance");
  792. }
  793. void ApplyClientNetPackVisitor::visitShowWorldViewEx(ShowWorldViewEx & pack)
  794. {
  795. callOnlyThatInterface(cl, pack.player, &CGameInterface::showWorldViewEx, pack.objectPositions, pack.showTerrain);
  796. }
  797. void ApplyClientNetPackVisitor::visitOpenWindow(OpenWindow & pack)
  798. {
  799. switch(pack.window)
  800. {
  801. case EOpenWindowMode::RECRUITMENT_FIRST:
  802. case EOpenWindowMode::RECRUITMENT_ALL:
  803. {
  804. const CGDwelling *dw = dynamic_cast<const CGDwelling*>(cl.getObj(ObjectInstanceID(pack.object)));
  805. const CArmedInstance *dst = dynamic_cast<const CArmedInstance*>(cl.getObj(ObjectInstanceID(pack.visitor)));
  806. callInterfaceIfPresent(cl, dst->tempOwner, &IGameEventsReceiver::showRecruitmentDialog, dw, dst, pack.window == EOpenWindowMode::RECRUITMENT_FIRST ? 0 : -1, pack.queryID);
  807. }
  808. break;
  809. case EOpenWindowMode::SHIPYARD_WINDOW:
  810. {
  811. assert(pack.queryID == QueryID::NONE);
  812. const IShipyard *sy = IShipyard::castFrom(cl.getObj(ObjectInstanceID(pack.object)));
  813. callInterfaceIfPresent(cl, sy->getObject()->getOwner(), &IGameEventsReceiver::showShipyardDialog, sy);
  814. }
  815. break;
  816. case EOpenWindowMode::THIEVES_GUILD:
  817. {
  818. assert(pack.queryID == QueryID::NONE);
  819. //displays Thieves' Guild window (when hero enters Den of Thieves)
  820. const CGObjectInstance *obj = cl.getObj(ObjectInstanceID(pack.object));
  821. const CGHeroInstance *hero = cl.getHero(ObjectInstanceID(pack.visitor));
  822. callInterfaceIfPresent(cl, hero->getOwner(), &IGameEventsReceiver::showThievesGuildWindow, obj);
  823. }
  824. break;
  825. case EOpenWindowMode::UNIVERSITY_WINDOW:
  826. {
  827. //displays University window (when hero enters University on adventure map)
  828. const IMarket *market = IMarket::castFrom(cl.getObj(ObjectInstanceID(pack.object)));
  829. const CGHeroInstance *hero = cl.getHero(ObjectInstanceID(pack.visitor));
  830. callInterfaceIfPresent(cl, hero->tempOwner, &IGameEventsReceiver::showUniversityWindow, market, hero, pack.queryID);
  831. }
  832. break;
  833. case EOpenWindowMode::MARKET_WINDOW:
  834. {
  835. //displays Thieves' Guild window (when hero enters Den of Thieves)
  836. const CGObjectInstance *obj = cl.getObj(ObjectInstanceID(pack.object));
  837. const CGHeroInstance *hero = cl.getHero(ObjectInstanceID(pack.visitor));
  838. const IMarket *market = IMarket::castFrom(obj);
  839. callInterfaceIfPresent(cl, cl.getTile(obj->visitablePos())->visitableObjects.back()->tempOwner, &IGameEventsReceiver::showMarketWindow, market, hero, pack.queryID);
  840. }
  841. break;
  842. case EOpenWindowMode::HILL_FORT_WINDOW:
  843. {
  844. assert(pack.queryID == QueryID::NONE);
  845. //displays Hill fort window
  846. const CGObjectInstance *obj = cl.getObj(ObjectInstanceID(pack.object));
  847. const CGHeroInstance *hero = cl.getHero(ObjectInstanceID(pack.visitor));
  848. callInterfaceIfPresent(cl, cl.getTile(obj->visitablePos())->visitableObjects.back()->tempOwner, &IGameEventsReceiver::showHillFortWindow, obj, hero);
  849. }
  850. break;
  851. case EOpenWindowMode::PUZZLE_MAP:
  852. {
  853. assert(pack.queryID == QueryID::NONE);
  854. const CGHeroInstance *hero = cl.getHero(ObjectInstanceID(pack.visitor));
  855. callInterfaceIfPresent(cl, hero->getOwner(), &IGameEventsReceiver::showPuzzleMap);
  856. }
  857. break;
  858. case EOpenWindowMode::TAVERN_WINDOW:
  859. {
  860. const CGObjectInstance *obj1 = cl.getObj(ObjectInstanceID(pack.object));
  861. const CGHeroInstance * hero = cl.getHero(ObjectInstanceID(pack.visitor));
  862. callInterfaceIfPresent(cl, hero->tempOwner, &IGameEventsReceiver::showTavernWindow, obj1, hero, pack.queryID);
  863. }
  864. break;
  865. }
  866. }
  867. void ApplyClientNetPackVisitor::visitCenterView(CenterView & pack)
  868. {
  869. callInterfaceIfPresent(cl, pack.player, &IGameEventsReceiver::centerView, pack.pos, pack.focusTime);
  870. }
  871. void ApplyClientNetPackVisitor::visitNewObject(NewObject & pack)
  872. {
  873. cl.invalidatePaths();
  874. const CGObjectInstance *obj = cl.getObj(pack.createdObjectID);
  875. if(CGI->mh)
  876. CGI->mh->onObjectFadeIn(obj, pack.initiator);
  877. for(auto i=cl.playerint.begin(); i!=cl.playerint.end(); i++)
  878. {
  879. if(gs.isVisible(obj, i->first))
  880. i->second->newObject(obj);
  881. }
  882. CGI->mh->waitForOngoingAnimations();
  883. }
  884. void ApplyClientNetPackVisitor::visitSetAvailableArtifacts(SetAvailableArtifacts & pack)
  885. {
  886. if(pack.id < 0) //artifact merchants globally
  887. {
  888. callAllInterfaces(cl, &IGameEventsReceiver::availableArtifactsChanged, nullptr);
  889. }
  890. else
  891. {
  892. const CGBlackMarket *bm = dynamic_cast<const CGBlackMarket *>(cl.getObj(ObjectInstanceID(pack.id)));
  893. assert(bm);
  894. callInterfaceIfPresent(cl, cl.getTile(bm->visitablePos())->visitableObjects.back()->tempOwner, &IGameEventsReceiver::availableArtifactsChanged, bm);
  895. }
  896. }
  897. void ApplyClientNetPackVisitor::visitEntitiesChanged(EntitiesChanged & pack)
  898. {
  899. cl.invalidatePaths();
  900. }