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