morganamilo

Turtle circle/cylinder digger v2.0

May 2nd, 2015
439
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.52 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. local z = 0
  32.  
  33. local xDiff = {0, 1, 0, -1}
  34. local yDiff = {1, 0, -1, 0}
  35.  
  36. local orient = 1
  37.  
  38. function forward()
  39.   x = x + xDiff[orient + 1]
  40.   y = y + yDiff[orient + 1]
  41.  
  42.   moved = turtle.forward()
  43.    
  44.   while not moved do
  45.     if turtle.getFuelLevel() == 0 then
  46.         print('Out of fuel\nWaiting for fuel in slot 1...')
  47.         while turtle.getFuelLevel() == 0 do
  48.            turtle.refuel()
  49.         end
  50.     print('Fuel level is: ' .. turtle.getFuelLevel())
  51.     end
  52.     if turtle.detect() then turtle.dig() end
  53.     moved = turtle.forward()
  54.   end
  55.  
  56.   if z > -depth + 1 and turtle.detectDown() then turtle.digDown() end
  57.   if turtle.detectUp() then turtle.digUp() end
  58. end
  59.  
  60. function left()
  61.   orient = orient - 1
  62.   orient = (orient % 4)
  63.   turtle.turnLeft()    
  64. end
  65.  
  66. function right()
  67.   orient = orient + 1
  68.   orient = (orient % 4)
  69.   turtle.turnRight()
  70. end
  71.  
  72. function down()
  73.   if turtle.detectDown() then turtle.digDown() end
  74.   moved = turtle.down()
  75.   if not moved then
  76.     repeat
  77.         if turtle.detectDown() then turtle.digDown() end
  78.         moved = turtle.down()
  79.     until moved
  80.   end
  81.   z = z - 1
  82.   if turtle.detectDown() and z > -depth + 1 then turtle.digDown() end
  83. end
  84.  
  85. function round(n)
  86.   if n - math.floor(n) >= 0.5 then
  87.     return math.ceil(n)
  88.   else
  89.     return math.floor(n)
  90.   end
  91. end
  92.  
  93. function inRad(x, y, rad)
  94.   return round(math.sqrt(x*x+y*y)) < rad
  95. end
  96.  
  97. function nextInRad(rad)
  98.   nextX = x + xDiff[orient + 1]
  99.   nextY = y + yDiff[orient + 1]
  100.   return inRad(nextX, nextY, rad)
  101. end
  102.  
  103. function turn(n)
  104.   if n % 2 == 0 then left()
  105.   else right() end
  106. end
  107.  
  108. function digLayer()
  109.   for n = 1, (radius -1)*2 do
  110.     while nextInRad(radius) do forward() end
  111.     turn(n)
  112.     if nextInRad(radius) then
  113.       forward()
  114.       if not (inRad(x, y - 1, radius) and inRad(x, y + 1, radius)) then
  115.       turn(n)
  116.       else
  117.         turn(n + 1)
  118.         while nextInRad(radius) do forward() end
  119.         turn(n)
  120.         turn(n)
  121.       end
  122.  
  123.     else
  124.       turn(n)
  125.       while not inRad(x - 1, y, radius) or not inRad(x + 1, y, radius) do forward() end
  126.       turn(n + 1)
  127.       forward()
  128.       turn(n)
  129.     end
  130.   end
  131.   while nextInRad(radius) do forward() end
  132.  
  133.   left()
  134.     left()
  135.     if n ~= depth then
  136.       for n = 1, 3 do
  137.         if z > -depth + 1 then down() end
  138.       end
  139.     end
  140. end
  141.  
  142. function digCyl()
  143.   if depth > 1 then down() end
  144.   for n = 1, 2 do
  145.     while nextInRad(radius) do forward() end
  146.     left()
  147.   end
  148.   left()
  149.  
  150.   for n = 1, (round((depth + 1)/3)*3)/3 do
  151.     digLayer()
  152.   end
  153.   --if z > -depth + 1 then digLayer() end
  154.  
  155.   while y ~= 0 do forward() end
  156.   turn(orient + 1)
  157.   while x ~= 0 do forward() end
  158. end
  159.  
  160. digCyl()
  161. print('Done!')
Advertisement
Add Comment
Please, Sign In to add comment