NetPacksClient.cpp 24 KB

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