NetPacksClient.cpp 27 KB

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