NetPacksClient.cpp 41 KB

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