@@ -56,41 +56,54 @@ static class VTRSync
5656 {
5757 // Special identifier for world map (since it doesn't have a uniqueID like regular maps)
5858 public const int WorldMapId = - 2 ;
59- public static int lastMovedToMap = - 1 ;
60- public static int lastSentTick = - 1 ;
59+ public const int InvalidMapIndex = - 1 ;
60+ public static int lastMovedToMap = InvalidMapIndex ;
61+ public static int lastSentAtTick = - 1 ;
6162
6263 // Vtr rates
6364 public const int MaximumVtr = 15 ;
6465 public const int MinimumVtr = 1 ;
6566
66- public static int GetSynchronizedUpdateRate ( Thing thing ) => thing ? . MapHeld ? . AsyncTime ( ) ? . VTR ?? VTRSync . MaximumVtr ;
67+ public static int GetSynchronizedUpdateRate ( Thing thing ) => thing ? . MapHeld ? . AsyncTime ( ) ? . VTR ?? MaximumVtr ;
68+
69+ public static void SendViewedMapUpdate ( int previous , int current )
70+ {
71+ string warn = string . Empty ;
72+ if ( previous != lastMovedToMap )
73+ warn = $ " mismatch between expected previous map { previous } and last moved to map { lastMovedToMap } ";
74+ else if ( previous == current ) return ;
75+ int currentTick = Find . TickManager ? . TicksGame ?? 0 ;
76+ MpLog . Debug ( $ "VTR MapSwitchPatch: { lastMovedToMap } ->{ current } @ tick { currentTick } { warn } ") ;
77+ Multiplayer . Client . SendCommand ( CommandType . PlayerCount , ScheduledCommand . Global , ByteWriter . GetBytes ( previous , current ) ) ;
78+ lastMovedToMap = current ;
79+ }
80+
81+ public static void Reset ( )
82+ {
83+ lastMovedToMap = InvalidMapIndex ;
84+ }
6785 }
6886
6987 [ HarmonyPatch ( typeof ( Game ) , nameof ( Game . CurrentMap ) , MethodType . Setter ) ]
7088 static class MapSwitchPatch
7189 {
72- const int InvalidMapIndex = - 1 ;
73-
7490 static void Prefix ( Map value )
7591 {
76- if ( Multiplayer . Client == null || Client . Multiplayer . session == null ) return ;
92+ if ( Multiplayer . Client == null ) return ;
7793
7894 try
7995 {
96+ // WorldRenderModePatch will handle it
97+ if ( VTRSync . lastMovedToMap == VTRSync . WorldMapId ) return ;
8098 int previousMap = GetPreviousMapIndex ( ) ;
81- int newMap = value ? . uniqueID ?? InvalidMapIndex ;
99+ int newMap = value ? . uniqueID ?? VTRSync . InvalidMapIndex ;
82100 int currentTick = Find . TickManager ? . TicksGame ?? 0 ;
83101
84- if ( previousMap == newMap )
85- return ;
86-
87- if ( VTRSync . lastMovedToMap == newMap && currentTick == VTRSync . lastSentTick )
88- return ;
102+ if ( previousMap == newMap ) return ;
103+ if ( VTRSync . lastMovedToMap == newMap && currentTick == VTRSync . lastSentAtTick ) return ;
89104
90- MpLog . Debug ( $ "VTR MapSwitchPatch: Switching from map { previousMap } to { newMap } at tick { currentTick } ") ;
91- Multiplayer . Client . SendCommand ( CommandType . PlayerCount , ScheduledCommand . Global , ByteWriter . GetBytes ( previousMap , newMap ) ) ;
92- VTRSync . lastMovedToMap = newMap ;
93- VTRSync . lastSentTick = currentTick ;
105+ VTRSync . SendViewedMapUpdate ( previousMap , newMap ) ;
106+ VTRSync . lastSentAtTick = currentTick ;
94107 }
95108 catch ( Exception ex )
96109 {
@@ -104,10 +117,10 @@ private static int GetPreviousMapIndex()
104117
105118 if ( currentMapIsRemovedAndWasLatestMap )
106119 {
107- return InvalidMapIndex ;
120+ return VTRSync . InvalidMapIndex ;
108121 }
109122
110- return Find . CurrentMap ? . uniqueID ?? InvalidMapIndex ;
123+ return Find . CurrentMap ? . uniqueID ?? VTRSync . InvalidMapIndex ;
111124 }
112125 }
113126
@@ -125,9 +138,18 @@ static void Postfix(WorldRenderMode __result)
125138 // Detect transition to world map (Planet mode)
126139 if ( __result == WorldRenderMode . Planet && lastRenderMode != WorldRenderMode . Planet )
127140 {
128- if ( VTRSync . lastMovedToMap != - 1 )
141+ if ( VTRSync . lastMovedToMap != VTRSync . InvalidMapIndex && VTRSync . lastMovedToMap != VTRSync . WorldMapId )
142+ {
143+ VTRSync . SendViewedMapUpdate ( VTRSync . lastMovedToMap , VTRSync . WorldMapId ) ;
144+ }
145+ }
146+ // Detect transition back to tile map
147+ else if ( __result != WorldRenderMode . Planet && lastRenderMode == WorldRenderMode . Planet )
148+ {
149+ var newMap = Find . CurrentMap ? . uniqueID ?? VTRSync . InvalidMapIndex ;
150+ if ( newMap != VTRSync . InvalidMapIndex && VTRSync . lastMovedToMap == VTRSync . WorldMapId )
129151 {
130- Multiplayer . Client . SendCommand ( CommandType . PlayerCount , ScheduledCommand . Global , ByteWriter . GetBytes ( VTRSync . lastMovedToMap , VTRSync . WorldMapId ) ) ;
152+ VTRSync . SendViewedMapUpdate ( VTRSync . WorldMapId , newMap ) ;
131153 }
132154 }
133155
0 commit comments