diff --git a/DisplayCommands/ByteGrid.cs b/DisplayCommands/ByteGrid.cs index 2572098..6b70e44 100644 --- a/DisplayCommands/ByteGrid.cs +++ b/DisplayCommands/ByteGrid.cs @@ -2,17 +2,17 @@ using System.Diagnostics; namespace DisplayCommands; -public class ByteGrid(ushort width, ushort height) +public sealed class ByteGrid(ushort width, ushort height) : IEquatable { public ushort Height { get; } = height; - + public ushort Width { get; } = width; - + internal Memory Data { get; } = new byte[width * height].AsMemory(); public byte this[ushort x, ushort y] { - get => Data.Span[ GetIndex(x, y)]; + get => Data.Span[GetIndex(x, y)]; set => Data.Span[GetIndex(x, y)] = value; } @@ -24,4 +24,16 @@ public class ByteGrid(ushort width, ushort height) } public void Clear() => Data.Span.Clear(); -} + + public bool Equals(ByteGrid? other) + { + if (ReferenceEquals(null, other)) return false; + if (ReferenceEquals(this, other)) return true; + return Height == other.Height && Width == other.Width && Data.Span.SequenceEqual(other.Data.Span); + } + + public override bool Equals(object? obj) => ReferenceEquals(this, obj) || (obj is ByteGrid other && Equals(other)); + public override int GetHashCode() => HashCode.Combine(Height, Width, Data); + public static bool operator ==(ByteGrid? left, ByteGrid? right) => Equals(left, right); + public static bool operator !=(ByteGrid? left, ByteGrid? right) => !Equals(left, right); +} \ No newline at end of file diff --git a/DisplayCommands/Cp437Grid.cs b/DisplayCommands/Cp437Grid.cs index 0602d15..3198f37 100644 --- a/DisplayCommands/Cp437Grid.cs +++ b/DisplayCommands/Cp437Grid.cs @@ -3,16 +3,16 @@ using System.Text; namespace DisplayCommands; -public sealed class Cp437Grid(ushort width, ushort height) +public sealed class Cp437Grid(ushort width, ushort height) : IEquatable { private readonly ByteGrid _byteGrid = new(width, height); - + public ushort Height { get; } = height; - + public ushort Width { get; } = width; internal Memory Data => _byteGrid.Data; - + private readonly Encoding _encoding = Encoding.GetEncoding(437); public char this[ushort x, ushort y] @@ -47,7 +47,6 @@ public sealed class Cp437Grid(ushort width, ushort height) var consumed = _encoding.GetBytes(valuesStr, convertedStr); Debug.Assert(consumed == 1); return convertedStr[0]; - } private char ByteToChar(byte b) @@ -57,4 +56,16 @@ public sealed class Cp437Grid(ushort width, ushort height) _encoding.GetChars(valueBytes, resultStr); return resultStr[0]; } + + public bool Equals(Cp437Grid? other) + { + if (ReferenceEquals(null, other)) return false; + if (ReferenceEquals(this, other)) return true; + return Height == other.Height && Width == other.Width && _byteGrid.Equals(other._byteGrid); + } + + public override bool Equals(object? obj) => ReferenceEquals(this, obj) || (obj is Cp437Grid other && Equals(other)); + public override int GetHashCode() => HashCode.Combine(_byteGrid, Height, Width); + public static bool operator ==(Cp437Grid? left, Cp437Grid? right) => Equals(left, right); + public static bool operator !=(Cp437Grid? left, Cp437Grid? right) => !Equals(left, right); } \ No newline at end of file diff --git a/DisplayCommands/PixelGrid.cs b/DisplayCommands/PixelGrid.cs index e84c84e..ea40ab5 100644 --- a/DisplayCommands/PixelGrid.cs +++ b/DisplayCommands/PixelGrid.cs @@ -2,7 +2,7 @@ using System.Diagnostics; namespace DisplayCommands; -public sealed class PixelGrid(ushort width, ushort height) +public sealed class PixelGrid(ushort width, ushort height) : IEquatable { private readonly ByteGrid _byteGrid = new((ushort)(width / 8u), height); @@ -34,6 +34,18 @@ public sealed class PixelGrid(ushort width, ushort height) public void Clear() => _byteGrid.Clear(); + public bool Equals(PixelGrid? other) + { + if (ReferenceEquals(null, other)) return false; + if (ReferenceEquals(this, other)) return true; + return Width == other.Width && Height == other.Height && _byteGrid.Equals(other._byteGrid); + } + + public override bool Equals(object? obj) => ReferenceEquals(this, obj) || (obj is PixelGrid other && Equals(other)); + public override int GetHashCode() => HashCode.Combine(_byteGrid, Width, Height); + public static bool operator ==(PixelGrid? left, PixelGrid? right) => Equals(left, right); + public static bool operator !=(PixelGrid? left, PixelGrid? right) => !Equals(left, right); + private (ushort byteIndex, byte bitInByteMask) GetIndexes(int x) { Debug.Assert(x < Width); diff --git a/TanksServer/Interactivity/SendToServicePointDisplay.cs b/TanksServer/Interactivity/SendToServicePointDisplay.cs index 0061a02..50d6883 100644 --- a/TanksServer/Interactivity/SendToServicePointDisplay.cs +++ b/TanksServer/Interactivity/SendToServicePointDisplay.cs @@ -13,6 +13,7 @@ internal sealed class SendToServicePointDisplay : ITickStep private readonly PlayerServer _players; private readonly ILogger _logger; private readonly IDisplayConnection _displayConnection; + private PixelGrid? _lastSentFrame; private DateTime _nextFailLog = DateTime.Now; @@ -50,7 +51,12 @@ internal sealed class SendToServicePointDisplay : ITickStep try { await _displayConnection.SendCp437DataAsync(MapService.TilesPerRow, 0, _scoresBuffer); - await _displayConnection.SendBitmapLinearWindowAsync(0, 0, _lastFinishedFrameProvider.LastFrame); + + var currentFrame = _lastFinishedFrameProvider.LastFrame; + if (_lastSentFrame == currentFrame) + return; + _lastSentFrame = currentFrame; + await _displayConnection.SendBitmapLinearWindowAsync(0, 0, _lastSentFrame); } catch (SocketException ex) { diff --git a/TanksServer/appsettings.json b/TanksServer/appsettings.json index b8cbeea..f346790 100644 --- a/TanksServer/appsettings.json +++ b/TanksServer/appsettings.json @@ -24,7 +24,7 @@ "MoveSpeed": 1.4, "TurnSpeed": 0.4, "ShootDelayMs": 400, - "BulletSpeed": 8 + "BulletSpeed": 3 }, "Players": { "SpawnDelayMs": 3000,