CBattleInfoEssentials.cpp 12 KB

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