CCallback.cpp 27 KB

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