Skip to content

Commit

Permalink
make history size readonly and set from constructor. Read tickrate fr…
Browse files Browse the repository at this point in the history
…om server (client don't need to set FPS)
  • Loading branch information
RevenantX committed Jan 13, 2025
1 parent c3bed67 commit 7fec853
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 30 deletions.
16 changes: 9 additions & 7 deletions LiteEntitySystem/ClientEntityManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -212,13 +212,13 @@ public T GetPlayerController<T>() where T : ControllerLogic
/// <param name="inputProcessor">Input processor (you can use default InputProcessor/<T/> or derive from abstract one to make your own input serialization</param>
/// <param name="netPeer">Local AbstractPeer</param>
/// <param name="headerByte">Header byte that will be used for packets (to distinguish entity system packets)</param>
/// <param name="framesPerSecond">Fixed framerate of game logic</param>
/// <param name="maxHistorySize">Maximum size of lag compensation history in ticks</param>
public ClientEntityManager(
EntityTypesMap typesMap,
InputProcessor inputProcessor,
AbstractNetPeer netPeer,
byte headerByte,
byte framesPerSecond) : base(typesMap, inputProcessor, NetworkMode.Client, framesPerSecond, headerByte)
MaxHistorySize maxHistorySize = MaxHistorySize.Size32) : base(typesMap, inputProcessor, NetworkMode.Client, headerByte, maxHistorySize)
{
_netPeer = netPeer;
_sendBuffer[0] = headerByte;
Expand All @@ -241,18 +241,18 @@ public override void Reset()
/// <param name="typesMap">EntityTypesMap with registered entity types</param>
/// <param name="netPeer">Local AbstractPeer</param>
/// <param name="headerByte">Header byte that will be used for packets (to distinguish entity system packets)</param>
/// <param name="framesPerSecond">Fixed framerate of game logic</param>
/// <param name="maxHistorySize">Maximum size of lag compensation history in ticks</param>
/// <typeparam name="TInput">Main input packet type</typeparam>
public static ClientEntityManager Create<TInput>(
EntityTypesMap typesMap,
AbstractNetPeer netPeer,
byte headerByte,
byte framesPerSecond) where TInput : unmanaged =>
MaxHistorySize maxHistorySize = MaxHistorySize.Size32) where TInput : unmanaged =>
new (typesMap,
new InputProcessor<TInput>(),
netPeer,
headerByte,
framesPerSecond);
maxHistorySize);

