1
0
mirror of https://github.com/vcmi/vcmi.git synced 2025-08-13 19:54:17 +02:00

Refactoring InfoAboutHero + GetHeroInfo

This commit is contained in:
dydzio
2016-09-28 13:22:33 +02:00
parent f9b5ca3374
commit 68af6a0c19
7 changed files with 52 additions and 26 deletions

View File

@@ -216,7 +216,7 @@ void CBattleHero::clickRight(tribool down, bool previousState)
if (down && myOwner->myTurn)
{
if (myHero != nullptr)
targetHero.initFromHero(myHero, true);
targetHero.initFromHero(myHero, InfoAboutHero::EInfoLevel::INBATTLE);
else
targetHero = myOwner->enemyHero();

View File

@@ -294,9 +294,9 @@ CHeroTooltip::CHeroTooltip(Point pos, const InfoAboutHero &hero):
}
CHeroTooltip::CHeroTooltip(Point pos, const CGHeroInstance * hero):
CArmyTooltip(pos, InfoAboutHero(hero, true))
CArmyTooltip(pos, InfoAboutHero(hero, InfoAboutHero::EInfoLevel::DETAILED))
{
init(InfoAboutHero(hero, true));
init(InfoAboutHero(hero, InfoAboutHero::EInfoLevel::DETAILED));
}
void CTownTooltip::init(const InfoAboutTown &town)

View File

