CCallback.cpp 27 KB

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