/// <summary>
/// Add local entity that will be not synchronized
Expand Down Expand Up @@ -331,6 +331,7 @@ public unsafe DeserializeResult Deserialize(ReadOnlySpan<byte> inData)
{
if (inData.Length < sizeof(BaselineDataHeader))
return DeserializeResult.Error;

_entitiesToConstruct.Clear();
_syncCallsCount = 0;
_syncCallsBeforeConstructCount = 0;
Expand All @@ -339,6 +340,7 @@ public unsafe DeserializeResult Deserialize(ReadOnlySpan<byte> inData)
var header = *(BaselineDataHeader*)rawData;
if (header.OriginalLength < 0)
return DeserializeResult.Error;
SetTickrate(header.Tickrate);
_serverSendRate = (ServerSendRate)header.SendRate;

//reset pooled
Expand Down Expand Up @@ -386,7 +388,6 @@ public unsafe DeserializeResult Deserialize(ReadOnlySpan<byte> inData)
ServerTick = _stateA.Tick;
_lastReadyTick = ServerTick;
_inputCommands.Clear();
_isSyncReceived = true;
_jitterTimer.Reset();
ConstructAndSync(true);
_entitiesToConstruct.Clear();
Expand Down Expand Up @@ -720,7 +721,8 @@ protected override unsafe void OnLogicTick()
/// </summary>
public override unsafe void Update()
{
if (!_isSyncReceived)
//skip update until receive first sync and tickrate
if (Tickrate == 0)
return;

//logic update
Expand Down
7 changes: 7 additions & 0 deletions LiteEntitySystem/Collections/CircularBuffer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace LiteEntitySystem.Collections
{
public class CircularBuffer
{

}
}
40 changes: 25 additions & 15 deletions LiteEntitySystem/EntityManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,24 +121,24 @@ public abstract class EntityManager
public readonly bool IsClient;

/// <summary>
/// FPS of game logic
/// tick rate of game logic (logic FPS, not visual)
/// </summary>
public readonly int FramesPerSecond;
public byte Tickrate { get; private set; }

/// <summary>
/// Fixed delta time
/// </summary>
public readonly double DeltaTime;
public double DeltaTime { get; private set; }

/// <summary>
/// Fixed delta time (float for less precision)
/// </summary>
public readonly float DeltaTimeF;
public float DeltaTimeF { get; private set; }

/// <summary>
/// Size of history (in ticks) for lag compensation. Tune for your game fps
/// </summary>
public MaxHistorySize MaxHistorySize = MaxHistorySize.Size32;
public readonly MaxHistorySize MaxHistorySize;

/// <summary>
/// Local player id (0 on server)
Expand Down Expand Up @@ -173,8 +173,8 @@ public abstract class EntityManager
internal readonly InternalEntity[] EntitiesDict = new InternalEntity[MaxEntityCount+1];
internal readonly EntityClassData[] ClassDataDict;

private readonly long _deltaTimeTicks;
private readonly long _slowdownTicks;
private long _deltaTimeTicks;
private long _slowdownTicks;
private long _accumulator;
private long _lastTime;
private bool _lagCompensationEnabled;
Expand Down Expand Up @@ -230,21 +230,31 @@ static EntityManager()
RegisterFieldType<FloatAngle>(FloatAngle.Lerp);
}

protected EntityManager(EntityTypesMap typesMap, InputProcessor inputProcessor, NetworkMode mode, byte framesPerSecond, byte headerByte)
protected void SetTickrate(byte tickrate)
{
Tickrate = tickrate;
DeltaTime = 1.0 / tickrate;
DeltaTimeF = (float) DeltaTime;
_deltaTimeTicks = (long)(DeltaTime * Stopwatch.Frequency);
_slowdownTicks = (long)(DeltaTime * TimeSpeedChangeCoef * Stopwatch.Frequency);
if (_slowdownTicks < 100)
_slowdownTicks = 100;
}

protected EntityManager(
EntityTypesMap typesMap,
InputProcessor inputProcessor,
NetworkMode mode,
byte headerByte,
MaxHistorySize maxHistorySize)
{
MaxHistorySize = maxHistorySize;
HeaderByte = headerByte;
ClassDataDict = new EntityClassData[typesMap.MaxId+1];
Mode = mode;
IsServer = Mode == NetworkMode.Server;
IsClient = Mode == NetworkMode.Client;
InputProcessor = inputProcessor;
FramesPerSecond = framesPerSecond;
DeltaTime = 1.0 / framesPerSecond;
DeltaTimeF = (float) DeltaTime;
_deltaTimeTicks = (long)(DeltaTime * Stopwatch.Frequency);
_slowdownTicks = (long)(DeltaTime * TimeSpeedChangeCoef * Stopwatch.Frequency);
if (_slowdownTicks < 100)
_slowdownTicks = 100;

ushort filterCount = 0;
ushort singletonCount = 0;
Expand Down
1 change: 1 addition & 0 deletions LiteEntitySystem/Internal/InternalPackets.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ internal struct BaselineDataHeader
public byte PlayerId;
public byte SendRate;
public ushort Tick;
public byte Tickrate;
public int OriginalLength;
}

Expand Down
21 changes: 13 additions & 8 deletions LiteEntitySystem/ServerEntityManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,17 +81,20 @@ public sealed class ServerEntityManager : EntityManager
/// <param name="packetHeader">Header byte that will be used for packets (to distinguish entity system packets)</param>
/// <param name="framesPerSecond">Fixed framerate of game logic</param>
/// <param name="sendRate">Send rate of server (depends on fps)</param>
/// <param name="maxHistorySize">Maximum size of lag compensation history in ticks</param>
public ServerEntityManager(
EntityTypesMap typesMap,
InputProcessor inputProcessor,
byte packetHeader,
byte framesPerSecond,
ServerSendRate sendRate)
: base(typesMap, inputProcessor, NetworkMode.Server, framesPerSecond, packetHeader)
ServerSendRate sendRate,
MaxHistorySize maxHistorySize = MaxHistorySize.Size32)
: base(typesMap, inputProcessor, NetworkMode.Server, packetHeader, maxHistorySize)
{
InternalPlayerId = ServerPlayerId;
_packetBuffer[0] = packetHeader;
SendRate = sendRate;
SetTickrate(framesPerSecond);
}

/// <summary>
Expand All @@ -101,20 +104,21 @@ public ServerEntityManager(
/// <param name="packetHeader">Header byte that will be used for packets (to distinguish entity system packets)</param>
/// <param name="framesPerSecond">Fixed framerate of game logic</param>
/// <param name="sendRate">Send rate of server (depends on fps)</param>
/// <param name="maxHistorySize">Maximum size of lag compensation history in ticks</param>
/// <typeparam name="TInput">Main input packet type</typeparam>
public static ServerEntityManager Create<TInput>(
EntityTypesMap typesMap,
byte packetHeader,
byte framesPerSecond,
ServerSendRate sendRate) where TInput : unmanaged
{
return new ServerEntityManager(
ServerSendRate sendRate,
MaxHistorySize maxHistorySize = MaxHistorySize.Size32) where TInput : unmanaged =>
new ServerEntityManager(
typesMap,
new InputProcessor<TInput>(),
packetHeader,
framesPerSecond,
sendRate);
}
sendRate,
maxHistorySize);

public override void Reset()
{
Expand Down Expand Up @@ -527,7 +531,8 @@ protected override unsafe void OnLogicTick()
OriginalLength = originalLength,
Tick = _tick,
PlayerId = player.Id,
SendRate = (byte)SendRate
SendRate = (byte)SendRate,
Tickrate = Tickrate
};

//compress
Expand Down

0 comments on commit 7fec853

Please sign in to comment.