IGameCallback.cpp 39 KB

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