NetPacksClient.cpp 41 KB

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