NetPacksClient.cpp 24 KB

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