@@ -332,7 +332,8 @@ InfoAboutHero CBattleInfoEssentials::battleGetHeroInfo( ui8 side ) const
return InfoAboutHero();
}
return InfoAboutHero(hero, battleDoWeKnowAbout(side));
InfoAboutHero::EInfoLevel infoLevel = battleDoWeKnowAbout(side) ? InfoAboutHero::EInfoLevel::DETAILED : InfoAboutHero::EInfoLevel::BASIC;
return InfoAboutHero(hero, infoLevel);
}
int CBattleInfoEssentials::battleCastSpells(ui8 side) const
@@ -424,6 +425,18 @@ ui8 CBattleInfoEssentials::playerToSide(PlayerColor player) const
return ret;
}
bool CBattleInfoEssentials::playerHasAccessToHeroInfo(PlayerColor player, const CGHeroInstance * h) const
{
RETURN_IF_NOT_BATTLE(false);
ui8 playerSide = playerToSide(player);
if (playerSide >= 0)
{
if (getBattle()->sides[!playerSide].hero == h)
return true;
}
return false;
}
ui8 CBattleInfoEssentials::battleGetSiegeLevel() const
{
RETURN_IF_NOT_BATTLE(0);

View File

@@ -190,6 +190,7 @@ public:
bool battleCanFlee(PlayerColor player) const;
bool battleCanSurrender(PlayerColor player) const;
ui8 playerToSide(PlayerColor player) const;
bool playerHasAccessToHeroInfo(PlayerColor player, const CGHeroInstance * h) const;
ui8 battleGetSiegeLevel() const; //returns 0 when there is no siege, 1 if fort, 2 is citadel, 3 is castle
bool battleHasHero(ui8 side) const;
int battleCastSpells(ui8 side) const; //how many spells has given side cast

View File

@@ -271,32 +271,31 @@ bool CGameInfoCallback::getHeroInfo(const CGObjectInstance * hero, InfoAboutHero
ERROR_RET_VAL_IF(!h, "That's not a hero!", false);
ERROR_RET_VAL_IF(!isVisible(h->getPosition(false)), "That hero is not visible!", false);
bool accessFlag = hasAccess(h->tempOwner);
InfoAboutHero::EInfoLevel infoLevel = InfoAboutHero::EInfoLevel::BASIC;
if (!accessFlag && gs->curB) //if it's battle we can get enemy hero full data
if(hasAccess(h->tempOwner))
infoLevel = InfoAboutHero::EInfoLevel::DETAILED;
if ( (infoLevel == InfoAboutHero::EInfoLevel::BASIC) && gs->curB) //if it's battle we can get enemy hero full data
{
ui8 playerSide = gs->curB->playerToSide(*player);
if (playerSide >= 0)
{
if (gs->curB->sides[!playerSide].hero == h)
accessFlag = true;
}
if(gs->curB->playerHasAccessToHeroInfo(*player, h))
infoLevel = InfoAboutHero::EInfoLevel::INBATTLE;
}
if(!accessFlag && nullptr != selectedObject)
if( (infoLevel == InfoAboutHero::EInfoLevel::BASIC) && nullptr != selectedObject)
{
const CGHeroInstance * selectedHero = dynamic_cast<const CGHeroInstance *>(selectedObject);
if(nullptr != selectedHero)
accessFlag = selectedHero->hasVisions(hero, 1);
if(selectedHero->hasVisions(hero, 1))
infoLevel = InfoAboutHero::EInfoLevel::DETAILED;
}
dest.initFromHero(h, accessFlag);
if (accessFlag && !gs->curB)
dest.details->manaLimit = -1; //we do not want to leak max mana info outside battle so set to meaningless value
dest.initFromHero(h, infoLevel);
//DISGUISED bonus implementation
bool disguiseFlag = (infoLevel == InfoAboutHero::EInfoLevel::DETAILED);
if(getPlayerRelations(getLocalPlayer(), hero->tempOwner) == PlayerRelations::ENEMIES)
{
//todo: bonus cashing
@@ -325,7 +324,7 @@ bool CGameInfoCallback::getHeroInfo(const CGObjectInstance * hero, InfoAboutHero
}
};
auto doAdvancedDisguise = [accessFlag, &doBasicDisguise](InfoAboutHero & info)
auto doAdvancedDisguise = [disguiseFlag, &doBasicDisguise](InfoAboutHero & info)
{
doBasicDisguise(info);

View File

@@ -2619,7 +2619,7 @@ void CGameState::obtainPlayersStats(SThievesGuildInfo & tgi, int level)
continue;
const CGHeroInstance * best = statsHLP::findBestHero(this, g->second.color);
InfoAboutHero iah;
iah.initFromHero(best, level >= 2);
iah.initFromHero(best, (level >= 2) ? InfoAboutHero::EInfoLevel::DETAILED : InfoAboutHero::EInfoLevel::BASIC);
iah.army.clear();
tgi.colorToBestHero[g->second.color] = iah;
}
@@ -3007,12 +3007,12 @@ InfoAboutHero::InfoAboutHero(const InfoAboutHero & iah):
assign(iah);
}
InfoAboutHero::InfoAboutHero(const CGHeroInstance *h, bool detailed)
InfoAboutHero::InfoAboutHero(const CGHeroInstance *h, InfoAboutHero::EInfoLevel infoLevel)
: details(nullptr),
hclass(nullptr),
portrait(-1)
{
initFromHero(h, detailed);
initFromHero(h, infoLevel);
}
InfoAboutHero::~InfoAboutHero()
@@ -3026,11 +3026,13 @@ InfoAboutHero & InfoAboutHero::operator=(const InfoAboutHero & iah)
return *this;
}
void InfoAboutHero::initFromHero(const CGHeroInstance *h, bool detailed)
void InfoAboutHero::initFromHero(const CGHeroInstance *h, InfoAboutHero::EInfoLevel infoLevel)
{
if(!h)
return;
bool detailed = ( (infoLevel == EInfoLevel::DETAILED) || (infoLevel == EInfoLevel::INBATTLE) );
initFromArmy(h, detailed);
hclass = h->type->heroClass;
@@ -3044,13 +3046,16 @@ void InfoAboutHero::initFromHero(const CGHeroInstance *h, bool detailed)
details->luck = h->LuckVal();
details->morale = h->MoraleVal();
details->mana = h->mana;
details->manaLimit = h->manaLimit();
details->primskills.resize(GameConstants::PRIMARY_SKILLS);
for (int i = 0; i < GameConstants::PRIMARY_SKILLS ; i++)
{
details->primskills[i] = h->getPrimSkillLevel(static_cast<PrimarySkill::PrimarySkill>(i));
}
if (infoLevel == EInfoLevel::INBATTLE)
details->manaLimit = h->manaLimit();
else
details->manaLimit = -1; //we do not want to leak max mana info outside battle so set to meaningless value
}
}

View File

@@ -44,6 +44,7 @@ struct DLL_LINKAGE InfoAboutHero : public InfoAboutArmy
{
private:
void assign(const InfoAboutHero & iah);
public:
struct DLL_LINKAGE Details
{
@@ -54,14 +55,21 @@ public:
const CHeroClass *hclass;
int portrait;
enum EInfoLevel
{
BASIC,
DETAILED,
INBATTLE
};
InfoAboutHero();
InfoAboutHero(const InfoAboutHero & iah);
InfoAboutHero(const CGHeroInstance *h, bool detailed);
InfoAboutHero(const CGHeroInstance *h, EInfoLevel infoLevel);
~InfoAboutHero();
InfoAboutHero & operator=(const InfoAboutHero & iah);
void initFromHero(const CGHeroInstance *h, bool detailed);
void initFromHero(const CGHeroInstance *h, EInfoLevel infoLevel);
};
/// Struct which holds a int information about a town