NetPacksClient.cpp 23 KB

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