Guest User

Untitled

a guest
Mar 23rd, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.98 KB | None | 0 0
  1. -- simple program for mining turtles to mine a 3x3 tunnel (as opposed to the default
  2. -- tunnel app, which mines a 3x2)
  3. -- adapted from user FutileFreedom's 3x3 "tunneler" program, posted on the ComputerCraft
  4. -- forums: http://www.computercraft.info/forums2/index.php?/topic/3044-turtle-cc-14-3x3-tunnel/
  5.  
  6. -- ---------------- config options
  7. -- specify the slot for chests to place for unloading items when the turtles
  8. -- inventory gets full. Set to 0 to disable this feature altogether.
  9. local CHEST_SLOT = 16
  10.  
  11. -- specify the slot for torches. Set to 0 to disable altogether.
  12. local TORCH_SLOT = 15
  13.  
  14. -- if TORCH_SLOT is non-zero, then torches will be placed on the right wall of
  15. -- the excavated tunnel every TORCH_STEP blocks
  16. local TORCH_STEP = 6
  17.  
  18. -- if you're using an ender chest, set this to true
  19. -- otherwise, provide a stack of chests in slot CHEST_SLOT and the turtle will
  20. -- leave a trail of chests behind
  21. local RECOLLECT_CHEST = false
  22.  
  23. -- how low our fuel level gets before we try to refuel off of anything in
  24. -- our inv slots (except for CHEST_SLOT)
  25. local REFUEL_LEVEL = 100
  26.  
  27. -- how long to wait between actions
  28. local SLEEP_INT = 0.5
  29.  
  30. -- sometimes gravel that falls while the turtle is mining a top block gets missed
  31. -- set this to true to snag that sneaky gravel, with the tradeoff that it adds a few
  32. -- seconds to each layer/iteration
  33. local GET_SNEAKY_GRAVEL = true
  34.  
  35.  
  36. -- -------------- initialization
  37. local tArgs = { ... }
  38. if #tArgs ~= 1 then
  39.     print( "Usage: tunnel3 <length>" )
  40.     return
  41. end
  42.  
  43. local length = tonumber( tArgs[1] )
  44. if length < 1 then
  45.     print( "Tunnel length must be positive" )
  46.     return
  47. end
  48.  
  49. -- -------------- digging
  50. local function oneeighty()
  51.     turtle.turnLeft()
  52.     turtle.turnLeft()
  53. end
  54.  
  55. local collected = 0
  56. local function collect()
  57.     collected = collected + 1
  58.     if math.fmod(collected, 25) == 0 then
  59.         print( "Mined "..collected.." blocks." )
  60.     end
  61.     if math.fmod(collected, 300) == 0 then
  62.         print( "Fuel level="..turtle.getFuelLevel())
  63.     end
  64. end
  65.  
  66. local function tryDig()
  67.     while turtle.dig() do
  68.         collect()
  69.         sleep(SLEEP_INT)
  70.         if not turtle.detect() then
  71.             return true
  72.         end
  73.     end
  74.     return not turtle.detect()
  75. end
  76.  
  77. local function tryDigUp()
  78.     while turtle.digUp() do
  79.         collect()
  80.         sleep(SLEEP_INT)
  81.         if not turtle.detectUp() then
  82.             return true
  83.         end
  84.     end
  85.     return not turtle.detectUp()
  86. end
  87.  
  88. -- --------------- fuel management
  89. local function needToRefuel()
  90.     return turtle.getFuelLevel() < REFUEL_LEVEL
  91. end
  92.  
  93. local function doRefuel(amount, once)
  94.     amount = amount or 10
  95.     once = once or false
  96.     for slot=1,16 do
  97.         if slot ~= CHEST_SLOT then
  98.             turtle.select(slot)
  99.             if turtle.refuel(amount) and (turtle.getFuelLevel() > (REFUEL_LEVEL * 2) or once) then
  100.                 break
  101.             end
  102.         end
  103.     end
  104.     print("Refueling complete; new fuel level="..turtle.getFuelLevel())
  105.     turtle.select(1)
  106. end
  107.  
  108. -- -------------- inventory management
  109. local function hasSpace()
  110.     local space = 0
  111.     for slot=1,16 do
  112.         if slot ~= CHEST_SLOT and slot ~= TORCH_SLOT then
  113.             space = space + turtle.getItemSpace(slot)
  114.         end
  115.     end
  116.     if space < 32 then
  117.         return false
  118.     end
  119.     return true
  120. end
  121.  
  122. local function hasEmptySlots()
  123.     local emptySlots = 0
  124.     for slot=1,16 do
  125.         if slot ~= CHEST_SLOT and slot ~= TORCH_SLOT then
  126.             if turtle.getItemCount(slot) == 0 then
  127.                 emptySlots = emptySlots + turtle.getItemSpace(slot)
  128.             end
  129.         end
  130.     end
  131.     if emptySlots < 2 then
  132.         return false
  133.     end
  134.     return true
  135. end
  136.  
  137. local function unloadToChest(isComplete)
  138.     isComplete = isComplete or false
  139.     oneeighty()
  140.     turtle.select(CHEST_SLOT)
  141.     if not turtle.place() then
  142.         print("Couldn't place chest! Not unloading current inventory..")
  143.         return false
  144.     end
  145.     sleep(SLEEP_INT)
  146.     for slot=1,16 do
  147.         if slot ~= CHEST_SLOT and slot ~= TORCH_SLOT then
  148.             turtle.select(slot)
  149.             if not turtle.drop(turtle.getItemCount(slot)) then
  150.                 print("Couldn't drop off inventory - invalid target or target is full")
  151.                 return false
  152.             end
  153.         end
  154.         sleep(SLEEP_INT)
  155.     end
  156.     if RECOLLECT_CHEST and not isComplete then
  157.         turtle.select(CHEST_SLOT)
  158.         turtle.dig()
  159.     end
  160.     turtle.select(1)
  161.     oneeighty()
  162.     return true
  163. end
  164.  
  165. local function checkUnloadToChest()
  166.     if CHEST_SLOT > 0 and (not hasSpace() or not hasEmptySlots()) then
  167.         print("Inv space low - dumping to chest...")
  168.         -- do a small refuel with anything we happen to have before we dump it
  169.         doRefuel(5, true)
  170.         unloadToChest()
  171.         if not hasSpace() or not hasEmptySlots() then
  172.             -- see if we weren't able to unload enough material to the chest
  173.             print("Doh - still no inventory space; aborting! (Is the chest full?)")
  174.             return false
  175.         end
  176.     end
  177.     return true
  178. end
  179.  
  180. -- ----------------- torches
  181. local function checkPlaceTorch()
  182.     -- if so configured, place a torch
  183.     if TORCH_SLOT > 0 and steps % TORCH_STEP == 0 then
  184.         turtle.select(TORCH_SLOT)
  185.         if not turtle.place() then
  186.             -- couldnt place it on the right; try on the left
  187.             oneeighty()
  188.             if not turtle.place() then
  189.                 print('Weird: couldnt place torch')
  190.             end
  191.             oneeighty()
  192.         end
  193.         if turtle.getItemCount(TORCH_SLOT) == 0 then
  194.             print('Out of torches! Disabling torch placement')
  195.             TORCH_SLOT = 0
  196.         end
  197.     end
  198. end
  199.  
  200. -- ----------------- Main Program for digging the tunnel
  201. local function tunnelize(tunnelLength)
  202.     --[[
  203.     -- Mine out a 3x3 tunnel for tunnelLength blocks:
  204.     --   1 2 3
  205.     -- A x x x
  206.     -- B x x x
  207.     -- C x S x
  208.     -- ...assuming that the turtle starts in position S
  209.     --]]
  210.     print("Tunneling for "..tunnelLength.." blocks")
  211.     steps = 0
  212.     for n=0,length do
  213.         turtle.select(1)
  214.         if not checkUnloadToChest() then
  215.             -- we were supposed to unload to a chest but
  216.             -- couldnt for some reason ..so abort
  217.             break
  218.         end
  219.         if needToRefuel() then
  220.             print("Fuel low ("..turtle.getFuelLevel().."<"..REFUEL_LEVEL.."); refueling..")
  221.             doRefuel()
  222.         end
  223.         -- C1
  224.         turtle.turnLeft()
  225.         tryDig()
  226.         -- B2
  227.         tryDigUp()
  228.         turtle.up()
  229.         -- B1
  230.         tryDig()
  231.         -- A2
  232.         tryDigUp()
  233.         turtle.up()
  234.         -- A1
  235.         tryDig()
  236.         oneeighty()
  237.         -- A3
  238.         tryDig()
  239.         while not turtle.down() do
  240.             sleep(SLEEP_INT)
  241.             turtle.digDown()
  242.         end
  243.         -- B3
  244.         tryDig()
  245.         while not turtle.down() do
  246.             sleep(SLEEP_INT)
  247.             turtle.digDown()
  248.         end
  249.         -- C3
  250.         tryDig()
  251.         checkPlaceTorch()
  252.         turtle.turnLeft()
  253.        
  254.         if GET_SNEAKY_GRAVEL then
  255.             -- check for gravel that we might have missed back at C1
  256.             turtle.turnLeft()
  257.             if turtle.detect() then
  258.                 tryDig()
  259.             end
  260.             -- start over
  261.             turtle.turnRight()
  262.         end
  263.  
  264.         -- done?
  265.         if n == tunnelLength then
  266.             -- call unloadToChest with isComplete=True so we leave the chest placed in the world
  267.             if CHEST_SLOT > 0 then
  268.                 unloadToChest(true)
  269.             end
  270.             print("Tunnel complete")
  271.             print("Blocks mined="..collected)
  272.             print("Fuel level="..turtle.getFuelLevel())
  273.             return
  274.         end
  275.         -- start the next layer
  276.         tryDig()
  277.         if not turtle.forward() then
  278.             print("Aborting Tunnel!")
  279.             break
  280.         end
  281.         steps = steps + 1
  282.     end
  283. end
  284.  
  285. tunnelize(length)
Add Comment
Please, Sign In to add comment