Maelstrom: More fine tuning of the ping algorithm, commented out debug info for now.

https://github.com/libsdl-org/Maelstrom/commit/0534a25e1b354d33f413a00071f6f36cb76d3467

From 0534a25e1b354d33f413a00071f6f36cb76d3467 Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Tue, 8 Nov 2011 02:58:14 -0500
Subject: [PATCH] More fine tuning of the ping algorithm, commented out debug
 info for now.

---
 netlogic/gameinfo.cpp | 38 +++++++++++++++++++++++++++-----------
 netlogic/netplay.cpp  |  8 ++++++++
 2 files changed, 35 insertions(+), 11 deletions(-)

diff --git a/netlogic/gameinfo.cpp b/netlogic/gameinfo.cpp
index d718a863..b7165ded 100644
--- a/netlogic/gameinfo.cpp
+++ b/netlogic/gameinfo.cpp
@@ -20,6 +20,7 @@
     slouken@libsdl.org
 */
 
+#include "../Maelstrom_Globals.h"
 #include "../screenlib/UIElement.h"
 #include "../screenlib/UIElementCheckbox.h"
 #include "../screenlib/UIElementRadio.h"
@@ -239,11 +240,11 @@ GameInfo::UpdatePingTime(int index, Uint32 timestamp)
 
 	player = GetPlayer(index);
 	player->ping.lastPing = now;
-	if (player->ping.roundTripTime) {
-		// Use a weighted average 1/4 previous value, 3/4 new value
-		player->ping.roundTripTime = (player->ping.roundTripTime + 3*elapsed) / 4;
-	} else {
+	if (!player->ping.roundTripTime) {
 		player->ping.roundTripTime = elapsed;
+	} else {
+		// Use a weighted average 2/3 previous value, 1/3 new value
+		player->ping.roundTripTime = (2*player->ping.roundTripTime + 1*elapsed) / 3;
 	}
 }
 
@@ -267,21 +268,36 @@ GameInfo::UpdatePingStatus(int index)
 
 		sinceLastPing = int(SDL_GetTicks() - player->ping.lastPing);
 		if (sinceLastPing < 2*PING_INTERVAL) {
-			if (player->ping.roundTripTime <= 48) {
-printf("Game 0x%8.8x: player 0x%8.8x round trip time %d (GOOD)\n", gameID, player->playerID, player->ping.roundTripTime);
+			if (player->ping.roundTripTime <= 2*FRAME_DELAY_MS) {
+#ifdef DEBUG_NETWORK
+printf("Game 0x%8.8x: player 0x%8.8x round trip time %d (GOOD)\n",
+	gameID, player->playerID, player->ping.roundTripTime);
+#endif
 				player->ping.status = PING_GOOD;
-			} else if (player->ping.roundTripTime <= 64) {
-printf("Game 0x%8.8x: player 0x%8.8x round trip time %d (OKAY)\n", gameID, player->playerID, player->ping.roundTripTime);
+			} else if (player->ping.roundTripTime <= 3*FRAME_DELAY_MS) {
+#ifdef DEBUG_NETWORK
+printf("Game 0x%8.8x: player 0x%8.8x round trip time %d (OKAY)\n",
+	gameID, player->playerID, player->ping.roundTripTime);
+#endif
 				player->ping.status = PING_OKAY;
 			} else {
-printf("Game 0x%8.8x: player 0x%8.8x round trip time %d (BAD)\n", gameID, player->playerID, player->ping.roundTripTime);
+#ifdef DEBUG_NETWORK
+printf("Game 0x%8.8x: player 0x%8.8x round trip time %d (BAD)\n",
+	gameID, player->playerID, player->ping.roundTripTime);
+#endif
 				player->ping.status = PING_BAD;
 			}
 		} else if (sinceLastPing < PING_TIMEOUT) {
-printf("Game 0x%8.8x: player 0x%8.8x since last ping %d (BAD)\n", gameID, player->playerID, sinceLastPing);
+#ifdef DEBUG_NETWORK
+printf("Game 0x%8.8x: player 0x%8.8x since last ping %d (BAD)\n",
+	gameID, player->playerID, sinceLastPing);
+#endif
 			player->ping.status = PING_BAD;
 		} else {
-printf("Game 0x%8.8x: player 0x%8.8x since last ping %d (TIMEDOUT)\n", gameID, player->playerID, sinceLastPing);
+#ifdef DEBUG_NETWORK
+printf("Game 0x%8.8x: player 0x%8.8x since last ping %d (TIMEDOUT)\n",
+	gameID, player->playerID, sinceLastPing);
+#endif
 			player->ping.status = PING_TIMEDOUT;
 		}
 	}
diff --git a/netlogic/netplay.cpp b/netlogic/netplay.cpp
index e4b79a50..b9db5f8c 100644
--- a/netlogic/netplay.cpp
+++ b/netlogic/netplay.cpp
@@ -329,15 +329,21 @@ int SyncNetwork(void)
 
 		/* Check the frame number */
 		frame = SDLNet_Read32(&buf[1]);
+#ifdef DEBUG_NETWORK
 //error("Received a packet of frame %lu from player %d\r\n", frame, index+1);
+#endif
 		if ( frame != NextFrame ) {
 			/* We kept the last frame cached, so send it */
 			if ( frame == (NextFrame-1) ) {
+#ifdef DEBUG_NETWORK
 error("Transmitting packet for old frame (%lu)\r\n", frame);
+#endif
 				SDLNet_UDP_Send(gNetFD, sent.channel, OutBound[!CurrOut]);
 			} else if ( frame == (NextFrame+1) ) {
+#ifdef DEBUG_NETWORK
 error("Received packet for next frame! (%lu, current = %lu)\r\n",
 						frame, NextFrame);
+#endif
 				/* Send this player our current frame */
 				SDLNet_UDP_Send(gNetFD, sent.channel, OutBound[CurrOut]);
 				/* Cache this frame for next round,
@@ -347,9 +353,11 @@ error("Received packet for next frame! (%lu, current = %lu)\r\n",
 				NextLen[index] = sent.len-PDATA_OFFSET;
 				++NextSync;
 			}
+#ifdef DEBUG_NETWORK
 else
 error("Warning! Received packet for really old frame! (%lu, current = %lu)\r\n",
 							frame, NextFrame);
+#endif
 			/* Go to select, reset timeout */
 			continue;
 		}