NetPacksClient.cpp 25 KB

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