CGameInfoCallback.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725
  1. /*
  2. * CGameInfoCallback.cpp, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #include "StdInc.h"
  11. #include "CGameInfoCallback.h"
  12. #include "CGameState.h" // PlayerState
  13. #include "mapObjects/CObjectHandler.h" // for CGObjectInstance
  14. #include "StartInfo.h" // for StartInfo
  15. #include "BattleState.h" // for BattleInfo
  16. #include "NetPacks.h" // for InfoWindow
  17. #include "CModHandler.h"
  18. //TODO make clean
  19. #define ERROR_VERBOSE_OR_NOT_RET_VAL_IF(cond, verbose, txt, retVal) do {if(cond){if(verbose)logGlobal->errorStream() << BOOST_CURRENT_FUNCTION << ": " << txt; return retVal;}} while(0)
  20. #define ERROR_RET_IF(cond, txt) do {if(cond){logGlobal->errorStream() << BOOST_CURRENT_FUNCTION << ": " << txt; return;}} while(0)
  21. #define ERROR_RET_VAL_IF(cond, txt, retVal) do {if(cond){logGlobal->errorStream() << BOOST_CURRENT_FUNCTION << ": " << txt; return retVal;}} while(0)
  22. PlayerColor CGameInfoCallback::getOwner(ObjectInstanceID heroID) const
  23. {
  24. const CGObjectInstance *obj = getObj(heroID);
  25. ERROR_RET_VAL_IF(!obj, "No such object!", PlayerColor::CANNOT_DETERMINE);
  26. return obj->tempOwner;
  27. }
  28. int CGameInfoCallback::getResource(PlayerColor Player, Res::ERes which) const
  29. {
  30. const PlayerState *p = getPlayer(Player);
  31. ERROR_RET_VAL_IF(!p, "No player info!", -1);
  32. ERROR_RET_VAL_IF(p->resources.size() <= which || which < 0, "No such resource!", -1);
  33. return p->resources[which];
  34. }
  35. const CGHeroInstance* CGameInfoCallback::getSelectedHero( PlayerColor Player ) const
  36. {
  37. const PlayerState *p = getPlayer(Player);
  38. ERROR_RET_VAL_IF(!p, "No player info!", nullptr);
  39. return getHero(p->currentSelection);
  40. }
  41. const CGHeroInstance* CGameInfoCallback::getSelectedHero() const
  42. {
  43. return getSelectedHero(gs->currentPlayer);
  44. }
  45. const PlayerSettings * CGameInfoCallback::getPlayerSettings(PlayerColor color) const
  46. {
  47. return &gs->scenarioOps->getIthPlayersSettings(color);
  48. }
  49. bool CGameInfoCallback::isAllowed( int type, int id )
  50. {
  51. switch(type)
  52. {
  53. case 0:
  54. return gs->map->allowedSpell[id];
  55. case 1:
  56. return gs->map->allowedArtifact[id];
  57. case 2:
  58. return gs->map->allowedAbilities[id];
  59. default:
  60. ERROR_RET_VAL_IF(1, "Wrong type!", false);
  61. }
  62. }
  63. const PlayerState * CGameInfoCallback::getPlayer(PlayerColor color, bool verbose) const
  64. {
  65. ERROR_VERBOSE_OR_NOT_RET_VAL_IF(!hasAccess(color), verbose, "Cannot access player " << color << "info!", nullptr);
  66. ERROR_VERBOSE_OR_NOT_RET_VAL_IF(!vstd::contains(gs->players,color), verbose, "Cannot find player " << color << "info!", nullptr);
  67. return &gs->players[color];
  68. }
  69. const CTown * CGameInfoCallback::getNativeTown(PlayerColor color) const
  70. {
  71. const PlayerSettings *ps = getPlayerSettings(color);
  72. ERROR_RET_VAL_IF(!ps, "There is no such player!", nullptr);
  73. return VLC->townh->factions[ps->castle]->town;
  74. }
  75. const CGObjectInstance * CGameInfoCallback::getObjByQuestIdentifier(int identifier) const
  76. {
  77. ERROR_RET_VAL_IF(!vstd::contains(gs->map->questIdentifierToId, identifier), "There is no object with such quest identifier!", nullptr);
  78. return getObj(gs->map->questIdentifierToId[identifier]);
  79. }
  80. /************************************************************************/
  81. /* */
  82. /************************************************************************/
  83. const CGObjectInstance* CGameInfoCallback::getObj(ObjectInstanceID objid, bool verbose) const
  84. {
  85. si32 oid = objid.num;
  86. if(oid < 0 || oid >= gs->map->objects.size())
  87. {
  88. if(verbose)
  89. logGlobal->errorStream() << "Cannot get object with id " << oid;
  90. return nullptr;
  91. }
  92. const CGObjectInstance *ret = gs->map->objects[oid];
  93. if(!ret)
  94. {
  95. if(verbose)
  96. logGlobal->errorStream() << "Cannot get object with id " << oid << ". Object was removed.";
  97. return nullptr;
  98. }
  99. if(!isVisible(ret, player))
  100. {
  101. if(verbose)
  102. logGlobal->errorStream() << "Cannot get object with id " << oid << ". Object is not visible.";
  103. return nullptr;
  104. }
  105. return ret;
  106. }
  107. const CGHeroInstance* CGameInfoCallback::getHero(ObjectInstanceID objid) const
  108. {
  109. const CGObjectInstance *obj = getObj(objid, false);
  110. if(obj)
  111. return dynamic_cast<const CGHeroInstance*>(obj);
  112. else
  113. return nullptr;
  114. }
  115. const CGTownInstance* CGameInfoCallback::getTown(ObjectInstanceID objid) const
  116. {
  117. const CGObjectInstance *obj = getObj(objid, false);
  118. if(obj)
  119. return dynamic_cast<const CGTownInstance*>(obj);
  120. else
  121. return nullptr;
  122. }
  123. void CGameInfoCallback::getUpgradeInfo(const CArmedInstance *obj, SlotID stackPos, UpgradeInfo &out) const
  124. {
  125. //boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  126. ERROR_RET_IF(!canGetFullInfo(obj), "Cannot get info about not owned object!");
  127. ERROR_RET_IF(!obj->hasStackAtSlot(stackPos), "There is no such stack!");
  128. out = gs->getUpgradeInfo(obj->getStack(stackPos));
  129. //return gs->getUpgradeInfo(obj->getStack(stackPos));
  130. }
  131. const StartInfo * CGameInfoCallback::getStartInfo(bool beforeRandomization /*= false*/) const
  132. {
  133. //boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  134. if(beforeRandomization)
  135. return gs->initialOpts;
  136. else
  137. return gs->scenarioOps;
  138. }
  139. int CGameInfoCallback::getSpellCost(const CSpell * sp, const CGHeroInstance * caster) const
  140. {
  141. //boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  142. ERROR_RET_VAL_IF(!canGetFullInfo(caster), "Cannot get info about caster!", -1);
  143. //if there is a battle
  144. if(gs->curB)
  145. return gs->curB->battleGetSpellCost(sp, caster);
  146. //if there is no battle
  147. return caster->getSpellCost(sp);
  148. }
  149. int CGameInfoCallback::estimateSpellDamage(const CSpell * sp, const CGHeroInstance * hero) const
  150. {
  151. //boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  152. ERROR_RET_VAL_IF(hero && !canGetFullInfo(hero), "Cannot get info about caster!", -1);
  153. if(!gs->curB) //no battle
  154. {
  155. if (hero) //but we see hero's spellbook
  156. return gs->curB->calculateSpellDmg(
  157. sp, hero, nullptr, hero->getSpellSchoolLevel(sp), hero->getPrimSkillLevel(PrimarySkill::SPELL_POWER));
  158. else
  159. return 0; //mage guild
  160. }
  161. //gs->getHero(gs->currentPlayer)
  162. //const CGHeroInstance * ourHero = gs->curB->heroes[0]->tempOwner == player ? gs->curB->heroes[0] : gs->curB->heroes[1];
  163. const CGHeroInstance * ourHero = hero;
  164. return gs->curB->calculateSpellDmg(
  165. sp, ourHero, nullptr, ourHero->getSpellSchoolLevel(sp), ourHero->getPrimSkillLevel(PrimarySkill::SPELL_POWER));
  166. }
  167. void CGameInfoCallback::getThievesGuildInfo(SThievesGuildInfo & thi, const CGObjectInstance * obj)
  168. {
  169. //boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  170. ERROR_RET_IF(!obj, "No guild object!");
  171. ERROR_RET_IF(obj->ID == Obj::TOWN && !canGetFullInfo(obj), "Cannot get info about town guild object!");
  172. //TODO: advmap object -> check if they're visited by our hero
  173. if(obj->ID == Obj::TOWN || obj->ID == Obj::TAVERN)
  174. {
  175. gs->obtainPlayersStats(thi, gs->players[obj->tempOwner].towns.size());
  176. }
  177. else if(obj->ID == Obj::DEN_OF_THIEVES)
  178. {
  179. gs->obtainPlayersStats(thi, 20);
  180. }
  181. }
  182. int CGameInfoCallback::howManyTowns(PlayerColor Player) const
  183. {
  184. ERROR_RET_VAL_IF(!hasAccess(Player), "Access forbidden!", -1);
  185. return gs->players[Player].towns.size();
  186. }
  187. bool CGameInfoCallback::getTownInfo( const CGObjectInstance *town, InfoAboutTown &dest ) const
  188. {
  189. ERROR_RET_VAL_IF(!isVisible(town, player), "Town is not visible!", false); //it's not a town or it's not visible for layer
  190. bool detailed = hasAccess(town->tempOwner);
  191. //TODO vision support
  192. if(town->ID == Obj::TOWN)
  193. dest.initFromTown(static_cast<const CGTownInstance *>(town), detailed);
  194. else if(town->ID == Obj::GARRISON || town->ID == Obj::GARRISON2)
  195. dest.initFromArmy(static_cast<const CArmedInstance *>(town), detailed);
  196. else
  197. return false;
  198. return true;
  199. }
  200. int3 CGameInfoCallback::guardingCreaturePosition (int3 pos) const //FIXME: redundant?
  201. {
  202. ERROR_RET_VAL_IF(!isVisible(pos), "Tile is not visible!", int3(-1,-1,-1));
  203. return gs->guardingCreaturePosition(pos);
  204. }
  205. std::vector<const CGObjectInstance*> CGameInfoCallback::getGuardingCreatures (int3 pos) const
  206. {
  207. ERROR_RET_VAL_IF(!isVisible(pos), "Tile is not visible!", std::vector<const CGObjectInstance*>());
  208. std::vector<const CGObjectInstance*> ret;
  209. for(auto cr : gs->guardingCreatures(pos))
  210. {
  211. ret.push_back(cr);
  212. }
  213. return ret;
  214. }
  215. bool CGameInfoCallback::getHeroInfo( const CGObjectInstance *hero, InfoAboutHero &dest ) const
  216. {
  217. const CGHeroInstance *h = dynamic_cast<const CGHeroInstance *>(hero);
  218. ERROR_RET_VAL_IF(!h, "That's not a hero!", false);
  219. ERROR_RET_VAL_IF(!isVisible(h->getPosition(false)), "That hero is not visible!", false);
  220. //TODO vision support
  221. dest.initFromHero(h, hasAccess(h->tempOwner));
  222. return true;
  223. }
  224. int CGameInfoCallback::getDate(Date::EDateType mode) const
  225. {
  226. //boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  227. return gs->getDate(mode);
  228. }
  229. bool CGameInfoCallback::isVisible(int3 pos, boost::optional<PlayerColor> Player) const
  230. {
  231. //boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  232. return gs->map->isInTheMap(pos) && (!Player || gs->isVisible(pos, *Player));
  233. }
  234. bool CGameInfoCallback::isVisible(int3 pos) const
  235. {
  236. return isVisible(pos, player);
  237. }
  238. bool CGameInfoCallback::isVisible( const CGObjectInstance *obj, boost::optional<PlayerColor> Player ) const
  239. {
  240. return gs->isVisible(obj, Player);
  241. }
  242. bool CGameInfoCallback::isVisible(const CGObjectInstance *obj) const
  243. {
  244. return isVisible(obj, player);
  245. }
  246. // const CCreatureSet* CInfoCallback::getGarrison(const CGObjectInstance *obj) const
  247. // {
  248. // //boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  249. // if()
  250. // const CArmedInstance *armi = dynamic_cast<const CArmedInstance*>(obj);
  251. // if(!armi)
  252. // return nullptr;
  253. // else
  254. // return armi;
  255. // }
  256. std::vector < const CGObjectInstance * > CGameInfoCallback::getBlockingObjs( int3 pos ) const
  257. {
  258. std::vector<const CGObjectInstance *> ret;
  259. const TerrainTile *t = getTile(pos);
  260. ERROR_RET_VAL_IF(!t, "Not a valid tile requested!", ret);
  261. for(const CGObjectInstance * obj : t->blockingObjects)
  262. ret.push_back(obj);
  263. return ret;
  264. }
  265. std::vector <const CGObjectInstance * > CGameInfoCallback::getVisitableObjs(int3 pos, bool verbose /*= true*/) const
  266. {
  267. std::vector<const CGObjectInstance *> ret;
  268. const TerrainTile *t = getTile(pos, verbose);
  269. ERROR_VERBOSE_OR_NOT_RET_VAL_IF(!t, verbose, pos << " is not visible!", ret);
  270. for(const CGObjectInstance * obj : t->visitableObjects)
  271. {
  272. if(player < nullptr || obj->ID != Obj::EVENT) //hide events from players
  273. ret.push_back(obj);
  274. }
  275. return ret;
  276. }
  277. const CGObjectInstance * CGameInfoCallback::getTopObj (int3 pos) const
  278. {
  279. return vstd::backOrNull(getVisitableObjs(pos));
  280. }
  281. std::vector < const CGObjectInstance * > CGameInfoCallback::getFlaggableObjects(int3 pos) const
  282. {
  283. std::vector<const CGObjectInstance *> ret;
  284. const TerrainTile *t = getTile(pos);
  285. ERROR_RET_VAL_IF(!t, "Not a valid tile requested!", ret);
  286. for(const CGObjectInstance *obj : t->blockingObjects)
  287. if(obj->tempOwner != PlayerColor::UNFLAGGABLE)
  288. ret.push_back(obj);
  289. // const std::vector < std::pair<const CGObjectInstance*,SDL_Rect> > & objs = CGI->mh->ttiles[pos.x][pos.y][pos.z].objects;
  290. // for(size_t b=0; b<objs.size(); ++b)
  291. // {
  292. // 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))
  293. // ret.push_back(CGI->mh->ttiles[pos.x][pos.y][pos.z].objects[b].first);
  294. // }
  295. return ret;
  296. }
  297. int3 CGameInfoCallback::getMapSize() const
  298. {
  299. return int3(gs->map->width, gs->map->height, gs->map->twoLevel ? 2 : 1);
  300. }
  301. std::vector<const CGHeroInstance *> CGameInfoCallback::getAvailableHeroes(const CGObjectInstance * townOrTavern) const
  302. {
  303. ASSERT_IF_CALLED_WITH_PLAYER
  304. std::vector<const CGHeroInstance *> ret;
  305. //ERROR_RET_VAL_IF(!isOwnedOrVisited(townOrTavern), "Town or tavern must be owned or visited!", ret);
  306. //TODO: town needs to be owned, advmap tavern needs to be visited; to be reimplemented when visit tracking is done
  307. range::copy(gs->players[*player].availableHeroes, std::back_inserter(ret));
  308. vstd::erase_if(ret, [](const CGHeroInstance *h) { return h == nullptr; });
  309. return ret;
  310. }
  311. const TerrainTile * CGameInfoCallback::getTile( int3 tile, bool verbose) const
  312. {
  313. ERROR_VERBOSE_OR_NOT_RET_VAL_IF(!isVisible(tile), verbose, tile << " is not visible!", nullptr);
  314. //boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  315. return &gs->map->getTile(tile);
  316. }
  317. EBuildingState::EBuildingState CGameInfoCallback::canBuildStructure( const CGTownInstance *t, BuildingID ID )
  318. {
  319. ERROR_RET_VAL_IF(!canGetFullInfo(t), "Town is not owned!", EBuildingState::TOWN_NOT_OWNED);
  320. if(!t->town->buildings.count(ID))
  321. return EBuildingState::BUILDING_ERROR;
  322. const CBuilding * building = t->town->buildings.at(ID);
  323. if(t->hasBuilt(ID)) //already built
  324. return EBuildingState::ALREADY_PRESENT;
  325. //can we build it?
  326. if(vstd::contains(t->forbiddenBuildings, ID))
  327. return EBuildingState::FORBIDDEN; //forbidden
  328. if(ID == BuildingID::CAPITOL)
  329. {
  330. const PlayerState *ps = getPlayer(t->tempOwner);
  331. if(ps)
  332. {
  333. for(const CGTownInstance *t : ps->towns)
  334. {
  335. if(t->hasBuilt(BuildingID::CAPITOL))
  336. {
  337. return EBuildingState::HAVE_CAPITAL; //no more than one capitol
  338. }
  339. }
  340. }
  341. }
  342. else if(ID == BuildingID::SHIPYARD)
  343. {
  344. const TerrainTile *tile = getTile(t->bestLocation(), false);
  345. if(!tile || tile->terType != ETerrainType::WATER)
  346. return EBuildingState::NO_WATER; //lack of water
  347. }
  348. auto buildTest = [&](const BuildingID & id)
  349. {
  350. return t->hasBuilt(id);
  351. };
  352. if(t->builded >= VLC->modh->settings.MAX_BUILDING_PER_TURN)
  353. return EBuildingState::CANT_BUILD_TODAY; //building limit
  354. if (!building->requirements.test(buildTest))
  355. return EBuildingState::PREREQUIRES;
  356. if (building->upgrade != BuildingID::NONE && !t->hasBuilt(building->upgrade))
  357. return EBuildingState::MISSING_BASE;
  358. //checking resources
  359. if(!building->resources.canBeAfforded(getPlayer(t->tempOwner)->resources))
  360. return EBuildingState::NO_RESOURCES; //lack of res
  361. return EBuildingState::ALLOWED;
  362. }
  363. const CMapHeader * CGameInfoCallback::getMapHeader() const
  364. {
  365. return gs->map;
  366. }
  367. bool CGameInfoCallback::hasAccess(boost::optional<PlayerColor> playerId) const
  368. {
  369. return !player || gs->getPlayerRelations( *playerId, *player ) != PlayerRelations::ENEMIES;
  370. }
  371. EPlayerStatus::EStatus CGameInfoCallback::getPlayerStatus(PlayerColor player, bool verbose) const
  372. {
  373. const PlayerState *ps = gs->getPlayer(player, verbose);
  374. ERROR_VERBOSE_OR_NOT_RET_VAL_IF(!ps, verbose, "No such player!", EPlayerStatus::WRONG);
  375. return ps->status;
  376. }
  377. std::string CGameInfoCallback::getTavernGossip(const CGObjectInstance * townOrTavern) const
  378. {
  379. return "GOSSIP TEST";
  380. }
  381. PlayerRelations::PlayerRelations CGameInfoCallback::getPlayerRelations( PlayerColor color1, PlayerColor color2 ) const
  382. {
  383. return gs->getPlayerRelations(color1, color2);
  384. }
  385. bool CGameInfoCallback::canGetFullInfo(const CGObjectInstance *obj) const
  386. {
  387. return !obj || hasAccess(obj->tempOwner);
  388. }
  389. int CGameInfoCallback::getHeroCount( PlayerColor player, bool includeGarrisoned ) const
  390. {
  391. int ret = 0;
  392. const PlayerState *p = gs->getPlayer(player);
  393. ERROR_RET_VAL_IF(!p, "No such player!", -1);
  394. if(includeGarrisoned)
  395. return p->heroes.size();
  396. else
  397. for(auto & elem : p->heroes)
  398. if(!elem->inTownGarrison)
  399. ret++;
  400. return ret;
  401. }
  402. bool CGameInfoCallback::isOwnedOrVisited(const CGObjectInstance *obj) const
  403. {
  404. if(canGetFullInfo(obj))
  405. return true;
  406. const TerrainTile *t = getTile(obj->visitablePos()); //get entrance tile
  407. const CGObjectInstance *visitor = t->visitableObjects.back(); //visitong hero if present or the obejct itself at last
  408. return visitor->ID == Obj::HERO && canGetFullInfo(visitor); //owned or allied hero is a visitor
  409. }
  410. PlayerColor CGameInfoCallback::getCurrentPlayer() const
  411. {
  412. return gs->currentPlayer;
  413. }
  414. CGameInfoCallback::CGameInfoCallback()
  415. {
  416. }
  417. CGameInfoCallback::CGameInfoCallback(CGameState *GS, boost::optional<PlayerColor> Player)
  418. {
  419. gs = GS;
  420. player = Player;
  421. }
  422. const std::vector< std::vector< std::vector<ui8> > > & CPlayerSpecificInfoCallback::getVisibilityMap() const
  423. {
  424. //boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  425. return gs->getPlayerTeam(*player)->fogOfWarMap;
  426. }
  427. int CPlayerSpecificInfoCallback::howManyTowns() const
  428. {
  429. //boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  430. ERROR_RET_VAL_IF(!player, "Applicable only for player callbacks", -1);
  431. return CGameInfoCallback::howManyTowns(*player);
  432. }
  433. std::vector < const CGTownInstance *> CPlayerSpecificInfoCallback::getTownsInfo(bool onlyOur) const
  434. {
  435. //boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  436. std::vector < const CGTownInstance *> ret = std::vector < const CGTownInstance *>();
  437. for(const auto & i : gs->players)
  438. {
  439. for(const auto & town : i.second.towns)
  440. {
  441. if (i.first==player || (isVisible(town, player) && !onlyOur))
  442. {
  443. ret.push_back(town);
  444. }
  445. }
  446. } // for ( std::map<int, PlayerState>::iterator i=gs->players.begin() ; i!=gs->players.end();i++)
  447. return ret;
  448. }
  449. std::vector < const CGHeroInstance *> CPlayerSpecificInfoCallback::getHeroesInfo(bool onlyOur) const
  450. {
  451. //boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  452. std::vector < const CGHeroInstance *> ret;
  453. for(auto hero : gs->map->heroesOnMap)
  454. {
  455. if( !player || (hero->tempOwner == *player) ||
  456. (isVisible(hero->getPosition(false), player) && !onlyOur) )
  457. {
  458. ret.push_back(hero);
  459. }
  460. }
  461. return ret;
  462. }
  463. boost::optional<PlayerColor> CPlayerSpecificInfoCallback::getMyColor() const
  464. {
  465. return player;
  466. }
  467. int CPlayerSpecificInfoCallback::getHeroSerial(const CGHeroInstance * hero, bool includeGarrisoned) const
  468. {
  469. if (hero->inTownGarrison && !includeGarrisoned)
  470. return -1;
  471. size_t index = 0;
  472. auto & heroes = gs->players[*player].heroes;
  473. for (auto & heroe : heroes)
  474. {
  475. if (includeGarrisoned || !(heroe)->inTownGarrison)
  476. index++;
  477. if (heroe == hero)
  478. return index;
  479. }
  480. return -1;
  481. }
  482. int3 CPlayerSpecificInfoCallback::getGrailPos( double &outKnownRatio )
  483. {
  484. if (!player || CGObelisk::obeliskCount == 0)
  485. {
  486. outKnownRatio = 0.0;
  487. }
  488. else
  489. {
  490. outKnownRatio = static_cast<double>(CGObelisk::visited[gs->getPlayerTeam(*player)->id]) / CGObelisk::obeliskCount;
  491. }
  492. return gs->map->grailPos;
  493. }
  494. std::vector < const CGObjectInstance * > CPlayerSpecificInfoCallback::getMyObjects() const
  495. {
  496. std::vector < const CGObjectInstance * > ret;
  497. for(const CGObjectInstance * obj : gs->map->objects)
  498. {
  499. if(obj && obj->tempOwner == player)
  500. ret.push_back(obj);
  501. }
  502. return ret;
  503. }
  504. std::vector < const CGDwelling * > CPlayerSpecificInfoCallback::getMyDwellings() const
  505. {
  506. ASSERT_IF_CALLED_WITH_PLAYER
  507. std::vector < const CGDwelling * > ret;
  508. for(CGDwelling * dw : gs->getPlayer(*player)->dwellings)
  509. {
  510. ret.push_back(dw);
  511. }
  512. return ret;
  513. }
  514. std::vector <QuestInfo> CPlayerSpecificInfoCallback::getMyQuests() const
  515. {
  516. std::vector <QuestInfo> ret;
  517. for (auto quest : gs->getPlayer(*player)->quests)
  518. {
  519. ret.push_back (quest);
  520. }
  521. return ret;
  522. }
  523. int CPlayerSpecificInfoCallback::howManyHeroes(bool includeGarrisoned) const
  524. {
  525. //boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  526. ERROR_RET_VAL_IF(!player, "Applicable only for player callbacks", -1);
  527. return getHeroCount(*player,includeGarrisoned);
  528. }
  529. const CGHeroInstance* CPlayerSpecificInfoCallback::getHeroBySerial(int serialId, bool includeGarrisoned) const
  530. {
  531. ASSERT_IF_CALLED_WITH_PLAYER
  532. const PlayerState *p = getPlayer(*player);
  533. ERROR_RET_VAL_IF(!p, "No player info", nullptr);
  534. if (!includeGarrisoned)
  535. {
  536. for(ui32 i = 0; i < p->heroes.size() && i<=serialId; i++)
  537. if(p->heroes[i]->inTownGarrison)
  538. serialId++;
  539. }
  540. ERROR_RET_VAL_IF(serialId < 0 || serialId >= p->heroes.size(), "No player info", nullptr);
  541. return p->heroes[serialId];
  542. }
  543. const CGTownInstance* CPlayerSpecificInfoCallback::getTownBySerial(int serialId) const
  544. {
  545. ASSERT_IF_CALLED_WITH_PLAYER
  546. const PlayerState *p = getPlayer(*player);
  547. ERROR_RET_VAL_IF(!p, "No player info", nullptr);
  548. ERROR_RET_VAL_IF(serialId < 0 || serialId >= p->towns.size(), "No player info", nullptr);
  549. return p->towns[serialId];
  550. }
  551. int CPlayerSpecificInfoCallback::getResourceAmount(Res::ERes type) const
  552. {
  553. //boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  554. ERROR_RET_VAL_IF(!player, "Applicable only for player callbacks", -1);
  555. return getResource(*player, type);
  556. }
  557. TResources CPlayerSpecificInfoCallback::getResourceAmount() const
  558. {
  559. //boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  560. ERROR_RET_VAL_IF(!player, "Applicable only for player callbacks", TResources());
  561. return gs->players[*player].resources;
  562. }
  563. const TeamState * CGameInfoCallback::getTeam( TeamID teamID ) const
  564. {
  565. ERROR_RET_VAL_IF(!vstd::contains(gs->teams, teamID), "Cannot find info for team " << teamID, nullptr);
  566. const TeamState *ret = &gs->teams[teamID];
  567. ERROR_RET_VAL_IF(!!player && !vstd::contains(ret->players, *player), "Illegal attempt to access team data!", nullptr);
  568. return ret;
  569. }
  570. const TeamState * CGameInfoCallback::getPlayerTeam( PlayerColor color ) const
  571. {
  572. const PlayerState * ps = getPlayer(color);
  573. if (ps)
  574. return getTeam(ps->team);
  575. return nullptr;
  576. }
  577. const CGHeroInstance* CGameInfoCallback::getHeroWithSubid( int subid ) const
  578. {
  579. for(const CGHeroInstance *h : gs->map->heroesOnMap)
  580. if(h->subID == subid)
  581. return h;
  582. return nullptr;
  583. }
  584. PlayerColor CGameInfoCallback::getLocalPlayer() const
  585. {
  586. return getCurrentPlayer();
  587. }
  588. bool CGameInfoCallback::isInTheMap(const int3 &pos) const
  589. {
  590. return gs->map->isInTheMap(pos);
  591. }
  592. const CArtifactInstance * CGameInfoCallback::getArtInstance( ArtifactInstanceID aid ) const
  593. {
  594. return gs->map->artInstances[aid.num];
  595. }
  596. const CGObjectInstance * CGameInfoCallback::getObjInstance( ObjectInstanceID oid ) const
  597. {
  598. return gs->map->objects[oid.num];
  599. }
  600. void IGameEventRealizer::showInfoDialog( InfoWindow *iw )
  601. {
  602. commitPackage(iw);
  603. }
  604. void IGameEventRealizer::showInfoDialog(const std::string &msg, PlayerColor player)
  605. {
  606. InfoWindow iw;
  607. iw.player = player;
  608. iw.text << msg;
  609. showInfoDialog(&iw);
  610. }
  611. void IGameEventRealizer::setObjProperty(ObjectInstanceID objid, int prop, si64 val)
  612. {
  613. SetObjectProperty sob;
  614. sob.id = objid;
  615. sob.what = prop;
  616. sob.val = static_cast<ui32>(val);
  617. commitPackage(&sob);
  618. }