NetPacksClient.cpp 27 KB

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