NetPacksClient.cpp 26 KB

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