IGameCallback.cpp 36 KB

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