NetPacksClient.cpp 24 KB

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