CGameInfoCallback.cpp 27 KB

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