diff --git a/.github/workflows/dotnet.yml b/.github/workflows/dotnet.yml new file mode 100644 index 00000000..858ef7f6 --- /dev/null +++ b/.github/workflows/dotnet.yml @@ -0,0 +1,21 @@ +name: RetroRaycaster CI + +on: [push, pull_request] + +permissions: + contents: read + +jobs: + verify: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - uses: actions/setup-dotnet@v4 + with: + dotnet-version: '8.0.x' + - name: Restore + run: dotnet restore RetroRaycaster.sln + - name: Build + run: dotnet build RetroRaycaster.sln -c Release --no-restore + - name: Test + run: dotnet test RetroRaycaster.sln -c Release --no-build diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..88c0c64d --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +/src/**/bin/ +/src/**/obj/ +/tests/**/bin/ +/tests/**/obj/ +/tests/**/TestResults/ diff --git a/AGENTS.md b/AGENTS.md new file mode 100644 index 00000000..571bc65a --- /dev/null +++ b/AGENTS.md @@ -0,0 +1,19 @@ +# RetroRaycaster + +## Struttura + +- `RetroRaycaster.sln` raccoglie i tre progetti C# con target `net8.0`. +- `src/RetroRaycaster.Core/` contiene la libreria autonoma. +- `src/RetroRaycaster.Desktop/` contiene l'eseguibile e fa riferimento a Core. +- `tests/RetroRaycaster.Core.Tests/` contiene i test xUnit di Core. +- `WOLFSRC/` conserva i sorgenti DOS come riferimento storico. Mantieni questa directory intatta. + +## Verifica + +Usa un SDK .NET 8 compatibile con `global.json`. Dalla radice del repository esegui gli stessi comandi del workflow GitHub Actions. + +```sh +dotnet restore RetroRaycaster.sln +dotnet build RetroRaycaster.sln -c Release --no-restore +dotnet test RetroRaycaster.sln -c Release --no-build +``` diff --git a/RetroRaycaster.sln b/RetroRaycaster.sln new file mode 100644 index 00000000..1677224f --- /dev/null +++ b/RetroRaycaster.sln @@ -0,0 +1,43 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.0.31903.59 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{2AFFA176-7E1D-4831-907B-72375D383F0C}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RetroRaycaster.Core", "src\RetroRaycaster.Core\RetroRaycaster.Core.csproj", "{D2A87438-1502-4120-91AD-CE93DEDB52CF}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RetroRaycaster.Desktop", "src\RetroRaycaster.Desktop\RetroRaycaster.Desktop.csproj", "{52828A9A-BD11-44AF-9B88-668BB58ACE52}" +EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tests", "tests", "{006F083A-7131-4F81-92A7-17B79D5698B5}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RetroRaycaster.Core.Tests", "tests\RetroRaycaster.Core.Tests\RetroRaycaster.Core.Tests.csproj", "{E6DB9596-F818-46D2-A20A-BD67ED4E8B5D}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {D2A87438-1502-4120-91AD-CE93DEDB52CF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {D2A87438-1502-4120-91AD-CE93DEDB52CF}.Debug|Any CPU.Build.0 = Debug|Any CPU + {D2A87438-1502-4120-91AD-CE93DEDB52CF}.Release|Any CPU.ActiveCfg = Release|Any CPU + {D2A87438-1502-4120-91AD-CE93DEDB52CF}.Release|Any CPU.Build.0 = Release|Any CPU + {52828A9A-BD11-44AF-9B88-668BB58ACE52}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {52828A9A-BD11-44AF-9B88-668BB58ACE52}.Debug|Any CPU.Build.0 = Debug|Any CPU + {52828A9A-BD11-44AF-9B88-668BB58ACE52}.Release|Any CPU.ActiveCfg = Release|Any CPU + {52828A9A-BD11-44AF-9B88-668BB58ACE52}.Release|Any CPU.Build.0 = Release|Any CPU + {E6DB9596-F818-46D2-A20A-BD67ED4E8B5D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {E6DB9596-F818-46D2-A20A-BD67ED4E8B5D}.Debug|Any CPU.Build.0 = Debug|Any CPU + {E6DB9596-F818-46D2-A20A-BD67ED4E8B5D}.Release|Any CPU.ActiveCfg = Release|Any CPU + {E6DB9596-F818-46D2-A20A-BD67ED4E8B5D}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(NestedProjects) = preSolution + {D2A87438-1502-4120-91AD-CE93DEDB52CF} = {2AFFA176-7E1D-4831-907B-72375D383F0C} + {52828A9A-BD11-44AF-9B88-668BB58ACE52} = {2AFFA176-7E1D-4831-907B-72375D383F0C} + {E6DB9596-F818-46D2-A20A-BD67ED4E8B5D} = {006F083A-7131-4F81-92A7-17B79D5698B5} + EndGlobalSection +EndGlobal diff --git a/global.json b/global.json new file mode 100644 index 00000000..4c4c3ae5 --- /dev/null +++ b/global.json @@ -0,0 +1,7 @@ +{ + "sdk": { + "version": "8.0.100", + "rollForward": "latestFeature", + "allowPrerelease": false + } +} diff --git a/src/RetroRaycaster.Core/GridMap.cs b/src/RetroRaycaster.Core/GridMap.cs new file mode 100644 index 00000000..f1daa027 --- /dev/null +++ b/src/RetroRaycaster.Core/GridMap.cs @@ -0,0 +1,78 @@ +using System; + +namespace RetroRaycaster.Core; + +public enum WallSide { Vertical, Horizontal } + +/// The entry point into the wall is origin + direction * T. +public readonly record struct RayHit(int CellX, int CellY, WallSide Side, double T); + +/// A grid of unit cells. X increases rightward and Y downward. +public sealed class GridMap +{ + private readonly bool[,] solid; + public int Width => solid.GetLength(1); + public int Height => solid.GetLength(0); + + /// Rows indexed by [y, x]. True denotes a wall. + public GridMap(bool[,] solidCells) + { + ArgumentNullException.ThrowIfNull(solidCells); + if (solidCells.GetLength(0) == 0 || solidCells.GetLength(1) == 0) + throw new ArgumentException("The grid must contain at least one row and column.", nameof(solidCells)); + + solid = (bool[,])solidCells.Clone(); + } + + /// + /// Finds the first wall entered, or returns null for an outside origin, a zero + /// direction, or a ray that leaves the map. Directions need not have unit length. + /// The origin belongs to cell (floor(x), floor(y)) and must be empty if inside. + /// At a corner the ray enters only the diagonal cell and reports its vertical side. + /// + public RayHit? CastRay(double originX, double originY, double directionX, double directionY) + { + if (!double.IsFinite(originX) || !double.IsFinite(originY) || + !double.IsFinite(directionX) || !double.IsFinite(directionY)) + throw new ArgumentException("Ray coordinates and direction must be finite."); + + if (originX < 0 || originX >= Width || originY < 0 || originY >= Height || + (directionX == 0 && directionY == 0)) + return null; + + int x = (int)originX; + int y = (int)originY; + if (solid[y, x]) + throw new ArgumentException("A moving ray must start in an empty cell."); + + int stepX = Math.Sign(directionX); + int stepY = Math.Sign(directionY); + double nextX = CrossingTime(x, originX, directionX); + double nextY = CrossingTime(y, originY, directionY); + + while (true) + { + bool crossX = stepX != 0 && (stepY == 0 || nextX <= nextY); + bool crossY = stepY != 0 && (stepX == 0 || nextY <= nextX); + double t = crossX ? nextX : nextY; + + if (crossX) x += stepX; + if (crossY) y += stepY; + if (x < 0 || x >= Width || y < 0 || y >= Height) + return null; + if (solid[y, x]) + return new RayHit(x, y, crossX ? WallSide.Vertical : WallSide.Horizontal, t); + + // Compute from the original ray to avoid accumulating rounding at corners. + if (crossX) nextX = CrossingTime(x, originX, directionX); + if (crossY) nextY = CrossingTime(y, originY, directionY); + } + } + + private static double CrossingTime(int cell, double origin, double direction) + { + if (direction == 0) return double.PositiveInfinity; + int boundary = direction > 0 ? cell + 1 : cell; + return (boundary - origin) / direction; + } +} diff --git a/src/RetroRaycaster.Core/RetroRaycaster.Core.csproj b/src/RetroRaycaster.Core/RetroRaycaster.Core.csproj new file mode 100644 index 00000000..a7a09e8a --- /dev/null +++ b/src/RetroRaycaster.Core/RetroRaycaster.Core.csproj @@ -0,0 +1,6 @@ + + + net8.0 + enable + + diff --git a/src/RetroRaycaster.Desktop/Program.cs b/src/RetroRaycaster.Desktop/Program.cs new file mode 100644 index 00000000..51bb64e4 --- /dev/null +++ b/src/RetroRaycaster.Desktop/Program.cs @@ -0,0 +1 @@ +return 0; diff --git a/src/RetroRaycaster.Desktop/RetroRaycaster.Desktop.csproj b/src/RetroRaycaster.Desktop/RetroRaycaster.Desktop.csproj new file mode 100644 index 00000000..0a4130a9 --- /dev/null +++ b/src/RetroRaycaster.Desktop/RetroRaycaster.Desktop.csproj @@ -0,0 +1,10 @@ + + + Exe + net8.0 + enable + + + + + diff --git a/tests/RetroRaycaster.Core.Tests/CoreAssemblyTests.cs b/tests/RetroRaycaster.Core.Tests/CoreAssemblyTests.cs new file mode 100644 index 00000000..e4bb96cf --- /dev/null +++ b/tests/RetroRaycaster.Core.Tests/CoreAssemblyTests.cs @@ -0,0 +1,15 @@ +using System.Reflection; +using Xunit; + +namespace RetroRaycaster.Core.Tests; + +public class CoreAssemblyTests +{ + [Fact] + public void CoreLoadsAsLibrary() + { + var core = Assembly.Load("RetroRaycaster.Core"); + + Assert.Null(core.EntryPoint); + } +} diff --git a/tests/RetroRaycaster.Core.Tests/GridMapTests.cs b/tests/RetroRaycaster.Core.Tests/GridMapTests.cs new file mode 100644 index 00000000..aa18100d --- /dev/null +++ b/tests/RetroRaycaster.Core.Tests/GridMapTests.cs @@ -0,0 +1,54 @@ +using System; +using Xunit; + +namespace RetroRaycaster.Core.Tests; + +public class GridMapTests +{ + [Theory] + [InlineData(1.5, 2.5, 1, 0, 4, 2, WallSide.Vertical, 2.5)] + [InlineData(1.5, 2.5, 2, 0, 4, 2, WallSide.Vertical, 1.25)] + [InlineData(4.5, 2.5, -1, 0, 1, 2, WallSide.Vertical, 2.5)] + [InlineData(3.5, 0.5, 0, 1, 3, 2, WallSide.Horizontal, 1.5)] + [InlineData(3.5, 2.5, 0, -1, 3, 0, WallSide.Horizontal, 1.5)] + [InlineData(0.75, 0.25, 2, 1, 2, 1, WallSide.Horizontal, 0.75)] + [InlineData(3, 1, -1, 0, 2, 1, WallSide.Vertical, 0)] + public void HitsFirstWall(double x, double y, double dx, double dy, + int wallX, int wallY, WallSide side, double t) + { + var cells = new bool[3, 6]; + cells[wallY, wallX] = true; + cells[2, 5] = true; + Assert.Equal(new RayHit(wallX, wallY, side, t), new GridMap(cells).CastRay(x, y, dx, dy)); + } + + [Fact] + public void CornerCrossingSkipsWallsTouchedOnlyAtTheirVertex() + { + var cells = new bool[,] { { false, true }, { true, false } }; + Assert.Null(new GridMap(cells).CastRay(0.5, 0.5, 1, 1)); + + cells[1, 1] = true; + Assert.Equal(new RayHit(1, 1, WallSide.Vertical, 0.5), new GridMap(cells).CastRay(0.5, 0.5, 1, 1)); + } + + [Theory] + [InlineData(0.5, 0.5, 1, 0)] + [InlineData(0.5, 0.5, 0, 0)] + [InlineData(-0.5, 1.5, 1, 0)] + [InlineData(4, 1.5, -1, 0)] + [InlineData(2.5, 2, 0, -1)] + public void ReturnsNoHit(double x, double y, double dx, double dy) + { + var cells = new bool[2, 4]; + cells[1, 2] = true; + Assert.Null(new GridMap(cells).CastRay(x, y, dx, dy)); + } + + [Fact] + public void RejectsMovingRayStartingInsideWall() + { + var map = new GridMap(new bool[,] { { true } }); + Assert.Throws(() => map.CastRay(0.5, 0.5, 1, 0)); + } +} diff --git a/tests/RetroRaycaster.Core.Tests/RetroRaycaster.Core.Tests.csproj b/tests/RetroRaycaster.Core.Tests/RetroRaycaster.Core.Tests.csproj new file mode 100644 index 00000000..a9e991a9 --- /dev/null +++ b/tests/RetroRaycaster.Core.Tests/RetroRaycaster.Core.Tests.csproj @@ -0,0 +1,14 @@ + + + net8.0 + enable + false + true + + + + + + + +