Advertisement
hoblin

Digger

Jan 7th, 2013
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.13 KB | None | 0 0
  1. -- -----------------------------
  2. -- Digger
  3. -- pastebin get F2HSytaa digger
  4. -- -----------------------------
  5.  
  6. function checkFuel()
  7.   print('checkFuel')
  8.   while turtle.getFuelLevel() < 10 do
  9.     turtle.select(1)
  10.     turtle.refuel(1)
  11.     if turtle.getFuelLevel() < 10 then
  12.       sleep(5)
  13.     end
  14.     if turtle.getItemCount(1) < 2 then
  15.       goHome()
  16.       goBack()
  17.     end
  18.   end
  19. end
  20.  
  21. function forward()
  22.   print('forward')
  23.   checkFuel()
  24.   while not turtle.forward() do
  25.     if turtle.detect() then
  26.       turtle.dig()
  27.     else
  28.       turtle.attack()
  29.     end
  30.   end
  31.   if not skip_tracing then
  32.     if direction == 0 then
  33.       x_coord = x_coord + 1
  34.     elseif direction == 1 then
  35.       y_coord = y_coord + 1
  36.     elseif direction == 2 then
  37.       x_coord = x_coord - 1
  38.     elseif direction == 3 then
  39.       y_coord = y_coord - 1
  40.     end
  41.   end
  42. end
  43.  
  44. function down()
  45.   print('down')
  46.   checkFuel()
  47.   attempts = 0
  48.   for i=1,3 do
  49.     while not turtle.down() do
  50.       if turtle.detectDown() then
  51.         if attempts == 5 then
  52.           goHome()
  53.           exit()
  54.         end
  55.         attempts = attempts + 1
  56.         turtle.digDown()
  57.       else
  58.         turtle.attackDown()
  59.       end
  60.     end
  61.   end
  62.   if not skip_tracing then
  63.     z_coord = z_coord + 1
  64.   end
  65. end
  66.  
  67. function up()
  68.   print('left')
  69.   checkFuel()
  70.   for i=1,3 do
  71.     while not turtle.up() do
  72.       if turtle.detectUp() then
  73.         turtle.digUp()
  74.       else
  75.         turtle.attackUp()
  76.       end
  77.     end
  78.   end
  79.   if not skip_tracing then
  80.     z_coord = z_coord - 1
  81.   end
  82. end
  83.  
  84. function right()
  85.   print('right')
  86.   turtle.turnRight()
  87.   direction = direction + 1
  88.   if direction == 4 then
  89.     direction = 0
  90.   end
  91. end
  92.  
  93. function left()
  94.   print('left')
  95.   turtle.turnLeft()
  96.   direction = direction - 1
  97.   if direction == -1 then
  98.     direction = 3
  99.   end
  100. end
  101.  
  102. function goHome()
  103.   print('goHome')
  104.   skip_tracing = true
  105.   if direction == 0 then
  106.     turtle.turnLeft()
  107.   elseif direction == 1 then
  108.     turtle.turnLeft()
  109.     turtle.turnLeft()
  110.   elseif direction == 2 then
  111.     turtle.turnRight()
  112.   end
  113.   if z_coord > 1 then
  114.     for i = 1, z_coord - 1 do
  115.       up()
  116.     end
  117.   end
  118.   if y_coord > 1 then
  119.     for i = 1, y_coord do
  120.       forward()
  121.     end
  122.   end
  123.   turtle.turnLeft()
  124.   if x_coord > 1 then
  125.     for i = 1, x_coord do
  126.       forward()
  127.     end
  128.   end
  129.   for i = 2, 16 do
  130.     turtle.select(i)
  131.     turtle.drop()
  132.   end
  133.   turtle.select(1)
  134.   turtle.suckUp()
  135.   turtle.select(2)
  136.   turtle.dropUp()
  137. end
  138.  
  139. function goBack()
  140.   print('goBack')
  141.   turtle.turnLeft()
  142.   turtle.turnLeft()
  143.   if x_coord > 1 then
  144.     for i = 1, x_coord do
  145.       forward()
  146.     end
  147.   end
  148.   turtle.turnRight()
  149.   if y_coord > 1 then
  150.     for i = 1, y_coord do
  151.       forward()
  152.     end
  153.   end
  154.   if z_coord > 1 then
  155.     for i = 1, z_coord - 1 do
  156.       down()
  157.     end
  158.   end
  159.   if direction == 0 then
  160.     turtle.turnLeft()
  161.   elseif direction == 2 then
  162.     turtle.turnRight()
  163.   elseif direction == 3 then
  164.     turtle.turnRight()
  165.     turtle.turnRight()
  166.   end
  167.   skip_tracing = false
  168. end
  169.  
  170. function dig()
  171.   print('dig')
  172.   turtle.digDown()
  173.   turtle.digUp()
  174.   turtle.dig()
  175.   if turtle.getItemCount(16) > 0 then
  176.     goHome()
  177.     goBack()
  178.   end
  179. end
  180.  
  181. term.clear()
  182. term.setCursorPos(1,1)
  183. print("Place fuel in slot 1")
  184. print("Place chest with fuel above the turtle")
  185. print("Place chest for loot behind the turtle")
  186.  
  187. write("Please enter quarry size ")
  188. lenth = read()
  189. x_coord = 0
  190. y_coord = 0
  191. z_coord = 1
  192. direction = 0
  193. even = true
  194. finished = false
  195. skip_tracing = false
  196. layer_changed = true
  197. forward()
  198. while not finished do
  199.   dig()
  200.   forward()
  201.   if x_coord == tonumber(lenth) or x_coord == 0 then
  202.     if (y_coord == tonumber(lenth) or y_coord == 0) and not layer_changed then
  203.       -- Go to next layer
  204.       down()
  205.       right()
  206.       right()
  207.       layer_changed = true
  208.     else
  209.       -- Go to next line
  210.       layer_changed = false
  211.       if even then
  212.         right()
  213.       else
  214.         left()
  215.       end
  216.       dig()
  217.       forward()
  218.       if even then
  219.         right()
  220.       else
  221.         left()
  222.       end
  223.       even = not even
  224.     end
  225.   end
  226. end
  227. goHome()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement