IGameCallback.cpp 38 KB

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