eniallator

Maze Generator

Aug 2nd, 2016
357
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.67 KB | None | 0 0
  1. local mazeGrid = {}
  2. local currCoords = {x = 1, y = 1}
  3. local currentIndex = 2
  4. local wallThickness = 1
  5.  
  6. local function chooseNearby()
  7.   local possDir = {}
  8.  
  9.   if mazeGrid[currCoords.x + wallThickness + 1] and mazeGrid[currCoords.x + wallThickness + 1][currCoords.y] == " " then
  10.     table.insert(possDir, "E")
  11.   end
  12.  
  13.   if mazeGrid[currCoords.x - wallThickness - 1] and mazeGrid[currCoords.x - wallThickness - 1][currCoords.y] == " " then
  14.     table.insert(possDir, "W")
  15.   end
  16.  
  17.   if mazeGrid[currCoords.x][currCoords.y + wallThickness + 1] and mazeGrid[currCoords.x][currCoords.y + wallThickness + 1] == " " then
  18.     table.insert(possDir, "S")
  19.   end
  20.  
  21.   if mazeGrid[currCoords.x][currCoords.y - wallThickness - 1] and mazeGrid[currCoords.x][currCoords.y - wallThickness - 1] == " " then
  22.     table.insert(possDir, "N")
  23.   end
  24.  
  25.   if possDir[1] then
  26.     return possDir[math.random(#possDir)]
  27.   end
  28.  
  29.   return false
  30. end
  31.  
  32. local function goToNearby(dir)
  33.   if dir == "E" then
  34.     for i=1, wallThickness do
  35.       mazeGrid[currCoords.x + i][currCoords.y] = 0
  36.     end
  37.  
  38.     currCoords.x = currCoords.x + wallThickness + 1
  39.  
  40.   elseif dir == "W" then
  41.     for i=1, wallThickness do
  42.       mazeGrid[currCoords.x - i][currCoords.y] = 0
  43.     end
  44.  
  45.     currCoords.x = currCoords.x - wallThickness - 1
  46.  
  47.   elseif dir == "S" then
  48.     for i=1, wallThickness do
  49.       mazeGrid[currCoords.x][currCoords.y + i] = 0
  50.     end
  51.  
  52.     currCoords.y = currCoords.y + wallThickness + 1
  53.  
  54.   else
  55.     for i=1, wallThickness do
  56.       mazeGrid[currCoords.x][currCoords.y - i] = 0
  57.     end
  58.  
  59.     currCoords.y = currCoords.y - wallThickness - 1
  60.   end
  61.  
  62.   mazeGrid[currCoords.x][currCoords.y] = currentIndex
  63.   currentIndex = currentIndex + 1
  64. end
  65.  
  66. local function createRoute()
  67.   while true do
  68.     chosenDir = chooseNearby()
  69.  
  70.     if chosenDir then
  71.       goToNearby(chosenDir)
  72.  
  73.     else
  74.       break
  75.     end
  76.   end
  77. end
  78.  
  79. local function choosePrevious()
  80.   local dir
  81.  
  82.   if mazeGrid[currCoords.x + wallThickness + 1] and mazeGrid[currCoords.x][currCoords.y] - 1 == mazeGrid[currCoords.x + wallThickness + 1][currCoords.y] then
  83.     dir = "E"
  84.  
  85.   elseif mazeGrid[currCoords.x - wallThickness - 1] and mazeGrid[currCoords.x][currCoords.y] - 1 == mazeGrid[currCoords.x - wallThickness - 1][currCoords.y] then
  86.     dir = "W"
  87.  
  88.   elseif mazeGrid[currCoords.x][currCoords.y + wallThickness + 1] and mazeGrid[currCoords.x][currCoords.y] - 1 == mazeGrid[currCoords.x][currCoords.y + wallThickness + 1] then
  89.     dir = "S"
  90.  
  91.   elseif mazeGrid[currCoords.x][currCoords.y - wallThickness - 1] and mazeGrid[currCoords.x][currCoords.y] - 1 == mazeGrid[currCoords.x][currCoords.y - wallThickness - 1] then
  92.     dir = "N"
  93.   end
  94.  
  95.   return dir
  96. end
  97.  
  98. local function goToPrevious(dir)
  99.   if dir == "E" then
  100.     currCoords.x = currCoords.x + wallThickness + 1
  101.  
  102.   elseif dir == "W" then
  103.     currCoords.x = currCoords.x - wallThickness - 1
  104.  
  105.   elseif dir == "S" then
  106.     currCoords.y = currCoords.y + wallThickness + 1
  107.  
  108.   else
  109.     currCoords.y = currCoords.y - wallThickness - 1
  110.   end
  111. end
  112.  
  113. local function backTrack()
  114.   while not chooseNearby() and (currCoords.x ~= 1 or currCoords.y ~= 1) do
  115.     goToPrevious(choosePrevious())
  116.   end
  117. end
  118.  
  119. function createMaze(sizeX,sizeY,seed,newWallThickness)
  120.  
  121.   if seed then
  122.     math.randomseed(seed)
  123.   end
  124.  
  125.   if newWallThickness then
  126.     wallThickness = newWallThickness
  127.   end
  128.  
  129.   for x=1,sizeX do
  130.     mazeGrid[x] = {}
  131.  
  132.     for y=1,sizeY do
  133.       mazeGrid[x][y] = " "
  134.     end
  135.   end
  136.  
  137.   mazeGrid[1][1] = 1
  138.  
  139.   repeat
  140.     createRoute()
  141.     backTrack()
  142.     currentIndex = mazeGrid[currCoords.x][currCoords.y] + 1
  143.  
  144.   until currCoords.x == 1 and currCoords.y == 1
  145.   return mazeGrid
  146. end
Advertisement
Add Comment
Please, Sign In to add comment