CCallback.cpp 27 KB

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