NetPacksClient.cpp 27 KB

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