2
0

NetPacksClient.cpp 37 KB

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