예제 #1
0
        private static List <Grid.Row> RowGetBestPath(Grid grid, Grid.Row start, Grid.Row end, GridPosition[] occupied, float maxCost = 9999)
        {
            List <Grid.Row> closed = new List <Grid.Row>();
            List <RowPath>  open   = new List <RowPath>();

            RowPath originPath = new RowPath();

            originPath.AddRow(start);

            open.Add(originPath);

            while (open.Count > 0)
            {
                open = open.OrderBy(x => x.costOfPath).ToList();
                RowPath current = open[0];
                open.Remove(open[0]);

                if (closed.Contains(current.lastRow))
                {
                    continue;
                }
                if (current.lastRow == end)
                {
                    current.listOfRows.Distinct();
                    current.listOfRows.Remove(start);

                    if (current.listOfRows.Count > maxCost)
                    {
                        return(null);
                    }
                    return(current.listOfRows);
                }

                closed.Add(current.lastRow);

                foreach (Grid.Row t in current.lastRow.GetNeighbors(grid))
                {
                    if (t.impassible || occupied.Contains(t.position) || t.occuped || System.Math.Abs(t.height - current.lastRow.height) > 1)
                    {
                        continue;
                    }
                    RowPath newRowPath = new RowPath(current);
                    newRowPath.AddRow(t);
                    open.Add(newRowPath);
                }
            }
            return(null);
        }
예제 #2
0
 public RowPath(RowPath tp)
 {
     listOfRows = tp.listOfRows.ToList();
     costOfPath = tp.costOfPath;
     lastRow    = tp.lastRow;
 }