CGameInfoCallback.cpp 22 KB

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