Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 10 additions & 9 deletions Source/Client/AsyncTime/AsyncTimeComp.cs
Original file line number Diff line number Diff line change
@@ -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
{
Expand Down Expand Up @@ -99,7 +98,7 @@ public int GameStartAbsTick

public Queue<ScheduledCommand> 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)
Expand Down Expand Up @@ -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()
{
Expand Down
20 changes: 15 additions & 5 deletions Source/Client/AsyncTime/AsyncWorldTimeComp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -58,6 +59,9 @@ public void SetDesiredTimeSpeed(TimeSpeed speed)
public Queue<ScheduledCommand> Cmds => cmds;
public Queue<ScheduledCommand> cmds = new();

public int CurrentPlayerCount { get; private set; }
public int VTR => CurrentPlayerCount > 0 ? VTRSync.MinimumVtr : VTRSync.MaximumVtr;

public int TickableId => -1;

public World world;
Expand Down Expand Up @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions Source/Client/Patches/VTRSyncPatch.cs
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -46,15 +46,15 @@ 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;
}
}

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;
Expand Down
28 changes: 17 additions & 11 deletions Source/Client/UI/DebugPanel/StatusBadge.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using Multiplayer.Client.Patches;
using RimWorld.Planet;
using UnityEngine;
using Verse;

Expand Down Expand Up @@ -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);
}

Expand All @@ -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");
}

}
}
}
Loading