Recursive AI

Calculate a grid path’s fixed-point cost in C#

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

TryGetFixedCost prices a grid route in integer ticks. One Q31.32 tick is 1/2³² of a cost unit. OnePath returns the same integer cost on every platform for the same path and area costs, so a replay can compare costs recorded on another machine.

Terminal
dotnet add package RecursiveAI.OnePath

Requires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.

C#
using RecursiveAI.OnePath;
 
// '.' is an open cell: 2 rows × 3 columns.
var grid = Grid.FromText("...\n...");
Cell[] cells =
    [new(0, 0), new(1, 0), new(2, 1)];
if (grid.TryGetFixedCost(cells, out var cost))
    Console.WriteLine($"Q31.32: {cost}");
else
    Console.WriteLine("Path refused.");

Unzip the example, then dotnet run.

Text
Q31.32: 10368968296

The grid has two open rows of three cells. The path goes one tile right, then one tile diagonally down and right.

The full program

The download also tries passing the same endpoints with the middle cell removed. TryGetFixedCost returns false because a step from (0, 0) straight to (2, 1) isn’t adjacent. A step with an endpoint on a wall or outside the grid is refused too.

C#
using RecursiveAI.OnePath;
 
var grid = Grid.FromText("...\n...");
Cell[] cells = [new(0, 0), new(1, 0), new(2, 1)];
if (grid.TryGetFixedCost(cells, out long cost))
    Console.WriteLine($"Q31.32: {cost}");
else
    Console.WriteLine("Path refused.");
 
Cell[] gap = [cells[0], cells[^1]];
Console.WriteLine(grid.TryGetFixedCost(gap, out _)
    ? "Gap accepted." : "Gap refused.");
Text
Q31.32: 10368968296
Gap refused.

Variations

Price a straight-only route. Replace the cells declaration at the top with these three cells; leave the cost call in place.

C#
Cell[] cells =
    [new(0, 0), new(1, 0), new(2, 0)];
Text
Q31.32: 8589934592

The short answer

Pass the ordered cells to Grid.TryGetFixedCost and keep the returned long if the call succeeds. In Q31.32, one whole unit is 4294967296 integer ticks. The sample’s straight step costs one unit and its diagonal step costs about 1.4142 units. Use the raw integer when comparing costs; if you display approximate decimal units, divide by 4294967296.0 rather than truncating with integer division.

How do you use fixed-point math for a path cost in C#?

Let OnePath price each step, then compare or sum the integer results. This is different from summing the doubles in a route and rounding the final number once. The grid docs specify that each step is “multiplied by 2^32 (exact) and rounded half to even, and the integers are added.” The returned integer is exact for that rounding rule, not an exact representation of an irrational distance. If you add results from multiple calls yourself, check for overflow there too.

Pass the cells of a route with each intervening tile present. An any-angle route can have a straight segment that spans several grid cells, and a list containing only its corners doesn’t specify which adjacent steps to price. This method doesn’t insert those cells or check whether your search options would allow every move; for instance, it has no corner-cutting setting. Find a route with the movement rules you want first, then price its adjacent steps.

How is diagonal distance priced on a weighted grid?

OnePath averages the area multipliers on the two ends of a step. With weights of 1 and 3, a straight step costs 2 before scaling. For a diagonal, the library also multiplies by 1.4142135623730951. That’s why the two routes in this recipe, each with two steps, have different costs even though every tile has the same weight. A route’s tile count isn’t its price.

The method reads the grid’s current area multipliers and doesn’t price a StepCost hook. If you change the area table after finding a route, you’re pricing it on the new table. Keep the raw cost with the route if you need the old figure; don’t expect to recover a custom search cost from the cells alone.

All onepath recipes