CBattleInfoEssentials.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441
  1. /*
  2. * CBattleInfoEssentials.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 "CBattleInfoEssentials.h"
  12. #include "../CStack.h"
  13. #include "BattleInfo.h"
  14. #include "CObstacleInstance.h"
  15. #include "../mapObjects/CGTownInstance.h"
  16. #include "../gameState/InfoAboutArmy.h"
  17. #include "../constants/EntityIdentifiers.h"
  18. VCMI_LIB_NAMESPACE_BEGIN
  19. bool CBattleInfoEssentials::duringBattle() const
  20. {
  21. return getBattle() != nullptr;
  22. }
  23. TerrainId CBattleInfoEssentials::battleTerrainType() const
  24. {
  25. RETURN_IF_NOT_BATTLE(TerrainId());
  26. return getBattle()->getTerrainType();
  27. }
  28. BattleField CBattleInfoEssentials::battleGetBattlefieldType() const
  29. {
  30. RETURN_IF_NOT_BATTLE(BattleField::NONE);
  31. return getBattle()->getBattlefieldType();
  32. }
  33. int32_t CBattleInfoEssentials::battleGetEnchanterCounter(BattleSide side) const
  34. {
  35. RETURN_IF_NOT_BATTLE(0);
  36. return getBattle()->getEnchanterCounter(side);
  37. }
  38. std::vector<std::shared_ptr<const CObstacleInstance>> CBattleInfoEssentials::battleGetAllObstacles(std::optional<BattleSide> perspective) const
  39. {
  40. std::vector<std::shared_ptr<const CObstacleInstance> > ret;
  41. RETURN_IF_NOT_BATTLE(ret);
  42. if(!perspective)
  43. {
  44. //if no particular perspective request, use default one
  45. perspective = std::make_optional(battleGetMySide());
  46. }
  47. else
  48. {
  49. if(!!getPlayerID() && *perspective != battleGetMySide())
  50. logGlobal->warn("Unauthorized obstacles access attempt, assuming massive spell");
  51. }
  52. for(const auto & obstacle : getBattle()->getAllObstacles())
  53. {
  54. if(battleIsObstacleVisibleForSide(*(obstacle), *perspective))
  55. ret.push_back(obstacle);
  56. }
  57. return ret;
  58. }
  59. std::shared_ptr<const CObstacleInstance> CBattleInfoEssentials::battleGetObstacleByID(uint32_t ID) const
  60. {
  61. std::shared_ptr<const CObstacleInstance> ret;
  62. RETURN_IF_NOT_BATTLE(std::shared_ptr<const CObstacleInstance>());
  63. for(auto obstacle : getBattle()->getAllObstacles())
  64. {
  65. if(obstacle->uniqueID == ID)
  66. return obstacle;
  67. }
  68. logGlobal->error("Invalid obstacle ID %d", ID);
  69. return std::shared_ptr<const CObstacleInstance>();
  70. }
  71. bool CBattleInfoEssentials::battleIsObstacleVisibleForSide(const CObstacleInstance & coi, BattleSide side) const
  72. {
  73. RETURN_IF_NOT_BATTLE(false);
  74. return side == BattleSide::ALL_KNOWING || coi.visibleForSide(side, battleHasNativeStack(side));
  75. }
  76. bool CBattleInfoEssentials::battleHasNativeStack(BattleSide side) const
  77. {
  78. RETURN_IF_NOT_BATTLE(false);
  79. for(const auto * s : battleGetAllStacks())
  80. {
  81. if(s->unitSide() == side && s->isNativeTerrain(getBattle()->getTerrainType()))
  82. return true;
  83. }
  84. return false;
  85. }
  86. TStacks CBattleInfoEssentials::battleGetAllStacks(bool includeTurrets) const
  87. {
  88. return battleGetStacksIf([=](const CStack * s)
  89. {
  90. return !s->isGhost() && (includeTurrets || !s->isTurret());
  91. });
  92. }
  93. TStacks CBattleInfoEssentials::battleGetStacksIf(const TStackFilter & predicate) const
  94. {
  95. RETURN_IF_NOT_BATTLE(TStacks());
  96. return getBattle()->getStacksIf(std::move(predicate));
  97. }
  98. battle::Units CBattleInfoEssentials::battleGetUnitsIf(const battle::UnitFilter & predicate) const
  99. {
  100. RETURN_IF_NOT_BATTLE(battle::Units());
  101. return getBattle()->getUnitsIf(predicate);
  102. }
  103. const battle::Unit * CBattleInfoEssentials::battleGetUnitByID(uint32_t ID) const
  104. {
  105. RETURN_IF_NOT_BATTLE(nullptr);
  106. //TODO: consider using map ID -> Unit
  107. auto ret = battleGetUnitsIf([=](const battle::Unit * unit)
  108. {
  109. return unit->unitId() == ID;
  110. });
  111. if(ret.empty())
  112. return nullptr;
  113. else
  114. return ret[0];
  115. }
  116. const battle::Unit * CBattleInfoEssentials::battleActiveUnit() const
  117. {
  118. RETURN_IF_NOT_BATTLE(nullptr);
  119. auto id = getBattle()->getActiveStackID();
  120. if(id >= 0)
  121. return battleGetUnitByID(static_cast<uint32_t>(id));
  122. else
  123. return nullptr;
  124. }
  125. uint32_t CBattleInfoEssentials::battleNextUnitId() const
  126. {
  127. return getBattle()->nextUnitId();
  128. }
  129. const CGTownInstance * CBattleInfoEssentials::battleGetDefendedTown() const
  130. {
  131. RETURN_IF_NOT_BATTLE(nullptr);
  132. return getBattle()->getDefendedTown();
  133. }
  134. BattleSide CBattleInfoEssentials::battleGetMySide() const
  135. {
  136. RETURN_IF_NOT_BATTLE(BattleSide::INVALID);
  137. if(!getPlayerID() || getPlayerID()->isSpectator())
  138. return BattleSide::ALL_KNOWING;
  139. if(*getPlayerID() == getBattle()->getSidePlayer(BattleSide::ATTACKER))
  140. return BattleSide::LEFT_SIDE;
  141. if(*getPlayerID() == getBattle()->getSidePlayer(BattleSide::DEFENDER))
  142. return BattleSide::RIGHT_SIDE;
  143. logGlobal->error("Cannot find player %s in battle!", getPlayerID()->toString());
  144. return BattleSide::INVALID;
  145. }
  146. const CStack* CBattleInfoEssentials::battleGetStackByID(int ID, bool onlyAlive) const
  147. {
  148. RETURN_IF_NOT_BATTLE(nullptr);
  149. auto stacks = battleGetStacksIf([=](const CStack * s)
  150. {
  151. return s->unitId() == ID && (!onlyAlive || s->alive());
  152. });
  153. if(stacks.empty())
  154. return nullptr;
  155. else
  156. return stacks[0];
  157. }
  158. bool CBattleInfoEssentials::battleDoWeKnowAbout(BattleSide side) const
  159. {
  160. RETURN_IF_NOT_BATTLE(false);
  161. auto p = battleGetMySide();
  162. return p == BattleSide::ALL_KNOWING || p == side;
  163. }
  164. si8 CBattleInfoEssentials::battleTacticDist() const
  165. {
  166. RETURN_IF_NOT_BATTLE(0);
  167. return getBattle()->getTacticDist();
  168. }
  169. BattleSide CBattleInfoEssentials::battleGetTacticsSide() const
  170. {
  171. RETURN_IF_NOT_BATTLE(BattleSide::NONE);
  172. return getBattle()->getTacticsSide();
  173. }
  174. const CGHeroInstance * CBattleInfoEssentials::battleGetFightingHero(BattleSide side) const
  175. {
  176. RETURN_IF_NOT_BATTLE(nullptr);
  177. if(side != BattleSide::DEFENDER && side != BattleSide::ATTACKER)
  178. {
  179. logGlobal->error("FIXME: %s wrong argument!", __FUNCTION__);
  180. return nullptr;
  181. }
  182. if(!battleDoWeKnowAbout(side))
  183. {
  184. logGlobal->error("FIXME: %s access check ", __FUNCTION__);
  185. return nullptr;
  186. }
  187. return getBattle()->getSideHero(side);
  188. }
  189. const CArmedInstance * CBattleInfoEssentials::battleGetArmyObject(BattleSide side) const
  190. {
  191. RETURN_IF_NOT_BATTLE(nullptr);
  192. if(side != BattleSide::DEFENDER && side != BattleSide::ATTACKER)
  193. {
  194. logGlobal->error("FIXME: %s wrong argument!", __FUNCTION__);
  195. return nullptr;
  196. }
  197. if(!battleDoWeKnowAbout(side))
  198. {
  199. logGlobal->error("FIXME: %s access check!", __FUNCTION__);
  200. return nullptr;
  201. }
  202. return getBattle()->getSideArmy(side);
  203. }
  204. InfoAboutHero CBattleInfoEssentials::battleGetHeroInfo(BattleSide side) const
  205. {
  206. const auto * hero = getBattle()->getSideHero(side);
  207. if(!hero)
  208. {
  209. return InfoAboutHero();
  210. }
  211. InfoAboutHero::EInfoLevel infoLevel = battleDoWeKnowAbout(side) ? InfoAboutHero::EInfoLevel::DETAILED : InfoAboutHero::EInfoLevel::BASIC;
  212. return InfoAboutHero(hero, infoLevel);
  213. }
  214. uint32_t CBattleInfoEssentials::battleCastSpells(BattleSide side) const
  215. {
  216. RETURN_IF_NOT_BATTLE(-1);
  217. return getBattle()->getCastSpells(side);
  218. }
  219. const IBonusBearer * CBattleInfoEssentials::getBonusBearer() const
  220. {
  221. return getBattle()->getBonusBearer();
  222. }
  223. bool CBattleInfoEssentials::battleCanFlee(const PlayerColor & player) const
  224. {
  225. RETURN_IF_NOT_BATTLE(false);
  226. const auto side = playerToSide(player);
  227. if(side == BattleSide::NONE)
  228. return false;
  229. const CGHeroInstance * myHero = battleGetFightingHero(side);
  230. //current player have no hero
  231. if(!myHero)
  232. return false;
  233. //eg. one of heroes is wearing shakles of war
  234. if(myHero->hasBonusOfType(BonusType::BATTLE_NO_FLEEING))
  235. return false;
  236. //we are besieged defender
  237. if(side == BattleSide::DEFENDER && getBattle()->getDefendedTown() != nullptr)
  238. {
  239. const auto * town = battleGetDefendedTown();
  240. if(!town->hasBuilt(BuildingSubID::ESCAPE_TUNNEL))
  241. return false;
  242. }
  243. return true;
  244. }
  245. BattleSide CBattleInfoEssentials::playerToSide(const PlayerColor & player) const
  246. {
  247. RETURN_IF_NOT_BATTLE(BattleSide::NONE);
  248. if(getBattle()->getSidePlayer(BattleSide::ATTACKER) == player)
  249. return BattleSide::ATTACKER;
  250. if(getBattle()->getSidePlayer(BattleSide::DEFENDER) == player)
  251. return BattleSide::DEFENDER;
  252. logGlobal->warn("Cannot find side for player %s", player.toString());
  253. return BattleSide::INVALID;
  254. }
  255. PlayerColor CBattleInfoEssentials::sideToPlayer(BattleSide side) const
  256. {
  257. RETURN_IF_NOT_BATTLE(PlayerColor::CANNOT_DETERMINE);
  258. return getBattle()->getSidePlayer(side);
  259. }
  260. BattleSide CBattleInfoEssentials::otherSide(BattleSide side)
  261. {
  262. if(side == BattleSide::ATTACKER)
  263. return BattleSide::DEFENDER;
  264. else
  265. return BattleSide::ATTACKER;
  266. }
  267. PlayerColor CBattleInfoEssentials::otherPlayer(const PlayerColor & player) const
  268. {
  269. RETURN_IF_NOT_BATTLE(PlayerColor::CANNOT_DETERMINE);
  270. auto side = playerToSide(player);
  271. if(side == BattleSide::NONE)
  272. return PlayerColor::CANNOT_DETERMINE;
  273. return getBattle()->getSidePlayer(otherSide(side));
  274. }
  275. bool CBattleInfoEssentials::playerHasAccessToHeroInfo(const PlayerColor & player, const CGHeroInstance * h) const
  276. {
  277. RETURN_IF_NOT_BATTLE(false);
  278. const auto side = playerToSide(player);
  279. if(side != BattleSide::NONE)
  280. {
  281. auto opponentSide = otherSide(side);
  282. if(getBattle()->getSideHero(opponentSide) == h)
  283. return true;
  284. }
  285. return false;
  286. }
  287. ui8 CBattleInfoEssentials::battleGetSiegeLevel() const
  288. {
  289. RETURN_IF_NOT_BATTLE(CGTownInstance::NONE);
  290. return getBattle()->getDefendedTown() ? getBattle()->getDefendedTown()->fortLevel() : CGTownInstance::NONE;
  291. }
  292. bool CBattleInfoEssentials::battleCanSurrender(const PlayerColor & player) const
  293. {
  294. RETURN_IF_NOT_BATTLE(false);
  295. const auto side = playerToSide(player);
  296. if(side == BattleSide::NONE)
  297. return false;
  298. bool iAmSiegeDefender = (side == BattleSide::DEFENDER && getBattle()->getDefendedTown() != nullptr);
  299. //conditions like for fleeing (except escape tunnel presence) + enemy must have a hero
  300. return battleCanFlee(player) && !iAmSiegeDefender && battleHasHero(otherSide(side));
  301. }
  302. bool CBattleInfoEssentials::battleHasHero(BattleSide side) const
  303. {
  304. RETURN_IF_NOT_BATTLE(false);
  305. return getBattle()->getSideHero(side) != nullptr;
  306. }
  307. EWallState CBattleInfoEssentials::battleGetWallState(EWallPart partOfWall) const
  308. {
  309. RETURN_IF_NOT_BATTLE(EWallState::NONE);
  310. if(battleGetSiegeLevel() == CGTownInstance::NONE)
  311. return EWallState::NONE;
  312. return getBattle()->getWallState(partOfWall);
  313. }
  314. EGateState CBattleInfoEssentials::battleGetGateState() const
  315. {
  316. RETURN_IF_NOT_BATTLE(EGateState::NONE);
  317. if(battleGetSiegeLevel() == CGTownInstance::NONE)
  318. return EGateState::NONE;
  319. return getBattle()->getGateState();
  320. }
  321. bool CBattleInfoEssentials::battleIsGatePassable() const
  322. {
  323. RETURN_IF_NOT_BATTLE(true);
  324. if(battleGetSiegeLevel() == CGTownInstance::NONE)
  325. return true;
  326. return battleGetGateState() == EGateState::OPENED || battleGetGateState() == EGateState::DESTROYED;
  327. }
  328. PlayerColor CBattleInfoEssentials::battleGetOwner(const battle::Unit * unit) const
  329. {
  330. RETURN_IF_NOT_BATTLE(PlayerColor::CANNOT_DETERMINE);
  331. PlayerColor initialOwner = getBattle()->getSidePlayer(unit->unitSide());
  332. static const CSelector selector = Selector::type()(BonusType::HYPNOTIZED);
  333. if(unit->hasBonus(selector))
  334. return otherPlayer(initialOwner);
  335. else
  336. return initialOwner;
  337. }
  338. const CGHeroInstance * CBattleInfoEssentials::battleGetOwnerHero(const battle::Unit * unit) const
  339. {
  340. RETURN_IF_NOT_BATTLE(nullptr);
  341. const auto side = playerToSide(battleGetOwner(unit));
  342. if(side == BattleSide::NONE)
  343. return nullptr;
  344. return getBattle()->getSideHero(side);
  345. }
  346. bool CBattleInfoEssentials::battleMatchOwner(const battle::Unit * attacker, const battle::Unit * defender, const boost::logic::tribool positivness) const
  347. {
  348. RETURN_IF_NOT_BATTLE(false);
  349. if(boost::logic::indeterminate(positivness))
  350. return true;
  351. else if(attacker->unitId() == defender->unitId())
  352. return (bool)positivness;
  353. else
  354. return battleMatchOwner(battleGetOwner(attacker), defender, positivness);
  355. }
  356. bool CBattleInfoEssentials::battleMatchOwner(const PlayerColor & attacker, const battle::Unit * defender, const boost::logic::tribool positivness) const
  357. {
  358. RETURN_IF_NOT_BATTLE(false);
  359. PlayerColor initialOwner = getBattle()->getSidePlayer(defender->unitSide());
  360. return boost::logic::indeterminate(positivness) || (attacker == initialOwner) == (bool)positivness;
  361. }
  362. VCMI_LIB_NAMESPACE_END