Advertisement
James2250

Recursive Division Algorithm (Maze)

Jun 22nd, 2014
821
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.26 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections;
  3.  
  4. public class Maze : MonoBehaviour
  5. {
  6.     public int Total_Width = 100;
  7.     public int Total_Height = 100;
  8.  
  9.     public Vector3 Origin = Vector3.zero;
  10.     public GameObject Wall;
  11.     public GameObject BorderWall;
  12.  
  13.     private enum Direction
  14.     {
  15.     HORIZONTAL,
  16.     VERTICAL
  17.     }
  18.  
  19.     //Returns direction depending if width or height are larger
  20.     Direction getDirection(int width, int height)
  21.     {
  22.         if (width == height)
  23.         {
  24.             int x = Random.Range(0, 2);
  25.  
  26.             if (x == 0)
  27.                 return Direction.VERTICAL;
  28.  
  29.             else
  30.                 return Direction.HORIZONTAL;
  31.         }
  32.  
  33.         return (width > height ? Direction.VERTICAL : Direction.HORIZONTAL);
  34.     }
  35.  
  36.     //Only for graphic effect
  37.     void AddBorder()
  38.     {
  39.         for (int i = 0; i < Total_Width; i++)
  40.         {
  41.             for (int j = 0; j < Total_Height; j++)
  42.             {
  43.                 if (i == Total_Width - 1 || j == Total_Height - 1 || i == 0 || j == 0)
  44.                     Instantiate(BorderWall, new Vector3(i, 0, j), Quaternion.identity);
  45.             }
  46.         }
  47.     }
  48.  
  49.     void Start()
  50.     {
  51.         AddBorder();
  52.         Divide(0, 0, Total_Width, Total_Height, getDirection(Total_Width, Total_Height), 0);
  53.     }
  54.  
  55.     void HandleVertical(int x, int y, int width, int height, Direction d, int index)
  56.     {
  57.         int NextPos = Random.Range(x + 1, x + width - 1);   // Random place to "add wall"
  58.         int Hole = Random.Range(y + 1, y + height - 1);     // Making 1 block space in that wall
  59.  
  60.         for (int i = y + 1; i < (y + height - 1); i++)
  61.         {
  62.             if (i != Hole)
  63.             {
  64.                 Instantiate(Wall, new Vector3(i, 0, NextPos), Quaternion.identity);  // Create wall block
  65.             }
  66.         }
  67.  
  68.         Divide(x, y, NextPos - x, height, d, index);
  69.         Divide(NextPos, y, x + width - NextPos, height, d, index);
  70.     }
  71.  
  72.     void HandleHorizontal(int x, int y, int width, int height, Direction d, int index)
  73.     {
  74.         int NextPos = Random.Range(y + 1, y + height - 1);  // Random place to "add wall"
  75.         int Hole = Random.Range(x + 1, x + width - 1);  // Making 1 block space in that wall
  76.  
  77.         for (int i = x + 1; i < (x + width - 1); i++)
  78.         {
  79.             if (i != Hole)
  80.             {
  81.                 Instantiate(Wall, new Vector3(NextPos, 0, i), Quaternion.identity);  // Create wall block
  82.             }
  83.         }
  84.  
  85.         Divide(x, y, width, NextPos - y, d, index);
  86.         Divide(x, NextPos, width, y + height - NextPos, d, index);
  87.     }
  88.  
  89.     // Main recursive function  
  90.     void Divide(int x, int y, int width, int height, Direction dir, int index)
  91.     {
  92.         Direction d = getDirection(width, height);
  93.         index++;
  94.  
  95.         if (index > 20)
  96.         {
  97.             Debug.Log("Stack Overflow Warning");
  98.             return;
  99.         }
  100.  
  101.         if (width <= 1 || height <= 1)   // Room size
  102.         {
  103.             return;
  104.         }
  105.  
  106.          if (d == Direction.VERTICAL)
  107.          {
  108.              HandleVertical(x, y, width, height, d, index);
  109.          }
  110.  
  111.          else if (d == Direction.HORIZONTAL)
  112.          {
  113.              HandleHorizontal(x, y, width, height, d, index);
  114.          }
  115.  
  116.     }
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement