Lag compensation for melee (#11885)
Co-authored-by: metalgearsloth <metalgearsloth@gmail.com>
This commit is contained in:
113
Content.Server/Movement/Systems/LagCompensationSystem.cs
Normal file
113
Content.Server/Movement/Systems/LagCompensationSystem.cs
Normal file
@@ -0,0 +1,113 @@
|
||||
using Content.Server.Movement.Components;
|
||||
using Robust.Server.Player;
|
||||
using Robust.Shared.Map;
|
||||
using Robust.Shared.Timing;
|
||||
|
||||
namespace Content.Server.Movement.Systems;
|
||||
|
||||
/// <summary>
|
||||
/// Stores a buffer of previous positions of the relevant entity.
|
||||
/// Can be used to check the entity's position at a recent point in time.
|
||||
/// </summary>
|
||||
public sealed class LagCompensationSystem : EntitySystem
|
||||
{
|
||||
[Dependency] private readonly IGameTiming _timing = default!;
|
||||
|
||||
// I figured 500 ping is max, so 1.5 is 750.
|
||||
// Max ping I've had is 350ms from aus to spain.
|
||||
public static readonly TimeSpan BufferTime = TimeSpan.FromMilliseconds(750);
|
||||
|
||||
private ISawmill _sawmill = Logger.GetSawmill("lagcomp");
|
||||
|
||||
public override void Initialize()
|
||||
{
|
||||
base.Initialize();
|
||||
_sawmill.Level = LogLevel.Info;
|
||||
SubscribeLocalEvent<LagCompensationComponent, MoveEvent>(OnLagMove);
|
||||
}
|
||||
|
||||
public override void Update(float frameTime)
|
||||
{
|
||||
base.Update(frameTime);
|
||||
|
||||
var curTime = _timing.CurTime;
|
||||
var earliestTime = curTime - BufferTime;
|
||||
|
||||
// Cull any old ones from active updates
|
||||
// Probably fine to include ignored.
|
||||
foreach (var (_, comp) in EntityQuery<ActiveLagCompensationComponent, LagCompensationComponent>(true))
|
||||
{
|
||||
while (comp.Positions.TryPeek(out var pos))
|
||||
{
|
||||
if (pos.Item1 < earliestTime)
|
||||
{
|
||||
comp.Positions.Dequeue();
|
||||
continue;
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
if (comp.Positions.Count == 0)
|
||||
{
|
||||
RemComp<ActiveLagCompensationComponent>(comp.Owner);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void OnLagMove(EntityUid uid, LagCompensationComponent component, ref MoveEvent args)
|
||||
{
|
||||
EnsureComp<ActiveLagCompensationComponent>(uid);
|
||||
component.Positions.Enqueue((_timing.CurTime, args.NewPosition, args.NewRotation));
|
||||
}
|
||||
|
||||
public (EntityCoordinates Coordinates, Angle Angle) GetCoordinatesAngle(EntityUid uid, IPlayerSession pSession,
|
||||
TransformComponent? xform = null)
|
||||
{
|
||||
if (!Resolve(uid, ref xform))
|
||||
return (EntityCoordinates.Invalid, Angle.Zero);
|
||||
|
||||
if (!TryComp<LagCompensationComponent>(uid, out var lag) || lag.Positions.Count == 0)
|
||||
return (xform.Coordinates, xform.LocalRotation);
|
||||
|
||||
var angle = Angle.Zero;
|
||||
var coordinates = EntityCoordinates.Invalid;
|
||||
var ping = pSession.Ping;
|
||||
// Use 1.5 due to the trip buffer.
|
||||
var sentTime = _timing.CurTime - TimeSpan.FromMilliseconds(ping * 1.5);
|
||||
|
||||
foreach (var pos in lag.Positions)
|
||||
{
|
||||
coordinates = pos.Item2;
|
||||
angle = pos.Item3;
|
||||
|
||||
if (pos.Item1 >= sentTime)
|
||||
break;
|
||||
}
|
||||
|
||||
if (coordinates == default)
|
||||
{
|
||||
_sawmill.Debug($"No long comp coords found, using {xform.Coordinates}");
|
||||
coordinates = xform.Coordinates;
|
||||
angle = xform.LocalRotation;
|
||||
}
|
||||
else
|
||||
{
|
||||
_sawmill.Debug($"Actual coords is {xform.Coordinates} and got {coordinates}");
|
||||
}
|
||||
|
||||
return (coordinates, angle);
|
||||
}
|
||||
|
||||
public Angle GetAngle(EntityUid uid, IPlayerSession pSession, TransformComponent? xform = null)
|
||||
{
|
||||
var (_, angle) = GetCoordinatesAngle(uid, pSession, xform);
|
||||
return angle;
|
||||
}
|
||||
|
||||
public EntityCoordinates GetCoordinates(EntityUid uid, IPlayerSession pSession, TransformComponent? xform = null)
|
||||
{
|
||||
var (coordinates, _) = GetCoordinatesAngle(uid, pSession, xform);
|
||||
return coordinates;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user