NetPacksClient.cpp 27 KB

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