Skip to content

Commit

Permalink
fix #4637: make ping replay-sync
Browse files Browse the repository at this point in the history
  • Loading branch information
jK committed Mar 10, 2015
1 parent dcea2c8 commit 4a6e653
Show file tree
Hide file tree
Showing 7 changed files with 16 additions and 21 deletions.
9 changes: 4 additions & 5 deletions rts/Game/GUI/PlayerRosterDrawer.cpp
Expand Up @@ -64,17 +64,16 @@ void CPlayerRosterDrawer::Draw()
float4 cpucolor(!p->spectator && p->cpuUsage > 0.75f && gs->speedFactor < gs->wantedSpeedFactor * 0.99f &&
(currentTime & 128) ? 0.5f : std::max(0.01f, std::min(1.0f, p->cpuUsage * 2.0f / 0.75f)),
std::min(1.0f, std::max(0.01f, (1.0f - p->cpuUsage / 0.75f) * 2.0f)), 0.01f, 1.0f);
int ping = (int)(((p->ping) * 1000) / (GAME_SPEED * gs->speedFactor));
float4 pingcolor(!p->spectator && globalConfig->reconnectTimeout > 0 && ping > 1000 * globalConfig->reconnectTimeout &&
(currentTime & 128) ? 0.5f : std::max(0.01f, std::min(1.0f, (ping - 250) / 375.0f)),
std::min(1.0f, std::max(0.01f, (1000 - ping) / 375.0f)), 0.01f, 1.0f);
float4 pingcolor(!p->spectator && globalConfig->reconnectTimeout > 0 && p->ping > 1000 * globalConfig->reconnectTimeout &&
(currentTime & 128) ? 0.5f : std::max(0.01f, std::min(1.0f, (p->ping - 250) / 375.0f)),
std::min(1.0f, std::max(0.01f, (1000 - p->ping) / 375.0f)), 0.01f, 1.0f);
SNPRINTF(buf, sizeof(buf), "\xff%c%c%c%c \t%i \t%s \t\xff%c%c%c%s \t\xff%c%c%c%.0f%% \t\xff%c%c%c%dms",
allycolor[0], allycolor[1], allycolor[2], (gu->spectating && !p->spectator && (gu->myTeam == p->team)) ? '-' : ' ',
p->team, prefix.c_str(), color[0], color[1], color[2], p->name.c_str(),
(unsigned char)(cpucolor[0] * 255.0f), (unsigned char)(cpucolor[1] * 255.0f), (unsigned char)(cpucolor[2] * 255.0f),
p->cpuUsage * 100.0f,
(unsigned char)(pingcolor[0] * 255.0f), (unsigned char)(pingcolor[1] * 255.0f), (unsigned char)(pingcolor[2] * 255.0f),
ping);
p->ping);
}
else {
prefix = "";
Expand Down
4 changes: 1 addition & 3 deletions rts/Lua/LuaSyncedRead.cpp
Expand Up @@ -1433,9 +1433,7 @@ int LuaSyncedRead::GetPlayerInfo(lua_State* L)
lua_pushboolean(L, player->spectator);
lua_pushnumber(L, player->team);
lua_pushnumber(L, teamHandler->AllyTeam(player->team));
const float pingScale = (GAME_SPEED * gs->speedFactor);
const float pingSecs = float(player->ping) / pingScale;
lua_pushnumber(L, pingSecs);
lua_pushnumber(L, player->ping * 0.001f); // in seconds
lua_pushnumber(L, player->cpuUsage);
lua_pushsstring(L, player->countryCode);
lua_pushnumber(L, player->rank);
Expand Down
3 changes: 1 addition & 2 deletions rts/Lua/LuaUnsyncedRead.cpp
Expand Up @@ -1513,10 +1513,9 @@ static void AddPlayerToRoster(lua_State* L, int playerID, bool includePathingFla
PUSH_ROSTER_ENTRY(number, teamHandler->AllyTeam(p->team));
PUSH_ROSTER_ENTRY(boolean, p->spectator);
PUSH_ROSTER_ENTRY(number, p->cpuUsage);
const float pingScale = (GAME_SPEED * gs->speedFactor);

if (!includePathingFlag || p->ping != PATHING_FLAG) {
const float pingSecs = float(p->ping - 1) / pingScale;
const float pingSecs = p->ping * 0.001f;
PUSH_ROSTER_ENTRY(number, pingSecs);
} else {
const float pingSecs = float(p->ping);
Expand Down
2 changes: 1 addition & 1 deletion rts/Net/GameServer.cpp
Expand Up @@ -924,7 +924,7 @@ void CGameServer::LagProtection()
GameParticipant& player = players[a];
if (player.myState == GameParticipant::INGAME) {
// send info about the players
const int curPing = (serverFrameNum - player.lastFrameResponse);
const int curPing = ((serverFrameNum - player.lastFrameResponse) * 1000) / (GAME_SPEED * internalSpeed);
Broadcast(CBaseNetProtocol::Get().SendPlayerInfo(a, player.cpuUsage, curPing));

const float playerCpuUsage = player.cpuUsage;
Expand Down
13 changes: 7 additions & 6 deletions rts/Net/NetCommands.cpp
Expand Up @@ -351,15 +351,16 @@ void CGame::ClientReadNet()
}

case NETMSG_PLAYERINFO: {
const unsigned char player = inbuf[1];
if (!playerHandler->IsValidPlayer(player)) {
LOG_L(L_ERROR, "Got invalid player num %i in playerinfo msg", player);
const unsigned char playerId = inbuf[1];
if (!playerHandler->IsValidPlayer(playerId)) {
LOG_L(L_ERROR, "Got invalid player num %i in playerinfo msg", playerId);
break;
}
playerHandler->Player(player)->cpuUsage = *(float*) &inbuf[2];
playerHandler->Player(player)->ping = *(boost::uint32_t*) &inbuf[6];
CPlayer* p = playerHandler->Player(playerId);
p->cpuUsage = *(float*) &inbuf[2];
p->ping = *(boost::uint32_t*) &inbuf[6];

AddTraffic(player, packetCode, dataLength);
AddTraffic(playerId, packetCode, dataLength);
break;
}

Expand Down
2 changes: 1 addition & 1 deletion rts/Net/Protocol/BaseNetProtocol.h
Expand Up @@ -73,7 +73,7 @@ enum NETMSG {
NETMSG_SYNCRESPONSE = 33, // uchar myPlayerNum; int frameNum; uint checksum;
NETMSG_SYSTEMMSG = 35, // uchar myPlayerNum, std::string message;
NETMSG_STARTPOS = 36, // uchar myPlayerNum, uchar myTeam, ready /*0: not ready, 1: ready, 2: don't update readiness*/; float x, y, z;
NETMSG_PLAYERINFO = 38, // uchar myPlayerNum; float cpuUsage; int ping /*in frames*/;
NETMSG_PLAYERINFO = 38, // uchar myPlayerNum; float cpuUsage; int ping /*in milliseconds*/;
NETMSG_PLAYERLEFT = 39, // uchar myPlayerNum, bIntended /*0: lost connection, 1: left, 2: forced (kicked) */;

#ifdef SYNCDEBUG
Expand Down
4 changes: 1 addition & 3 deletions rts/Rendering/TeamHighlight.cpp
Expand Up @@ -78,9 +78,7 @@ void CTeamHighlight::Update(int frameNum) {
hasPlayers = true;

if (p->ping != PATHING_FLAG && p->ping >= 0) {
const int speed = GAME_SPEED * gs->speedFactor;
const int ping = (p->ping * 1000) / std::max(1, speed);
minPing = std::min(ping, minPing);
minPing = std::min(p->ping, minPing);
}
}
if (!hasPlayers || !t->HasLeader()) {
Expand Down

0 comments on commit 4a6e653

Please sign in to comment.