Load the grid and the expected route cost from tiny.map and tiny.scen in the zip.
dotnet add package RecursiveAI.OnePathRequires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.
using RecursiveAI.OnePath; using RecursiveAI.OnePath.Grids; var grid = MovingAiMap.Parse( File.ReadAllText("tiny.map")).ToGrid(); var route = MovingAiScenarios.Parse( File.ReadAllText("tiny.scen")).Single(); var start = new Cell( route.StartX, route.StartY); var goal = new Cell( route.GoalX, route.GoalY); var path = grid.FindPath(start, goal); var expected = route.OptimalLength; bool matches = path.Found && Math.Abs(path.Cost - expected) <= 1e-6 * expected; Console.WriteLine($"Matches: {matches}");
Unzip the example, then dotnet run.
Using the two files below (also in the zip), the snippet prints:
Matches: TrueThe full program
tiny.map is a 3×3 grid with one blocked cell, T:
type octile height 3 width 3 map .T. ... ...
tiny.scen lists one route, from (0, 0) to (2, 2):
version 1 0 tiny.map 3 3 0 0 2 2 3.4142135624
The full program also checks that the scenario names the supplied map and gives its dimensions. It explicitly sets the same movement rules used by default: eight directions and no diagonal corner cutting.
using RecursiveAI.OnePath; using RecursiveAI.OnePath.Grids; var grid = MovingAiMap.Parse(File.ReadAllText("tiny.map")).ToGrid(); var route = MovingAiScenarios.Parse(File.ReadAllText("tiny.scen")).Single(); if (route.Map != "tiny.map" || grid.Width != route.MapWidth || grid.Height != route.MapHeight) throw new InvalidDataException("Scenario does not match the map."); var start = new Cell(route.StartX, route.StartY); var goal = new Cell(route.GoalX, route.GoalY); var path = grid.FindPath(start, goal, new GridOptions { Connectivity = Connectivity.Eight, Diagonals = DiagonalRule.NoCornerCutting }); if (!path.Found) throw new InvalidOperationException($"No complete path: {path.Status}."); bool match = Math.Abs(path.Cost - route.OptimalLength) <= 1e-6 * route.OptimalLength; Console.WriteLine(FormattableString.Invariant($"Cost: {path.Cost:F8}")); Console.WriteLine(FormattableString.Invariant($"Expected: {route.OptimalLength:F8}")); Console.WriteLine($"Matches: {match}");
Cost: 3.41421356 Expected: 3.41421356 Matches: True
Malformed file contents throw GridFormatException; check the location in the exception message.
Variations
To try a straight route, replace the one data row in tiny.scen with:
0 tiny.map 3 3 0 0 0 2 2Cost: 2.00000000 Expected: 2.00000000 Matches: True
Related recipes
The short answer
Parse the map with MovingAiMap.Parse and call ToGrid. Parse the route rows with MovingAiScenarios.Parse, then pass each row’s start and goal to FindPath. For a cost check, first confirm the route was found and then compare its cost to OptimalLength with a tolerance. The files describe a grid and a target cost, not a prescribed sequence of cells.
What’s in the Moving AI map and scenario formats?
The Moving AI file format uses an ASCII grid with (0, 0) at the upper left. x is the column and y is the row. OnePath treats . and G as open and @, O and T as blocked. By default, swamp (S) is walkable and water (W) is blocked; MovingAiOptions can change those two rules.
After version 1, a scenario row has nine whitespace-separated fields: bucket, map filename, map width, map height, start x and y, goal x and y, and optimal length. The parser returns these fields; it doesn’t open the map named in the row. For files with multiple rows, check that each row belongs to the map you loaded before using its coordinates. This example rejects mismatched dimensions instead of rescaling the grid.
Why might my path cost differ from the scenario’s optimal length?
The benchmark allows horizontal, vertical and diagonal steps. A straight step costs 1; a diagonal costs √2 and can’t pass a blocked cell at the corner. In this example the tree at (1, 0) blocks the first diagonal from (0, 0) to (1, 1). An optimal route goes down, then diagonally, then right, for 2 + √2.
Check movement and terrain rules before investigating the search. A different diagonal rule, terrain weights or a mover larger than one cell changes the problem. Also check for rounding: scenario lengths are written as decimals, so requiring exact equality between two floating-point values can reject a matching route. OnePath’s grid documentation uses a relative tolerance of 1e-6 when comparing with published benchmark lengths.