CCallback.cpp 26 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006
  1. #include "stdafx.h"
  2. #include "CCallback.h"
  3. #include "client/CGameInfo.h"
  4. #include "lib/CGameState.h"
  5. #include "client/CPlayerInterface.h"
  6. #include "client/Client.h"
  7. #include "lib/map.h"
  8. #include "hch/CBuildingHandler.h"
  9. #include "hch/CDefObjInfoHandler.h"
  10. #include "hch/CHeroHandler.h"
  11. #include "hch/CObjectHandler.h"
  12. #include "lib/Connection.h"
  13. #include "lib/NetPacks.h"
  14. #include "mapHandler.h"
  15. #include <boost/foreach.hpp>
  16. #include <boost/thread.hpp>
  17. #include <boost/thread/shared_mutex.hpp>
  18. #include "hch/CSpellHandler.h"
  19. #ifdef min
  20. #undef min
  21. #endif
  22. #ifdef max
  23. #undef max
  24. #endif
  25. /*
  26. * CCallback.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. static int gcd(int x, int y)
  35. {
  36. int temp;
  37. if (y > x)
  38. std::swap(x,y);
  39. while (y != 0)
  40. {
  41. temp = y;
  42. y = x-y;
  43. x = temp;
  44. if (y > x)
  45. std::swap(x,y);
  46. }
  47. return x;
  48. }
  49. HeroMoveDetails::HeroMoveDetails(int3 Src, int3 Dst, CGHeroInstance*Ho)
  50. :src(Src),dst(Dst),ho(Ho)
  51. {
  52. owner = ho->getOwner();
  53. };
  54. template <ui16 N> bool isType(CPack *pack)
  55. {
  56. return pack->getType() == N;
  57. }
  58. bool CCallback::moveHero(const CGHeroInstance *h, int3 dst)
  59. {
  60. MoveHero pack(dst,h->id);
  61. sendRequest(&pack);
  62. return true;
  63. }
  64. void CCallback::selectionMade(int selection, int asker)
  65. {
  66. QueryReply pack(asker,selection);
  67. *cl->serv << &pack;
  68. }
  69. void CCallback::recruitCreatures(const CGObjectInstance *obj, ui32 ID, ui32 amount)
  70. {
  71. if(player!=obj->tempOwner) return;
  72. RecruitCreatures pack(obj->id,ID,amount);
  73. sendRequest(&pack);
  74. }
  75. bool CCallback::dismissCreature(const CArmedInstance *obj, int stackPos)
  76. {
  77. if(((player>=0) && obj->tempOwner != player) || (obj->army.slots.size()<2 && obj->needsLastStack()))
  78. return false;
  79. DisbandCreature pack(stackPos,obj->id);
  80. sendRequest(&pack);
  81. return true;
  82. }
  83. bool CCallback::upgradeCreature(const CArmedInstance *obj, int stackPos, int newID)
  84. {
  85. UpgradeCreature pack(stackPos,obj->id,newID);
  86. sendRequest(&pack);
  87. return false;
  88. }
  89. void CCallback::endTurn()
  90. {
  91. tlog5 << "Player " << (unsigned)player << " ended his turn." << std::endl;
  92. EndTurn pack;
  93. sendRequest(&pack); //report that we ended turn
  94. }
  95. UpgradeInfo CCallback::getUpgradeInfo(const CArmedInstance *obj, int stackPos) const
  96. {
  97. boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  98. return gs->getUpgradeInfo(const_cast<CArmedInstance*>(obj),stackPos);
  99. }
  100. const StartInfo * CCallback::getStartInfo() const
  101. {
  102. boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  103. return gs->scenarioOps;
  104. }
  105. int CCallback::getSpellCost(const CSpell * sp, const CGHeroInstance * caster) const
  106. {
  107. boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  108. //if there is a battle
  109. if(gs->curB)
  110. return gs->curB->getSpellCost(sp, caster);
  111. //if there is no battle
  112. return caster->getSpellCost(sp);
  113. }
  114. int CCallback::estimateSpellDamage(const CSpell * sp) const
  115. {
  116. boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  117. if(!gs->curB)
  118. return 0;
  119. const CGHeroInstance * ourHero = gs->curB->heroes[0]->tempOwner == player ? gs->curB->heroes[0] : gs->curB->heroes[1];
  120. return gs->curB->calculateSpellDmg(sp, ourHero, NULL, ourHero->getSpellSchoolLevel(sp), ourHero->getPrimSkillLevel(2));
  121. }
  122. void CCallback::getThievesGuildInfo(SThievesGuildInfo & thi, const CGObjectInstance * obj)
  123. {
  124. boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  125. if(obj == NULL)
  126. return;
  127. if(obj->ID == TOWNI_TYPE) //it is a town
  128. {
  129. gs->obtainPlayersStats(thi, gs->players[player].towns.size());
  130. }
  131. else if(obj->ID == 97) //Den of Thieves
  132. {
  133. gs->obtainPlayersStats(thi, 20);
  134. }
  135. }
  136. int CCallback::howManyTowns() const
  137. {
  138. boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  139. return gs->players[player].towns.size();
  140. }
  141. const CGTownInstance * CCallback::getTownInfo(int val, bool mode) const //mode = 0 -> val = serial; mode = 1 -> val = ID
  142. {
  143. boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  144. if (!mode)
  145. {
  146. const std::vector<CGTownInstance *> &towns = gs->players[gs->currentPlayer].towns;
  147. if(val < towns.size())
  148. return towns[val];
  149. else
  150. return NULL;
  151. }
  152. else
  153. {
  154. //TODO: add some smart ID to the CTownInstance
  155. //for (int i=0; i<gs->players[gs->currentPlayer].towns.size();i++)
  156. //{
  157. // if (gs->players[gs->currentPlayer].towns[i]->someID==val)
  158. // return gs->players[gs->currentPlayer].towns[i];
  159. //}
  160. return NULL;
  161. }
  162. return NULL;
  163. }
  164. bool CCallback::getTownInfo( const CGObjectInstance *town, InfoAboutTown &dest ) const
  165. {
  166. const CGTownInstance *t = dynamic_cast<const CGTownInstance *>(town);
  167. if(!t || !isVisible(t, player)) //it's not a town or it's not visible for layer
  168. return false;
  169. //TODO vision support, info about allies
  170. dest.initFromTown(t, false);
  171. return true;
  172. }
  173. int CCallback::howManyHeroes(bool includeGarrisoned) const
  174. {
  175. boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  176. return cl->getHeroCount(player,includeGarrisoned);
  177. }
  178. const CGHeroInstance * CCallback::getHeroInfo(int val, int mode) const //mode = 0 -> val = serial; mode = 1 -> val = ID
  179. {
  180. boost::shared_lock<boost::shared_mutex> lock(*gs->mx); //TODO use me?
  181. //if (gs->currentPlayer!=player) //TODO: checking if we are allowed to give that info
  182. // return NULL;
  183. if (!mode) //esrial id
  184. {
  185. if(val<gs->players[player].heroes.size())
  186. {
  187. return gs->players[player].heroes[val];
  188. }
  189. else
  190. {
  191. return NULL;
  192. }
  193. }
  194. else if(mode==1) //it's hero type id
  195. {
  196. for (size_t i=0; i < gs->players[player].heroes.size(); ++i)
  197. {
  198. if (gs->players[player].heroes[i]->type->ID==val)
  199. {
  200. return gs->players[player].heroes[i];
  201. }
  202. }
  203. }
  204. else //object id
  205. {
  206. return static_cast<const CGHeroInstance*>(gs->map->objects[val]);
  207. }
  208. return NULL;
  209. }
  210. const CGObjectInstance * CCallback::getObjectInfo(int ID) const
  211. {
  212. return gs->map->objects[ID];
  213. }
  214. bool CCallback::getHeroInfo( const CGObjectInstance *hero, InfoAboutHero &dest ) const
  215. {
  216. const CGHeroInstance *h = dynamic_cast<const CGHeroInstance *>(hero);
  217. if(!h || !isVisible(h->getPosition(false))) //it's not a hero or it's not visible for layer
  218. return false;
  219. //TODO vision support, info about allies
  220. dest.initFromHero(h, false);
  221. return true;
  222. }
  223. int CCallback::getResourceAmount(int type) const
  224. {
  225. boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  226. return gs->players[player].resources[type];
  227. }
  228. std::vector<si32> CCallback::getResourceAmount() const
  229. {
  230. boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  231. return gs->players[player].resources;
  232. }
  233. int CCallback::getDate(int mode) const
  234. {
  235. boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  236. return gs->getDate(mode);
  237. }
  238. std::vector < std::string > CCallback::getObjDescriptions(int3 pos) const
  239. {
  240. boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  241. std::vector<std::string> ret;
  242. if(!isVisible(pos,player))
  243. return ret;
  244. BOOST_FOREACH(const CGObjectInstance * obj, gs->map->terrain[pos.x][pos.y][pos.z].blockingObjects)
  245. ret.push_back(obj->getHoverText());
  246. return ret;
  247. }
  248. bool CCallback::verifyPath(CPath * path, bool blockSea) const
  249. {
  250. for (size_t i=0; i < path->nodes.size(); ++i)
  251. {
  252. if ( CGI->mh->ttiles[path->nodes[i].coord.x][path->nodes[i].coord.y][path->nodes[i].coord.z].tileInfo->blocked
  253. && (! (CGI->mh->ttiles[path->nodes[i].coord.x][path->nodes[i].coord.y][path->nodes[i].coord.z].tileInfo->visitable)))
  254. return false; //path is wrong - one of the tiles is blocked
  255. if (blockSea)
  256. {
  257. if (i==0)
  258. continue;
  259. if (
  260. ((CGI->mh->ttiles[path->nodes[i].coord.x][path->nodes[i].coord.y][path->nodes[i].coord.z].tileInfo->tertype==TerrainTile::water)
  261. &&
  262. (CGI->mh->ttiles[path->nodes[i-1].coord.x][path->nodes[i-1].coord.y][path->nodes[i-1].coord.z].tileInfo->tertype!=TerrainTile::water))
  263. ||
  264. ((CGI->mh->ttiles[path->nodes[i].coord.x][path->nodes[i].coord.y][path->nodes[i].coord.z].tileInfo->tertype!=TerrainTile::water)
  265. &&
  266. (CGI->mh->ttiles[path->nodes[i-1].coord.x][path->nodes[i-1].coord.y][path->nodes[i-1].coord.z].tileInfo->tertype==TerrainTile::water))
  267. ||
  268. (CGI->mh->ttiles[path->nodes[i-1].coord.x][path->nodes[i-1].coord.y][path->nodes[i-1].coord.z].tileInfo->tertype==TerrainTile::rock)
  269. )
  270. return false;
  271. }
  272. }
  273. return true;
  274. }
  275. std::vector< std::vector< std::vector<unsigned char> > > & CCallback::getVisibilityMap() const
  276. {
  277. boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  278. return gs->players[player].fogOfWarMap;
  279. }
  280. bool CCallback::isVisible(int3 pos, int Player) const
  281. {
  282. boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  283. return gs->map->isInTheMap(pos) && gs->isVisible(pos, Player);
  284. }
  285. std::vector < const CGTownInstance *> CCallback::getTownsInfo(bool onlyOur) const
  286. {
  287. boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  288. std::vector < const CGTownInstance *> ret = std::vector < const CGTownInstance *>();
  289. for ( std::map<ui8, PlayerState>::iterator i=gs->players.begin() ; i!=gs->players.end();i++)
  290. {
  291. for (size_t j=0; j < (*i).second.towns.size(); ++j)
  292. {
  293. if ((*i).first==player
  294. || (isVisible((*i).second.towns[j],player) && !onlyOur))
  295. {
  296. ret.push_back((*i).second.towns[j]);
  297. }
  298. }
  299. } // for ( std::map<int, PlayerState>::iterator i=gs->players.begin() ; i!=gs->players.end();i++)
  300. return ret;
  301. }
  302. std::vector < const CGHeroInstance *> CCallback::getHeroesInfo(bool onlyOur) const
  303. {
  304. boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  305. std::vector < const CGHeroInstance *> ret;
  306. for(size_t i=0;i<gs->map->heroes.size();i++)
  307. {
  308. if( (gs->map->heroes[i]->tempOwner==player) ||
  309. (isVisible(gs->map->heroes[i]->getPosition(false),player) && !onlyOur) )
  310. {
  311. ret.push_back(gs->map->heroes[i]);
  312. }
  313. }
  314. return ret;
  315. }
  316. bool CCallback::isVisible(int3 pos) const
  317. {
  318. boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  319. return isVisible(pos,player);
  320. }
  321. bool CCallback::isVisible( const CGObjectInstance *obj, int Player ) const
  322. {
  323. return gs->isVisible(obj, Player);
  324. }
  325. int CCallback::getMyColor() const
  326. {
  327. return player;
  328. }
  329. int CCallback::getHeroSerial(const CGHeroInstance * hero) const
  330. {
  331. boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  332. for (size_t i=0; i<gs->players[player].heroes.size();i++)
  333. {
  334. if (gs->players[player].heroes[i]==hero)
  335. return i;
  336. }
  337. return -1;
  338. }
  339. const CCreatureSet* CCallback::getGarrison(const CGObjectInstance *obj) const
  340. {
  341. boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  342. const CArmedInstance *armi = dynamic_cast<const CArmedInstance*>(obj);
  343. if(!armi)
  344. return NULL;
  345. else
  346. return &armi->army;
  347. }
  348. int CCallback::swapCreatures(const CArmedInstance *s1, const CArmedInstance *s2, int p1, int p2)
  349. {
  350. ArrangeStacks pack(1,p1,p2,s1->id,s2->id,0);
  351. sendRequest(&pack);
  352. return 0;
  353. }
  354. int CCallback::mergeStacks(const CArmedInstance *s1, const CArmedInstance *s2, int p1, int p2)
  355. {
  356. ArrangeStacks pack(2,p1,p2,s1->id,s2->id,0);
  357. sendRequest(&pack);
  358. return 0;
  359. }
  360. int CCallback::splitStack(const CArmedInstance *s1, const CArmedInstance *s2, int p1, int p2, int val)
  361. {
  362. ArrangeStacks pack(3,p1,p2,s1->id,s2->id,val);
  363. sendRequest(&pack);
  364. return 0;
  365. }
  366. bool CCallback::dismissHero(const CGHeroInstance *hero)
  367. {
  368. if(player!=hero->tempOwner) return false;
  369. DismissHero pack(hero->id);
  370. sendRequest(&pack);
  371. return true;
  372. }
  373. int CCallback::getMySerial() const
  374. {
  375. boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  376. return gs->players[player].serial;
  377. }
  378. bool CCallback::swapArtifacts(const CGHeroInstance * hero1, ui16 pos1, const CGHeroInstance * hero2, ui16 pos2)
  379. {
  380. if(player!=hero1->tempOwner || player!=hero2->tempOwner)
  381. return false;
  382. ExchangeArtifacts ea(hero1->id, hero2->id, pos1, pos2);
  383. sendRequest(&ea);
  384. return true;
  385. }
  386. /**
  387. * Assembles or disassembles a combination artifact.
  388. * @param hero Hero holding the artifact(s).
  389. * @param artifactSlot The worn slot ID of the combination- or constituent artifact.
  390. * @param assemble True for assembly operation, false for disassembly.
  391. * @param assembleTo If assemble is true, this represents the artifact ID of the combination
  392. * artifact to assemble to. Otherwise it's not used.
  393. */
  394. bool CCallback::assembleArtifacts (const CGHeroInstance * hero, ui16 artifactSlot, bool assemble, ui32 assembleTo)
  395. {
  396. if (player != hero->tempOwner)
  397. return false;
  398. AssembleArtifacts aa(hero->id, artifactSlot, assemble, assembleTo);
  399. sendRequest(&aa);
  400. return true;
  401. }
  402. bool CCallback::buildBuilding(const CGTownInstance *town, si32 buildingID)
  403. {
  404. CGTownInstance * t = const_cast<CGTownInstance *>(town);
  405. if(town->tempOwner!=player)
  406. return false;
  407. CBuilding *b = CGI->buildh->buildings[t->subID][buildingID];
  408. for(int i=0;i<b->resources.size();i++)
  409. if(b->resources[i] > gs->players[player].resources[i])
  410. return false; //lack of resources
  411. BuildStructure pack(town->id,buildingID);
  412. sendRequest(&pack);
  413. return true;
  414. }
  415. int CCallback::battleGetBattlefieldType()
  416. {
  417. boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  418. return gs->battleGetBattlefieldType();
  419. }
  420. int CCallback::battleGetObstaclesAtTile(int tile) //returns bitfield
  421. {
  422. //TODO - write
  423. return -1;
  424. }
  425. std::vector<CObstacleInstance> CCallback::battleGetAllObstacles()
  426. {
  427. boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  428. if(gs->curB)
  429. return gs->curB->obstacles;
  430. else
  431. return std::vector<CObstacleInstance>();
  432. }
  433. int CCallback::battleGetStack(int pos, bool onlyAlive)
  434. {
  435. boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  436. return gs->battleGetStack(pos, onlyAlive);
  437. }
  438. const CStack* CCallback::battleGetStackByID(int ID, bool onlyAlive)
  439. {
  440. boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  441. if(!gs->curB) return NULL;
  442. return gs->curB->getStack(ID, onlyAlive);
  443. }
  444. int CCallback::battleMakeAction(BattleAction* action)
  445. {
  446. MakeCustomAction mca(*action);
  447. sendRequest(&mca);
  448. return 0;
  449. }
  450. const CStack* CCallback::battleGetStackByPos(int pos, bool onlyAlive)
  451. {
  452. boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  453. return battleGetStackByID(battleGetStack(pos, onlyAlive), onlyAlive);
  454. }
  455. int CCallback::battleGetPos(int stack)
  456. {
  457. boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  458. if(!gs->curB)
  459. {
  460. tlog2<<"battleGetPos called when there is no battle!"<<std::endl;
  461. return -1;
  462. }
  463. for(size_t g=0; g<gs->curB->stacks.size(); ++g)
  464. {
  465. if(gs->curB->stacks[g]->ID == stack)
  466. return gs->curB->stacks[g]->position;
  467. }
  468. return -1;
  469. }
  470. std::map<int, CStack> CCallback::battleGetStacks()
  471. {
  472. boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  473. std::map<int, CStack> ret;
  474. if(!gs->curB) //there is no battle
  475. {
  476. return ret;
  477. }
  478. for(size_t g=0; g<gs->curB->stacks.size(); ++g)
  479. {
  480. ret[gs->curB->stacks[g]->ID] = *(gs->curB->stacks[g]);
  481. }
  482. return ret;
  483. }
  484. void CCallback::getStackQueue( std::vector<const CStack *> &out, int howMany )
  485. {
  486. if(!gs->curB)
  487. {
  488. tlog2 << "battleGetStackQueue called when there is not battle!" << std::endl;
  489. return;
  490. }
  491. gs->curB->getStackQueue(out, howMany);
  492. }
  493. CCreature CCallback::battleGetCreature(int number)
  494. {
  495. boost::shared_lock<boost::shared_mutex> lock(*gs->mx); //TODO use me?
  496. if(!gs->curB)
  497. {
  498. tlog2<<"battleGetCreature called when there is no battle!"<<std::endl;
  499. }
  500. for(size_t h=0; h<gs->curB->stacks.size(); ++h)
  501. {
  502. if(gs->curB->stacks[h]->ID == number) //creature found
  503. return *(gs->curB->stacks[h]->creature);
  504. }
  505. #ifndef __GNUC__
  506. throw new std::exception("Cannot find the creature");
  507. #else
  508. throw new std::exception();
  509. #endif
  510. }
  511. std::vector<int> CCallback::battleGetAvailableHexes(int ID, bool addOccupiable)
  512. {
  513. boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  514. if(!gs->curB)
  515. {
  516. tlog2<<"battleGetAvailableHexes called when there is no battle!"<<std::endl;
  517. return std::vector<int>();
  518. }
  519. return gs->curB->getAccessibility(ID, addOccupiable);
  520. //return gs->battleGetRange(ID);
  521. }
  522. bool CCallback::battleIsStackMine(int ID)
  523. {
  524. boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  525. if(!gs->curB)
  526. {
  527. tlog2<<"battleIsStackMine called when there is no battle!"<<std::endl;
  528. return false;
  529. }
  530. for(size_t h=0; h<gs->curB->stacks.size(); ++h)
  531. {
  532. if(gs->curB->stacks[h]->ID == ID) //creature found
  533. return gs->curB->stacks[h]->owner == player;
  534. }
  535. return false;
  536. }
  537. bool CCallback::battleCanShoot(int ID, int dest)
  538. {
  539. boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  540. if(!gs->curB) return false;
  541. return gs->battleCanShoot(ID, dest);
  542. }
  543. bool CCallback::battleCanCastSpell()
  544. {
  545. if(!gs->curB) //there is no battle
  546. return false;
  547. if(gs->curB->side1 == player)
  548. return gs->curB->castSpells[0] == 0 && gs->curB->heroes[0]->getArt(17);
  549. else
  550. return gs->curB->castSpells[1] == 0 && gs->curB->heroes[1]->getArt(17);
  551. }
  552. bool CCallback::battleCanFlee()
  553. {
  554. return gs->battleCanFlee(player);
  555. }
  556. const CGTownInstance *CCallback::battleGetDefendedTown()
  557. {
  558. if(!gs->curB || gs->curB->tid == -1)
  559. return NULL;
  560. return static_cast<const CGTownInstance *>(gs->map->objects[gs->curB->tid]);
  561. }
  562. ui8 CCallback::battleGetWallState(int partOfWall)
  563. {
  564. if(!gs->curB || gs->curB->siege == 0)
  565. {
  566. return 0;
  567. }
  568. return gs->curB->si.wallState[partOfWall];
  569. }
  570. int CCallback::battleGetWallUnderHex(int hex)
  571. {
  572. if(!gs->curB || gs->curB->siege == 0)
  573. {
  574. return -1;
  575. }
  576. return gs->curB->hexToWallPart(hex);
  577. }
  578. std::pair<ui32, ui32> CCallback::battleEstimateDamage(int attackerID, int defenderID)
  579. {
  580. if(!gs->curB)
  581. return std::make_pair(0, 0);
  582. const CGHeroInstance * attackerHero, * defenderHero;
  583. if(gs->curB->side1 == player)
  584. {
  585. attackerHero = gs->curB->heroes[0];
  586. defenderHero = gs->curB->heroes[1];
  587. }
  588. else
  589. {
  590. attackerHero = gs->curB->heroes[1];
  591. defenderHero = gs->curB->heroes[0];
  592. }
  593. const CStack * attacker = gs->curB->getStack(attackerID, false),
  594. * defender = gs->curB->getStack(defenderID);
  595. return BattleInfo::calculateDmgRange(attacker, defender, attackerHero, defenderHero, battleCanShoot(attacker->ID, defender->position), 0);
  596. }
  597. ui8 CCallback::battleGetSiegeLevel()
  598. {
  599. if(!gs->curB)
  600. return 0;
  601. return gs->curB->siege;
  602. }
  603. const CGHeroInstance * CCallback::battleGetFightingHero(ui8 side) const
  604. {
  605. if(!gs->curB)
  606. return 0;
  607. return gs->curB->heroes[side];
  608. }
  609. void CCallback::swapGarrisonHero( const CGTownInstance *town )
  610. {
  611. if(town->tempOwner != player) return;
  612. GarrisonHeroSwap pack(town->id);
  613. sendRequest(&pack);
  614. }
  615. void CCallback::buyArtifact(const CGHeroInstance *hero, int aid)
  616. {
  617. if(hero->tempOwner != player) return;
  618. BuyArtifact pack(hero->id,aid);
  619. sendRequest(&pack);
  620. }
  621. std::vector < const CGObjectInstance * > CCallback::getBlockingObjs( int3 pos ) const
  622. {
  623. std::vector<const CGObjectInstance *> ret;
  624. boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  625. if(!gs->map->isInTheMap(pos) || !isVisible(pos))
  626. return ret;
  627. BOOST_FOREACH(const CGObjectInstance * obj, gs->map->terrain[pos.x][pos.y][pos.z].blockingObjects)
  628. ret.push_back(obj);
  629. return ret;
  630. }
  631. std::vector < const CGObjectInstance * > CCallback::getVisitableObjs( int3 pos ) const
  632. {
  633. std::vector<const CGObjectInstance *> ret;
  634. boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  635. if(!gs->map->isInTheMap(pos) || !isVisible(pos))
  636. return ret;
  637. BOOST_FOREACH(const CGObjectInstance * obj, gs->map->terrain[pos.x][pos.y][pos.z].visitableObjects)
  638. ret.push_back(obj);
  639. return ret;
  640. }
  641. void CCallback::getMarketOffer( int t1, int t2, int &give, int &rec, int mode/*=0*/ ) const
  642. {
  643. if(mode) return; //TODO - support
  644. boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  645. //if(gs->resVals[t1] >= gs->resVals[t2])
  646. float r = gs->resVals[t1], //price of given resource
  647. g = gs->resVals[t2] / gs->getMarketEfficiency(player,mode); //price of wanted resource
  648. if(r>g) //if given resource is more expensive than wanted
  649. {
  650. rec = ceil(r / g);
  651. give = 1;
  652. }
  653. else //if wanted resource is more expensive
  654. {
  655. give = ceil(g / r);
  656. rec = 1;
  657. }
  658. }
  659. std::vector < const CGObjectInstance * > CCallback::getFlaggableObjects(int3 pos) const
  660. {
  661. if(!isVisible(pos))
  662. return std::vector < const CGObjectInstance * >();
  663. std::vector < const CGObjectInstance * > ret;
  664. std::vector < std::pair<const CGObjectInstance*,SDL_Rect> > & objs = CGI->mh->ttiles[pos.x][pos.y][pos.z].objects;
  665. for(size_t b=0; b<objs.size(); ++b)
  666. {
  667. if(objs[b].first->tempOwner!=254 && !((objs[b].first->defInfo->blockMap[pos.y - objs[b].first->pos.y + 5] >> (objs[b].first->pos.x - pos.x)) & 1))
  668. ret.push_back(CGI->mh->ttiles[pos.x][pos.y][pos.z].objects[b].first);
  669. }
  670. return ret;
  671. }
  672. int3 CCallback::getMapSize() const
  673. {
  674. return CGI->mh->sizes;
  675. }
  676. void CCallback::trade( int mode, int id1, int id2, int val1 )
  677. {
  678. int p1, p2;
  679. getMarketOffer(id1,id2,p1,p2,mode);
  680. TradeOnMarketplace pack(player,mode,id1,id2,val1);
  681. sendRequest(&pack);
  682. }
  683. void CCallback::setFormation(const CGHeroInstance * hero, bool tight)
  684. {
  685. const_cast<CGHeroInstance*>(hero)->army.formation = tight;
  686. SetFormation pack(hero->id,tight);
  687. sendRequest(&pack);
  688. }
  689. void CCallback::setSelection(const CArmedInstance * obj)
  690. {
  691. SetSelection ss;
  692. ss.player = player;
  693. ss.id = obj->id;
  694. sendRequest(&ss);
  695. if(obj->ID == HEROI_TYPE)
  696. {
  697. cl->gs->calculatePaths(static_cast<const CGHeroInstance *>(obj), *cl->pathInfo);
  698. //nasty workaround. TODO: nice workaround
  699. cl->gs->getPlayer(player)->currentSelection = obj->id;
  700. }
  701. }
  702. void CCallback::recruitHero(const CGTownInstance *town, const CGHeroInstance *hero)
  703. {
  704. ui8 i=0;
  705. for(; i<gs->players[player].availableHeroes.size(); i++)
  706. {
  707. if(gs->players[player].availableHeroes[i] == hero)
  708. {
  709. HireHero pack(i,town->id);
  710. sendRequest(&pack);
  711. return;
  712. }
  713. }
  714. }
  715. std::vector<const CGHeroInstance *> CCallback::getAvailableHeroes(const CGTownInstance * town) const
  716. {
  717. std::vector<const CGHeroInstance *> ret(gs->players[player].availableHeroes.size());
  718. std::copy(gs->players[player].availableHeroes.begin(),gs->players[player].availableHeroes.end(),ret.begin());
  719. return ret;
  720. }
  721. const TerrainTile * CCallback::getTileInfo( int3 tile ) const
  722. {
  723. if(!gs->map->isInTheMap(tile))
  724. {
  725. tlog1 << tile << "is outside the map! (call to getTileInfo)\n";
  726. return NULL;
  727. }
  728. if(!isVisible(tile, player)) return NULL;
  729. boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  730. return &gs->map->getTile(tile);
  731. }
  732. int CCallback::canBuildStructure( const CGTownInstance *t, int ID )
  733. {
  734. return gs->canBuildStructure(t,ID);
  735. }
  736. std::set<int> CCallback::getBuildingRequiments( const CGTownInstance *t, int ID )
  737. {
  738. return gs->getBuildingRequiments(t,ID);
  739. }
  740. bool CCallback::getPath(int3 src, int3 dest, const CGHeroInstance * hero, CPath &ret)
  741. {
  742. boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  743. return gs->getPath(src,dest,hero, ret);
  744. }
  745. void CCallback::save( const std::string &fname )
  746. {
  747. cl->save(fname);
  748. }
  749. void CCallback::sendMessage(const std::string &mess)
  750. {
  751. PlayerMessage pm(player, mess);
  752. sendRequest(&pm);
  753. }
  754. void CCallback::buildBoat( const IShipyard *obj )
  755. {
  756. BuildBoat bb;
  757. bb.objid = obj->o->id;
  758. sendRequest(&bb);
  759. }
  760. template <typename T>
  761. void CCallback::sendRequest(const T* request)
  762. {
  763. //TODO? should be part of CClient but it would have to be very tricky cause template/serialization issues
  764. if(waitTillRealize)
  765. cl->waitingRequest.set(true);
  766. *cl->serv << request;
  767. if(waitTillRealize)
  768. cl->waitingRequest.waitWhileTrue();
  769. }
  770. CCallback::CCallback( CGameState * GS, int Player, CClient *C )
  771. :gs(GS), cl(C), player(Player)
  772. {
  773. waitTillRealize = false;
  774. }
  775. const CMapHeader * CCallback::getMapHeader() const
  776. {
  777. return gs->map;
  778. }
  779. const CGPathNode * CCallback::getPathInfo( int3 tile )
  780. {
  781. return &cl->pathInfo->nodes[tile.x][tile.y][tile.z];
  782. }
  783. bool CCallback::getPath2( int3 dest, CGPath &ret )
  784. {
  785. if (!gs->map->isInTheMap(dest))
  786. return false;
  787. const CGHeroInstance *h = cl->IGameCallback::getSelectedHero(player);
  788. assert(cl->pathInfo->hero == h);
  789. if(cl->pathInfo->hpos != h->getPosition(false)) //hero position changed, must update paths
  790. {
  791. recalculatePaths();
  792. }
  793. return cl->pathInfo->getPath(dest, ret);
  794. }
  795. void CCallback::recalculatePaths()
  796. {
  797. gs->calculatePaths(cl->IGameCallback::getSelectedHero(player), *cl->pathInfo);
  798. }
  799. void CCallback::calculatePaths( const CGHeroInstance *hero, CPathsInfo &out, int3 src /*= int3(-1,-1,-1)*/, int movement /*= -1*/ )
  800. {
  801. gs->calculatePaths(hero, out, src, movement);
  802. }
  803. int3 CCallback::getGrailPos( float &outKnownRatio )
  804. {
  805. if (CGObelisk::obeliskCount == 0)
  806. {
  807. outKnownRatio = 0.0f;
  808. }
  809. else
  810. {
  811. outKnownRatio = (float)CGObelisk::visited[player] / CGObelisk::obeliskCount;
  812. }
  813. return gs->map->grailPos;
  814. }
  815. void CCallback::dig( const CGObjectInstance *hero )
  816. {
  817. DigWithHero dwh;
  818. dwh.id = hero->id;
  819. sendRequest(&dwh);
  820. }
  821. si8 CCallback::battleGetStackMorale( int stackID )
  822. {
  823. return gs->curB->Morale( gs->curB->getStack(stackID) );
  824. }
  825. si8 CCallback::battleGetStackLuck( int stackID )
  826. {
  827. return gs->curB->Luck( gs->curB->getStack(stackID) );
  828. }
  829. void CCallback::castSpell(const CGHeroInstance *hero, int spellID, const int3 &pos)
  830. {
  831. CastAdvSpell cas;
  832. cas.hid = hero->id;
  833. cas.sid = spellID;
  834. cas.pos = pos;
  835. sendRequest(&cas);
  836. }
  837. InfoAboutTown::InfoAboutTown()
  838. {
  839. tType = NULL;
  840. details = NULL;
  841. fortLevel = 0;
  842. owner = -1;
  843. }
  844. InfoAboutTown::~InfoAboutTown()
  845. {
  846. delete details;
  847. }
  848. void InfoAboutTown::initFromTown( const CGTownInstance *t, bool detailed )
  849. {
  850. obj = t;
  851. army = t->army;
  852. built = t->builded;
  853. fortLevel = t->fortLevel();
  854. name = t->name;
  855. tType = t->town;
  856. owner = t->tempOwner;
  857. if(detailed)
  858. {
  859. //include details about hero
  860. details = new Details;
  861. details->goldIncome = t->dailyIncome();
  862. details->customRes = vstd::contains(t->builtBuildings, 15);
  863. details->hallLevel = t->hallLevel();
  864. details->garrisonedHero = t->garrisonHero;
  865. }
  866. /*else
  867. {
  868. //hide info about hero stacks counts
  869. for(std::map<si32,std::pair<ui32,si32> >::iterator i = army.slots.begin(); i != army.slots.end(); ++i)
  870. {
  871. i->second.second = 0;
  872. }
  873. }*/
  874. }