Skip to content

Commit

Permalink
Merge pull request #294 from InvalidArgument3/slipspace-patches
Browse files Browse the repository at this point in the history
Revert "rate limiter on TransmitWarpSpeed packet"
  • Loading branch information
InvalidArgument3 authored Dec 15, 2024
2 parents 8a8f7f7 + 17bb331 commit 341f933
Showing 1 changed file with 6 additions and 35 deletions.
41 changes: 6 additions & 35 deletions Slipspace Engine/Data/Scripts/WarpDrive/WarpDriveSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -254,48 +254,19 @@ public void TransmitToggleWarp(IMyTerminalBlock block)
// MyAPIGateway.Multiplayer.SendMessageToOthers(toggleWarpPacketIdSpeed, data);
}

private Dictionary<long, SpeedUpdateInfo> speedUpdateInfo = new Dictionary<long, SpeedUpdateInfo>();

private class SpeedUpdateInfo
{
public int UpdateTick = 0;
public double LastSentSpeed = 0;
}

private const int SPEED_UPDATE_DELAY = 60; // Update once per second (assuming 60 ticks per second)
private const double SPEED_CHANGE_THRESHOLD = 1.0; // Only update if speed changed by 1 m/s or more

public void TransmitWarpSpeed(IMyFunctionalBlock WarpBlock, double currentSpeedPt)
{
var DriveBlock = WarpBlock as IMyTerminalBlock;
WarpDrive drive = DriveBlock != null ? DriveBlock.GameLogic.GetAs<WarpDrive>() : null;
WarpDrive drive = DriveBlock?.GameLogic?.GetAs<WarpDrive>();
if (drive == null)
return;

long entityId = DriveBlock.EntityId;

SpeedUpdateInfo updateInfo;
if (!speedUpdateInfo.TryGetValue(entityId, out updateInfo))
{
updateInfo = new SpeedUpdateInfo();
speedUpdateInfo[entityId] = updateInfo;
}

updateInfo.UpdateTick++;
if (updateInfo.UpdateTick >= SPEED_UPDATE_DELAY)
{
updateInfo.UpdateTick = 0;
if (Math.Abs(currentSpeedPt - updateInfo.LastSentSpeed) >= SPEED_CHANGE_THRESHOLD)
MyAPIGateway.Multiplayer.SendMessageToServer(toggleWarpPacketIdSpeed,
message: MyAPIGateway.Utilities.SerializeToBinary(new SpeedMessage
{
MyAPIGateway.Multiplayer.SendMessageToServer(toggleWarpPacketIdSpeed,
message: MyAPIGateway.Utilities.SerializeToBinary(new SpeedMessage
{
EntityId = entityId,
WarpSpeed = currentSpeedPt
}));
updateInfo.LastSentSpeed = currentSpeedPt;
}
}
EntityId = DriveBlock.EntityId,
WarpSpeed = currentSpeedPt
}));
}

private void ReceiveWarpConfig(ushort channel, byte[] data, ulong sender, bool fromServer)
Expand Down

0 comments on commit 341f933

Please sign in to comment.