fix: nameplate health bar division by zero when maxHealth is 0

Freshly spawned entities have maxHealth=0 before fields populate.
0/0 produces NaN which propagates through all geometry calculations
for that nameplate frame. Guard with a maxHealth>0 check.
This commit is contained in:
Kelsi 2026-03-29 19:08:51 -07:00
parent 7462fdd41f
commit e629898bfb

View file

@ -11832,9 +11832,12 @@ void GameScreen::renderNameplates(game::GameHandler& gameHandler) {
const float barH = 8.0f * nameplateScale_;
const float barX = sx - barW * 0.5f;
float healthPct = std::clamp(
static_cast<float>(unit->getHealth()) / static_cast<float>(unit->getMaxHealth()),
0.0f, 1.0f);
// Guard against division by zero when maxHealth hasn't been populated yet
// (freshly spawned entity with default fields). 0/0 produces NaN which
// poisons all downstream geometry; +inf is clamped but still wasteful.
float healthPct = (unit->getMaxHealth() > 0)
? std::clamp(static_cast<float>(unit->getHealth()) / static_cast<float>(unit->getMaxHealth()), 0.0f, 1.0f)
: 0.0f;
drawList->AddRectFilled(ImVec2(barX, sy), ImVec2(barX + barW, sy + barH), bgColor, 2.0f);
// For corpses, don't fill health bar (just show grey background)