IGameCallback2.cpp 22 KB

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