CBattleInfoEssentials.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  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. ETerrainType CBattleInfoEssentials::battleTerrainType() const
  17. {
  18. RETURN_IF_NOT_BATTLE(ETerrainType::WRONG);
  19. return getBattle()->terrainType;
  20. }
  21. BFieldType CBattleInfoEssentials::battleGetBattlefieldType() const
  22. {
  23. RETURN_IF_NOT_BATTLE(BFieldType::NONE);
  24. return getBattle()->battlefieldType;
  25. }
  26. std::vector<std::shared_ptr<const CObstacleInstance>> CBattleInfoEssentials::battleGetAllObstacles(boost::optional<BattlePerspective::BattlePerspective> perspective) const
  27. {
  28. std::vector<std::shared_ptr<const CObstacleInstance> > ret;
  29. RETURN_IF_NOT_BATTLE(ret);
  30. if(!perspective)
  31. {
  32. //if no particular perspective request, use default one
  33. perspective = boost::make_optional(battleGetMySide());
  34. }
  35. else
  36. {
  37. if(!!player && *perspective != battleGetMySide())
  38. {
  39. logGlobal->error("Unauthorized access attempt!");
  40. assert(0); //I want to notice if that happens
  41. //perspective = battleGetMySide();
  42. }
  43. }
  44. for(auto oi : getBattle()->obstacles)
  45. {
  46. if(getBattle()->battleIsObstacleVisibleForSide(*oi, *perspective))
  47. ret.push_back(oi);
  48. }
  49. return ret;
  50. }
  51. bool CBattleInfoEssentials::battleIsObstacleVisibleForSide(const CObstacleInstance & coi, BattlePerspective::BattlePerspective side) const
  52. {
  53. RETURN_IF_NOT_BATTLE(false);
  54. return side == BattlePerspective::ALL_KNOWING || coi.visibleForSide(side, battleHasNativeStack(side));
  55. }
  56. bool CBattleInfoEssentials::battleHasNativeStack(ui8 side) const
  57. {
  58. RETURN_IF_NOT_BATTLE(false);
  59. for(const CStack * s : battleGetAllStacks())
  60. {
  61. if(s->side == side && s->getCreature()->isItNativeTerrain(getBattle()->terrainType))
  62. return true;
  63. }
  64. return false;
  65. }
  66. TStacks CBattleInfoEssentials::battleGetAllStacks(bool includeTurrets) const
  67. {
  68. return battleGetStacksIf([=](const CStack * s)
  69. {
  70. return !s->isGhost() && (includeTurrets || !s->isTurret());
  71. });
  72. }
  73. TStacks CBattleInfoEssentials::battleGetStacksIf(TStackFilter predicate) const
  74. {
  75. TStacks ret;
  76. RETURN_IF_NOT_BATTLE(ret);
  77. vstd::copy_if(getBattle()->stacks, std::back_inserter(ret), predicate);
  78. return ret;
  79. }
  80. TStacks CBattleInfoEssentials::battleAliveStacks() const
  81. {
  82. return battleGetStacksIf([](const CStack * s){
  83. return s->isValidTarget(false);
  84. });
  85. }
  86. TStacks CBattleInfoEssentials::battleAliveStacks(ui8 side) const
  87. {
  88. return battleGetStacksIf([=](const CStack * s){
  89. return s->isValidTarget(false) && s->side == side;
  90. });
  91. }
  92. int CBattleInfoEssentials::battleGetMoatDmg() const
  93. {
  94. RETURN_IF_NOT_BATTLE(0);
  95. auto town = getBattle()->town;
  96. if(!town)
  97. return 0;
  98. return town->town->moatDamage;
  99. }
  100. const CGTownInstance * CBattleInfoEssentials::battleGetDefendedTown() const
  101. {
  102. RETURN_IF_NOT_BATTLE(nullptr);
  103. if(!getBattle() || getBattle()->town == nullptr)
  104. return nullptr;
  105. return getBattle()->town;
  106. }
  107. BattlePerspective::BattlePerspective CBattleInfoEssentials::battleGetMySide() const
  108. {
  109. RETURN_IF_NOT_BATTLE(BattlePerspective::INVALID);
  110. if(!player || player.get().isSpectator())
  111. return BattlePerspective::ALL_KNOWING;
  112. if(*player == getBattle()->sides[0].color)
  113. return BattlePerspective::LEFT_SIDE;
  114. if(*player == getBattle()->sides[1].color)
  115. return BattlePerspective::RIGHT_SIDE;
  116. logGlobal->error("Cannot find player %s in battle!", player->getStr());
  117. return BattlePerspective::INVALID;
  118. }
  119. const CStack * CBattleInfoEssentials::battleActiveStack() const
  120. {
  121. RETURN_IF_NOT_BATTLE(nullptr);
  122. return battleGetStackByID(getBattle()->activeStack);
  123. }
  124. const CStack* CBattleInfoEssentials::battleGetStackByID(int ID, bool onlyAlive) const
  125. {
  126. RETURN_IF_NOT_BATTLE(nullptr);
  127. auto stacks = battleGetStacksIf([=](const CStack * s)
  128. {
  129. return s->ID == ID && (!onlyAlive || s->alive());
  130. });
  131. if(stacks.empty())
  132. return nullptr;
  133. else
  134. return stacks[0];
  135. }
  136. bool CBattleInfoEssentials::battleDoWeKnowAbout(ui8 side) const
  137. {
  138. RETURN_IF_NOT_BATTLE(false);
  139. auto p = battleGetMySide();
  140. return p == BattlePerspective::ALL_KNOWING || p == side;
  141. }
  142. si8 CBattleInfoEssentials::battleTacticDist() const
  143. {
  144. RETURN_IF_NOT_BATTLE(0);
  145. return getBattle()->tacticDistance;
  146. }
  147. si8 CBattleInfoEssentials::battleGetTacticsSide() const
  148. {
  149. RETURN_IF_NOT_BATTLE(-1);
  150. return getBattle()->tacticsSide;
  151. }
  152. const CGHeroInstance * CBattleInfoEssentials::battleGetFightingHero(ui8 side) const
  153. {
  154. RETURN_IF_NOT_BATTLE(nullptr);
  155. if(side > 1)
  156. {
  157. logGlobal->error("FIXME: %s wrong argument!", __FUNCTION__);
  158. return nullptr;
  159. }
  160. if(!battleDoWeKnowAbout(side))
  161. {
  162. logGlobal->error("FIXME: %s access check ", __FUNCTION__);
  163. return nullptr;
  164. }
  165. return getBattle()->sides[side].hero;
  166. }
  167. const CArmedInstance * CBattleInfoEssentials::battleGetArmyObject(ui8 side) const
  168. {
  169. RETURN_IF_NOT_BATTLE(nullptr);
  170. if(side > 1)
  171. {
  172. logGlobal->error("FIXME: %s wrong argument!", __FUNCTION__);
  173. return nullptr;
  174. }
  175. if(!battleDoWeKnowAbout(side))
  176. {
  177. logGlobal->error("FIXME: %s access check!", __FUNCTION__);
  178. return nullptr;
  179. }
  180. return getBattle()->sides[side].armyObject;
  181. }
  182. InfoAboutHero CBattleInfoEssentials::battleGetHeroInfo(ui8 side) const
  183. {
  184. auto hero = getBattle()->sides[side].hero;
  185. if(!hero)
  186. {
  187. logGlobal->warn("%s: side %d does not have hero!", __FUNCTION__, static_cast<int>(side));
  188. return InfoAboutHero();
  189. }
  190. InfoAboutHero::EInfoLevel infoLevel = battleDoWeKnowAbout(side) ? InfoAboutHero::EInfoLevel::DETAILED : InfoAboutHero::EInfoLevel::BASIC;
  191. return InfoAboutHero(hero, infoLevel);
  192. }
  193. int CBattleInfoEssentials::battleCastSpells(ui8 side) const
  194. {
  195. RETURN_IF_NOT_BATTLE(-1);
  196. return getBattle()->sides[side].castSpellsCount;
  197. }
  198. const IBonusBearer * CBattleInfoEssentials::getBattleNode() const
  199. {
  200. return getBattle();
  201. }
  202. bool CBattleInfoEssentials::battleCanFlee(PlayerColor player) const
  203. {
  204. RETURN_IF_NOT_BATTLE(false);
  205. const auto side = playerToSide(player);
  206. if(!side)
  207. return false;
  208. const CGHeroInstance *myHero = battleGetFightingHero(side.get());
  209. //current player have no hero
  210. if(!myHero)
  211. return false;
  212. //eg. one of heroes is wearing shakles of war
  213. if(myHero->hasBonusOfType(Bonus::BATTLE_NO_FLEEING))
  214. return false;
  215. //we are besieged defender
  216. if(side.get() == BattleSide::DEFENDER && battleGetSiegeLevel())
  217. {
  218. auto town = battleGetDefendedTown();
  219. if(!town->hasBuilt(BuildingID::ESCAPE_TUNNEL, ETownType::STRONGHOLD))
  220. return false;
  221. }
  222. return true;
  223. }
  224. BattleSideOpt CBattleInfoEssentials::playerToSide(PlayerColor player) const
  225. {
  226. RETURN_IF_NOT_BATTLE(boost::none);
  227. int ret = vstd::find_pos_if(getBattle()->sides, [=](const SideInBattle &side){ return side.color == player; });
  228. if(ret < 0)
  229. logGlobal->warn("Cannot find side for player %s", player.getStr());
  230. return BattleSideOpt(ret);
  231. }
  232. ui8 CBattleInfoEssentials::otherSide(ui8 side) const
  233. {
  234. if(side == BattleSide::ATTACKER)
  235. return BattleSide::DEFENDER;
  236. else
  237. return BattleSide::ATTACKER;
  238. }
  239. bool CBattleInfoEssentials::playerHasAccessToHeroInfo(PlayerColor player, const CGHeroInstance * h) const
  240. {
  241. RETURN_IF_NOT_BATTLE(false);
  242. const auto side = playerToSide(player);
  243. if(side)
  244. {
  245. if (getBattle()->sides[otherSide(side.get())].hero == h)
  246. return true;
  247. }
  248. return false;
  249. }
  250. ui8 CBattleInfoEssentials::battleGetSiegeLevel() const
  251. {
  252. RETURN_IF_NOT_BATTLE(0);
  253. return getBattle()->town ? getBattle()->town->fortLevel() : CGTownInstance::NONE;
  254. }
  255. bool CBattleInfoEssentials::battleCanSurrender(PlayerColor player) const
  256. {
  257. RETURN_IF_NOT_BATTLE(false);
  258. const auto side = playerToSide(player);
  259. if(!side)
  260. return false;
  261. bool iAmSiegeDefender = (side.get() == BattleSide::DEFENDER && battleGetSiegeLevel());
  262. //conditions like for fleeing (except escape tunnel presence) + enemy must have a hero
  263. return battleCanFlee(player) && !iAmSiegeDefender && battleHasHero(otherSide(side.get()));
  264. }
  265. bool CBattleInfoEssentials::battleHasHero(ui8 side) const
  266. {
  267. RETURN_IF_NOT_BATTLE(false);
  268. assert(side < 2);
  269. return getBattle()->sides[side].hero;
  270. }
  271. si8 CBattleInfoEssentials::battleGetWallState(int partOfWall) const
  272. {
  273. RETURN_IF_NOT_BATTLE(0);
  274. if(getBattle()->town == nullptr || getBattle()->town->fortLevel() == CGTownInstance::NONE)
  275. return EWallState::NONE;
  276. assert(partOfWall >= 0 && partOfWall < EWallPart::PARTS_COUNT);
  277. return getBattle()->si.wallState[partOfWall];
  278. }
  279. EGateState CBattleInfoEssentials::battleGetGateState() const
  280. {
  281. RETURN_IF_NOT_BATTLE(EGateState::NONE);
  282. if(getBattle()->town == nullptr || getBattle()->town->fortLevel() == CGTownInstance::NONE)
  283. return EGateState::NONE;
  284. return getBattle()->si.gateState;
  285. }
  286. PlayerColor CBattleInfoEssentials::battleGetOwner(const CStack * stack) const
  287. {
  288. RETURN_IF_NOT_BATTLE(PlayerColor::CANNOT_DETERMINE);
  289. if(stack->hasBonusOfType(Bonus::HYPNOTIZED))
  290. return getBattle()->theOtherPlayer(stack->owner);
  291. else
  292. return stack->owner;
  293. }
  294. const CGHeroInstance * CBattleInfoEssentials::battleGetOwnerHero(const CStack * stack) const
  295. {
  296. RETURN_IF_NOT_BATTLE(nullptr);
  297. const auto side = playerToSide(battleGetOwner(stack));
  298. if(!side)
  299. return nullptr;
  300. return getBattle()->sides.at(side.get()).hero;
  301. }
  302. bool CBattleInfoEssentials::battleMatchOwner(const CStack * attacker, const CStack * defender, const boost::logic::tribool positivness ) const
  303. {
  304. RETURN_IF_NOT_BATTLE(false);
  305. if(boost::logic::indeterminate(positivness))
  306. return true;
  307. else if(attacker == defender)
  308. return positivness;
  309. else
  310. return battleMatchOwner(battleGetOwner(attacker), defender, positivness);
  311. }
  312. bool CBattleInfoEssentials::battleMatchOwner(const PlayerColor & attacker, const CStack * defender, const boost::logic::tribool positivness ) const
  313. {
  314. RETURN_IF_NOT_BATTLE(false);
  315. if(boost::logic::indeterminate(positivness))
  316. return true;
  317. else if(defender->owner != battleGetOwner(defender))
  318. return true; //mind controlled unit is attackable for both sides
  319. else
  320. return (attacker == battleGetOwner(defender)) == positivness;
  321. }