IGameCallback.cpp 38 KB

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