CBattleInfoEssentials.cpp 12 KB

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