NetPacksClient.cpp 40 KB

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