CGameInfoCallback.cpp 26 KB

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