NetPacksClient.cpp 27 KB

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