2
0

NetPacksClient.cpp 27 KB

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