2
0

NetPacksClient.cpp 27 KB

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