From 542b8a7171a8e5daec4a0ec206c88c9bbde16a9e Mon Sep 17 00:00:00 2001 From: Edward Richardson Date: Sat, 16 Aug 2014 21:15:39 +1200 Subject: [PATCH] Added network load balancing - Guests can now attempt to match latency with the arbitrator. (net_loadbalance) - Added althud feature to show arbitrator and local latency. (hud_showlag 1 is on for netgames, 2 is always on) --- src/d_net.cpp | 34 +++++++++++++++++++++++---- src/d_net.h | 1 + src/g_shared/shared_hud.cpp | 47 +++++++++++++++++++++++++++++++++++++ wadsrc/static/menudef.txt | 8 +++++++ 4 files changed, 86 insertions(+), 4 deletions(-) diff --git a/src/d_net.cpp b/src/d_net.cpp index 9ba6eefa0..8bbe70e43 100644 --- a/src/d_net.cpp +++ b/src/d_net.cpp @@ -117,6 +117,7 @@ int playerfornode[MAXNETNODES]; int maketic; int skiptics; int ticdup; +int arb_maketic; // Arbitrators maketic difference void D_ProcessEvents (void); void G_BuildTiccmd (ticcmd_t *cmd); @@ -151,6 +152,8 @@ CUSTOM_CVAR (Bool, cl_capfps, false, CVAR_ARCHIVE|CVAR_GLOBALCONFIG) } } +CVAR(Bool, net_loadbalance, true, CVAR_SERVERINFO) + // [RH] Special "ticcmds" get stored in here static struct TicSpecial { @@ -347,6 +350,9 @@ int NetbufferSize () k += netbuffer[k] + 1; } + if (doomcom.remotenode == nodeforplayer[Net_Arbitrator]) + k++; + if (netbuffer[0] & NCMD_MULTI) { count = netbuffer[k]; @@ -570,6 +576,9 @@ bool HGetPacket (void) if (doomcom.datalength != NetbufferSize ()) { + Printf("Bad packet length %i (calculated %i)\n", + doomcom.datalength, NetbufferSize()); + if (debugfile) fprintf (debugfile,"---bad packet length %i (calculated %i)\n", doomcom.datalength, NetbufferSize()); @@ -782,6 +791,11 @@ void GetPackets (void) } } + if (netconsole == Net_Arbitrator) + { + arb_maketic = netbuffer[k++]; + } + playerbytes[0] = netconsole; if (netbuffer[0] & NCMD_MULTI) { @@ -1190,6 +1204,12 @@ void NetUpdate (void) } } + if (consoleplayer == Net_Arbitrator) + { + // The number of tics we just made should be removed from the count. + netbuffer[k++] = ((maketic - newtics - gametic) / ticdup); + } + if (numtics > 0) { int l; @@ -1298,10 +1318,16 @@ void NetUpdate (void) // very jerky. The way I have it written right now basically means // that it won't adapt. Fortunately, player prediction helps // alleviate the lag somewhat. - - if (NetMode != NET_PacketServer) + int average = 0; + if (net_loadbalance) + average = (((maketic - newtics - gametic) / ticdup) + arb_maketic) / 2; + if (NetMode == NET_PeerToPeer) { - mastertics = nettics[nodeforplayer[Net_Arbitrator]]; + mastertics = nettics[nodeforplayer[Net_Arbitrator]] + average; + } + else if (NetMode == NET_PacketServer) + { + mastertics = mastertics + average; } if (nettics[0] <= mastertics) { @@ -2713,7 +2739,7 @@ void Net_SkipCommand (int type, BYTE **stream) CCMD (pings) { int i; - + Printf("%d (%d ms) arbitrator buffer time\n", arb_maketic * ticdup, (arb_maketic * ticdup) * (1000 / TICRATE)); for (i = 0; i < MAXPLAYERS; i++) if (playeringame[i]) Printf ("% 4d %s\n", currrecvtime[i] - lastrecvtime[i], diff --git a/src/d_net.h b/src/d_net.h index 4cd3e66f5..8d352b25d 100644 --- a/src/d_net.h +++ b/src/d_net.h @@ -143,6 +143,7 @@ extern struct ticcmd_t localcmds[LOCALCMDTICS]; extern int maketic; extern int nettics[MAXNETNODES]; +extern int arb_maketic; extern ticcmd_t netcmds[MAXPLAYERS][BACKUPTICS]; extern int ticdup; diff --git a/src/g_shared/shared_hud.cpp b/src/g_shared/shared_hud.cpp index 2daeff7a8..67a6bd82c 100644 --- a/src/g_shared/shared_hud.cpp +++ b/src/g_shared/shared_hud.cpp @@ -37,6 +37,7 @@ // copy would be. #include "doomtype.h" +#include "doomdef.h" #include "v_video.h" #include "gi.h" #include "c_cvars.h" @@ -48,6 +49,7 @@ #include "p_local.h" #include "doomstat.h" #include "g_level.h" +#include "d_net.h" #include @@ -73,6 +75,7 @@ CVAR (Bool, hud_showscore, false, CVAR_ARCHIVE); // for user maintained score CVAR (Bool, hud_showweapons, true, CVAR_ARCHIVE); // Show weapons collected CVAR (Int , hud_showtime, 0, CVAR_ARCHIVE); // Show time on HUD CVAR (Int , hud_timecolor, CR_GOLD,CVAR_ARCHIVE); // Color of in-game time on HUD +CVAR (Int , hud_showlag, 0, CVAR_ARCHIVE); // Show input latency (maketic - gametic difference) CVAR (Int, hud_ammo_red, 25, CVAR_ARCHIVE) // ammo percent less than which status is red CVAR (Int, hud_ammo_yellow, 50, CVAR_ARCHIVE) // ammo percent less is yellow more green @@ -917,6 +920,49 @@ static void DrawTime() DrawHudText(SmallFont, hud_timecolor, timeString, hudwidth - width, height, FRACUNIT); } +//--------------------------------------------------------------------------- +// +// Draw in-game latency +// +//--------------------------------------------------------------------------- + +static void DrawLatency() +{ + if (hud_showlag <= 0 || + (hud_showlag == 1 && !netgame) || + hud_showlag > 2) + { + return; + } + + int localdelay = (maketic - gametic) * (1000 / TICRATE); + int arbitratordelay = (arb_maketic * ticdup) * (1000 / TICRATE); + int color = CR_GREEN; + if (MAX(localdelay, arbitratordelay) > 200) + { + color = CR_YELLOW; + } + if (MAX(localdelay, arbitratordelay) > 400) + { + color = CR_ORANGE; + } + if (MAX(localdelay, arbitratordelay) >= ((BACKUPTICS / 2 - 1) * ticdup) * (1000 / TICRATE)) + { + color = CR_RED; + } + + char tempstr[32]; + + const int millis = (level.time % TICRATE) * (1000 / TICRATE); + mysnprintf(tempstr, sizeof(tempstr), "a:%dms - l:%dms", arbitratordelay, localdelay); + + const int characterCount = strlen(tempstr); + const int width = SmallFont->GetCharWidth('0') * characterCount + 2; // small offset from screen's border + const int height = SmallFont->GetHeight() * 2; + + DrawHudText(SmallFont, color, tempstr, hudwidth - width, height, FRACUNIT); +} + //--------------------------------------------------------------------------- // @@ -982,6 +1028,7 @@ void DrawHUD() if (idmypos) DrawCoordinates(CPlayer); DrawTime(); + DrawLatency(); } else { diff --git a/wadsrc/static/menudef.txt b/wadsrc/static/menudef.txt index 324c94e6b..66d7537f6 100644 --- a/wadsrc/static/menudef.txt +++ b/wadsrc/static/menudef.txt @@ -805,6 +805,13 @@ OptionValue "AltHUDTime" 9, "System" } +OptionValue "AltHUDLag" +{ + 0, "Off" + 1, "Netgames only" + 2, "Always" +} + OptionMenu "AltHUDOptions" { Title "Alternative HUD" @@ -819,6 +826,7 @@ OptionMenu "AltHUDOptions" Option "Show weapons", "hud_showweapons", "OnOff" Option "Show time", "hud_showtime", "AltHUDTime" Option "Time color", "hud_timecolor", "TextColors" + Option "Show network latency", "hud_showlag", "AltHUDLag" Slider "Red ammo display below %", "hud_ammo_red", 0, 100, 1, 0 Slider "Yellow ammo display below %", "hud_ammo_yellow", 0, 100, 1, 0 Slider "Red health display below", "hud_health_red", 0, 100, 1, 0