CGameInfoCallback.cpp 22 KB

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