CGameInfoCallback.cpp 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933
  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. #include "mapping/CMap.h"
  20. //TODO make clean
  21. #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)
  22. #define ERROR_RET_IF(cond, txt) do {if(cond){logGlobal->errorStream() << BOOST_CURRENT_FUNCTION << ": " << txt; return;}} while(0)
  23. #define ERROR_RET_VAL_IF(cond, txt, retVal) do {if(cond){logGlobal->errorStream() << BOOST_CURRENT_FUNCTION << ": " << txt; return retVal;}} while(0)
  24. PlayerColor CGameInfoCallback::getOwner(ObjectInstanceID heroID) const
  25. {
  26. const CGObjectInstance *obj = getObj(heroID);
  27. ERROR_RET_VAL_IF(!obj, "No such object!", PlayerColor::CANNOT_DETERMINE);
  28. return obj->tempOwner;
  29. }
  30. int CGameInfoCallback::getResource(PlayerColor Player, Res::ERes which) const
  31. {
  32. const PlayerState *p = getPlayer(Player);
  33. ERROR_RET_VAL_IF(!p, "No player info!", -1);
  34. ERROR_RET_VAL_IF(p->resources.size() <= which || which < 0, "No such resource!", -1);
  35. return p->resources[which];
  36. }
  37. const PlayerSettings * CGameInfoCallback::getPlayerSettings(PlayerColor color) const
  38. {
  39. return &gs->scenarioOps->getIthPlayersSettings(color);
  40. }
  41. bool CGameInfoCallback::isAllowed( int type, int id )
  42. {
  43. switch(type)
  44. {
  45. case 0:
  46. return gs->map->allowedSpell[id];
  47. case 1:
  48. return gs->map->allowedArtifact[id];
  49. case 2:
  50. return gs->map->allowedAbilities[id];
  51. default:
  52. ERROR_RET_VAL_IF(1, "Wrong type!", false);
  53. }
  54. }
  55. const PlayerState * CGameInfoCallback::getPlayer(PlayerColor color, bool verbose) const
  56. {
  57. //funtion written from scratch since it's accessed A LOT by AI
  58. auto player = gs->players.find(color);
  59. if (player != gs->players.end())
  60. {
  61. if (hasAccess(color))
  62. return &player->second;
  63. else
  64. {
  65. if (verbose)
  66. logGlobal->errorStream() << boost::format("Cannot access player %d info!") % color;
  67. return nullptr;
  68. }
  69. }
  70. else
  71. {
  72. if (verbose)
  73. logGlobal->errorStream() << boost::format("Cannot find player %d info!") % color;
  74. return nullptr;
  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) && ret->tempOwner != 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 (hero) //we see hero's spellbook
  162. return sp->calculateDamage(hero, nullptr, hero->getEffectLevel(sp), hero->getEffectPower(sp));
  163. else
  164. return 0; //mage guild
  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 CGObjectInstance * selectedObject/* = nullptr*/) 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. if(town->ID == Obj::TOWN)
  191. {
  192. if(!detailed && nullptr != selectedObject)
  193. {
  194. const CGHeroInstance * selectedHero = dynamic_cast<const CGHeroInstance *>(selectedObject);
  195. if(nullptr != selectedHero)
  196. detailed = selectedHero->hasVisions(town, 1);
  197. }
  198. dest.initFromTown(static_cast<const CGTownInstance *>(town), detailed);
  199. }
  200. else if(town->ID == Obj::GARRISON || town->ID == Obj::GARRISON2)
  201. dest.initFromArmy(static_cast<const CArmedInstance *>(town), detailed);
  202. else
  203. return false;
  204. return true;
  205. }
  206. int3 CGameInfoCallback::guardingCreaturePosition (int3 pos) const //FIXME: redundant?
  207. {
  208. ERROR_RET_VAL_IF(!isVisible(pos), "Tile is not visible!", int3(-1,-1,-1));
  209. return gs->guardingCreaturePosition(pos);
  210. }
  211. std::vector<const CGObjectInstance*> CGameInfoCallback::getGuardingCreatures (int3 pos) const
  212. {
  213. ERROR_RET_VAL_IF(!isVisible(pos), "Tile is not visible!", std::vector<const CGObjectInstance*>());
  214. std::vector<const CGObjectInstance*> ret;
  215. for(auto cr : gs->guardingCreatures(pos))
  216. {
  217. ret.push_back(cr);
  218. }
  219. return ret;
  220. }
  221. bool CGameInfoCallback::getHeroInfo(const CGObjectInstance * hero, InfoAboutHero & dest, const CGObjectInstance * selectedObject/* = nullptr*/) const
  222. {
  223. const CGHeroInstance *h = dynamic_cast<const CGHeroInstance *>(hero);
  224. ERROR_RET_VAL_IF(!h, "That's not a hero!", false);
  225. ERROR_RET_VAL_IF(!isVisible(h->getPosition(false)), "That hero is not visible!", false);
  226. bool accessFlag = hasAccess(h->tempOwner);
  227. if(!accessFlag && nullptr != selectedObject)
  228. {
  229. const CGHeroInstance * selectedHero = dynamic_cast<const CGHeroInstance *>(selectedObject);
  230. if(nullptr != selectedHero)
  231. accessFlag = selectedHero->hasVisions(hero, 1);
  232. }
  233. dest.initFromHero(h, accessFlag);
  234. //DISGUISED bonus implementation
  235. if(getPlayerRelations(getLocalPlayer(), hero->tempOwner) == PlayerRelations::ENEMIES)
  236. {
  237. //todo: bonus cashing
  238. int disguiseLevel = h->valOfBonuses(Selector::typeSubtype(Bonus::DISGUISED, 0));
  239. auto doBasicDisguise = [disguiseLevel](InfoAboutHero & info)
  240. {
  241. int maxAIValue = 0;
  242. const CCreature * mostStrong = nullptr;
  243. for(auto & elem : info.army)
  244. {
  245. if(elem.second.type->AIValue > maxAIValue)
  246. {
  247. maxAIValue = elem.second.type->AIValue;
  248. mostStrong = elem.second.type;
  249. }
  250. }
  251. if(nullptr == mostStrong)//just in case
  252. logGlobal->errorStream() << "CGameInfoCallback::getHeroInfo: Unable to select most strong stack" << disguiseLevel;
  253. else
  254. for(auto & elem : info.army)
  255. {
  256. elem.second.type = mostStrong;
  257. }
  258. };
  259. auto doAdvancedDisguise = [accessFlag, &doBasicDisguise](InfoAboutHero & info)
  260. {
  261. doBasicDisguise(info);
  262. for(auto & elem : info.army)
  263. elem.second.count = 0;
  264. };
  265. auto doExpertDisguise = [this,h](InfoAboutHero & info)
  266. {
  267. for(auto & elem : info.army)
  268. elem.second.count = 0;
  269. const auto factionIndex = getStartInfo(false)->playerInfos.at(h->tempOwner).castle;
  270. int maxAIValue = 0;
  271. const CCreature * mostStrong = nullptr;
  272. for(auto creature : VLC->creh->creatures)
  273. {
  274. if(creature->faction == factionIndex && creature->AIValue > maxAIValue)
  275. {
  276. maxAIValue = creature->AIValue;
  277. mostStrong = creature;
  278. }
  279. }
  280. if(nullptr != mostStrong) //possible, faction may have no creatures at all
  281. for(auto & elem : info.army)
  282. elem.second.type = mostStrong;
  283. };
  284. switch (disguiseLevel)
  285. {
  286. case 0:
  287. //no bonus at all - do nothing
  288. break;
  289. case 1:
  290. doBasicDisguise(dest);
  291. break;
  292. case 2:
  293. doAdvancedDisguise(dest);
  294. break;
  295. case 3:
  296. doExpertDisguise(dest);
  297. break;
  298. default:
  299. //invalid value
  300. logGlobal->errorStream() << "CGameInfoCallback::getHeroInfo: Invalid DISGUISED bonus value " << disguiseLevel;
  301. break;
  302. }
  303. }
  304. return true;
  305. }
  306. int CGameInfoCallback::getDate(Date::EDateType mode) const
  307. {
  308. //boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  309. return gs->getDate(mode);
  310. }
  311. bool CGameInfoCallback::isVisible(int3 pos, boost::optional<PlayerColor> Player) const
  312. {
  313. //boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  314. return gs->map->isInTheMap(pos) && (!Player || gs->isVisible(pos, *Player));
  315. }
  316. bool CGameInfoCallback::isVisible(int3 pos) const
  317. {
  318. return isVisible(pos, player);
  319. }
  320. bool CGameInfoCallback::isVisible( const CGObjectInstance *obj, boost::optional<PlayerColor> Player ) const
  321. {
  322. return gs->isVisible(obj, Player);
  323. }
  324. bool CGameInfoCallback::isVisible(const CGObjectInstance *obj) const
  325. {
  326. return isVisible(obj, player);
  327. }
  328. // const CCreatureSet* CInfoCallback::getGarrison(const CGObjectInstance *obj) const
  329. // {
  330. // //boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  331. // if()
  332. // const CArmedInstance *armi = dynamic_cast<const CArmedInstance*>(obj);
  333. // if(!armi)
  334. // return nullptr;
  335. // else
  336. // return armi;
  337. // }
  338. std::vector < const CGObjectInstance * > CGameInfoCallback::getBlockingObjs( int3 pos ) const
  339. {
  340. std::vector<const CGObjectInstance *> ret;
  341. const TerrainTile *t = getTile(pos);
  342. ERROR_RET_VAL_IF(!t, "Not a valid tile requested!", ret);
  343. for(const CGObjectInstance * obj : t->blockingObjects)
  344. ret.push_back(obj);
  345. return ret;
  346. }
  347. std::vector <const CGObjectInstance * > CGameInfoCallback::getVisitableObjs(int3 pos, bool verbose /*= true*/) const
  348. {
  349. std::vector<const CGObjectInstance *> ret;
  350. const TerrainTile *t = getTile(pos, verbose);
  351. ERROR_VERBOSE_OR_NOT_RET_VAL_IF(!t, verbose, pos << " is not visible!", ret);
  352. for(const CGObjectInstance * obj : t->visitableObjects)
  353. {
  354. if(player || obj->ID != Obj::EVENT) //hide events from players
  355. ret.push_back(obj);
  356. }
  357. return ret;
  358. }
  359. const CGObjectInstance * CGameInfoCallback::getTopObj (int3 pos) const
  360. {
  361. return vstd::backOrNull(getVisitableObjs(pos));
  362. }
  363. std::vector < const CGObjectInstance * > CGameInfoCallback::getFlaggableObjects(int3 pos) const
  364. {
  365. std::vector<const CGObjectInstance *> ret;
  366. const TerrainTile *t = getTile(pos);
  367. ERROR_RET_VAL_IF(!t, "Not a valid tile requested!", ret);
  368. for(const CGObjectInstance *obj : t->blockingObjects)
  369. if(obj->tempOwner != PlayerColor::UNFLAGGABLE)
  370. ret.push_back(obj);
  371. // const std::vector < std::pair<const CGObjectInstance*,SDL_Rect> > & objs = CGI->mh->ttiles[pos.x][pos.y][pos.z].objects;
  372. // for(size_t b=0; b<objs.size(); ++b)
  373. // {
  374. // 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))
  375. // ret.push_back(CGI->mh->ttiles[pos.x][pos.y][pos.z].objects[b].first);
  376. // }
  377. return ret;
  378. }
  379. int3 CGameInfoCallback::getMapSize() const
  380. {
  381. return int3(gs->map->width, gs->map->height, gs->map->twoLevel ? 2 : 1);
  382. }
  383. std::vector<const CGHeroInstance *> CGameInfoCallback::getAvailableHeroes(const CGObjectInstance * townOrTavern) const
  384. {
  385. ASSERT_IF_CALLED_WITH_PLAYER
  386. std::vector<const CGHeroInstance *> ret;
  387. //ERROR_RET_VAL_IF(!isOwnedOrVisited(townOrTavern), "Town or tavern must be owned or visited!", ret);
  388. //TODO: town needs to be owned, advmap tavern needs to be visited; to be reimplemented when visit tracking is done
  389. range::copy(gs->players[*player].availableHeroes, std::back_inserter(ret));
  390. vstd::erase_if(ret, [](const CGHeroInstance *h) { return h == nullptr; });
  391. return ret;
  392. }
  393. const TerrainTile * CGameInfoCallback::getTile( int3 tile, bool verbose) const
  394. {
  395. ERROR_VERBOSE_OR_NOT_RET_VAL_IF(!isVisible(tile), verbose, tile << " is not visible!", nullptr);
  396. //boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  397. return &gs->map->getTile(tile);
  398. }
  399. //TODO: typedef?
  400. shared_ptr<boost::multi_array<TerrainTile*, 3>> CGameInfoCallback::getAllVisibleTiles() const
  401. {
  402. assert(player.is_initialized());
  403. auto team = getPlayerTeam(player.get());
  404. size_t width = gs->map->width;
  405. size_t height = gs->map->height;
  406. size_t levels = (gs->map->twoLevel ? 2 : 1);
  407. boost::multi_array<TerrainTile*, 3> tileArray(boost::extents[width][height][levels]);
  408. for (size_t x = 0; x < width; x++)
  409. for (size_t y = 0; y < height; y++)
  410. for (size_t z = 0; z < levels; z++)
  411. {
  412. if (team->fogOfWarMap[x][y][z])
  413. tileArray[x][y][z] = &gs->map->getTile(int3(x, y, z));
  414. else
  415. tileArray[x][y][z] = nullptr;
  416. }
  417. return make_shared<boost::multi_array<TerrainTile*, 3>>(tileArray);
  418. }
  419. EBuildingState::EBuildingState CGameInfoCallback::canBuildStructure( const CGTownInstance *t, BuildingID ID )
  420. {
  421. ERROR_RET_VAL_IF(!canGetFullInfo(t), "Town is not owned!", EBuildingState::TOWN_NOT_OWNED);
  422. if(!t->town->buildings.count(ID))
  423. return EBuildingState::BUILDING_ERROR;
  424. const CBuilding * building = t->town->buildings.at(ID);
  425. if(t->hasBuilt(ID)) //already built
  426. return EBuildingState::ALREADY_PRESENT;
  427. //can we build it?
  428. if(vstd::contains(t->forbiddenBuildings, ID))
  429. return EBuildingState::FORBIDDEN; //forbidden
  430. if(ID == BuildingID::CAPITOL)
  431. {
  432. const PlayerState *ps = getPlayer(t->tempOwner, false);
  433. if(ps)
  434. {
  435. for(const CGTownInstance *t : ps->towns)
  436. {
  437. if(t->hasBuilt(BuildingID::CAPITOL))
  438. {
  439. return EBuildingState::HAVE_CAPITAL; //no more than one capitol
  440. }
  441. }
  442. }
  443. }
  444. else if(ID == BuildingID::SHIPYARD)
  445. {
  446. const TerrainTile *tile = getTile(t->bestLocation(), false);
  447. if(!tile || tile->terType != ETerrainType::WATER)
  448. return EBuildingState::NO_WATER; //lack of water
  449. }
  450. auto buildTest = [&](BuildingID id) -> bool
  451. {
  452. return t->hasBuilt(id);
  453. };
  454. if (!t->genBuildingRequirements(ID).test(buildTest))
  455. return EBuildingState::PREREQUIRES;
  456. if(t->builded >= VLC->modh->settings.MAX_BUILDING_PER_TURN)
  457. return EBuildingState::CANT_BUILD_TODAY; //building limit
  458. //checking resources
  459. if(!building->resources.canBeAfforded(getPlayer(t->tempOwner)->resources))
  460. return EBuildingState::NO_RESOURCES; //lack of res
  461. return EBuildingState::ALLOWED;
  462. }
  463. const CMapHeader * CGameInfoCallback::getMapHeader() const
  464. {
  465. return gs->map;
  466. }
  467. bool CGameInfoCallback::hasAccess(boost::optional<PlayerColor> playerId) const
  468. {
  469. return !player || gs->getPlayerRelations( *playerId, *player ) != PlayerRelations::ENEMIES;
  470. }
  471. EPlayerStatus::EStatus CGameInfoCallback::getPlayerStatus(PlayerColor player, bool verbose) const
  472. {
  473. const PlayerState *ps = gs->getPlayer(player, verbose);
  474. ERROR_VERBOSE_OR_NOT_RET_VAL_IF(!ps, verbose, "No such player!", EPlayerStatus::WRONG);
  475. return ps->status;
  476. }
  477. std::string CGameInfoCallback::getTavernGossip(const CGObjectInstance * townOrTavern) const
  478. {
  479. return "GOSSIP TEST";
  480. }
  481. PlayerRelations::PlayerRelations CGameInfoCallback::getPlayerRelations( PlayerColor color1, PlayerColor color2 ) const
  482. {
  483. return gs->getPlayerRelations(color1, color2);
  484. }
  485. bool CGameInfoCallback::canGetFullInfo(const CGObjectInstance *obj) const
  486. {
  487. return !obj || hasAccess(obj->tempOwner);
  488. }
  489. int CGameInfoCallback::getHeroCount( PlayerColor player, bool includeGarrisoned ) const
  490. {
  491. int ret = 0;
  492. const PlayerState *p = gs->getPlayer(player);
  493. ERROR_RET_VAL_IF(!p, "No such player!", -1);
  494. if(includeGarrisoned)
  495. return p->heroes.size();
  496. else
  497. for(auto & elem : p->heroes)
  498. if(!elem->inTownGarrison)
  499. ret++;
  500. return ret;
  501. }
  502. bool CGameInfoCallback::isOwnedOrVisited(const CGObjectInstance *obj) const
  503. {
  504. if(canGetFullInfo(obj))
  505. return true;
  506. const TerrainTile *t = getTile(obj->visitablePos()); //get entrance tile
  507. const CGObjectInstance *visitor = t->visitableObjects.back(); //visitong hero if present or the obejct itself at last
  508. return visitor->ID == Obj::HERO && canGetFullInfo(visitor); //owned or allied hero is a visitor
  509. }
  510. PlayerColor CGameInfoCallback::getCurrentPlayer() const
  511. {
  512. return gs->currentPlayer;
  513. }
  514. CGameInfoCallback::CGameInfoCallback()
  515. {
  516. }
  517. CGameInfoCallback::CGameInfoCallback(CGameState *GS, boost::optional<PlayerColor> Player)
  518. {
  519. gs = GS;
  520. player = Player;
  521. }
  522. const std::vector< std::vector< std::vector<ui8> > > & CPlayerSpecificInfoCallback::getVisibilityMap() const
  523. {
  524. //boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  525. return gs->getPlayerTeam(*player)->fogOfWarMap;
  526. }
  527. int CPlayerSpecificInfoCallback::howManyTowns() const
  528. {
  529. //boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  530. ERROR_RET_VAL_IF(!player, "Applicable only for player callbacks", -1);
  531. return CGameInfoCallback::howManyTowns(*player);
  532. }
  533. std::vector < const CGTownInstance *> CPlayerSpecificInfoCallback::getTownsInfo(bool onlyOur) const
  534. {
  535. //boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  536. std::vector < const CGTownInstance *> ret = std::vector < const CGTownInstance *>();
  537. for(const auto & i : gs->players)
  538. {
  539. for(const auto & town : i.second.towns)
  540. {
  541. if (i.first==player || (isVisible(town, player) && !onlyOur))
  542. {
  543. ret.push_back(town);
  544. }
  545. }
  546. } // for ( std::map<int, PlayerState>::iterator i=gs->players.begin() ; i!=gs->players.end();i++)
  547. return ret;
  548. }
  549. std::vector < const CGHeroInstance *> CPlayerSpecificInfoCallback::getHeroesInfo(bool onlyOur) const
  550. {
  551. //boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  552. std::vector < const CGHeroInstance *> ret;
  553. for(auto hero : gs->map->heroesOnMap)
  554. {
  555. // !player || // - why would we even get access to hero not owned by any player?
  556. if((hero->tempOwner == *player) ||
  557. (isVisible(hero->getPosition(false), player) && !onlyOur) )
  558. {
  559. ret.push_back(hero);
  560. }
  561. }
  562. return ret;
  563. }
  564. boost::optional<PlayerColor> CPlayerSpecificInfoCallback::getMyColor() const
  565. {
  566. return player;
  567. }
  568. int CPlayerSpecificInfoCallback::getHeroSerial(const CGHeroInstance * hero, bool includeGarrisoned) const
  569. {
  570. if (hero->inTownGarrison && !includeGarrisoned)
  571. return -1;
  572. size_t index = 0;
  573. auto & heroes = gs->players[*player].heroes;
  574. for (auto & heroe : heroes)
  575. {
  576. if (includeGarrisoned || !(heroe)->inTownGarrison)
  577. index++;
  578. if (heroe == hero)
  579. return index;
  580. }
  581. return -1;
  582. }
  583. int3 CPlayerSpecificInfoCallback::getGrailPos( double &outKnownRatio )
  584. {
  585. if (!player || CGObelisk::obeliskCount == 0)
  586. {
  587. outKnownRatio = 0.0;
  588. }
  589. else
  590. {
  591. outKnownRatio = static_cast<double>(CGObelisk::visited[gs->getPlayerTeam(*player)->id]) / CGObelisk::obeliskCount;
  592. }
  593. return gs->map->grailPos;
  594. }
  595. std::vector < const CGObjectInstance * > CPlayerSpecificInfoCallback::getMyObjects() const
  596. {
  597. std::vector < const CGObjectInstance * > ret;
  598. for(const CGObjectInstance * obj : gs->map->objects)
  599. {
  600. if(obj && obj->tempOwner == player)
  601. ret.push_back(obj);
  602. }
  603. return ret;
  604. }
  605. std::vector < const CGDwelling * > CPlayerSpecificInfoCallback::getMyDwellings() const
  606. {
  607. ASSERT_IF_CALLED_WITH_PLAYER
  608. std::vector < const CGDwelling * > ret;
  609. for(CGDwelling * dw : gs->getPlayer(*player)->dwellings)
  610. {
  611. ret.push_back(dw);
  612. }
  613. return ret;
  614. }
  615. std::vector <QuestInfo> CPlayerSpecificInfoCallback::getMyQuests() const
  616. {
  617. std::vector <QuestInfo> ret;
  618. for (auto quest : gs->getPlayer(*player)->quests)
  619. {
  620. ret.push_back (quest);
  621. }
  622. return ret;
  623. }
  624. int CPlayerSpecificInfoCallback::howManyHeroes(bool includeGarrisoned) const
  625. {
  626. //boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  627. ERROR_RET_VAL_IF(!player, "Applicable only for player callbacks", -1);
  628. return getHeroCount(*player,includeGarrisoned);
  629. }
  630. const CGHeroInstance* CPlayerSpecificInfoCallback::getHeroBySerial(int serialId, bool includeGarrisoned) const
  631. {
  632. ASSERT_IF_CALLED_WITH_PLAYER
  633. const PlayerState *p = getPlayer(*player);
  634. ERROR_RET_VAL_IF(!p, "No player info", nullptr);
  635. if (!includeGarrisoned)
  636. {
  637. for(ui32 i = 0; i < p->heroes.size() && i<=serialId; i++)
  638. if(p->heroes[i]->inTownGarrison)
  639. serialId++;
  640. }
  641. ERROR_RET_VAL_IF(serialId < 0 || serialId >= p->heroes.size(), "No player info", nullptr);
  642. return p->heroes[serialId];
  643. }
  644. const CGTownInstance* CPlayerSpecificInfoCallback::getTownBySerial(int serialId) const
  645. {
  646. ASSERT_IF_CALLED_WITH_PLAYER
  647. const PlayerState *p = getPlayer(*player);
  648. ERROR_RET_VAL_IF(!p, "No player info", nullptr);
  649. ERROR_RET_VAL_IF(serialId < 0 || serialId >= p->towns.size(), "No player info", nullptr);
  650. return p->towns[serialId];
  651. }
  652. int CPlayerSpecificInfoCallback::getResourceAmount(Res::ERes type) const
  653. {
  654. //boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  655. ERROR_RET_VAL_IF(!player, "Applicable only for player callbacks", -1);
  656. return getResource(*player, type);
  657. }
  658. TResources CPlayerSpecificInfoCallback::getResourceAmount() const
  659. {
  660. //boost::shared_lock<boost::shared_mutex> lock(*gs->mx);
  661. ERROR_RET_VAL_IF(!player, "Applicable only for player callbacks", TResources());
  662. return gs->players[*player].resources;
  663. }
  664. const TeamState * CGameInfoCallback::getTeam( TeamID teamID ) const
  665. {
  666. //rewritten by hand, AI calls this function a lot
  667. auto team = gs->teams.find(teamID);
  668. if (team != gs->teams.end())
  669. {
  670. const TeamState *ret = &team->second;
  671. if (!player.is_initialized()) //neutral (or invalid) player
  672. return ret;
  673. else
  674. {
  675. if (vstd::contains(ret->players, *player)) //specific player
  676. return ret;
  677. else
  678. {
  679. logGlobal->errorStream() << boost::format("Illegal attempt to access team data!");
  680. return nullptr;
  681. }
  682. }
  683. }
  684. else
  685. {
  686. logGlobal->errorStream() << boost::format("Cannot find info for team %d") % teamID;
  687. return nullptr;
  688. }
  689. }
  690. const TeamState * CGameInfoCallback::getPlayerTeam( PlayerColor color ) const
  691. {
  692. auto player = gs->players.find(color);
  693. if (player != gs->players.end())
  694. {
  695. return getTeam (player->second.team);
  696. }
  697. else
  698. {
  699. return nullptr;
  700. }
  701. }
  702. const CGHeroInstance* CGameInfoCallback::getHeroWithSubid( int subid ) const
  703. {
  704. for(const CGHeroInstance *h : gs->map->heroesOnMap)
  705. if(h->subID == subid)
  706. return h;
  707. return nullptr;
  708. }
  709. PlayerColor CGameInfoCallback::getLocalPlayer() const
  710. {
  711. return getCurrentPlayer();
  712. }
  713. bool CGameInfoCallback::isInTheMap(const int3 &pos) const
  714. {
  715. return gs->map->isInTheMap(pos);
  716. }
  717. const CArtifactInstance * CGameInfoCallback::getArtInstance( ArtifactInstanceID aid ) const
  718. {
  719. return gs->map->artInstances[aid.num];
  720. }
  721. const CGObjectInstance * CGameInfoCallback::getObjInstance( ObjectInstanceID oid ) const
  722. {
  723. return gs->map->objects[oid.num];
  724. }
  725. std::vector<ObjectInstanceID> CGameInfoCallback::getVisibleTeleportObjects(std::vector<ObjectInstanceID> ids, PlayerColor player) const
  726. {
  727. vstd::erase_if(ids, [&](ObjectInstanceID id) -> bool
  728. {
  729. auto obj = getObj(id);
  730. return player != PlayerColor::UNFLAGGABLE && (!obj || !isVisible(obj->pos, player));
  731. });
  732. return ids;
  733. }
  734. std::vector<ObjectInstanceID> CGameInfoCallback::getTeleportChannelEntraces(TeleportChannelID id, PlayerColor player) const
  735. {
  736. return getVisibleTeleportObjects(gs->map->teleportChannels[id]->entrances, player);
  737. }
  738. std::vector<ObjectInstanceID> CGameInfoCallback::getTeleportChannelExits(TeleportChannelID id, PlayerColor player) const
  739. {
  740. return getVisibleTeleportObjects(gs->map->teleportChannels[id]->exits, player);
  741. }
  742. ETeleportChannelType CGameInfoCallback::getTeleportChannelType(TeleportChannelID id, PlayerColor player) const
  743. {
  744. std::vector<ObjectInstanceID> entrances = getTeleportChannelEntraces(id, player);
  745. std::vector<ObjectInstanceID> exits = getTeleportChannelExits(id, player);
  746. if((!entrances.size() || !exits.size()) // impassable if exits or entrances list are empty
  747. || (entrances.size() == 1 && entrances == exits)) // impassable if only entrance and only exit is same object. e.g bidirectional monolith
  748. {
  749. return ETeleportChannelType::IMPASSABLE;
  750. }
  751. auto intersection = vstd::intersection(entrances, exits);
  752. if(intersection.size() == entrances.size() && intersection.size() == exits.size())
  753. return ETeleportChannelType::BIDIRECTIONAL;
  754. else if(!intersection.size())
  755. return ETeleportChannelType::UNIDIRECTIONAL;
  756. else
  757. return ETeleportChannelType::MIXED;
  758. }
  759. bool CGameInfoCallback::isTeleportChannelImpassable(TeleportChannelID id, PlayerColor player) const
  760. {
  761. return ETeleportChannelType::IMPASSABLE == getTeleportChannelType(id, player);
  762. }
  763. bool CGameInfoCallback::isTeleportChannelBidirectional(TeleportChannelID id, PlayerColor player) const
  764. {
  765. return ETeleportChannelType::BIDIRECTIONAL == getTeleportChannelType(id, player);
  766. }
  767. bool CGameInfoCallback::isTeleportChannelUnidirectional(TeleportChannelID id, PlayerColor player) const
  768. {
  769. return ETeleportChannelType::UNIDIRECTIONAL == getTeleportChannelType(id, player);
  770. }
  771. bool CGameInfoCallback::isTeleportEntrancePassable(const CGTeleport * obj, PlayerColor player) const
  772. {
  773. return obj && obj->isEntrance() && !isTeleportChannelImpassable(obj->channel, player);
  774. }
  775. void IGameEventRealizer::showInfoDialog( InfoWindow *iw )
  776. {
  777. commitPackage(iw);
  778. }
  779. void IGameEventRealizer::showInfoDialog(const std::string &msg, PlayerColor player)
  780. {
  781. InfoWindow iw;
  782. iw.player = player;
  783. iw.text << msg;
  784. showInfoDialog(&iw);
  785. }
  786. void IGameEventRealizer::setObjProperty(ObjectInstanceID objid, int prop, si64 val)
  787. {
  788. SetObjectProperty sob;
  789. sob.id = objid;
  790. sob.what = prop;
  791. sob.val = static_cast<ui32>(val);
  792. commitPackage(&sob);
  793. }