IGameCallback.cpp 35 KB

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