NetPacksClient.cpp 30 KB

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