IGameCallback.cpp 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363
  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 * 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(bool beforeRandomization /*= false*/) const
  613. {
  614. //boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  615. if(beforeRandomization)
  616. return gs->initialOpts;
  617. else
  618. return gs->scenarioOps;
  619. }
  620. int CGameInfoCallback::getSpellCost(const CSpell * sp, const CGHeroInstance * caster) const
  621. {
  622. //boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  623. ERROR_RET_VAL_IF(!canGetFullInfo(caster), "Cannot get info about caster!", -1);
  624. //if there is a battle
  625. if(gs->curB)
  626. return gs->curB->getSpellCost(sp, caster);
  627. //if there is no battle
  628. return caster->getSpellCost(sp);
  629. }
  630. int CGameInfoCallback::estimateSpellDamage(const CSpell * sp, const CGHeroInstance * hero) const
  631. {
  632. //boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  633. ERROR_RET_VAL_IF(hero && !canGetFullInfo(hero), "Cannot get info about caster!", -1);
  634. if(!gs->curB) //no battle
  635. {
  636. if (hero) //but we see hero's spellbook
  637. return gs->curB->calculateSpellDmg(sp, hero, NULL, hero->getSpellSchoolLevel(sp), hero->getPrimSkillLevel(2));
  638. else
  639. return 0; //mage guild
  640. }
  641. //gs->getHero(gs->currentPlayer)
  642. //const CGHeroInstance * ourHero = gs->curB->heroes[0]->tempOwner == player ? gs->curB->heroes[0] : gs->curB->heroes[1];
  643. const CGHeroInstance * ourHero = hero;
  644. return gs->curB->calculateSpellDmg(sp, ourHero, NULL, ourHero->getSpellSchoolLevel(sp), ourHero->getPrimSkillLevel(2));
  645. }
  646. void CGameInfoCallback::getThievesGuildInfo(SThievesGuildInfo & thi, const CGObjectInstance * obj)
  647. {
  648. //boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  649. ERROR_RET_IF(!obj, "No guild object!");
  650. ERROR_RET_IF(obj->ID == GameConstants::TOWNI_TYPE && !canGetFullInfo(obj), "Cannot get info about town guild object!");
  651. //TODO: advmap object -> check if they're visited by our hero
  652. if(obj->ID == GameConstants::TOWNI_TYPE || obj->ID == 95) //it is a town or adv map tavern
  653. {
  654. gs->obtainPlayersStats(thi, gs->players[obj->tempOwner].towns.size());
  655. }
  656. else if(obj->ID == 97) //Den of Thieves
  657. {
  658. gs->obtainPlayersStats(thi, 20);
  659. }
  660. }
  661. int CGameInfoCallback::howManyTowns(int Player) const
  662. {
  663. ERROR_RET_VAL_IF(!hasAccess(Player), "Access forbidden!", -1);
  664. return gs->players[Player].towns.size();
  665. }
  666. bool CGameInfoCallback::getTownInfo( const CGObjectInstance *town, InfoAboutTown &dest ) const
  667. {
  668. ERROR_RET_VAL_IF(!isVisible(town, player), "Town is not visible!", false); //it's not a town or it's not visible for layer
  669. bool detailed = hasAccess(town->tempOwner);
  670. //TODO vision support
  671. if(town->ID == GameConstants::TOWNI_TYPE)
  672. dest.initFromTown(static_cast<const CGTownInstance *>(town), detailed);
  673. else if(town->ID == 33 || town->ID == 219)
  674. dest.initFromGarrison(static_cast<const CGGarrison *>(town), detailed);
  675. else
  676. return false;
  677. return true;
  678. }
  679. int3 CGameInfoCallback::guardingCreaturePosition (int3 pos) const
  680. {
  681. ERROR_RET_VAL_IF(!isVisible(pos), "Tile is not visible!", int3(-1,-1,-1));
  682. return gs->guardingCreaturePosition(pos);
  683. }
  684. bool CGameInfoCallback::getHeroInfo( const CGObjectInstance *hero, InfoAboutHero &dest ) const
  685. {
  686. const CGHeroInstance *h = dynamic_cast<const CGHeroInstance *>(hero);
  687. ERROR_RET_VAL_IF(!h, "That's not a hero!", false);
  688. ERROR_RET_VAL_IF(!isVisible(h->getPosition(false)), "That hero is not visible!", false);
  689. //TODO vision support
  690. dest.initFromHero(h, hasAccess(h->tempOwner));
  691. return true;
  692. }
  693. int CGameInfoCallback::getDate(int mode) const
  694. {
  695. //boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  696. return gs->getDate(mode);
  697. }
  698. std::vector < std::string > CGameInfoCallback::getObjDescriptions(int3 pos) const
  699. {
  700. //boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  701. std::vector<std::string> ret;
  702. const TerrainTile *t = getTile(pos);
  703. ERROR_RET_VAL_IF(!t, "Not a valid tile given!", ret);
  704. BOOST_FOREACH(const CGObjectInstance * obj, t->blockingObjects)
  705. ret.push_back(obj->getHoverText());
  706. return ret;
  707. }
  708. bool CGameInfoCallback::verifyPath(CPath * path, bool blockSea) const
  709. {
  710. for (size_t i=0; i < path->nodes.size(); ++i)
  711. {
  712. const TerrainTile *t = getTile(path->nodes[i].coord); //current tile
  713. ERROR_RET_VAL_IF(!t, "Path contains not visible tile: " << path->nodes[i].coord << "!", false);
  714. if (t->blocked && !t->visitable)
  715. return false; //path is wrong - one of the tiles is blocked
  716. if (blockSea)
  717. {
  718. if (i==0)
  719. continue;
  720. const TerrainTile *prev = getTile(path->nodes[i-1].coord); //tile of previous node on the path
  721. if (( t->tertype == TerrainTile::water && prev->tertype != TerrainTile::water)
  722. || (t->tertype != TerrainTile::water && prev->tertype == TerrainTile::water)
  723. || prev->tertype == TerrainTile::rock
  724. )
  725. return false;
  726. }
  727. }
  728. return true;
  729. }
  730. bool CGameInfoCallback::isVisible(int3 pos, int Player) const
  731. {
  732. //boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  733. return gs->map->isInTheMap(pos) && (Player == -1 || gs->isVisible(pos, Player));
  734. }
  735. bool CGameInfoCallback::isVisible(int3 pos) const
  736. {
  737. return isVisible(pos,player);
  738. }
  739. bool CGameInfoCallback::isVisible( const CGObjectInstance *obj, int Player ) const
  740. {
  741. return gs->isVisible(obj, Player);
  742. }
  743. bool CGameInfoCallback::isVisible(const CGObjectInstance *obj) const
  744. {
  745. return isVisible(obj, player);
  746. }
  747. // const CCreatureSet* CInfoCallback::getGarrison(const CGObjectInstance *obj) const
  748. // {
  749. // //boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  750. // if()
  751. // const CArmedInstance *armi = dynamic_cast<const CArmedInstance*>(obj);
  752. // if(!armi)
  753. // return NULL;
  754. // else
  755. // return armi;
  756. // }
  757. std::vector < const CGObjectInstance * > CGameInfoCallback::getBlockingObjs( int3 pos ) const
  758. {
  759. std::vector<const CGObjectInstance *> ret;
  760. const TerrainTile *t = getTile(pos);
  761. ERROR_RET_VAL_IF(!t, "Not a valid tile requested!", ret);
  762. BOOST_FOREACH(const CGObjectInstance * obj, t->blockingObjects)
  763. ret.push_back(obj);
  764. return ret;
  765. }
  766. std::vector <const CGObjectInstance * > CGameInfoCallback::getVisitableObjs(int3 pos, bool verbose /*= true*/) const
  767. {
  768. std::vector<const CGObjectInstance *> ret;
  769. const TerrainTile *t = getTile(pos, verbose);
  770. ERROR_VERBOSE_OR_NOT_RET_VAL_IF(!t, verbose, pos << " is not visible!", ret);
  771. BOOST_FOREACH(const CGObjectInstance * obj, t->visitableObjects)
  772. {
  773. if(player < 0 || obj->ID != GameConstants::EVENTI_TYPE) //hide events from players
  774. ret.push_back(obj);
  775. }
  776. return ret;
  777. }
  778. std::vector < const CGObjectInstance * > CGameInfoCallback::getFlaggableObjects(int3 pos) const
  779. {
  780. std::vector<const CGObjectInstance *> ret;
  781. const TerrainTile *t = getTile(pos);
  782. ERROR_RET_VAL_IF(!t, "Not a valid tile requested!", ret);
  783. BOOST_FOREACH(const CGObjectInstance *obj, t->blockingObjects)
  784. if(obj->tempOwner != 254)
  785. ret.push_back(obj);
  786. // const std::vector < std::pair<const CGObjectInstance*,SDL_Rect> > & objs = CGI->mh->ttiles[pos.x][pos.y][pos.z].objects;
  787. // for(size_t b=0; b<objs.size(); ++b)
  788. // {
  789. // 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))
  790. // ret.push_back(CGI->mh->ttiles[pos.x][pos.y][pos.z].objects[b].first);
  791. // }
  792. return ret;
  793. }
  794. int3 CGameInfoCallback::getMapSize() const
  795. {
  796. return int3(gs->map->width, gs->map->height, gs->map->twoLevel+1);
  797. }
  798. std::vector<const CGHeroInstance *> CGameInfoCallback::getAvailableHeroes(const CGObjectInstance * townOrTavern) const
  799. {
  800. std::vector<const CGHeroInstance *> ret;
  801. //ERROR_RET_VAL_IF(!isOwnedOrVisited(townOrTavern), "Town or tavern must be owned or visited!", ret);
  802. //TODO: town needs to be owned, advmap tavern needs to be visited; to be reimplemented when visit tracking is done
  803. ret.resize(gs->players[player].availableHeroes.size());
  804. std::copy(gs->players[player].availableHeroes.begin(),gs->players[player].availableHeroes.end(),ret.begin());
  805. return ret;
  806. }
  807. const TerrainTile * CGameInfoCallback::getTile( int3 tile, bool verbose) const
  808. {
  809. ERROR_VERBOSE_OR_NOT_RET_VAL_IF(!isVisible(tile), verbose, tile << " is not visible!", NULL);
  810. //boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  811. return &gs->map->getTile(tile);
  812. }
  813. int CGameInfoCallback::canBuildStructure( const CGTownInstance *t, int ID )
  814. {
  815. ERROR_RET_VAL_IF(!canGetFullInfo(t), "Town is not owned!", -1);
  816. int ret = EBuildingState::ALLOWED;
  817. if(t->builded >= GameConstants::MAX_BUILDING_PER_TURN)
  818. ret = EBuildingState::CANT_BUILD_TODAY; //building limit
  819. CBuilding * pom = VLC->buildh->buildings[t->subID][ID];
  820. if(!pom)
  821. return EBuildingState::BUILDING_ERROR;
  822. //checking resources
  823. if(!pom->resources.canBeAfforded(getPlayer(t->tempOwner)->resources))
  824. ret = EBuildingState::NO_RESOURCES; //lack of res
  825. //checking for requirements
  826. std::set<int> reqs = getBuildingRequiments(t, ID);//getting all requirements
  827. for( std::set<int>::iterator ri = reqs.begin(); ri != reqs.end(); ri++ )
  828. {
  829. if(t->builtBuildings.find(*ri)==t->builtBuildings.end())
  830. ret = EBuildingState::PREREQUIRES; //lack of requirements - cannot build
  831. }
  832. //can we build it?
  833. if(t->forbiddenBuildings.find(ID)!=t->forbiddenBuildings.end())
  834. ret = EBuildingState::FORBIDDEN; //forbidden
  835. if(ID == 13) //capitol
  836. {
  837. const PlayerState *ps = getPlayer(t->tempOwner);
  838. if(ps)
  839. {
  840. BOOST_FOREACH(const CGTownInstance *t, ps->towns)
  841. {
  842. if(vstd::contains(t->builtBuildings, 13))
  843. {
  844. ret = EBuildingState::HAVE_CAPITAL; //no more than one capitol
  845. break;
  846. }
  847. }
  848. }
  849. }
  850. else if(ID == 6) //shipyard
  851. {
  852. const TerrainTile *tile = getTile(t->bestLocation());
  853. if(!tile || tile->tertype != TerrainTile::water )
  854. ret = EBuildingState::NO_WATER; //lack of water
  855. }
  856. if(t->builtBuildings.find(ID)!=t->builtBuildings.end()) //already built
  857. ret = EBuildingState::ALREADY_PRESENT;
  858. return ret;
  859. }
  860. std::set<int> CGameInfoCallback::getBuildingRequiments( const CGTownInstance *t, int ID )
  861. {
  862. ERROR_RET_VAL_IF(!canGetFullInfo(t), "Town is not owned!", std::set<int>());
  863. std::set<int> used;
  864. used.insert(ID);
  865. std::set<int> reqs = VLC->townh->requirements[t->subID][ID];
  866. while(true)
  867. {
  868. size_t noloop=0;
  869. for(std::set<int>::iterator i=reqs.begin();i!=reqs.end();i++)
  870. {
  871. if(used.find(*i)==used.end()) //we haven't added requirements for this building
  872. {
  873. used.insert(*i);
  874. for(
  875. std::set<int>::iterator j=VLC->townh->requirements[t->subID][*i].begin();
  876. j!=VLC->townh->requirements[t->subID][*i].end();
  877. j++)
  878. {
  879. reqs.insert(*j);//creating full list of requirements
  880. }
  881. }
  882. else
  883. {
  884. noloop++;
  885. }
  886. }
  887. if(noloop==reqs.size())
  888. break;
  889. }
  890. return reqs;
  891. }
  892. const CMapHeader * CGameInfoCallback::getMapHeader() const
  893. {
  894. return gs->map;
  895. }
  896. bool CGameInfoCallback::hasAccess(int playerId) const
  897. {
  898. return player < 0 || gs->getPlayerRelations( playerId, player );
  899. }
  900. int CGameInfoCallback::getPlayerStatus(int player) const
  901. {
  902. const PlayerState *ps = gs->getPlayer(player, false);
  903. if(!ps)
  904. return -1;
  905. return ps->status;
  906. }
  907. std::string CGameInfoCallback::getTavernGossip(const CGObjectInstance * townOrTavern) const
  908. {
  909. return "GOSSIP TEST";
  910. }
  911. int CGameInfoCallback::getPlayerRelations( ui8 color1, ui8 color2 ) const
  912. {
  913. return gs->getPlayerRelations(color1, color2);
  914. }
  915. bool CGameInfoCallback::canGetFullInfo(const CGObjectInstance *obj) const
  916. {
  917. return !obj || hasAccess(obj->tempOwner);
  918. }
  919. int CGameInfoCallback::getHeroCount( int player, bool includeGarrisoned ) const
  920. {
  921. int ret = 0;
  922. const PlayerState *p = gs->getPlayer(player);
  923. ERROR_RET_VAL_IF(!p, "No such player!", -1);
  924. if(includeGarrisoned)
  925. return p->heroes.size();
  926. else
  927. for(ui32 i = 0; i < p->heroes.size(); i++)
  928. if(!p->heroes[i]->inTownGarrison)
  929. ret++;
  930. return ret;
  931. }
  932. bool CGameInfoCallback::isOwnedOrVisited(const CGObjectInstance *obj) const
  933. {
  934. if(canGetFullInfo(obj))
  935. return true;
  936. const TerrainTile *t = getTile(obj->visitablePos()); //get entrance tile
  937. const CGObjectInstance *visitor = t->visitableObjects.back(); //visitong hero if present or the obejct itself at last
  938. return visitor->ID == GameConstants::HEROI_TYPE && canGetFullInfo(visitor); //owned or allied hero is a visitor
  939. }
  940. int CGameInfoCallback::getCurrentPlayer() const
  941. {
  942. return gs->currentPlayer;
  943. }
  944. CGameInfoCallback::CGameInfoCallback()
  945. {
  946. }
  947. CGameInfoCallback::CGameInfoCallback(CGameState *GS, int Player)
  948. {
  949. gs = GS;
  950. player = Player;
  951. }
  952. const std::vector< std::vector< std::vector<ui8> > > & CPlayerSpecificInfoCallback::getVisibilityMap() const
  953. {
  954. //boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  955. return gs->getPlayerTeam(player)->fogOfWarMap;
  956. }
  957. int CPlayerSpecificInfoCallback::howManyTowns() const
  958. {
  959. //boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  960. ERROR_RET_VAL_IF(player == -1, "Applicable only for player callbacks", -1);
  961. return CGameInfoCallback::howManyTowns(player);
  962. }
  963. std::vector < const CGTownInstance *> CPlayerSpecificInfoCallback::getTownsInfo(bool onlyOur) const
  964. {
  965. //boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  966. std::vector < const CGTownInstance *> ret = std::vector < const CGTownInstance *>();
  967. for ( std::map<ui8, PlayerState>::iterator i=gs->players.begin() ; i!=gs->players.end();i++)
  968. {
  969. for (size_t j = 0; j < (*i).second.towns.size(); ++j)
  970. {
  971. if ((*i).first==player
  972. || (isVisible((*i).second.towns[j],player) && !onlyOur))
  973. {
  974. ret.push_back((*i).second.towns[j]);
  975. }
  976. }
  977. } // for ( std::map<int, PlayerState>::iterator i=gs->players.begin() ; i!=gs->players.end();i++)
  978. return ret;
  979. }
  980. std::vector < const CGHeroInstance *> CPlayerSpecificInfoCallback::getHeroesInfo(bool onlyOur) const
  981. {
  982. //boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  983. std::vector < const CGHeroInstance *> ret;
  984. for(size_t i=0;i<gs->map->heroes.size();i++)
  985. {
  986. if( (gs->map->heroes[i]->tempOwner==player) ||
  987. (isVisible(gs->map->heroes[i]->getPosition(false),player) && !onlyOur) )
  988. {
  989. ret.push_back(gs->map->heroes[i]);
  990. }
  991. }
  992. return ret;
  993. }
  994. int CPlayerSpecificInfoCallback::getMyColor() const
  995. {
  996. return player;
  997. }
  998. int CPlayerSpecificInfoCallback::getHeroSerial(const CGHeroInstance * hero, bool includeGarrisoned) const
  999. {
  1000. if (hero->inTownGarrison && !includeGarrisoned)
  1001. return -1;
  1002. size_t index = 0;
  1003. auto & heroes = gs->players[player].heroes;
  1004. for (auto curHero= heroes.begin(); curHero!=heroes.end(); curHero++)
  1005. {
  1006. if (includeGarrisoned || !(*curHero)->inTownGarrison)
  1007. index++;
  1008. if (*curHero == hero)
  1009. return index;
  1010. }
  1011. return -1;
  1012. }
  1013. int3 CPlayerSpecificInfoCallback::getGrailPos( double &outKnownRatio )
  1014. {
  1015. if (CGObelisk::obeliskCount == 0)
  1016. {
  1017. outKnownRatio = 0.0;
  1018. }
  1019. else
  1020. {
  1021. outKnownRatio = static_cast<double>(CGObelisk::visited[gs->getPlayerTeam(player)->id]) / CGObelisk::obeliskCount;
  1022. }
  1023. return gs->map->grailPos;
  1024. }
  1025. std::vector < const CGObjectInstance * > CPlayerSpecificInfoCallback::getMyObjects() const
  1026. {
  1027. std::vector < const CGObjectInstance * > ret;
  1028. BOOST_FOREACH(const CGObjectInstance * obj, gs->map->objects)
  1029. {
  1030. if(obj && obj->tempOwner == player)
  1031. ret.push_back(obj);
  1032. }
  1033. return ret;
  1034. }
  1035. std::vector < const CGDwelling * > CPlayerSpecificInfoCallback::getMyDwellings() const
  1036. {
  1037. std::vector < const CGDwelling * > ret;
  1038. BOOST_FOREACH(CGDwelling * dw, gs->getPlayer(player)->dwellings)
  1039. {
  1040. ret.push_back(dw);
  1041. }
  1042. return ret;
  1043. }
  1044. int CPlayerSpecificInfoCallback::howManyHeroes(bool includeGarrisoned) const
  1045. {
  1046. //boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  1047. ERROR_RET_VAL_IF(player == -1, "Applicable only for player callbacks", -1);
  1048. return getHeroCount(player,includeGarrisoned);
  1049. }
  1050. const CGHeroInstance* CPlayerSpecificInfoCallback::getHeroBySerial(int serialId, bool includeGarrisoned) const
  1051. {
  1052. const PlayerState *p = getPlayer(player);
  1053. ERROR_RET_VAL_IF(!p, "No player info", NULL);
  1054. if (!includeGarrisoned)
  1055. {
  1056. for(ui32 i = 0; i < p->heroes.size() && i<=serialId; i++)
  1057. if(p->heroes[i]->inTownGarrison)
  1058. serialId++;
  1059. }
  1060. ERROR_RET_VAL_IF(serialId < 0 || serialId >= p->heroes.size(), "No player info", NULL);
  1061. return p->heroes[serialId];
  1062. }
  1063. const CGTownInstance* CPlayerSpecificInfoCallback::getTownBySerial(int serialId) const
  1064. {
  1065. const PlayerState *p = getPlayer(player);
  1066. ERROR_RET_VAL_IF(!p, "No player info", NULL);
  1067. ERROR_RET_VAL_IF(serialId < 0 || serialId >= p->towns.size(), "No player info", NULL);
  1068. return p->towns[serialId];
  1069. }
  1070. int CPlayerSpecificInfoCallback::getResourceAmount(int type) const
  1071. {
  1072. //boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  1073. ERROR_RET_VAL_IF(player == -1, "Applicable only for player callbacks", -1);
  1074. return getResource(player, type);
  1075. }
  1076. TResources CPlayerSpecificInfoCallback::getResourceAmount() const
  1077. {
  1078. //boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  1079. ERROR_RET_VAL_IF(player == -1, "Applicable only for player callbacks", TResources());
  1080. return gs->players[player].resources;
  1081. }
  1082. CGHeroInstance *CNonConstInfoCallback::getHero(int objid)
  1083. {
  1084. return const_cast<CGHeroInstance*>(CGameInfoCallback::getHero(objid));
  1085. }
  1086. CGTownInstance *CNonConstInfoCallback::getTown(int objid)
  1087. {
  1088. return const_cast<CGTownInstance*>(CGameInfoCallback::getTown(objid));
  1089. }
  1090. TeamState *CNonConstInfoCallback::getTeam(ui8 teamID)
  1091. {
  1092. return const_cast<TeamState*>(CGameInfoCallback::getTeam(teamID));
  1093. }
  1094. TeamState *CNonConstInfoCallback::getPlayerTeam(ui8 color)
  1095. {
  1096. return const_cast<TeamState*>(CGameInfoCallback::getPlayerTeam(color));
  1097. }
  1098. PlayerState * CNonConstInfoCallback::getPlayer( ui8 color, bool verbose )
  1099. {
  1100. return const_cast<PlayerState*>(CGameInfoCallback::getPlayer(color, verbose));
  1101. }
  1102. const TeamState * CGameInfoCallback::getTeam( ui8 teamID ) const
  1103. {
  1104. ERROR_RET_VAL_IF(!vstd::contains(gs->teams, teamID), "Cannot find info for team " << int(teamID), NULL);
  1105. const TeamState *ret = &gs->teams[teamID];
  1106. ERROR_RET_VAL_IF(player != -1 && !vstd::contains(ret->players, player), "Illegal attempt to access team data!", NULL);
  1107. return ret;
  1108. }
  1109. const TeamState * CGameInfoCallback::getPlayerTeam( ui8 teamID ) const
  1110. {
  1111. const PlayerState * ps = getPlayer(teamID);
  1112. if (ps)
  1113. return getTeam(ps->team);
  1114. return NULL;
  1115. }
  1116. const CGHeroInstance* CGameInfoCallback::getHeroWithSubid( int subid ) const
  1117. {
  1118. BOOST_FOREACH(const CGHeroInstance *h, gs->map->heroes)
  1119. if(h->subID == subid)
  1120. return h;
  1121. return NULL;
  1122. }
  1123. int CGameInfoCallback::getLocalPlayer() const
  1124. {
  1125. return getCurrentPlayer();
  1126. }
  1127. bool CGameInfoCallback::isInTheMap(const int3 &pos) const
  1128. {
  1129. return gs->map->isInTheMap(pos);
  1130. }
  1131. void IGameEventRealizer::showInfoDialog( InfoWindow *iw )
  1132. {
  1133. commitPackage(iw);
  1134. }
  1135. void IGameEventRealizer::showInfoDialog(const std::string &msg, int player)
  1136. {
  1137. InfoWindow iw;
  1138. iw.player = player;
  1139. iw.text << msg;
  1140. showInfoDialog(&iw);
  1141. }
  1142. void IGameEventRealizer::setObjProperty(int objid, int prop, si64 val)
  1143. {
  1144. SetObjectProperty sob;
  1145. sob.id = objid;
  1146. sob.what = prop;
  1147. sob.val = static_cast<ui32>(val);
  1148. commitPackage(&sob);
  1149. }
  1150. const CGObjectInstance * IGameCallback::putNewObject(int ID, int subID, int3 pos)
  1151. {
  1152. NewObject no;
  1153. no.ID = ID; //creature
  1154. no.subID= subID;
  1155. no.pos = pos;
  1156. commitPackage(&no);
  1157. return getObj(no.id); //id field will be filled during applying on gs
  1158. }
  1159. const CGCreature * IGameCallback::putNewMonster(int creID, int count, int3 pos)
  1160. {
  1161. const CGObjectInstance *m = putNewObject(54, creID, pos);
  1162. setObjProperty(m->id, ObjProperty::MONSTER_COUNT, count);
  1163. setObjProperty(m->id, ObjProperty::MONSTER_POWER, (si64)1000*count);
  1164. return dynamic_cast<const CGCreature*>(m);
  1165. }