NetPacksClient.cpp 36 KB

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