NetPacksClient.cpp 26 KB

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