halflife icon indicating copy to clipboard operation
halflife copied to clipboard

[HLSDK] Do not send incorrect health to client

Open xPaw opened this issue 13 years ago • 6 comments

    if (pev->health != m_iClientHealth)
    {
        int iHealth = max( static_cast<int>(pev->health), 0 );  // make sure that no negative health values are sent

        // send "health" update message
        MESSAGE_BEGIN( MSG_ONE, gmsgHealth, NULL, pev );
            WRITE_BYTE( iHealth );
        MESSAGE_END();

        m_iClientHealth = static_cast<int>(pev->health);
    }

This part of the code is responsible for sending health to client in CBasePlayer :: UpdateClientData, but it never clamps the value to 255 (it sends byte, so that's the limit). If player's real health is above 255, it will just keep overflowing. If player's health is exactly 256 (or any value % 256), then value of 0 will be sent to client, making the client think that it's dead because CL_IsDead in input.cpp assumes that client is dead by only checking if client's health is below or equal to 0. The fix for this is simple: if player's real health is above 255, only send 255. And yes, this can happen on maps without any mods (doors with damage below zero will make players gain health. These kinds of health machines are widely used on KZ maps)

xPaw avatar Feb 22 '13 09:02 xPaw

Yes, I confirm it. The 0 hp bug sometimes gets really annoying.

Just replace:

int iHealth = max( static_cast<int>(pev->health), 0 );

with:

int iHealth = clamp( static_cast<int>(pev->health), 0, 255 );

MPNumB avatar Feb 22 '13 09:02 MPNumB

Alternatively, byte could be replaced with something more reasonable :cake:

xPaw avatar Feb 22 '13 09:02 xPaw

mmm, cake.

alfred-valve avatar Feb 22 '13 17:02 alfred-valve

Since this is somewhat related (HL not liking when it has to display more than 255 HP on HUD, etc.) may I bring this one up too #217?

Careful, by the way, cakes tend to be a lie.

Egon-Spengler avatar Feb 22 '13 17:02 Egon-Spengler

Issue #1008 was closed but it was a bit different, although it would most likely be the same section of code.

As you can see pev->health is being cast to an int, so if the players health was 0.8 for example the .8 would be truncated and the result returned is 0.

So if the health message stays the same and only sends a single byte, then iHealth will need to be set to 1 if pev->health is less than 1 and greater than 0.


Upon further testing (in TFC) I found that if the Health message is forcefully sent with a value of 0 the clients health will show as 0 and strange things will happen such as not seeing muzzleflashes and projectile based weapon fire.

I had to actually set the players pev->health to 0.1 to 0.9 to see the bug I originally talked about in issue #1008. This leads me to believe issue #488 and issue #1008 are two separate bugs and that report should be reopened.

hlstriker avatar Apr 13 '13 02:04 hlstriker

@hlstriker , these are related, it is just an issue at either end of the bounds. I will clamp it to 255 at the top end and 0 at the bottom, but also round > 0.0 && < 1.0 to 1, so if you have any health the client logic knows.

alfred-valve avatar Apr 26 '13 19:04 alfred-valve