NetPacksClient.cpp 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001
  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. callInterfaceIfPresent(cl, srcLoc.owningPlayer(), &IGameEventsReceiver::artifactMoved, srcLoc, dstLoc);
  261. if(srcLoc.owningPlayer() != dstLoc.owningPlayer())
  262. callInterfaceIfPresent(cl, dstLoc.owningPlayer(), &IGameEventsReceiver::artifactMoved, srcLoc, dstLoc);
  263. }
  264. };
  265. applyMove(artsPack0);
  266. if(swap)
  267. applyMove(artsPack1);
  268. }
  269. void AssembledArtifact::applyCl(CClient *cl)
  270. {
  271. callInterfaceIfPresent(cl, al.owningPlayer(), &IGameEventsReceiver::artifactAssembled, al);
  272. }
  273. void DisassembledArtifact::applyCl(CClient *cl)
  274. {
  275. callInterfaceIfPresent(cl, al.owningPlayer(), &IGameEventsReceiver::artifactDisassembled, al);
  276. }
  277. void HeroVisit::applyCl(CClient * cl)
  278. {
  279. auto hero = cl->getHero(heroId);
  280. auto obj = cl->getObj(objId, false);
  281. callInterfaceIfPresent(cl, player, &IGameEventsReceiver::heroVisit, hero, obj, starting);
  282. }
  283. void NewTurn::applyCl(CClient *cl)
  284. {
  285. cl->invalidatePaths();
  286. }
  287. void GiveBonus::applyCl(CClient *cl)
  288. {
  289. cl->invalidatePaths();
  290. switch(who)
  291. {
  292. case HERO:
  293. {
  294. const CGHeroInstance *h = GS(cl)->getHero(ObjectInstanceID(id));
  295. callInterfaceIfPresent(cl, h->tempOwner, &IGameEventsReceiver::heroBonusChanged, h, *h->getBonusList().back(), true);
  296. }
  297. break;
  298. case PLAYER:
  299. {
  300. const PlayerState *p = GS(cl)->getPlayerState(PlayerColor(id));
  301. callInterfaceIfPresent(cl, PlayerColor(id), &IGameEventsReceiver::playerBonusChanged, *p->getBonusList().back(), true);
  302. }
  303. break;
  304. }
  305. }
  306. void ChangeObjPos::applyFirstCl(CClient *cl)
  307. {
  308. CGObjectInstance *obj = GS(cl)->getObjInstance(objid);
  309. if(flags & 1 && CGI->mh)
  310. CGI->mh->hideObject(obj);
  311. }
  312. void ChangeObjPos::applyCl(CClient *cl)
  313. {
  314. CGObjectInstance *obj = GS(cl)->getObjInstance(objid);
  315. if(flags & 1 && CGI->mh)
  316. CGI->mh->printObject(obj);
  317. cl->invalidatePaths();
  318. }
  319. void PlayerEndsGame::applyCl(CClient *cl)
  320. {
  321. callAllInterfaces(cl, &IGameEventsReceiver::gameOver, player, victoryLossCheckResult);
  322. // In auto testing mode we always close client if red player won or lose
  323. if(!settings["session"]["testmap"].isNull() && player == PlayerColor(0))
  324. handleQuit(settings["session"]["spectate"].Bool()); // if spectator is active ask to close client or not
  325. }
  326. void PlayerReinitInterface::applyCl(CClient * cl)
  327. {
  328. auto initInterfaces = [cl]()
  329. {
  330. cl->initPlayerInterfaces();
  331. auto currentPlayer = cl->gameState()->currentPlayer;
  332. callAllInterfaces(cl, &IGameEventsReceiver::playerStartsTurn, currentPlayer);
  333. callOnlyThatInterface(cl, currentPlayer, &CGameInterface::yourTurn);
  334. };
  335. for(auto player : players)
  336. {
  337. auto & plSettings = CSH->si->getIthPlayersSettings(player);
  338. if(playerConnectionId == PlayerSettings::PLAYER_AI)
  339. {
  340. plSettings.connectedPlayerIDs.clear();
  341. cl->initPlayerEnvironments();
  342. initInterfaces();
  343. }
  344. else if(playerConnectionId == CSH->c->connectionID)
  345. {
  346. plSettings.connectedPlayerIDs.insert(playerConnectionId);
  347. cl->playerint.clear();
  348. initInterfaces();
  349. }
  350. }
  351. }
  352. void RemoveBonus::applyCl(CClient *cl)
  353. {
  354. cl->invalidatePaths();
  355. switch(who)
  356. {
  357. case HERO:
  358. {
  359. const CGHeroInstance *h = GS(cl)->getHero(ObjectInstanceID(id));
  360. callInterfaceIfPresent(cl, h->tempOwner, &IGameEventsReceiver::heroBonusChanged, h, bonus, false);
  361. }
  362. break;
  363. case PLAYER:
  364. {
  365. //const PlayerState *p = GS(cl)->getPlayerState(id);
  366. callInterfaceIfPresent(cl, PlayerColor(id), &IGameEventsReceiver::playerBonusChanged, bonus, false);
  367. }
  368. break;
  369. }
  370. }
  371. void RemoveObject::applyFirstCl(CClient *cl)
  372. {
  373. const CGObjectInstance *o = cl->getObj(id);
  374. if(CGI->mh)
  375. CGI->mh->hideObject(o, true);
  376. //notify interfaces about removal
  377. for(auto i=cl->playerint.begin(); i!=cl->playerint.end(); i++)
  378. {
  379. //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
  380. //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
  381. if(GS(cl)->isVisible(o, i->first) || (!cl->getPlayerState(i->first)->human && o->ID == Obj::HERO && o->tempOwner != i->first))
  382. i->second->objectRemoved(o);
  383. }
  384. }
  385. void RemoveObject::applyCl(CClient *cl)
  386. {
  387. cl->invalidatePaths();
  388. }
  389. void TryMoveHero::applyFirstCl(CClient *cl)
  390. {
  391. CGHeroInstance *h = GS(cl)->getHero(id);
  392. //check if playerint will have the knowledge about movement - if not, directly update maphandler
  393. for(auto i=cl->playerint.begin(); i!=cl->playerint.end(); i++)
  394. {
  395. auto ps = GS(cl)->getPlayerState(i->first);
  396. if(ps && (GS(cl)->isVisible(start - int3(1, 0, 0), i->first) || GS(cl)->isVisible(end - int3(1, 0, 0), i->first)))
  397. {
  398. if(ps->human)
  399. humanKnows = true;
  400. }
  401. }
  402. if(!CGI->mh)
  403. return;
  404. if(result == TELEPORTATION || result == EMBARK || result == DISEMBARK || !humanKnows)
  405. CGI->mh->hideObject(h, result == EMBARK && humanKnows);
  406. if(result == DISEMBARK)
  407. CGI->mh->printObject(h->boat);
  408. }
  409. void TryMoveHero::applyCl(CClient *cl)
  410. {
  411. const CGHeroInstance *h = cl->getHero(id);
  412. cl->invalidatePaths();
  413. if(CGI->mh)
  414. {
  415. if(result == TELEPORTATION || result == EMBARK || result == DISEMBARK)
  416. CGI->mh->printObject(h, result == DISEMBARK);
  417. if(result == EMBARK)
  418. CGI->mh->hideObject(h->boat);
  419. }
  420. PlayerColor player = h->tempOwner;
  421. for(auto &i : cl->playerint)
  422. if(cl->getPlayerRelations(i.first, player) != PlayerRelations::ENEMIES)
  423. i.second->tileRevealed(fowRevealed);
  424. //notify interfaces about move
  425. auto gs = cl->gameState();
  426. for(auto i=cl->playerint.begin(); i!=cl->playerint.end(); i++)
  427. {
  428. if(i->first != PlayerColor::SPECTATOR && gs->checkForStandardLoss(i->first)) // Do not notify vanquished player's interface
  429. continue;
  430. if(GS(cl)->isVisible(start - int3(1, 0, 0), i->first)
  431. || GS(cl)->isVisible(end - int3(1, 0, 0), i->first))
  432. {
  433. // src and dst of enemy hero move may be not visible => 'verbose' should be false
  434. const bool verbose = cl->getPlayerRelations(i->first, player) != PlayerRelations::ENEMIES;
  435. i->second->heroMoved(*this, verbose);
  436. }
  437. }
  438. //maphandler didn't get update from playerint, do it now
  439. //TODO: restructure nicely
  440. if(!humanKnows && CGI->mh)
  441. CGI->mh->printObject(h);
  442. }
  443. bool TryMoveHero::stopMovement() const
  444. {
  445. return result != SUCCESS && result != EMBARK && result != DISEMBARK && result != TELEPORTATION;
  446. }
  447. void NewStructures::applyCl(CClient *cl)
  448. {
  449. CGTownInstance *town = GS(cl)->getTown(tid);
  450. for(const auto & id : bid)
  451. {
  452. callInterfaceIfPresent(cl, town->tempOwner, &IGameEventsReceiver::buildChanged, town, id, 1);
  453. }
  454. }
  455. void RazeStructures::applyCl (CClient *cl)
  456. {
  457. CGTownInstance *town = GS(cl)->getTown(tid);
  458. for(const auto & id : bid)
  459. {
  460. callInterfaceIfPresent(cl, town->tempOwner, &IGameEventsReceiver::buildChanged, town, id, 2);
  461. }
  462. }
  463. void SetAvailableCreatures::applyCl(CClient *cl)
  464. {
  465. const CGDwelling *dw = static_cast<const CGDwelling*>(cl->getObj(tid));
  466. //inform order about the change
  467. PlayerColor p;
  468. if(dw->ID == Obj::WAR_MACHINE_FACTORY) //War Machines Factory is not flaggable, it's "owned" by visitor
  469. p = cl->getTile(dw->visitablePos())->visitableObjects.back()->tempOwner;
  470. else
  471. p = dw->tempOwner;
  472. callInterfaceIfPresent(cl, p, &IGameEventsReceiver::availableCreaturesChanged, dw);
  473. }
  474. void SetHeroesInTown::applyCl(CClient *cl)
  475. {
  476. CGTownInstance *t = GS(cl)->getTown(tid);
  477. CGHeroInstance *hGarr = GS(cl)->getHero(this->garrison);
  478. CGHeroInstance *hVisit = GS(cl)->getHero(this->visiting);
  479. //inform all players that see this object
  480. for(auto i = cl->playerint.cbegin(); i != cl->playerint.cend(); ++i)
  481. {
  482. if(i->first >= PlayerColor::PLAYER_LIMIT)
  483. continue;
  484. if(GS(cl)->isVisible(t, i->first) ||
  485. (hGarr && GS(cl)->isVisible(hGarr, i->first)) ||
  486. (hVisit && GS(cl)->isVisible(hVisit, i->first)))
  487. {
  488. cl->playerint[i->first]->heroInGarrisonChange(t);
  489. }
  490. }
  491. }
  492. void HeroRecruited::applyCl(CClient *cl)
  493. {
  494. CGHeroInstance *h = GS(cl)->map->heroesOnMap.back();
  495. if(h->subID != hid)
  496. {
  497. logNetwork->error("Something wrong with hero recruited!");
  498. }
  499. bool needsPrinting = true;
  500. if(callInterfaceIfPresent(cl, h->tempOwner, &IGameEventsReceiver::heroCreated, h))
  501. {
  502. if(const CGTownInstance *t = GS(cl)->getTown(tid))
  503. {
  504. callInterfaceIfPresent(cl, h->tempOwner, &IGameEventsReceiver::heroInGarrisonChange, t);
  505. needsPrinting = false;
  506. }
  507. }
  508. if(needsPrinting && CGI->mh)
  509. CGI->mh->printObject(h);
  510. }
  511. void GiveHero::applyCl(CClient *cl)
  512. {
  513. CGHeroInstance *h = GS(cl)->getHero(id);
  514. if(CGI->mh)
  515. CGI->mh->printObject(h);
  516. callInterfaceIfPresent(cl, h->tempOwner, &IGameEventsReceiver::heroCreated, h);
  517. }
  518. void GiveHero::applyFirstCl(CClient *cl)
  519. {
  520. if(CGI->mh)
  521. CGI->mh->hideObject(GS(cl)->getHero(id));
  522. }
  523. void InfoWindow::applyCl(CClient *cl)
  524. {
  525. std::string str;
  526. text.toString(str);
  527. if(!callInterfaceIfPresent(cl, player, &CGameInterface::showInfoDialog, str,components,(soundBase::soundID)soundID))
  528. logNetwork->warn("We received InfoWindow for not our player...");
  529. }
  530. void SetObjectProperty::applyCl(CClient * cl)
  531. {
  532. //inform all players that see this object
  533. for(auto it = cl->playerint.cbegin(); it != cl->playerint.cend(); ++it)
  534. {
  535. if(GS(cl)->isVisible(GS(cl)->getObjInstance(id), it->first))
  536. callInterfaceIfPresent(cl, it->first, &IGameEventsReceiver::objectPropertyChanged, this);
  537. }
  538. }
  539. void HeroLevelUp::applyCl(CClient *cl)
  540. {
  541. const CGHeroInstance * hero = cl->getHero(heroId);
  542. assert(hero);
  543. callOnlyThatInterface(cl, player, &CGameInterface::heroGotLevel, hero, primskill, skills, queryID);
  544. }
  545. void CommanderLevelUp::applyCl(CClient *cl)
  546. {
  547. const CGHeroInstance * hero = cl->getHero(heroId);
  548. assert(hero);
  549. const CCommanderInstance * commander = hero->commander;
  550. assert(commander);
  551. assert(commander->armyObj); //is it possible for Commander to exist beyond armed instance?
  552. callOnlyThatInterface(cl, player, &CGameInterface::commanderGotLevel, commander, skills, queryID);
  553. }
  554. void BlockingDialog::applyCl(CClient *cl)
  555. {
  556. std::string str;
  557. text.toString(str);
  558. if(!callOnlyThatInterface(cl, player, &CGameInterface::showBlockingDialog, str, components, queryID, (soundBase::soundID)soundID, selection(), cancel()))
  559. logNetwork->warn("We received YesNoDialog for not our player...");
  560. }
  561. void GarrisonDialog::applyCl(CClient *cl)
  562. {
  563. const CGHeroInstance *h = cl->getHero(hid);
  564. const CArmedInstance *obj = static_cast<const CArmedInstance*>(cl->getObj(objid));
  565. callOnlyThatInterface(cl, h->getOwner(), &CGameInterface::showGarrisonDialog, obj, h, removableUnits, queryID);
  566. }
  567. void ExchangeDialog::applyCl(CClient *cl)
  568. {
  569. callInterfaceIfPresent(cl, player, &IGameEventsReceiver::heroExchangeStarted, hero1, hero2, queryID);
  570. }
  571. void TeleportDialog::applyCl(CClient *cl)
  572. {
  573. callOnlyThatInterface(cl, player, &CGameInterface::showTeleportDialog, channel, exits, impassable, queryID);
  574. }
  575. void MapObjectSelectDialog::applyCl(CClient * cl)
  576. {
  577. callOnlyThatInterface(cl, player, &CGameInterface::showMapObjectSelectDialog, queryID, icon, title, description, objects);
  578. }
  579. void BattleStart::applyFirstCl(CClient *cl)
  580. {
  581. // Cannot use the usual code because curB is not set yet
  582. callOnlyThatBattleInterface(cl, info->sides[0].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, info->sides[1].color, &IBattleEventsReceiver::battleStartBefore, info->sides[0].armyObject, info->sides[1].armyObject,
  585. info->tile, info->sides[0].hero, info->sides[1].hero);
  586. callOnlyThatBattleInterface(cl, PlayerColor::SPECTATOR, &IBattleEventsReceiver::battleStartBefore, info->sides[0].armyObject, info->sides[1].armyObject,
  587. info->tile, info->sides[0].hero, info->sides[1].hero);
  588. }
  589. void BattleStart::applyCl(CClient *cl)
  590. {
  591. cl->battleStarted(info);
  592. }
  593. void BattleNextRound::applyFirstCl(CClient *cl)
  594. {
  595. callBattleInterfaceIfPresentForBothSides(cl, &IBattleEventsReceiver::battleNewRoundFirst, round);
  596. }
  597. void BattleNextRound::applyCl(CClient *cl)
  598. {
  599. callBattleInterfaceIfPresentForBothSides(cl, &IBattleEventsReceiver::battleNewRound, round);
  600. }
  601. void BattleSetActiveStack::applyCl(CClient *cl)
  602. {
  603. if(!askPlayerInterface)
  604. return;
  605. const CStack *activated = GS(cl)->curB->battleGetStackByID(stack);
  606. PlayerColor playerToCall; //player that will move activated stack
  607. if (activated->hasBonusOfType(Bonus::HYPNOTIZED))
  608. {
  609. playerToCall = (GS(cl)->curB->sides[0].color == activated->owner
  610. ? GS(cl)->curB->sides[1].color
  611. : GS(cl)->curB->sides[0].color);
  612. }
  613. else
  614. {
  615. playerToCall = activated->owner;
  616. }
  617. cl->startPlayerBattleAction(playerToCall);
  618. }
  619. void BattleLogMessage::applyCl(CClient * cl)
  620. {
  621. callBattleInterfaceIfPresentForBothSides(cl, &IBattleEventsReceiver::battleLogMessage, lines);
  622. }
  623. void BattleTriggerEffect::applyCl(CClient * cl)
  624. {
  625. callBattleInterfaceIfPresentForBothSides(cl, &IBattleEventsReceiver::battleTriggerEffect, *this);
  626. }
  627. void BattleUpdateGateState::applyFirstCl(CClient * cl)
  628. {
  629. callBattleInterfaceIfPresentForBothSides(cl, &IBattleEventsReceiver::battleGateStateChanged, state);
  630. }
  631. void BattleResult::applyFirstCl(CClient *cl)
  632. {
  633. callBattleInterfaceIfPresentForBothSides(cl, &IBattleEventsReceiver::battleEnd, this);
  634. cl->battleFinished();
  635. }
  636. void BattleStackMoved::applyFirstCl(CClient *cl)
  637. {
  638. const CStack * movedStack = GS(cl)->curB->battleGetStackByID(stack);
  639. callBattleInterfaceIfPresentForBothSides(cl, &IBattleEventsReceiver::battleStackMoved, movedStack, tilesToMove, distance, teleporting);
  640. }
  641. void BattleAttack::applyFirstCl(CClient *cl)
  642. {
  643. callBattleInterfaceIfPresentForBothSides(cl, &IBattleEventsReceiver::battleAttack, this);
  644. }
  645. void BattleAttack::applyCl(CClient *cl)
  646. {
  647. callBattleInterfaceIfPresentForBothSides(cl, &IBattleEventsReceiver::battleStacksAttacked, bsa, shot());
  648. }
  649. void StartAction::applyFirstCl(CClient *cl)
  650. {
  651. cl->curbaction = boost::make_optional(ba);
  652. callBattleInterfaceIfPresentForBothSides(cl, &IBattleEventsReceiver::actionStarted, ba);
  653. }
  654. void BattleSpellCast::applyCl(CClient *cl)
  655. {
  656. callBattleInterfaceIfPresentForBothSides(cl, &IBattleEventsReceiver::battleSpellCast, this);
  657. }
  658. void SetStackEffect::applyCl(CClient *cl)
  659. {
  660. //informing about effects
  661. callBattleInterfaceIfPresentForBothSides(cl, &IBattleEventsReceiver::battleStacksEffectsSet, *this);
  662. }
  663. void StacksInjured::applyCl(CClient *cl)
  664. {
  665. callBattleInterfaceIfPresentForBothSides(cl, &IBattleEventsReceiver::battleStacksAttacked, stacks, false);
  666. }
  667. void BattleResultsApplied::applyCl(CClient *cl)
  668. {
  669. callInterfaceIfPresent(cl, player1, &IGameEventsReceiver::battleResultsApplied);
  670. callInterfaceIfPresent(cl, player2, &IGameEventsReceiver::battleResultsApplied);
  671. callInterfaceIfPresent(cl, PlayerColor::SPECTATOR, &IGameEventsReceiver::battleResultsApplied);
  672. }
  673. void BattleUnitsChanged::applyCl(CClient * cl)
  674. {
  675. callBattleInterfaceIfPresentForBothSides(cl, &IBattleEventsReceiver::battleUnitsChanged, changedStacks);
  676. }
  677. void BattleObstaclesChanged::applyCl(CClient *cl)
  678. {
  679. //inform interfaces about removed obstacles
  680. callBattleInterfaceIfPresentForBothSides(cl, &IBattleEventsReceiver::battleObstaclesChanged, changes);
  681. }
  682. void CatapultAttack::applyCl(CClient *cl)
  683. {
  684. //inform interfaces about catapult attack
  685. callBattleInterfaceIfPresentForBothSides(cl, &IBattleEventsReceiver::battleCatapultAttacked, *this);
  686. }
  687. CGameState* CPackForClient::GS(CClient *cl)
  688. {
  689. return cl->gs;
  690. }
  691. void EndAction::applyCl(CClient *cl)
  692. {
  693. callBattleInterfaceIfPresentForBothSides(cl, &IBattleEventsReceiver::actionFinished, *cl->curbaction);
  694. cl->curbaction.reset();
  695. }
  696. void PackageApplied::applyCl(CClient *cl)
  697. {
  698. callInterfaceIfPresent(cl, player, &IGameEventsReceiver::requestRealized, this);
  699. if(!CClient::waitingRequest.tryRemovingElement(requestID))
  700. logNetwork->warn("Surprising server message! PackageApplied for unknown requestID!");
  701. }
  702. void SystemMessage::applyCl(CClient *cl)
  703. {
  704. std::ostringstream str;
  705. str << "System message: " << text;
  706. logNetwork->error(str.str()); // usually used to receive error messages from server
  707. if(LOCPLINT && !settings["session"]["hideSystemMessages"].Bool())
  708. LOCPLINT->cingconsole->print(str.str());
  709. }
  710. void PlayerBlocked::applyCl(CClient *cl)
  711. {
  712. callInterfaceIfPresent(cl, player, &IGameEventsReceiver::playerBlocked, reason, startOrEnd == BLOCKADE_STARTED);
  713. }
  714. void YourTurn::applyCl(CClient *cl)
  715. {
  716. logNetwork->debug("Server gives turn to %s", player.getStr());
  717. callAllInterfaces(cl, &IGameEventsReceiver::playerStartsTurn, player);
  718. callOnlyThatInterface(cl, player, &CGameInterface::yourTurn);
  719. }
  720. void SaveGameClient::applyCl(CClient *cl)
  721. {
  722. const auto stem = FileInfo::GetPathStem(fname);
  723. if(!CResourceHandler::get("local")->createResource(stem.to_string() + ".vcgm1"))
  724. {
  725. logNetwork->error("Failed to create resource %s", stem.to_string() + ".vcgm1");
  726. return;
  727. }
  728. try
  729. {
  730. CSaveFile save(*CResourceHandler::get()->getResourceName(ResourceID(stem.to_string(), EResType::CLIENT_SAVEGAME)));
  731. cl->saveCommonState(save);
  732. save << *cl;
  733. }
  734. catch(std::exception &e)
  735. {
  736. logNetwork->error("Failed to save game:%s", e.what());
  737. }
  738. }
  739. void PlayerMessageClient::applyCl(CClient *cl)
  740. {
  741. logNetwork->debug("Player %s sends a message: %s", player.getStr(), text);
  742. std::ostringstream str;
  743. if(player.isSpectator())
  744. str << "Spectator: " << text;
  745. else
  746. str << cl->getPlayerState(player)->nodeName() <<": " << text;
  747. if(LOCPLINT)
  748. LOCPLINT->cingconsole->print(str.str());
  749. }
  750. void ShowInInfobox::applyCl(CClient *cl)
  751. {
  752. callInterfaceIfPresent(cl, player, &IGameEventsReceiver::showComp, c, text.toString());
  753. }
  754. void AdvmapSpellCast::applyCl(CClient *cl)
  755. {
  756. cl->invalidatePaths();
  757. auto caster = cl->getHero(casterID);
  758. if(caster)
  759. //consider notifying other interfaces that see hero?
  760. callInterfaceIfPresent(cl, caster->getOwner(), &IGameEventsReceiver::advmapSpellCast, caster, spellID);
  761. else
  762. logNetwork->error("Invalid hero instance");
  763. }
  764. void ShowWorldViewEx::applyCl(CClient * cl)
  765. {
  766. callOnlyThatInterface(cl, player, &CGameInterface::showWorldViewEx, objectPositions);
  767. }
  768. void OpenWindow::applyCl(CClient *cl)
  769. {
  770. switch(window)
  771. {
  772. case RECRUITMENT_FIRST:
  773. case RECRUITMENT_ALL:
  774. {
  775. const CGDwelling *dw = dynamic_cast<const CGDwelling*>(cl->getObj(ObjectInstanceID(id1)));
  776. const CArmedInstance *dst = dynamic_cast<const CArmedInstance*>(cl->getObj(ObjectInstanceID(id2)));
  777. callInterfaceIfPresent(cl, dst->tempOwner, &IGameEventsReceiver::showRecruitmentDialog, dw, dst, window == RECRUITMENT_FIRST ? 0 : -1);
  778. }
  779. break;
  780. case SHIPYARD_WINDOW:
  781. {
  782. const IShipyard *sy = IShipyard::castFrom(cl->getObj(ObjectInstanceID(id1)));
  783. callInterfaceIfPresent(cl, sy->o->tempOwner, &IGameEventsReceiver::showShipyardDialog, sy);
  784. }
  785. break;
  786. case THIEVES_GUILD:
  787. {
  788. //displays Thieves' Guild window (when hero enters Den of Thieves)
  789. const CGObjectInstance *obj = cl->getObj(ObjectInstanceID(id2));
  790. callInterfaceIfPresent(cl, PlayerColor(id1), &IGameEventsReceiver::showThievesGuildWindow, obj);
  791. }
  792. break;
  793. case UNIVERSITY_WINDOW:
  794. {
  795. //displays University window (when hero enters University on adventure map)
  796. const IMarket *market = IMarket::castFrom(cl->getObj(ObjectInstanceID(id1)));
  797. const CGHeroInstance *hero = cl->getHero(ObjectInstanceID(id2));
  798. callInterfaceIfPresent(cl, hero->tempOwner, &IGameEventsReceiver::showUniversityWindow, market, hero);
  799. }
  800. break;
  801. case MARKET_WINDOW:
  802. {
  803. //displays Thieves' Guild window (when hero enters Den of Thieves)
  804. const CGObjectInstance *obj = cl->getObj(ObjectInstanceID(id1));
  805. const CGHeroInstance *hero = cl->getHero(ObjectInstanceID(id2));
  806. const IMarket *market = IMarket::castFrom(obj);
  807. callInterfaceIfPresent(cl, cl->getTile(obj->visitablePos())->visitableObjects.back()->tempOwner, &IGameEventsReceiver::showMarketWindow, market, hero);
  808. }
  809. break;
  810. case HILL_FORT_WINDOW:
  811. {
  812. //displays Hill fort window
  813. const CGObjectInstance *obj = cl->getObj(ObjectInstanceID(id1));
  814. const CGHeroInstance *hero = cl->getHero(ObjectInstanceID(id2));
  815. callInterfaceIfPresent(cl, cl->getTile(obj->visitablePos())->visitableObjects.back()->tempOwner, &IGameEventsReceiver::showHillFortWindow, obj, hero);
  816. }
  817. break;
  818. case PUZZLE_MAP:
  819. {
  820. callInterfaceIfPresent(cl, PlayerColor(id1), &IGameEventsReceiver::showPuzzleMap);
  821. }
  822. break;
  823. case TAVERN_WINDOW:
  824. const CGObjectInstance *obj1 = cl->getObj(ObjectInstanceID(id1)),
  825. *obj2 = cl->getObj(ObjectInstanceID(id2));
  826. callInterfaceIfPresent(cl, obj1->tempOwner, &IGameEventsReceiver::showTavernWindow, obj2);
  827. break;
  828. }
  829. }
  830. void CenterView::applyCl(CClient *cl)
  831. {
  832. callInterfaceIfPresent(cl, player, &IGameEventsReceiver::centerView, pos, focusTime);
  833. }
  834. void NewObject::applyCl(CClient *cl)
  835. {
  836. cl->invalidatePaths();
  837. const CGObjectInstance *obj = cl->getObj(id);
  838. if(CGI->mh)
  839. CGI->mh->printObject(obj, true);
  840. for(auto i=cl->playerint.begin(); i!=cl->playerint.end(); i++)
  841. {
  842. if(GS(cl)->isVisible(obj, i->first))
  843. i->second->newObject(obj);
  844. }
  845. }
  846. void SetAvailableArtifacts::applyCl(CClient *cl)
  847. {
  848. if(id < 0) //artifact merchants globally
  849. {
  850. callAllInterfaces(cl, &IGameEventsReceiver::availableArtifactsChanged, nullptr);
  851. }
  852. else
  853. {
  854. const CGBlackMarket *bm = dynamic_cast<const CGBlackMarket *>(cl->getObj(ObjectInstanceID(id)));
  855. assert(bm);
  856. callInterfaceIfPresent(cl, cl->getTile(bm->visitablePos())->visitableObjects.back()->tempOwner, &IGameEventsReceiver::availableArtifactsChanged, bm);
  857. }
  858. }
  859. void EntitiesChanged::applyCl(CClient *cl)
  860. {
  861. cl->invalidatePaths();
  862. }