CBattleInfoEssentials.cpp 9.8 KB

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