NetPacksClient.cpp 23 KB

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