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