Skip to content

Commit

Permalink
fix npcs showing player name sometimes
Browse files Browse the repository at this point in the history
This happens because the client doesn't immediately update the status bar when receiving an update from the server. With packet loss and lag, there is a chance status bar messages arrive at the same time as the name revert message. Delaying the name revert until a full second after there haven't been any status bar changes should fix 99% of cases.

Also fixed NPC names flickering between red and blue while taking damage.
  • Loading branch information
wootguy committed Oct 6, 2024
1 parent 09adb6b commit 73e861e
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 8 deletions.
23 changes: 20 additions & 3 deletions dlls/CBasePlayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2204,10 +2204,11 @@ void CBasePlayer::UpdateStatusBar()
}
}

if ((!fakePlayerInfo.enabled && tempNameActive) || tempNameActive >= 2) {
if (tempNameActive >= 10) {
// have a to wait a bit before resetting the player name and color because
// the client doesn't parse the new status bar text until its next rendering frame.
// This can fail if the client fps is less than 10 (or whatever speed this func is called).
// With packet loss and lag, this message can arrive at the same time the status bar
// was last updated, causing the player name to show instead of the npc name.
tempNameActive = 0;
Rename(STRING(pev->netname), false, MSG_ONE, edict());
UpdateTeamInfo(-1, MSG_ONE, edict());
Expand Down Expand Up @@ -5548,7 +5549,23 @@ void CBasePlayer::UpdateScore() {
m_lastScoreUpdate = g_engfuncs.pfnTime();
m_lastScore = pev->frags;

UpdateTeamInfo();
if (tempNameActive) {
for (int i = 1; i <= gpGlobals->maxClients; i++)
{
CBaseEntity* pPlayer = UTIL_PlayerByIndex(i);

if (!pPlayer) {
continue;
}

// prevent flashing NPC names while gaining score shooting them
UpdateTeamInfo(i == entindex() ? m_tempTeam : -1, MSG_ONE, edict());
}
}
else {
UpdateTeamInfo();
}

}

void CBasePlayer::UpdateTeamInfo(int color, int msg_mode, edict_t* dst) {
Expand Down
10 changes: 5 additions & 5 deletions dlls/multiplay_gamerules.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -910,15 +910,15 @@ void CHalfLifeMultiplay::DeathNotice( CBaseMonster *pVictim, entvars_t *pKiller,
CBasePlayer* plr = (CBasePlayer*)pVictim;
if (plr->tempNameActive && plr != hackedPlayer1 && plr != hackedPlayer2) {
plr->Rename(STRING(plr->pev->netname), true, MSG_ONE, plr->edict());
plr->UpdateTeamInfo(-1, MSG_ONE, plr->edict());
plr->UpdateTeamInfo(plr->GetNameColor(), MSG_ONE, plr->edict());
}
}
if (Killer->IsPlayer()) {
CBasePlayer* plr = (CBasePlayer*)Killer;
if (plr->tempNameActive && plr != hackedPlayer1 && plr != hackedPlayer2) {
if (!originalKillerName)
plr->Rename(STRING(plr->pev->netname), true, MSG_ONE, plr->edict());
plr->UpdateTeamInfo(-1, MSG_ONE, plr->edict());
plr->UpdateTeamInfo(plr->GetNameColor(), MSG_ONE, plr->edict());
}
}

Expand All @@ -931,7 +931,7 @@ void CHalfLifeMultiplay::DeathNotice( CBaseMonster *pVictim, entvars_t *pKiller,
// restore player name and team info
if (hackedPlayer1) {
hackedPlayer1->Rename(hackedKillerOriginalName, false);
hackedPlayer1->UpdateTeamInfo();
hackedPlayer1->UpdateTeamInfo(hackedPlayer1->GetNameColor());

// TODO: this is sending the hacked player double the network packets, and these are already heavy
if (hackedPlayer1->tempNameActive) {
Expand All @@ -942,11 +942,11 @@ void CHalfLifeMultiplay::DeathNotice( CBaseMonster *pVictim, entvars_t *pKiller,
if (originalKillerName) {
CBasePlayer* plr = (CBasePlayer*)Killer;
plr->Rename(originalKillerName, false);
plr->UpdateTeamInfo(); // forces name on client to update NOW
plr->UpdateTeamInfo(plr->GetNameColor()); // forces name on client to update NOW
}
if (hackedPlayer2) {
hackedPlayer2->Rename(hackedVictimOriginalName, false);
hackedPlayer2->UpdateTeamInfo();
hackedPlayer2->UpdateTeamInfo(hackedPlayer2->GetNameColor());

if (hackedPlayer2->tempNameActive) {
hackedPlayer2->Rename(hackedPlayer2->m_tempName, false, MSG_ONE, hackedPlayer2->edict());
Expand Down

0 comments on commit 73e861e

Please sign in to comment.