NetPacksClient.cpp 40 KB

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