private Car[] RankCars(Car[] prevCars) { List <Car> cars = new List <Car>(prevCars); foreach (Car car in cars) { car.Distance = GridUnit.GetByLocation(new Point(Convert.ToInt32(car.Centre.X), Convert.ToInt32(car.Centre.Y)), ParkourGrid, GridUnitSize).Value; } Car[] sortedCars = cars.OrderBy(car => car.Distance).ThenBy(car => car.Lifetime).Reverse().ToArray(); return(sortedCars); }
public static GridUnit[] GetNeighbours(GridUnit unit, GridUnit[][] grid) { GridUnit[] neighbours = new GridUnit[4]; int gridx = unit.GridLocation.X; int gridy = unit.GridLocation.Y; neighbours[0] = grid[gridy][gridx + 1]; neighbours[1] = grid[gridy][gridx - 1]; neighbours[2] = grid[gridy + 1][gridx]; neighbours[3] = grid[gridy - 1][gridx]; return(neighbours); }
public Engine(Display _display, string simulationImage, Point spawn, Point target) { display = _display; //Parse image, divide into grid, generate path ParkourBitmap = new Bitmap(Image.FromFile(simulationImage)); ParkourPixel = ParkourImage.ParseImage(ParkourBitmap); ParkourGrid = GridUnit.CreateGrid(ParkourPixel, GridUnitSize); SpawnLocation = spawn; TargetLocation = target; GridUnit.FindPath(TargetLocation, ParkourGrid, GridUnitSize); GridMaxValue = GridUnit.GetByLocation(TargetLocation, ParkourGrid, GridUnitSize).Value; LoopThread = new Thread(new ThreadStart(EngineLoop)); loopCounterThread = new Thread(IterationCounter); loopCounterThread.Start(); //GenerateCars(); }
public static void FindPath(Point startingLocation, GridUnit[][] grid, int unitSize) // get value for each grid that lies in path { GridUnit startUnit = GetByLocation(startingLocation, grid, unitSize); // starting unit List <GridUnit> selectedUnits = new List <GridUnit>() { startUnit }; // curently active units List <GridUnit> neighbourUnits = new List <GridUnit>(); // next units bool active = true; int index = 0; while (active) { for (int i = 0; i < selectedUnits.Count; i++) { selectedUnits[i].Value = index; GridUnit[] neighbours = GetNeighbours(selectedUnits[i], grid); foreach (GridUnit unit in neighbours) { if (unit.Value == -1 && unit.AccessibilityPercentage >= 75) { unit.Value = -2; // selected units get id -2 to eliminate reselection neighbourUnits.Add(unit); } } } if (neighbourUnits.Count > 0) // if no possible units are found { selectedUnits = new List <GridUnit>(neighbourUnits); neighbourUnits = new List <GridUnit>(); index++; } else { active = false; } } }
public static GridUnit[][] CreateGrid(byte[][][] bitmap, int unitSize) // Creates a grid of a given image { int width = bitmap[0].Length / unitSize; int height = bitmap.Length / unitSize; GridUnit[][] grid = new GridUnit[height][]; for (int iter = 0; iter < height; iter++) { GridUnit[] gridRow = new GridUnit[width]; for (int i = 0; i < width; i++) { GridUnit unit = new GridUnit(new Point(i * unitSize, iter * unitSize), new Point(i, iter), unitSize); unit.GetUnit(bitmap); gridRow[i] = unit; } grid[iter] = gridRow; } return(grid); }