public static Point?FindNearest(this GameField gameField, Point pos, CellFlags flags, int minPath = 1) { var openList = new List <Point> { pos }; var nextOpenList = new List <Point>(); var rowLen = gameField.Width; var colLen = gameField.Height; var closedList = GetClosedList(gameField); var iterations = 1; for (var i = 0;; i++) { if (i == openList.Count) { if (nextOpenList.Count == 0) { return(null); } ++iterations; var sw = openList; openList = nextOpenList; nextOpenList = sw; nextOpenList.Clear(); i = 0; } var src = openList[i]; for (int j = 0; j < 4; j++) { var adj = new Point(src.X + ColNum[j], src.Y + RowNum[j]); Warp(ref adj, rowLen, colLen); if (!IsValid(adj, rowLen, colLen)) { continue; } if (closedList[adj.ToIdx(rowLen)]) { continue; } if (!gameField.CanTraverse(adj)) { continue; } if (iterations >= minPath && gameField.GetFlags(adj).CHasFlag(flags)) { return(adj); } closedList[adj.ToIdx(rowLen)] = true; nextOpenList.Add(adj); } } }
public static Path FindPath(this GameField gameField, Point @from, Point to) { var closedList = GetClosedList(gameField); var cameFrom = new Dictionary <Point, Breadcrump>(); var openList = new HashSet <Breadcrump> { new Breadcrump(@from, 0, @from.Distance(to)) }; var rowLen = gameField.Width; var colLen = gameField.Height; closedList[from.ToIdx(rowLen)] = true; while (openList.Count > 0) { var src = openList.FindMin(n => n.HScore); openList.Remove(src); for (int i = 0; i < 4; i++) { var adj = new Point(src.Pos.X + ColNum[i], src.Pos.Y + RowNum[i]); Warp(ref adj, rowLen, colLen); if (!IsValid(adj, rowLen, colLen)) { continue; } if (closedList[adj.ToIdx(rowLen)]) { continue; } if (!gameField.CanTraverse(adj)) { continue; } closedList[adj.ToIdx(rowLen)] = true; cameFrom[adj] = src; if (adj == to) { return(ReconstructPath(cameFrom, to)); } openList.Add(new Breadcrump(adj, src.Hops + 1, adj.Distance(to))); } } Player.Print($"path not found {from} {to}"); return(null); }
public static List <List <Point> > FindNearest(GameField gameField, Point pos, int hops = 10) { var openList = new List <Point> { pos }; var nextOpenList = new List <Point>(); var rowLen = gameField.Width; var colLen = gameField.Height; var closedList = AStarUtil.GetClosedList(gameField); var res = new List <List <Point> >(hops); var hopList = new List <Point>(8); res.Add(hopList); var iterations = 1; for (var i = 0;; i++) { if (i == openList.Count) { ++iterations; if (nextOpenList.Count == 0 || iterations == hops) { return(res); } hopList = new List <Point>(); res.Add(hopList); var sw = openList; openList = nextOpenList; nextOpenList = sw; nextOpenList.Clear(); i = 0; } var src = openList[i]; for (int j = 0; j < 4; j++) { var adj = new Point(src.X + AStarUtil.ColNum[j], src.Y + AStarUtil.RowNum[j]); AStarUtil.Warp(ref adj, rowLen, colLen); if (!AStarUtil.IsValid(adj, rowLen, colLen)) { continue; } if (closedList[adj.ToIdx(rowLen)]) { continue; } if (!gameField.CanTraverse(adj)) { continue; } closedList[adj.ToIdx(rowLen)] = true; nextOpenList.Add(adj); hopList.Add(adj); } } }