From 36613d58e9177f868a400a15d53f4600e8d6d36e Mon Sep 17 00:00:00 2001
From: LJ Sonic <lamr@free.fr>
Date: Thu, 29 Dec 2022 22:30:26 +0100
Subject: [PATCH] Cleanup sphere redistribution

---
 src/d_clisrv.c | 55 ++++++++++++++++----------------------------------
 1 file changed, 17 insertions(+), 38 deletions(-)

diff --git a/src/d_clisrv.c b/src/d_clisrv.c
index 432001b80a..1b344a0de9 100755
--- a/src/d_clisrv.c
+++ b/src/d_clisrv.c
@@ -2321,52 +2321,31 @@ static void UnlinkPlayerFromNode(INT32 playernum)
 // I feel like this shouldn't even be in this file at all, but well.
 static void RedistributeSpecialStageSpheres(INT32 playernum)
 {
-	INT32 i, count, sincrement, spheres, rincrement, rings;
-
-	if (!G_IsSpecialStage(gamemap))
+	if (!G_IsSpecialStage(gamemap) || D_NumPlayers() <= 1)
 		return;
 
-	for (i = 0, count = 0; i < MAXPLAYERS; i++)
-	{
-		if (playeringame[i])
-			count++;
-	}
-
-	count--;
-	spheres = players[playernum].spheres;
-	rings = players[playernum].rings;
+	INT32 count = D_NumPlayers() - 1;
+	INT32 spheres = players[playernum].spheres;
+	INT32 rings = players[playernum].rings;
 
-	while (count && (spheres || rings))
+	while (spheres || rings)
 	{
-		sincrement = max(spheres / count, 1);
-		rincrement = max(rings / count, 1);
+		INT32 sincrement = max(spheres / count, 1);
+		INT32 rincrement = max(rings / count, 1);
 
+		INT32 i, n;
 		for (i = 0; i < MAXPLAYERS; i++)
 		{
-			if (playeringame[i] && i != playernum)
-			{
-				if (spheres < sincrement)
-				{
-					P_GivePlayerSpheres(&players[i], spheres);
-					spheres = 0;
-				}
-				else
-				{
-					P_GivePlayerSpheres(&players[i], sincrement);
-					spheres -= sincrement;
-				}
+			if (!playeringame[i] || i == playernum)
+				continue;
 
-				if (rings < rincrement)
-				{
-					P_GivePlayerRings(&players[i], rings);
-					rings = 0;
-				}
-				else
-				{
-					P_GivePlayerRings(&players[i], rincrement);
-					rings -= rincrement;
-				}
-			}
+			n = min(spheres, sincrement);
+			P_GivePlayerSpheres(&players[i], n);
+			spheres -= n;
+
+			n = min(rings, rincrement);
+			P_GivePlayerRings(&players[i], n);
+			rings -= n;
 		}
 	}
 }
-- 
GitLab