NetPacksClient.cpp 39 KB

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