Advertisement
CaptainSpaceCat

3D Branching Maze Generator

Aug 7th, 2015
286
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.08 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4.  
  5. public class MazeGenBranching : MonoBehaviour {
  6.    
  7.     List<Vector3> stack;
  8.     List<Vector3> availableCells;
  9.  
  10.     public bool active;
  11.    
  12.     [RangeAttribute(5, 15)]
  13.     public int width, height, depth;
  14.     bool[,,,] maze;
  15.     private Vector3 nullVector = new Vector3(-1, -1, -1);
  16.     //lets make it:   up - Y axis, forward - Z axis, and sideways - X axis
  17.     float[,] cellScan = new float[6, 3] {{0.0f, 1.0f, 0.0f}, {0.0f, 0.0f, 1.0f}, {1.0f, 0.0f, 0.0f}, {0.0f, 0.0f, -1.0f}, {-1.0f, 0.0f, 0.0f}, {0.0f, -1.0f, 0.0f}};
  18.     //up              //forward            //right              //back               //left                //down
  19.  
  20.     public void Activate(int w, int h, int d) {
  21.         width = w;
  22.         height = h;
  23.         depth = d;
  24.         if (active) {
  25.             Debug.Log ("Initializing...");
  26.             InitializeMaze ();
  27.             Debug.Log ("Generating...");
  28.             GenerateMaze ();
  29.             Debug.Log ("Displaying...");
  30.  
  31.             MazeSpawner mazeSpawner = GetComponent<MazeSpawner>();
  32.             mazeSpawner.InstantiateMaze (maze);
  33.         }
  34.     }
  35.    
  36.     void InitializeMaze() {
  37.         maze = new bool[width,height,depth,6];
  38.         for (int x = 0; x < width; x++) {
  39.             for (int z = 0; z < depth; z++) {
  40.                 for (int y = 0; y < height; y++) {
  41.                     for (int n = 0; n < 6; n++) {
  42.                         maze[x,y,z,n] = true; //[x][z][y][n]
  43.                     } //top-1, front-2, right-3, back-4, left-5, bottom-6
  44.                 }
  45.             }
  46.         }
  47.     }
  48.    
  49.     //[x][z][y][1 - 6] those 6 correspond to the walls
  50.    
  51.     void GenerateMaze() {
  52.         stack = new List<Vector3>();
  53.         int remainingCells = width * height * depth - 1;
  54.         Vector3 current = new Vector3 (Random.Range (1, width - 2), Random.Range (1, height - 2), Random.Range (1, depth - 2));
  55.         stack.Insert (stack.Count, current);
  56.  
  57.         while (remainingCells > 0) {
  58.             current = stack[Random.Range (0, stack.Count - 1)];
  59.             Vector3 choice = ScanAdjacentCells(current);
  60.             //Debug.Log (choice);
  61.             if (choice != nullVector) {
  62.                 RemoveConnectingWalls(current, choice);
  63.                 stack.Insert (stack.Count, choice);
  64.                 remainingCells --;
  65.             } else {
  66.                 stack.Remove (current);
  67.             }
  68.         }
  69.     }
  70.    
  71.     Vector3 ScanAdjacentCells(Vector3 currentPos) {
  72.         availableCells = new List<Vector3>();
  73.         for (int i = 0; i < 6; i ++) {
  74.             Vector3 cellToScan = new Vector3();
  75.             cellToScan.x = currentPos.x + cellScan[i,0];
  76.             cellToScan.y = currentPos.y + cellScan[i,1];
  77.             cellToScan.z = currentPos.z + cellScan[i,2];
  78.             if (cellToScan.x >= 0 && cellToScan.x < width && cellToScan.y >= 0 && cellToScan.y < height && cellToScan.z >= 0 && cellToScan.z < depth) {
  79.                 bool cellOpen = ScanCell (cellToScan);
  80.                 if (cellOpen) {
  81.                     availableCells.Add (cellToScan);
  82.                 }
  83.             }
  84.         }
  85.         if (availableCells.Count > 0) {
  86.             int selection = Random.Range (0, availableCells.Count - 1);
  87.             return availableCells[selection];
  88.         }
  89.         return nullVector;
  90.     }
  91.    
  92.     bool ScanCell(Vector3 cellPos) {
  93.         bool flag = true;
  94.         for (int i = 0; i < 6; i ++) {
  95.             if (!(maze[(int)cellPos.x,(int)cellPos.y,(int)cellPos.z,i])) {
  96.                 flag = false;
  97.                 break;
  98.             }
  99.         }
  100.         return flag;
  101.     }
  102.    
  103.     void RemoveConnectingWalls(Vector3 cell1, Vector3 cell2) {
  104.         Vector3 difference = cell2 - cell1;
  105.         if (difference.y == 1) {          //up
  106.             maze[(int)cell1.x,(int)cell1.y,(int)cell1.z,0] = false;
  107.             maze[(int)cell2.x,(int)cell2.y,(int)cell2.z,5] = false;
  108.         } else if (difference.z == 1) {   //forward
  109.             maze[(int)cell1.x,(int)cell1.y,(int)cell1.z,1] = false;
  110.             maze[(int)cell2.x,(int)cell2.y,(int)cell2.z,3] = false;
  111.         } else if (difference.x == 1) {   //right
  112.             maze[(int)cell1.x,(int)cell1.y,(int)cell1.z,2] = false;
  113.             maze[(int)cell2.x,(int)cell2.y,(int)cell2.z,4] = false;
  114.         } else if (difference.z == -1) {  //back
  115.             maze[(int)cell1.x,(int)cell1.y,(int)cell1.z,3] = false;
  116.             maze[(int)cell2.x,(int)cell2.y,(int)cell2.z,1] = false;
  117.         } else if (difference.x == -1) {  //left
  118.             maze[(int)cell1.x,(int)cell1.y,(int)cell1.z,4] = false;
  119.             maze[(int)cell2.x,(int)cell2.y,(int)cell2.z,2] = false;
  120.         } else if (difference.y == -1) {  //down
  121.             maze[(int)cell1.x,(int)cell1.y,(int)cell1.z,5] = false;
  122.             maze[(int)cell2.x,(int)cell2.y,(int)cell2.z,0] = false;
  123.         }
  124.     }
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement