Skip to content

Commit 2e249e4

Browse files
committed
optimized playerstate (saved 2 bytes per state)
1 parent f4650e3 commit 2e249e4

8 files changed

Lines changed: 40 additions & 22 deletions

File tree

Assets/Code/Client/ClientPlayer.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,9 @@ public void ReceiveServerState(ServerState serverState, PlayerState ourState)
4040
if (_predictionPlayerStates.Count == 0)
4141
return;
4242

43-
int diff = NetworkGeneral.SeqDiff(ourState.ProcessedCommandId,_predictionPlayerStates.First.Id);
43+
ushort lastProcessedCommand = serverState.LastProcessedCommand;
44+
int diff = NetworkGeneral.SeqDiff(lastProcessedCommand,_predictionPlayerStates.First.Id);
45+
4446

4547
//apply prediction
4648
if (diff >= 0 && diff < _predictionPlayerStates.Count)
@@ -51,10 +53,10 @@ public void ReceiveServerState(ServerState serverState, PlayerState ourState)
5153
}
5254
else
5355
{
54-
Debug.Log($"[C] Player input lag: {_predictionPlayerStates.First.Id} {ourState.ProcessedCommandId}");
56+
Debug.Log($"[C] Player input lag: {_predictionPlayerStates.First.Id} {lastProcessedCommand}");
5557
//lag
5658
_predictionPlayerStates.FastClear();
57-
_nextCommand.Id = ourState.ProcessedCommandId;
59+
_nextCommand.Id = lastProcessedCommand;
5860
}
5961
}
6062

Assets/Code/Client/ClientPlayerManager.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public void ApplyServerState(ref ServerState serverState)
3939
else
4040
{
4141
var rp = (RemotePlayer)handler.Player;
42-
rp.OnPlayerState(state);
42+
rp.OnPlayerState(serverState.Tick, state);
4343
}
4444
}
4545
}

Assets/Code/Client/RemotePlayer.cs

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,21 @@
22
using UnityEngine;
33

44
namespace Code.Client
5-
{
5+
{
66
public class RemotePlayer : BasePlayer
77
{
8-
private readonly LiteRingBuffer<PlayerState> _buffer = new LiteRingBuffer<PlayerState>(30);
8+
struct IncomingData
9+
{
10+
public PlayerState State;
11+
public ushort Tick;
12+
13+
public IncomingData(PlayerState state, ushort serverTick)
14+
{
15+
State = state;
16+
Tick = serverTick;
17+
}
18+
}
19+
private readonly LiteRingBuffer<IncomingData> _buffer = new LiteRingBuffer<IncomingData>(30);
920
private float _receivedTime;
1021
private float _timer;
1122
private const float BufferTime = 0.1f; //100 milliseconds
@@ -15,7 +26,7 @@ public RemotePlayer(ClientPlayerManager manager, string name, PlayerJoinedPacket
1526
_position = pjPacket.InitialPlayerState.Position;
1627
_health = pjPacket.Health;
1728
_rotation = pjPacket.InitialPlayerState.Rotation;
18-
_buffer.Add(pjPacket.InitialPlayerState);
29+
_buffer.Add(new IncomingData(pjPacket.InitialPlayerState, pjPacket.ServerTick));
1930
}
2031

2132
public override void Spawn(Vector2 position)
@@ -28,13 +39,13 @@ public void UpdatePosition(float delta)
2839
{
2940
if (_receivedTime < BufferTime || _buffer.Count < 2)
3041
return;
31-
var stateA = _buffer[0];
32-
var stateB = _buffer[1];
42+
var dataA = _buffer[0];
43+
var dataB = _buffer[1];
3344

34-
float lerpTime = NetworkGeneral.SeqDiff(stateB.ProcessedCommandId, stateA.ProcessedCommandId)*LogicTimer.FixedDelta;
45+
float lerpTime = NetworkGeneral.SeqDiff(dataB.Tick, dataA.Tick)*LogicTimer.FixedDelta;
3546
float t = _timer / lerpTime;
36-
_position = Vector2.Lerp(stateA.Position, stateB.Position, t);
37-
_rotation = Mathf.Lerp(stateA.Rotation, stateB.Rotation, t);
47+
_position = Vector2.Lerp(dataA.State.Position, dataB.State.Position, t);
48+
_rotation = Mathf.Lerp(dataA.State.Rotation, dataB.State.Rotation, t);
3849
_timer += delta;
3950
if (_timer > lerpTime)
4051
{
@@ -44,10 +55,10 @@ public void UpdatePosition(float delta)
4455
}
4556
}
4657

47-
public void OnPlayerState(PlayerState state)
58+
public void OnPlayerState(ushort serverTick, PlayerState state)
4859
{
4960
//old command
50-
int diff = NetworkGeneral.SeqDiff(state.ProcessedCommandId, _buffer.Last.ProcessedCommandId);
61+
int diff = NetworkGeneral.SeqDiff(serverTick, _buffer.Last.Tick);
5162
if (diff <= 0)
5263
return;
5364

@@ -59,7 +70,7 @@ public void OnPlayerState(PlayerState state)
5970
_receivedTime = 0f;
6071
_buffer.FastClear();
6172
}
62-
_buffer.Add(state);
73+
_buffer.Add(new IncomingData(state, serverTick));
6374
}
6475
}
6576
}

