NetPacksClient.cpp 23 KB

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