public void GenerateReactiveSquarePath(ISquareMap squareMap, Queue <ICell> optimalPath, ICell vehicleCurrentSquareCell) { //Clean out old path _reactiveSquarePath.Clear(); //If the optimal path is empty well we are done, otherwise pop 1 if (!optimalPath.TryDequeue(out ICell convergentPoint)) { return; } //Dequeue until the cell is not blocked while (squareMap.GetCell(convergentPoint.X, convergentPoint.Y).Blocked) { //if optimal path is empty were done and we can not clear around this position, otherwise //lest pop another. if (!optimalPath.TryDequeue(out convergentPoint)) { return; } } var map = (SquareMap)squareMap; var reactivePath = map.GetShortestPath(vehicleCurrentSquareCell, convergentPoint); foreach (var node in reactivePath) { _reactiveSquarePath.Enqueue(node); } }
public MapFactory(IVehicle vehicle) { _defaultHeight = 0; _defaultWidth = 0; Height = _defaultHeight; Width = _defaultWidth; _hexMap = default; _squareMap = default; }
public static List <ICell> FindShortestPath(ISquareMap graph, ICell start, ICell goal) { var costSoFar = new Dictionary <ICell, int>(); var cameFrom = new Dictionary <ICell, ICell>(); var frontier = new PriorityQueue <ICell>(); frontier.Enqueue(start, 0); cameFrom[start] = start; costSoFar[start] = 0; while (frontier.Count > 0) { var current = frontier.Dequeue(); if (current.Equals(goal)) { break; } foreach (var next in graph.GetPassableNeighbors(current)) { var newCost = costSoFar[current] + (int)graph.GetCell(next.X, next.Y).Coverage; if (costSoFar.ContainsKey(next) && newCost >= costSoFar[next]) { continue; } costSoFar[next] = newCost; var priority = newCost + Heuristic(next, goal); frontier.Enqueue(next, priority); cameFrom[next] = current; } } var path = new List <ICell>(); var pathWasNotFound = !cameFrom.ContainsKey(goal); // Returning an empty list if the path wasn't found if (pathWasNotFound) { return(path); } // Reconstructing path var curr = goal; while (!curr.Equals(start)) { path.Add(curr); curr = cameFrom[curr]; } // Reverse it to start at actual start point path.Reverse(); return(path); }
public Queue <ICell> GenerateOptimalSquarePath(ISquareMap map, IVehicle vehicle) { var currentPostion = vehicle.CurrentSquareCell; var currentHeading = GlobalDirection.East; var finished = false; var path = new List <ICell>(); while (!finished) { //Heading East if (currentHeading == GlobalDirection.East) { path.AddRange( map.GetShortestPath(currentPostion, map.GetCell(map.Width - 1, currentPostion.Y))); currentPostion = new Cell(map.Width - 1, currentPostion.Y); } //Heading West else if (currentHeading == GlobalDirection.West) { path.AddRange( map.GetShortestPath(currentPostion, map.GetCell(0, currentPostion.Y))); currentPostion = new Cell(0, currentPostion.Y); } //Check for finish if (currentPostion.Y + (vehicle.DetectorRadius * 2) >= map.Height) { finished = true; continue; } //Move up X Axis path.AddRange( map.GetShortestPath( currentPostion, map.GetCell(currentPostion.X, currentPostion.Y + (vehicle.DetectorRadius * 2) - 1))); currentPostion = new Cell(currentPostion.X, currentPostion.Y + (vehicle.DetectorRadius * 2) - 1); if (currentHeading == GlobalDirection.East) { currentHeading = GlobalDirection.West; } else { currentHeading = GlobalDirection.East; } } return(new Queue <ICell>(path)); }
public void GenerateMaps(int x, int y, double minePercentage) { //convert to cm x *= 100; y *= 100; //calculate number of cells on x and y axis var xCellCount = (int)Math.Ceiling((decimal)(x) / 25); var yCellCount = (int)Math.Ceiling((decimal)(y) / 25); Width = xCellCount; Height = yCellCount; _squareMap = new SquareMap(xCellCount, yCellCount); _hexMap = new HexMap(xCellCount, yCellCount); _mineMap = new MineMap(xCellCount, yCellCount, minePercentage); }
public static List <Cell> GetCoveredCells( ISquareMap squareMap, Cell centerCell, int detectorRadius) => squareMap.GetRange(centerCell, detectorRadius * 2 - detectorRadius % 2);