//Returns an array of the neighbors of this path. internal List <path> getNeighbors() { Point[] points = position.getAdjacent(); List <path> paths = new List <path>(); foreach (Point p in points) { if (map.isWithinMap(p)) { paths.Add(map.getPath(p)); } } return(paths); }
//Creates corridors from room to room,and returns the list of corridors. (Rooms tiles are not included in corridors). private static List <TileData> defaultConnect(int seed, ref TileData[,] tiles, ref List <Room> rooms) { //Generate the weight map for A*. int[,] weights = generateWeight(seed, tiles); List <TileData> corridors = new List <TileData>(); //Connect first Room to all others. for (int i = 1; i < rooms.Count; i++) { //Get starting point. (middle of start Room) Point start = rooms[0].getTopRight(); start = new Point(start.x - rooms[0].length / 2, start.y - rooms[0].height / 2); //Get ending point. (middle of other Room) Point end = rooms[i].getTopRight(); end = new Point(end.x - rooms[i].length / 2, end.y - rooms[i].height / 2); //Plan: //Add starting point to Open List. //Sort Open list by F value, which is the distance from the end (found by heuristic) + currentCost. //While Open List is NOT empty //Get next Point to calculate, and put it on closed list. //foreach neighbor to this nextPoint //If neighbor point is the final point, make connections and break. //If neighbor point is not walkable, continue. //If neighbor point is NOT on the open List, calc ALL it's moveCost values F,G,H, and set the next Point //as it's cameFrom point. //If neighbor point is ON the open List (and maybe the closed), //if this REALcost (G, not heuristic) from this nextPoint is better than //the one it already has, replace its cameFrom with next point, and re-calc its F,G,H //OpenList sorted by F values. PriorityQueue <int, path> openList = new PriorityQueue <int, path>(); pathMap map = new pathMap(tiles.GetLength(0), tiles.GetLength(1)); path startPath = map.getPath(start); startPath.cameFrom = null; //Starting point came From null. (this is a flag for later) startPath.openList = true; startPath.setValues(0, 0, 0); //Start point doesn't need values. openList.Enqueue(0, startPath); bool isFinished = false; while (!openList.IsEmpty && !isFinished) { path next = openList.DequeueValue(); next.closedList = true; foreach (path neighbor in next.getNeighbors()) { if (neighbor.position.Equals(end)) { //Do ending stuff! isFinished = true; neighbor.cameFrom = next; //Start function to get the path, and put the path into corridors, and put the corridors on the tile map. corridors.AddRange(getFinishedPath(neighbor, ref tiles)); break; } //If not walkable, then check for that here. (currently not possible) if (!neighbor.openList) { //PUT on open List. neighbor.openList = true; neighbor.cameFrom = next; neighbor.setValues(next.cost + weights[neighbor.position.x, neighbor.position.y], neighbor.position.tileDiff(end)); openList.Enqueue(neighbor.probableCost, neighbor); } else if (!neighbor.closedList) { //Compare its current values, and change em if need be. int newCost = next.cost + weights[neighbor.position.x, neighbor.position.y]; if (newCost < neighbor.cost) { //May not actually work... KeyValuePair <int, path> oldPair = new KeyValuePair <int, path>(neighbor.probableCost, neighbor); openList.Remove(oldPair); neighbor.setValues(newCost, neighbor.position.tileDiff(end)); openList.Enqueue(neighbor.probableCost, neighbor); } } } } //End of While Loop } //End of For Loop return(corridors); }