NetPacksClient.cpp 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000
  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 "../lib/NetPacks.h"
  12. #include "Client.h"
  13. #include "CPlayerInterface.h"
  14. #include "CGameInfo.h"
  15. #include "windows/GUIClasses.h"
  16. #include "adventureMap/mapHandler.h"
  17. #include "adventureMap/CInGameConsole.h"
  18. #include "battle/BattleInterface.h"
  19. #include "gui/CGuiHandler.h"
  20. #include "widgets/MiscWidgets.h"
  21. #include "CMT.h"
  22. #include "CServerHandler.h"
  23. #include "../CCallback.h"
  24. #include "../lib/filesystem/Filesystem.h"
  25. #include "../lib/filesystem/FileInfo.h"
  26. #include "../lib/serializer/Connection.h"
  27. #include "../lib/serializer/BinarySerializer.h"
  28. #include "../lib/CGeneralTextHandler.h"
  29. #include "../lib/CHeroHandler.h"
  30. #include "../lib/VCMI_Lib.h"
  31. #include "../lib/mapping/CMap.h"
  32. #include "../lib/VCMIDirs.h"
  33. #include "../lib/spells/CSpellHandler.h"
  34. #include "../lib/CSoundBase.h"
  35. #include "../lib/StartInfo.h"
  36. #include "../lib/CConfigHandler.h"
  37. #include "../lib/mapping/CCampaignHandler.h"
  38. #include "../lib/CGameState.h"
  39. #include "../lib/CStack.h"
  40. #include "../lib/battle/BattleInfo.h"
  41. #include "../lib/GameConstants.h"
  42. #include "../lib/CPlayerState.h"
  43. // TODO: as Tow suggested these template should all be part of CClient
  44. // This will require rework spectator interface properly though
  45. template<typename T, typename ... Args, typename ... Args2>
  46. bool callOnlyThatInterface(CClient * cl, PlayerColor player, void (T::*ptr)(Args...), Args2 && ...args)
  47. {
  48. if(vstd::contains(cl->playerint, player))
  49. {
  50. ((*cl->playerint[player]).*ptr)(std::forward<Args2>(args)...);
  51. return true;
  52. }
  53. return false;
  54. }
  55. template<typename T, typename ... Args, typename ... Args2>
  56. bool callInterfaceIfPresent(CClient * cl, PlayerColor player, void (T::*ptr)(Args...), Args2 && ...args)
  57. {
  58. bool called = callOnlyThatInterface(cl, player, ptr, std::forward<Args2>(args)...);
  59. return called;
  60. }
  61. template<typename T, typename ... Args, typename ... Args2>
  62. void callOnlyThatBattleInterface(CClient * cl, PlayerColor player, void (T::*ptr)(Args...), Args2 && ...args)
  63. {
  64. if(vstd::contains(cl->battleints,player))
  65. ((*cl->battleints[player]).*ptr)(std::forward<Args2>(args)...);
  66. if(cl->additionalBattleInts.count(player))
  67. {
  68. for(auto bInt : cl->additionalBattleInts[player])
  69. ((*bInt).*ptr)(std::forward<Args2>(args)...);
  70. }
  71. }
  72. template<typename T, typename ... Args, typename ... Args2>
  73. void callBattleInterfaceIfPresent(CClient * cl, PlayerColor player, void (T::*ptr)(Args...), Args2 && ...args)
  74. {
  75. callOnlyThatInterface(cl, player, ptr, std::forward<Args2>(args)...);
  76. }
  77. //calls all normal interfaces and privileged ones, playerints may be updated when iterating over it, so we need a copy
  78. template<typename T, typename ... Args, typename ... Args2>
  79. void callAllInterfaces(CClient * cl, void (T::*ptr)(Args...), Args2 && ...args)
  80. {
  81. for(auto pInt : cl->playerint)
  82. ((*pInt.second).*ptr)(std::forward<Args2>(args)...);
  83. }
  84. //calls all normal interfaces and privileged ones, playerints may be updated when iterating over it, so we need a copy
  85. template<typename T, typename ... Args, typename ... Args2>
  86. void callBattleInterfaceIfPresentForBothSides(CClient * cl, void (T::*ptr)(Args...), Args2 && ...args)
  87. {
  88. callOnlyThatBattleInterface(cl, cl->gameState()->curB->sides[0].color, ptr, std::forward<Args2>(args)...);
  89. callOnlyThatBattleInterface(cl, cl->gameState()->curB->sides[1].color, ptr, std::forward<Args2>(args)...);
  90. if(settings["session"]["spectate"].Bool() && !settings["session"]["spectate-skip-battle"].Bool() && LOCPLINT->battleInt)
  91. {
  92. callOnlyThatBattleInterface(cl, PlayerColor::SPECTATOR, ptr, std::forward<Args2>(args)...);
  93. }
  94. }
  95. void SetResources::applyCl(CClient *cl)
  96. {
  97. //todo: inform on actual resource set transfered
  98. callInterfaceIfPresent(cl, player, &IGameEventsReceiver::receivedResource);
  99. }
  100. void SetPrimSkill::applyCl(CClient *cl)
  101. {
  102. const CGHeroInstance *h = cl->getHero(id);
  103. if(!h)
  104. {
  105. logNetwork->error("Cannot find hero with ID %d", id.getNum());
  106. return;
  107. }
  108. callInterfaceIfPresent(cl, h->tempOwner, &IGameEventsReceiver::heroPrimarySkillChanged, h, which, val);
  109. }
  110. void SetSecSkill::applyCl(CClient *cl)
  111. {
  112. const CGHeroInstance *h = cl->getHero(id);
  113. if(!h)
  114. {
  115. logNetwork->error("Cannot find hero with ID %d", id.getNum());
  116. return;
  117. }
  118. callInterfaceIfPresent(cl, h->tempOwner, &IGameEventsReceiver::heroSecondarySkillChanged, h, which, val);
  119. }
  120. void HeroVisitCastle::applyCl(CClient *cl)
  121. {
  122. const CGHeroInstance *h = cl->getHero(hid);
  123. if(start())
  124. {
  125. callInterfaceIfPresent(cl, h->tempOwner, &IGameEventsReceiver::heroVisitsTown, h, GS(cl)->getTown(tid));
  126. }
  127. }
  128. void ChangeSpells::applyCl(CClient *cl)
  129. {
  130. //TODO: inform interface?
  131. }
  132. void SetMana::applyCl(CClient *cl)
  133. {
  134. const CGHeroInstance *h = cl->getHero(hid);
  135. callInterfaceIfPresent(cl, h->tempOwner, &IGameEventsReceiver::heroManaPointsChanged, h);
  136. }
  137. void SetMovePoints::applyCl(CClient *cl)
  138. {
  139. const CGHeroInstance *h = cl->getHero(hid);
  140. cl->invalidatePaths();
  141. callInterfaceIfPresent(cl, h->tempOwner, &IGameEventsReceiver::heroMovePointsChanged, h);
  142. }
  143. void FoWChange::applyCl(CClient *cl)
  144. {
  145. for(auto &i : cl->playerint)
  146. {
  147. if(cl->getPlayerRelations(i.first, player) == PlayerRelations::SAME_PLAYER && waitForDialogs && LOCPLINT == i.second.get())
  148. {
  149. LOCPLINT->waitWhileDialog();
  150. }
  151. if(cl->getPlayerRelations(i.first, player) != PlayerRelations::ENEMIES)
  152. {
  153. if(mode)
  154. i.second->tileRevealed(tiles);
  155. else
  156. i.second->tileHidden(tiles);
  157. }
  158. }
  159. cl->invalidatePaths();
  160. }
  161. void SetAvailableHeroes::applyCl(CClient *cl)
  162. {
  163. //TODO: inform interface?
  164. }
  165. static void dispatchGarrisonChange(CClient * cl, ObjectInstanceID army1, ObjectInstanceID army2)
  166. {
  167. auto obj1 = cl->getObj(army1);
  168. if(!obj1)
  169. {
  170. logNetwork->error("Cannot find army with ID %d", army1.getNum());
  171. return;
  172. }
  173. callInterfaceIfPresent(cl, obj1->tempOwner, &IGameEventsReceiver::garrisonsChanged, army1, army2);
  174. if(army2 != ObjectInstanceID() && army2 != army1)
  175. {
  176. auto obj2 = cl->getObj(army2);
  177. if(!obj2)
  178. {
  179. logNetwork->error("Cannot find army with ID %d", army2.getNum());
  180. return;
  181. }
  182. if(obj1->tempOwner != obj2->tempOwner)
  183. callInterfaceIfPresent(cl, obj2->tempOwner, &IGameEventsReceiver::garrisonsChanged, army1, army2);
  184. }
  185. }
  186. void ChangeStackCount::applyCl(CClient * cl)
  187. {
  188. dispatchGarrisonChange(cl, army, ObjectInstanceID());
  189. }
  190. void SetStackType::applyCl(CClient * cl)
  191. {
  192. dispatchGarrisonChange(cl, army, ObjectInstanceID());
  193. }
  194. void EraseStack::applyCl(CClient * cl)
  195. {
  196. dispatchGarrisonChange(cl, army, ObjectInstanceID());
  197. }
  198. void SwapStacks::applyCl(CClient * cl)
  199. {
  200. dispatchGarrisonChange(cl, srcArmy, dstArmy);
  201. }
  202. void InsertNewStack::applyCl(CClient * cl)
  203. {
  204. dispatchGarrisonChange(cl, army, ObjectInstanceID());
  205. }
  206. void RebalanceStacks::applyCl(CClient * cl)
  207. {
  208. dispatchGarrisonChange(cl, srcArmy, dstArmy);
  209. }
  210. void BulkRebalanceStacks::applyCl(CClient * cl)
  211. {
  212. if(!moves.empty())
  213. {
  214. auto destArmy = moves[0].srcArmy == moves[0].dstArmy
  215. ? ObjectInstanceID()
  216. : moves[0].dstArmy;
  217. dispatchGarrisonChange(cl, moves[0].srcArmy, destArmy);
  218. }
  219. }
  220. void BulkSmartRebalanceStacks::applyCl(CClient * cl)
  221. {
  222. if(!moves.empty())
  223. {
  224. assert(moves[0].srcArmy == moves[0].dstArmy);
  225. dispatchGarrisonChange(cl, moves[0].srcArmy, ObjectInstanceID());
  226. }
  227. else if(!changes.empty())
  228. {
  229. dispatchGarrisonChange(cl, changes[0].army, ObjectInstanceID());
  230. }
  231. }
  232. void PutArtifact::applyCl(CClient *cl)
  233. {
  234. callInterfaceIfPresent(cl, al.owningPlayer(), &IGameEventsReceiver::artifactPut, al);
  235. }
  236. void EraseArtifact::applyCl(CClient *cl)
  237. {
  238. callInterfaceIfPresent(cl, al.owningPlayer(), &IGameEventsReceiver::artifactRemoved, al);
  239. }
  240. void MoveArtifact::applyCl(CClient * cl)
  241. {
  242. auto moveArtifact = [this, cl](PlayerColor player) -> void
  243. {
  244. callInterfaceIfPresent(cl, player, &IGameEventsReceiver::artifactMoved, src, dst);
  245. if(askAssemble)
  246. callInterfaceIfPresent(cl, player, &IGameEventsReceiver::artifactPossibleAssembling, dst);
  247. };
  248. moveArtifact(src.owningPlayer());
  249. if(src.owningPlayer() != dst.owningPlayer())
  250. moveArtifact(dst.owningPlayer());
  251. }
  252. void BulkMoveArtifacts::applyCl(CClient * cl)
  253. {
  254. auto applyMove = [this, cl](std::vector<LinkedSlots> & artsPack) -> void
  255. {
  256. for(auto & slotToMove : artsPack)
  257. {
  258. auto srcLoc = ArtifactLocation(srcArtHolder, slotToMove.srcPos);
  259. auto dstLoc = ArtifactLocation(dstArtHolder, slotToMove.dstPos);
  260. MoveArtifact ma(&srcLoc, &dstLoc, false);
  261. ma.applyCl(cl);
  262. }
  263. };
  264. applyMove(artsPack0);
  265. if(swap)
  266. applyMove(artsPack1);
  267. }
  268. void AssembledArtifact::applyCl(CClient *cl)
  269. {
  270. callInterfaceIfPresent(cl, al.owningPlayer(), &IGameEventsReceiver::artifactAssembled, al);
  271. }
  272. void DisassembledArtifact::applyCl(CClient *cl)
  273. {
  274. callInterfaceIfPresent(cl, al.owningPlayer(), &IGameEventsReceiver::artifactDisassembled, al);
  275. }
  276. void HeroVisit::applyCl(CClient * cl)
  277. {
  278. auto hero = cl->getHero(heroId);
  279. auto obj = cl->getObj(objId, false);
  280. callInterfaceIfPresent(cl, player, &IGameEventsReceiver::heroVisit, hero, obj, starting);
  281. }
  282. void NewTurn::applyCl(CClient *cl)
  283. {
  284. cl->invalidatePaths();
  285. }
  286. void GiveBonus::applyCl(CClient *cl)
  287. {
  288. cl->invalidatePaths();
  289. switch(who)
  290. {
  291. case HERO:
  292. {
  293. const CGHeroInstance *h = GS(cl)->getHero(ObjectInstanceID(id));
  294. callInterfaceIfPresent(cl, h->tempOwner, &IGameEventsReceiver::heroBonusChanged, h, *h->getBonusList().back(), true);
  295. }
  296. break;
  297. case PLAYER:
  298. {
  299. const PlayerState *p = GS(cl)->getPlayerState(PlayerColor(id));
  300. callInterfaceIfPresent(cl, PlayerColor(id), &IGameEventsReceiver::playerBonusChanged, *p->getBonusList().back(), true);
  301. }
  302. break;
  303. }
  304. }
  305. void ChangeObjPos::applyFirstCl(CClient *cl)
  306. {
  307. CGObjectInstance *obj = GS(cl)->getObjInstance(objid);
  308. if(flags & 1 && CGI->mh)
  309. CGI->mh->hideObject(obj);
  310. }
  311. void ChangeObjPos::applyCl(CClient *cl)
  312. {
  313. CGObjectInstance *obj = GS(cl)->getObjInstance(objid);
  314. if(flags & 1 && CGI->mh)
  315. CGI->mh->printObject(obj);
  316. cl->invalidatePaths();
  317. }
  318. void PlayerEndsGame::applyCl(CClient *cl)
  319. {
  320. callAllInterfaces(cl, &IGameEventsReceiver::gameOver, player, victoryLossCheckResult);
  321. // In auto testing mode we always close client if red player won or lose
  322. if(!settings["session"]["testmap"].isNull() && player == PlayerColor(0))
  323. handleQuit(settings["session"]["spectate"].Bool()); // if spectator is active ask to close client or not
  324. }
  325. void PlayerReinitInterface::applyCl(CClient * cl)
  326. {
  327. auto initInterfaces = [cl]()
  328. {
  329. cl->initPlayerInterfaces();
  330. auto currentPlayer = cl->gameState()->currentPlayer;
  331. callAllInterfaces(cl, &IGameEventsReceiver::playerStartsTurn, currentPlayer);
  332. callOnlyThatInterface(cl, currentPlayer, &CGameInterface::yourTurn);
  333. };
  334. for(auto player : players)
  335. {
  336. auto & plSettings = CSH->si->getIthPlayersSettings(player);
  337. if(playerConnectionId == PlayerSettings::PLAYER_AI)
  338. {
  339. plSettings.connectedPlayerIDs.clear();
  340. cl->initPlayerEnvironments();
  341. initInterfaces();
  342. }
  343. else if(playerConnectionId == CSH->c->connectionID)
  344. {
  345. plSettings.connectedPlayerIDs.insert(playerConnectionId);
  346. cl->playerint.clear();
  347. initInterfaces();
  348. }
  349. }
  350. }
  351. void RemoveBonus::applyCl(CClient *cl)
  352. {
  353. cl->invalidatePaths();
  354. switch(who)
  355. {
  356. case HERO:
  357. {
  358. const CGHeroInstance *h = GS(cl)->getHero(ObjectInstanceID(id));
  359. callInterfaceIfPresent(cl, h->tempOwner, &IGameEventsReceiver::heroBonusChanged, h, bonus, false);
  360. }
  361. break;
  362. case PLAYER:
  363. {
  364. //const PlayerState *p = GS(cl)->getPlayerState(id);
  365. callInterfaceIfPresent(cl, PlayerColor(id), &IGameEventsReceiver::playerBonusChanged, bonus, false);
  366. }
  367. break;
  368. }
  369. }
  370. void RemoveObject::applyFirstCl(CClient *cl)
  371. {
  372. const CGObjectInstance *o = cl->getObj(id);
  373. if(CGI->mh)
  374. CGI->mh->hideObject(o, true);
  375. //notify interfaces about removal
  376. for(auto i=cl->playerint.begin(); i!=cl->playerint.end(); i++)
  377. {
  378. //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
  379. //TODO: loose requirements as next AI related crashes appear, for example another player collects object that got re-covered by FoW, unsure if AI code workarounds this
  380. if(GS(cl)->isVisible(o, i->first) || (!cl->getPlayerState(i->first)->human && o->ID == Obj::HERO && o->tempOwner != i->first))
  381. i->second->objectRemoved(o);
  382. }
  383. }
  384. void RemoveObject::applyCl(CClient *cl)
  385. {
  386. cl->invalidatePaths();
  387. }
  388. void TryMoveHero::applyFirstCl(CClient *cl)
  389. {
  390. CGHeroInstance *h = GS(cl)->getHero(id);
  391. //check if playerint will have the knowledge about movement - if not, directly update maphandler
  392. for(auto i=cl->playerint.begin(); i!=cl->playerint.end(); i++)
  393. {
  394. auto ps = GS(cl)->getPlayerState(i->first);
  395. if(ps && (GS(cl)->isVisible(start - int3(1, 0, 0), i->first) || GS(cl)->isVisible(end - int3(1, 0, 0), i->first)))
  396. {
  397. if(ps->human)
  398. humanKnows = true;
  399. }
  400. }
  401. if(!CGI->mh)
  402. return;
  403. if(result == TELEPORTATION || result == EMBARK || result == DISEMBARK || !humanKnows)
  404. CGI->mh->hideObject(h, result == EMBARK && humanKnows);
  405. if(result == DISEMBARK)
  406. CGI->mh->printObject(h->boat);
  407. }
  408. void TryMoveHero::applyCl(CClient *cl)
  409. {
  410. const CGHeroInstance *h = cl->getHero(id);
  411. cl->invalidatePaths();
  412. if(CGI->mh)
  413. {
  414. if(result == TELEPORTATION || result == EMBARK || result == DISEMBARK)
  415. CGI->mh->printObject(h, result == DISEMBARK);
  416. if(result == EMBARK)
  417. CGI->mh->hideObject(h->boat);
  418. }
  419. PlayerColor player = h->tempOwner;
  420. for(auto &i : cl->playerint)
  421. if(cl->getPlayerRelations(i.first, player) != PlayerRelations::ENEMIES)
  422. i.second->tileRevealed(fowRevealed);
  423. //notify interfaces about move
  424. auto gs = cl->gameState();
  425. for(auto i=cl->playerint.begin(); i!=cl->playerint.end(); i++)
  426. {
  427. if(i->first != PlayerColor::SPECTATOR && gs->checkForStandardLoss(i->first)) // Do not notify vanquished player's interface
  428. continue;
  429. if(GS(cl)->isVisible(start - int3(1, 0, 0), i->first)
  430. || GS(cl)->isVisible(end - int3(1, 0, 0), i->first))
  431. {
  432. // src and dst of enemy hero move may be not visible => 'verbose' should be false
  433. const bool verbose = cl->getPlayerRelations(i->first, player) != PlayerRelations::ENEMIES;
  434. i->second->heroMoved(*this, verbose);
  435. }
  436. }
  437. //maphandler didn't get update from playerint, do it now
  438. //TODO: restructure nicely
  439. if(!humanKnows && CGI->mh)
  440. CGI->mh->printObject(h);
  441. }
  442. bool TryMoveHero::stopMovement() const
  443. {
  444. return result != SUCCESS && result != EMBARK && result != DISEMBARK && result != TELEPORTATION;
  445. }
  446. void NewStructures::applyCl(CClient *cl)
  447. {
  448. CGTownInstance *town = GS(cl)->getTown(tid);
  449. for(const auto & id : bid)
  450. {
  451. callInterfaceIfPresent(cl, town->tempOwner, &IGameEventsReceiver::buildChanged, town, id, 1);
  452. }
  453. }
  454. void RazeStructures::applyCl (CClient *cl)
  455. {
  456. CGTownInstance *town = GS(cl)->getTown(tid);
  457. for(const auto & id : bid)
  458. {
  459. callInterfaceIfPresent(cl, town->tempOwner, &IGameEventsReceiver::buildChanged, town, id, 2);
  460. }
  461. }
  462. void SetAvailableCreatures::applyCl(CClient *cl)
  463. {
  464. const CGDwelling *dw = static_cast<const CGDwelling*>(cl->getObj(tid));
  465. //inform order about the change
  466. PlayerColor p;
  467. if(dw->ID == Obj::WAR_MACHINE_FACTORY) //War Machines Factory is not flaggable, it's "owned" by visitor
  468. p = cl->getTile(dw->visitablePos())->visitableObjects.back()->tempOwner;
  469. else
  470. p = dw->tempOwner;
  471. callInterfaceIfPresent(cl, p, &IGameEventsReceiver::availableCreaturesChanged, dw);
  472. }
  473. void SetHeroesInTown::applyCl(CClient *cl)
  474. {
  475. CGTownInstance *t = GS(cl)->getTown(tid);
  476. CGHeroInstance *hGarr = GS(cl)->getHero(this->garrison);
  477. CGHeroInstance *hVisit = GS(cl)->getHero(this->visiting);
  478. //inform all players that see this object
  479. for(auto i = cl->playerint.cbegin(); i != cl->playerint.cend(); ++i)
  480. {
  481. if(i->first >= PlayerColor::PLAYER_LIMIT)
  482. continue;
  483. if(GS(cl)->isVisible(t, i->first) ||
  484. (hGarr && GS(cl)->isVisible(hGarr, i->first)) ||
  485. (hVisit && GS(cl)->isVisible(hVisit, i->first)))
  486. {
  487. cl->playerint[i->first]->heroInGarrisonChange(t);
  488. }
  489. }
  490. }
  491. void HeroRecruited::applyCl(CClient *cl)
  492. {
  493. CGHeroInstance *h = GS(cl)->map->heroesOnMap.back();
  494. if(h->subID != hid)
  495. {
  496. logNetwork->error("Something wrong with hero recruited!");
  497. }
  498. bool needsPrinting = true;
  499. if(callInterfaceIfPresent(cl, h->tempOwner, &IGameEventsReceiver::heroCreated, h))
  500. {
  501. if(const CGTownInstance *t = GS(cl)->getTown(tid))
  502. {
  503. callInterfaceIfPresent(cl, h->tempOwner, &IGameEventsReceiver::heroInGarrisonChange, t);
  504. needsPrinting = false;
  505. }
  506. }
  507. if(needsPrinting && CGI->mh)
  508. CGI->mh->printObject(h);
  509. }
  510. void GiveHero::applyCl(CClient *cl)
  511. {
  512. CGHeroInstance *h = GS(cl)->getHero(id);
  513. if(CGI->mh)
  514. CGI->mh->printObject(h);
  515. callInterfaceIfPresent(cl, h->tempOwner, &IGameEventsReceiver::heroCreated, h);
  516. }
  517. void GiveHero::applyFirstCl(CClient *cl)
  518. {
  519. if(CGI->mh)
  520. CGI->mh->hideObject(GS(cl)->getHero(id));
  521. }
  522. void InfoWindow::applyCl(CClient *cl)
  523. {
  524. std::string str;
  525. text.toString(str);
  526. if(!callInterfaceIfPresent(cl, player, &CGameInterface::showInfoDialog, str,components,(soundBase::soundID)soundID))
  527. logNetwork->warn("We received InfoWindow for not our player...");
  528. }
  529. void SetObjectProperty::applyCl(CClient * cl)
  530. {
  531. //inform all players that see this object
  532. for(auto it = cl->playerint.cbegin(); it != cl->playerint.cend(); ++it)
  533. {
  534. if(GS(cl)->isVisible(GS(cl)->getObjInstance(id), it->first))
  535. callInterfaceIfPresent(cl, it->first, &IGameEventsReceiver::objectPropertyChanged, this);
  536. }
  537. }
  538. void HeroLevelUp::applyCl(CClient *cl)
  539. {
  540. const CGHeroInstance * hero = cl->getHero(heroId);
  541. assert(hero);
  542. callOnlyThatInterface(cl, player, &CGameInterface::heroGotLevel, hero, primskill, skills, queryID);
  543. }
  544. void CommanderLevelUp::applyCl(CClient *cl)
  545. {
  546. const CGHeroInstance * hero = cl->getHero(heroId);
  547. assert(hero);
  548. const CCommanderInstance * commander = hero->commander;
  549. assert(commander);
  550. assert(commander->armyObj); //is it possible for Commander to exist beyond armed instance?
  551. callOnlyThatInterface(cl, player, &CGameInterface::commanderGotLevel, commander, skills, queryID);
  552. }
  553. void BlockingDialog::applyCl(CClient *cl)
  554. {
  555. std::string str;
  556. text.toString(str);
  557. if(!callOnlyThatInterface(cl, player, &CGameInterface::showBlockingDialog, str, components, queryID, (soundBase::soundID)soundID, selection(), cancel()))
  558. logNetwork->warn("We received YesNoDialog for not our player...");
  559. }
  560. void GarrisonDialog::applyCl(CClient *cl)
  561. {
  562. const CGHeroInstance *h = cl->getHero(hid);
  563. const CArmedInstance *obj = static_cast<const CArmedInstance*>(cl->getObj(objid));
  564. callOnlyThatInterface(cl, h->getOwner(), &CGameInterface::showGarrisonDialog, obj, h, removableUnits, queryID);
  565. }
  566. void ExchangeDialog::applyCl(CClient *cl)
  567. {
  568. callInterfaceIfPresent(cl, player, &IGameEventsReceiver::heroExchangeStarted, hero1, hero2, queryID);
  569. }
  570. void TeleportDialog::applyCl(CClient *cl)
  571. {
  572. callOnlyThatInterface(cl, player, &CGameInterface::showTeleportDialog, channel, exits, impassable, queryID);
  573. }
  574. void MapObjectSelectDialog::applyCl(CClient * cl)
  575. {
  576. callOnlyThatInterface(cl, player, &CGameInterface::showMapObjectSelectDialog, queryID, icon, title, description, objects);
  577. }
  578. void BattleStart::applyFirstCl(CClient *cl)
  579. {
  580. // Cannot use the usual code because curB is not set yet
  581. callOnlyThatBattleInterface(cl, info->sides[0].color, &IBattleEventsReceiver::battleStartBefore, info->sides[0].armyObject, info->sides[1].armyObject,
  582. info->tile, info->sides[0].hero, info->sides[1].hero);
  583. callOnlyThatBattleInterface(cl, info->sides[1].color, &IBattleEventsReceiver::battleStartBefore, info->sides[0].armyObject, info->sides[1].armyObject,
  584. info->tile, info->sides[0].hero, info->sides[1].hero);
  585. callOnlyThatBattleInterface(cl, PlayerColor::SPECTATOR, &IBattleEventsReceiver::battleStartBefore, info->sides[0].armyObject, info->sides[1].armyObject,
  586. info->tile, info->sides[0].hero, info->sides[1].hero);
  587. }
  588. void BattleStart::applyCl(CClient *cl)
  589. {
  590. cl->battleStarted(info);
  591. }
  592. void BattleNextRound::applyFirstCl(CClient *cl)
  593. {
  594. callBattleInterfaceIfPresentForBothSides(cl, &IBattleEventsReceiver::battleNewRoundFirst, round);
  595. }
  596. void BattleNextRound::applyCl(CClient *cl)
  597. {
  598. callBattleInterfaceIfPresentForBothSides(cl, &IBattleEventsReceiver::battleNewRound, round);
  599. }
  600. void BattleSetActiveStack::applyCl(CClient *cl)
  601. {
  602. if(!askPlayerInterface)
  603. return;
  604. const CStack *activated = GS(cl)->curB->battleGetStackByID(stack);
  605. PlayerColor playerToCall; //player that will move activated stack
  606. if (activated->hasBonusOfType(Bonus::HYPNOTIZED))
  607. {
  608. playerToCall = (GS(cl)->curB->sides[0].color == activated->owner
  609. ? GS(cl)->curB->sides[1].color
  610. : GS(cl)->curB->sides[0].color);
  611. }
  612. else
  613. {
  614. playerToCall = activated->owner;
  615. }
  616. cl->startPlayerBattleAction(playerToCall);
  617. }
  618. void BattleLogMessage::applyCl(CClient * cl)
  619. {
  620. callBattleInterfaceIfPresentForBothSides(cl, &IBattleEventsReceiver::battleLogMessage, lines);
  621. }
  622. void BattleTriggerEffect::applyCl(CClient * cl)
  623. {
  624. callBattleInterfaceIfPresentForBothSides(cl, &IBattleEventsReceiver::battleTriggerEffect, *this);
  625. }
  626. void BattleUpdateGateState::applyFirstCl(CClient * cl)
  627. {
  628. callBattleInterfaceIfPresentForBothSides(cl, &IBattleEventsReceiver::battleGateStateChanged, state);
  629. }
  630. void BattleResult::applyFirstCl(CClient *cl)
  631. {
  632. callBattleInterfaceIfPresentForBothSides(cl, &IBattleEventsReceiver::battleEnd, this);
  633. cl->battleFinished();
  634. }
  635. void BattleStackMoved::applyFirstCl(CClient *cl)
  636. {
  637. const CStack * movedStack = GS(cl)->curB->battleGetStackByID(stack);
  638. callBattleInterfaceIfPresentForBothSides(cl, &IBattleEventsReceiver::battleStackMoved, movedStack, tilesToMove, distance, teleporting);
  639. }
  640. void BattleAttack::applyFirstCl(CClient *cl)
  641. {
  642. callBattleInterfaceIfPresentForBothSides(cl, &IBattleEventsReceiver::battleAttack, this);
  643. }
  644. void BattleAttack::applyCl(CClient *cl)
  645. {
  646. callBattleInterfaceIfPresentForBothSides(cl, &IBattleEventsReceiver::battleStacksAttacked, bsa, shot());
  647. }
  648. void StartAction::applyFirstCl(CClient *cl)
  649. {
  650. cl->curbaction = boost::make_optional(ba);
  651. callBattleInterfaceIfPresentForBothSides(cl, &IBattleEventsReceiver::actionStarted, ba);
  652. }
  653. void BattleSpellCast::applyCl(CClient *cl)
  654. {
  655. callBattleInterfaceIfPresentForBothSides(cl, &IBattleEventsReceiver::battleSpellCast, this);
  656. }
  657. void SetStackEffect::applyCl(CClient *cl)
  658. {
  659. //informing about effects
  660. callBattleInterfaceIfPresentForBothSides(cl, &IBattleEventsReceiver::battleStacksEffectsSet, *this);
  661. }
  662. void StacksInjured::applyCl(CClient *cl)
  663. {
  664. callBattleInterfaceIfPresentForBothSides(cl, &IBattleEventsReceiver::battleStacksAttacked, stacks, false);
  665. }
  666. void BattleResultsApplied::applyCl(CClient *cl)
  667. {
  668. callInterfaceIfPresent(cl, player1, &IGameEventsReceiver::battleResultsApplied);
  669. callInterfaceIfPresent(cl, player2, &IGameEventsReceiver::battleResultsApplied);
  670. callInterfaceIfPresent(cl, PlayerColor::SPECTATOR, &IGameEventsReceiver::battleResultsApplied);
  671. }
  672. void BattleUnitsChanged::applyCl(CClient * cl)
  673. {
  674. callBattleInterfaceIfPresentForBothSides(cl, &IBattleEventsReceiver::battleUnitsChanged, changedStacks);
  675. }
  676. void BattleObstaclesChanged::applyCl(CClient *cl)
  677. {
  678. //inform interfaces about removed obstacles
  679. callBattleInterfaceIfPresentForBothSides(cl, &IBattleEventsReceiver::battleObstaclesChanged, changes);
  680. }
  681. void CatapultAttack::applyCl(CClient *cl)
  682. {
  683. //inform interfaces about catapult attack
  684. callBattleInterfaceIfPresentForBothSides(cl, &IBattleEventsReceiver::battleCatapultAttacked, *this);
  685. }
  686. CGameState* CPackForClient::GS(CClient *cl)
  687. {
  688. return cl->gs;
  689. }
  690. void EndAction::applyCl(CClient *cl)
  691. {
  692. callBattleInterfaceIfPresentForBothSides(cl, &IBattleEventsReceiver::actionFinished, *cl->curbaction);
  693. cl->curbaction.reset();
  694. }
  695. void PackageApplied::applyCl(CClient *cl)
  696. {
  697. callInterfaceIfPresent(cl, player, &IGameEventsReceiver::requestRealized, this);
  698. if(!CClient::waitingRequest.tryRemovingElement(requestID))
  699. logNetwork->warn("Surprising server message! PackageApplied for unknown requestID!");
  700. }
  701. void SystemMessage::applyCl(CClient *cl)
  702. {
  703. std::ostringstream str;
  704. str << "System message: " << text;
  705. logNetwork->error(str.str()); // usually used to receive error messages from server
  706. if(LOCPLINT && !settings["session"]["hideSystemMessages"].Bool())
  707. LOCPLINT->cingconsole->print(str.str());
  708. }
  709. void PlayerBlocked::applyCl(CClient *cl)
  710. {
  711. callInterfaceIfPresent(cl, player, &IGameEventsReceiver::playerBlocked, reason, startOrEnd == BLOCKADE_STARTED);
  712. }
  713. void YourTurn::applyCl(CClient *cl)
  714. {
  715. logNetwork->debug("Server gives turn to %s", player.getStr());
  716. callAllInterfaces(cl, &IGameEventsReceiver::playerStartsTurn, player);
  717. callOnlyThatInterface(cl, player, &CGameInterface::yourTurn);
  718. }
  719. void SaveGameClient::applyCl(CClient *cl)
  720. {
  721. const auto stem = FileInfo::GetPathStem(fname);
  722. if(!CResourceHandler::get("local")->createResource(stem.to_string() + ".vcgm1"))
  723. {
  724. logNetwork->error("Failed to create resource %s", stem.to_string() + ".vcgm1");
  725. return;
  726. }
  727. try
  728. {
  729. CSaveFile save(*CResourceHandler::get()->getResourceName(ResourceID(stem.to_string(), EResType::CLIENT_SAVEGAME)));
  730. cl->saveCommonState(save);
  731. save << *cl;
  732. }
  733. catch(std::exception &e)
  734. {
  735. logNetwork->error("Failed to save game:%s", e.what());
  736. }
  737. }
  738. void PlayerMessageClient::applyCl(CClient *cl)
  739. {
  740. logNetwork->debug("Player %s sends a message: %s", player.getStr(), text);
  741. std::ostringstream str;
  742. if(player.isSpectator())
  743. str << "Spectator: " << text;
  744. else
  745. str << cl->getPlayerState(player)->nodeName() <<": " << text;
  746. if(LOCPLINT)
  747. LOCPLINT->cingconsole->print(str.str());
  748. }
  749. void ShowInInfobox::applyCl(CClient *cl)
  750. {
  751. callInterfaceIfPresent(cl, player, &IGameEventsReceiver::showComp, c, text.toString());
  752. }
  753. void AdvmapSpellCast::applyCl(CClient *cl)
  754. {
  755. cl->invalidatePaths();
  756. auto caster = cl->getHero(casterID);
  757. if(caster)
  758. //consider notifying other interfaces that see hero?
  759. callInterfaceIfPresent(cl, caster->getOwner(), &IGameEventsReceiver::advmapSpellCast, caster, spellID);
  760. else
  761. logNetwork->error("Invalid hero instance");
  762. }
  763. void ShowWorldViewEx::applyCl(CClient * cl)
  764. {
  765. callOnlyThatInterface(cl, player, &CGameInterface::showWorldViewEx, objectPositions);
  766. }
  767. void OpenWindow::applyCl(CClient *cl)
  768. {
  769. switch(window)
  770. {
  771. case RECRUITMENT_FIRST:
  772. case RECRUITMENT_ALL:
  773. {
  774. const CGDwelling *dw = dynamic_cast<const CGDwelling*>(cl->getObj(ObjectInstanceID(id1)));
  775. const CArmedInstance *dst = dynamic_cast<const CArmedInstance*>(cl->getObj(ObjectInstanceID(id2)));
  776. callInterfaceIfPresent(cl, dst->tempOwner, &IGameEventsReceiver::showRecruitmentDialog, dw, dst, window == RECRUITMENT_FIRST ? 0 : -1);
  777. }
  778. break;
  779. case SHIPYARD_WINDOW:
  780. {
  781. const IShipyard *sy = IShipyard::castFrom(cl->getObj(ObjectInstanceID(id1)));
  782. callInterfaceIfPresent(cl, sy->o->tempOwner, &IGameEventsReceiver::showShipyardDialog, sy);
  783. }
  784. break;
  785. case THIEVES_GUILD:
  786. {
  787. //displays Thieves' Guild window (when hero enters Den of Thieves)
  788. const CGObjectInstance *obj = cl->getObj(ObjectInstanceID(id2));
  789. callInterfaceIfPresent(cl, PlayerColor(id1), &IGameEventsReceiver::showThievesGuildWindow, obj);
  790. }
  791. break;
  792. case UNIVERSITY_WINDOW:
  793. {
  794. //displays University window (when hero enters University on adventure map)
  795. const IMarket *market = IMarket::castFrom(cl->getObj(ObjectInstanceID(id1)));
  796. const CGHeroInstance *hero = cl->getHero(ObjectInstanceID(id2));
  797. callInterfaceIfPresent(cl, hero->tempOwner, &IGameEventsReceiver::showUniversityWindow, market, hero);
  798. }
  799. break;
  800. case MARKET_WINDOW:
  801. {
  802. //displays Thieves' Guild window (when hero enters Den of Thieves)
  803. const CGObjectInstance *obj = cl->getObj(ObjectInstanceID(id1));
  804. const CGHeroInstance *hero = cl->getHero(ObjectInstanceID(id2));
  805. const IMarket *market = IMarket::castFrom(obj);
  806. callInterfaceIfPresent(cl, cl->getTile(obj->visitablePos())->visitableObjects.back()->tempOwner, &IGameEventsReceiver::showMarketWindow, market, hero);
  807. }
  808. break;
  809. case HILL_FORT_WINDOW:
  810. {
  811. //displays Hill fort window
  812. const CGObjectInstance *obj = cl->getObj(ObjectInstanceID(id1));
  813. const CGHeroInstance *hero = cl->getHero(ObjectInstanceID(id2));
  814. callInterfaceIfPresent(cl, cl->getTile(obj->visitablePos())->visitableObjects.back()->tempOwner, &IGameEventsReceiver::showHillFortWindow, obj, hero);
  815. }
  816. break;
  817. case PUZZLE_MAP:
  818. {
  819. callInterfaceIfPresent(cl, PlayerColor(id1), &IGameEventsReceiver::showPuzzleMap);
  820. }
  821. break;
  822. case TAVERN_WINDOW:
  823. const CGObjectInstance *obj1 = cl->getObj(ObjectInstanceID(id1)),
  824. *obj2 = cl->getObj(ObjectInstanceID(id2));
  825. callInterfaceIfPresent(cl, obj1->tempOwner, &IGameEventsReceiver::showTavernWindow, obj2);
  826. break;
  827. }
  828. }
  829. void CenterView::applyCl(CClient *cl)
  830. {
  831. callInterfaceIfPresent(cl, player, &IGameEventsReceiver::centerView, pos, focusTime);
  832. }
  833. void NewObject::applyCl(CClient *cl)
  834. {
  835. cl->invalidatePaths();
  836. const CGObjectInstance *obj = cl->getObj(id);
  837. if(CGI->mh)
  838. CGI->mh->printObject(obj, true);
  839. for(auto i=cl->playerint.begin(); i!=cl->playerint.end(); i++)
  840. {
  841. if(GS(cl)->isVisible(obj, i->first))
  842. i->second->newObject(obj);
  843. }
  844. }
  845. void SetAvailableArtifacts::applyCl(CClient *cl)
  846. {
  847. if(id < 0) //artifact merchants globally
  848. {
  849. callAllInterfaces(cl, &IGameEventsReceiver::availableArtifactsChanged, nullptr);
  850. }
  851. else
  852. {
  853. const CGBlackMarket *bm = dynamic_cast<const CGBlackMarket *>(cl->getObj(ObjectInstanceID(id)));
  854. assert(bm);
  855. callInterfaceIfPresent(cl, cl->getTile(bm->visitablePos())->visitableObjects.back()->tempOwner, &IGameEventsReceiver::availableArtifactsChanged, bm);
  856. }
  857. }
  858. void EntitiesChanged::applyCl(CClient *cl)
  859. {
  860. cl->invalidatePaths();
  861. }