NetPacksClient.cpp 25 KB

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