NetPacksClient.cpp 39 KB

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