IGameCallback.cpp 36 KB

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