NetPacksClient.cpp 37 KB

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