From b3dacbd6f61547eb485ea904848ab5b26a25b0fb Mon Sep 17 00:00:00 2001 From: Vinzenz Schroeter Date: Sat, 13 Apr 2024 16:27:45 +0200 Subject: [PATCH] socket fixes, fix tank jumping --- DisplayCommands/Cp437Grid.cs | 3 ++- TanksServer/GameLogic/SpawnNewTanks.cs | 2 +- TanksServer/Graphics/DrawBulletsStep.cs | 2 +- TanksServer/Graphics/DrawMapStep.cs | 4 +-- TanksServer/Graphics/DrawTanksStep.cs | 4 +-- .../Interactivity/ByteChannelWebSocket.cs | 2 +- .../Interactivity/ClientScreenServer.cs | 4 +-- TanksServer/Models/FloatPosition.cs | 4 ++- TanksServer/Models/PixelBounds.cs | 3 +++ TanksServer/Models/PixelPosition.cs | 16 +++++++++--- TanksServer/Models/PositionHelpers.cs | 25 +++---------------- TanksServer/Models/Tank.cs | 17 ++++++------- TanksServer/Models/TilePosition.cs | 4 ++- 13 files changed, 44 insertions(+), 46 deletions(-) diff --git a/DisplayCommands/Cp437Grid.cs b/DisplayCommands/Cp437Grid.cs index e8650e4..19afe2f 100644 --- a/DisplayCommands/Cp437Grid.cs +++ b/DisplayCommands/Cp437Grid.cs @@ -52,7 +52,8 @@ public sealed class Cp437Grid(ushort width, ushort height) : IEquatable valueBytes = stackalloc byte[] { b }; Span resultStr = stackalloc char[1]; - _encoding.GetChars(valueBytes, resultStr); + var consumed = _encoding.GetChars(valueBytes, resultStr); + Debug.Assert(consumed == 1); return resultStr[0]; } diff --git a/TanksServer/GameLogic/SpawnNewTanks.cs b/TanksServer/GameLogic/SpawnNewTanks.cs index a5455ff..ccf9cc9 100644 --- a/TanksServer/GameLogic/SpawnNewTanks.cs +++ b/TanksServer/GameLogic/SpawnNewTanks.cs @@ -32,7 +32,7 @@ internal sealed class SpawnNewTanks( if (map.IsCurrentlyWall(tile)) continue; - var tilePixelCenter = tile.GetPixelRelative(4, 4).ToFloatPosition(); + var tilePixelCenter = tile.ToPixelPosition().GetPixelRelative(4, 4).ToFloatPosition(); var minDistance = bullets.GetAll() .Cast() diff --git a/TanksServer/Graphics/DrawBulletsStep.cs b/TanksServer/Graphics/DrawBulletsStep.cs index ae0566e..2be64c5 100644 --- a/TanksServer/Graphics/DrawBulletsStep.cs +++ b/TanksServer/Graphics/DrawBulletsStep.cs @@ -8,6 +8,6 @@ internal sealed class DrawBulletsStep(BulletManager bullets) : IDrawStep public void Draw(PixelGrid buffer) { foreach (var position in bullets.GetAll().Select(b => b.Position.ToPixelPosition())) - buffer[position.X, position.Y] = true; + buffer[(ushort)position.X, (ushort)position.Y] = true; } } diff --git a/TanksServer/Graphics/DrawMapStep.cs b/TanksServer/Graphics/DrawMapStep.cs index 63e593d..b153b26 100644 --- a/TanksServer/Graphics/DrawMapStep.cs +++ b/TanksServer/Graphics/DrawMapStep.cs @@ -17,8 +17,8 @@ internal sealed class DrawMapStep(MapService map) : IDrawStep for (byte pixelInTileY = 0; pixelInTileY < MapService.TileSize; pixelInTileY++) for (byte pixelInTileX = 0; pixelInTileX < MapService.TileSize; pixelInTileX++) { - var position = tile.GetPixelRelative(pixelInTileX, pixelInTileY); - buffer[position.X, position.Y] = pixelInTileX % 2 == pixelInTileY % 2; + var (x, y) = tile.ToPixelPosition().GetPixelRelative(pixelInTileX, pixelInTileY); + buffer[(ushort)x, (ushort)y] = pixelInTileX % 2 == pixelInTileY % 2; } } } diff --git a/TanksServer/Graphics/DrawTanksStep.cs b/TanksServer/Graphics/DrawTanksStep.cs index d3185d8..769ed49 100644 --- a/TanksServer/Graphics/DrawTanksStep.cs +++ b/TanksServer/Graphics/DrawTanksStep.cs @@ -40,8 +40,8 @@ internal sealed class DrawTanksStep : IDrawStep if (!TankSpriteAt(dx, dy, orientation)) continue; - var position = tankPosition.GetPixelRelative(dx, dy); - buffer[position.X, position.Y] = true; + var (x, y) = tankPosition.GetPixelRelative(dx, dy); + buffer[(ushort)x, (ushort)y] = true; } } } diff --git a/TanksServer/Interactivity/ByteChannelWebSocket.cs b/TanksServer/Interactivity/ByteChannelWebSocket.cs index a0ea688..de5ae7d 100644 --- a/TanksServer/Interactivity/ByteChannelWebSocket.cs +++ b/TanksServer/Interactivity/ByteChannelWebSocket.cs @@ -18,7 +18,7 @@ internal sealed class ByteChannelWebSocket(WebSocket socket, ILogger logger, int yield return _buffer.ToArray(); } - if (socket.State != WebSocketState.Closed) + if (socket.State is not WebSocketState.Closed and not WebSocketState.Aborted) Debugger.Break(); } diff --git a/TanksServer/Interactivity/ClientScreenServer.cs b/TanksServer/Interactivity/ClientScreenServer.cs index 7f7c18d..b6a00a6 100644 --- a/TanksServer/Interactivity/ClientScreenServer.cs +++ b/TanksServer/Interactivity/ClientScreenServer.cs @@ -93,9 +93,9 @@ internal sealed class ClientScreenServer( await _channel.SendAsync(pixels.Data); _lastSentPixels = pixels; } - catch (ChannelClosedException) + catch (WebSocketException ex) { - _logger.LogWarning("send failed, channel is closed"); + _logger.LogWarning(ex, "send failed"); } } diff --git a/TanksServer/Models/FloatPosition.cs b/TanksServer/Models/FloatPosition.cs index dcb6f70..5388e50 100644 --- a/TanksServer/Models/FloatPosition.cs +++ b/TanksServer/Models/FloatPosition.cs @@ -1,9 +1,11 @@ +using System.Diagnostics; using TanksServer.GameLogic; namespace TanksServer.Models; +[DebuggerDisplay("({X} | {Y})")] internal readonly struct FloatPosition(double x, double y) { public double X { get; } = (x + MapService.PixelsPerRow) % MapService.PixelsPerRow; public double Y { get; } = (y + MapService.PixelsPerColumn) % MapService.PixelsPerColumn; -} \ No newline at end of file +} diff --git a/TanksServer/Models/PixelBounds.cs b/TanksServer/Models/PixelBounds.cs index a2ee757..9c5593d 100644 --- a/TanksServer/Models/PixelBounds.cs +++ b/TanksServer/Models/PixelBounds.cs @@ -1,3 +1,6 @@ +using System.Diagnostics; + namespace TanksServer.Models; +[DebuggerDisplay("{TopLeft}, {BottomRight}")] internal record struct PixelBounds(PixelPosition TopLeft, PixelPosition BottomRight); diff --git a/TanksServer/Models/PixelPosition.cs b/TanksServer/Models/PixelPosition.cs index 651ca61..e92e3a9 100644 --- a/TanksServer/Models/PixelPosition.cs +++ b/TanksServer/Models/PixelPosition.cs @@ -1,9 +1,17 @@ +using System.Diagnostics; using TanksServer.GameLogic; namespace TanksServer.Models; -internal readonly struct PixelPosition(ushort x, ushort y) +[DebuggerDisplay("({X} | {Y})")] +internal readonly struct PixelPosition(int x, int y) { - public ushort X { get; } = (ushort)((x + MapService.PixelsPerRow) % MapService.PixelsPerRow); - public ushort Y { get; } = (ushort)((y + MapService.PixelsPerColumn) % MapService.PixelsPerColumn); -} \ No newline at end of file + public int X { get; } = (x + MapService.PixelsPerRow) % MapService.PixelsPerRow; + public int Y { get; } = (y + MapService.PixelsPerColumn) % MapService.PixelsPerColumn; + + public void Deconstruct(out int x, out int y) + { + x = X; + y = Y; + } +} diff --git a/TanksServer/Models/PositionHelpers.cs b/TanksServer/Models/PositionHelpers.cs index 4443e04..baa819f 100644 --- a/TanksServer/Models/PositionHelpers.cs +++ b/TanksServer/Models/PositionHelpers.cs @@ -1,31 +1,14 @@ -using System.Diagnostics; using TanksServer.GameLogic; namespace TanksServer.Models; internal static class PositionHelpers { - public static PixelPosition GetPixelRelative(this TilePosition position, byte subX, byte subY) - { - Debug.Assert(subX < 8); - Debug.Assert(subY < 8); - return new PixelPosition( - (ushort)(position.X * MapService.TileSize + subX), - (ushort)(position.Y * MapService.TileSize + subY) - ); - } + public static PixelPosition GetPixelRelative(this PixelPosition position, short subX, short subY) + => new(position.X + subX, position.Y + subY); - public static PixelPosition GetPixelRelative(this PixelPosition position, byte subX, byte subY) - { - Debug.Assert(subX < 8); - Debug.Assert(subY < 8); - return new PixelPosition((ushort)(position.X + subX), (ushort)(position.Y + subY)); - } - - public static PixelPosition ToPixelPosition(this FloatPosition position) => new( - (ushort)((int)position.X % MapService.PixelsPerRow), - (ushort)((int)position.Y % MapService.PixelsPerRow) - ); + public static PixelPosition ToPixelPosition(this FloatPosition position) + => new((int)position.X, (int)position.Y); public static PixelPosition ToPixelPosition(this TilePosition position) => new( (ushort)(position.X * MapService.TileSize), diff --git a/TanksServer/Models/Tank.cs b/TanksServer/Models/Tank.cs index e4d51ef..6072659 100644 --- a/TanksServer/Models/Tank.cs +++ b/TanksServer/Models/Tank.cs @@ -28,13 +28,12 @@ internal sealed class Tank(Player player, FloatPosition spawnPosition) : IMapEnt public PixelBounds Bounds => GetBoundsForCenter(Position); - public static PixelBounds GetBoundsForCenter(FloatPosition position) => new( - new PixelPosition( - (ushort)(position.X - MapService.TileSize / 2d), - (ushort)(position.Y - MapService.TileSize / 2d) - ), new PixelPosition( - (ushort)(position.X + MapService.TileSize / 2d - 1d), - (ushort)(position.Y + MapService.TileSize / 2d - 1d) - ) - ); + public static PixelBounds GetBoundsForCenter(FloatPosition position) + { + var pixelPosition = position.ToPixelPosition(); + return new PixelBounds( + pixelPosition.GetPixelRelative(-3, -3), + pixelPosition.GetPixelRelative(4, 4) + ); + } } diff --git a/TanksServer/Models/TilePosition.cs b/TanksServer/Models/TilePosition.cs index d4cd3d6..089ff6f 100644 --- a/TanksServer/Models/TilePosition.cs +++ b/TanksServer/Models/TilePosition.cs @@ -1,9 +1,11 @@ +using System.Diagnostics; using TanksServer.GameLogic; namespace TanksServer.Models; +[DebuggerDisplay("({X} | {Y})")] internal readonly struct TilePosition(ushort x, ushort y) { public ushort X { get; } = (ushort)((x + MapService.TilesPerRow) % MapService.TilesPerRow); public ushort Y { get; } = (ushort)((y + MapService.TilesPerColumn) % MapService.TilesPerColumn); -} \ No newline at end of file +}