NetPacksClient.cpp 36 KB

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