NetPacksClient.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850
  1. #include "../lib/NetPacks.h"
  2. #include "../CCallback.h"
  3. #include "Client.h"
  4. #include "CPlayerInterface.h"
  5. #include "CGameInfo.h"
  6. #include "../lib/Connection.h"
  7. #include "../lib/CGeneralTextHandler.h"
  8. #include "../lib/CDefObjInfoHandler.h"
  9. #include "../lib/CHeroHandler.h"
  10. #include "../lib/CObjectHandler.h"
  11. #include "../lib/VCMI_Lib.h"
  12. #include "../lib/map.h"
  13. #include "../lib/VCMIDirs.h"
  14. #include "../lib/CSpellHandler.h"
  15. #include "CSoundBase.h"
  16. #include "mapHandler.h"
  17. #include "GUIClasses.h"
  18. #include <boost/bind.hpp>
  19. #include <boost/foreach.hpp>
  20. #include <boost/thread.hpp>
  21. #include <boost/thread/shared_mutex.hpp>
  22. #include "CConfigHandler.h"
  23. #include "SDL_Extensions.h"
  24. #include "CBattleInterface.h"
  25. #include "../lib/CCampaignHandler.h"
  26. //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,stackChagedCount, sl, count, absoluteValue);
  111. }
  112. void SetStackType::applyCl( CClient *cl )
  113. {
  114. INTERFACE_CALL_IF_PRESENT(sl.army->tempOwner,stackChangedType,sl, *type);
  115. }
  116. void EraseStack::applyCl( CClient *cl )
  117. {
  118. INTERFACE_CALL_IF_PRESENT(sl.army->tempOwner,stacksErased,sl);
  119. }
  120. void SwapStacks::applyCl( CClient *cl )
  121. {
  122. INTERFACE_CALL_IF_PRESENT(sl1.army->tempOwner,stacksSwapped, sl1, sl2);
  123. if(sl1.army->tempOwner != sl2.army->tempOwner)
  124. INTERFACE_CALL_IF_PRESENT(sl2.army->tempOwner,stacksSwapped, sl1, sl2);
  125. }
  126. void InsertNewStack::applyCl( CClient *cl )
  127. {
  128. INTERFACE_CALL_IF_PRESENT(sl.army->tempOwner,newStackInserted,sl, *sl.getStack());
  129. }
  130. void RebalanceStacks::applyCl( CClient *cl )
  131. {
  132. INTERFACE_CALL_IF_PRESENT(src.army->tempOwner, stacksRebalanced, src, dst, count);
  133. if(src.army->tempOwner != dst.army->tempOwner)
  134. INTERFACE_CALL_IF_PRESENT(dst.army->tempOwner,stacksRebalanced, src, dst, count);
  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. cl->battleStarted(info);
  415. }
  416. void BattleNextRound::applyFirstCl(CClient *cl)
  417. {
  418. INTERFACE_CALL_IF_PRESENT(GS(cl)->curB->side1,battleNewRoundFirst,round);
  419. INTERFACE_CALL_IF_PRESENT(GS(cl)->curB->side2,battleNewRoundFirst,round);
  420. }
  421. void BattleNextRound::applyCl( CClient *cl )
  422. {
  423. INTERFACE_CALL_IF_PRESENT(GS(cl)->curB->side1,battleNewRound,round);
  424. INTERFACE_CALL_IF_PRESENT(GS(cl)->curB->side2,battleNewRound,round);
  425. }
  426. void BattleSetActiveStack::applyCl( CClient *cl )
  427. {
  428. CStack * activated = GS(cl)->curB->getStack(stack);
  429. int playerToCall = -1; //player that will move activated stack
  430. if( activated->hasBonusOfType(Bonus::HYPNOTIZED) )
  431. {
  432. playerToCall = ( GS(cl)->curB->side1 == activated->owner ? GS(cl)->curB->side2 : GS(cl)->curB->side1 );
  433. }
  434. else
  435. {
  436. playerToCall = activated->owner;
  437. }
  438. if( vstd::contains(cl->playerint, playerToCall) )
  439. boost::thread( boost::bind(&CClient::waitForMoveAndSend, cl, playerToCall) );
  440. }
  441. void BattleResult::applyFirstCl( CClient *cl )
  442. {
  443. if(cl->playerint.find(GS(cl)->curB->side1) != cl->playerint.end())
  444. cl->playerint[GS(cl)->curB->side1]->battleEnd(this);
  445. if(cl->playerint.find(GS(cl)->curB->side2) != cl->playerint.end())
  446. cl->playerint[GS(cl)->curB->side2]->battleEnd(this);
  447. }
  448. void BattleStackMoved::applyFirstCl( CClient *cl )
  449. {
  450. const CStack * movedStack = GS(cl)->curB->getStack(stack);
  451. INTERFACE_CALL_IF_PRESENT(GS(cl)->curB->side1,battleStackMoved,movedStack,tile,distance,ending);
  452. INTERFACE_CALL_IF_PRESENT(GS(cl)->curB->side2,battleStackMoved,movedStack,tile,distance,ending);
  453. }
  454. void BattleStackAttacked::applyCl( CClient *cl )
  455. {
  456. std::vector<BattleStackAttacked> bsa;
  457. bsa.push_back(*this);
  458. INTERFACE_CALL_IF_PRESENT(GS(cl)->curB->side1,battleStacksAttacked,bsa);
  459. INTERFACE_CALL_IF_PRESENT(GS(cl)->curB->side2,battleStacksAttacked,bsa);
  460. }
  461. void BattleAttack::applyFirstCl( CClient *cl )
  462. {
  463. if(cl->playerint.find(GS(cl)->curB->side1) != cl->playerint.end())
  464. cl->playerint[GS(cl)->curB->side1]->battleAttack(this);
  465. if(cl->playerint.find(GS(cl)->curB->side2) != cl->playerint.end())
  466. cl->playerint[GS(cl)->curB->side2]->battleAttack(this);
  467. for (int g=0; g<bsa.size(); ++g)
  468. {
  469. for (int z=0; z<bsa[g].healedStacks.size(); ++z)
  470. {
  471. bsa[g].healedStacks[z].applyCl(cl);
  472. }
  473. }
  474. }
  475. void BattleAttack::applyCl( CClient *cl )
  476. {
  477. INTERFACE_CALL_IF_PRESENT(GS(cl)->curB->side1,battleStacksAttacked,bsa);
  478. INTERFACE_CALL_IF_PRESENT(GS(cl)->curB->side2,battleStacksAttacked,bsa);
  479. }
  480. void StartAction::applyFirstCl( CClient *cl )
  481. {
  482. cl->curbaction = new BattleAction(ba);
  483. if(cl->playerint.find(GS(cl)->curB->side1) != cl->playerint.end())
  484. cl->playerint[GS(cl)->curB->side1]->actionStarted(&ba);
  485. if(cl->playerint.find(GS(cl)->curB->side2) != cl->playerint.end())
  486. cl->playerint[GS(cl)->curB->side2]->actionStarted(&ba);
  487. }
  488. void BattleSpellCast::applyCl( CClient *cl )
  489. {
  490. if(cl->playerint.find(GS(cl)->curB->side1) != cl->playerint.end())
  491. cl->playerint[GS(cl)->curB->side1]->battleSpellCast(this);
  492. if(cl->playerint.find(GS(cl)->curB->side2) != cl->playerint.end())
  493. cl->playerint[GS(cl)->curB->side2]->battleSpellCast(this);
  494. if(id >= 66 && id <= 69) //elemental summoning
  495. {
  496. if(cl->playerint.find(GS(cl)->curB->side1) != cl->playerint.end())
  497. cl->playerint[GS(cl)->curB->side1]->battleNewStackAppeared(GS(cl)->curB->stacks[GS(cl)->curB->stacks.size() - 1]);
  498. if(cl->playerint.find(GS(cl)->curB->side2) != cl->playerint.end())
  499. cl->playerint[GS(cl)->curB->side2]->battleNewStackAppeared(GS(cl)->curB->stacks[GS(cl)->curB->stacks.size() - 1]);
  500. }
  501. }
  502. void SetStackEffect::applyCl( CClient *cl )
  503. {
  504. BattleSpellCast sc;
  505. sc.id = effect.id;
  506. sc.side = 3; //doesn't matter
  507. sc.skill = effect.val;
  508. //informing about effects
  509. if(cl->playerint.find(GS(cl)->curB->side1) != cl->playerint.end())
  510. cl->playerint[GS(cl)->curB->side1]->battleStacksEffectsSet(*this);
  511. if(cl->playerint.find(GS(cl)->curB->side2) != cl->playerint.end())
  512. cl->playerint[GS(cl)->curB->side2]->battleStacksEffectsSet(*this);
  513. }
  514. void StacksInjured::applyCl( CClient *cl )
  515. {
  516. INTERFACE_CALL_IF_PRESENT(GS(cl)->curB->side1,battleStacksAttacked,stacks);
  517. INTERFACE_CALL_IF_PRESENT(GS(cl)->curB->side2,battleStacksAttacked,stacks);
  518. }
  519. void BattleResultsApplied::applyCl( CClient *cl )
  520. {
  521. INTERFACE_CALL_IF_PRESENT(player1,battleResultsApplied);
  522. INTERFACE_CALL_IF_PRESENT(player2,battleResultsApplied);
  523. }
  524. void StacksHealedOrResurrected::applyCl( CClient *cl )
  525. {
  526. std::vector<std::pair<ui32, ui32> > shiftedHealed;
  527. for(int v=0; v<healedStacks.size(); ++v)
  528. {
  529. shiftedHealed.push_back(std::make_pair(healedStacks[v].stackID, healedStacks[v].healedHP));
  530. }
  531. INTERFACE_CALL_IF_PRESENT(GS(cl)->curB->side1, battleStacksHealedRes, shiftedHealed, lifeDrain, drainedFrom);
  532. INTERFACE_CALL_IF_PRESENT(GS(cl)->curB->side2, battleStacksHealedRes, shiftedHealed, lifeDrain, drainedFrom);
  533. }
  534. void ObstaclesRemoved::applyCl( CClient *cl )
  535. {
  536. //inform interfaces about removed obstacles
  537. INTERFACE_CALL_IF_PRESENT(GS(cl)->curB->side1, battleObstaclesRemoved, obstacles);
  538. INTERFACE_CALL_IF_PRESENT(GS(cl)->curB->side2, battleObstaclesRemoved, obstacles);
  539. }
  540. void CatapultAttack::applyCl( CClient *cl )
  541. {
  542. //inform interfaces about catapult attack
  543. INTERFACE_CALL_IF_PRESENT(GS(cl)->curB->side1, battleCatapultAttacked, *this);
  544. INTERFACE_CALL_IF_PRESENT(GS(cl)->curB->side2, battleCatapultAttacked, *this);
  545. }
  546. void BattleStacksRemoved::applyCl( CClient *cl )
  547. {
  548. //inform interfaces about removed stacks
  549. INTERFACE_CALL_IF_PRESENT(GS(cl)->curB->side1, battleStacksRemoved, *this);
  550. INTERFACE_CALL_IF_PRESENT(GS(cl)->curB->side2, battleStacksRemoved, *this);
  551. }
  552. CGameState* CPackForClient::GS( CClient *cl )
  553. {
  554. return cl->gs;
  555. }
  556. void EndAction::applyCl( CClient *cl )
  557. {
  558. INTERFACE_CALL_IF_PRESENT(GS(cl)->curB->side1,actionFinished,cl->curbaction);
  559. INTERFACE_CALL_IF_PRESENT(GS(cl)->curB->side2,actionFinished,cl->curbaction);
  560. delete cl->curbaction;
  561. cl->curbaction = NULL;
  562. }
  563. void PackageApplied::applyCl( CClient *cl )
  564. {
  565. ui8 player = GS(cl)->currentPlayer;
  566. INTERFACE_CALL_IF_PRESENT(player, requestRealized, this);
  567. if(cl->waitingRequest.get())
  568. cl->waitingRequest.setn(false);
  569. }
  570. void SystemMessage::applyCl( CClient *cl )
  571. {
  572. std::ostringstream str;
  573. str << "System message: " << text;
  574. tlog4 << str.str() << std::endl;
  575. if(LOCPLINT)
  576. LOCPLINT->cingconsole->print(str.str());
  577. }
  578. void PlayerBlocked::applyCl( CClient *cl )
  579. {
  580. INTERFACE_CALL_IF_PRESENT(player,playerBlocked,reason);
  581. }
  582. void YourTurn::applyCl( CClient *cl )
  583. {
  584. INTERFACE_CALL_IF_PRESENT(player,yourTurn);
  585. }
  586. void SaveGame::applyCl(CClient *cl)
  587. {
  588. CSaveFile save(GVCMIDirs.UserPath + "/Games/" + fname + ".vcgm1");
  589. save << *cl;
  590. }
  591. void PlayerMessage::applyCl(CClient *cl)
  592. {
  593. std::ostringstream str;
  594. str << "Player "<<(int)player<<" sends a message: " << text;
  595. tlog4 << str.str() << std::endl;
  596. if(LOCPLINT)
  597. LOCPLINT->cingconsole->print(str.str());
  598. }
  599. void SetSelection::applyCl(CClient *cl)
  600. {
  601. const CGHeroInstance *h = cl->getHero(id);
  602. if(!h)
  603. return;
  604. //CPackForClient::GS(cl)->calculatePaths(h, *cl->pathInfo);
  605. }
  606. void ShowInInfobox::applyCl(CClient *cl)
  607. {
  608. SComponent sc(c);
  609. text.toString(sc.description);
  610. if(vstd::contains(cl->playerint, player) && cl->playerint[player]->human)
  611. {
  612. static_cast<CPlayerInterface*>(cl->playerint[player])->showComp(sc);
  613. }
  614. }
  615. void AdvmapSpellCast::applyCl(CClient *cl)
  616. {
  617. cl->playerint[caster->getOwner()]->advmapSpellCast(caster, spellID);
  618. }
  619. void OpenWindow::applyCl(CClient *cl)
  620. {
  621. switch(window)
  622. {
  623. case EXCHANGE_WINDOW:
  624. {
  625. const CGHeroInstance *h = cl->getHero(id1);
  626. const CGObjectInstance *h2 = cl->getHero(id2);
  627. assert(h && h2);
  628. INTERFACE_CALL_IF_PRESENT(h->tempOwner,heroExchangeStarted, id1, id2);
  629. }
  630. break;
  631. case RECRUITMENT_FIRST:
  632. case RECRUITMENT_ALL:
  633. {
  634. const CGDwelling *dw = dynamic_cast<const CGDwelling*>(cl->getObj(id1));
  635. const CArmedInstance *dst = dynamic_cast<const CArmedInstance*>(cl->getObj(id2));
  636. INTERFACE_CALL_IF_PRESENT(dst->tempOwner,showRecruitmentDialog, dw, dst, window == RECRUITMENT_FIRST ? 0 : -1);
  637. }
  638. break;
  639. case SHIPYARD_WINDOW:
  640. {
  641. const IShipyard *sy = IShipyard::castFrom(cl->getObj(id1));
  642. INTERFACE_CALL_IF_PRESENT(sy->o->tempOwner, showShipyardDialog, sy);
  643. }
  644. break;
  645. case THIEVES_GUILD:
  646. {
  647. //displays Thieves' Guild window (when hero enters Den of Thieves)
  648. const CGObjectInstance *obj = cl->getObj(id1);
  649. GH.pushInt( new CThievesGuildWindow(obj) );
  650. }
  651. break;
  652. case UNIVERSITY_WINDOW:
  653. {
  654. //displays University window (when hero enters University on adventure map)
  655. const IMarket *market = IMarket::castFrom(cl->getObj(id1));
  656. const CGHeroInstance *hero = cl->getHero(id2);
  657. INTERFACE_CALL_IF_PRESENT(hero->tempOwner,showUniversityWindow, market, hero);
  658. }
  659. break;
  660. case MARKET_WINDOW:
  661. {
  662. //displays Thieves' Guild window (when hero enters Den of Thieves)
  663. const CGObjectInstance *obj = cl->getObj(id1);
  664. const CGHeroInstance *hero = cl->getHero(id2);
  665. const IMarket *market = IMarket::castFrom(obj);
  666. INTERFACE_CALL_IF_PRESENT(cl->getTile(obj->visitablePos())->visitableObjects.back()->tempOwner, showMarketWindow, market, hero);
  667. }
  668. break;
  669. case HILL_FORT_WINDOW:
  670. {
  671. //displays Hill fort window
  672. const CGObjectInstance *obj = cl->getObj(id1);
  673. const CGHeroInstance *hero = cl->getHero(id2);
  674. INTERFACE_CALL_IF_PRESENT(cl->getTile(obj->visitablePos())->visitableObjects.back()->tempOwner, showHillFortWindow, obj, hero);
  675. }
  676. break;
  677. case PUZZLE_MAP:
  678. {
  679. INTERFACE_CALL_IF_PRESENT(id1, showPuzzleMap);
  680. }
  681. break;
  682. case TAVERN_WINDOW:
  683. const CGObjectInstance *obj1 = cl->getObj(id1),
  684. *obj2 = cl->getObj(id2);
  685. INTERFACE_CALL_IF_PRESENT(obj1->tempOwner, showTavernWindow, obj2);
  686. break;
  687. }
  688. }
  689. void CenterView::applyCl(CClient *cl)
  690. {
  691. INTERFACE_CALL_IF_PRESENT (player, centerView, pos, focusTime);
  692. }
  693. void NewObject::applyCl(CClient *cl)
  694. {
  695. cl->updatePaths();
  696. const CGObjectInstance *obj = cl->getObj(id);
  697. //notify interfaces about move
  698. for(std::map<ui8, CGameInterface*>::iterator i=cl->playerint.begin();i!=cl->playerint.end();i++)
  699. {
  700. //TODO: check if any covered tile is visible
  701. if(i->first >= PLAYER_LIMIT) continue;
  702. if(GS(cl)->getPlayerTeam(i->first)->fogOfWarMap[obj->pos.x][obj->pos.y][obj->pos.z])
  703. {
  704. i->second->newObject(obj);
  705. }
  706. }
  707. }
  708. void SetAvailableArtifacts::applyCl(CClient *cl)
  709. {
  710. if(id < 0) //artifact merchants globally
  711. {
  712. for(std::map<ui8, CGameInterface*>::iterator i=cl->playerint.begin();i!=cl->playerint.end();i++)
  713. i->second->availableArtifactsChanged(NULL);
  714. }
  715. else
  716. {
  717. const CGBlackMarket *bm = dynamic_cast<const CGBlackMarket *>(cl->getObj(id));
  718. assert(bm);
  719. INTERFACE_CALL_IF_PRESENT(cl->getTile(bm->visitablePos())->visitableObjects.back()->tempOwner, availableArtifactsChanged, bm);
  720. }
  721. }
  722. void TradeComponents::applyCl(CClient *cl)
  723. {///Shop handler
  724. switch (CGI->mh->map->objects[objectid]->ID)
  725. {
  726. case 7: //Black Market
  727. break;
  728. case 95: //Tavern
  729. break;
  730. case 97: //Den of Thieves
  731. break;
  732. case 221: //Trading Post
  733. break;
  734. default:
  735. tlog2 << "Shop type not supported! \n";
  736. }
  737. }