NetPacksClient.cpp 38 KB

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