CGameInfoCallback.cpp 27 KB

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