Skip to content

Commit c2b0946

Browse files
authored
VTRSync: mapIndex -> mapId (#712)
1 parent d2205f1 commit c2b0946

File tree

4 files changed

+29
-29
lines changed

4 files changed

+29
-29
lines changed

Source/Client/Patches/VTRSyncPatch.cs

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@ 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 const int InvalidMapIndex = -1;
60-
public static int lastMovedToMap = InvalidMapIndex;
59+
public const int InvalidMapId = -1;
60+
public static int lastMovedToMapId = InvalidMapId;
6161
public static int lastSentAtTick = -1;
6262

6363
// Vtr rates
@@ -69,18 +69,18 @@ static class VTRSync
6969
public static void SendViewedMapUpdate(int previous, int current)
7070
{
7171
string warn = string.Empty;
72-
if (previous != lastMovedToMap)
73-
warn = $" mismatch between expected previous map {previous} and last moved to map {lastMovedToMap}";
72+
if (previous != lastMovedToMapId)
73+
warn = $" mismatch between expected previous map {previous} and last moved to map {lastMovedToMapId}";
7474
else if (previous == current) return;
7575
int currentTick = Find.TickManager?.TicksGame ?? 0;
76-
MpLog.Debug($"VTR MapSwitchPatch: {lastMovedToMap}->{current} @ tick {currentTick}{warn}");
76+
MpLog.Debug($"VTR MapSwitchPatch: {lastMovedToMapId}->{current} @ tick {currentTick}{warn}");
7777
Multiplayer.Client.SendCommand(CommandType.PlayerCount, ScheduledCommand.Global, ByteWriter.GetBytes(previous, current));
78-
lastMovedToMap = current;
78+
lastMovedToMapId = current;
7979
}
8080

8181
public static void Reset()
8282
{
83-
lastMovedToMap = InvalidMapIndex;
83+
lastMovedToMapId = InvalidMapId;
8484
}
8585
}
8686

@@ -94,13 +94,13 @@ static void Prefix(Map value)
9494
try
9595
{
9696
// WorldRenderModePatch will handle it
97-
if (VTRSync.lastMovedToMap == VTRSync.WorldMapId) return;
98-
int previousMap = GetPreviousMapIndex();
99-
int newMap = value?.uniqueID ?? VTRSync.InvalidMapIndex;
97+
if (VTRSync.lastMovedToMapId == VTRSync.WorldMapId) return;
98+
int previousMap = GetPreviousMapId();
99+
int newMap = value?.uniqueID ?? VTRSync.InvalidMapId;
100100
int currentTick = Find.TickManager?.TicksGame ?? 0;
101101

102102
if (previousMap == newMap) return;
103-
if (VTRSync.lastMovedToMap == newMap && currentTick == VTRSync.lastSentAtTick) return;
103+
if (VTRSync.lastMovedToMapId == newMap && currentTick == VTRSync.lastSentAtTick) return;
104104

105105
VTRSync.SendViewedMapUpdate(previousMap, newMap);
106106
VTRSync.lastSentAtTick = currentTick;
@@ -111,16 +111,16 @@ static void Prefix(Map value)
111111
}
112112
}
113113

114-
private static int GetPreviousMapIndex()
114+
private static int GetPreviousMapId()
115115
{
116116
bool currentMapIsRemovedAndWasLatestMap = Current.Game.currentMapIndex >= Find.Maps.Count;
117117

118118
if (currentMapIsRemovedAndWasLatestMap)
119119
{
120-
return VTRSync.InvalidMapIndex;
120+
return VTRSync.InvalidMapId;
121121
}
122122

123-
return Find.CurrentMap?.uniqueID ?? VTRSync.InvalidMapIndex;
123+
return Find.CurrentMap?.uniqueID ?? VTRSync.InvalidMapId;
124124
}
125125
}
126126

