Skip to content

Commit

Permalink
Merge pull request #291 from InvalidArgument3/slipspace-patches
Browse files Browse the repository at this point in the history
rate limiter on transmitwarpspeedpacket, undo teleport rate (this breaks collision checks)
  • Loading branch information
InvalidArgument3 authored Dec 9, 2024
2 parents e93ea59 + 76b71a3 commit 73b2c6b
Showing 1 changed file with 35 additions and 6 deletions.
41 changes: 35 additions & 6 deletions Slipspace Engine/Data/Scripts/WarpDrive/WarpDriveSession.cs
Original file line number Diff line number Diff line change
Expand Up @@ -254,19 +254,48 @@ 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?.GameLogic?.GetAs<WarpDrive>();
WarpDrive drive = DriveBlock != null ? DriveBlock.GameLogic.GetAs<WarpDrive>() : null;
if (drive == null)
return;

MyAPIGateway.Multiplayer.SendMessageToServer(toggleWarpPacketIdSpeed,
message: MyAPIGateway.Utilities.SerializeToBinary(new SpeedMessage
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)
{
EntityId = DriveBlock.EntityId,
WarpSpeed = currentSpeedPt
}));
MyAPIGateway.Multiplayer.SendMessageToServer(toggleWarpPacketIdSpeed,
message: MyAPIGateway.Utilities.SerializeToBinary(new SpeedMessage
{
EntityId = entityId,
WarpSpeed = currentSpeedPt
}));
updateInfo.LastSentSpeed = currentSpeedPt;
}
}
}

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

0 comments on commit 73b2c6b

Please sign in to comment.