Advertisement
GauHelldragon

GauMaze v1.5

Mar 2nd, 2013
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.86 KB | None | 0 0
  1. -- Maze building script for MC computercraft turtles
  2. -- Turtle requires the proper amount of fuel to build before it will start, does not attempt to refuel,
  3. --   this must be done manually
  4. -- Builds the maze to the front and left of the turtle.
  5. -- Can specify size with command line arguments. Example ( to make a 21x9 maze) : Maze 21 9
  6.  
  7. local maze = {}
  8. local args = { ... }
  9. local xMax = tonumber(args[1])
  10. local yMax = tonumber(args[2])
  11.  
  12. local selection = 1
  13. turtle.select(1)
  14.  
  15. if #args < 2 then
  16.   xMax = 7
  17.   yMax = 7
  18. end
  19.  
  20. if ( xMax < 7 ) then xMax = 7 end
  21. if ( yMax < 7 ) then yMax = 7 end
  22. if ( xMax % 2 == 0 ) then xMax = xMax + 1 end
  23. if ( yMax % 2 == 0 ) then yMax = yMax + 1 end
  24.  
  25. local fuelNeeded = yMax * xMax * 3 + yMax * 2 + xMax * 2
  26. if ( turtle.getFuelLevel() < fuelNeeded ) then
  27.   print("Not enough fuel, need at least " .. fuelNeeded .. " to make a maze of that size")
  28.   return
  29. end
  30.  
  31. function tryForward()
  32.   turtle.dig()
  33.   if ( turtle.forward() == false ) then
  34.     print("Get out of my way!!")
  35.     while ( turtle.forward() == false ) do
  36.       os.sleep(2)  
  37.     end
  38.   end
  39. end
  40.  
  41.  
  42. function initilize(x, y)
  43.   local i = 1
  44.   while i <= x do
  45.     maze[i] = {}
  46.     local j = 1
  47.     while j <= y do
  48.       if ( i % 2 == 0 ) and ( j % 2 == 0 ) then
  49.         maze[i][j] = 2
  50.       else
  51.         maze[i][j] = 1
  52.       end
  53.       j = j + 1
  54.     end
  55.     i = i + 1
  56.   end
  57. end
  58.  
  59. function arrayToCell(A)
  60.   return ( A - 1 ) / 2
  61. end
  62.  
  63. function cellToArray(C)
  64.   return C * 2
  65. end
  66.  
  67. function isOpenCell( cell )
  68.   local x = cell[1]
  69.   local y = cell[2]
  70.  
  71.   if ( x < 1 or y < 1 ) then return false end
  72.  
  73.   local cellXMax = arrayToCell( xMax )
  74.   local cellYMax = arrayToCell( yMax )
  75.  
  76.   if ( x > cellXMax or y > cellYMax ) then return false end
  77.  
  78.   local arrayX = cellToArray(x)
  79.   local arrayY = cellToArray(y)
  80.  
  81.   if ( maze[arrayX][arrayY] == 2 ) then
  82.      return true
  83.   else
  84.      return false
  85.   end
  86.  
  87. end
  88.  
  89. function getNeighbors ( cell )
  90.   local x = cell[1]
  91.   local y = cell[2]
  92.  
  93.   local neighbors = {}
  94.   local testCell
  95.  
  96.   testCell = { x , y + 1 }
  97.   if ( isOpenCell(testCell) ) then table.insert(neighbors,testCell) end
  98.  
  99.   testCell = { x , y - 1 }
  100.   if ( isOpenCell(testCell) ) then table.insert(neighbors,testCell) end
  101.  
  102.   testCell = { x + 1 , y }
  103.   if ( isOpenCell(testCell) ) then table.insert(neighbors,testCell) end
  104.  
  105.   testCell = { x - 1, y }
  106.   if ( isOpenCell(testCell) ) then table.insert(neighbors,testCell) end
  107.  
  108.   return neighbors
  109. end
  110.  
  111. function addToMaze( cell )
  112.   local x = cellToArray( cell[1] )
  113.   local y = cellToArray( cell[2] )
  114.   maze[x][y] = 0
  115. end
  116.  
  117. function addToOpenCells( cell, openCells )
  118.   local newCell = { cell[1] , cell[2] }
  119.  
  120.  
  121.   openCells = table.insert(openCells,newCell)  
  122.  
  123. end
  124.  
  125. function openWall( cell, cell2 )
  126.   local x1 = cellToArray( cell[1] )
  127.   local x2 = cellToArray( cell2[1] )
  128.   local y1 = cellToArray( cell[2] )
  129.   local y2 = cellToArray( cell2[2] )
  130.   local midX = ( x1 + x2 ) / 2
  131.   local midY = ( y1 + y2 ) / 2  
  132.   maze[midX][midY] = 0
  133.  
  134. end
  135.  
  136. function makeMaze()
  137.  
  138.  
  139.   local openCells = { {math.random(arrayToCell(xMax)),math.random(arrayToCell(yMax))} }
  140.  
  141.   while #openCells >= 1 do
  142.     local pickedCellNumber = math.random(#openCells)
  143.     local currentCell = openCells[pickedCellNumber]
  144.     addToMaze( currentCell )
  145.    
  146.     local neighborCells = getNeighbors( currentCell )
  147.        
  148.     if ( #neighborCells >= 1 ) then
  149.         local neighbor = 1
  150.         --while neighbor <= #neighborCells do
  151.         --   addToOpenCells( neighborCells[neighbor], openCells )
  152.         --   neighbor = neighbor + 1
  153.         --end
  154.        
  155.         neighbor = math.random(#neighborCells)
  156.         openWall(currentCell, neighborCells[neighbor])
  157.         addToOpenCells(neighborCells[neighbor],openCells)
  158.         addToMaze( neighborCells[neighbor] )
  159.     else
  160.         table.remove(openCells,pickedCellNumber)
  161.     end
  162.    
  163.  
  164.    
  165.    
  166.   end  
  167.   maze[1][2] = 0
  168.   maze[xMax][yMax-1] = 0
  169.  
  170.  
  171.  
  172.  
  173. end
  174.  
  175.  
  176. function dropBlock(x,y)
  177.   if ( maze[x][y] == 1 ) then
  178.      while ( turtle.getItemCount(selection) <= 0 ) do
  179.        selection = selection + 1
  180.        if ( selection > 16 ) then
  181.          selection = 1
  182.          if ( turtle.getItemCount(selection) <= 0 ) then         
  183.            print ("Need more blocks, please put more in!")
  184.            while ( turtle.getItemCount(selection) <= 0 ) do
  185.              os.sleep(10)
  186.            end
  187.          end
  188.        end
  189.        turtle.select(selection)
  190.      end
  191.      turtle.placeDown()
  192.  
  193.  
  194.   else
  195.     turtle.digDown()
  196.   end
  197. end
  198.  
  199. function drawMaze(addFloor)
  200.   local x = 1
  201.  
  202.   while ( x <= xMax ) do
  203.     local goForward = ( x % 2 == 1 )
  204.     local y
  205.     if ( goForward ) then
  206.       y = 1
  207.     else
  208.       y = yMax
  209.     end
  210.    
  211.     while ( goForward and y <= yMax ) or ( not goForward and y >= 1 ) do
  212.       dropBlock(x,y)
  213.       tryForward()
  214.       if ( goForward ) then
  215.         y = y + 1
  216.       else
  217.         y = y - 1
  218.       end
  219.      
  220.     end
  221.    
  222.     dropBlock(x,y)
  223.    
  224.     if ( goForward ) then
  225.       turtle.turnLeft()
  226.       tryForward()
  227.       turtle.turnLeft()
  228.     else
  229.       turtle.turnRight()
  230.       tryForward()
  231.       turtle.turnRight()
  232.     end  
  233.     tryForward()
  234.     x = x + 1
  235.   end
  236.  
  237. end  
  238.  
  239. function goHome()
  240.   local i = 1
  241.   while i < yMax do
  242.     tryForward()
  243.     i = i + 1
  244.   end
  245.   i = 1
  246.   turtle.turnLeft()
  247.   while i <= xMax do
  248.     tryForward()
  249.     i = i + 1
  250.   end
  251.   turtle.turnLeft()
  252.   turtle.digUp()
  253.   turtle.up()
  254.  
  255. end
  256.  
  257. function coverMaze()
  258.   turtle.turnLeft()
  259.   tryForward()
  260.   turtle.up()
  261.   local y = 1
  262.   while y <= yMax do
  263.     local x = 1
  264.     while x < xMax do
  265.       dropBlock(1,1)
  266.       tryForward()
  267.       x = x + 1
  268.     end
  269.  
  270.     dropBlock(1,1)
  271.     if ( y % 2 == 0 ) then
  272.       turtle.turnLeft()
  273.       tryForward()
  274.       turtle.turnLeft()
  275.     else
  276.       turtle.turnRight()
  277.       tryForward()
  278.       turtle.turnRight()
  279.     end
  280.  
  281.     y = y + 1
  282.   end
  283.  
  284.  
  285. end
  286.  
  287. if ( turtle.detectDown() ) then turtle.up() end
  288.  
  289. initilize(xMax,yMax)
  290. makeMaze()
  291.  
  292. drawMaze()
  293. goHome()
  294. drawMaze()
  295.  
  296. if ( #args < 3 or args[3] ~= "-r" ) then
  297.   coverMaze()
  298. end
  299. turtle.down()
  300. turtle.down()
  301. turtle.down()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement