NetPacksClient.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952
  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. //macros to avoid code duplication - calls given method with given arguments if interface for specific player is present
  44. //awaiting variadic templates...
  45. #define CALL_IN_PRIVILAGED_INTS(function, ...) \
  46. do \
  47. { \
  48. for(auto &ger : cl->privilagedGameEventReceivers) \
  49. ger->function(__VA_ARGS__); \
  50. } while(0)
  51. #define CALL_ONLY_THAT_INTERFACE(player, function, ...) \
  52. do \
  53. { \
  54. if(vstd::contains(cl->playerint,player)) \
  55. cl->playerint[player]->function(__VA_ARGS__); \
  56. }while(0)
  57. #define INTERFACE_CALL_IF_PRESENT(player,function,...) \
  58. do \
  59. { \
  60. CALL_ONLY_THAT_INTERFACE(player, function, __VA_ARGS__);\
  61. CALL_IN_PRIVILAGED_INTS(function, __VA_ARGS__); \
  62. } while(0)
  63. #define CALL_ONLY_THAT_BATTLE_INTERFACE(player,function, ...) \
  64. do \
  65. { \
  66. if(vstd::contains(cl->battleints,player)) \
  67. cl->battleints[player]->function(__VA_ARGS__); \
  68. \
  69. if(cl->additionalBattleInts.count(player)) \
  70. { \
  71. for(auto bInt : cl->additionalBattleInts[player])\
  72. bInt->function(__VA_ARGS__); \
  73. } \
  74. } while (0);
  75. #define BATTLE_INTERFACE_CALL_RECEIVERS(function,...) \
  76. do \
  77. { \
  78. for(auto & ber : cl->privilagedBattleEventReceivers)\
  79. ber->function(__VA_ARGS__); \
  80. } while(0)
  81. #define BATTLE_INTERFACE_CALL_IF_PRESENT(player,function,...) \
  82. do \
  83. { \
  84. CALL_ONLY_THAT_INTERFACE(player, function, __VA_ARGS__);\
  85. BATTLE_INTERFACE_CALL_RECEIVERS(function, __VA_ARGS__); \
  86. } while(0)
  87. //calls all normal interfaces and privilaged ones, playerints may be updated when iterating over it, so we need a copy
  88. #define CALL_IN_ALL_INTERFACES(function, ...) \
  89. do \
  90. { \
  91. auto ints = cl->playerint; \
  92. for(auto i = ints.begin(); i != ints.end(); i++)\
  93. CALL_ONLY_THAT_INTERFACE(i->first, function, __VA_ARGS__); \
  94. } while(0)
  95. #define BATTLE_INTERFACE_CALL_IF_PRESENT_FOR_BOTH_SIDES(function,...) \
  96. CALL_ONLY_THAT_BATTLE_INTERFACE(GS(cl)->curB->sides[0].color, function, __VA_ARGS__) \
  97. CALL_ONLY_THAT_BATTLE_INTERFACE(GS(cl)->curB->sides[1].color, function, __VA_ARGS__) \
  98. if(settings["session"]["spectate"].Bool() && !settings["session"]["spectate-skip-battle"].Bool() && LOCPLINT->battleInt) \
  99. { \
  100. CALL_ONLY_THAT_BATTLE_INTERFACE(PlayerColor::SPECTATOR, function, __VA_ARGS__) \
  101. } \
  102. BATTLE_INTERFACE_CALL_RECEIVERS(function, __VA_ARGS__)
  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->error("Cannot find hero with ID %d", 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->error("Cannot find hero with ID %d", id.getNum());
  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. // In auto testing mode we always close client if red player won or lose
  268. if(!settings["session"]["testmap"].isNull() && player == PlayerColor(0))
  269. handleQuit(settings["session"]["spectate"].Bool()); // if spectator is active ask to close client or not
  270. }
  271. void RemoveBonus::applyCl(CClient *cl)
  272. {
  273. cl->invalidatePaths();
  274. switch(who)
  275. {
  276. case HERO:
  277. {
  278. const CGHeroInstance *h = GS(cl)->getHero(ObjectInstanceID(id));
  279. INTERFACE_CALL_IF_PRESENT(h->tempOwner, heroBonusChanged, h, bonus,false);
  280. }
  281. break;
  282. case PLAYER:
  283. {
  284. //const PlayerState *p = GS(cl)->getPlayer(id);
  285. INTERFACE_CALL_IF_PRESENT(PlayerColor(id), playerBonusChanged, bonus, false);
  286. }
  287. break;
  288. }
  289. }
  290. void UpdateCampaignState::applyCl(CClient *cl)
  291. {
  292. cl->stopConnection();
  293. cl->campaignMapFinished(camp);
  294. }
  295. void PrepareForAdvancingCampaign::applyCl(CClient *cl)
  296. {
  297. cl->serv->prepareForSendingHeroes();
  298. }
  299. void RemoveObject::applyFirstCl(CClient *cl)
  300. {
  301. const CGObjectInstance *o = cl->getObj(id);
  302. if(CGI->mh)
  303. CGI->mh->hideObject(o, true);
  304. //notify interfaces about removal
  305. for(auto i=cl->playerint.begin(); i!=cl->playerint.end(); i++)
  306. {
  307. if(GS(cl)->isVisible(o, i->first))
  308. i->second->objectRemoved(o);
  309. }
  310. }
  311. void RemoveObject::applyCl(CClient *cl)
  312. {
  313. cl->invalidatePaths();
  314. }
  315. void TryMoveHero::applyFirstCl(CClient *cl)
  316. {
  317. CGHeroInstance *h = GS(cl)->getHero(id);
  318. //check if playerint will have the knowledge about movement - if not, directly update maphandler
  319. for(auto i=cl->playerint.begin(); i!=cl->playerint.end(); i++)
  320. {
  321. auto ps = GS(cl)->getPlayer(i->first);
  322. if(ps && (GS(cl)->isVisible(start - int3(1, 0, 0), i->first) || GS(cl)->isVisible(end - int3(1, 0, 0), i->first)))
  323. {
  324. if(ps->human)
  325. humanKnows = true;
  326. }
  327. }
  328. if(!CGI->mh)
  329. return;
  330. if(result == TELEPORTATION || result == EMBARK || result == DISEMBARK || !humanKnows)
  331. CGI->mh->hideObject(h, result == EMBARK && humanKnows);
  332. if(result == DISEMBARK)
  333. CGI->mh->printObject(h->boat);
  334. }
  335. void TryMoveHero::applyCl(CClient *cl)
  336. {
  337. const CGHeroInstance *h = cl->getHero(id);
  338. cl->invalidatePaths();
  339. if(CGI->mh)
  340. {
  341. if(result == TELEPORTATION || result == EMBARK || result == DISEMBARK)
  342. CGI->mh->printObject(h, result == DISEMBARK);
  343. if(result == EMBARK)
  344. CGI->mh->hideObject(h->boat);
  345. }
  346. PlayerColor player = h->tempOwner;
  347. for(auto &i : cl->playerint)
  348. if(cl->getPlayerRelations(i.first, player) != PlayerRelations::ENEMIES)
  349. i.second->tileRevealed(fowRevealed);
  350. //notify interfaces about move
  351. for(auto i=cl->playerint.begin(); i!=cl->playerint.end(); i++)
  352. {
  353. if(GS(cl)->isVisible(start - int3(1, 0, 0), i->first)
  354. || GS(cl)->isVisible(end - int3(1, 0, 0), i->first))
  355. {
  356. i->second->heroMoved(*this);
  357. }
  358. }
  359. //maphandler didn't get update from playerint, do it now
  360. //TODO: restructure nicely
  361. if(!humanKnows && CGI->mh)
  362. CGI->mh->printObject(h);
  363. }
  364. bool TryMoveHero::stopMovement() const
  365. {
  366. return result != SUCCESS && result != EMBARK && result != DISEMBARK && result != TELEPORTATION;
  367. }
  368. void NewStructures::applyCl(CClient *cl)
  369. {
  370. CGTownInstance *town = GS(cl)->getTown(tid);
  371. for(const auto & id : bid)
  372. {
  373. if(vstd::contains(cl->playerint,town->tempOwner))
  374. cl->playerint[town->tempOwner]->buildChanged(town,id,1);
  375. }
  376. }
  377. void RazeStructures::applyCl (CClient *cl)
  378. {
  379. CGTownInstance *town = GS(cl)->getTown(tid);
  380. for(const auto & id : bid)
  381. {
  382. if(vstd::contains (cl->playerint,town->tempOwner))
  383. cl->playerint[town->tempOwner]->buildChanged (town,id,2);
  384. }
  385. }
  386. void SetAvailableCreatures::applyCl(CClient *cl)
  387. {
  388. const CGDwelling *dw = static_cast<const CGDwelling*>(cl->getObj(tid));
  389. //inform order about the change
  390. PlayerColor p;
  391. if(dw->ID == Obj::WAR_MACHINE_FACTORY) //War Machines Factory is not flaggable, it's "owned" by visitor
  392. p = cl->getTile(dw->visitablePos())->visitableObjects.back()->tempOwner;
  393. else
  394. p = dw->tempOwner;
  395. INTERFACE_CALL_IF_PRESENT(p, availableCreaturesChanged, dw);
  396. }
  397. void SetHeroesInTown::applyCl(CClient *cl)
  398. {
  399. CGTownInstance *t = GS(cl)->getTown(tid);
  400. CGHeroInstance *hGarr = GS(cl)->getHero(this->garrison);
  401. CGHeroInstance *hVisit = GS(cl)->getHero(this->visiting);
  402. //inform all players that see this object
  403. for(auto i = cl->playerint.cbegin(); i != cl->playerint.cend(); ++i)
  404. {
  405. if(i->first >= PlayerColor::PLAYER_LIMIT)
  406. continue;
  407. if(GS(cl)->isVisible(t, i->first) ||
  408. (hGarr && GS(cl)->isVisible(hGarr, i->first)) ||
  409. (hVisit && GS(cl)->isVisible(hVisit, i->first)))
  410. {
  411. cl->playerint[i->first]->heroInGarrisonChange(t);
  412. }
  413. }
  414. }
  415. void HeroRecruited::applyCl(CClient *cl)
  416. {
  417. CGHeroInstance *h = GS(cl)->map->heroesOnMap.back();
  418. if(h->subID != hid)
  419. {
  420. logNetwork->error("Something wrong with hero recruited!");
  421. }
  422. bool needsPrinting = true;
  423. if(vstd::contains(cl->playerint, h->tempOwner))
  424. {
  425. cl->playerint[h->tempOwner]->heroCreated(h);
  426. if(const CGTownInstance *t = GS(cl)->getTown(tid))
  427. {
  428. cl->playerint[h->tempOwner]->heroInGarrisonChange(t);
  429. needsPrinting = false;
  430. }
  431. }
  432. if(needsPrinting && CGI->mh)
  433. CGI->mh->printObject(h);
  434. }
  435. void GiveHero::applyCl(CClient *cl)
  436. {
  437. CGHeroInstance *h = GS(cl)->getHero(id);
  438. if(CGI->mh)
  439. CGI->mh->printObject(h);
  440. cl->playerint[h->tempOwner]->heroCreated(h);
  441. }
  442. void GiveHero::applyFirstCl(CClient *cl)
  443. {
  444. if(CGI->mh)
  445. CGI->mh->hideObject(GS(cl)->getHero(id));
  446. }
  447. void InfoWindow::applyCl(CClient *cl)
  448. {
  449. std::vector<Component*> comps;
  450. for(auto & elem : components)
  451. {
  452. comps.push_back(&elem);
  453. }
  454. std::string str;
  455. text.toString(str);
  456. if(vstd::contains(cl->playerint,player))
  457. cl->playerint.at(player)->showInfoDialog(str,comps,(soundBase::soundID)soundID);
  458. else
  459. logNetwork->warn("We received InfoWindow for not our player...");
  460. }
  461. void SetObjectProperty::applyCl(CClient *cl)
  462. {
  463. //inform all players that see this object
  464. for(auto it = cl->playerint.cbegin(); it != cl->playerint.cend(); ++it)
  465. {
  466. if(GS(cl)->isVisible(GS(cl)->getObjInstance(id), it->first))
  467. INTERFACE_CALL_IF_PRESENT(it->first, objectPropertyChanged, this);
  468. }
  469. }
  470. void HeroLevelUp::applyCl(CClient *cl)
  471. {
  472. //INTERFACE_CALL_IF_PRESENT(h->tempOwner, heroGotLevel, h, primskill, skills, id);
  473. if(vstd::contains(cl->playerint,hero->tempOwner))
  474. {
  475. cl->playerint[hero->tempOwner]->heroGotLevel(hero, primskill, skills, queryID);
  476. }
  477. //else
  478. // cb->selectionMade(0, queryID);
  479. }
  480. void CommanderLevelUp::applyCl(CClient *cl)
  481. {
  482. const CCommanderInstance * commander = hero->commander;
  483. assert (commander);
  484. PlayerColor player = hero->tempOwner;
  485. if (commander->armyObj && vstd::contains(cl->playerint, player)) //is it possible for Commander to exist beyond armed instance?
  486. {
  487. cl->playerint[player]->commanderGotLevel(commander, skills, queryID);
  488. }
  489. }
  490. void BlockingDialog::applyCl(CClient *cl)
  491. {
  492. std::string str;
  493. text.toString(str);
  494. if(vstd::contains(cl->playerint,player))
  495. cl->playerint.at(player)->showBlockingDialog(str,components,queryID,(soundBase::soundID)soundID,selection(),cancel());
  496. else
  497. logNetwork->warn("We received YesNoDialog for not our player...");
  498. }
  499. void GarrisonDialog::applyCl(CClient *cl)
  500. {
  501. const CGHeroInstance *h = cl->getHero(hid);
  502. const CArmedInstance *obj = static_cast<const CArmedInstance*>(cl->getObj(objid));
  503. if(!vstd::contains(cl->playerint,h->getOwner()))
  504. return;
  505. cl->playerint.at(h->getOwner())->showGarrisonDialog(obj,h,removableUnits,queryID);
  506. }
  507. void ExchangeDialog::applyCl(CClient *cl)
  508. {
  509. assert(heroes[0] && heroes[1]);
  510. INTERFACE_CALL_IF_PRESENT(heroes[0]->tempOwner, heroExchangeStarted, heroes[0]->id, heroes[1]->id, queryID);
  511. }
  512. void TeleportDialog::applyCl(CClient *cl)
  513. {
  514. CALL_ONLY_THAT_INTERFACE(hero->tempOwner,showTeleportDialog,channel,exits,impassable,queryID);
  515. }
  516. void MapObjectSelectDialog::applyCl(CClient * cl)
  517. {
  518. CALL_ONLY_THAT_INTERFACE(player, showMapObjectSelectDialog, queryID, icon, title, description, objects);
  519. }
  520. void BattleStart::applyFirstCl(CClient *cl)
  521. {
  522. //Cannot use the usual macro because curB is not set yet
  523. CALL_ONLY_THAT_BATTLE_INTERFACE(info->sides[0].color, battleStartBefore, info->sides[0].armyObject, info->sides[1].armyObject,
  524. info->tile, info->sides[0].hero, info->sides[1].hero);
  525. CALL_ONLY_THAT_BATTLE_INTERFACE(info->sides[1].color, battleStartBefore, info->sides[0].armyObject, info->sides[1].armyObject,
  526. info->tile, info->sides[0].hero, info->sides[1].hero);
  527. CALL_ONLY_THAT_BATTLE_INTERFACE(PlayerColor::SPECTATOR, battleStartBefore, info->sides[0].armyObject, info->sides[1].armyObject,
  528. info->tile, info->sides[0].hero, info->sides[1].hero);
  529. BATTLE_INTERFACE_CALL_RECEIVERS(battleStartBefore, info->sides[0].armyObject, info->sides[1].armyObject,
  530. info->tile, info->sides[0].hero, info->sides[1].hero);
  531. }
  532. void BattleStart::applyCl(CClient *cl)
  533. {
  534. cl->battleStarted(info);
  535. }
  536. void BattleNextRound::applyFirstCl(CClient *cl)
  537. {
  538. BATTLE_INTERFACE_CALL_IF_PRESENT_FOR_BOTH_SIDES(battleNewRoundFirst,round);
  539. }
  540. void BattleNextRound::applyCl(CClient *cl)
  541. {
  542. BATTLE_INTERFACE_CALL_IF_PRESENT_FOR_BOTH_SIDES(battleNewRound,round);
  543. }
  544. void BattleSetActiveStack::applyCl(CClient *cl)
  545. {
  546. if(!askPlayerInterface)
  547. return;
  548. const CStack *activated = GS(cl)->curB->battleGetStackByID(stack);
  549. PlayerColor playerToCall; //player that will move activated stack
  550. if (activated->hasBonusOfType(Bonus::HYPNOTIZED))
  551. {
  552. playerToCall = (GS(cl)->curB->sides[0].color == activated->owner
  553. ? GS(cl)->curB->sides[1].color
  554. : GS(cl)->curB->sides[0].color);
  555. }
  556. else
  557. {
  558. playerToCall = activated->owner;
  559. }
  560. if (vstd::contains(cl->battleints, playerToCall))
  561. boost::thread(std::bind(&CClient::waitForMoveAndSend, cl, playerToCall));
  562. }
  563. void BattleTriggerEffect::applyCl(CClient * cl)
  564. {
  565. BATTLE_INTERFACE_CALL_IF_PRESENT_FOR_BOTH_SIDES(battleTriggerEffect, *this);
  566. }
  567. void BattleObstaclePlaced::applyCl(CClient * cl)
  568. {
  569. BATTLE_INTERFACE_CALL_IF_PRESENT_FOR_BOTH_SIDES(battleObstaclePlaced, *obstacle);
  570. }
  571. void BattleUpdateGateState::applyFirstCl(CClient * cl)
  572. {
  573. BATTLE_INTERFACE_CALL_IF_PRESENT_FOR_BOTH_SIDES(battleGateStateChanged, state);
  574. }
  575. void BattleResult::applyFirstCl(CClient *cl)
  576. {
  577. BATTLE_INTERFACE_CALL_IF_PRESENT_FOR_BOTH_SIDES(battleEnd,this);
  578. cl->battleFinished();
  579. }
  580. void BattleStackMoved::applyFirstCl(CClient *cl)
  581. {
  582. const CStack * movedStack = GS(cl)->curB->battleGetStackByID(stack);
  583. BATTLE_INTERFACE_CALL_IF_PRESENT_FOR_BOTH_SIDES(battleStackMoved,movedStack,tilesToMove,distance);
  584. }
  585. //void BattleStackAttacked::(CClient *cl)
  586. void BattleStackAttacked::applyFirstCl(CClient *cl)
  587. {
  588. std::vector<BattleStackAttacked> bsa;
  589. bsa.push_back(*this);
  590. BATTLE_INTERFACE_CALL_IF_PRESENT_FOR_BOTH_SIDES(battleStacksAttacked,bsa);
  591. }
  592. void BattleAttack::applyFirstCl(CClient *cl)
  593. {
  594. BATTLE_INTERFACE_CALL_IF_PRESENT_FOR_BOTH_SIDES(battleAttack,this);
  595. for (auto & elem : bsa)
  596. {
  597. for (int z=0; z<elem.healedStacks.size(); ++z)
  598. {
  599. elem.healedStacks[z].applyCl(cl);
  600. }
  601. }
  602. }
  603. void BattleAttack::applyCl(CClient *cl)
  604. {
  605. BATTLE_INTERFACE_CALL_IF_PRESENT_FOR_BOTH_SIDES(battleStacksAttacked,bsa);
  606. }
  607. void StartAction::applyFirstCl(CClient *cl)
  608. {
  609. cl->curbaction = boost::make_optional(ba);
  610. BATTLE_INTERFACE_CALL_IF_PRESENT_FOR_BOTH_SIDES(actionStarted, ba);
  611. }
  612. void BattleSpellCast::applyCl(CClient *cl)
  613. {
  614. BATTLE_INTERFACE_CALL_IF_PRESENT_FOR_BOTH_SIDES(battleSpellCast,this);
  615. }
  616. void SetStackEffect::applyCl(CClient *cl)
  617. {
  618. //informing about effects
  619. BATTLE_INTERFACE_CALL_IF_PRESENT_FOR_BOTH_SIDES(battleStacksEffectsSet,*this);
  620. }
  621. void StacksInjured::applyCl(CClient *cl)
  622. {
  623. BATTLE_INTERFACE_CALL_IF_PRESENT_FOR_BOTH_SIDES(battleStacksAttacked,stacks);
  624. }
  625. void BattleResultsApplied::applyCl(CClient *cl)
  626. {
  627. INTERFACE_CALL_IF_PRESENT(player1, battleResultsApplied);
  628. INTERFACE_CALL_IF_PRESENT(player2, battleResultsApplied);
  629. INTERFACE_CALL_IF_PRESENT(PlayerColor::SPECTATOR, battleResultsApplied);
  630. }
  631. void StacksHealedOrResurrected::applyCl(CClient * cl)
  632. {
  633. std::vector<std::pair<ui32, ui32> > shiftedHealed;
  634. for(auto & elem : healedStacks)
  635. {
  636. shiftedHealed.push_back(std::make_pair(elem.stackId, (ui32)elem.delta));
  637. }
  638. BATTLE_INTERFACE_CALL_IF_PRESENT_FOR_BOTH_SIDES(battleStacksHealedRes, shiftedHealed, lifeDrain, tentHealing, drainedFrom);
  639. }
  640. void ObstaclesRemoved::applyCl(CClient *cl)
  641. {
  642. //inform interfaces about removed obstacles
  643. BATTLE_INTERFACE_CALL_IF_PRESENT_FOR_BOTH_SIDES(battleObstaclesRemoved, obstacles);
  644. }
  645. void CatapultAttack::applyCl(CClient *cl)
  646. {
  647. //inform interfaces about catapult attack
  648. BATTLE_INTERFACE_CALL_IF_PRESENT_FOR_BOTH_SIDES(battleCatapultAttacked, *this);
  649. }
  650. void BattleStacksRemoved::applyFirstCl(CClient * cl)
  651. {
  652. //inform interfaces about removed stacks
  653. BATTLE_INTERFACE_CALL_IF_PRESENT_FOR_BOTH_SIDES(battleStacksRemoved, *this);
  654. }
  655. void BattleStackAdded::applyCl(CClient *cl)
  656. {
  657. BATTLE_INTERFACE_CALL_IF_PRESENT_FOR_BOTH_SIDES(battleNewStackAppeared, GS(cl)->curB->stacks.back());
  658. }
  659. CGameState* CPackForClient::GS(CClient *cl)
  660. {
  661. return cl->gs;
  662. }
  663. void EndAction::applyCl(CClient *cl)
  664. {
  665. BATTLE_INTERFACE_CALL_IF_PRESENT_FOR_BOTH_SIDES(actionFinished, *cl->curbaction);
  666. cl->curbaction.reset();
  667. }
  668. void PackageApplied::applyCl(CClient *cl)
  669. {
  670. INTERFACE_CALL_IF_PRESENT(player, requestRealized, this);
  671. if(!CClient::waitingRequest.tryRemovingElement(requestID))
  672. logNetwork->warn("Surprising server message!");
  673. }
  674. void SystemMessage::applyCl(CClient *cl)
  675. {
  676. std::ostringstream str;
  677. str << "System message: " << text;
  678. logNetwork->error(str.str()); // usually used to receive error messages from server
  679. if(LOCPLINT && !settings["session"]["hideSystemMessages"].Bool())
  680. LOCPLINT->cingconsole->print(str.str());
  681. }
  682. void PlayerBlocked::applyCl(CClient *cl)
  683. {
  684. INTERFACE_CALL_IF_PRESENT(player,playerBlocked,reason, startOrEnd==BLOCKADE_STARTED);
  685. }
  686. void YourTurn::applyCl(CClient *cl)
  687. {
  688. CALL_IN_ALL_INTERFACES(playerStartsTurn, player);
  689. CALL_ONLY_THAT_INTERFACE(player,yourTurn);
  690. }
  691. void SaveGame::applyCl(CClient *cl)
  692. {
  693. const auto stem = FileInfo::GetPathStem(fname);
  694. CResourceHandler::get("local")->createResource(stem.to_string() + ".vcgm1");
  695. try
  696. {
  697. CSaveFile save(*CResourceHandler::get()->getResourceName(ResourceID(stem.to_string(), EResType::CLIENT_SAVEGAME)));
  698. cl->saveCommonState(save);
  699. save << *cl;
  700. }
  701. catch(std::exception &e)
  702. {
  703. logNetwork->error("Failed to save game:%s", e.what());
  704. }
  705. }
  706. void PlayerMessage::applyCl(CClient *cl)
  707. {
  708. logNetwork->debug("Player %s sends a message: %s", player.getStr(), text);
  709. std::ostringstream str;
  710. if(player.isSpectator())
  711. str << "Spectator: " << text;
  712. else
  713. str << cl->getPlayer(player)->nodeName() <<": " << text;
  714. if(LOCPLINT)
  715. LOCPLINT->cingconsole->print(str.str());
  716. }
  717. void ShowInInfobox::applyCl(CClient *cl)
  718. {
  719. INTERFACE_CALL_IF_PRESENT(player,showComp, c, text.toString());
  720. }
  721. void AdvmapSpellCast::applyCl(CClient *cl)
  722. {
  723. cl->invalidatePaths();
  724. //consider notifying other interfaces that see hero?
  725. INTERFACE_CALL_IF_PRESENT(caster->getOwner(),advmapSpellCast, caster, spellID);
  726. }
  727. void ShowWorldViewEx::applyCl(CClient * cl)
  728. {
  729. CALL_ONLY_THAT_INTERFACE(player, showWorldViewEx, objectPositions);
  730. }
  731. void OpenWindow::applyCl(CClient *cl)
  732. {
  733. switch(window)
  734. {
  735. case RECRUITMENT_FIRST:
  736. case RECRUITMENT_ALL:
  737. {
  738. const CGDwelling *dw = dynamic_cast<const CGDwelling*>(cl->getObj(ObjectInstanceID(id1)));
  739. const CArmedInstance *dst = dynamic_cast<const CArmedInstance*>(cl->getObj(ObjectInstanceID(id2)));
  740. INTERFACE_CALL_IF_PRESENT(dst->tempOwner,showRecruitmentDialog, dw, dst, window == RECRUITMENT_FIRST ? 0 : -1);
  741. }
  742. break;
  743. case SHIPYARD_WINDOW:
  744. {
  745. const IShipyard *sy = IShipyard::castFrom(cl->getObj(ObjectInstanceID(id1)));
  746. INTERFACE_CALL_IF_PRESENT(sy->o->tempOwner, showShipyardDialog, sy);
  747. }
  748. break;
  749. case THIEVES_GUILD:
  750. {
  751. //displays Thieves' Guild window (when hero enters Den of Thieves)
  752. const CGObjectInstance *obj = cl->getObj(ObjectInstanceID(id2));
  753. INTERFACE_CALL_IF_PRESENT(PlayerColor(id1), showThievesGuildWindow, obj);
  754. }
  755. break;
  756. case UNIVERSITY_WINDOW:
  757. {
  758. //displays University window (when hero enters University on adventure map)
  759. const IMarket *market = IMarket::castFrom(cl->getObj(ObjectInstanceID(id1)));
  760. const CGHeroInstance *hero = cl->getHero(ObjectInstanceID(id2));
  761. INTERFACE_CALL_IF_PRESENT(hero->tempOwner,showUniversityWindow, market, hero);
  762. }
  763. break;
  764. case MARKET_WINDOW:
  765. {
  766. //displays Thieves' Guild window (when hero enters Den of Thieves)
  767. const CGObjectInstance *obj = cl->getObj(ObjectInstanceID(id1));
  768. const CGHeroInstance *hero = cl->getHero(ObjectInstanceID(id2));
  769. const IMarket *market = IMarket::castFrom(obj);
  770. INTERFACE_CALL_IF_PRESENT(cl->getTile(obj->visitablePos())->visitableObjects.back()->tempOwner, showMarketWindow, market, hero);
  771. }
  772. break;
  773. case HILL_FORT_WINDOW:
  774. {
  775. //displays Hill fort window
  776. const CGObjectInstance *obj = cl->getObj(ObjectInstanceID(id1));
  777. const CGHeroInstance *hero = cl->getHero(ObjectInstanceID(id2));
  778. INTERFACE_CALL_IF_PRESENT(cl->getTile(obj->visitablePos())->visitableObjects.back()->tempOwner, showHillFortWindow, obj, hero);
  779. }
  780. break;
  781. case PUZZLE_MAP:
  782. {
  783. INTERFACE_CALL_IF_PRESENT(PlayerColor(id1), showPuzzleMap);
  784. }
  785. break;
  786. case TAVERN_WINDOW:
  787. const CGObjectInstance *obj1 = cl->getObj(ObjectInstanceID(id1)),
  788. *obj2 = cl->getObj(ObjectInstanceID(id2));
  789. INTERFACE_CALL_IF_PRESENT(obj1->tempOwner, showTavernWindow, obj2);
  790. break;
  791. }
  792. }
  793. void CenterView::applyCl(CClient *cl)
  794. {
  795. INTERFACE_CALL_IF_PRESENT (player, centerView, pos, focusTime);
  796. }
  797. void NewObject::applyCl(CClient *cl)
  798. {
  799. cl->invalidatePaths();
  800. const CGObjectInstance *obj = cl->getObj(id);
  801. if(CGI->mh)
  802. CGI->mh->printObject(obj, true);
  803. for(auto i=cl->playerint.begin(); i!=cl->playerint.end(); i++)
  804. {
  805. if(GS(cl)->isVisible(obj, i->first))
  806. i->second->newObject(obj);
  807. }
  808. }
  809. void SetAvailableArtifacts::applyCl(CClient *cl)
  810. {
  811. if(id < 0) //artifact merchants globally
  812. {
  813. for(auto & elem : cl->playerint)
  814. elem.second->availableArtifactsChanged(nullptr);
  815. }
  816. else
  817. {
  818. const CGBlackMarket *bm = dynamic_cast<const CGBlackMarket *>(cl->getObj(ObjectInstanceID(id)));
  819. assert(bm);
  820. INTERFACE_CALL_IF_PRESENT(cl->getTile(bm->visitablePos())->visitableObjects.back()->tempOwner, availableArtifactsChanged, bm);
  821. }
  822. }