IGameCallback.cpp 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043
  1. #define VCMI_DLL
  2. #include "IGameCallback.h"
  3. #include "../lib/CGameState.h"
  4. #include "../lib/map.h"
  5. #include "CObjectHandler.h"
  6. #include "CHeroHandler.h"
  7. #include "../StartInfo.h"
  8. #include "CArtHandler.h"
  9. #include "CSpellHandler.h"
  10. #include "../lib/VCMI_Lib.h"
  11. #include <boost/random/linear_congruential.hpp>
  12. #include "CTownHandler.h"
  13. #include "BattleState.h"
  14. #include <boost/foreach.hpp>
  15. #include "NetPacks.h"
  16. #include <boost/bind.hpp>
  17. /*
  18. * IGameCallback.cpp, part of VCMI engine
  19. *
  20. * Authors: listed in file AUTHORS in main folder
  21. *
  22. * License: GNU General Public License v2.0 or later
  23. * Full text of license available in license.txt file, in main folder
  24. *
  25. */
  26. #define ERROR_SILENT_RET_VAL_IF(cond, txt, retVal) do {if(cond){return retVal;}} while(0)
  27. #define ERROR_RET_IF(cond, txt) do {if(cond){tlog1 << BOOST_CURRENT_FUNCTION << ": " << txt << std::endl; return;}} while(0)
  28. #define ERROR_RET_VAL_IF(cond, txt, retVal) do {if(cond){tlog1 << BOOST_CURRENT_FUNCTION << ": " << txt << std::endl; return retVal;}} while(0)
  29. //#define ERROR_RET_IF(cond, printCond, txt, retVal) do {if(cond){ if(printCond) tlog1 << BOOST_CURRENT_FUNCTION << ": " << txt << std::endl; return retVal;}} while(0)
  30. extern boost::rand48 ran;
  31. si8 CBattleInfoCallback::battleHasDistancePenalty( const CStack * stack, THex destHex )
  32. {
  33. return gs->curB->hasDistancePenalty(stack, destHex);
  34. }
  35. si8 CBattleInfoCallback::battleHasWallPenalty( const CStack * stack, THex destHex )
  36. {
  37. return gs->curB->hasWallPenalty(stack, destHex);
  38. }
  39. si8 CBattleInfoCallback::battleCanTeleportTo(const CStack * stack, THex destHex, int telportLevel)
  40. {
  41. return gs->curB->canTeleportTo(stack, destHex, telportLevel);
  42. }
  43. std::vector<int> CBattleInfoCallback::battleGetDistances(const CStack * stack, THex hex /*= THex::INVALID*/, THex * predecessors /*= NULL*/)
  44. {
  45. if(!hex.isValid())
  46. hex = stack->position;
  47. std::vector<int> ret;
  48. bool ac[BFIELD_SIZE] = {0};
  49. std::set<THex> occupyable;
  50. gs->curB->getAccessibilityMap(ac, stack->doubleWide(), stack->attackerOwned, false, occupyable, stack->hasBonusOfType(Bonus::FLYING), stack);
  51. THex pr[BFIELD_SIZE];
  52. int dist[BFIELD_SIZE];
  53. gs->curB->makeBFS(stack->position, ac, pr, dist, stack->doubleWide(), stack->attackerOwned, stack->hasBonusOfType(Bonus::FLYING), false);
  54. for(int i=0; i<BFIELD_SIZE; ++i)
  55. {
  56. if(pr[i] == -1)
  57. ret.push_back(-1);
  58. else
  59. ret.push_back(dist[i]);
  60. }
  61. if(predecessors)
  62. {
  63. memcpy(predecessors, pr, BFIELD_SIZE * sizeof(THex));
  64. }
  65. return ret;
  66. }
  67. SpellCasting::ESpellCastProblem CBattleInfoCallback::battleCanCastThisSpell( const CSpell * spell )
  68. {
  69. if(!gs->curB)
  70. {
  71. tlog1 << "battleCanCastThisSpell called when there is no battle!\n";
  72. return SpellCasting::NO_HERO_TO_CAST_SPELL;
  73. }
  74. return gs->curB->battleCanCastThisSpell(player, spell, SpellCasting::HERO_CASTING);
  75. }
  76. si8 CBattleInfoCallback::battleGetTacticDist()
  77. {
  78. if (!gs->curB)
  79. {
  80. tlog1 << "battleGetTacticDist called when no battle!\n";
  81. return 0;
  82. }
  83. if (gs->curB->sides[gs->curB->tacticsSide] == player)
  84. {
  85. return gs->curB->tacticDistance;
  86. }
  87. return 0;
  88. }
  89. ui8 CBattleInfoCallback::battleGetMySide()
  90. {
  91. if (!gs->curB)
  92. {
  93. tlog1 << "battleGetMySide called when no battle!\n";
  94. return 0;
  95. }
  96. return gs->curB->sides[1] == player;
  97. }
  98. int CBattleInfoCallback::battleGetSurrenderCost()
  99. {
  100. if (!gs->curB)
  101. {
  102. tlog1 << "battleGetSurrenderCost called when no battle!\n";
  103. return -1;
  104. }
  105. return gs->curB->getSurrenderingCost(player);
  106. }
  107. int CBattleInfoCallback::battleGetBattlefieldType()
  108. {
  109. //boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  110. //return gs->battleGetBattlefieldType();
  111. if(!gs->curB)
  112. {
  113. tlog2<<"battleGetBattlefieldType called when there is no battle!"<<std::endl;
  114. return -1;
  115. }
  116. return gs->curB->battlefieldType;
  117. }
  118. int CBattleInfoCallback::battleGetObstaclesAtTile(THex tile) //returns bitfield
  119. {
  120. //TODO - write
  121. return -1;
  122. }
  123. std::vector<CObstacleInstance> CBattleInfoCallback::battleGetAllObstacles()
  124. {
  125. //boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  126. if(gs->curB)
  127. return gs->curB->obstacles;
  128. else
  129. return std::vector<CObstacleInstance>();
  130. }
  131. const CStack* CBattleInfoCallback::battleGetStackByID(int ID, bool onlyAlive)
  132. {
  133. //boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  134. if(!gs->curB) return NULL;
  135. return gs->curB->getStack(ID, onlyAlive);
  136. }
  137. const CStack* CBattleInfoCallback::battleGetStackByPos(THex pos, bool onlyAlive)
  138. {
  139. //boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  140. return gs->curB->battleGetStack(pos, onlyAlive);
  141. }
  142. THex CBattleInfoCallback::battleGetPos(int stack)
  143. {
  144. //boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  145. if(!gs->curB)
  146. {
  147. tlog2<<"battleGetPos called when there is no battle!"<<std::endl;
  148. return THex::INVALID;
  149. }
  150. for(size_t g=0; g<gs->curB->stacks.size(); ++g)
  151. {
  152. if(gs->curB->stacks[g]->ID == stack)
  153. return gs->curB->stacks[g]->position;
  154. }
  155. return THex::INVALID;
  156. }
  157. TStacks CBattleInfoCallback::battleGetStacks(EStackOwnership whose /*= MINE_AND_ENEMY*/, bool onlyAlive /*= true*/)
  158. {
  159. //boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  160. TStacks ret;
  161. if(!gs->curB) //there is no battle
  162. {
  163. tlog2<<"battleGetStacks called when there is no battle!"<<std::endl;
  164. return ret;
  165. }
  166. BOOST_FOREACH(const CStack *s, gs->curB->stacks)
  167. {
  168. bool ownerMatches = whose == MINE_AND_ENEMY || whose == ONLY_MINE && s->owner == player || whose == ONLY_ENEMY && s->owner != player;
  169. bool alivenessMatches = s->alive() || !onlyAlive;
  170. if(ownerMatches && alivenessMatches)
  171. ret.push_back(s);
  172. }
  173. return ret;
  174. }
  175. void CBattleInfoCallback::getStackQueue( std::vector<const CStack *> &out, int howMany )
  176. {
  177. if(!gs->curB)
  178. {
  179. tlog2 << "battleGetStackQueue called when there is not battle!" << std::endl;
  180. return;
  181. }
  182. gs->curB->getStackQueue(out, howMany);
  183. }
  184. std::vector<THex> CBattleInfoCallback::battleGetAvailableHexes(const CStack * stack, bool addOccupiable, std::vector<THex> * attackable)
  185. {
  186. //boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  187. if(!gs->curB)
  188. {
  189. tlog2<<"battleGetAvailableHexes called when there is no battle!"<<std::endl;
  190. return std::vector<THex>();
  191. }
  192. return gs->curB->getAccessibility(stack, addOccupiable, attackable);
  193. //return gs->battleGetRange(ID);
  194. }
  195. bool CBattleInfoCallback::battleCanShoot(const CStack * stack, THex dest)
  196. {
  197. //boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  198. if(!gs->curB) return false;
  199. return gs->curB->battleCanShoot(stack, dest);
  200. }
  201. bool CBattleInfoCallback::battleCanCastSpell()
  202. {
  203. if(!gs->curB) //there is no battle
  204. return false;
  205. return gs->curB->battleCanCastSpell(player, SpellCasting::HERO_CASTING) == SpellCasting::OK;
  206. }
  207. bool CBattleInfoCallback::battleCanFlee()
  208. {
  209. return gs->curB->battleCanFlee(player);
  210. }
  211. const CGTownInstance *CBattleInfoCallback::battleGetDefendedTown()
  212. {
  213. if(!gs->curB || gs->curB->town == NULL)
  214. return NULL;
  215. return gs->curB->town;
  216. }
  217. ui8 CBattleInfoCallback::battleGetWallState(int partOfWall)
  218. {
  219. if(!gs->curB || gs->curB->siege == 0)
  220. {
  221. return 0;
  222. }
  223. return gs->curB->si.wallState[partOfWall];
  224. }
  225. int CBattleInfoCallback::battleGetWallUnderHex(THex hex)
  226. {
  227. if(!gs->curB || gs->curB->siege == 0)
  228. {
  229. return -1;
  230. }
  231. return gs->curB->hexToWallPart(hex);
  232. }
  233. TDmgRange CBattleInfoCallback::battleEstimateDamage(const CStack * attacker, const CStack * defender, TDmgRange * retaliationDmg)
  234. {
  235. if(!gs->curB)
  236. return std::make_pair(0, 0);
  237. const CGHeroInstance * attackerHero, * defenderHero;
  238. bool shooting = battleCanShoot(attacker, defender->position);
  239. if(gs->curB->sides[0] == attacker->owner)
  240. {
  241. attackerHero = gs->curB->heroes[0];
  242. defenderHero = gs->curB->heroes[1];
  243. }
  244. else
  245. {
  246. attackerHero = gs->curB->heroes[1];
  247. defenderHero = gs->curB->heroes[0];
  248. }
  249. TDmgRange ret = gs->curB->calculateDmgRange(attacker, defender, attackerHero, defenderHero, shooting, 0, false, false);
  250. if(retaliationDmg)
  251. {
  252. if(shooting)
  253. {
  254. retaliationDmg->first = retaliationDmg->second = 0;
  255. }
  256. else
  257. {
  258. ui32 TDmgRange::* pairElems[] = {&TDmgRange::first, &TDmgRange::second};
  259. for (int i=0; i<2; ++i)
  260. {
  261. BattleStackAttacked bsa;
  262. bsa.damageAmount = ret.*pairElems[i];
  263. retaliationDmg->*pairElems[!i] = gs->curB->calculateDmgRange(defender, attacker, bsa.newAmount, attacker->count, attackerHero, defenderHero, false, false, false, false).*pairElems[!i];
  264. }
  265. }
  266. }
  267. return ret;
  268. }
  269. ui8 CBattleInfoCallback::battleGetSiegeLevel()
  270. {
  271. if(!gs->curB)
  272. return 0;
  273. return gs->curB->siege;
  274. }
  275. const CGHeroInstance * CBattleInfoCallback::battleGetFightingHero(ui8 side) const
  276. {
  277. if(!gs->curB)
  278. return 0;
  279. return gs->curB->heroes[side];
  280. }
  281. CGameState *const CPrivilagedInfoCallback::gameState ()
  282. {
  283. return gs;
  284. }
  285. int CGameInfoCallback::getOwner(int heroID) const
  286. {
  287. const CGObjectInstance *obj = getObj(heroID);
  288. ERROR_RET_VAL_IF(!obj, "No such object!", -1);
  289. return gs->map->objects[heroID]->tempOwner;
  290. }
  291. int CGameInfoCallback::getResource(int Player, int which) const
  292. {
  293. const PlayerState *p = getPlayerState(Player);
  294. ERROR_RET_VAL_IF(!p, "No player info!", -1);
  295. ERROR_RET_VAL_IF(p->resources.size() <= which || which < 0, "No such resource!", -1);
  296. return p->resources[which];
  297. }
  298. const CGHeroInstance* CGameInfoCallback::getSelectedHero( int Player ) const
  299. {
  300. const PlayerState *p = getPlayerState(Player);
  301. ERROR_RET_VAL_IF(!p, "No player info!", NULL);
  302. return getHero(p->currentSelection);
  303. }
  304. int CGameInfoCallback::getSelectedHero() const
  305. {
  306. const CGHeroInstance *h = getSelectedHero(gs->currentPlayer);
  307. return h ? h->id : -1; // TODO synchronous turns?
  308. }
  309. const PlayerSettings * CGameInfoCallback::getPlayerSettings(int color) const
  310. {
  311. return &gs->scenarioOps->getIthPlayersSettings(color);
  312. }
  313. void CPrivilagedInfoCallback::getTilesInRange( boost::unordered_set<int3, ShashInt3> &tiles, int3 pos, int radious, int player/*=-1*/, int mode/*=0*/ ) const
  314. {
  315. if(player >= PLAYER_LIMIT)
  316. {
  317. tlog1 << "Illegal call to getTilesInRange!\n";
  318. return;
  319. }
  320. if (radious == -1) //reveal entire map
  321. getAllTiles (tiles, player, -1, 0);
  322. else
  323. {
  324. const TeamState * team = gs->getPlayerTeam(player);
  325. for (int xd = std::max<int>(pos.x - radious , 0); xd <= std::min<int>(pos.x + radious, gs->map->width - 1); xd++)
  326. {
  327. for (int yd = std::max<int>(pos.y - radious, 0); yd <= std::min<int>(pos.y + radious, gs->map->height - 1); yd++)
  328. {
  329. double distance = pos.dist2d(int3(xd,yd,pos.z)) - 0.5;
  330. if(distance <= radious)
  331. {
  332. if(player < 0
  333. || (mode == 1 && team->fogOfWarMap[xd][yd][pos.z]==0)
  334. || (mode == -1 && team->fogOfWarMap[xd][yd][pos.z]==1)
  335. )
  336. tiles.insert(int3(xd,yd,pos.z));
  337. }
  338. }
  339. }
  340. }
  341. }
  342. void CPrivilagedInfoCallback::getAllTiles (boost::unordered_set<int3, ShashInt3> &tiles, int Player/*=-1*/, int level, int surface ) const
  343. {
  344. if(Player >= PLAYER_LIMIT)
  345. {
  346. tlog1 << "Illegal call to getAllTiles !\n";
  347. return;
  348. }
  349. bool water = surface == 0 || surface == 2,
  350. land = surface == 0 || surface == 1;
  351. std::vector<int> floors;
  352. if(level == -1)
  353. {
  354. for (int xd = 0; xd <= gs->map->width - 1; xd++)
  355. for(int b=0; b<gs->map->twoLevel + 1; ++b) //if gs->map->twoLevel is false then false (0) + 1 is 1, if it's true (1) then we have 2
  356. {
  357. floors.push_back(b);
  358. }
  359. }
  360. else
  361. floors.push_back(level);
  362. for (std::vector<int>::const_iterator i = floors.begin(); i!= floors.end(); i++)
  363. {
  364. register int zd = *i;
  365. for (int xd = 0; xd < gs->map->width; xd++)
  366. {
  367. for (int yd = 0; yd < gs->map->height; yd++)
  368. {
  369. if ((getTile (int3 (xd,yd,zd))->tertype == 8 && water)
  370. || (getTile (int3 (xd,yd,zd))->tertype != 8 && land))
  371. tiles.insert(int3(xd,yd,zd));
  372. }
  373. }
  374. }
  375. }
  376. void CPrivilagedInfoCallback::getFreeTiles (std::vector<int3> &tiles) const
  377. {
  378. std::vector<int> floors;
  379. for (int b=0; b<gs->map->twoLevel + 1; ++b) //if gs->map->twoLevel is false then false (0) + 1 is 1, if it's true (1) then we have 2
  380. {
  381. floors.push_back(b);
  382. }
  383. TerrainTile *tinfo;
  384. for (std::vector<int>::const_iterator i = floors.begin(); i!= floors.end(); i++)
  385. {
  386. register int zd = *i;
  387. for (int xd = 0; xd < gs->map->width; xd++)
  388. {
  389. for (int yd = 0; yd < gs->map->height; yd++)
  390. {
  391. tinfo = getTile(int3 (xd,yd,zd));
  392. if (tinfo->tertype != 8 && !tinfo->blocked) //land and free
  393. tiles.push_back (int3 (xd,yd,zd));
  394. }
  395. }
  396. }
  397. }
  398. bool CGameInfoCallback::isAllowed( int type, int id )
  399. {
  400. switch(type)
  401. {
  402. case 0:
  403. return gs->map->allowedSpell[id];
  404. case 1:
  405. return gs->map->allowedArtifact[id];
  406. case 2:
  407. return gs->map->allowedAbilities[id];
  408. default:
  409. ERROR_RET_VAL_IF(1, "Wrong type!", false);
  410. }
  411. }
  412. void CPrivilagedInfoCallback::pickAllowedArtsSet(std::vector<const CArtifact*> &out)
  413. {
  414. for (int i = 0; i < 2; i++)
  415. {
  416. for (int j = 0; j < 3 ; j++)
  417. {
  418. out.push_back(VLC->arth->artifacts[getRandomArt(CArtifact::ART_TREASURE << i)]);
  419. }
  420. }
  421. out.push_back(VLC->arth->artifacts[getRandomArt(CArtifact::ART_MAJOR)]);
  422. }
  423. ui16 CPrivilagedInfoCallback::getRandomArt (int flags)
  424. {
  425. return VLC->arth->getRandomArt(flags);
  426. }
  427. ui16 CPrivilagedInfoCallback::getArtSync (ui32 rand, int flags)
  428. {
  429. return VLC->arth->getArtSync (rand, flags);
  430. }
  431. void CPrivilagedInfoCallback::erasePickedArt (si32 id)
  432. {
  433. VLC->arth->erasePickedArt(id);
  434. }
  435. void CPrivilagedInfoCallback::getAllowedSpells(std::vector<ui16> &out, ui16 level)
  436. {
  437. CSpell *spell;
  438. for (int i = 0; i < gs->map->allowedSpell.size(); i++) //spellh size appears to be greater (?)
  439. {
  440. spell = VLC->spellh->spells[i];
  441. if (isAllowed (0, spell->id) && spell->level == level)
  442. {
  443. out.push_back(spell->id);
  444. }
  445. }
  446. }
  447. inline TerrainTile * CPrivilagedInfoCallback::getTile( int3 pos ) const
  448. {
  449. if(!gs->map->isInTheMap(pos))
  450. return NULL;
  451. return &gs->map->getTile(pos);
  452. }
  453. const PlayerState * CGameInfoCallback::getPlayerState(int color) const
  454. {
  455. ERROR_RET_VAL_IF(!hasAccess(color), "Cannot access player " << color << "info!", NULL);
  456. return gs->getPlayer(color, false);
  457. }
  458. const CTown * CGameInfoCallback::getNativeTown(int color) const
  459. {
  460. const PlayerSettings *ps = getPlayerSettings(color);
  461. ERROR_RET_VAL_IF(!ps, "There is no such player!", NULL);
  462. return &VLC->townh->towns[ps->castle];
  463. }
  464. const CGObjectInstance * CGameInfoCallback::getObjByQuestIdentifier(int identifier) const
  465. {
  466. ERROR_RET_VAL_IF(!vstd::contains(gs->map->questIdentifierToId, identifier), "There is no object with such quest identifier!", NULL);
  467. return getObj(gs->map->questIdentifierToId[identifier]);
  468. }
  469. /************************************************************************/
  470. /* */
  471. /************************************************************************/
  472. const CGObjectInstance* CGameInfoCallback::getObj(int objid, bool verbose) const
  473. {
  474. if(objid < 0 || objid >= gs->map->objects.size())
  475. {
  476. if(verbose)
  477. tlog1 << "Cannot get object with id " << objid << std::endl;
  478. return NULL;
  479. }
  480. const CGObjectInstance *ret = gs->map->objects[objid];
  481. if(!ret)
  482. {
  483. if(verbose)
  484. tlog1 << "Cannot get object with id " << objid << ". Object was removed.\n";
  485. return NULL;
  486. }
  487. if(!isVisible(ret, player))
  488. {
  489. if(verbose)
  490. tlog1 << "Cannot get object with id " << objid << ". Object is not visible.\n";
  491. return NULL;
  492. }
  493. return ret;
  494. }
  495. const CGHeroInstance* CGameInfoCallback::getHero(int objid) const
  496. {
  497. const CGObjectInstance *obj = getObj(objid, false);
  498. if(obj)
  499. return dynamic_cast<const CGHeroInstance*>(obj);
  500. else
  501. return NULL;
  502. }
  503. const CGTownInstance* CGameInfoCallback::getTown(int objid) const
  504. {
  505. const CGObjectInstance *obj = getObj(objid, false);
  506. if(obj)
  507. return dynamic_cast<const CGTownInstance*>(gs->map->objects[objid].get());
  508. else
  509. return NULL;
  510. }
  511. void CGameInfoCallback::getUpgradeInfo(const CArmedInstance *obj, int stackPos, UpgradeInfo &out) const
  512. {
  513. //boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  514. ERROR_RET_IF(!canGetFullInfo(obj), "Cannot get info about not owned object!");
  515. ERROR_RET_IF(!obj->hasStackAtSlot(stackPos), "There is no such stack!");
  516. out = gs->getUpgradeInfo(obj->getStack(stackPos));
  517. //return gs->getUpgradeInfo(obj->getStack(stackPos));
  518. }
  519. const StartInfo * CGameInfoCallback::getStartInfo() const
  520. {
  521. //boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  522. return gs->scenarioOps;
  523. }
  524. int CGameInfoCallback::getSpellCost(const CSpell * sp, const CGHeroInstance * caster) const
  525. {
  526. //boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  527. ERROR_RET_VAL_IF(!canGetFullInfo(caster), "Cannot get info about caster!", -1);
  528. //if there is a battle
  529. if(gs->curB)
  530. return gs->curB->getSpellCost(sp, caster);
  531. //if there is no battle
  532. return caster->getSpellCost(sp);
  533. }
  534. int CGameInfoCallback::estimateSpellDamage(const CSpell * sp, const CGHeroInstance * hero) const
  535. {
  536. //boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  537. ERROR_RET_VAL_IF(hero && !canGetFullInfo(hero), "Cannot get info about caster!", -1);
  538. if(!gs->curB) //no battle
  539. {
  540. if (hero) //but we see hero's spellbook
  541. return gs->curB->calculateSpellDmg(sp, hero, NULL, hero->getSpellSchoolLevel(sp), hero->getPrimSkillLevel(2));
  542. else
  543. return 0; //mage guild
  544. }
  545. //gs->getHero(gs->currentPlayer)
  546. //const CGHeroInstance * ourHero = gs->curB->heroes[0]->tempOwner == player ? gs->curB->heroes[0] : gs->curB->heroes[1];
  547. const CGHeroInstance * ourHero = hero;
  548. return gs->curB->calculateSpellDmg(sp, ourHero, NULL, ourHero->getSpellSchoolLevel(sp), ourHero->getPrimSkillLevel(2));
  549. }
  550. void CGameInfoCallback::getThievesGuildInfo(SThievesGuildInfo & thi, const CGObjectInstance * obj)
  551. {
  552. //boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  553. ERROR_RET_IF(!obj, "No guild object!");
  554. ERROR_RET_IF(obj->ID == TOWNI_TYPE && !canGetFullInfo(obj), "Cannot get info about town guild object!");
  555. //TODO: advmap object -> check if they're visited by our hero
  556. if(obj->ID == TOWNI_TYPE || obj->ID == 95) //it is a town or adv map tavern
  557. {
  558. gs->obtainPlayersStats(thi, gs->players[obj->tempOwner].towns.size());
  559. }
  560. else if(obj->ID == 97) //Den of Thieves
  561. {
  562. gs->obtainPlayersStats(thi, 20);
  563. }
  564. }
  565. int CGameInfoCallback::howManyTowns(int Player) const
  566. {
  567. ERROR_RET_VAL_IF(!hasAccess(Player), "Access forbidden!", -1);
  568. return gs->players[Player].towns.size();
  569. }
  570. bool CGameInfoCallback::getTownInfo( const CGObjectInstance *town, InfoAboutTown &dest ) const
  571. {
  572. ERROR_RET_VAL_IF(!isVisible(town, player), "Town is not visible!", false); //it's not a town or it's not visible for layer
  573. bool detailed = hasAccess(town->tempOwner);
  574. //TODO vision support
  575. if(town->ID == TOWNI_TYPE)
  576. dest.initFromTown(static_cast<const CGTownInstance *>(town), detailed);
  577. else if(town->ID == 33 || town->ID == 219)
  578. dest.initFromGarrison(static_cast<const CGGarrison *>(town), detailed);
  579. else
  580. return false;
  581. return true;
  582. }
  583. int3 CGameInfoCallback::guardingCreaturePosition (int3 pos) const
  584. {
  585. ERROR_RET_VAL_IF(!isVisible(pos), "Tile is not visible!", int3(-1,-1,-1));
  586. return gs->guardingCreaturePosition(pos);
  587. }
  588. bool CGameInfoCallback::getHeroInfo( const CGObjectInstance *hero, InfoAboutHero &dest ) const
  589. {
  590. const CGHeroInstance *h = dynamic_cast<const CGHeroInstance *>(hero);
  591. ERROR_RET_VAL_IF(!h, "That's not a hero!", false);
  592. ERROR_RET_VAL_IF(!isVisible(h->getPosition(false)), "That hero is not visible!", false);
  593. //TODO vision support
  594. dest.initFromHero(h, hasAccess(h->tempOwner));
  595. return true;
  596. }
  597. int CGameInfoCallback::getDate(int mode) const
  598. {
  599. //boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  600. return gs->getDate(mode);
  601. }
  602. std::vector < std::string > CGameInfoCallback::getObjDescriptions(int3 pos) const
  603. {
  604. //boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  605. std::vector<std::string> ret;
  606. const TerrainTile *t = getTileInfo(pos);
  607. ERROR_RET_VAL_IF(!t, "Not a valid tile given!", ret);
  608. BOOST_FOREACH(const CGObjectInstance * obj, t->blockingObjects)
  609. ret.push_back(obj->getHoverText());
  610. return ret;
  611. }
  612. bool CGameInfoCallback::verifyPath(CPath * path, bool blockSea) const
  613. {
  614. for (size_t i=0; i < path->nodes.size(); ++i)
  615. {
  616. const TerrainTile *t = getTileInfo(path->nodes[i].coord); //current tile
  617. if (t->blocked && !t->visitable)
  618. return false; //path is wrong - one of the tiles is blocked
  619. if (blockSea)
  620. {
  621. if (i==0)
  622. continue;
  623. const TerrainTile *prev = getTileInfo(path->nodes[i-1].coord); //tile of previous node on the path
  624. if (( t->tertype == TerrainTile::water && prev->tertype != TerrainTile::water)
  625. || (t->tertype != TerrainTile::water && prev->tertype == TerrainTile::water)
  626. || prev->tertype == TerrainTile::rock
  627. )
  628. return false;
  629. }
  630. }
  631. return true;
  632. }
  633. bool CGameInfoCallback::isVisible(int3 pos, int Player) const
  634. {
  635. //boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  636. return gs->map->isInTheMap(pos) && gs->isVisible(pos, Player);
  637. }
  638. bool CGameInfoCallback::isVisible(int3 pos) const
  639. {
  640. return isVisible(pos,player);
  641. }
  642. bool CGameInfoCallback::isVisible( const CGObjectInstance *obj, int Player ) const
  643. {
  644. return gs->isVisible(obj, Player);
  645. }
  646. bool CGameInfoCallback::isVisible(const CGObjectInstance *obj) const
  647. {
  648. return isVisible(obj, player);
  649. }
  650. // const CCreatureSet* CInfoCallback::getGarrison(const CGObjectInstance *obj) const
  651. // {
  652. // //boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  653. // if()
  654. // const CArmedInstance *armi = dynamic_cast<const CArmedInstance*>(obj);
  655. // if(!armi)
  656. // return NULL;
  657. // else
  658. // return armi;
  659. // }
  660. std::vector < const CGObjectInstance * > CGameInfoCallback::getBlockingObjs( int3 pos ) const
  661. {
  662. std::vector<const CGObjectInstance *> ret;
  663. const TerrainTile *t = getTileInfo(pos);
  664. ERROR_RET_VAL_IF(!t, "Not a valid tile requested!", ret);
  665. BOOST_FOREACH(const CGObjectInstance * obj, t->blockingObjects)
  666. ret.push_back(obj);
  667. return ret;
  668. }
  669. std::vector < const CGObjectInstance * > CGameInfoCallback::getVisitableObjs( int3 pos ) const
  670. {
  671. std::vector<const CGObjectInstance *> ret;
  672. const TerrainTile *t = getTileInfo(pos);
  673. ERROR_RET_VAL_IF(!t, "Not a valid tile requested!", ret);
  674. //boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  675. BOOST_FOREACH(const CGObjectInstance * obj, t->visitableObjects)
  676. ret.push_back(obj);
  677. return ret;
  678. }
  679. std::vector < const CGObjectInstance * > CGameInfoCallback::getFlaggableObjects(int3 pos) const
  680. {
  681. std::vector<const CGObjectInstance *> ret;
  682. const TerrainTile *t = getTileInfo(pos);
  683. ERROR_RET_VAL_IF(!t, "Not a valid tile requested!", ret);
  684. BOOST_FOREACH(const CGObjectInstance *obj, t->blockingObjects)
  685. if(obj->tempOwner != 254)
  686. ret.push_back(obj);
  687. // const std::vector < std::pair<const CGObjectInstance*,SDL_Rect> > & objs = CGI->mh->ttiles[pos.x][pos.y][pos.z].objects;
  688. // for(size_t b=0; b<objs.size(); ++b)
  689. // {
  690. // 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))
  691. // ret.push_back(CGI->mh->ttiles[pos.x][pos.y][pos.z].objects[b].first);
  692. // }
  693. return ret;
  694. }
  695. int3 CGameInfoCallback::getMapSize() const
  696. {
  697. return int3(gs->map->width, gs->map->height, gs->map->twoLevel+1);
  698. }
  699. std::vector<const CGHeroInstance *> CGameInfoCallback::getAvailableHeroes(const CGObjectInstance * townOrTavern) const
  700. {
  701. std::vector<const CGHeroInstance *> ret;
  702. ERROR_RET_VAL_IF(!isOwnedOrVisited(townOrTavern), "Town or tavern must be owned or visited!", ret);
  703. ret.resize(gs->players[player].availableHeroes.size());
  704. std::copy(gs->players[player].availableHeroes.begin(),gs->players[player].availableHeroes.end(),ret.begin());
  705. return ret;
  706. }
  707. const TerrainTile * CGameInfoCallback::getTileInfo( int3 tile ) const
  708. {
  709. //ERROR_RET_VAL_IF(!gs->map->isInTheMap(tile), tile << " is outside the map!", NULL);
  710. ERROR_SILENT_RET_VAL_IF(!isVisible(tile), tile << " is not visible!", NULL);
  711. //boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  712. return &gs->map->getTile(tile);
  713. }
  714. int CGameInfoCallback::canBuildStructure( const CGTownInstance *t, int ID )
  715. {
  716. ERROR_RET_VAL_IF(!canGetFullInfo(t), "Town is not owned!", -1);
  717. return gs->canBuildStructure(t,ID);
  718. }
  719. std::set<int> CGameInfoCallback::getBuildingRequiments( const CGTownInstance *t, int ID )
  720. {
  721. ERROR_RET_VAL_IF(!canGetFullInfo(t), "Town is not owned!", std::set<int>());
  722. return gs->getBuildingRequiments(t,ID);
  723. }
  724. const CMapHeader * CGameInfoCallback::getMapHeader() const
  725. {
  726. return gs->map;
  727. }
  728. bool CGameInfoCallback::hasAccess(int playerId) const
  729. {
  730. return gs->getPlayerRelations( playerId, player ) || player < 0;
  731. }
  732. int CGameInfoCallback::getPlayerStatus(int player) const
  733. {
  734. const PlayerState *ps = gs->getPlayer(player, false);
  735. if(!ps)
  736. return -1;
  737. return ps->status;
  738. }
  739. std::string CGameInfoCallback::getTavernGossip(const CGObjectInstance * townOrTavern) const
  740. {
  741. return "GOSSIP TEST";
  742. }
  743. int CGameInfoCallback::getPlayerRelations( ui8 color1, ui8 color2 ) const
  744. {
  745. return gs->getPlayerRelations(color1, color2);
  746. }
  747. bool CGameInfoCallback::canGetFullInfo(const CGObjectInstance *obj) const
  748. {
  749. return !obj || hasAccess(obj->tempOwner);
  750. }
  751. int CGameInfoCallback::getHeroCount( int player, bool includeGarrisoned ) const
  752. {
  753. int ret = 0;
  754. const PlayerState *p = gs->getPlayer(player);
  755. ERROR_RET_VAL_IF(!p, "No such player!", -1);
  756. if(includeGarrisoned)
  757. return p->heroes.size();
  758. else
  759. for(int i=0; i < p->heroes.size(); i++)
  760. if(!p->heroes[i]->inTownGarrison)
  761. ret++;
  762. return ret;
  763. }
  764. bool CGameInfoCallback::isOwnedOrVisited(const CGObjectInstance *obj) const
  765. {
  766. if(canGetFullInfo(obj))
  767. return true;
  768. const TerrainTile *t = getTileInfo(obj->visitablePos()); //get entrance tile
  769. const CGObjectInstance *visitor = t->visitableObjects.back(); //visitong hero if present or the obejct itself at last
  770. return visitor->ID == HEROI_TYPE && canGetFullInfo(visitor); //owned or allied hero is a visitor
  771. }
  772. int CGameInfoCallback::getCurrentPlayer() const
  773. {
  774. return gs->currentPlayer;
  775. }
  776. CGameInfoCallback::CGameInfoCallback()
  777. {
  778. }
  779. const std::vector< std::vector< std::vector<unsigned char> > > & CPlayerSpecificInfoCallback::getVisibilityMap() const
  780. {
  781. //boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  782. return gs->getPlayerTeam(player)->fogOfWarMap;
  783. }
  784. int CPlayerSpecificInfoCallback::howManyTowns() const
  785. {
  786. //boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  787. ERROR_RET_VAL_IF(player == -1, "Applicable only for player callbacks", -1);
  788. return CGameInfoCallback::howManyTowns(player);
  789. }
  790. std::vector < const CGTownInstance *> CPlayerSpecificInfoCallback::getTownsInfo(bool onlyOur) const
  791. {
  792. //boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  793. std::vector < const CGTownInstance *> ret = std::vector < const CGTownInstance *>();
  794. for ( std::map<ui8, PlayerState>::iterator i=gs->players.begin() ; i!=gs->players.end();i++)
  795. {
  796. for (size_t j=0; j < (*i).second.towns.size(); ++j)
  797. {
  798. if ((*i).first==player
  799. || (isVisible((*i).second.towns[j],player) && !onlyOur))
  800. {
  801. ret.push_back((*i).second.towns[j]);
  802. }
  803. }
  804. } // for ( std::map<int, PlayerState>::iterator i=gs->players.begin() ; i!=gs->players.end();i++)
  805. return ret;
  806. }
  807. std::vector < const CGHeroInstance *> CPlayerSpecificInfoCallback::getHeroesInfo(bool onlyOur) const
  808. {
  809. //boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  810. std::vector < const CGHeroInstance *> ret;
  811. for(size_t i=0;i<gs->map->heroes.size();i++)
  812. {
  813. if( (gs->map->heroes[i]->tempOwner==player) ||
  814. (isVisible(gs->map->heroes[i]->getPosition(false),player) && !onlyOur) )
  815. {
  816. ret.push_back(gs->map->heroes[i]);
  817. }
  818. }
  819. return ret;
  820. }
  821. int CPlayerSpecificInfoCallback::getMyColor() const
  822. {
  823. return player;
  824. }
  825. int CPlayerSpecificInfoCallback::getHeroSerial(const CGHeroInstance * hero) const
  826. {
  827. //boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  828. for (size_t i=0; i<gs->players[player].heroes.size();i++)
  829. {
  830. if (gs->players[player].heroes[i]==hero)
  831. return i;
  832. }
  833. return -1;
  834. }
  835. int3 CPlayerSpecificInfoCallback::getGrailPos( float &outKnownRatio )
  836. {
  837. if (CGObelisk::obeliskCount == 0)
  838. {
  839. outKnownRatio = 0.0f;
  840. }
  841. else
  842. {
  843. outKnownRatio = (float)CGObelisk::visited[gs->getPlayerTeam(player)->id] / CGObelisk::obeliskCount;
  844. }
  845. return gs->map->grailPos;
  846. }
  847. std::vector < const CGObjectInstance * > CPlayerSpecificInfoCallback::getMyObjects() const
  848. {
  849. std::vector < const CGObjectInstance * > ret;
  850. BOOST_FOREACH(const CGObjectInstance * obj, gs->map->objects)
  851. {
  852. if(obj && obj->tempOwner == player)
  853. ret.push_back(obj);
  854. }
  855. return ret;
  856. }
  857. std::vector < const CGDwelling * > CPlayerSpecificInfoCallback::getMyDwellings() const
  858. {
  859. std::vector < const CGDwelling * > ret;
  860. BOOST_FOREACH(CGDwelling * dw, gs->getPlayer(player)->dwellings)
  861. {
  862. ret.push_back(dw);
  863. }
  864. return ret;
  865. }
  866. int CPlayerSpecificInfoCallback::howManyHeroes(bool includeGarrisoned) const
  867. {
  868. //boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  869. ERROR_RET_VAL_IF(player == -1, "Applicable only for player callbacks", -1);
  870. return getHeroCount(player,includeGarrisoned);
  871. }
  872. const CGHeroInstance* CPlayerSpecificInfoCallback::getHeroBySerial(int serialId) const
  873. {
  874. const PlayerState *p = getPlayerState(serialId);
  875. ERROR_RET_VAL_IF(!p, "No player info", NULL);
  876. ERROR_RET_VAL_IF(serialId < 0 || serialId >= p->heroes.size(), "No player info", NULL);
  877. return p->heroes[serialId];
  878. }
  879. const CGTownInstance* CPlayerSpecificInfoCallback::getTownBySerial(int serialId) const
  880. {
  881. const PlayerState *p = getPlayerState(serialId);
  882. ERROR_RET_VAL_IF(!p, "No player info", NULL);
  883. ERROR_RET_VAL_IF(serialId < 0 || serialId >= p->towns.size(), "No player info", NULL);
  884. return p->towns[serialId];
  885. }
  886. int CPlayerSpecificInfoCallback::getResourceAmount(int type) const
  887. {
  888. //boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  889. ERROR_RET_VAL_IF(player == -1, "Applicable only for player callbacks", -1);
  890. return getResource(player, type);
  891. }
  892. std::vector<si32> CPlayerSpecificInfoCallback::getResourceAmount() const
  893. {
  894. //boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  895. ERROR_RET_VAL_IF(player == -1, "Applicable only for player callbacks", std::vector<si32>());
  896. return gs->players[player].resources;
  897. }