2
0

NetPacksClient.cpp 28 KB

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