CBattleInfoEssentials.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  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. const CStack* CBattleInfoEssentials::battleGetStackByUnitId(int unitId, bool onlyAlive) const
  156. {
  157. RETURN_IF_NOT_BATTLE(nullptr);
  158. auto stacks = battleGetStacksIf([=](const CStack * s)
  159. {
  160. return s->unitId() == unitId && (!onlyAlive || s->alive());
  161. });
  162. if(stacks.empty())
  163. return nullptr;
  164. else
  165. return stacks[0];
  166. }
  167. bool CBattleInfoEssentials::battleDoWeKnowAbout(ui8 side) const
  168. {
  169. RETURN_IF_NOT_BATTLE(false);
  170. auto p = battleGetMySide();
  171. return p == BattlePerspective::ALL_KNOWING || p == side;
  172. }
  173. si8 CBattleInfoEssentials::battleTacticDist() const
  174. {
  175. RETURN_IF_NOT_BATTLE(0);
  176. return getBattle()->getTacticDist();
  177. }
  178. si8 CBattleInfoEssentials::battleGetTacticsSide() const
  179. {
  180. RETURN_IF_NOT_BATTLE(-1);
  181. return getBattle()->getTacticsSide();
  182. }
  183. const CGHeroInstance * CBattleInfoEssentials::battleGetFightingHero(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()->getSideHero(side);
  197. }
  198. const CArmedInstance * CBattleInfoEssentials::battleGetArmyObject(ui8 side) const
  199. {
  200. RETURN_IF_NOT_BATTLE(nullptr);
  201. if(side > 1)
  202. {
  203. logGlobal->error("FIXME: %s wrong argument!", __FUNCTION__);
  204. return nullptr;
  205. }
  206. if(!battleDoWeKnowAbout(side))
  207. {
  208. logGlobal->error("FIXME: %s access check!", __FUNCTION__);
  209. return nullptr;
  210. }
  211. return getBattle()->getSideArmy(side);
  212. }
  213. InfoAboutHero CBattleInfoEssentials::battleGetHeroInfo(ui8 side) const
  214. {
  215. auto hero = getBattle()->getSideHero(side);
  216. if(!hero)
  217. {
  218. return InfoAboutHero();
  219. }
  220. InfoAboutHero::EInfoLevel infoLevel = battleDoWeKnowAbout(side) ? InfoAboutHero::EInfoLevel::DETAILED : InfoAboutHero::EInfoLevel::BASIC;
  221. return InfoAboutHero(hero, infoLevel);
  222. }
  223. uint32_t CBattleInfoEssentials::battleCastSpells(ui8 side) const
  224. {
  225. RETURN_IF_NOT_BATTLE(-1);
  226. return getBattle()->getCastSpells(side);
  227. }
  228. const IBonusBearer * CBattleInfoEssentials::getBattleNode() const
  229. {
  230. return getBattle()->asBearer();
  231. }
  232. bool CBattleInfoEssentials::battleCanFlee(PlayerColor player) const
  233. {
  234. RETURN_IF_NOT_BATTLE(false);
  235. const auto side = playerToSide(player);
  236. if(!side)
  237. return false;
  238. const CGHeroInstance *myHero = battleGetFightingHero(side.get());
  239. //current player have no hero
  240. if(!myHero)
  241. return false;
  242. //eg. one of heroes is wearing shakles of war
  243. if(myHero->hasBonusOfType(Bonus::BATTLE_NO_FLEEING))
  244. return false;
  245. //we are besieged defender
  246. if(side.get() == BattleSide::DEFENDER && battleGetSiegeLevel())
  247. {
  248. auto town = battleGetDefendedTown();
  249. if(!town->hasBuilt(BuildingSubID::ESCAPE_TUNNEL))
  250. return false;
  251. }
  252. return true;
  253. }
  254. BattleSideOpt CBattleInfoEssentials::playerToSide(PlayerColor player) const
  255. {
  256. RETURN_IF_NOT_BATTLE(boost::none);
  257. if(getBattle()->getSidePlayer(BattleSide::ATTACKER) == player)
  258. return BattleSideOpt(BattleSide::ATTACKER);
  259. if(getBattle()->getSidePlayer(BattleSide::DEFENDER) == player)
  260. return BattleSideOpt(BattleSide::DEFENDER);
  261. logGlobal->warn("Cannot find side for player %s", player.getStr());
  262. return boost::none;
  263. }
  264. PlayerColor CBattleInfoEssentials::sideToPlayer(ui8 side) const
  265. {
  266. RETURN_IF_NOT_BATTLE(PlayerColor::CANNOT_DETERMINE);
  267. return getBattle()->getSidePlayer(side);
  268. }
  269. ui8 CBattleInfoEssentials::otherSide(ui8 side) const
  270. {
  271. if(side == BattleSide::ATTACKER)
  272. return BattleSide::DEFENDER;
  273. else
  274. return BattleSide::ATTACKER;
  275. }
  276. PlayerColor CBattleInfoEssentials::otherPlayer(PlayerColor player) const
  277. {
  278. RETURN_IF_NOT_BATTLE(PlayerColor::CANNOT_DETERMINE);
  279. auto side = playerToSide(player);
  280. if(!side)
  281. return PlayerColor::CANNOT_DETERMINE;
  282. return getBattle()->getSidePlayer(otherSide(side.get()));
  283. }
  284. bool CBattleInfoEssentials::playerHasAccessToHeroInfo(PlayerColor player, const CGHeroInstance * h) const
  285. {
  286. RETURN_IF_NOT_BATTLE(false);
  287. const auto side = playerToSide(player);
  288. if(side)
  289. {
  290. auto opponentSide = otherSide(side.get());
  291. if(getBattle()->getSideHero(opponentSide) == h)
  292. return true;
  293. }
  294. return false;
  295. }
  296. ui8 CBattleInfoEssentials::battleGetSiegeLevel() const
  297. {
  298. RETURN_IF_NOT_BATTLE(CGTownInstance::NONE);
  299. return getBattle()->getDefendedTown() ? getBattle()->getDefendedTown()->fortLevel() : CGTownInstance::NONE;
  300. }
  301. bool CBattleInfoEssentials::battleCanSurrender(PlayerColor player) const
  302. {
  303. RETURN_IF_NOT_BATTLE(false);
  304. const auto side = playerToSide(player);
  305. if(!side)
  306. return false;
  307. bool iAmSiegeDefender = (side.get() == BattleSide::DEFENDER && battleGetSiegeLevel());
  308. //conditions like for fleeing (except escape tunnel presence) + enemy must have a hero
  309. return battleCanFlee(player) && !iAmSiegeDefender && battleHasHero(otherSide(side.get()));
  310. }
  311. bool CBattleInfoEssentials::battleHasHero(ui8 side) const
  312. {
  313. RETURN_IF_NOT_BATTLE(false);
  314. return getBattle()->getSideHero(side) != nullptr;
  315. }
  316. EWallState CBattleInfoEssentials::battleGetWallState(EWallPart partOfWall) const
  317. {
  318. RETURN_IF_NOT_BATTLE(EWallState::NONE);
  319. if(battleGetSiegeLevel() == CGTownInstance::NONE)
  320. return EWallState::NONE;
  321. return getBattle()->getWallState(partOfWall);
  322. }
  323. EGateState CBattleInfoEssentials::battleGetGateState() const
  324. {
  325. RETURN_IF_NOT_BATTLE(EGateState::NONE);
  326. if(battleGetSiegeLevel() == CGTownInstance::NONE)
  327. return EGateState::NONE;
  328. return getBattle()->getGateState();
  329. }
  330. PlayerColor CBattleInfoEssentials::battleGetOwner(const battle::Unit * unit) const
  331. {
  332. RETURN_IF_NOT_BATTLE(PlayerColor::CANNOT_DETERMINE);
  333. PlayerColor initialOwner = getBattle()->getSidePlayer(unit->unitSide());
  334. static CSelector selector = Selector::type()(Bonus::HYPNOTIZED);
  335. static std::string cachingString = "type_103s-1";
  336. if(unit->hasBonus(selector, cachingString))
  337. return otherPlayer(initialOwner);
  338. else
  339. return initialOwner;
  340. }
  341. const CGHeroInstance * CBattleInfoEssentials::battleGetOwnerHero(const battle::Unit * unit) const
  342. {
  343. RETURN_IF_NOT_BATTLE(nullptr);
  344. const auto side = playerToSide(battleGetOwner(unit));
  345. if(!side)
  346. return nullptr;
  347. return getBattle()->getSideHero(side.get());
  348. }
  349. bool CBattleInfoEssentials::battleMatchOwner(const battle::Unit * attacker, const battle::Unit * defender, const boost::logic::tribool positivness) const
  350. {
  351. RETURN_IF_NOT_BATTLE(false);
  352. if(boost::logic::indeterminate(positivness))
  353. return true;
  354. else if(attacker->unitId() == defender->unitId())
  355. return (bool)positivness;
  356. else
  357. return battleMatchOwner(battleGetOwner(attacker), defender, positivness);
  358. }
  359. bool CBattleInfoEssentials::battleMatchOwner(const PlayerColor & attacker, const battle::Unit * defender, const boost::logic::tribool positivness) const
  360. {
  361. RETURN_IF_NOT_BATTLE(false);
  362. PlayerColor initialOwner = getBattle()->getSidePlayer(defender->unitSide());
  363. return boost::logic::indeterminate(positivness) || (attacker == initialOwner) == (bool)positivness;
  364. }
  365. VCMI_LIB_NAMESPACE_END