NetPacksClient.cpp 25 KB

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