NetPacksClient.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950
  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. }while(0)
  44. #define INTERFACE_CALL_IF_PRESENT(player,function,...) \
  45. do \
  46. { \
  47. CALL_ONLY_THAT_INTERFACE(player, function, __VA_ARGS__);\
  48. CALL_IN_PRIVILAGED_INTS(function, __VA_ARGS__); \
  49. } while(0)
  50. #define CALL_ONLY_THAT_BATTLE_INTERFACE(player,function, ...) \
  51. do \
  52. { \
  53. if(vstd::contains(cl->battleints,player)) \
  54. cl->battleints[player]->function(__VA_ARGS__); \
  55. \
  56. if(cl->additionalBattleInts.count(player)) \
  57. BOOST_FOREACH(auto bInt, cl->additionalBattleInts[player])\
  58. bInt->function(__VA_ARGS__); \
  59. } while (0);
  60. #define BATTLE_INTERFACE_CALL_RECEIVERS(function,...) \
  61. do \
  62. { \
  63. BOOST_FOREACH(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], function, __VA_ARGS__) \
  82. CALL_ONLY_THAT_BATTLE_INTERFACE(GS(cl)->curB->sides[1], 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. BOOST_FOREACH(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(hero->tempOwner, 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, victory);
  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. int3 pos = o->visitablePos();
  288. //notify interfaces about removal
  289. for(auto i=cl->playerint.begin(); i!=cl->playerint.end(); i++)
  290. {
  291. if(GS(cl)->isVisible(o, i->first))
  292. i->second->objectRemoved(o);
  293. }
  294. }
  295. void RemoveObject::applyCl( CClient *cl )
  296. {
  297. cl->invalidatePaths();
  298. }
  299. void TryMoveHero::applyFirstCl( CClient *cl )
  300. {
  301. CGHeroInstance *h = GS(cl)->getHero(id);
  302. //check if playerint will have the knowledge about movement - if not, directly update maphandler
  303. for(auto i=cl->playerint.begin(); i!=cl->playerint.end(); i++)
  304. {
  305. if(i->first >= PlayerColor::PLAYER_LIMIT)
  306. continue;
  307. TeamState *t = GS(cl)->getPlayerTeam(i->first);
  308. if((t->fogOfWarMap[start.x-1][start.y][start.z] || t->fogOfWarMap[end.x-1][end.y][end.z])
  309. && GS(cl)->getPlayer(i->first)->human)
  310. humanKnows = true;
  311. }
  312. if(result == TELEPORTATION || result == EMBARK || result == DISEMBARK || !humanKnows)
  313. CGI->mh->removeObject(h);
  314. if(result == DISEMBARK)
  315. CGI->mh->printObject(h->boat);
  316. }
  317. void TryMoveHero::applyCl( CClient *cl )
  318. {
  319. const CGHeroInstance *h = cl->getHero(id);
  320. cl->invalidatePaths();
  321. if(result == TELEPORTATION || result == EMBARK || result == DISEMBARK)
  322. {
  323. CGI->mh->printObject(h);
  324. }
  325. if(result == EMBARK)
  326. CGI->mh->hideObject(h->boat);
  327. PlayerColor player = h->tempOwner;
  328. BOOST_FOREACH(auto &i, cl->playerint)
  329. if(cl->getPlayerRelations(i.first, player) != PlayerRelations::ENEMIES)
  330. i.second->tileRevealed(fowRevealed);
  331. //notify interfaces about move
  332. for(auto i=cl->playerint.begin(); i!=cl->playerint.end(); i++)
  333. {
  334. if(i->first >= PlayerColor::PLAYER_LIMIT) continue;
  335. TeamState *t = GS(cl)->getPlayerTeam(i->first);
  336. if(t->fogOfWarMap[start.x-1][start.y][start.z] || t->fogOfWarMap[end.x-1][end.y][end.z])
  337. {
  338. i->second->heroMoved(*this);
  339. }
  340. }
  341. if(!humanKnows) //maphandler didn't get update from playerint, do it now
  342. { //TODO: restructure nicely
  343. CGI->mh->printObject(h);
  344. }
  345. }
  346. void NewStructures::applyCl( CClient *cl )
  347. {
  348. CGTownInstance *town = GS(cl)->getTown(tid);
  349. BOOST_FOREACH(const auto & id, bid)
  350. {
  351. if(id == BuildingID::CAPITOL) //fort or capitol
  352. {
  353. town->defInfo = const_cast<CGDefInfo*>(CGI->dobjinfo->capitols[town->subID].get());
  354. }
  355. if(id == BuildingID::FORT)
  356. {
  357. town->defInfo = const_cast<CGDefInfo*>(CGI->dobjinfo->gobjs[Obj::TOWN][town->subID].get());
  358. }
  359. if(vstd::contains(cl->playerint,town->tempOwner))
  360. cl->playerint[town->tempOwner]->buildChanged(town,id,1);
  361. }
  362. }
  363. void RazeStructures::applyCl (CClient *cl)
  364. {
  365. CGTownInstance *town = GS(cl)->getTown(tid);
  366. BOOST_FOREACH(const auto & id, bid)
  367. {
  368. if (id == BuildingID::CAPITOL) //fort or capitol
  369. {
  370. town->defInfo = const_cast<CGDefInfo*>(CGI->dobjinfo->gobjs[Obj::TOWN][town->subID].get());
  371. }
  372. if(vstd::contains (cl->playerint,town->tempOwner))
  373. cl->playerint[town->tempOwner]->buildChanged (town,id,2);
  374. }
  375. }
  376. void SetAvailableCreatures::applyCl( CClient *cl )
  377. {
  378. const CGDwelling *dw = static_cast<const CGDwelling*>(cl->getObj(tid));
  379. //inform order about the change
  380. PlayerColor p;
  381. if(dw->ID == Obj::WAR_MACHINE_FACTORY) //War Machines Factory is not flaggable, it's "owned" by visitor
  382. p = cl->getTile(dw->visitablePos())->visitableObjects.back()->tempOwner;
  383. else
  384. p = dw->tempOwner;
  385. INTERFACE_CALL_IF_PRESENT(p, availableCreaturesChanged, dw);
  386. }
  387. void SetHeroesInTown::applyCl( CClient *cl )
  388. {
  389. CGTownInstance *t = GS(cl)->getTown(tid);
  390. CGHeroInstance *hGarr = GS(cl)->getHero(this->garrison);
  391. CGHeroInstance *hVisit = GS(cl)->getHero(this->visiting);
  392. std::set<PlayerColor> playersToNotify;
  393. if(vstd::contains(cl->playerint,t->tempOwner)) // our town
  394. playersToNotify.insert(t->tempOwner);
  395. if (hGarr && vstd::contains(cl->playerint, hGarr->tempOwner))
  396. playersToNotify.insert(hGarr->tempOwner);
  397. if (hVisit && vstd::contains(cl->playerint, hVisit->tempOwner))
  398. playersToNotify.insert(hVisit->tempOwner);
  399. BOOST_FOREACH(auto playerID, playersToNotify)
  400. cl->playerint[playerID]->heroInGarrisonChange(t);
  401. }
  402. // void SetHeroArtifacts::applyCl( CClient *cl )
  403. // {
  404. // // CGHeroInstance *h = GS(cl)->getHero(hid);
  405. // // CGameInterface *player = (vstd::contains(cl->playerint,h->tempOwner) ? cl->playerint[h->tempOwner] : NULL);
  406. // // if(!player)
  407. // // return;
  408. //
  409. // //h->recreateArtBonuses();
  410. // //player->heroArtifactSetChanged(h);
  411. //
  412. // // BOOST_FOREACH(Bonus bonus, gained)
  413. // // {
  414. // // player->heroBonusChanged(h,bonus,true);
  415. // // }
  416. // // BOOST_FOREACH(Bonus bonus, lost)
  417. // // {
  418. // // player->heroBonusChanged(h,bonus,false);
  419. // // }
  420. // }
  421. void HeroRecruited::applyCl( CClient *cl )
  422. {
  423. CGHeroInstance *h = GS(cl)->map->heroesOnMap.back();
  424. if(h->subID != hid)
  425. {
  426. logNetwork->errorStream() << "Something wrong with hero recruited!";
  427. }
  428. CGI->mh->initHeroDef(h);
  429. CGI->mh->printObject(h);
  430. if(vstd::contains(cl->playerint,h->tempOwner))
  431. {
  432. cl->playerint[h->tempOwner]->heroCreated(h);
  433. if(const CGTownInstance *t = GS(cl)->getTown(tid))
  434. cl->playerint[h->tempOwner]->heroInGarrisonChange(t);
  435. }
  436. }
  437. void GiveHero::applyCl( CClient *cl )
  438. {
  439. CGHeroInstance *h = GS(cl)->getHero(id);
  440. CGI->mh->initHeroDef(h);
  441. CGI->mh->printObject(h);
  442. cl->playerint[h->tempOwner]->heroCreated(h);
  443. }
  444. void GiveHero::applyFirstCl( CClient *cl )
  445. {
  446. CGI->mh->hideObject(GS(cl)->getHero(id));
  447. }
  448. void InfoWindow::applyCl( CClient *cl )
  449. {
  450. std::vector<Component*> comps;
  451. for(size_t i=0;i<components.size();i++)
  452. {
  453. comps.push_back(&components[i]);
  454. }
  455. std::string str;
  456. text.toString(str);
  457. if(vstd::contains(cl->playerint,player))
  458. cl->playerint[player]->showInfoDialog(str,comps,(soundBase::soundID)soundID);
  459. else
  460. logNetwork->warnStream() << "We received InfoWindow for not our player...";
  461. }
  462. void SetObjectProperty::applyCl( CClient *cl )
  463. {
  464. //inform all players that see this object
  465. for(auto it = cl->playerint.cbegin(); it != cl->playerint.cend(); ++it)
  466. {
  467. if(GS(cl)->isVisible(GS(cl)->getObjInstance(id), it->first))
  468. INTERFACE_CALL_IF_PRESENT(it->first, objectPropertyChanged, this);
  469. }
  470. }
  471. void HeroLevelUp::applyCl( CClient *cl )
  472. {
  473. //INTERFACE_CALL_IF_PRESENT(h->tempOwner, heroGotLevel, h, primskill, skills, id);
  474. if(vstd::contains(cl->playerint,hero->tempOwner))
  475. {
  476. cl->playerint[hero->tempOwner]->heroGotLevel(hero, primskill, skills, queryID);
  477. }
  478. //else
  479. // cb->selectionMade(0, queryID);
  480. }
  481. void CommanderLevelUp::applyCl( CClient *cl )
  482. {
  483. const CCommanderInstance * commander = hero->commander;
  484. assert (commander);
  485. PlayerColor player = hero->tempOwner;
  486. if (commander->armyObj && vstd::contains(cl->playerint, player)) //is it possible for Commander to exist beyond armed instance?
  487. {
  488. cl->playerint[player]->commanderGotLevel(commander, skills, queryID);
  489. }
  490. }
  491. void BlockingDialog::applyCl( CClient *cl )
  492. {
  493. std::string str;
  494. text.toString(str);
  495. if(vstd::contains(cl->playerint,player))
  496. cl->playerint[player]->showBlockingDialog(str,components,queryID,(soundBase::soundID)soundID,selection(),cancel());
  497. else
  498. logNetwork->warnStream() << "We received YesNoDialog for not our player...";
  499. }
  500. void GarrisonDialog::applyCl(CClient *cl)
  501. {
  502. const CGHeroInstance *h = cl->getHero(hid);
  503. const CArmedInstance *obj = static_cast<const CArmedInstance*>(cl->getObj(objid));
  504. if(!vstd::contains(cl->playerint,h->getOwner()))
  505. return;
  506. cl->playerint[h->getOwner()]->showGarrisonDialog(obj,h,removableUnits,queryID);
  507. }
  508. void ExchangeDialog::applyCl(CClient *cl)
  509. {
  510. assert(heroes[0] && heroes[1]);
  511. INTERFACE_CALL_IF_PRESENT(heroes[0]->tempOwner, heroExchangeStarted, heroes[0]->id, heroes[1]->id, queryID);
  512. }
  513. void BattleStart::applyCl( CClient *cl )
  514. {
  515. cl->battleStarted(info);
  516. }
  517. void BattleNextRound::applyFirstCl(CClient *cl)
  518. {
  519. BATTLE_INTERFACE_CALL_IF_PRESENT_FOR_BOTH_SIDES(battleNewRoundFirst,round);
  520. }
  521. void BattleNextRound::applyCl( CClient *cl )
  522. {
  523. BATTLE_INTERFACE_CALL_IF_PRESENT_FOR_BOTH_SIDES(battleNewRound,round);
  524. }
  525. void BattleSetActiveStack::applyCl( CClient *cl )
  526. {
  527. if(!askPlayerInterface)
  528. return;
  529. const CStack * activated = GS(cl)->curB->battleGetStackByID(stack);
  530. PlayerColor playerToCall; //player that will move activated stack
  531. if( activated->hasBonusOfType(Bonus::HYPNOTIZED) )
  532. {
  533. playerToCall = ( GS(cl)->curB->sides[0] == activated->owner ? GS(cl)->curB->sides[1] : GS(cl)->curB->sides[0] );
  534. }
  535. else
  536. {
  537. playerToCall = activated->owner;
  538. }
  539. if( vstd::contains(cl->battleints, playerToCall) )
  540. boost::thread( boost::bind(&CClient::waitForMoveAndSend, cl, playerToCall) );
  541. }
  542. void BattleTriggerEffect::applyCl(CClient * cl)
  543. {
  544. BATTLE_INTERFACE_CALL_IF_PRESENT_FOR_BOTH_SIDES(battleTriggerEffect, *this);
  545. }
  546. void BattleObstaclePlaced::applyCl(CClient * cl)
  547. {
  548. BATTLE_INTERFACE_CALL_IF_PRESENT_FOR_BOTH_SIDES(battleObstaclePlaced, *obstacle);
  549. }
  550. void BattleResult::applyFirstCl( CClient *cl )
  551. {
  552. BATTLE_INTERFACE_CALL_IF_PRESENT_FOR_BOTH_SIDES(battleEnd,this);
  553. cl->battleFinished();
  554. }
  555. void BattleStackMoved::applyFirstCl( CClient *cl )
  556. {
  557. const CStack * movedStack = GS(cl)->curB->battleGetStackByID(stack);
  558. BATTLE_INTERFACE_CALL_IF_PRESENT_FOR_BOTH_SIDES(battleStackMoved,movedStack,tilesToMove,distance);
  559. }
  560. //void BattleStackAttacked::( CClient *cl )
  561. void BattleStackAttacked::applyFirstCl( CClient *cl )
  562. {
  563. std::vector<BattleStackAttacked> bsa;
  564. bsa.push_back(*this);
  565. BATTLE_INTERFACE_CALL_IF_PRESENT_FOR_BOTH_SIDES(battleStacksAttacked,bsa);
  566. }
  567. void BattleAttack::applyFirstCl( CClient *cl )
  568. {
  569. BATTLE_INTERFACE_CALL_IF_PRESENT_FOR_BOTH_SIDES(battleAttack,this);
  570. for (int g=0; g<bsa.size(); ++g)
  571. {
  572. for (int z=0; z<bsa[g].healedStacks.size(); ++z)
  573. {
  574. bsa[g].healedStacks[z].applyCl(cl);
  575. }
  576. }
  577. }
  578. void BattleAttack::applyCl( CClient *cl )
  579. {
  580. BATTLE_INTERFACE_CALL_IF_PRESENT_FOR_BOTH_SIDES(battleStacksAttacked,bsa);
  581. }
  582. void StartAction::applyFirstCl( CClient *cl )
  583. {
  584. cl->curbaction = ba;
  585. BATTLE_INTERFACE_CALL_IF_PRESENT_FOR_BOTH_SIDES(actionStarted, ba);
  586. }
  587. void BattleSpellCast::applyCl( CClient *cl )
  588. {
  589. BATTLE_INTERFACE_CALL_IF_PRESENT_FOR_BOTH_SIDES(battleSpellCast,this);
  590. }
  591. void SetStackEffect::applyCl( CClient *cl )
  592. {
  593. //informing about effects
  594. BATTLE_INTERFACE_CALL_IF_PRESENT_FOR_BOTH_SIDES(battleStacksEffectsSet,*this);
  595. }
  596. void StacksInjured::applyCl( CClient *cl )
  597. {
  598. BATTLE_INTERFACE_CALL_IF_PRESENT_FOR_BOTH_SIDES(battleStacksAttacked,stacks);
  599. }
  600. void BattleResultsApplied::applyCl( CClient *cl )
  601. {
  602. INTERFACE_CALL_IF_PRESENT(player1, battleResultsApplied);
  603. INTERFACE_CALL_IF_PRESENT(player2, battleResultsApplied);
  604. INTERFACE_CALL_IF_PRESENT(PlayerColor::UNFLAGGABLE, battleResultsApplied);
  605. if(GS(cl)->initialOpts->mode == StartInfo::DUEL)
  606. {
  607. handleQuit();
  608. }
  609. }
  610. void StacksHealedOrResurrected::applyCl( CClient *cl )
  611. {
  612. std::vector<std::pair<ui32, ui32> > shiftedHealed;
  613. for(int v=0; v<healedStacks.size(); ++v)
  614. {
  615. shiftedHealed.push_back(std::make_pair(healedStacks[v].stackID, healedStacks[v].healedHP));
  616. }
  617. BATTLE_INTERFACE_CALL_IF_PRESENT_FOR_BOTH_SIDES(battleStacksHealedRes, shiftedHealed, lifeDrain, tentHealing, drainedFrom);
  618. }
  619. void ObstaclesRemoved::applyCl( CClient *cl )
  620. {
  621. //inform interfaces about removed obstacles
  622. BATTLE_INTERFACE_CALL_IF_PRESENT_FOR_BOTH_SIDES(battleObstaclesRemoved, obstacles);
  623. }
  624. void CatapultAttack::applyCl( CClient *cl )
  625. {
  626. //inform interfaces about catapult attack
  627. BATTLE_INTERFACE_CALL_IF_PRESENT_FOR_BOTH_SIDES(battleCatapultAttacked, *this);
  628. }
  629. void BattleStacksRemoved::applyCl( CClient *cl )
  630. {
  631. //inform interfaces about removed stacks
  632. BATTLE_INTERFACE_CALL_IF_PRESENT_FOR_BOTH_SIDES(battleStacksRemoved, *this);
  633. }
  634. void BattleStackAdded::applyCl( CClient *cl )
  635. {
  636. BATTLE_INTERFACE_CALL_IF_PRESENT_FOR_BOTH_SIDES(battleNewStackAppeared, GS(cl)->curB->stacks.back());
  637. }
  638. CGameState* CPackForClient::GS( CClient *cl )
  639. {
  640. return cl->gs;
  641. }
  642. void EndAction::applyCl( CClient *cl )
  643. {
  644. BATTLE_INTERFACE_CALL_IF_PRESENT_FOR_BOTH_SIDES(actionFinished, *cl->curbaction);
  645. cl->curbaction.reset();
  646. }
  647. void PackageApplied::applyCl( CClient *cl )
  648. {
  649. INTERFACE_CALL_IF_PRESENT(player, requestRealized, this);
  650. if(!cl->waitingRequest.tryRemovingElement(requestID))
  651. logNetwork->warnStream() << "Surprising server message!";
  652. }
  653. void SystemMessage::applyCl( CClient *cl )
  654. {
  655. std::ostringstream str;
  656. str << "System message: " << text;
  657. logNetwork->errorStream() << str.str(); // usually used to receive error messages from server
  658. if(LOCPLINT)
  659. LOCPLINT->cingconsole->print(str.str());
  660. }
  661. void PlayerBlocked::applyCl( CClient *cl )
  662. {
  663. INTERFACE_CALL_IF_PRESENT(player,playerBlocked,reason);
  664. }
  665. void YourTurn::applyCl( CClient *cl )
  666. {
  667. CALL_IN_ALL_INTERFACES(playerStartsTurn, player);
  668. CALL_ONLY_THAT_INTERFACE(player,yourTurn);
  669. }
  670. void SaveGame::applyCl(CClient *cl)
  671. {
  672. CFileInfo info(fname);
  673. CResourceHandler::get()->createResource(info.getStem() + ".vcgm1");
  674. try
  675. {
  676. CSaveFile save(CResourceHandler::get()->getResourceName(ResourceID(info.getStem(), EResType::CLIENT_SAVEGAME)));
  677. cl->saveCommonState(save);
  678. save << *cl;
  679. }
  680. catch(std::exception &e)
  681. {
  682. logNetwork->errorStream() << "Failed to save game:" << e.what();
  683. }
  684. }
  685. void PlayerMessage::applyCl(CClient *cl)
  686. {
  687. std::ostringstream str;
  688. str << "Player "<< player <<" sends a message: " << text;
  689. logNetwork->debugStream() << str.str();
  690. if(LOCPLINT)
  691. LOCPLINT->cingconsole->print(str.str());
  692. }
  693. void SetSelection::applyCl(CClient *cl)
  694. {
  695. const CGHeroInstance *h = cl->getHero(id);
  696. if(!h)
  697. return;
  698. //CPackForClient::GS(cl)->calculatePaths(h, *cl->pathInfo);
  699. }
  700. void ShowInInfobox::applyCl(CClient *cl)
  701. {
  702. INTERFACE_CALL_IF_PRESENT(player,showComp, c, text.toString());
  703. }
  704. void AdvmapSpellCast::applyCl(CClient *cl)
  705. {
  706. cl->invalidatePaths();
  707. //consider notifying other interfaces that see hero?
  708. INTERFACE_CALL_IF_PRESENT(caster->getOwner(),advmapSpellCast, caster, spellID);
  709. }
  710. void OpenWindow::applyCl(CClient *cl)
  711. {
  712. switch(window)
  713. {
  714. case RECRUITMENT_FIRST:
  715. case RECRUITMENT_ALL:
  716. {
  717. const CGDwelling *dw = dynamic_cast<const CGDwelling*>(cl->getObj(ObjectInstanceID(id1)));
  718. const CArmedInstance *dst = dynamic_cast<const CArmedInstance*>(cl->getObj(ObjectInstanceID(id2)));
  719. INTERFACE_CALL_IF_PRESENT(dst->tempOwner,showRecruitmentDialog, dw, dst, window == RECRUITMENT_FIRST ? 0 : -1);
  720. }
  721. break;
  722. case SHIPYARD_WINDOW:
  723. {
  724. const IShipyard *sy = IShipyard::castFrom(cl->getObj(ObjectInstanceID(id1)));
  725. INTERFACE_CALL_IF_PRESENT(sy->o->tempOwner, showShipyardDialog, sy);
  726. }
  727. break;
  728. case THIEVES_GUILD:
  729. {
  730. //displays Thieves' Guild window (when hero enters Den of Thieves)
  731. const CGObjectInstance *obj = cl->getObj(ObjectInstanceID(id2));
  732. INTERFACE_CALL_IF_PRESENT(PlayerColor(id1), showThievesGuildWindow, obj);
  733. }
  734. break;
  735. case UNIVERSITY_WINDOW:
  736. {
  737. //displays University window (when hero enters University on adventure map)
  738. const IMarket *market = IMarket::castFrom(cl->getObj(ObjectInstanceID(id1)));
  739. const CGHeroInstance *hero = cl->getHero(ObjectInstanceID(id2));
  740. INTERFACE_CALL_IF_PRESENT(hero->tempOwner,showUniversityWindow, market, hero);
  741. }
  742. break;
  743. case MARKET_WINDOW:
  744. {
  745. //displays Thieves' Guild window (when hero enters Den of Thieves)
  746. const CGObjectInstance *obj = cl->getObj(ObjectInstanceID(id1));
  747. const CGHeroInstance *hero = cl->getHero(ObjectInstanceID(id2));
  748. const IMarket *market = IMarket::castFrom(obj);
  749. INTERFACE_CALL_IF_PRESENT(cl->getTile(obj->visitablePos())->visitableObjects.back()->tempOwner, showMarketWindow, market, hero);
  750. }
  751. break;
  752. case HILL_FORT_WINDOW:
  753. {
  754. //displays Hill fort window
  755. const CGObjectInstance *obj = cl->getObj(ObjectInstanceID(id1));
  756. const CGHeroInstance *hero = cl->getHero(ObjectInstanceID(id2));
  757. INTERFACE_CALL_IF_PRESENT(cl->getTile(obj->visitablePos())->visitableObjects.back()->tempOwner, showHillFortWindow, obj, hero);
  758. }
  759. break;
  760. case PUZZLE_MAP:
  761. {
  762. INTERFACE_CALL_IF_PRESENT(PlayerColor(id1), showPuzzleMap);
  763. }
  764. break;
  765. case TAVERN_WINDOW:
  766. const CGObjectInstance *obj1 = cl->getObj(ObjectInstanceID(id1)),
  767. *obj2 = cl->getObj(ObjectInstanceID(id2));
  768. INTERFACE_CALL_IF_PRESENT(obj1->tempOwner, showTavernWindow, obj2);
  769. break;
  770. }
  771. }
  772. void CenterView::applyCl(CClient *cl)
  773. {
  774. INTERFACE_CALL_IF_PRESENT (player, centerView, pos, focusTime);
  775. }
  776. void NewObject::applyCl(CClient *cl)
  777. {
  778. cl->updatePaths();
  779. const CGObjectInstance *obj = cl->getObj(id);
  780. CGI->mh->printObject(obj);
  781. for(auto i=cl->playerint.begin(); i!=cl->playerint.end(); i++)
  782. {
  783. if(GS(cl)->isVisible(obj, i->first))
  784. i->second->newObject(obj);
  785. }
  786. }
  787. void SetAvailableArtifacts::applyCl(CClient *cl)
  788. {
  789. if(id < 0) //artifact merchants globally
  790. {
  791. for(auto i=cl->playerint.begin(); i!=cl->playerint.end(); i++)
  792. i->second->availableArtifactsChanged(NULL);
  793. }
  794. else
  795. {
  796. const CGBlackMarket *bm = dynamic_cast<const CGBlackMarket *>(cl->getObj(ObjectInstanceID(id)));
  797. assert(bm);
  798. INTERFACE_CALL_IF_PRESENT(cl->getTile(bm->visitablePos())->visitableObjects.back()->tempOwner, availableArtifactsChanged, bm);
  799. }
  800. }
  801. void TradeComponents::applyCl(CClient *cl)
  802. {///Shop handler
  803. switch (CGI->mh->map->objects[objectid]->ID)
  804. {
  805. case Obj::BLACK_MARKET:
  806. break;
  807. case Obj::TAVERN:
  808. break;
  809. case Obj::DEN_OF_THIEVES:
  810. break;
  811. case Obj::TRADING_POST_SNOW:
  812. break;
  813. default:
  814. logNetwork->warnStream() << "Shop type not supported!";
  815. }
  816. }