---
title: "Load Moving AI .map and .scen pathfinding files in C#"
url: https://recursiveai.org/onepath/cookbook/load-moving-ai-grid-and-scenarios/
description: "Parse a Moving AI grid and version 1 scenario in C# with OnePath, run the listed route, and compare its path cost with the scenario’s optimal length."
version: "1.2.0"
---

# Load Moving AI .map and .scen pathfinding files in C#

**OnePath** is a free, open-source pathfinding library for .NET from Recursive AI. MIT or Apache-2.0, no native dependencies.

[NuGet](https://www.nuget.org/packages/RecursiveAI.OnePath) · [GitHub](https://github.com/recursive-ai-oss/onepath-csharp) · [Docs](https://recursiveai.org/onepath/docs/) · [Download example (.zip)](https://recursiveai.org/onepath/cookbook/load-moving-ai-grid-and-scenarios/load-moving-ai-grid-and-scenarios.zip)

Load the grid and the expected route cost from `tiny.map` and `tiny.scen` in the zip.

```shell
dotnet add package RecursiveAI.OnePath
```

Requires [.NET 10](https://dotnet.microsoft.com/download/dotnet/10.0) (net10.0); .NET 9 and earlier aren’t supported.

```csharp
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](https://recursiveai.org/onepath/cookbook/load-moving-ai-grid-and-scenarios/load-moving-ai-grid-and-scenarios.zip), then `dotnet run`.

Using the two files below (also in the zip), the snippet prints:

```text
Matches: True
```

## The full program

`tiny.map` is a 3×3 grid with one blocked cell, `T`:

```text
type octile
height 3
width 3
map
.T.
...
...
```

`tiny.scen` lists one route, from `(0, 0)` to `(2, 2)`:

```text
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.

```csharp
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}");
```

```text
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:

```text
0 tiny.map 3 3 0 0 0 2 2
```

```text
Cost: 2.00000000
Expected: 2.00000000
Matches: True
```

## Related recipes

- [Prevent diagonal corner cutting](https://recursiveai.org/onepath/cookbook/prevent-diagonal-corner-cutting/)
- [Find paths on a grid](https://recursiveai.org/onepath/cookbook/grid-pathfinding/)

## 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](https://movingai.com/benchmarks/formats.html) 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](https://recursiveai.org/onepath/docs/grids/) uses a relative tolerance of `1e-6` when comparing with published benchmark lengths.
