NetPacksClient.cpp 40 KB

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