NetPacksClient.cpp 26 KB

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