Assets/Code/Server/ServerLogic.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ private void OnJoinReceived(JoinPacket joinPacket, NetPeer peer)
121121
pj.UserName = joinPacket.UserName;
122122
pj.NewPlayer = true;
123123
pj.InitialPlayerState = player.NetworkState;
124+
pj.ServerTick = _serverTick;
124125
_netManager.SendToAll(WritePacket(pj), DeliveryMethod.ReliableOrdered, peer);
125126

126127
//Send to new player info about old players

Assets/Code/Server/ServerPlayer.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ public class ServerPlayer : BasePlayer
99
private readonly ServerPlayerManager _playerManager;
1010
public readonly NetPeer AssociatedPeer;
1111
public PlayerState NetworkState;
12+
public ushort LastProcessedCommandId { get; private set; }
1213

1314
public ServerPlayer(ServerPlayerManager playerManager, string name, NetPeer peer) : base(playerManager, name, (byte)peer.Id)
1415
{
@@ -20,7 +21,7 @@ public ServerPlayer(ServerPlayerManager playerManager, string name, NetPeer peer
2021

2122
public override void ApplyInput(PlayerInputPacket command, float delta)
2223
{
23-
NetworkState.ProcessedCommandId = command.Id;
24+
LastProcessedCommandId = command.Id;
2425
base.ApplyInput(command, delta);
2526
}
2627

Assets/Code/Server/ServerPlayerManager.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public override void OnShoot(BasePlayer from, Vector2 to, BasePlayer hit)
5151
ShootPacket sp = new ShootPacket
5252
{
5353
FromPlayer = serverPlayer.Id,
54-
CommandId = serverPlayer.NetworkState.ProcessedCommandId,
54+
CommandId = serverPlayer.LastProcessedCommandId,
5555
ServerTick = _serverLogic.Tick,
5656
Hit = to
5757
};

Assets/Code/Shared/GamePackets.cs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ public class PlayerJoinedPacket
3030
public string UserName { get; set; }
3131
public bool NewPlayer { get; set; }
3232
public byte Health { get; set; }
33+
public ushort ServerTick { get; set; }
3334

3435
public PlayerState InitialPlayerState { get; set; }
3536
}
@@ -121,40 +122,40 @@ public struct PlayerState : INetSerializable
121122
public byte Id;
122123
public Vector2 Position;
123124
public float Rotation;
124-
public ushort ProcessedCommandId;
125125

126-
public const int Size = 1 + 8 + 4 + 2;
126+
public const int Size = 1 + 8 + 4;
127127

128128
public void Serialize(NetDataWriter writer)
129129
{
130130
writer.Put(Id);
131131
writer.Put(Position);
132132
writer.Put(Rotation);
133-
writer.Put(ProcessedCommandId);
134133
}
135134

136135
public void Deserialize(NetDataReader reader)
137136
{
138137
Id = reader.GetByte();
139138
Position = reader.GetVector2();
140139
Rotation = reader.GetFloat();
141-
ProcessedCommandId = reader.GetUShort();
142140
}
143141
}
144142

145143
public struct ServerState : INetSerializable
146144
{
147145
public ushort Tick;
146+
public ushort LastProcessedCommand;
147+
148148
public int PlayerStatesCount;
149149
public int StartState; //server only
150150
public PlayerState[] PlayerStates;
151151

152152
//tick
153-
public const int HeaderSize = sizeof(ushort);
153+
public const int HeaderSize = sizeof(ushort)*2;
154154

155155
public void Serialize(NetDataWriter writer)
156156
{
157157
writer.Put(Tick);
158+
writer.Put(LastProcessedCommand);
158159

159160
for (int i = 0; i < PlayerStatesCount; i++)
160161
PlayerStates[StartState + i].Serialize(writer);
@@ -163,6 +164,7 @@ public void Serialize(NetDataWriter writer)
163164
public void Deserialize(NetDataReader reader)
164165
{
165166
Tick = reader.GetUShort();
167+
LastProcessedCommand = reader.GetUShort();
166168

167169
PlayerStatesCount = reader.AvailableBytes / PlayerState.Size;
168170
if (PlayerStates == null || PlayerStates.Length < PlayerStatesCount)

Assets/Code/Shared/NetworkGeneral.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ namespace Code.Shared
44
{
55
public static class NetworkGeneral
66
{
7+
public const int ProtocolId = 1;
78
public static readonly int PacketTypesCount = Enum.GetValues(typeof(PacketType)).Length;
89

910
public const int MaxGameSequence = 512;

0 commit comments

Comments
 (0)