diff --git a/Source/Client/AsyncTime/AsyncTimeComp.cs b/Source/Client/AsyncTime/AsyncTimeComp.cs index 866609c3..e54f954b 100644 --- a/Source/Client/AsyncTime/AsyncTimeComp.cs +++ b/Source/Client/AsyncTime/AsyncTimeComp.cs @@ -1,16 +1,15 @@ -using HarmonyLib; -using Multiplayer.Common; -using RimWorld; -using RimWorld.Planet; using System; using System.Collections.Generic; -using Verse; +using HarmonyLib; using Multiplayer.Client.Comp; using Multiplayer.Client.Factions; using Multiplayer.Client.Patches; using Multiplayer.Client.Saving; using Multiplayer.Client.Util; -using System.Linq; +using Multiplayer.Common; +using RimWorld; +using RimWorld.Planet; +using Verse; namespace Multiplayer.Client { @@ -99,7 +98,7 @@ public int GameStartAbsTick public Queue cmds = new(); - public int CurrentPlayerCount { get; private set; } = 0; + public int CurrentPlayerCount { get; private set; } public int VTR => CurrentPlayerCount > 0 ? VTRSync.MinimumVtr : VTRSync.MaximumVtr; public AsyncTimeComp(Map map, int gameStartAbsTick = 0) @@ -232,8 +231,10 @@ public void ExposeData() Scribe_Custom.LookULong(ref randState, "randState", 1); } - public void IncreasePlayerCount() => CurrentPlayerCount++; - public void DecreasePlayerCount() => CurrentPlayerCount = Math.Max(0, CurrentPlayerCount - 1); + public int IncreasePlayerCount() => CurrentPlayerCount += 1; + // This should never go below 0, this is just defensive programming. Hopefully not needed anymore, but + // nevertheless still left. + public int DecreasePlayerCount() => CurrentPlayerCount = Math.Max(0, CurrentPlayerCount - 1); public void FinalizeInit() { diff --git a/Source/Client/AsyncTime/AsyncWorldTimeComp.cs b/Source/Client/AsyncTime/AsyncWorldTimeComp.cs index 4be6aaa2..603ff372 100644 --- a/Source/Client/AsyncTime/AsyncWorldTimeComp.cs +++ b/Source/Client/AsyncTime/AsyncWorldTimeComp.cs @@ -5,6 +5,7 @@ using Multiplayer.Client.Comp; using Multiplayer.Client.Desyncs; using Multiplayer.Client.Factions; +using Multiplayer.Client.Patches; using Multiplayer.Client.Saving; using Multiplayer.Client.Util; using Multiplayer.Common; @@ -58,6 +59,9 @@ public void SetDesiredTimeSpeed(TimeSpeed speed) public Queue Cmds => cmds; public Queue cmds = new(); + public int CurrentPlayerCount { get; private set; } + public int VTR => CurrentPlayerCount > 0 ? VTRSync.MinimumVtr : VTRSync.MaximumVtr; + public int TickableId => -1; public World world; @@ -229,13 +233,19 @@ public void ExecuteCmd(ScheduledCommand cmd) int newMapId = data.ReadInt32(); int mapCount = Find.Maps.Count; - MpLog.Debug($"[{worldTicks}|{Multiplayer.session.remoteTickUntil}] Player count change: previousMapId={previousMapId}, newMapId={newMapId}, mapCount={mapCount}"); + var prev = -1; + if (previousMapId >= 0) + prev = Find.Maps.FirstOrDefault(x => x.uniqueID == previousMapId)?.AsyncTime()?.DecreasePlayerCount() ?? -1; + else if (previousMapId == VTRSync.WorldMapId) + prev = Multiplayer.AsyncWorldTime.CurrentPlayerCount -= 1; - if (0 <= previousMapId) - Find.Maps.FirstOrDefault(x => x.uniqueID == previousMapId)?.AsyncTime()?.DecreasePlayerCount(); + var curr = -1; + if (newMapId >= 0) + curr = Find.Maps.FirstOrDefault(x => x.uniqueID == newMapId)?.AsyncTime()?.IncreasePlayerCount() ?? -1; + else if (newMapId == VTRSync.WorldMapId) + curr = Multiplayer.AsyncWorldTime.CurrentPlayerCount += 1; - if (0 <= newMapId) - Find.Maps.FirstOrDefault(x => x.uniqueID == newMapId)?.AsyncTime()?.IncreasePlayerCount(); + MpLog.Debug($"[{worldTicks}|{Multiplayer.session.remoteTickUntil}] Player count change: previousMapId={previousMapId} ({prev}), newMapId={newMapId} ({curr}), mapCount={mapCount}"); } } catch (Exception e) diff --git a/Source/Client/Patches/VTRSyncPatch.cs b/Source/Client/Patches/VTRSyncPatch.cs index d07b99cb..4bb37b7b 100644 --- a/Source/Client/Patches/VTRSyncPatch.cs +++ b/Source/Client/Patches/VTRSyncPatch.cs @@ -1,8 +1,8 @@ +using System; using HarmonyLib; using Multiplayer.Client.Util; using Multiplayer.Common; using RimWorld.Planet; -using System; using Verse; namespace Multiplayer.Client.Patches @@ -46,7 +46,7 @@ static bool Prefix(ref int __result, WorldObject __instance) if (__instance is Gravship or TravellingTransporters) __result = VTRSync.MinimumVtr; else - __result = VTRSync.MaximumVtr; + __result = Multiplayer.AsyncWorldTime.VTR; return false; } @@ -54,7 +54,7 @@ static bool Prefix(ref int __result, WorldObject __instance) static class VTRSync { - // Special identifier for world map (since it doesn't have a uniqueID like regular maps) + // Special identifier for the world map (since it doesn't have a uniqueID like regular maps) public const int WorldMapId = -2; public const int InvalidMapId = -1; public static int lastMovedToMapId = InvalidMapId; diff --git a/Source/Client/UI/DebugPanel/StatusBadge.cs b/Source/Client/UI/DebugPanel/StatusBadge.cs index 66ca4325..f1623ce8 100644 --- a/Source/Client/UI/DebugPanel/StatusBadge.cs +++ b/Source/Client/UI/DebugPanel/StatusBadge.cs @@ -1,3 +1,5 @@ +using Multiplayer.Client.Patches; +using RimWorld.Planet; using UnityEngine; using Verse; @@ -32,20 +34,20 @@ public static StatusBadge GetSyncStatus() public static StatusBadge GetPerformanceStatus() { float tps = IngameUIPatch.tps; - + if (PerformanceCalculator.IsInStabilizationPeriod()) { return new StatusBadge("▲", Color.yellow, "STAB", "Stabilizing after speed change"); } - + float normalizedTps = PerformanceCalculator.GetNormalizedTPS(tps); - - string tooltip = normalizedTps >= 90f ? "Performance is excellent" : - normalizedTps >= 70f ? "Performance is good" : - normalizedTps >= 50f ? "Performance is moderate" : - normalizedTps >= 25f ? "Performance is poor" : + + string tooltip = normalizedTps >= 90f ? "Performance is excellent" : + normalizedTps >= 70f ? "Performance is good" : + normalizedTps >= 50f ? "Performance is moderate" : + normalizedTps >= 25f ? "Performance is poor" : "Performance is very poor"; - + return new StatusBadge("▲", PerformanceCalculator.GetPerformanceColor(normalizedTps, 90f, 70f), $"{normalizedTps:F0}%", tooltip); } @@ -60,15 +62,19 @@ public static StatusBadge GetTickStatus() public static StatusBadge GetVtrStatus() { - int rate = Find.CurrentMap?.AsyncTime()?.VTR ?? Patches.VTRSync.MaximumVtr; + int rate = (WorldRendererUtility.WorldSelected + ? Multiplayer.AsyncWorldTime?.VTR + : Find.CurrentMap.AsyncTime()?.VTR) ?? VTRSync.MaximumVtr; return new StatusBadge("V", rate == 15 ? Color.red : Color.green, rate.ToString(), $"Variable Tick Rate: Things update every {rate} tick(s)"); } public static StatusBadge GetNumOfPlayersStatus() { - int playerCount = Find.CurrentMap?.AsyncTime()?.CurrentPlayerCount ?? 0; + int playerCount = (WorldRendererUtility.WorldSelected + ? Multiplayer.AsyncWorldTime?.CurrentPlayerCount + : Find.CurrentMap.AsyncTime()?.CurrentPlayerCount) ?? 0; return new StatusBadge("P", playerCount > 0 ? Color.green : Color.red, $"{playerCount}", "Active players in the current map"); } } -} +}