@@ -138,16 +138,16 @@ static void Postfix(WorldRenderMode __result)
138138
// Detect transition to world map (Planet mode)
139139
if (__result == WorldRenderMode.Planet && lastRenderMode != WorldRenderMode.Planet)
140140
{
141-
if (VTRSync.lastMovedToMap != VTRSync.InvalidMapIndex && VTRSync.lastMovedToMap != VTRSync.WorldMapId)
141+
if (VTRSync.lastMovedToMapId != VTRSync.InvalidMapId && VTRSync.lastMovedToMapId != VTRSync.WorldMapId)
142142
{
143-
VTRSync.SendViewedMapUpdate(VTRSync.lastMovedToMap, VTRSync.WorldMapId);
143+
VTRSync.SendViewedMapUpdate(VTRSync.lastMovedToMapId, VTRSync.WorldMapId);
144144
}
145145
}
146146
// Detect transition back to tile map
147147
else if (__result != WorldRenderMode.Planet && lastRenderMode == WorldRenderMode.Planet)
148148
{
149-
var newMap = Find.CurrentMap?.uniqueID ?? VTRSync.InvalidMapIndex;
150-
if (newMap != VTRSync.InvalidMapIndex && VTRSync.lastMovedToMap == VTRSync.WorldMapId)
149+
var newMap = Find.CurrentMap?.uniqueID ?? VTRSync.InvalidMapId;
150+
if (newMap != VTRSync.InvalidMapId && VTRSync.lastMovedToMapId == VTRSync.WorldMapId)
151151
{
152152
VTRSync.SendViewedMapUpdate(VTRSync.WorldMapId, newMap);
153153
}

Source/Common/Networking/State/ServerPlayingState.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,12 @@ public void HandleClientCommand(ByteReader data)
5151
if (cmd == CommandType.PlayerCount)
5252
{
5353
ByteReader reader = new ByteReader(extra);
54-
var prevMap = reader.ReadInt32();
55-
var newMap = reader.ReadInt32();
56-
if (Player.currentMap != prevMap)
57-
ServerLog.Error($"Inconsistent player {Player.Username} map. Last known map: {Player.currentMap}, " +
58-
$"however received command with transition: {prevMap} -> {newMap}");
59-
Player.currentMap = newMap;
54+
var prevMapId = reader.ReadInt32();
55+
var newMapId = reader.ReadInt32();
56+
if (Player.currentMapId != prevMapId)
57+
ServerLog.Error($"Inconsistent player {Player.Username} map. Last known map: {Player.currentMapId}, " +
58+
$"however received command with transition: {prevMapId} -> {newMapId}");
59+
Player.currentMapId = newMapId;
6060
}
6161

6262
// todo check if map id is valid for the player

Source/Common/PlayerManager.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System.Collections.Generic;
1+
using System.Collections.Generic;
22
using System.Diagnostics;
33
using System.Linq;
44
using System.Net;
@@ -84,9 +84,9 @@ public void SetDisconnected(ConnectionBase conn, MpDisconnectReason reason)
8484
if (player.hasJoined)
8585
{
8686
// Send PlayerCount command to remove the player from their last known map
87-
if (player.currentMap != -1)
87+
if (player.currentMapId != -1)
8888
{
89-
byte[] playerCountData = ByteWriter.GetBytes(player.currentMap, -1); // previousMap: player's map, newMap: -1 (disconnected)
89+
byte[] playerCountData = ByteWriter.GetBytes(player.currentMapId, -1); // previousMap: player's map, newMap: -1 (disconnected)
9090
server.commands.Send(CommandType.PlayerCount, ScheduledCommand.NoFaction, ScheduledCommand.Global, playerCountData);
9191
}
9292
// todo check player.IsPlaying?

Source/Common/ServerPlayer.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22
using System.Diagnostics;
33
using System.Linq;
44

@@ -31,7 +31,7 @@ public class ServerPlayer : IChatSource
3131
public int unfrozenAt;
3232

3333
// Track which map the player is currently on
34-
public int currentMap = -1;
34+
public int currentMapId = -1;
3535

3636
public string Username => conn.username;
3737
public int Latency => conn.Latency;

0 commit comments

Comments
 (0)