NetPacksClient.cpp 28 KB

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