NetPacksClient.cpp 37 KB

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