2017-06-24 15:51:07 +02:00
|
|
|
/*
|
|
|
|
* CBattleInfoEssentials.cpp, part of VCMI engine
|
|
|
|
*
|
|
|
|
* Authors: listed in file AUTHORS in main folder
|
|
|
|
*
|
|
|
|
* License: GNU General Public License v2.0 or later
|
|
|
|
* Full text of license available in license.txt file, in main folder
|
|
|
|
*
|
|
|
|
*/
|
2017-07-01 20:05:10 +02:00
|
|
|
#include "StdInc.h"
|
2017-06-24 15:51:07 +02:00
|
|
|
#include "CBattleInfoEssentials.h"
|
2017-06-29 01:02:05 +02:00
|
|
|
#include "../CStack.h"
|
2017-06-24 15:51:07 +02:00
|
|
|
#include "BattleInfo.h"
|
2017-06-29 01:02:05 +02:00
|
|
|
#include "../NetPacks.h"
|
|
|
|
#include "../mapObjects/CGTownInstance.h"
|
2017-06-24 15:51:07 +02:00
|
|
|
|
|
|
|
ETerrainType CBattleInfoEssentials::battleTerrainType() const
|
|
|
|
{
|
|
|
|
RETURN_IF_NOT_BATTLE(ETerrainType::WRONG);
|
|
|
|
return getBattle()->terrainType;
|
|
|
|
}
|
|
|
|
|
|
|
|
BFieldType CBattleInfoEssentials::battleGetBattlefieldType() const
|
|
|
|
{
|
|
|
|
RETURN_IF_NOT_BATTLE(BFieldType::NONE);
|
|
|
|
return getBattle()->battlefieldType;
|
|
|
|
}
|
|
|
|
|
2017-07-15 13:08:20 +02:00
|
|
|
std::vector<std::shared_ptr<const CObstacleInstance>> CBattleInfoEssentials::battleGetAllObstacles(boost::optional<BattlePerspective::BattlePerspective> perspective) const
|
2017-06-24 15:51:07 +02:00
|
|
|
{
|
|
|
|
std::vector<std::shared_ptr<const CObstacleInstance> > ret;
|
|
|
|
RETURN_IF_NOT_BATTLE(ret);
|
|
|
|
|
|
|
|
if(!perspective)
|
|
|
|
{
|
|
|
|
//if no particular perspective request, use default one
|
2017-08-05 15:09:29 +02:00
|
|
|
perspective = boost::make_optional(battleGetMySide());
|
2017-06-24 15:51:07 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if(!!player && *perspective != battleGetMySide())
|
|
|
|
{
|
2017-06-26 18:50:35 +02:00
|
|
|
logGlobal->error("Unauthorized access attempt!");
|
2017-06-24 15:51:07 +02:00
|
|
|
assert(0); //I want to notice if that happens
|
|
|
|
//perspective = battleGetMySide();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for(auto oi : getBattle()->obstacles)
|
|
|
|
{
|
|
|
|
if(getBattle()->battleIsObstacleVisibleForSide(*oi, *perspective))
|
|
|
|
ret.push_back(oi);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CBattleInfoEssentials::battleIsObstacleVisibleForSide(const CObstacleInstance & coi, BattlePerspective::BattlePerspective side) const
|
|
|
|
{
|
|
|
|
RETURN_IF_NOT_BATTLE(false);
|
|
|
|
return side == BattlePerspective::ALL_KNOWING || coi.visibleForSide(side, battleHasNativeStack(side));
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CBattleInfoEssentials::battleHasNativeStack(ui8 side) const
|
|
|
|
{
|
|
|
|
RETURN_IF_NOT_BATTLE(false);
|
|
|
|
|
2017-06-26 18:50:35 +02:00
|
|
|
for(const CStack * s : battleGetAllStacks())
|
2017-06-24 15:51:07 +02:00
|
|
|
{
|
2017-07-01 10:34:00 +02:00
|
|
|
if(s->side == side && s->getCreature()->isItNativeTerrain(getBattle()->terrainType))
|
2017-06-24 15:51:07 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-07-15 13:08:20 +02:00
|
|
|
TStacks CBattleInfoEssentials::battleGetAllStacks(bool includeTurrets) const
|
2017-06-24 15:51:07 +02:00
|
|
|
{
|
|
|
|
return battleGetStacksIf([=](const CStack * s)
|
|
|
|
{
|
|
|
|
return !s->isGhost() && (includeTurrets || !s->isTurret());
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
TStacks CBattleInfoEssentials::battleGetStacksIf(TStackFilter predicate) const
|
|
|
|
{
|
|
|
|
TStacks ret;
|
|
|
|
RETURN_IF_NOT_BATTLE(ret);
|
|
|
|
|
|
|
|
vstd::copy_if(getBattle()->stacks, std::back_inserter(ret), predicate);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
TStacks CBattleInfoEssentials::battleAliveStacks() const
|
|
|
|
{
|
|
|
|
return battleGetStacksIf([](const CStack * s){
|
|
|
|
return s->isValidTarget(false);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
TStacks CBattleInfoEssentials::battleAliveStacks(ui8 side) const
|
|
|
|
{
|
|
|
|
return battleGetStacksIf([=](const CStack * s){
|
2017-07-01 10:34:00 +02:00
|
|
|
return s->isValidTarget(false) && s->side == side;
|
2017-06-24 15:51:07 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
int CBattleInfoEssentials::battleGetMoatDmg() const
|
|
|
|
{
|
|
|
|
RETURN_IF_NOT_BATTLE(0);
|
|
|
|
auto town = getBattle()->town;
|
|
|
|
if(!town)
|
|
|
|
return 0;
|
|
|
|
return town->town->moatDamage;
|
|
|
|
}
|
|
|
|
|
|
|
|
const CGTownInstance * CBattleInfoEssentials::battleGetDefendedTown() const
|
|
|
|
{
|
|
|
|
RETURN_IF_NOT_BATTLE(nullptr);
|
|
|
|
if(!getBattle() || getBattle()->town == nullptr)
|
|
|
|
return nullptr;
|
|
|
|
return getBattle()->town;
|
|
|
|
}
|
|
|
|
|
|
|
|
BattlePerspective::BattlePerspective CBattleInfoEssentials::battleGetMySide() const
|
|
|
|
{
|
|
|
|
RETURN_IF_NOT_BATTLE(BattlePerspective::INVALID);
|
|
|
|
if(!player || player.get().isSpectator())
|
|
|
|
return BattlePerspective::ALL_KNOWING;
|
|
|
|
if(*player == getBattle()->sides[0].color)
|
|
|
|
return BattlePerspective::LEFT_SIDE;
|
|
|
|
if(*player == getBattle()->sides[1].color)
|
|
|
|
return BattlePerspective::RIGHT_SIDE;
|
|
|
|
|
2017-08-12 13:36:04 +02:00
|
|
|
logGlobal->error("Cannot find player %s in battle!", player->getStr());
|
2017-06-24 15:51:07 +02:00
|
|
|
return BattlePerspective::INVALID;
|
|
|
|
}
|
|
|
|
|
|
|
|
const CStack * CBattleInfoEssentials::battleActiveStack() const
|
|
|
|
{
|
|
|
|
RETURN_IF_NOT_BATTLE(nullptr);
|
|
|
|
return battleGetStackByID(getBattle()->activeStack);
|
|
|
|
}
|
|
|
|
|
|
|
|
const CStack* CBattleInfoEssentials::battleGetStackByID(int ID, bool onlyAlive) const
|
|
|
|
{
|
|
|
|
RETURN_IF_NOT_BATTLE(nullptr);
|
|
|
|
|
|
|
|
auto stacks = battleGetStacksIf([=](const CStack * s)
|
|
|
|
{
|
|
|
|
return s->ID == ID && (!onlyAlive || s->alive());
|
|
|
|
});
|
|
|
|
|
|
|
|
if(stacks.empty())
|
|
|
|
return nullptr;
|
|
|
|
else
|
|
|
|
return stacks[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CBattleInfoEssentials::battleDoWeKnowAbout(ui8 side) const
|
|
|
|
{
|
|
|
|
RETURN_IF_NOT_BATTLE(false);
|
|
|
|
auto p = battleGetMySide();
|
2017-06-26 18:50:35 +02:00
|
|
|
return p == BattlePerspective::ALL_KNOWING || p == side;
|
2017-06-24 15:51:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
si8 CBattleInfoEssentials::battleTacticDist() const
|
|
|
|
{
|
|
|
|
RETURN_IF_NOT_BATTLE(0);
|
|
|
|
return getBattle()->tacticDistance;
|
|
|
|
}
|
|
|
|
|
|
|
|
si8 CBattleInfoEssentials::battleGetTacticsSide() const
|
|
|
|
{
|
|
|
|
RETURN_IF_NOT_BATTLE(-1);
|
|
|
|
return getBattle()->tacticsSide;
|
|
|
|
}
|
|
|
|
|
|
|
|
const CGHeroInstance * CBattleInfoEssentials::battleGetFightingHero(ui8 side) const
|
|
|
|
{
|
|
|
|
RETURN_IF_NOT_BATTLE(nullptr);
|
|
|
|
if(side > 1)
|
|
|
|
{
|
2017-08-11 19:03:05 +02:00
|
|
|
logGlobal->error("FIXME: %s wrong argument!", __FUNCTION__);
|
2017-06-24 15:51:07 +02:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!battleDoWeKnowAbout(side))
|
|
|
|
{
|
2017-08-11 19:03:05 +02:00
|
|
|
logGlobal->error("FIXME: %s access check ", __FUNCTION__);
|
2017-06-24 15:51:07 +02:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return getBattle()->sides[side].hero;
|
|
|
|
}
|
|
|
|
|
|
|
|
const CArmedInstance * CBattleInfoEssentials::battleGetArmyObject(ui8 side) const
|
|
|
|
{
|
|
|
|
RETURN_IF_NOT_BATTLE(nullptr);
|
|
|
|
if(side > 1)
|
|
|
|
{
|
2017-08-11 19:03:05 +02:00
|
|
|
logGlobal->error("FIXME: %s wrong argument!", __FUNCTION__);
|
2017-06-24 15:51:07 +02:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
if(!battleDoWeKnowAbout(side))
|
|
|
|
{
|
2017-08-12 14:43:41 +02:00
|
|
|
logGlobal->error("FIXME: %s access check!", __FUNCTION__);
|
2017-06-24 15:51:07 +02:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return getBattle()->sides[side].armyObject;
|
|
|
|
}
|
|
|
|
|
2017-06-26 18:50:35 +02:00
|
|
|
InfoAboutHero CBattleInfoEssentials::battleGetHeroInfo(ui8 side) const
|
2017-06-24 15:51:07 +02:00
|
|
|
{
|
|
|
|
auto hero = getBattle()->sides[side].hero;
|
|
|
|
if(!hero)
|
|
|
|
{
|
2017-08-12 14:43:41 +02:00
|
|
|
logGlobal->warn("%s: side %d does not have hero!", __FUNCTION__, static_cast<int>(side));
|
2017-06-24 15:51:07 +02:00
|
|
|
return InfoAboutHero();
|
|
|
|
}
|
|
|
|
InfoAboutHero::EInfoLevel infoLevel = battleDoWeKnowAbout(side) ? InfoAboutHero::EInfoLevel::DETAILED : InfoAboutHero::EInfoLevel::BASIC;
|
|
|
|
return InfoAboutHero(hero, infoLevel);
|
|
|
|
}
|
|
|
|
|
|
|
|
int CBattleInfoEssentials::battleCastSpells(ui8 side) const
|
|
|
|
{
|
|
|
|
RETURN_IF_NOT_BATTLE(-1);
|
|
|
|
return getBattle()->sides[side].castSpellsCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
const IBonusBearer * CBattleInfoEssentials::getBattleNode() const
|
|
|
|
{
|
|
|
|
return getBattle();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CBattleInfoEssentials::battleCanFlee(PlayerColor player) const
|
|
|
|
{
|
|
|
|
RETURN_IF_NOT_BATTLE(false);
|
2017-07-01 10:34:00 +02:00
|
|
|
const auto side = playerToSide(player);
|
|
|
|
if(!side)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
const CGHeroInstance *myHero = battleGetFightingHero(side.get());
|
2017-06-24 15:51:07 +02:00
|
|
|
|
|
|
|
//current player have no hero
|
|
|
|
if(!myHero)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
//eg. one of heroes is wearing shakles of war
|
|
|
|
if(myHero->hasBonusOfType(Bonus::BATTLE_NO_FLEEING))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
//we are besieged defender
|
2017-07-01 10:34:00 +02:00
|
|
|
if(side.get() == BattleSide::DEFENDER && battleGetSiegeLevel())
|
2017-06-24 15:51:07 +02:00
|
|
|
{
|
|
|
|
auto town = battleGetDefendedTown();
|
|
|
|
if(!town->hasBuilt(BuildingID::ESCAPE_TUNNEL, ETownType::STRONGHOLD))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-07-01 10:34:00 +02:00
|
|
|
BattleSideOpt CBattleInfoEssentials::playerToSide(PlayerColor player) const
|
2017-06-24 15:51:07 +02:00
|
|
|
{
|
2017-07-01 10:34:00 +02:00
|
|
|
RETURN_IF_NOT_BATTLE(boost::none);
|
2017-06-24 15:51:07 +02:00
|
|
|
int ret = vstd::find_pos_if(getBattle()->sides, [=](const SideInBattle &side){ return side.color == player; });
|
|
|
|
if(ret < 0)
|
2017-08-12 13:36:04 +02:00
|
|
|
logGlobal->warn("Cannot find side for player %s", player.getStr());
|
2017-06-24 15:51:07 +02:00
|
|
|
|
2017-07-01 10:34:00 +02:00
|
|
|
return BattleSideOpt(ret);
|
|
|
|
}
|
|
|
|
|
|
|
|
ui8 CBattleInfoEssentials::otherSide(ui8 side) const
|
|
|
|
{
|
|
|
|
if(side == BattleSide::ATTACKER)
|
|
|
|
return BattleSide::DEFENDER;
|
|
|
|
else
|
|
|
|
return BattleSide::ATTACKER;
|
2017-06-24 15:51:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CBattleInfoEssentials::playerHasAccessToHeroInfo(PlayerColor player, const CGHeroInstance * h) const
|
|
|
|
{
|
|
|
|
RETURN_IF_NOT_BATTLE(false);
|
2017-07-01 10:34:00 +02:00
|
|
|
const auto side = playerToSide(player);
|
|
|
|
if(side)
|
2017-06-24 15:51:07 +02:00
|
|
|
{
|
2017-07-01 10:34:00 +02:00
|
|
|
if (getBattle()->sides[otherSide(side.get())].hero == h)
|
2017-06-24 15:51:07 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
ui8 CBattleInfoEssentials::battleGetSiegeLevel() const
|
|
|
|
{
|
|
|
|
RETURN_IF_NOT_BATTLE(0);
|
|
|
|
return getBattle()->town ? getBattle()->town->fortLevel() : CGTownInstance::NONE;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CBattleInfoEssentials::battleCanSurrender(PlayerColor player) const
|
|
|
|
{
|
|
|
|
RETURN_IF_NOT_BATTLE(false);
|
2017-07-01 10:34:00 +02:00
|
|
|
const auto side = playerToSide(player);
|
|
|
|
if(!side)
|
|
|
|
return false;
|
|
|
|
bool iAmSiegeDefender = (side.get() == BattleSide::DEFENDER && battleGetSiegeLevel());
|
2017-06-24 15:51:07 +02:00
|
|
|
//conditions like for fleeing (except escape tunnel presence) + enemy must have a hero
|
2017-07-01 10:34:00 +02:00
|
|
|
return battleCanFlee(player) && !iAmSiegeDefender && battleHasHero(otherSide(side.get()));
|
2017-06-24 15:51:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool CBattleInfoEssentials::battleHasHero(ui8 side) const
|
|
|
|
{
|
|
|
|
RETURN_IF_NOT_BATTLE(false);
|
|
|
|
assert(side < 2);
|
|
|
|
return getBattle()->sides[side].hero;
|
|
|
|
}
|
|
|
|
|
|
|
|
si8 CBattleInfoEssentials::battleGetWallState(int partOfWall) const
|
|
|
|
{
|
|
|
|
RETURN_IF_NOT_BATTLE(0);
|
|
|
|
if(getBattle()->town == nullptr || getBattle()->town->fortLevel() == CGTownInstance::NONE)
|
|
|
|
return EWallState::NONE;
|
|
|
|
|
|
|
|
assert(partOfWall >= 0 && partOfWall < EWallPart::PARTS_COUNT);
|
|
|
|
return getBattle()->si.wallState[partOfWall];
|
|
|
|
}
|
|
|
|
|
|
|
|
EGateState CBattleInfoEssentials::battleGetGateState() const
|
|
|
|
{
|
|
|
|
RETURN_IF_NOT_BATTLE(EGateState::NONE);
|
|
|
|
if(getBattle()->town == nullptr || getBattle()->town->fortLevel() == CGTownInstance::NONE)
|
|
|
|
return EGateState::NONE;
|
|
|
|
|
|
|
|
return getBattle()->si.gateState;
|
|
|
|
}
|
|
|
|
|
|
|
|
PlayerColor CBattleInfoEssentials::battleGetOwner(const CStack * stack) const
|
|
|
|
{
|
|
|
|
RETURN_IF_NOT_BATTLE(PlayerColor::CANNOT_DETERMINE);
|
|
|
|
if(stack->hasBonusOfType(Bonus::HYPNOTIZED))
|
|
|
|
return getBattle()->theOtherPlayer(stack->owner);
|
|
|
|
else
|
|
|
|
return stack->owner;
|
|
|
|
}
|
|
|
|
|
|
|
|
const CGHeroInstance * CBattleInfoEssentials::battleGetOwnerHero(const CStack * stack) const
|
|
|
|
{
|
|
|
|
RETURN_IF_NOT_BATTLE(nullptr);
|
2017-07-01 10:34:00 +02:00
|
|
|
const auto side = playerToSide(battleGetOwner(stack));
|
|
|
|
if(!side)
|
|
|
|
return nullptr;
|
|
|
|
return getBattle()->sides.at(side.get()).hero;
|
2017-06-24 15:51:07 +02:00
|
|
|
}
|
|
|
|
|
2017-07-15 13:08:20 +02:00
|
|
|
bool CBattleInfoEssentials::battleMatchOwner(const CStack * attacker, const CStack * defender, const boost::logic::tribool positivness ) const
|
2017-06-24 15:51:07 +02:00
|
|
|
{
|
|
|
|
RETURN_IF_NOT_BATTLE(false);
|
|
|
|
if(boost::logic::indeterminate(positivness))
|
|
|
|
return true;
|
2017-07-04 10:21:22 +02:00
|
|
|
else if(attacker == defender)
|
|
|
|
return positivness;
|
2017-07-09 15:55:10 +02:00
|
|
|
else
|
|
|
|
return battleMatchOwner(battleGetOwner(attacker), defender, positivness);
|
|
|
|
}
|
|
|
|
|
2017-07-15 13:08:20 +02:00
|
|
|
bool CBattleInfoEssentials::battleMatchOwner(const PlayerColor & attacker, const CStack * defender, const boost::logic::tribool positivness ) const
|
2017-07-09 15:55:10 +02:00
|
|
|
{
|
|
|
|
RETURN_IF_NOT_BATTLE(false);
|
|
|
|
if(boost::logic::indeterminate(positivness))
|
|
|
|
return true;
|
2017-06-24 15:51:07 +02:00
|
|
|
else if(defender->owner != battleGetOwner(defender))
|
2017-06-26 18:50:35 +02:00
|
|
|
return true; //mind controlled unit is attackable for both sides
|
2017-06-24 15:51:07 +02:00
|
|
|
else
|
2017-07-09 15:55:10 +02:00
|
|
|
return (attacker == battleGetOwner(defender)) == positivness;
|
2017-06-24 15:51:07 +02:00
|
|
|
}
|