NetPacksClient.cpp 22 KB

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