NetPacksClient.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  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 "../hch/CSpellHandler.h"
  14. #include "../hch/CSoundBase.h"
  15. #include "../mapHandler.h"
  16. #include "GUIClasses.h"
  17. #include <boost/bind.hpp>
  18. #include <boost/foreach.hpp>
  19. #include <boost/thread.hpp>
  20. #include <boost/thread/shared_mutex.hpp>
  21. //macro to avoid code duplication - calls given method with given arguments if interface for specific player is present
  22. #define INTERFACE_CALL_IF_PRESENT(player,function,...) \
  23. if(vstd::contains(cl->playerint,player)) \
  24. cl->playerint[player]->function(__VA_ARGS__);
  25. /*
  26. * NetPacksClient.cpp, part of VCMI engine
  27. *
  28. * Authors: listed in file AUTHORS in main folder
  29. *
  30. * License: GNU General Public License v2.0 or later
  31. * Full text of license available in license.txt file, in main folder
  32. *
  33. */
  34. void SetResources::applyCl( CClient *cl )
  35. {
  36. cl->playerint[player]->receivedResource(-1,-1);
  37. }
  38. void SetResource::applyCl( CClient *cl )
  39. {
  40. cl->playerint[player]->receivedResource(resid,val);
  41. }
  42. void SetPrimSkill::applyCl( CClient *cl )
  43. {
  44. const CGHeroInstance *h = GS(cl)->getHero(id);
  45. if(!h)
  46. {
  47. tlog1 << "Cannot find hero with ID " << id << std::endl;
  48. return;
  49. }
  50. INTERFACE_CALL_IF_PRESENT(h->tempOwner,heroPrimarySkillChanged,h,which,val);
  51. }
  52. void SetSecSkill::applyCl( CClient *cl )
  53. {
  54. //TODO: inform interface?
  55. }
  56. void HeroVisitCastle::applyCl( CClient *cl )
  57. {
  58. if(start() && !garrison() && vstd::contains(cl->playerint,GS(cl)->getHero(hid)->tempOwner))
  59. {
  60. cl->playerint[GS(cl)->getHero(hid)->tempOwner]->heroVisitsTown(GS(cl)->getHero(hid),GS(cl)->getTown(tid));
  61. }
  62. }
  63. void ChangeSpells::applyCl( CClient *cl )
  64. {
  65. //TODO: inform interface?
  66. }
  67. void SetMana::applyCl( CClient *cl )
  68. {
  69. CGHeroInstance *h = GS(cl)->getHero(hid);
  70. if(vstd::contains(cl->playerint,h->tempOwner))
  71. cl->playerint[h->tempOwner]->heroManaPointsChanged(h);
  72. }
  73. void SetMovePoints::applyCl( CClient *cl )
  74. {
  75. CGHeroInstance *h = GS(cl)->getHero(hid);
  76. if(vstd::contains(cl->playerint,h->tempOwner))
  77. cl->playerint[h->tempOwner]->heroMovePointsChanged(h);
  78. }
  79. void FoWChange::applyCl( CClient *cl )
  80. {
  81. if(!vstd::contains(cl->playerint,player))
  82. return;
  83. if(mode)
  84. cl->playerint[player]->tileRevealed(tiles);
  85. else
  86. cl->playerint[player]->tileHidden(tiles);
  87. }
  88. void SetAvailableHeroes::applyCl( CClient *cl )
  89. {
  90. //TODO: inform interface?
  91. }
  92. void GiveBonus::applyCl( CClient *cl )
  93. {
  94. CGHeroInstance *h = GS(cl)->getHero(hid);
  95. if(vstd::contains(cl->playerint,h->tempOwner))
  96. cl->playerint[h->tempOwner]->heroBonusChanged(h,h->bonuses.back(),true);
  97. }
  98. void ChangeObjPos::applyFirstCl( CClient *cl )
  99. {
  100. CGObjectInstance *obj = GS(cl)->map->objects[objid];
  101. if(flags & 1)
  102. CGI->mh->hideObject(obj);
  103. }
  104. void ChangeObjPos::applyCl( CClient *cl )
  105. {
  106. CGObjectInstance *obj = GS(cl)->map->objects[objid];
  107. if(flags & 1)
  108. CGI->mh->printObject(obj);
  109. }
  110. void RemoveObject::applyFirstCl( CClient *cl )
  111. {
  112. CGI->mh->hideObject(cl->getObj(id));
  113. CGHeroInstance *h = GS(cl)->getHero(id);
  114. if(h)
  115. {
  116. if(vstd::contains(cl->playerint,h->tempOwner))
  117. cl->playerint[h->tempOwner]->heroKilled(h);
  118. }
  119. }
  120. void RemoveObject::applyCl( CClient *cl )
  121. {
  122. }
  123. void TryMoveHero::applyFirstCl( CClient *cl )
  124. {
  125. CGHeroInstance *h = GS(cl)->getHero(id);
  126. if(result == TELEPORTATION || result == EMBARK || result == DISEMBARK)
  127. CGI->mh->removeObject(h);
  128. if(result == DISEMBARK)
  129. CGI->mh->printObject(h->boat);
  130. }
  131. void TryMoveHero::applyCl( CClient *cl )
  132. {
  133. const CGHeroInstance *h = cl->getHero(id);
  134. if(result == TELEPORTATION || result == EMBARK || result == DISEMBARK)
  135. CGI->mh->printObject(h);
  136. if(result == EMBARK)
  137. CGI->mh->hideObject(h->boat);
  138. int player = h->tempOwner;
  139. if(vstd::contains(cl->playerint,player))
  140. {
  141. cl->playerint[player]->tileRevealed(fowRevealed);
  142. }
  143. //notify interfaces about move
  144. for(std::map<ui8, CGameInterface*>::iterator i=cl->playerint.begin();i!=cl->playerint.end();i++)
  145. {
  146. if(i->first >= PLAYER_LIMIT) continue;
  147. if(GS(cl)->players[i->first].fogOfWarMap[start.x-1][start.y][start.z] || GS(cl)->players[i->first].fogOfWarMap[end.x-1][end.y][end.z])
  148. {
  149. i->second->heroMoved(*this);
  150. }
  151. }
  152. }
  153. void SetGarrisons::applyCl( CClient *cl )
  154. {
  155. for(std::map<ui32,CCreatureSet>::iterator i = garrs.begin(); i!=garrs.end(); i++)
  156. if(vstd::contains(cl->playerint,cl->getOwner(i->first)))
  157. cl->playerint[cl->getOwner(i->first)]->garrisonChanged(cl->getObj(i->first));
  158. }
  159. void NewStructures::applyCl( CClient *cl )
  160. {
  161. CGTownInstance *town = GS(cl)->getTown(tid);
  162. BOOST_FOREACH(si32 id, bid)
  163. {
  164. if(id==13) //fort or capitol
  165. {
  166. town->defInfo = GS(cl)->capitols[town->subID];
  167. }
  168. if(id ==7)
  169. {
  170. town->defInfo = GS(cl)->forts[town->subID];
  171. }
  172. if(vstd::contains(cl->playerint,town->tempOwner))
  173. cl->playerint[town->tempOwner]->buildChanged(town,id,1);
  174. }
  175. }
  176. void SetAvailableCreatures::applyCl( CClient *cl )
  177. {
  178. const CGDwelling *dw = static_cast<const CGDwelling*>(cl->getObj(tid));
  179. if(vstd::contains(cl->playerint,dw->tempOwner))
  180. cl->playerint[dw->tempOwner]->availableCreaturesChanged(dw);
  181. }
  182. void SetHeroesInTown::applyCl( CClient *cl )
  183. {
  184. CGTownInstance *t = GS(cl)->getTown(tid);
  185. if(vstd::contains(cl->playerint,t->tempOwner))
  186. cl->playerint[t->tempOwner]->heroInGarrisonChange(t);
  187. }
  188. void SetHeroArtifacts::applyCl( CClient *cl )
  189. {
  190. CGHeroInstance *h = GS(cl)->getHero(hid);
  191. CGameInterface *player = (vstd::contains(cl->playerint,h->tempOwner) ? cl->playerint[h->tempOwner] : NULL);
  192. if(!player)
  193. return;
  194. player->heroArtifactSetChanged(h);
  195. BOOST_FOREACH(HeroBonus *bonus, gained)
  196. {
  197. player->heroBonusChanged(h,*bonus,true);
  198. }
  199. BOOST_FOREACH(HeroBonus *bonus, lost)
  200. {
  201. player->heroBonusChanged(h,*bonus,false);
  202. }
  203. }
  204. void HeroRecruited::applyCl( CClient *cl )
  205. {
  206. CGHeroInstance *h = GS(cl)->map->heroes.back();
  207. if(h->subID != hid)
  208. {
  209. tlog1 << "Something wrong with hero recruited!\n";
  210. }
  211. CGI->mh->initHeroDef(h);
  212. CGI->mh->printObject(h);
  213. if(vstd::contains(cl->playerint,h->tempOwner))
  214. {
  215. cl->playerint[h->tempOwner]->heroCreated(h);
  216. cl->playerint[h->tempOwner]->heroInGarrisonChange(GS(cl)->getTown(tid));
  217. }
  218. }
  219. void GiveHero::applyCl( CClient *cl )
  220. {
  221. CGHeroInstance *h = GS(cl)->getHero(id);
  222. CGI->mh->initHeroDef(h);
  223. CGI->mh->printObject(h);
  224. cl->playerint[h->tempOwner]->heroCreated(h);
  225. }
  226. void GiveHero::applyFirstCl( CClient *cl )
  227. {
  228. CGI->mh->hideObject(GS(cl)->getHero(id));
  229. }
  230. void InfoWindow::applyCl( CClient *cl )
  231. {
  232. std::vector<Component*> comps;
  233. for(size_t i=0;i<components.size();i++)
  234. {
  235. comps.push_back(&components[i]);
  236. }
  237. std::string str;
  238. text.toString(str);
  239. if(vstd::contains(cl->playerint,player))
  240. cl->playerint[player]->showInfoDialog(str,comps,(soundBase::soundID)soundID);
  241. else
  242. tlog2 << "We received InfoWindow for not our player...\n";
  243. }
  244. void SetObjectProperty::applyCl( CClient *cl )
  245. {
  246. //inform all players that see this object
  247. for(std::map<ui8,CGameInterface *>::const_iterator it = cl->playerint.begin(); it != cl->playerint.end(); ++it)
  248. {
  249. if(GS(cl)->isVisible(GS(cl)->map->objects[id], it->first))
  250. INTERFACE_CALL_IF_PRESENT(it->first, objectPropertyChanged, this);
  251. }
  252. }
  253. void HeroLevelUp::applyCl( CClient *cl )
  254. {
  255. CGHeroInstance *h = GS(cl)->getHero(heroid);
  256. if(vstd::contains(cl->playerint,h->tempOwner))
  257. {
  258. boost::function<void(ui32)> callback = boost::function<void(ui32)>(boost::bind(&CCallback::selectionMade,LOCPLINT->cb,_1,id));
  259. cl->playerint[h->tempOwner]->heroGotLevel(const_cast<const CGHeroInstance*>(h),static_cast<int>(primskill),skills, callback);
  260. }
  261. }
  262. void BlockingDialog::applyCl( CClient *cl )
  263. {
  264. std::string str;
  265. text.toString(str);
  266. if(vstd::contains(cl->playerint,player))
  267. cl->playerint[player]->showBlockingDialog(str,components,id,(soundBase::soundID)soundID,selection(),cancel());
  268. else
  269. tlog2 << "We received YesNoDialog for not our player...\n";
  270. }
  271. void GarrisonDialog::applyCl(CClient *cl)
  272. {
  273. const CGHeroInstance *h = cl->getHero(hid);
  274. const CArmedInstance *obj = static_cast<const CArmedInstance*>(cl->getObj(objid));
  275. if(!vstd::contains(cl->playerint,h->getOwner()))
  276. return;
  277. boost::function<void()> callback = boost::bind(&CCallback::selectionMade,LOCPLINT->cb,0,id);
  278. cl->playerint[h->getOwner()]->showGarrisonDialog(obj,h,callback);
  279. }
  280. void BattleStart::applyCl( CClient *cl )
  281. {
  282. if(vstd::contains(cl->playerint,info->side1))
  283. cl->playerint[info->side1]->battleStart(&info->army1, &info->army2, info->tile, GS(cl)->getHero(info->hero1), GS(cl)->getHero(info->hero2), 0);
  284. if(vstd::contains(cl->playerint,info->side2))
  285. cl->playerint[info->side2]->battleStart(&info->army1, &info->army2, info->tile, GS(cl)->getHero(info->hero1), GS(cl)->getHero(info->hero2), 1);
  286. }
  287. void BattleNextRound::applyCl( CClient *cl )
  288. {
  289. if(cl->playerint.find(GS(cl)->curB->side1) != cl->playerint.end())
  290. cl->playerint[GS(cl)->curB->side1]->battleNewRound(round);
  291. if(cl->playerint.find(GS(cl)->curB->side2) != cl->playerint.end())
  292. cl->playerint[GS(cl)->curB->side2]->battleNewRound(round);
  293. }
  294. void BattleSetActiveStack::applyCl( CClient *cl )
  295. {
  296. int owner = GS(cl)->curB->getStack(stack)->owner;
  297. if(vstd::contains(cl->playerint,owner))
  298. boost::thread(boost::bind(&CClient::waitForMoveAndSend,cl,owner));
  299. }
  300. void BattleResult::applyFirstCl( CClient *cl )
  301. {
  302. if(cl->playerint.find(GS(cl)->curB->side1) != cl->playerint.end())
  303. cl->playerint[GS(cl)->curB->side1]->battleEnd(this);
  304. if(cl->playerint.find(GS(cl)->curB->side2) != cl->playerint.end())
  305. cl->playerint[GS(cl)->curB->side2]->battleEnd(this);
  306. }
  307. void BattleStackMoved::applyFirstCl( CClient *cl )
  308. {
  309. INTERFACE_CALL_IF_PRESENT(GS(cl)->curB->side1,battleStackMoved,stack,tile,distance,ending);
  310. INTERFACE_CALL_IF_PRESENT(GS(cl)->curB->side2,battleStackMoved,stack,tile,distance,ending);
  311. }
  312. void BattleStackAttacked::applyCl( CClient *cl )
  313. {
  314. std::set<BattleStackAttacked> bsa;
  315. bsa.insert(*this);
  316. INTERFACE_CALL_IF_PRESENT(GS(cl)->curB->side1,battleStacksAttacked,bsa);
  317. INTERFACE_CALL_IF_PRESENT(GS(cl)->curB->side2,battleStacksAttacked,bsa);
  318. }
  319. void BattleAttack::applyFirstCl( CClient *cl )
  320. {
  321. if(cl->playerint.find(GS(cl)->curB->side1) != cl->playerint.end())
  322. cl->playerint[GS(cl)->curB->side1]->battleAttack(this);
  323. if(cl->playerint.find(GS(cl)->curB->side2) != cl->playerint.end())
  324. cl->playerint[GS(cl)->curB->side2]->battleAttack(this);
  325. }
  326. void BattleAttack::applyCl( CClient *cl )
  327. {
  328. INTERFACE_CALL_IF_PRESENT(GS(cl)->curB->side1,battleStacksAttacked,bsa);
  329. INTERFACE_CALL_IF_PRESENT(GS(cl)->curB->side2,battleStacksAttacked,bsa);
  330. }
  331. void StartAction::applyFirstCl( CClient *cl )
  332. {
  333. cl->curbaction = new BattleAction(ba);
  334. if(cl->playerint.find(GS(cl)->curB->side1) != cl->playerint.end())
  335. cl->playerint[GS(cl)->curB->side1]->actionStarted(&ba);
  336. if(cl->playerint.find(GS(cl)->curB->side2) != cl->playerint.end())
  337. cl->playerint[GS(cl)->curB->side2]->actionStarted(&ba);
  338. }
  339. void SpellCast::applyCl( CClient *cl )
  340. {
  341. if(cl->playerint.find(GS(cl)->curB->side1) != cl->playerint.end())
  342. cl->playerint[GS(cl)->curB->side1]->battleSpellCast(this);
  343. if(cl->playerint.find(GS(cl)->curB->side2) != cl->playerint.end())
  344. cl->playerint[GS(cl)->curB->side2]->battleSpellCast(this);
  345. }
  346. void SetStackEffect::applyCl( CClient *cl )
  347. {
  348. SpellCast sc;
  349. sc.id = effect.id;
  350. sc.side = 3; //doesn't matter
  351. sc.skill = effect.level;
  352. //informing about effects
  353. if(cl->playerint.find(GS(cl)->curB->side1) != cl->playerint.end())
  354. cl->playerint[GS(cl)->curB->side1]->battleStacksEffectsSet(*this);
  355. if(cl->playerint.find(GS(cl)->curB->side2) != cl->playerint.end())
  356. cl->playerint[GS(cl)->curB->side2]->battleStacksEffectsSet(*this);
  357. }
  358. void StacksInjured::applyCl( CClient *cl )
  359. {
  360. INTERFACE_CALL_IF_PRESENT(GS(cl)->curB->side1,battleStacksAttacked,stacks);
  361. INTERFACE_CALL_IF_PRESENT(GS(cl)->curB->side2,battleStacksAttacked,stacks);
  362. }
  363. CGameState* CPackForClient::GS( CClient *cl )
  364. {
  365. return cl->gs;
  366. }
  367. void EndAction::applyCl( CClient *cl )
  368. {
  369. INTERFACE_CALL_IF_PRESENT(GS(cl)->curB->side1,actionFinished,cl->curbaction);
  370. INTERFACE_CALL_IF_PRESENT(GS(cl)->curB->side2,actionFinished,cl->curbaction);
  371. delete cl->curbaction;
  372. cl->curbaction = NULL;
  373. }
  374. void PackageApplied::applyCl( CClient *cl )
  375. {
  376. INTERFACE_CALL_IF_PRESENT(GS(cl)->currentPlayer,requestRealized,this);
  377. }
  378. void SystemMessage::applyCl( CClient *cl )
  379. {
  380. std::ostringstream str;
  381. str << "System message: " << text;
  382. tlog4 << str.str() << std::endl;
  383. if(LOCPLINT)
  384. LOCPLINT->cingconsole->print(str.str());
  385. }
  386. void YourTurn::applyCl( CClient *cl )
  387. {
  388. boost::thread(boost::bind(&CGameInterface::yourTurn,cl->playerint[player]));
  389. }
  390. void SaveGame::applyCl(CClient *cl)
  391. {
  392. CSaveFile save("Games" PATHSEPARATOR + fname + ".vcgm1");
  393. save << *cl;
  394. }
  395. void PlayerMessage::applyCl(CClient *cl)
  396. {
  397. std::ostringstream str;
  398. str << "Player "<<(int)player<<" sends a message: " << text;
  399. tlog4 << str.str() << std::endl;
  400. if(LOCPLINT)
  401. LOCPLINT->cingconsole->print(str.str());
  402. }
  403. void ShowInInfobox::applyCl(CClient *cl)
  404. {
  405. SComponent sc(c);
  406. text.toString(sc.description);
  407. if(cl->playerint[player]->human)
  408. {
  409. static_cast<CPlayerInterface*>(cl->playerint[player])->showComp(sc);
  410. }
  411. }
  412. void OpenWindow::applyCl(CClient *cl)
  413. {
  414. switch(window)
  415. {
  416. case EXCHANGE_WINDOW:
  417. {
  418. const CGHeroInstance *h = cl->getHero(id1);
  419. const CGObjectInstance *h2 = cl->getHero(id2);
  420. assert(h && h2);
  421. INTERFACE_CALL_IF_PRESENT(h->tempOwner,heroExchangeStarted, id1, id2);
  422. }
  423. break;
  424. case RECRUITMENT_FIRST:
  425. case RECRUITMENT_ALL:
  426. {
  427. const CGDwelling *dw = dynamic_cast<const CGDwelling*>(cl->getObj(id1));
  428. const CArmedInstance *dst = dynamic_cast<const CArmedInstance*>(cl->getObj(id2));
  429. INTERFACE_CALL_IF_PRESENT(dw->tempOwner,showRecruitmentDialog, dw, dst, window == RECRUITMENT_FIRST ? 0 : -1);
  430. }
  431. break;
  432. case SHIPYARD_WINDOW:
  433. {
  434. const IShipyard *sy = IShipyard::castFrom(cl->getObj(id1));
  435. INTERFACE_CALL_IF_PRESENT(sy->o->tempOwner, showShipyardDialog, sy);
  436. }
  437. break;
  438. }
  439. }
  440. void NewObject::applyCl(CClient *cl)
  441. {
  442. const CGObjectInstance *obj = cl->getObj(id);
  443. //notify interfaces about move
  444. for(std::map<ui8, CGameInterface*>::iterator i=cl->playerint.begin();i!=cl->playerint.end();i++)
  445. {
  446. //TODO: check if any covered tile is visible
  447. if(i->first >= PLAYER_LIMIT) continue;
  448. if(GS(cl)->players[i->first].fogOfWarMap[obj->pos.x][obj->pos.y][obj->pos.z])
  449. {
  450. i->second->newObject(obj);
  451. }
  452. }
  453. }