NetPacksClient.cpp 24 KB

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