NetPacksClient.cpp 28 KB

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