morganamilo

Turtle circle/cylinder digger

Dec 27th, 2014
454
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.34 KB | None | 0 0
  1. -- by morganamilo ([email protected]) based on http://pastebin.com/TgDE8CSY
  2.  
  3. local args = {...}
  4. local radius = tonumber(args[1]) --the size of the radius includes the center block
  5. local depth = tonumber(args[2])
  6.  
  7. local usage = 'Usage:' .. shell.getRunningProgram() .. ' <radius> <depth>'
  8.  
  9. if table.getn(args) ~= 2 then
  10.   print(usage)
  11.   error()
  12. end
  13.  
  14. if radius == nil or depth == nil then
  15.   print('Radius and depth must both be numbers')
  16.   error()
  17. end
  18.  
  19. if radius ~= math.floor(args[1]) or depth ~= math.floor(args[2]) then
  20.   print('Radius and depth must be whole numbers')
  21.   error()
  22. end
  23.  
  24. if radius < 3 or depth < 1 then
  25.   print('Radius must be at least 3, depth 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.  
  32. local xDiff = {0, 1, 0, -1}
  33. local yDiff = {1, 0, -1, 0}
  34.  
  35. local orient = 1
  36. local moves = {}
  37.  
  38. function forward()
  39.   x = x + xDiff[orient + 1]
  40.   y = y + yDiff[orient + 1]
  41.  
  42.   table.insert(moves, 'forward')
  43. end
  44.  
  45. function left()
  46.   orient = orient - 1
  47.   orient = (orient % 4)
  48.  
  49.   if moves[table.getn(moves)] ~= 'right' then
  50.      table.insert(moves, 'left')
  51.   else
  52.     table.remove(moves)
  53.   end    
  54. end
  55.  
  56. function right()
  57.   orient = orient + 1
  58.   orient = (orient % 4)
  59.  
  60.   table.insert(moves, 'right')
  61. end
  62.  
  63. function doMoves()
  64.   for n = 1, table.getn(moves) do
  65.     if moves[n] == 'forward' then
  66.       turtle.dig()
  67.       while turtle.forward() == false do
  68.         if turtle.getFuelLevel() == 0 then
  69.           print('Out of fuel\nWaiting for fuel in slot 1...')
  70.             while turtle.getFuelLevel() == 0 do
  71.              turtle.refuel()
  72.              end
  73.              print('Fuel level is: ' .. turtle.getFuelLevel())
  74.              
  75.           end
  76.         end
  77.     end
  78.     if moves[n] == 'left' then
  79.       turtle.turnLeft() end
  80.     if moves[n] == 'right' then
  81.       turtle.turnRight() end
  82.   end
  83.   moves = {}
  84. end
  85.  
  86. function round(n)
  87.   if n - math.floor(n) >= 0.5 then
  88.     return math.ceil(n)
  89.   else
  90.     return math.floor(n)
  91.   end
  92. end
  93.  
  94. function inRad(x, y, rad)
  95.   return round(math.sqrt(x*x+y*y)) < rad
  96. end
  97.  
  98. function nextInRad(rad)
  99.   nextX = x + xDiff[orient + 1]
  100.   nextY = y + yDiff[orient + 1]
  101.   return inRad(nextX, nextY, rad)
  102. end
  103.  
  104. function digRing(rad)
  105.   while nextInRad(rad) do forward() end
  106.   left()
  107.  
  108.   originalX = x
  109.   originalY = y
  110.  
  111.   repeat
  112.     while nextInRad(rad) do forward() end
  113.     while not nextInRad(rad) do left() end  
  114.     forward()
  115.     right()
  116.   until x == originalX and y == originalY
  117.  
  118.   doMoves()
  119. end
  120.  
  121. function digLayer()
  122.   for n=1,4 do
  123.     turtle.dig()
  124.     turtle.turnLeft()
  125.   end
  126.  
  127.   for n = 2, radius do
  128.     digRing(n)
  129.   end
  130.  
  131.   left()
  132.   left()
  133.   for n = 2, radius do
  134.     forward()
  135.   end
  136.   left()
  137.   left()
  138.   doMoves()
  139. end
  140.  
  141. function digCyl()
  142.   for n = 1, depth do
  143.     digLayer(radius)
  144.     if n ~= depth then
  145.       turtle.digDown()
  146.       while turtle.down() == false do
  147.           if turtle.getFuelLevel() == 0 then
  148.             print('Out of fuel\nWaiting for fuel in slot 1...')
  149.               while turtle.getFuelLevel() == 0 do
  150.                 turtle.refuel()
  151.               end
  152.                 print('Fuel level is: ' .. turtle.getFuelLevel())
  153.           end
  154.       end
  155.     end
  156.   end
  157. end
  158.  
  159. digCyl()
  160. print('Done!')
Advertisement
Add Comment
Please, Sign In to add comment