NetPacksClient.cpp 41 KB

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