CGameInfoCallback.cpp 22 KB

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