NetPacksClient.cpp 25 KB

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