NetPacksClient.cpp 27 KB

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