NetPacksClient.cpp 22 KB

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