omr__

Turtle - Area miner

Aug 22nd, 2019
288
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.39 KB | None | 0 0
  1. -- ********************************************************************************** --
  2. -- **                                                                              ** --
  3. -- **   Mining selected area turtle v1.5 by omr___                                 ** --
  4. -- **   (Add support of negative height and width)                                 ** --
  5. -- **   ----------------------------------------------------                       ** --
  6. -- **                                                                              ** --
  7. -- **   How to use:                                                                ** --
  8. -- **     Download: rom/programs/http/pastebin get 0XqzVUPM dig                    ** --
  9. -- **     !IMPORTANT! Place a chest above the turtle                               ** --
  10. -- **     Start position: Bottom Left before area                                  ** --
  11. -- **     Fuel slot: 16, Bottom Right                                              ** --
  12. -- **                                                                              ** --
  13. -- **  Change Log:                                                                 ** --
  14. -- **    27th Aug 2018: [v1.5] Add support of negative height and width            ** --
  15. -- **                   [v1.5] Fix items drop when no chest above                  ** --
  16. -- **    26th Aug 2018: [v1.4] Type parameters after init                          ** --
  17. -- **    26th Aug 2018: [v1.3] Unload items in chest when done                     ** --
  18. -- **                   [v1.3] Return to start after finishing                     ** --
  19. -- **    25th Aug 2018: [v1.2] Mining performance improvements                     ** --
  20. -- **                   [v1.2] Fixed falling blocks issue                          ** --
  21. -- **                                                                              ** --
  22. -- **  TODO:                                                                       ** --
  23. -- **    - Auto resume                                                             ** --
  24. -- **    - Unload when full                                                        ** --
  25. -- **    - Allow trash cobble                                                      ** --
  26. -- **                                                                              ** --
  27. -- ********************************************************************************** --
  28.  
  29. local args = { ... }
  30. local direction = {FORWARD = 0, LEFT = 1, BACK = 2, RIGHT = 3, UP = 4, DOWN = 5}
  31. local axis = {x = -1, y = -1}
  32. local invertPos
  33. local width
  34. local height
  35. local length
  36.  
  37. function rotate(invert)
  38.     if invert == 1 then
  39.         turtle.turnRight()
  40.     else
  41.         turtle.turnLeft()
  42.     end
  43. end
  44.  
  45. function turn(invert)
  46.     if axis.x == 1 then
  47.         rotate(invert)
  48.     else
  49.         rotate(-invert)
  50.     end
  51. end
  52.  
  53. function unloadToChest()
  54.     for slot = 1, 16 do
  55.         turtle.select(slot)
  56.         if turtle.detectUp() then
  57.             turtle.dropUp()
  58.         end
  59.     end
  60.     turtle.select(1)
  61.     print("Items were unloaded")
  62. end
  63.  
  64. function returnToStart()
  65.     if (axis.x == 1) and not (invertPos == true) then
  66.         rotate()
  67.         for i = 1, width - 1 do
  68.             move(direction.FORWARD)
  69.         end
  70.         rotate(1)
  71.     end
  72.     if (axis.y == 1) and not (invertPos == true) then
  73.         for i = 1, height - 1 do
  74.             move(direction.DOWN)
  75.         end
  76.     end
  77.     for i = 1, length do
  78.         move(direction.BACK)
  79.     end
  80.     print("Return complete")
  81. end
  82.  
  83. function verifyFuel()
  84.     if turtle.getFuelLevel() < 10 then
  85.         print("Fuel level is low, put fuel in slot 16")
  86.         local selectedSlot = turtle.getSelectedSlot()
  87.         turtle.select(16)
  88.         turtle.refuel()
  89.         turtle.select(selectedSlot)
  90.     end
  91. end
  92.  
  93. function dig(dir)
  94.     function try(detectFn, digFn)
  95.         while detectFn() do
  96.             verifyFuel()
  97.             digFn()
  98.             os.sleep(0.5)
  99.         end
  100.     end
  101.  
  102.     if dir == direction.UP then
  103.         try(turtle.detectUp, turtle.digUp)
  104.     elseif dir == direction.DOWN then
  105.         try(turtle.detectDown, turtle.digDown)
  106.     elseif dir == direction.BACK then
  107.         turtle.turnLeft()
  108.         turtle.turnLeft()
  109.         try(turtle.detect, turtle.dig)
  110.         turtle.turnRight()
  111.         turtle.turnRight()
  112.     else
  113.         try(turtle.detect, turtle.dig)
  114.     end
  115. end
  116.  
  117. function move(dir)
  118.     function try(moveFn)
  119.         dig(dir)
  120.         moveFn()
  121.     end
  122.  
  123.     if dir == direction.FORWARD then
  124.         try(turtle.forward)
  125.     elseif dir == direction.UP then
  126.         try(turtle.up)
  127.     elseif dir == direction.DOWN then
  128.         try(turtle.down)
  129.     elseif dir == direction.BACK then
  130.         try(turtle.back)
  131.     end
  132. end
  133.  
  134. function digRow(w)
  135.     turn(-1)
  136.     for i = 1, w do
  137.         dig()
  138.         move(direction.FORWARD)
  139.     end
  140.     turn(1)
  141.     axis.x = -axis.x
  142. end
  143.  
  144. function digWall()
  145.     for i = 1, height do
  146.         digRow(width - 1)
  147.         if (i < tonumber(height)) then
  148.             if axis.y == 1 then
  149.                 dig(direction.DOWN)
  150.                 move(direction.DOWN)
  151.             else
  152.                 dig(direction.UP)
  153.                 move(direction.UP)
  154.             end
  155.         end
  156.     end
  157. end
  158.  
  159. function digArea()
  160.     local blocks = width * height * length
  161.     print("Fuel level: "..turtle.getFuelLevel())
  162.     print("Area size: "..width.."x"..height.."x"..length.." ("..blocks.." blocks)")
  163.  
  164.     for i = 1, length do
  165.         dig()
  166.         move(direction.FORWARD)
  167.         digWall()
  168.         axis.y = -axis.y
  169.     end
  170. end
  171.  
  172. if #args ~= 3 then
  173.     print("Usage: dig <Length> <Width> <Height>")
  174.     print("Length?")
  175.     length = tonumber(read())
  176.     print("Width?")
  177.     width = tonumber(read())
  178.     print("Height?")
  179.     height = tonumber(read())
  180. else
  181.     length = tonumber(args[1])
  182.     width = tonumber(args[2])
  183.     height = tonumber(args[3])
  184. end
  185.  
  186. if height < 0 then
  187.     axis.y = 1
  188.     height = -height
  189. end
  190.  
  191. if width < 0 then
  192.     axis.x = 1
  193.     width = -width
  194.     invertPos = true
  195. end
  196.  
  197. if length < 1 then
  198.     print("Length must be positive")
  199.     return
  200. end
  201.  
  202. digArea()
  203. returnToStart()
  204. unloadToChest()
Add Comment
Please, Sign In to add comment