Advertisement
Guest User

Maze Generator by nimaid

a guest
Jul 23rd, 2017
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.88 KB | None | 0 0
  1. -- A maze generator using the recursive backtracker method.
  2. -- Made by nimaid
  3. -- Maze will start 1 block in front of the turtle.
  4. -- It will build in the horizontal direction (right) (2 * width) + 1 blocks
  5. -- It will build in the vertical direction (forward) (2 * height) + 1 blocks
  6. -- Make sure the turtle is fueled.
  7. -- It will hang when it runs out of materials, and will resume when given more.
  8. -- Make sure the space is clear for 1 block up and to the sides of the final sized maze.
  9. -- Keep mobs away from the building turtle.
  10.  
  11. turtle.select(1)
  12.  
  13. function select_filled_slot()
  14.   while turtle.getItemCount() == 0 do
  15.     if turtle.getSelectedSlot() == 16 then
  16.       turtle.select(1)
  17.     else
  18.       turtle.select(turtle.getSelectedSlot() + 1)
  19.     end
  20.   end
  21. end
  22.  
  23. turtle.refuel()
  24. write("\nHorizontal cells: ")
  25. width = tonumber(read())
  26. write("Vertical cells: ")
  27. height = tonumber(read())
  28. write("Maze Height: ")
  29. vheight = tonumber(read())
  30. print("\nCreating initial grid...")
  31.  
  32. turtle.forward()
  33. turtle.turnLeft()
  34. turtle.turnLeft()
  35.  
  36. for j = 0, (width * 2) do
  37.   for i = 0, (height * 2) do
  38.     select_filled_slot()
  39.     turtle.back()
  40.    
  41.     if j % 2 == 1 then
  42.       if i % 2 == 0 then
  43.         select_filled_slot()
  44.         turtle.place()
  45.       end
  46.     else
  47.       turtle.place()
  48.     end
  49.   end
  50.  
  51.   if j % 2 == 0 then
  52.     turtle.turnLeft()
  53.     turtle.forward()
  54.     turtle.turnLeft()
  55.     turtle.back()
  56.   else
  57.     turtle.turnRight()
  58.     turtle.forward()
  59.     turtle.turnRight()
  60.     turtle.back()
  61.   end
  62. end
  63.  
  64. turtle.back()
  65. turtle.turnLeft()
  66. turtle.dig()
  67. turtle.forward()
  68. turtle.forward()
  69.  
  70. direction = 4
  71.  
  72. function face_dir(dir)
  73.   while direction ~= dir do
  74.     if direction == 1 and dir == 4 then
  75.       turtle.turnLeft()
  76.       direction = 4
  77.     elseif direction == 4 and dir == 1 then
  78.       turtle.turnRight()
  79.       direction = 1
  80.     elseif dir < direction then
  81.       turtle.turnLeft()
  82.       if direction == 1 then
  83.         direction = 4
  84.       else
  85.         direction = direction - 1
  86.       end
  87.     else
  88.       turtle.turnRight()
  89.       if direction == 4 then
  90.         direction = 1
  91.       else
  92.         direction = direction + 1
  93.       end
  94.     end
  95.   end
  96. end
  97.  
  98. stack = {}
  99.  
  100. cell_visited = {}
  101. for i = 1, width do
  102.   cell_visited[i] = {}
  103.   for j = 1, height do
  104.     cell_visited[i][j] = false
  105.   end
  106. end
  107.  
  108. cell_visited[width][height] = true
  109.  
  110. current_position = {width, height}
  111.  
  112. function update_position(dir)
  113.   if dir == 1 then
  114.     current_position[2] = current_position[2] + 1
  115.   elseif dir == 2 then
  116.     current_position[1] = current_position[1] + 1
  117.   elseif dir == 3 then
  118.     current_position[2] = current_position[2] - 1
  119.   elseif dir == 4 then
  120.     current_position[1] = current_position[1] - 1
  121.   end
  122. end
  123.  
  124. function move_dir(dir)
  125.   face_dir(dir)
  126.   turtle.dig()
  127.   turtle.forward()
  128.   turtle.forward()
  129.  
  130.   table.insert(stack, dir)
  131.  
  132.   update_position(dir)
  133.  
  134.   cell_visited[current_position[1]][current_position[2]] = true
  135. end
  136.  
  137. function can_move(dir)
  138.   if dir == 1 then
  139.     if current_position[2] == height then
  140.       return false
  141.     elseif cell_visited[current_position[1]][current_position[2] + 1] then
  142.       return false
  143.     else
  144.       return true
  145.     end
  146.   elseif dir == 2 then
  147.     if current_position[1] == width then
  148.       return false
  149.     elseif cell_visited[current_position[1] + 1][current_position[2]] then
  150.       return false
  151.     else
  152.       return true
  153.     end
  154.   elseif dir == 3 then
  155.     if current_position[2] == 1 then
  156.       return false
  157.     elseif cell_visited[current_position[1]][current_position[2] - 1] then
  158.       return false
  159.     else
  160.       return true
  161.     end
  162.   elseif dir == 4 then
  163.     if current_position[1] == 1 then
  164.       return false
  165.     elseif cell_visited[current_position[1] - 1][current_position[2]] then
  166.       return false
  167.     else
  168.       return true
  169.     end
  170.   else
  171.     return false
  172.   end
  173. end
  174.  
  175. function viable_directions()
  176.   local dirs = {}
  177.  
  178.   for i = 1, 4 do
  179.     if can_move(i) then
  180.       table.insert(dirs, i)
  181.     end
  182.   end
  183.  
  184.   return dirs
  185. end
  186.  
  187. function move_back()
  188.   if #stack == 0 then
  189.     return false
  190.   end
  191.    
  192.   local orig_dir = table.remove(stack)
  193.  
  194.   face_dir(orig_dir)
  195.   turtle.back()
  196.   turtle.back()
  197.  
  198.   local rev_dir = orig_dir + 2
  199.   while rev_dir > 4 do
  200.     rev_dir = rev_dir - 4
  201.   end
  202.  
  203.   update_position(rev_dir)
  204.   return true
  205. end
  206.  
  207. math.randomseed(os.time())
  208.  
  209. made_entrance = false
  210.  
  211. function step()
  212.   if current_position[1] == 1 and current_position[2] == 1 and not made_entrance then
  213.     face_dir(3)
  214.     turtle.dig()
  215.     made_entrance = true
  216.     end
  217.   possible_dirs = viable_directions()
  218.   if #possible_dirs > 0 then
  219.     local dir_to_move = possible_dirs[math.random(#possible_dirs)]
  220.     print("Moving in direction ", dir_to_move)
  221.     move_dir(dir_to_move)
  222.     return true
  223.   else
  224.     print("Can't move anywhere.")
  225.     if not move_back() then
  226.       print("\nMaze generation is done.")
  227.       return false
  228.     else
  229.       print("Backtracked a step")
  230.       return true
  231.     end
  232.   end
  233. end
  234.  
  235. curr_step = 0
  236. while step() do
  237.   curr_step = curr_step + 1
  238.   print("\nStep: ", curr_step)
  239. end
  240.  
  241. face_dir(2)
  242. turtle.forward()
  243. turtle.up()
  244. face_dir(3)
  245. turtle.back()
  246.  
  247. function extend_up_step()
  248.   select_filled_slot()
  249.   if turtle.detectDown() then
  250.     for x = 2, vheight do
  251.       turtle.up()
  252.       turtle.placeDown()
  253.     end
  254.    
  255.     turtle.forward()
  256.    
  257.     for x = 2, vheight do
  258.       turtle.down()
  259.     end
  260.   else
  261.     turtle.forward()
  262.   end
  263. end
  264.  
  265. print("\nExtending walls upwards...")
  266.  
  267. face_dir(3)
  268. for i = 0, width * 2 do
  269.   for j = 0, height * 2 do
  270.     extend_up_step()
  271.   end
  272.  
  273.   if i ~= width * 2 then
  274.     face_dir(4)
  275.     turtle.forward()
  276.     if i % 2 == 0 then
  277.       face_dir(1)
  278.     else
  279.       face_dir(3)
  280.     end
  281.     turtle.forward()
  282.   end
  283. end
  284.  
  285. print("\nDone extending walls.")
  286.  
  287. turtle.down()
  288. face_dir(1)
  289.  
  290. print("\nMaze complete!")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement