NetPacksClient.cpp 38 KB

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