morganamilo

[Wip] turtle excavator v1.0

May 8th, 2015
2,340
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.43 KB | None | 0 0
  1. local args = {...}
  2.  
  3. local width = tonumber(args[1])
  4. local height = tonumber(args[2])
  5. local depth = tonumber(args [3])
  6.  
  7. if table.getn(args) < 3 then
  8.   print('Usage: ' .. shell.getRunningProgram() .. ' <width> <length> <depth>')
  9.   error()
  10. end
  11.  
  12. if width == nil or height == nil or depth == nil then
  13.   print('Dimensions must be numbers')
  14.   error()
  15. end
  16.  
  17. if width ~= math.floor(width) or height ~= math.floor(height) or depth ~= math.floor(depth) then
  18.   print('Dimensions must be whole numbers')
  19.   error()
  20. end
  21.  
  22. if width < 1 or height < 1 or depth < 1 then
  23.   print('All dimensions must be at least 1')
  24.   error()
  25. end
  26.  
  27. local x = 0
  28. local y = 0 --in minecraft y is really z but the turtles coords are relative to itself so it doesnt matter
  29. local z = 0
  30. local orient = 1 -- 1 = default, goes clockwise
  31. local useEnderChest = false
  32.  
  33. local xDiff = {0, 1, 0, -1}
  34. local yDiff = {1, 0, -1, 0}
  35.  
  36. if turtle.getItemCount(1) == 1 then
  37.     useEnderChest = true
  38. end
  39.  
  40. local function hasSpace()
  41.     for n = 1, 16 do
  42.         if turtle.getItemCount(n) == 0 then
  43.             return true
  44.         end
  45.         sleep(0)
  46.     end
  47.    
  48.     return false
  49. end
  50.  
  51. local function dumpItems(dir, force)
  52.     if not useEnderChest then
  53. return
  54. end
  55.     if hasSpace() and not force then
  56.         return
  57.     end
  58.    
  59.     local place = turtle.place
  60.     local dig = turtle.dig
  61.     local drop = turtle.drop
  62.     local detect = turtle.detect
  63.    
  64.     if dir == "up" then
  65.         place = turtle.placeUp
  66.         dig = turtle.digUp
  67.         drop = turtle.dropUp
  68.         detect = turtle.detectUp
  69.     elseif dir == "down" then
  70.         place = turtle.placeDown
  71.         dig = turtle.digDown
  72.         drop = turtle.dropDown
  73.         detect = turtle.detectDown
  74.     end
  75.  
  76.         while detect() and not dig() do end
  77.        
  78.         turtle.select(1)
  79.        
  80.         while not place() do end
  81.    
  82.     for n = 1, 16 do
  83.         turtle.select(n)
  84.         drop()
  85.     end
  86.    
  87.     turtle.select(1)
  88.     while not dig() do end
  89. end
  90.  
  91. local function forward()
  92.   x = x + xDiff[orient + 1]
  93.   y = y + yDiff[orient + 1]
  94.  
  95.   moved = turtle.forward()
  96.    
  97.   while not moved do
  98.     turtle.attack()
  99.     if turtle.getFuelLevel() == 0 then
  100.         print('Out of fuel\nWaiting for fuel in slot 2...')
  101.         turtle.select(2)
  102.         while turtle.getFuelLevel() == 0 do
  103.            turtle.refuel()
  104.         end
  105.     print('Fuel level is: ' .. turtle.getFuelLevel())
  106.     end
  107.     if turtle.detect() then turtle.dig() dumpItems() end
  108.     moved = turtle.forward()
  109.   end
  110.  
  111.   if z > -depth + 1 and turtle.detectDown() then turtle.digDown() dumpItems("down")  end
  112.   if turtle.detectUp() then turtle.digUp() dumpItems("up")  end
  113. end
  114.  
  115. local function left()
  116.   orient = orient - 1
  117.   orient = (orient % 4)
  118.   turtle.turnLeft()    
  119. end
  120.  
  121. local function right()
  122.   orient = orient + 1
  123.   orient = (orient % 4)
  124.   turtle.turnRight()
  125. end
  126.  
  127. local function up()
  128.   if turtle.detectUp() then turtle.digUp() dumpItems("up")  end
  129.   moved = turtle.up()
  130.   if not moved then
  131.     repeat
  132.         if turtle.detectUp() then turtle.digUp() dumpItems("up")  end
  133.         moved = turtle.up()
  134.     until moved
  135.   end
  136.  
  137.   z = z + 1
  138.   if turtle.detectUp() and z < height -1 then turtle.digUp() dumpItems("up")  end
  139. end
  140.  
  141. local function down()
  142.   local dug = true
  143.  
  144.   if turtle.detectDown() then turtle.digDown() dumpItems("down") end
  145.   moved = turtle.down()
  146.   if not moved then
  147.     repeat
  148.         if turtle.detectDown() then dug = turtle.digDown() dumpItems("down") end
  149.         if not dug then --bedrock
  150.         dumpItems(nil, true)
  151.         error()
  152.         end
  153.         moved = turtle.down()
  154.     until moved
  155.   end
  156.  
  157.   z = z - 1
  158.   if turtle.detectDown() and z > -depth + 1 then turtle.digDown() dumpItems("down")  end
  159. end
  160.  
  161. local function isEven(n)
  162.   return n % 2 == 0
  163. end
  164.  
  165. local function round(n) -- rounds numbers
  166.   return math.floor(n + 0.5)
  167. end
  168.  
  169. local function turn(n)
  170.   if n % 2 == 0 then left()
  171.   else right() end
  172. end
  173.  
  174. local function digStrip()
  175.   for n=0, height-2 do
  176.     forward()
  177.   end
  178. end
  179.  
  180. local function digLayer()
  181.   for n = 0, width-1 do
  182.     if n ~= width-1  then
  183.       digStrip()
  184.       turn(n)
  185.       forward()
  186.       turn(n)
  187.     end
  188.   end
  189.   digStrip()
  190.   if width % 2 ~= 0 then left() end
  191.  
  192.   for n = 1, 3 do
  193.     if z > -depth + 1 then down() end
  194.   end
  195. end
  196.  
  197. local function digCube()
  198.   if depth > 1 then down() end
  199.  
  200.   for n = 1, (round((depth + 1)/3)*3)/3 do
  201.     digLayer()
  202.     left()
  203.   end
  204. end
  205.  
  206. if width % 2 == 0 then
  207.   width, height = height, width
  208.   for n=0, width-2 do forward() end
  209.   left()
  210.   orient = 1
  211.   x, y = 0, 0
  212. end
  213.  
  214. digCube()
  215.  
  216. dumpItems(nil, true)
Advertisement
Add Comment
Please, Sign In to add comment