Advertisement
NickM13

Maze DSF Algorithm (CC)

Jun 1st, 2015
304
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.55 KB | None | 0 0
  1. --[[
  2.  
  3. Generates a random map based on Depth-First Search,
  4. Displays on ComputerCraft advanced computer
  5.  
  6. ]]--
  7.  
  8. args = {...}
  9.  
  10. width = 10
  11. height = 9
  12.  
  13. for i,v in ipairs(args) do
  14.     print(v)
  15.     if tonumber(v) ~= nil then
  16.         if i == 1 then width = math.abs(tonumber(v))
  17.         elseif i == 2 then height = math.abs(tonumber(v))
  18.         end
  19.     end
  20. end
  21.  
  22. cellvisit = {}
  23. for i = 1, width do
  24.     cellvisit[i] = {}
  25.     for j=1, height do
  26.         cellvisit[i][j] = 0
  27.     end
  28. end
  29.  
  30. cellwall = {}
  31. for i=1, width do
  32.     cellwall[i] = {}
  33.     for j=1, height*2-1 do
  34.         cellwall[i][j] = 0
  35.     end
  36. end
  37.  
  38. cCell = {}
  39. cCell.x = 1
  40. cCell.y = 1
  41.  
  42. walls = {}
  43. wallsaround = 0
  44.  
  45. local function delay()
  46.     os.sleep(0.002)
  47. end
  48.  
  49. local function setWalls()
  50.     walls = 0
  51.     wallsaround = 0
  52.     if cCell.y == 1 then --up
  53.         walls = walls + 1
  54.         wallsaround = wallsaround+1
  55.     elseif cellvisit[cCell.x][cCell.y-1] == 1 then
  56.         walls = walls + 1
  57.         wallsaround = wallsaround+1
  58.     end
  59.     if cCell.x == width then --right
  60.         walls = walls + 2
  61.         wallsaround = wallsaround+1
  62.     elseif cellvisit[cCell.x+1][cCell.y] == 1 then
  63.         walls = walls + 2
  64.         wallsaround = wallsaround+1
  65.     end
  66.     if cCell.y == height then --down
  67.         walls = walls + 4
  68.         wallsaround = wallsaround+1
  69.     elseif cellvisit[cCell.x][cCell.y+1] == 1 then
  70.         walls = walls + 4
  71.         wallsaround = wallsaround+1
  72.     end
  73.     if cCell.x == 1 then --left
  74.         walls = walls + 8
  75.         wallsaround = wallsaround+1
  76.     elseif cellvisit[cCell.x-1][cCell.y] == 1 then
  77.         walls = walls + 8
  78.         wallsaround = wallsaround+1
  79.     end
  80. end
  81.  
  82. local function render()
  83.     term.setBackgroundColor(colors.black)
  84.     term.clear()
  85.    
  86.     for x=1, width do
  87.         for y=1, height do
  88.             term.setCursorPos(x*2-1, y*2-1)
  89.             if cellvisit[x][y] == 1 then term.setBackgroundColor(colors.white)
  90.             else term.setBackgroundColor(colors.white) end
  91.             term.write(" ")
  92.         end
  93.     end
  94.    
  95.     for x=1, width do
  96.         for y=1, height*2 do
  97.             term.setCursorPos(x*2+(y%2)-1, y)
  98.             if cellwall[x][y] == 1 then
  99.                 term.setBackgroundColor(colors.white)
  100.             else
  101.                 term.setBackgroundColor(colors.black)
  102.             end
  103.                 term.write(" ")
  104.         end
  105.     end
  106.     term.setBackgroundColor(colors.orange)
  107.     term.setCursorPos(cCell.x*2-1, cCell.y*2-1)
  108.     term.write(" ")
  109.     delay()
  110. end
  111.  
  112. path = {}--path rotation direction, 1 = up, 2 = right, 3 = down, 4 = left
  113. pathdist = 0
  114. exitting = false
  115.  
  116. local function backtrack()
  117.     if pathdist > 1 then
  118.         if path[pathdist] == 1 then cCell.y = cCell.y + 1 end
  119.         if path[pathdist] == 2 then cCell.x = cCell.x - 1 end
  120.         if path[pathdist] == 3 then cCell.y = cCell.y - 1 end
  121.         if path[pathdist] == 4 then cCell.x = cCell.x + 1 end
  122.     end
  123.     pathdist = pathdist - 1
  124. end
  125.  
  126. local function foretrack(rotation)
  127.     pathdist = pathdist + 1
  128.     path[pathdist] = rotation
  129. end
  130.  
  131. tempcell = {}
  132. setWalls()
  133. repeat
  134.     setWalls()
  135.     direction = math.random(4)
  136.     notwall = bit.band(bit.brshift(walls, direction-1), 1)
  137.     wallschecked = 0
  138.     --print(wallsaround.." "..wallschecked)
  139.     cellvisit[cCell.x][cCell.y] = 1
  140.     if wallsaround >= 4 then
  141.         backtrack()
  142.     else
  143.         checkwalls = {}
  144.         index = 0
  145.         for i=1, 4 do
  146.             if (bit.brshift(walls, i-1))%2 == 0 then
  147.                 index = index + 1
  148.                 checkwalls[index] = i
  149.             end
  150.         end
  151.         for i=1, 4-wallsaround do
  152.             if index <= 0 then
  153.                 print("something went wrong with indexing")
  154.                 exitting = true
  155.                 break
  156.             end
  157.             direction = checkwalls[math.random(index)]
  158.             notwall = bit.band(bit.brshift(walls, direction-1), 1)
  159.             walloffset = {}
  160.             if notwall == 0 then
  161.                 tempcell.x = cCell.x
  162.                 tempcell.y = cCell.y
  163.                 walloffset.x = 0  walloffset.y = 0
  164.                 if direction == 1 then tempcell.y = tempcell.y - 1 walloffset.y = 1 end --up
  165.                 if direction == 2 then tempcell.x = tempcell.x + 1 walloffset.x = 0 end --right
  166.                 if direction == 3 then tempcell.y = tempcell.y + 1 walloffset.y = 0 end --down
  167.                 if direction == 4 then tempcell.x = tempcell.x - 1 walloffset.x = 1 end --left
  168.                 --print("cell movement: "..tempcell.x.."   "..tempcell.y.."   "..direction.."   "..walls)
  169.                 if cellvisit[tempcell.x][tempcell.y] == 0 then
  170.                     if direction == 2 or direction == 4 then
  171.                         cellwall[cCell.x-walloffset.x][cCell.y*2-1] = 1
  172.                     else
  173.                         cellwall[cCell.x][(cCell.y-walloffset.y)*2] = 1
  174.                     end
  175.                     cCell.x = tempcell.x
  176.                     cCell.y = tempcell.y
  177.                     cellvisit[cCell.x][cCell.y] = 1
  178.                     foretrack(direction)
  179.                     break
  180.                 end
  181.             end
  182.             wallschecked = wallschecked + bit.blshift(1, direction-1)
  183.             if wallschecked >= 15 then
  184.                 break
  185.             end
  186.             delay()
  187.         end
  188.     end
  189.     render()
  190.     delay()
  191. until (pathdist == 0 and walls == 15 or exitting)
  192. cCell.x = 1
  193. cCell.y = 1
  194. render()
  195. term.setCursorPos(1, 19)
  196. io.write("Maze complete!")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement