Recursive AI

Find the nearest walkable tile in C#

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

Use TryFindNearestWalkable to move a spawn off a wall. In the sample, # is a wall and . is open. AgentSize is the unit’s width in cells; NearestRadius is how far the search may look.

Terminal
dotnet add package RecursiveAI.OnePath

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

C#
using RecursiveAI.OnePath;
 
var grid = Grid.FromText(
    ".#...\n.....\n.....");
var blocked = new Cell(1, 0);
var options = new GridOptions
{
    AgentSize = 2, NearestRadius = 1
};
if (grid.TryFindNearestWalkable(
    blocked, out var start, options))
    Console.WriteLine(
        $"Start: {start.X}, {start.Y}");

Unzip the example, then dotnet run.

Text
Start: 2, 0

A 2×2 unit at (0, 0) would cover the wall, so that tile isn’t picked.

If no tile fits within the radius, the call returns false; don’t use the out cell in that case.

The full program

The download passes the recovered start and the same GridOptions into FindPath. In this map, the 2×2 unit can move from (2, 0) to (3, 1).

C#
using RecursiveAI.OnePath;
 
var grid = Grid.FromText(".#...\n.....\n.....");
var blocked = new Cell(1, 0);
var options = new GridOptions
{
    AgentSize = 2, NearestRadius = 1
};
if (!grid.TryFindNearestWalkable(blocked, out var start, options))
{
    Console.WriteLine("No walkable start.");
    return;
}
Console.WriteLine($"Start: {start.X}, {start.Y}");
 
var goal = new Cell(3, 1);
var path = grid.FindPath(start, goal, options);
Console.WriteLine($"Path: {path.Status}");
Text
Start: 2, 0
Path: Found

Variations

Search only the original tile. Replace the TryFindNearestWalkable call in the snippet at the top with this check. A radius of zero leaves no room to move off the wall in this example.

C#
var exact = options with { NearestRadius = 0 };
Console.WriteLine(grid.TryFindNearestWalkable(
    blocked, out _, exact));
Text
False

The short answer

Call Grid.TryFindNearestWalkable with the blocked position and a GridOptions whose AgentSize is the side of the unit’s square footprint in cells. Set NearestRadius to limit how far the unit can snap. If the call returns true, pass the new start and the same options to FindPath. Finding a valid start and finding a route to the goal are separate decisions.

How do you move to the closest walkable tile near an obstacle?

Set the radius in grid cells, not world units. The grid docs say “NearestRadius bounds the search in Chebyshev rings.” One ring includes both the four edge neighbors and the four diagonal neighbors. Within that square, OnePath ranks candidates by squared straight-line distance in cell coordinates. An edge neighbor beats a diagonal neighbor if both fit. For a tie, the smaller row index wins, then the smaller column index. In the sample, (2, 0) and (1, 1) are both one cell away and both fit; (2, 0) is on the earlier row.

A tile being open is necessary but isn’t sufficient for a wide unit. The tile is the top-left of the footprint, not its center. For AgentSize = 2, the anchor at (0, 0) fails because the footprint would cover the wall at (1, 0); an anchor right against the bottom edge would fail because the footprint would extend off the grid. The call also rejects candidates refused by your Blocked callback, if you supply one.

What if there’s still no path to the goal?

The nearest fit is a geometric choice, not a reachability check. A wall can separate the returned tile from the goal even when there’s another valid start a little farther away. Check the path result rather than treating a successful snap as a successful route. If the gameplay rule is to stay on the unit’s side of a barrier, choose or filter spawn candidates by that rule before snapping. Increasing the radius alone does not change which of two valid tiles is nearer.

All onepath recipes