CBattleInfoEssentials.cpp 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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 /*= boost::none*/) 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 = 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->attackerOwned == !side && s->getCreature()->isItNativeTerrain(getBattle()->terrainType))
  62. return true;
  63. }
  64. return false;
  65. }
  66. TStacks CBattleInfoEssentials::battleGetAllStacks(bool includeTurrets /*= false*/) 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->attackerOwned == !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->errorStream() << "Cannot find player " << *player << " in battle!";
  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->errorStream() << "FIXME: " << __FUNCTION__ << " wrong argument!";
  158. return nullptr;
  159. }
  160. if(!battleDoWeKnowAbout(side))
  161. {
  162. logGlobal->errorStream() << "FIXME: " << __FUNCTION__ << " access check ";
  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->errorStream() << "FIXME: " << __FUNCTION__ << " wrong argument!";
  173. return nullptr;
  174. }
  175. if(!battleDoWeKnowAbout(side))
  176. {
  177. logGlobal->errorStream() << "FIXME: " << __FUNCTION__ << " access check ";
  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->warnStream() << __FUNCTION__ << ": side " << (int)side << " does not have hero!";
  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 si8 mySide = playerToSide(player);
  206. const CGHeroInstance *myHero = battleGetFightingHero(mySide);
  207. //current player have no hero
  208. if(!myHero)
  209. return false;
  210. //eg. one of heroes is wearing shakles of war
  211. if(myHero->hasBonusOfType(Bonus::BATTLE_NO_FLEEING))
  212. return false;
  213. //we are besieged defender
  214. if(mySide == BattleSide::DEFENDER && battleGetSiegeLevel())
  215. {
  216. auto town = battleGetDefendedTown();
  217. if(!town->hasBuilt(BuildingID::ESCAPE_TUNNEL, ETownType::STRONGHOLD))
  218. return false;
  219. }
  220. return true;
  221. }
  222. si8 CBattleInfoEssentials::playerToSide(PlayerColor player) const
  223. {
  224. RETURN_IF_NOT_BATTLE(-1);
  225. int ret = vstd::find_pos_if(getBattle()->sides, [=](const SideInBattle &side){ return side.color == player; });
  226. if(ret < 0)
  227. logGlobal->warnStream() << "Cannot find side for player " << player;
  228. return ret;
  229. }
  230. bool CBattleInfoEssentials::playerHasAccessToHeroInfo(PlayerColor player, const CGHeroInstance * h) const
  231. {
  232. RETURN_IF_NOT_BATTLE(false);
  233. const si8 playerSide = playerToSide(player);
  234. if (playerSide >= 0)
  235. {
  236. if (getBattle()->sides[!playerSide].hero == h)
  237. return true;
  238. }
  239. return false;
  240. }
  241. ui8 CBattleInfoEssentials::battleGetSiegeLevel() const
  242. {
  243. RETURN_IF_NOT_BATTLE(0);
  244. return getBattle()->town ? getBattle()->town->fortLevel() : CGTownInstance::NONE;
  245. }
  246. bool CBattleInfoEssentials::battleCanSurrender(PlayerColor player) const
  247. {
  248. RETURN_IF_NOT_BATTLE(false);
  249. ui8 mySide = playerToSide(player);
  250. bool iAmSiegeDefender = (mySide == BattleSide::DEFENDER && battleGetSiegeLevel());
  251. //conditions like for fleeing (except escape tunnel presence) + enemy must have a hero
  252. return battleCanFlee(player) && !iAmSiegeDefender && battleHasHero(!mySide);
  253. }
  254. bool CBattleInfoEssentials::battleHasHero(ui8 side) const
  255. {
  256. RETURN_IF_NOT_BATTLE(false);
  257. assert(side < 2);
  258. return getBattle()->sides[side].hero;
  259. }
  260. si8 CBattleInfoEssentials::battleGetWallState(int partOfWall) const
  261. {
  262. RETURN_IF_NOT_BATTLE(0);
  263. if(getBattle()->town == nullptr || getBattle()->town->fortLevel() == CGTownInstance::NONE)
  264. return EWallState::NONE;
  265. assert(partOfWall >= 0 && partOfWall < EWallPart::PARTS_COUNT);
  266. return getBattle()->si.wallState[partOfWall];
  267. }
  268. EGateState CBattleInfoEssentials::battleGetGateState() const
  269. {
  270. RETURN_IF_NOT_BATTLE(EGateState::NONE);
  271. if(getBattle()->town == nullptr || getBattle()->town->fortLevel() == CGTownInstance::NONE)
  272. return EGateState::NONE;
  273. return getBattle()->si.gateState;
  274. }
  275. PlayerColor CBattleInfoEssentials::battleGetOwner(const CStack * stack) const
  276. {
  277. RETURN_IF_NOT_BATTLE(PlayerColor::CANNOT_DETERMINE);
  278. if(stack->hasBonusOfType(Bonus::HYPNOTIZED))
  279. return getBattle()->theOtherPlayer(stack->owner);
  280. else
  281. return stack->owner;
  282. }
  283. const CGHeroInstance * CBattleInfoEssentials::battleGetOwnerHero(const CStack * stack) const
  284. {
  285. RETURN_IF_NOT_BATTLE(nullptr);
  286. return getBattle()->sides.at(playerToSide(battleGetOwner(stack))).hero;
  287. }
  288. bool CBattleInfoEssentials::battleMatchOwner(const CStack * attacker, const CStack * defender, const boost::logic::tribool positivness /* = false*/) const
  289. {
  290. RETURN_IF_NOT_BATTLE(false);
  291. if(boost::logic::indeterminate(positivness))
  292. return true;
  293. else if(defender->owner != battleGetOwner(defender))
  294. return true; //mind controlled unit is attackable for both sides
  295. else
  296. return (battleGetOwner(attacker) == battleGetOwner(defender)) == positivness;
  297. }