NetPacksClient.cpp 36 KB

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