NetPacksClient.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969
  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 SetHeroArtifacts::applyCl(CClient *cl)
  404. // {
  405. // // CGHeroInstance *h = GS(cl)->getHero(hid);
  406. // // CGameInterface *player = (vstd::contains(cl->playerint,h->tempOwner) ? cl->playerint[h->tempOwner] : nullptr);
  407. // // if(!player)
  408. // // return;
  409. //
  410. // //h->recreateArtBonuses();
  411. // //player->heroArtifactSetChanged(h);
  412. //
  413. // // for(Bonus bonus : gained)
  414. // // {
  415. // // player->heroBonusChanged(h,bonus,true);
  416. // // }
  417. // // for(Bonus bonus : lost)
  418. // // {
  419. // // player->heroBonusChanged(h,bonus,false);
  420. // // }
  421. // }
  422. void HeroRecruited::applyCl(CClient *cl)
  423. {
  424. CGHeroInstance *h = GS(cl)->map->heroesOnMap.back();
  425. if(h->subID != hid)
  426. {
  427. logNetwork->errorStream() << "Something wrong with hero recruited!";
  428. }
  429. bool needsPrinting = true;
  430. if(vstd::contains(cl->playerint, h->tempOwner))
  431. {
  432. cl->playerint[h->tempOwner]->heroCreated(h);
  433. if(const CGTownInstance *t = GS(cl)->getTown(tid))
  434. {
  435. cl->playerint[h->tempOwner]->heroInGarrisonChange(t);
  436. needsPrinting = false;
  437. }
  438. }
  439. if (needsPrinting)
  440. {
  441. CGI->mh->printObject(h);
  442. }
  443. }
  444. void GiveHero::applyCl(CClient *cl)
  445. {
  446. CGHeroInstance *h = GS(cl)->getHero(id);
  447. CGI->mh->printObject(h);
  448. cl->playerint[h->tempOwner]->heroCreated(h);
  449. }
  450. void GiveHero::applyFirstCl(CClient *cl)
  451. {
  452. CGI->mh->hideObject(GS(cl)->getHero(id));
  453. }
  454. void InfoWindow::applyCl(CClient *cl)
  455. {
  456. std::vector<Component*> comps;
  457. for(auto & elem : components)
  458. {
  459. comps.push_back(&elem);
  460. }
  461. std::string str;
  462. text.toString(str);
  463. if(vstd::contains(cl->playerint,player))
  464. cl->playerint.at(player)->showInfoDialog(str,comps,(soundBase::soundID)soundID);
  465. else
  466. logNetwork->warnStream() << "We received InfoWindow for not our player...";
  467. }
  468. void SetObjectProperty::applyCl(CClient *cl)
  469. {
  470. //inform all players that see this object
  471. for(auto it = cl->playerint.cbegin(); it != cl->playerint.cend(); ++it)
  472. {
  473. if(GS(cl)->isVisible(GS(cl)->getObjInstance(id), it->first))
  474. INTERFACE_CALL_IF_PRESENT(it->first, objectPropertyChanged, this);
  475. }
  476. }
  477. void HeroLevelUp::applyCl(CClient *cl)
  478. {
  479. //INTERFACE_CALL_IF_PRESENT(h->tempOwner, heroGotLevel, h, primskill, skills, id);
  480. if(vstd::contains(cl->playerint,hero->tempOwner))
  481. {
  482. cl->playerint[hero->tempOwner]->heroGotLevel(hero, primskill, skills, queryID);
  483. }
  484. //else
  485. // cb->selectionMade(0, queryID);
  486. }
  487. void CommanderLevelUp::applyCl(CClient *cl)
  488. {
  489. const CCommanderInstance * commander = hero->commander;
  490. assert (commander);
  491. PlayerColor player = hero->tempOwner;
  492. if (commander->armyObj && vstd::contains(cl->playerint, player)) //is it possible for Commander to exist beyond armed instance?
  493. {
  494. cl->playerint[player]->commanderGotLevel(commander, skills, queryID);
  495. }
  496. }
  497. void BlockingDialog::applyCl(CClient *cl)
  498. {
  499. std::string str;
  500. text.toString(str);
  501. if(vstd::contains(cl->playerint,player))
  502. cl->playerint.at(player)->showBlockingDialog(str,components,queryID,(soundBase::soundID)soundID,selection(),cancel());
  503. else
  504. logNetwork->warnStream() << "We received YesNoDialog for not our player...";
  505. }
  506. void GarrisonDialog::applyCl(CClient *cl)
  507. {
  508. const CGHeroInstance *h = cl->getHero(hid);
  509. const CArmedInstance *obj = static_cast<const CArmedInstance*>(cl->getObj(objid));
  510. if(!vstd::contains(cl->playerint,h->getOwner()))
  511. return;
  512. cl->playerint.at(h->getOwner())->showGarrisonDialog(obj,h,removableUnits,queryID);
  513. }
  514. void ExchangeDialog::applyCl(CClient *cl)
  515. {
  516. assert(heroes[0] && heroes[1]);
  517. INTERFACE_CALL_IF_PRESENT(heroes[0]->tempOwner, heroExchangeStarted, heroes[0]->id, heroes[1]->id, queryID);
  518. }
  519. void TeleportDialog::applyCl(CClient *cl)
  520. {
  521. CALL_ONLY_THAT_INTERFACE(hero->tempOwner,showTeleportDialog,channel,exits,impassable,queryID);
  522. }
  523. void BattleStart::applyFirstCl(CClient *cl)
  524. {
  525. //Cannot use the usual macro because curB is not set yet
  526. CALL_ONLY_THAT_BATTLE_INTERFACE(info->sides[0].color, battleStartBefore, info->sides[0].armyObject, info->sides[1].armyObject,
  527. info->tile, info->sides[0].hero, info->sides[1].hero);
  528. CALL_ONLY_THAT_BATTLE_INTERFACE(info->sides[1].color, battleStartBefore, info->sides[0].armyObject, info->sides[1].armyObject,
  529. info->tile, info->sides[0].hero, info->sides[1].hero);
  530. BATTLE_INTERFACE_CALL_RECEIVERS(battleStartBefore, info->sides[0].armyObject, info->sides[1].armyObject,
  531. info->tile, info->sides[0].hero, info->sides[1].hero);
  532. }
  533. void BattleStart::applyCl(CClient *cl)
  534. {
  535. cl->battleStarted(info);
  536. }
  537. void BattleNextRound::applyFirstCl(CClient *cl)
  538. {
  539. BATTLE_INTERFACE_CALL_IF_PRESENT_FOR_BOTH_SIDES(battleNewRoundFirst,round);
  540. }
  541. void BattleNextRound::applyCl(CClient *cl)
  542. {
  543. BATTLE_INTERFACE_CALL_IF_PRESENT_FOR_BOTH_SIDES(battleNewRound,round);
  544. }
  545. void BattleSetActiveStack::applyCl(CClient *cl)
  546. {
  547. if(!askPlayerInterface)
  548. return;
  549. const CStack *activated = GS(cl)->curB->battleGetStackByID(stack);
  550. PlayerColor playerToCall; //player that will move activated stack
  551. if (activated->hasBonusOfType(Bonus::HYPNOTIZED))
  552. {
  553. playerToCall = (GS(cl)->curB->sides[0].color == activated->owner
  554. ? GS(cl)->curB->sides[1].color
  555. : GS(cl)->curB->sides[0].color);
  556. }
  557. else
  558. {
  559. playerToCall = activated->owner;
  560. }
  561. if (vstd::contains(cl->battleints, playerToCall))
  562. boost::thread(std::bind(&CClient::waitForMoveAndSend, cl, playerToCall));
  563. }
  564. void BattleTriggerEffect::applyCl(CClient * cl)
  565. {
  566. BATTLE_INTERFACE_CALL_IF_PRESENT_FOR_BOTH_SIDES(battleTriggerEffect, *this);
  567. }
  568. void BattleObstaclePlaced::applyCl(CClient * cl)
  569. {
  570. BATTLE_INTERFACE_CALL_IF_PRESENT_FOR_BOTH_SIDES(battleObstaclePlaced, *obstacle);
  571. }
  572. void BattleUpdateGateState::applyFirstCl(CClient * cl)
  573. {
  574. BATTLE_INTERFACE_CALL_IF_PRESENT_FOR_BOTH_SIDES(battleGateStateChanged, state);
  575. }
  576. void BattleResult::applyFirstCl(CClient *cl)
  577. {
  578. BATTLE_INTERFACE_CALL_IF_PRESENT_FOR_BOTH_SIDES(battleEnd,this);
  579. cl->battleFinished();
  580. }
  581. void BattleStackMoved::applyFirstCl(CClient *cl)
  582. {
  583. const CStack * movedStack = GS(cl)->curB->battleGetStackByID(stack);
  584. BATTLE_INTERFACE_CALL_IF_PRESENT_FOR_BOTH_SIDES(battleStackMoved,movedStack,tilesToMove,distance);
  585. }
  586. //void BattleStackAttacked::(CClient *cl)
  587. void BattleStackAttacked::applyFirstCl(CClient *cl)
  588. {
  589. std::vector<BattleStackAttacked> bsa;
  590. bsa.push_back(*this);
  591. BATTLE_INTERFACE_CALL_IF_PRESENT_FOR_BOTH_SIDES(battleStacksAttacked,bsa);
  592. }
  593. void BattleAttack::applyFirstCl(CClient *cl)
  594. {
  595. BATTLE_INTERFACE_CALL_IF_PRESENT_FOR_BOTH_SIDES(battleAttack,this);
  596. for (auto & elem : bsa)
  597. {
  598. for (int z=0; z<elem.healedStacks.size(); ++z)
  599. {
  600. elem.healedStacks[z].applyCl(cl);
  601. }
  602. }
  603. }
  604. void BattleAttack::applyCl(CClient *cl)
  605. {
  606. BATTLE_INTERFACE_CALL_IF_PRESENT_FOR_BOTH_SIDES(battleStacksAttacked,bsa);
  607. }
  608. void StartAction::applyFirstCl(CClient *cl)
  609. {
  610. cl->curbaction = ba;
  611. BATTLE_INTERFACE_CALL_IF_PRESENT_FOR_BOTH_SIDES(actionStarted, ba);
  612. }
  613. void BattleSpellCast::applyCl(CClient *cl)
  614. {
  615. BATTLE_INTERFACE_CALL_IF_PRESENT_FOR_BOTH_SIDES(battleSpellCast,this);
  616. }
  617. void SetStackEffect::applyCl(CClient *cl)
  618. {
  619. //informing about effects
  620. BATTLE_INTERFACE_CALL_IF_PRESENT_FOR_BOTH_SIDES(battleStacksEffectsSet,*this);
  621. }
  622. void StacksInjured::applyCl(CClient *cl)
  623. {
  624. BATTLE_INTERFACE_CALL_IF_PRESENT_FOR_BOTH_SIDES(battleStacksAttacked,stacks);
  625. }
  626. void BattleResultsApplied::applyCl(CClient *cl)
  627. {
  628. INTERFACE_CALL_IF_PRESENT(player1, battleResultsApplied);
  629. INTERFACE_CALL_IF_PRESENT(player2, battleResultsApplied);
  630. INTERFACE_CALL_IF_PRESENT(PlayerColor::UNFLAGGABLE, battleResultsApplied);
  631. if(GS(cl)->initialOpts->mode == StartInfo::DUEL)
  632. {
  633. handleQuit();
  634. }
  635. }
  636. void StacksHealedOrResurrected::applyCl(CClient *cl)
  637. {
  638. std::vector<std::pair<ui32, ui32> > shiftedHealed;
  639. for(auto & elem : healedStacks)
  640. {
  641. shiftedHealed.push_back(std::make_pair(elem.stackID, elem.healedHP));
  642. }
  643. BATTLE_INTERFACE_CALL_IF_PRESENT_FOR_BOTH_SIDES(battleStacksHealedRes, shiftedHealed, lifeDrain, tentHealing, drainedFrom);
  644. }
  645. void ObstaclesRemoved::applyCl(CClient *cl)
  646. {
  647. //inform interfaces about removed obstacles
  648. BATTLE_INTERFACE_CALL_IF_PRESENT_FOR_BOTH_SIDES(battleObstaclesRemoved, obstacles);
  649. }
  650. void CatapultAttack::applyCl(CClient *cl)
  651. {
  652. //inform interfaces about catapult attack
  653. BATTLE_INTERFACE_CALL_IF_PRESENT_FOR_BOTH_SIDES(battleCatapultAttacked, *this);
  654. }
  655. void BattleStacksRemoved::applyFirstCl(CClient * cl)
  656. {
  657. //inform interfaces about removed stacks
  658. BATTLE_INTERFACE_CALL_IF_PRESENT_FOR_BOTH_SIDES(battleStacksRemoved, *this);
  659. }
  660. void BattleStackAdded::applyCl(CClient *cl)
  661. {
  662. BATTLE_INTERFACE_CALL_IF_PRESENT_FOR_BOTH_SIDES(battleNewStackAppeared, GS(cl)->curB->stacks.back());
  663. }
  664. CGameState* CPackForClient::GS(CClient *cl)
  665. {
  666. return cl->gs;
  667. }
  668. void EndAction::applyCl(CClient *cl)
  669. {
  670. BATTLE_INTERFACE_CALL_IF_PRESENT_FOR_BOTH_SIDES(actionFinished, *cl->curbaction);
  671. cl->curbaction.reset();
  672. }
  673. void PackageApplied::applyCl(CClient *cl)
  674. {
  675. INTERFACE_CALL_IF_PRESENT(player, requestRealized, this);
  676. if(!cl->waitingRequest.tryRemovingElement(requestID))
  677. logNetwork->warnStream() << "Surprising server message!";
  678. }
  679. void SystemMessage::applyCl(CClient *cl)
  680. {
  681. std::ostringstream str;
  682. str << "System message: " << text;
  683. logNetwork->errorStream() << str.str(); // usually used to receive error messages from server
  684. if(LOCPLINT && !settings["session"]["hideSystemMessages"].Bool())
  685. LOCPLINT->cingconsole->print(str.str());
  686. }
  687. void PlayerBlocked::applyCl(CClient *cl)
  688. {
  689. INTERFACE_CALL_IF_PRESENT(player,playerBlocked,reason, startOrEnd==BLOCKADE_STARTED);
  690. }
  691. void YourTurn::applyCl(CClient *cl)
  692. {
  693. CALL_IN_ALL_INTERFACES(playerStartsTurn, player);
  694. CALL_ONLY_THAT_INTERFACE(player,yourTurn);
  695. }
  696. void SaveGame::applyCl(CClient *cl)
  697. {
  698. const auto stem = FileInfo::GetPathStem(fname);
  699. CResourceHandler::get("local")->createResource(stem.to_string() + ".vcgm1");
  700. try
  701. {
  702. CSaveFile save(*CResourceHandler::get()->getResourceName(ResourceID(stem.to_string(), EResType::CLIENT_SAVEGAME)));
  703. cl->saveCommonState(save);
  704. save << *cl;
  705. }
  706. catch(std::exception &e)
  707. {
  708. logNetwork->errorStream() << "Failed to save game:" << e.what();
  709. }
  710. }
  711. void PlayerMessage::applyCl(CClient *cl)
  712. {
  713. logNetwork->debugStream() << "Player "<< player <<" sends a message: " << text;
  714. std::ostringstream str;
  715. str << cl->getPlayer(player)->nodeName() <<": " << text;
  716. if(LOCPLINT)
  717. LOCPLINT->cingconsole->print(str.str());
  718. }
  719. void ShowInInfobox::applyCl(CClient *cl)
  720. {
  721. INTERFACE_CALL_IF_PRESENT(player,showComp, c, text.toString());
  722. }
  723. void AdvmapSpellCast::applyCl(CClient *cl)
  724. {
  725. cl->invalidatePaths();
  726. //consider notifying other interfaces that see hero?
  727. INTERFACE_CALL_IF_PRESENT(caster->getOwner(),advmapSpellCast, caster, spellID);
  728. }
  729. void ShowWorldViewEx::applyCl(CClient * cl)
  730. {
  731. CALL_ONLY_THAT_INTERFACE(player, showWorldViewEx, objectPositions);
  732. }
  733. void OpenWindow::applyCl(CClient *cl)
  734. {
  735. switch(window)
  736. {
  737. case RECRUITMENT_FIRST:
  738. case RECRUITMENT_ALL:
  739. {
  740. const CGDwelling *dw = dynamic_cast<const CGDwelling*>(cl->getObj(ObjectInstanceID(id1)));
  741. const CArmedInstance *dst = dynamic_cast<const CArmedInstance*>(cl->getObj(ObjectInstanceID(id2)));
  742. INTERFACE_CALL_IF_PRESENT(dst->tempOwner,showRecruitmentDialog, dw, dst, window == RECRUITMENT_FIRST ? 0 : -1);
  743. }
  744. break;
  745. case SHIPYARD_WINDOW:
  746. {
  747. const IShipyard *sy = IShipyard::castFrom(cl->getObj(ObjectInstanceID(id1)));
  748. INTERFACE_CALL_IF_PRESENT(sy->o->tempOwner, showShipyardDialog, sy);
  749. }
  750. break;
  751. case THIEVES_GUILD:
  752. {
  753. //displays Thieves' Guild window (when hero enters Den of Thieves)
  754. const CGObjectInstance *obj = cl->getObj(ObjectInstanceID(id2));
  755. INTERFACE_CALL_IF_PRESENT(PlayerColor(id1), showThievesGuildWindow, obj);
  756. }
  757. break;
  758. case UNIVERSITY_WINDOW:
  759. {
  760. //displays University window (when hero enters University on adventure map)
  761. const IMarket *market = IMarket::castFrom(cl->getObj(ObjectInstanceID(id1)));
  762. const CGHeroInstance *hero = cl->getHero(ObjectInstanceID(id2));
  763. INTERFACE_CALL_IF_PRESENT(hero->tempOwner,showUniversityWindow, market, hero);
  764. }
  765. break;
  766. case MARKET_WINDOW:
  767. {
  768. //displays Thieves' Guild window (when hero enters Den of Thieves)
  769. const CGObjectInstance *obj = cl->getObj(ObjectInstanceID(id1));
  770. const CGHeroInstance *hero = cl->getHero(ObjectInstanceID(id2));
  771. const IMarket *market = IMarket::castFrom(obj);
  772. INTERFACE_CALL_IF_PRESENT(cl->getTile(obj->visitablePos())->visitableObjects.back()->tempOwner, showMarketWindow, market, hero);
  773. }
  774. break;
  775. case HILL_FORT_WINDOW:
  776. {
  777. //displays Hill fort window
  778. const CGObjectInstance *obj = cl->getObj(ObjectInstanceID(id1));
  779. const CGHeroInstance *hero = cl->getHero(ObjectInstanceID(id2));
  780. INTERFACE_CALL_IF_PRESENT(cl->getTile(obj->visitablePos())->visitableObjects.back()->tempOwner, showHillFortWindow, obj, hero);
  781. }
  782. break;
  783. case PUZZLE_MAP:
  784. {
  785. INTERFACE_CALL_IF_PRESENT(PlayerColor(id1), showPuzzleMap);
  786. }
  787. break;
  788. case TAVERN_WINDOW:
  789. const CGObjectInstance *obj1 = cl->getObj(ObjectInstanceID(id1)),
  790. *obj2 = cl->getObj(ObjectInstanceID(id2));
  791. INTERFACE_CALL_IF_PRESENT(obj1->tempOwner, showTavernWindow, obj2);
  792. break;
  793. }
  794. }
  795. void CenterView::applyCl(CClient *cl)
  796. {
  797. INTERFACE_CALL_IF_PRESENT (player, centerView, pos, focusTime);
  798. }
  799. void NewObject::applyCl(CClient *cl)
  800. {
  801. cl->invalidatePaths();
  802. const CGObjectInstance *obj = cl->getObj(id);
  803. CGI->mh->printObject(obj, true);
  804. for(auto i=cl->playerint.begin(); i!=cl->playerint.end(); i++)
  805. {
  806. if(GS(cl)->isVisible(obj, i->first))
  807. i->second->newObject(obj);
  808. }
  809. }
  810. void SetAvailableArtifacts::applyCl(CClient *cl)
  811. {
  812. if(id < 0) //artifact merchants globally
  813. {
  814. for(auto & elem : cl->playerint)
  815. elem.second->availableArtifactsChanged(nullptr);
  816. }
  817. else
  818. {
  819. const CGBlackMarket *bm = dynamic_cast<const CGBlackMarket *>(cl->getObj(ObjectInstanceID(id)));
  820. assert(bm);
  821. INTERFACE_CALL_IF_PRESENT(cl->getTile(bm->visitablePos())->visitableObjects.back()->tempOwner, availableArtifactsChanged, bm);
  822. }
  823. }
  824. void TradeComponents::applyCl(CClient *cl)
  825. {///Shop handler
  826. switch (CGI->mh->map->objects.at(objectid)->ID)
  827. {
  828. case Obj::BLACK_MARKET:
  829. break;
  830. case Obj::TAVERN:
  831. break;
  832. case Obj::DEN_OF_THIEVES:
  833. break;
  834. case Obj::TRADING_POST_SNOW:
  835. break;
  836. default:
  837. logNetwork->warnStream() << "Shop type not supported!";
  838. }
  839. }