NetPacksClient.cpp 26 KB

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