NetPacksClient.cpp 40 KB

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