CCallback.cpp 28 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079
  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(obj->getStack(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
  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
  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->map->terrain[path->nodes[i].coord.x][path->nodes[i].coord.y][path->nodes[i].coord.z].blocked
  256. && (! (CGI->mh->map->terrain[path->nodes[i].coord.x][path->nodes[i].coord.y][path->nodes[i].coord.z].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->map->terrain[path->nodes[i].coord.x][path->nodes[i].coord.y][path->nodes[i].coord.z].tertype==TerrainTile::water)
  264. &&
  265. (CGI->mh->map->terrain[path->nodes[i-1].coord.x][path->nodes[i-1].coord.y][path->nodes[i-1].coord.z].tertype!=TerrainTile::water))
  266. ||
  267. ((CGI->mh->map->terrain[path->nodes[i].coord.x][path->nodes[i].coord.y][path->nodes[i].coord.z].tertype!=TerrainTile::water)
  268. &&
  269. (CGI->mh->map->terrain[path->nodes[i-1].coord.x][path->nodes[i-1].coord.y][path->nodes[i-1].coord.z].tertype==TerrainTile::water))
  270. ||
  271. (CGI->mh->map->terrain[path->nodes[i-1].coord.x][path->nodes[i-1].coord.y][path->nodes[i-1].coord.z].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->getPlayerTeam(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] && gs->curB->heroes[0]->getArt(17);
  552. else
  553. return gs->curB->castSpells[1] == 0 && gs->curB->heroes[1] && 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[gs->getPlayerTeam(player)->id] / 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 gs->getPlayerRelations( 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. std::vector < const CGObjectInstance * > CCallback::getMyObjects() const
  855. {
  856. std::vector < const CGObjectInstance * > ret;
  857. for (int g=0; g<gs->map->objects.size(); ++g)
  858. {
  859. if (gs->map->objects[g]->tempOwner == LOCPLINT->playerID)
  860. {
  861. ret.push_back(gs->map->objects[g]);
  862. }
  863. }
  864. return ret;
  865. }
  866. std::vector < const CGDwelling * > CCallback::getMyDwellings() const
  867. {
  868. std::vector < const CGDwelling * > ret;
  869. BOOST_FOREACH(CGDwelling * dw, gs->getPlayer(player)->dwellings)
  870. {
  871. ret.push_back(dw);
  872. }
  873. return ret;
  874. }
  875. int CCallback::getPlayerRelations( ui8 color1, ui8 color2 ) const
  876. {
  877. return gs->getPlayerRelations(color1, color2);
  878. }
  879. InfoAboutTown::InfoAboutTown()
  880. {
  881. tType = NULL;
  882. details = NULL;
  883. fortLevel = 0;
  884. owner = -1;
  885. }
  886. InfoAboutTown::~InfoAboutTown()
  887. {
  888. delete details;
  889. }
  890. void InfoAboutTown::initFromTown( const CGTownInstance *t, bool detailed )
  891. {
  892. obj = t;
  893. army = t->getArmy();
  894. built = t->builded;
  895. fortLevel = t->fortLevel();
  896. name = t->name;
  897. tType = t->town;
  898. owner = t->tempOwner;
  899. if(detailed)
  900. {
  901. //include details about hero
  902. details = new Details;
  903. details->goldIncome = t->dailyIncome();
  904. details->customRes = vstd::contains(t->builtBuildings, 15);
  905. details->hallLevel = t->hallLevel();
  906. details->garrisonedHero = t->garrisonHero;
  907. }
  908. /*else
  909. {
  910. //hide info about hero stacks counts
  911. for(std::map<si32,std::pair<ui32,si32> >::iterator i = slots.begin(); i != slots.end(); ++i)
  912. {
  913. i->second.second = 0;
  914. }
  915. }*/
  916. }
  917. void InfoAboutTown::initFromGarrison(const CGGarrison *garr, bool detailed)
  918. {
  919. obj = garr;
  920. fortLevel = 0;
  921. army = garr->getArmy();
  922. name = CGI->generaltexth->names[33]; // "Garrison"
  923. owner = garr->tempOwner;
  924. built = false;
  925. tType = NULL;
  926. // Show detailed info only to owning player.
  927. if(detailed)
  928. {
  929. details = new InfoAboutTown::Details;
  930. details->customRes = false;
  931. details->garrisonedHero = false;
  932. details->goldIncome = -1;
  933. details->hallLevel = -1;
  934. }
  935. }