IGameCallback.cpp 37 KB

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