IGameCallback.cpp 37 KB

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