NetPacksClient.cpp 38 KB

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