NetPacksClient.cpp 27 KB

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