NetPacksClient.cpp 37 KB

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