1
0
Fork 0
mirror of https://github.com/OpenMW/openmw.git synced 2025-02-21 08:39:45 +00:00

Add getRatio method

This commit is contained in:
Evil Eye 2022-02-10 22:10:46 +01:00
parent 6b203892fc
commit 054d8babc4
6 changed files with 20 additions and 24 deletions

View file

@ -257,11 +257,7 @@ bool MWDialogue::Filter::testSelectStructNumeric (const SelectWrapper& select) c
case SelectWrapper::Function_PcHealthPercent:
{
MWWorld::Ptr player = MWMechanics::getPlayer();
float ratio = player.getClass().getCreatureStats(player).getHealth().getModified();
if(ratio > 0)
ratio = player.getClass().getCreatureStats(player).getHealth().getCurrent() / ratio;
return select.selectCompare (static_cast<int>(ratio*100));
return select.selectCompare(static_cast<int>(player.getClass().getCreatureStats(player).getHealth().getRatio() * 100));
}
case SelectWrapper::Function_PcDynamicStat:
@ -276,11 +272,7 @@ bool MWDialogue::Filter::testSelectStructNumeric (const SelectWrapper& select) c
case SelectWrapper::Function_HealthPercent:
{
float ratio = mActor.getClass().getCreatureStats(mActor).getHealth().getModified();
if(ratio > 0)
ratio = mActor.getClass().getCreatureStats(mActor).getHealth().getCurrent() / ratio;
return select.selectCompare (static_cast<int>(ratio*100));
return select.selectCompare(static_cast<int>(mActor.getClass().getCreatureStats(mActor).getHealth().getRatio() * 100));
}
default:

View file

@ -608,8 +608,7 @@ namespace MWGui
mEnemyHealth->setProgressRange(100);
// Health is usually cast to int before displaying. Actors die whenever they are < 1 health.
// Therefore any value < 1 should show as an empty health bar. We do the same in statswindow :)
float health = stats.getHealth().getModified();
mEnemyHealth->setProgressPosition(health == 0.f ? 0 : static_cast<size_t>(stats.getHealth().getCurrent() / health * 100));
mEnemyHealth->setProgressPosition(static_cast<size_t>(stats.getHealth().getRatio() * 100));
static const float fNPCHealthBarFade = MWBase::Environment::get().getWorld()->getStore().get<ESM::GameSetting>().find("fNPCHealthBarFade")->mValue.getFloat();
if (fNPCHealthBarFade > 0.f)

View file

@ -476,8 +476,7 @@ namespace MWMechanics
static const float fAIFleeHealthMult = gmst.find("fAIFleeHealthMult")->mValue.getFloat();
static const float fAIFleeFleeMult = gmst.find("fAIFleeFleeMult")->mValue.getFloat();
float healthPercentage = (stats.getHealth().getModified() == 0.0f)
? 1.0f : stats.getHealth().getCurrent() / stats.getHealth().getModified();
float healthPercentage = stats.getHealth().getRatio(false);
float rating = (1.0f - healthPercentage) * fAIFleeHealthMult + flee * fAIFleeFleeMult;
static const int iWereWolfLevelToAttack = gmst.find("iWereWolfLevelToAttack")->mValue.getInteger();

View file

@ -65,6 +65,19 @@ namespace MWMechanics
}
}
template<typename T>
T DynamicStat<T>::getRatio(bool nanIsZero) const
{
T modified = getModified();
if(modified == T{})
{
if(nanIsZero)
return modified;
return {1};
}
return getCurrent() / modified;
}
template<typename T>
void DynamicStat<T>::writeState (ESM::StatState<T>& state) const
{

View file

@ -67,6 +67,7 @@ namespace MWMechanics
const T& getBase() const { return mStatic.getBase(); };
T getModified(bool capped = true) const { return mStatic.getModified(capped); };
const T& getCurrent() const { return mCurrent; };
T getRatio(bool nanIsZero = true) const;
/// Set base and adjust current accordingly.
void setBase(const T& value) { mStatic.setBase(value); };

View file

@ -324,17 +324,9 @@ namespace MWScript
void execute (Interpreter::Runtime& runtime) override
{
MWWorld::Ptr ptr = R()(runtime);
const MWMechanics::CreatureStats& stats = ptr.getClass().getCreatureStats(ptr);
MWMechanics::CreatureStats& stats = ptr.getClass().getCreatureStats (ptr);
Interpreter::Type_Float value = 0;
Interpreter::Type_Float max = stats.getDynamic(mIndex).getModified();
if (max>0)
value = stats.getDynamic(mIndex).getCurrent() / max;
runtime.push (value);
runtime.push(stats.getDynamic(mIndex).getRatio());
}
};