morganamilo

Turtle Tunnel v2.1

May 2nd, 2015
371
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.51 KB | None | 0 0
  1. --place in bottom left corner
  2.  
  3. local args = {...}
  4.  
  5. local width = tonumber(args[1])
  6. local height = tonumber(args[2])
  7. local depth = tonumber(args [3])
  8.  
  9. if table.getn(args) < 3 then
  10.   print('Usage: ' .. shell.getRunningProgram() .. ' <width> <height> <depth>')
  11.   error()
  12. end
  13.  
  14. if width == nil or height == nil or depth == nil then
  15.   print('Dimensions must be numbers')
  16.   error()
  17. end
  18.  
  19. if width ~= math.floor(width) or height ~= math.floor(height) or depth ~= math.floor(depth) then
  20.   print('Dimensions must be whole numbers')
  21.   error()
  22. end
  23.  
  24. if width < 1 or height < 1 or depth < 1 then
  25.   print('All dimensions must be at least 1')
  26.   error()
  27. end
  28.  
  29. local x = 0
  30. local y = 0 --in minecraft y is really z but the turtles coords are relative to itself so it doesnt matter
  31. local z = 0
  32. local orient = 1
  33.  
  34. local xDiff = {0, 1, 0, -1}
  35. local yDiff = {1, 0, -1, 0}
  36.  
  37. function forward()
  38.   x = x + xDiff[orient + 1]
  39.   y = y + yDiff[orient + 1]
  40.  
  41.   moved = turtle.forward()
  42.    
  43.   while not moved do
  44.     if turtle.getFuelLevel() == 0 then
  45.         print('Out of fuel\nWaiting for fuel in slot 1...')
  46.         while turtle.getFuelLevel() == 0 do
  47.            turtle.refuel()
  48.         end
  49.     print('Fuel level is: ' .. turtle.getFuelLevel())
  50.     end
  51.     if turtle.detect() then turtle.dig() end
  52.     moved = turtle.forward()
  53.   end
  54.  
  55.   if z > -depth + 1 and turtle.detectDown() and z > 0 then turtle.digDown() end
  56.   if turtle.detectUp() then turtle.digUp() end
  57. end
  58.  
  59. function left()
  60.   orient = orient - 1
  61.   orient = (orient % 4)
  62.   turtle.turnLeft()    
  63. end
  64.  
  65. function right()
  66.   orient = orient + 1
  67.   orient = (orient % 4)
  68.   turtle.turnRight()
  69. end
  70.  
  71. function up()
  72.   if turtle.detectUp() then turtle.digUp() end
  73.   moved = turtle.up()
  74.   if not moved then
  75.     repeat
  76.         if turtle.detectUp() then turtle.digUp() end
  77.         moved = turtle.up()
  78.     until moved
  79.   end
  80.  
  81.   z = z + 1
  82.   if turtle.detectUp() and z < height -1 then turtle.digUp() end
  83. end
  84.  
  85. function down()
  86.   if turtle.detectDown() then turtle.digDown() end
  87.   moved = turtle.down()
  88.   if not moved then
  89.     repeat
  90.         if turtle.detectDown() then turtle.digDown() end
  91.         moved = turtle.down()
  92.     until moved
  93.   end
  94.  
  95.   z = z - 1
  96.   if turtle.detectDown() and z > 0 then turtle.digDown() end
  97. end
  98.  
  99. function digRow()
  100.   if width > 1 then
  101.     repeat
  102.         forward()
  103.      until y == width - 1 or y == 0
  104.   end
  105. end
  106.  
  107. function digLayer(direction)
  108.   if z == 0 and height ~= 1 then up()
  109.   elseif z == height then down() end
  110.  
  111.   if width > 1 then
  112.     if y ~= 0 then right()
  113.     else left() end
  114.   end
  115.  
  116.  
  117.   repeat
  118.     digRow()
  119.     if (width > 3 or height > 3) and height ~= 1 and width ~= 1 then
  120.       left()
  121.       left()
  122.     end
  123.    
  124.     if height > 1 then
  125.       if direction and z < height - 2 then
  126.         up()
  127.         if z < height - 2 then up() end
  128.         if z < height - 2 then up() end
  129.    
  130.       elseif z > 1 then
  131.         down()
  132.         if z > 1 then down() end
  133.         if z > 1 then down() end
  134.       end
  135.     end
  136.  
  137.     until z <= 1 or z >= height -2
  138.    
  139.     if (width > 3 or height > 3) and height ~= 1 then digRow() end
  140.    
  141.     while orient ~= 1 do
  142.       if orient == 2 then left()
  143.       else right()
  144.     end
  145.   end
  146. end
  147.      
  148. function digTunnel()
  149.   for n = 1, depth do
  150.     forward()
  151.     digLayer((n % 2) ~= 0)
  152.   end
  153. end
  154.  
  155. if width == 1 and height <= 2 then
  156.   for n = 1, depth do
  157.     forward()
  158.     if height == 2 then turtle.digUp() end
  159.   end
  160. else
  161.   digTunnel()
  162. end
  163.  
  164. while z > 0 do down() end
Advertisement
Add Comment
Please, Sign In to add comment