NetPacksClient.cpp 27 KB

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