NetPacksClient.cpp 39 KB

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