Skip to content

Fix hpbar on hud is not compatible visually with MAX_HEALTH stat #3807 #3929

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 31 additions & 2 deletions Client/game_sa/CHudSA.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,11 @@ CHudSA::CHudSA()
componentProperties.radioName = MapGet(defaultComponentProperties, HUD_RADIO);
componentProperties.weaponIcon = MapGet(defaultComponentProperties, HUD_WEAPON);
componentProperties.wanted = MapGet(defaultComponentProperties, HUD_WANTED);

// Change multiplier for STAT_10 and STAT_9 (MAX_HEALTH) from 176 to 200
// Fix GH #3807
float* maxHealth = reinterpret_cast<float*>(0x8CDE78);
*maxHealth = 200.0f;
}

void CHudSA::Disable(bool bDisabled)
Expand Down Expand Up @@ -635,8 +640,9 @@ void CHudSA::RenderHealthBar(int x, int y)
float barWidth = useCustomSize ? componentProperties.hpBar.placement.customWidth : componentProperties.hpBar.placement.width;

// Calc bar width depending on MAX_HEALTH stat
double statModifier = ((double(__cdecl*)(int))FUNC_CStats_GetFatAndMuscleModifier)(10);
float totalWidth = (barWidth * maxHealth) / statModifier;
// We want to maintain the proportions of the bar and its width after changing MAX_HEALTH from 176 to 200
const float baseWidth = barWidth * 100.0f / 176.0f; // 176 is default STAT_10 value
float totalWidth = baseWidth + (((barWidth - baseWidth) / 100.0f) * (maxHealth - 100.0f));

float posX = useCustomPosition ? componentProperties.hpBar.placement.customX : (barWidth - totalWidth + x);
float posY = useCustomPosition ? componentProperties.hpBar.placement.customY : y;
Expand Down Expand Up @@ -922,6 +928,27 @@ static void HOOK_RenderHudBar(int playerId, int x, int y)
CHudSA::RenderArmorBar(x, y);
}

////////////////////////////////////////////////////////////////////////
// CStats::GetFatAndMuscleModifier
//
// After changing MAX_HEALTH from 176 to 200, we need to maintain the HP proportions
// meaning stat = 569 = 100 HP, stat = 1000 = 200 HP
//
// HP = a * STAT - b
// a = (100/431) = 0.23196
// b = 32 (we use 31.9 due to rounding the result down - with 32, instead of 100 HP, it's 99)
////////////////////////////////////////////////////////////////////////
static const float mult = 0.23196f;
static const float subVal = 31.9f;
static void HOOK_MaxHealthStat_Correction()
{
_asm
{
fmul dword ptr [mult] // stat * a
fsub dword ptr [subVal] // stat * a - b
}
}

void CHudSA::StaticSetHooks()
{
HookInstall(FUNC_RenderHealthBar, &HOOK_RenderHudBar, 11);
Expand All @@ -940,4 +967,6 @@ void CHudSA::StaticSetHooks()
HookInstallCall(0x58D8FD, (DWORD)&RenderWeaponIcon_XLU);

HookInstall(0x58DFD3, &HOOK_RenderWanted);

HookInstall(0x559E0D, &HOOK_MaxHealthStat_Correction, 6);
}
Loading