From 98acd92bfbc2ec3cca458099f6842daa776bbd92 Mon Sep 17 00:00:00 2001 From: Sokyran Date: Sat, 4 Feb 2023 19:51:47 +0100 Subject: [PATCH 1/3] Removed some code repetition, simplified pause/forced slowdown check --- Source/Client/AsyncTime/AsyncTimeComp.cs | 32 ++++++++++------- Source/Client/AsyncTime/ITickable.cs | 4 +++ Source/Client/AsyncTime/TimeControlUI.cs | 15 ++++---- Source/Client/Comp/MultiplayerWorldComp.cs | 17 ++++----- Source/Client/Patches/TickPatch.cs | 40 ++++++++++++++++++++-- 5 files changed, 78 insertions(+), 30 deletions(-) diff --git a/Source/Client/AsyncTime/AsyncTimeComp.cs b/Source/Client/AsyncTime/AsyncTimeComp.cs index 0262fb75..3d1f3df1 100644 --- a/Source/Client/AsyncTime/AsyncTimeComp.cs +++ b/Source/Client/AsyncTime/AsyncTimeComp.cs @@ -24,20 +24,10 @@ public class AsyncTimeComp : IExposable, ITickable public float TickRateMultiplier(TimeSpeed speed) { - var comp = map.MpComp(); - - var enforcePause = comp.transporterLoading != null || - comp.caravanForming != null || - comp.ritualSession != null || - comp.mapDialogs.Any() || - Multiplayer.WorldComp.AnyTradeSessionsOnMap(map) || - Multiplayer.WorldComp.splitSession != null || - pauseLocks.Any(x => x(map)); - - if (enforcePause) + if (IsPaused) return 0f; - if (mapTicks < slower.forceNormalSpeedUntil) + if (IsForceSlowdown) return speed == TimeSpeed.Paused ? 0 : 1; switch (speed) @@ -65,6 +55,24 @@ public TimeSpeed TimeSpeed set => timeSpeedInt = value; } + public bool IsPaused + { + get + { + var comp = map.MpComp(); + + return comp.transporterLoading != null || + comp.caravanForming != null || + comp.ritualSession != null || + comp.mapDialogs.Any() || + Multiplayer.WorldComp.AnyTradeSessionsOnMap(map) || + Multiplayer.WorldComp.splitSession != null || + pauseLocks.Any(x => x(map)); + } + } + + public bool IsForceSlowdown => mapTicks < slower.forceNormalSpeedUntil; + public bool Paused => this.ActualRateMultiplier(TimeSpeed) == 0f; public float RealTimeToTickThrough { get; set; } diff --git a/Source/Client/AsyncTime/ITickable.cs b/Source/Client/AsyncTime/ITickable.cs index 7ecb78d3..81aca896 100644 --- a/Source/Client/AsyncTime/ITickable.cs +++ b/Source/Client/AsyncTime/ITickable.cs @@ -20,5 +20,9 @@ public interface ITickable void Tick(); void ExecuteCmd(ScheduledCommand cmd); + + bool IsPaused { get; } + + bool IsForceSlowdown { get; } } } diff --git a/Source/Client/AsyncTime/TimeControlUI.cs b/Source/Client/AsyncTime/TimeControlUI.cs index b84834d8..fcd3d46f 100644 --- a/Source/Client/AsyncTime/TimeControlUI.cs +++ b/Source/Client/AsyncTime/TimeControlUI.cs @@ -106,12 +106,11 @@ private static void DoTimeControlsGUI(Rect timerRect) rect.x += rect.width; } - float normalSpeed = Tickable.ActualRateMultiplier(TimeSpeed.Normal); - float fastSpeed = Tickable.ActualRateMultiplier(TimeSpeed.Fast); + TimePauseSlowdownInfo info = Tickable.IsPausedOrSlowedDown(); - if (normalSpeed == 0f) // Completely paused + if (info == TimePauseSlowdownInfo.Paused) // Completely paused Widgets.DrawLineHorizontal(rect.width, rect.height / 2f, rect.width * 3f); - else if (normalSpeed == fastSpeed) // Slowed down + else if (info == TimePauseSlowdownInfo.Slowdown) // Slowed down Widgets.DrawLineHorizontal(rect.width * 2f, rect.height / 2f, rect.width * 2f); Widgets.EndGroup(); @@ -305,12 +304,12 @@ static void DrawButtons() { if (curGroup == entry.group) continue; - ITickable entryTickable = entry.map?.AsyncTime(); - if (entryTickable == null) entryTickable = Multiplayer.WorldComp; + ITickable entryTickable = entry.map?.AsyncTime() ?? (ITickable)Multiplayer.WorldComp; Rect groupBar = bar.drawer.GroupFrameRect(entry.group); float drawXPos = groupBar.x; - Color bgColor = (entryTickable.ActualRateMultiplier(TimeSpeed.Normal) == 0f) ? pauseBgColor : normalBgColor; + bool paused = entryTickable.IsPaused; + Color bgColor = paused ? pauseBgColor : normalBgColor; if (Multiplayer.GameComp.asyncTime) { @@ -321,7 +320,7 @@ static void DrawButtons() MpTimeControls.TimeControlButton(button, bgColor, entryTickable); drawXPos += TimeControls.TimeButSize.x; } - else if (entryTickable.ActualRateMultiplier(TimeSpeed.Normal) == 0f) + else if (paused) { MpTimeControls.TimeIndicateBlockingPause(button, bgColor); drawXPos += TimeControls.TimeButSize.x; diff --git a/Source/Client/Comp/MultiplayerWorldComp.cs b/Source/Client/Comp/MultiplayerWorldComp.cs index 29b965bd..5209452c 100644 --- a/Source/Client/Comp/MultiplayerWorldComp.cs +++ b/Source/Client/Comp/MultiplayerWorldComp.cs @@ -27,14 +27,8 @@ public class MultiplayerWorldComp : IExposable, ITickable public float TickRateMultiplier(TimeSpeed speed) { - if (Multiplayer.GameComp.asyncTime) - { - var enforcePause = Multiplayer.WorldComp.splitSession != null || - AsyncTimeComp.pauseLocks.Any(x => x(null)); - - if (enforcePause) - return 0f; - } + if (IsPaused) + return 0f; return speed switch { @@ -56,6 +50,13 @@ public TimeSpeed TimeSpeed } } + public bool IsPaused + => Multiplayer.GameComp.asyncTime && + (Multiplayer.WorldComp.splitSession != null || + AsyncTimeComp.pauseLocks.Any(x => x(null))); + + public bool IsForceSlowdown => false; + /** * Clamps the World's TimeSpeed to be between (slowest map) and (fastest map) * Caution: doesn't work if called inside a MapAsyncTime.PreContext() diff --git a/Source/Client/Patches/TickPatch.cs b/Source/Client/Patches/TickPatch.cs index 810f026c..93b614c8 100644 --- a/Source/Client/Patches/TickPatch.cs +++ b/Source/Client/Patches/TickPatch.cs @@ -227,9 +227,10 @@ private static float ReplayMultiplier() public static float TimePerTick(this ITickable tickable, TimeSpeed speed) { - if (tickable.ActualRateMultiplier(speed) == 0f) + var mult = tickable.ActualRateMultiplier(speed); + if (mult == 0f) return 0f; - return 1f / tickable.ActualRateMultiplier(speed); + return 1f / mult; } public static float ActualRateMultiplier(this ITickable tickable, TimeSpeed speed) @@ -244,6 +245,39 @@ public static float ActualRateMultiplier(this ITickable tickable, TimeSpeed spee return rate; } + public static TimePauseSlowdownInfo IsPausedOrSlowedDown(this ITickable tickable) + { + if (Multiplayer.GameComp.asyncTime) + { + if (tickable.IsPaused) + return TimePauseSlowdownInfo.Paused; + if (tickable.IsForceSlowdown) + return TimePauseSlowdownInfo.Slowdown; + return TimePauseSlowdownInfo.Normal; + } + + if (Multiplayer.WorldComp.IsPaused) + return TimePauseSlowdownInfo.Paused; + + // Could just use false, as it currently can not be force slowed down. + // If we keep this, some mods may potentially use force slowdown on world. + var isForceSlowdown = Multiplayer.WorldComp.IsForceSlowdown; + + foreach (var map in Find.Maps) + { + var comp = map.AsyncTime(); + + if (comp.IsPaused) + return TimePauseSlowdownInfo.Paused; + if (!isForceSlowdown) + isForceSlowdown = comp.IsForceSlowdown; + } + + if (isForceSlowdown) + return TimePauseSlowdownInfo.Slowdown; + return TimePauseSlowdownInfo.Normal; + } + public static void ClearSimulating() { simulating = null; @@ -284,4 +318,6 @@ public class SimulatingData public string cancelButtonKey; public string simTextKey; } + + public enum TimePauseSlowdownInfo { Normal, Paused, Slowdown, } } From 322cacb35d4a02ff0bc359406498b8d4fd18b5b2 Mon Sep 17 00:00:00 2001 From: SokyranTheDragon Date: Mon, 26 Aug 2024 20:47:59 +0200 Subject: [PATCH 2/3] Update for AsyncWorldTimeComp, some renames Some changes that originally were made for `MultiplayerWorldComp` were properly moved to `AsyncWorldTimeComp`. Some names were changed to be clearer to what it does or is. `IsPaused` property was renamed to `IsForcePaused`. `TimePauseSlowdownInfo` enum was renamed to `ForcedTickRate`. --- Source/Client/AsyncTime/AsyncWorldTimeComp.cs | 16 ++++++---- Source/Client/AsyncTime/ITickable.cs | 2 +- Source/Client/AsyncTime/TimeControlUI.cs | 8 ++--- Source/Client/Patches/TickPatch.cs | 30 +++++++++---------- 4 files changed, 30 insertions(+), 26 deletions(-) diff --git a/Source/Client/AsyncTime/AsyncWorldTimeComp.cs b/Source/Client/AsyncTime/AsyncWorldTimeComp.cs index 6859309e..b86de425 100644 --- a/Source/Client/AsyncTime/AsyncWorldTimeComp.cs +++ b/Source/Client/AsyncTime/AsyncWorldTimeComp.cs @@ -25,13 +25,12 @@ public class AsyncWorldTimeComp : IExposable, ITickable public float TickRateMultiplier(TimeSpeed speed) { - if (Multiplayer.GameComp.asyncTime) - { - var enforcePause = Multiplayer.WorldComp.sessionManager.IsAnySessionCurrentlyPausing(null); + if (IsForcePaused) + return 0f; - if (enforcePause) - return 0f; - } + // Could just skip, as it currently can not be true unless a mod makes a Harmony patch. + if (IsForceSlowdown) + return speed == TimeSpeed.Paused ? 0 : 1; return speed switch { @@ -44,6 +43,11 @@ public float TickRateMultiplier(TimeSpeed speed) }; } + public bool IsForcePaused => Multiplayer.GameComp.asyncTime && + Multiplayer.WorldComp.sessionManager.IsAnySessionCurrentlyPausing(null); + + public bool IsForceSlowdown => false; + // Run at the speed of the fastest map or at chosen speed if there are no maps public TimeSpeed DesiredTimeSpeed => !Find.Maps.Any() ? timeSpeedInt : diff --git a/Source/Client/AsyncTime/ITickable.cs b/Source/Client/AsyncTime/ITickable.cs index b2b5adbc..b83a2338 100644 --- a/Source/Client/AsyncTime/ITickable.cs +++ b/Source/Client/AsyncTime/ITickable.cs @@ -22,7 +22,7 @@ public interface ITickable void ExecuteCmd(ScheduledCommand cmd); - bool IsPaused { get; } + bool IsForcePaused { get; } bool IsForceSlowdown { get; } } diff --git a/Source/Client/AsyncTime/TimeControlUI.cs b/Source/Client/AsyncTime/TimeControlUI.cs index a1549a98..1ef8de3e 100644 --- a/Source/Client/AsyncTime/TimeControlUI.cs +++ b/Source/Client/AsyncTime/TimeControlUI.cs @@ -1,4 +1,4 @@ -using System.Collections.Generic; +using System.Collections.Generic; using System.Linq; using System.Reflection.Emit; using HarmonyLib; @@ -106,11 +106,11 @@ private static void DoTimeControlsGUI(Rect timerRect) rect.x += rect.width; } - ForcedTickRateInfo info = Tickable.GetForcedSpeedInfo(); + ForcedTickRate info = Tickable.GetForcedSpeedInfo(); - if (info == ForcedTickRateInfo.Paused) // Completely paused + if (info == ForcedTickRate.Paused) // Completely paused Widgets.DrawLineHorizontal(rect.width, rect.height / 2f, rect.width * 3f); - else if (info == ForcedTickRateInfo.Slowdown) // Slowed down + else if (info == ForcedTickRate.Slowdown) // Slowed down Widgets.DrawLineHorizontal(rect.width * 2f, rect.height / 2f, rect.width * 2f); Widgets.EndGroup(); diff --git a/Source/Client/Patches/TickPatch.cs b/Source/Client/Patches/TickPatch.cs index c134849f..f05811b1 100644 --- a/Source/Client/Patches/TickPatch.cs +++ b/Source/Client/Patches/TickPatch.cs @@ -288,37 +288,37 @@ public static float ActualRateMultiplier(this ITickable tickable, TimeSpeed spee return rate; } - public static TimePauseSlowdownInfo IsPausedOrSlowedDown(this ITickable tickable) + public static ForcedTickRate GetForcedSpeedInfo(this ITickable tickable) { if (Multiplayer.GameComp.asyncTime) { - if (tickable.IsPaused) - return TimePauseSlowdownInfo.Paused; + if (tickable.IsForcePaused) + return ForcedTickRate.Paused; if (tickable.IsForceSlowdown) - return TimePauseSlowdownInfo.Slowdown; - return TimePauseSlowdownInfo.Normal; + return ForcedTickRate.Slowdown; + return ForcedTickRate.Normal; } - if (Multiplayer.WorldComp.IsPaused) - return TimePauseSlowdownInfo.Paused; + if (Multiplayer.AsyncWorldTime.IsForcePaused) + return ForcedTickRate.Paused; - // Could just use false, as it currently can not be force slowed down. - // If we keep this, some mods may potentially use force slowdown on world. - var isForceSlowdown = Multiplayer.WorldComp.IsForceSlowdown; + // Could just use false as default, as it currently can + // not be true unless a mod makes a Harmony patch. + var isForceSlowdown = Multiplayer.AsyncWorldTime.IsForceSlowdown; foreach (var map in Find.Maps) { var comp = map.AsyncTime(); - if (comp.IsPaused) - return TimePauseSlowdownInfo.Paused; + if (comp.IsForcePaused) + return ForcedTickRate.Paused; if (!isForceSlowdown) isForceSlowdown = comp.IsForceSlowdown; } if (isForceSlowdown) - return TimePauseSlowdownInfo.Slowdown; - return TimePauseSlowdownInfo.Normal; + return ForcedTickRate.Slowdown; + return ForcedTickRate.Normal; } public static void ClearSimulating() @@ -362,5 +362,5 @@ public class SimulatingData public string simTextKey; } - public enum TimePauseSlowdownInfo { Normal, Paused, Slowdown, } + public enum ForcedTickRate { Normal, Paused, Slowdown, } } From 2483e2bdda53d17cd75f302dbb2efc2f61a5de12 Mon Sep 17 00:00:00 2001 From: SokyranTheDragon Date: Mon, 26 Aug 2024 20:50:29 +0200 Subject: [PATCH 3/3] Another slight rename --- Source/Client/AsyncTime/TimeControlUI.cs | 2 +- Source/Client/Patches/TickPatch.cs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Source/Client/AsyncTime/TimeControlUI.cs b/Source/Client/AsyncTime/TimeControlUI.cs index 1ef8de3e..6d37b320 100644 --- a/Source/Client/AsyncTime/TimeControlUI.cs +++ b/Source/Client/AsyncTime/TimeControlUI.cs @@ -106,7 +106,7 @@ private static void DoTimeControlsGUI(Rect timerRect) rect.x += rect.width; } - ForcedTickRate info = Tickable.GetForcedSpeedInfo(); + ForcedTickRate info = Tickable.GetForcedTickRate(); if (info == ForcedTickRate.Paused) // Completely paused Widgets.DrawLineHorizontal(rect.width, rect.height / 2f, rect.width * 3f); diff --git a/Source/Client/Patches/TickPatch.cs b/Source/Client/Patches/TickPatch.cs index f05811b1..612926a4 100644 --- a/Source/Client/Patches/TickPatch.cs +++ b/Source/Client/Patches/TickPatch.cs @@ -288,7 +288,7 @@ public static float ActualRateMultiplier(this ITickable tickable, TimeSpeed spee return rate; } - public static ForcedTickRate GetForcedSpeedInfo(this ITickable tickable) + public static ForcedTickRate GetForcedTickRate(this ITickable tickable) { if (Multiplayer.GameComp.asyncTime) {