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