IGameCallback.cpp 36 KB

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