-
-
Notifications
You must be signed in to change notification settings - Fork 133
Variable Tick Rate #549
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Variable Tick Rate #549
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,134 @@ | ||
| using HarmonyLib; | ||
| using Multiplayer.Client.Patches; | ||
| using Multiplayer.Client.Util; | ||
| using Multiplayer.Common; | ||
| using RimWorld.Planet; | ||
| using System; | ||
| using Verse; | ||
|
|
||
| namespace Multiplayer.Client.Patches | ||
| { | ||
| [HarmonyPatch(typeof(GenTicks), nameof(GenTicks.GetCameraUpdateRate))] | ||
| public static class VTRSyncPatch | ||
| { | ||
| static bool Prefix(Thing thing, ref int __result) | ||
| { | ||
| if (Multiplayer.Client == null) | ||
| return true; | ||
|
|
||
| __result = VTRSync.GetSynchronizedUpdateRate(thing); | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| [HarmonyPatch(typeof(Projectile), nameof(Projectile.UpdateRateTicks), MethodType.Getter)] | ||
| public static class VtrSyncProjectilePatch | ||
| { | ||
| static bool Prefix(ref int __result, Projectile __instance) | ||
| { | ||
| if (Multiplayer.Client == null) | ||
| return true; | ||
|
|
||
| __result = __instance.Spawned ? VTRSync.GetSynchronizedUpdateRate(__instance) : VTRSync.MaximumVtr; | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| [HarmonyPatch(typeof(WorldObject), nameof(WorldObject.UpdateRateTicks), MethodType.Getter)] | ||
| public static class VtrSyncWorldObjectPatch | ||
| { | ||
| static bool Prefix(ref int __result, WorldObject __instance) | ||
| { | ||
| if (Multiplayer.Client == null) | ||
| return true; | ||
|
|
||
| __result = VTRSync.MaximumVtr; | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| static class VTRSync | ||
| { | ||
| // Special identifier for world map (since it doesn't have a uniqueID like regular maps) | ||
| public const int WorldMapId = -2; | ||
| public static int lastMovedToMap = -1; | ||
| public static int lastSentTick = -1; | ||
|
|
||
| // Vtr rates | ||
| public const int MaximumVtr = 15; | ||
| public const int MinimumVtr = 1; | ||
|
|
||
| public static int GetSynchronizedUpdateRate(Thing thing) => thing?.MapHeld?.AsyncTime()?.VTR ?? VTRSync.MaximumVtr; | ||
| } | ||
|
|
||
| [HarmonyPatch(typeof(Game), nameof(Game.CurrentMap), MethodType.Setter)] | ||
| static class MapSwitchPatch | ||
| { | ||
| static void Prefix(Map value) | ||
| { | ||
| if (Multiplayer.Client == null || Client.Multiplayer.session == null) return; | ||
|
|
||
| try | ||
| { | ||
| int previousMap = Find.CurrentMap?.uniqueID ?? -1; | ||
| int newMap = value?.uniqueID ?? -1; | ||
| int currentTick = Find.TickManager?.TicksGame ?? 0; | ||
|
|
||
| // If no change in map, do nothing | ||
| if (previousMap == newMap) | ||
| return; | ||
|
|
||
| // Prevent duplicate commands for the same transition, but allow retry after a tick | ||
| if (VTRSync.lastMovedToMap == previousMap && currentTick == VTRSync.lastSentTick) | ||
| return; | ||
|
|
||
| // Send map change command to server | ||
| // Send as global command since it affects multiple maps | ||
| Multiplayer.Client.SendCommand(CommandType.PlayerCount, ScheduledCommand.Global, ByteWriter.GetBytes(previousMap, newMap)); | ||
|
|
||
| // Track this command to prevent duplicates | ||
| VTRSync.lastMovedToMap = newMap; | ||
| VTRSync.lastSentTick = currentTick; | ||
| } | ||
| catch (Exception ex) | ||
| { | ||
| MpLog.Error($"VTR MapSwitchPatch error: {ex.Message}"); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| [HarmonyPatch(typeof(WorldRendererUtility), nameof(WorldRendererUtility.CurrentWorldRenderMode), MethodType.Getter)] | ||
| static class WorldRenderModePatch | ||
| { | ||
| private static WorldRenderMode lastRenderMode = WorldRenderMode.None; | ||
|
|
||
| static void Postfix(WorldRenderMode __result) | ||
| { | ||
| if (Multiplayer.Client == null) return; | ||
|
|
||
| try | ||
| { | ||
| // Detect transition to world map (Planet mode) | ||
| if (__result == WorldRenderMode.Planet && lastRenderMode != WorldRenderMode.Planet) | ||
| { | ||
| if (VTRSync.lastMovedToMap != -1) | ||
| { | ||
| Multiplayer.Client.SendCommand(CommandType.PlayerCount, ScheduledCommand.Global, ByteWriter.GetBytes(VTRSync.lastMovedToMap, VTRSync.WorldMapId)); | ||
| } | ||
| } | ||
| // Detect transition away from world map | ||
| else if (__result != WorldRenderMode.Planet && lastRenderMode == WorldRenderMode.Planet) | ||
| { | ||
| int currentMapId = Find.CurrentMap?.uniqueID ?? -1; | ||
| Multiplayer.Client.SendCommand(CommandType.PlayerCount, ScheduledCommand.Global, ByteWriter.GetBytes(VTRSync.WorldMapId, currentMapId)); | ||
| } | ||
|
|
||
| lastRenderMode = __result; | ||
| } | ||
| catch (Exception ex) | ||
| { | ||
| MpLog.Error($"WorldRenderModePatch error: {ex.Message}"); | ||
| } | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,4 +16,5 @@ public enum CommandType : byte | |
| // Map scope | ||
| MapTimeSpeed, | ||
| Designator, | ||
| PlayerCount, | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've just noticed these are already defined in Thing, we could probably use those.