Browse Source

Refactor player hp percentage calculation to avoid C4244 warnings

pull/2274/head
ephphatha 5 years ago committed by Anders Jenbo
parent
commit
58f3ca9ba3
  1. 16
      Source/control.cpp
  2. 21
      Source/player.h

16
Source/control.cpp

@ -582,12 +582,7 @@ void DrawLifeFlask(const CelOutputBuffer &out)
{
auto &myPlayer = plr[myplr];
double p = 0.0;
if (myPlayer._pMaxHP > 0) {
p = (double)myPlayer._pHitPoints / (double)myPlayer._pMaxHP * 80.0;
}
myPlayer._pHPPer = p;
int filled = myPlayer._pHPPer;
int filled = myPlayer.UpdateHitPointPercentage();
if (filled > 80)
filled = 80;
@ -606,12 +601,7 @@ void UpdateLifeFlask(const CelOutputBuffer &out)
{
auto &myPlayer = plr[myplr];
double p = 0.0;
if (myPlayer._pMaxHP > 0) {
p = (double)myPlayer._pHitPoints / (double)myPlayer._pMaxHP * 80.0;
}
int filled = p;
myPlayer._pHPPer = filled;
int filled = myPlayer.UpdateHitPointPercentage();
if (filled > 69)
filled = 69;
@ -645,7 +635,7 @@ void control_update_life_mana()
int maxMana = std::max(myPlayer._pMaxMana, 0);
int mana = std::max(myPlayer._pMana, 0);
myPlayer._pManaPer = maxMana != 0 ? ((double)mana / (double)maxMana * 80.0) : 0;
myPlayer._pHPPer = (double)myPlayer._pHitPoints / (double)myPlayer._pMaxHP * 80.0;
myPlayer.UpdateHitPointPercentage();
}
void UpdateManaFlask(const CelOutputBuffer &out)

21
Source/player.h

@ -402,6 +402,27 @@ struct PlayerStruct {
* @brief Resets all Data of the current PlayerStruct
*/
void Reset();
/**
* @brief Calculates the players current Hit Points as a percentage of their max HP and stores it for later reference
*
* The stored value is unused...
* @see _pHPPer
* @return The players current hit points as a percentage of their maximum (from 0 to 80%)
*/
int UpdateHitPointPercentage()
{
if (_pMaxHP <= 0) { // divide by zero guard
_pHPPer = 0;
} else {
// Maximum achievable HP is approximately 1200. Diablo uses fixed point integers where the last 6 bits are
// fractional values. This means that we will never overflow HP values normally by doing this multiplication
// as the max value is representable in 17 bits and the multiplication result will be at most 23 bits
_pHPPer = _pHitPoints * 80 / _pMaxHP;
}
return _pHPPer;
}
};
extern int myplr;

Loading…
Cancel
Save