CCallback.cpp 27 KB

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