public void Initialize() { this.left = new NeighborCell(Directions.Left); this.right = new NeighborCell(Directions.Right); this.topLeft = new NeighborCell(Directions.TopLeft); this.topRight = new NeighborCell(Directions.TopRight); this.downLeft = new NeighborCell(Directions.DownLeft); this.downRight = new NeighborCell(Directions.DownRight); }
/// <summary> /// Generates a seeded randomized dungeon /// </summary> /// <param name="seed"></param> public void Generate(int seed) { mNodes = new List <Node>(Width * Length * Floors); mEdges = new List <CellEdge>(Width * Length * Floors * 3 - Width - Length - Floors); InitializeEdges(); Random rnd = new Random(seed); //Use this list for fetching walls along nodes and picking node walls at random List <CellEdge> nodeWalls = new List <CellEdge>(); List <NeighborCell> neighborCells = new List <NeighborCell>(); //Backtracking stack List <Node> nodeStack = new List <Node>(); //Also, remember which 'cells' are occupied, and which node //Since a node can vary in size, it may occupy multiple cells Node[,,] nodeGrid = new Node[Width, Length, Floors]; //Create start node Node startNode = new Node(rnd.Next(Width), rnd.Next(Length), rnd.Next(Floors), 1, 1); GetCellEdges(startNode, nodeWalls); SetEdges(nodeWalls, CellEdgeType.Unused, CellEdgeType.Wall); PlaceNodeOnGrid(nodeGrid, startNode); mNodes.Add(startNode); Node currentNode = startNode; bool mazeFilled = false; while (!mazeFilled) { neighborCells.Clear(); nodeWalls.Clear(); FindVacantNeighborCells(nodeGrid, currentNode, neighborCells); if (neighborCells.Count > 0) { //Pick an empty space NeighborCell target = neighborCells[rnd.Next(neighborCells.Count)]; //...create an opening to this new space CellEdge openEdge = target.SharedEdge; openEdge.ConnectionType = CellEdgeType.Door; //...and create another node on the other side Node newNode = null; while (newNode == null) { NodeConfiguration size = PickRandomNodeConfiguration(rnd); newNode = TryNodePlacement(nodeGrid, rnd, target.X, target.Y, target.Z, size.Width, size.Length); } GetCellEdges(newNode, nodeWalls); SetEdges(nodeWalls, CellEdgeType.Unused, CellEdgeType.Wall); //Move into the new room, push old room onto stack nodeStack.Add(currentNode); currentNode = newNode; } else { //Else, pop back to previous room, and find new walls if (nodeStack.Count > 0) { currentNode = nodeStack[nodeStack.Count - 1]; nodeStack.RemoveAt(nodeStack.Count - 1); } else { //If there are no new places to go to, the maze is filled mazeFilled = true; } } } Console.WriteLine("Created " + mNodes.Count + " node dungeon."); }