public static RouteFinderLists OpenCandidateMoves( RouteFinderLists lists, LocationCandiate currentNode, Vector target, Func <Vector, bool> isValidMove) { Func <Vector, bool> isViable = move => isValidMove(move) && !lists.IsClosed(move); return(currentNode.Location .GetValidMoves(isViable) .Select(move => LocationCandiate .Create(target, move, currentNode.ToSome())) .Aggregate( lists.Close(currentNode), (finderLists, candiate) => finderLists.Open(candiate))); }
public static IMaybe <Path> FindRoute( this Vector start, Vector target, Func <Vector, bool> isValidMove, IMaybe <int> breakSize) { Debug.Assert(isValidMove != null); if (start == target) { return(Maybe <Path> .None); } var lists = RouteFinderLists .Create() .Open(LocationCandiate.Create(target, start)); while (lists.HasOpenCandidates) { // Capture to stop access modified closure var closedList = lists.ClosedList; if (breakSize.Select(x => x < closedList.Count).OrElse(false)) { break; } var currentNode = lists.NextOpenCandiate; if (currentNode.Location == target) { return(BuildActorPath(currentNode).ToSome()); } lists = OpenCandidateMoves( lists, currentNode, target, isValidMove); } return(Maybe <Path> .None); }