From e629898bfb394f93c3e5eb5f33186db9e0d11ff4 Mon Sep 17 00:00:00 2001 From: Kelsi Date: Sun, 29 Mar 2026 19:08:51 -0700 Subject: [PATCH] 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. --- src/ui/game_screen.cpp | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/ui/game_screen.cpp b/src/ui/game_screen.cpp index 9491e238..b5e932bd 100644 --- a/src/ui/game_screen.cpp +++ b/src/ui/game_screen.cpp @@ -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(unit->getHealth()) / static_cast(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(unit->getHealth()) / static_cast(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)