private void InitializeNodeGrid() { nodeGrid = new Node[][] { new Node[outerRowCellCount], new Node[middleRowCellCount], new Node[outerRowCellCount], }; gridIsFat = (middleRowCellCount > outerRowCellCount); numberOfDataLeft = dataNodeCount; numberOfTreasureLeft = levelSettings.TreasureNodeCount; numberOfFirewallLeft = levelSettings.FirewallNodeCount; numberOfSpamLeft = levelSettings.SpamNodeCount; emptyLeft = 2 * outerRowCellCount + middleRowCellCount - 1 - levelSettings.NodeCount; // We place the start node and one of the treasure nodes on opposite edges of the grid in order to hopefully make all the nodes and connections useful. Cell[] leftCells; if (gridIsFat) { leftCells = new Cell[] { new Cell(row: 0, column: 0), new Cell(row: 1, column: 0), new Cell(row: 2, column: 0) }; } else { leftCells = new Cell[] { new Cell(row: 0, column: 0), new Cell(row: 2, column: 0) }; } startCell = NetworkAlgorithms.RandomFrom(leftCells); nodeGrid[startCell.row][startCell.column] = new Node(NodeType.START); switch (startCell.row) { case 0: endCell = new Cell(row: 2, column: outerRowCellCount - 1); break; case 1: Cell[] rightCells = new Cell[] { new Cell(row: 0, column: outerRowCellCount - 1), new Cell(row: 1, column: outerRowCellCount - 1), new Cell(row: 2, column: outerRowCellCount - 1) }; endCell = NetworkAlgorithms.RandomFrom(rightCells); break; case 2: endCell = new Cell(row: 0, column: outerRowCellCount - 1); break; default: endCell = new Cell(row: 0, column: 0); break; } nodeGrid[endCell.row][endCell.column] = new Node(NodeType.TREASURE); numberOfTreasureLeft--; }
private void PopulateCell(int row, int column) { // Skip the grid cell if it is already accupied by the start or treasure nodes // (which we placed when we initialized the grid) if ((row == startCell.row && column == startCell.column) || (row == endCell.row && column == endCell.column)) { return; } // Choose a node type for this grid cell. Try to not put treasure or firewall next to start node if possible. int choice; if (NetworkAlgorithms.AreNeighbours(gridIsFat, row, column, startCell.row, startCell.column) && (emptyLeft + numberOfDataLeft + numberOfSpamLeft) > 0) { choice = Random.Range(0, emptyLeft + numberOfDataLeft + numberOfSpamLeft); } else { choice = Random.Range(0, emptyLeft + numberOfDataLeft + numberOfSpamLeft + numberOfFirewallLeft + numberOfTreasureLeft); } if (choice < emptyLeft) { nodeGrid[row][column] = new Node(NodeType.EMPTY); emptyLeft--; } else if (choice < emptyLeft + numberOfDataLeft) { nodeGrid[row][column] = new Node(NodeType.DATA); numberOfDataLeft--; } else if (choice < emptyLeft + numberOfDataLeft + numberOfSpamLeft) { nodeGrid[row][column] = new Node(NodeType.SPAM); numberOfSpamLeft--; } else if (choice < emptyLeft + numberOfDataLeft + numberOfSpamLeft + numberOfFirewallLeft) { nodeGrid[row][column] = new Node(NodeType.FIREWALL); numberOfFirewallLeft--; } else { nodeGrid[row][column] = new Node(NodeType.TREASURE); numberOfTreasureLeft--; } }
private void MakeConnections() { connections = new List <Connection>(); int minColumns = gridIsFat ? outerRowCellCount : middleRowCellCount; Node previousTop = nodeGrid[0][0]; Node previousMiddle = nodeGrid[1][0]; Node previousBottom = nodeGrid[2][0]; Node top = nodeGrid[0][0]; Node middle = nodeGrid[1][0]; Node bottom = nodeGrid[2][0]; // Connecting all cells in first column connections.Add(new Connection(top, middle)); connections.Add(new Connection(middle, bottom)); for (int currentColumn = 0; currentColumn < minColumns; currentColumn++) { top = nodeGrid[0][currentColumn]; middle = nodeGrid[1][currentColumn]; bottom = nodeGrid[2][currentColumn]; List <int> rows = new List <int>() { 0, 1, 2 }; List <int> connectingNodes = rows .Where(row => NodeType.FIREWALL != nodeGrid[row][currentColumn].Type && NodeType.EMPTY != nodeGrid[row][currentColumn].Type) .ToList <int>(); void ConnectRowWise(int row) { //TODO Check if previous nodes were empty before connecting Node previous; switch (row) { case 0: previous = gridIsFat ? previousTop : NetworkAlgorithms.RandomFrom(new Node[] { previousTop, previousMiddle }); connections.Add(new Connection(previous, top)); break; case 1: previous = gridIsFat ? NetworkAlgorithms.RandomFrom(new Node[] { previousTop, previousMiddle, previousBottom }) : previousMiddle; connections.Add(new Connection(previous, middle)); break; case 2: previous = gridIsFat ? previousTop : NetworkAlgorithms.RandomFrom(new Node[] { previousMiddle, previousBottom }); connections.Add(new Connection(previous, bottom)); break; } } // Connecting previous to current column at least once int gauranteedConnectionRow = NetworkAlgorithms.RandomFrom(connectingNodes.ToArray()); ConnectRowWise(gauranteedConnectionRow); foreach (int connectingNode in connectingNodes) { if (connectingNode != gauranteedConnectionRow) { if (Random.Range(0, chanceToConnectDenomminator) == 0) { ConnectRowWise(connectingNode); } } } // Connect column wise if (connectingNodes.Contains(0) && connectingNodes.Contains(1)) { if (Random.Range(0, chanceToConnectDenomminator) == 0) { connections.Add(new Connection(top, middle)); } } if (connectingNodes.Contains(1) && connectingNodes.Contains(2)) { if (Random.Range(0, chanceToConnectDenomminator) == 0) { connections.Add(new Connection(middle, bottom)); } } // Connect firewall if present in column List <int> firewallNodes = rows .Where(row => NodeType.FIREWALL != nodeGrid[row][currentColumn].Type && NodeType.EMPTY != nodeGrid[row][currentColumn].Type) .ToList <int>(); foreach (int firewallRow in firewallNodes) { ConnectRowWise(firewallRow); } previousTop = top; previousMiddle = middle; previousBottom = bottom; } // Connect last column if (gridIsFat) { middle = nodeGrid[1][middleRowCellCount - 1]; Node previous = NetworkAlgorithms.RandomFrom(new Node[] { previousTop, previousMiddle, previousBottom }); connections.Add(new Connection(previous, middle)); } else { top = nodeGrid[0][outerRowCellCount - 1]; Node previous = NetworkAlgorithms.RandomFrom(new Node[] { previousTop, previousMiddle }); connections.Add(new Connection(previous, top)); bottom = nodeGrid[2][outerRowCellCount - 1]; previous = NetworkAlgorithms.RandomFrom(new Node[] { previousMiddle, previousBottom }); connections.Add(new Connection(previous, bottom)); } }