Maxwelljonez

MJZ LUA simple Tunneling x4

Oct 19th, 2021 (edited)
125
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. --{program="Tunneler",version="1.1",date="19.10.2021"}
  2. ---------------------------------------
  3. -- Arguements and Arg Checking
  4. local tArgs = { ... }
  5. if #tArgs ~= 1 then
  6.     print( "Usage: tunnel <length>" )
  7.     return
  8. end
  9. local length = tonumber( tArgs[1] )
  10. if length < 1 then
  11.     print( "Tunnel length must be positive" )
  12.     return
  13. end
  14.  
  15. -- Collected block number / Increaser
  16. local collected = 0
  17. local function collect()
  18.     collected = collected + 1
  19.     if math.fmod(collected, 25) == 0 then
  20.         print( "Mined "..collected.." blocks." )
  21.     end
  22. end
  23.  
  24. -- Digging functions
  25. local function tryDig()
  26.     while turtle.dig() do
  27.         collect()
  28.         if not turtle.detect() then
  29.             return true
  30.         end
  31.     end
  32.     return not turtle.detect()
  33. end
  34.  
  35. local function tryDigUp()
  36.     while turtle.digUp() do
  37.         collect()
  38.         if not turtle.detectUp() then
  39.             return true
  40.         end
  41.     end
  42.     return not turtle.detectUp()
  43. end
  44.  
  45. -- Functions for dropping off items into a chest from slot 1.
  46. local function depositChest()
  47.     turtle.up()
  48.     turtle.turnRight()
  49.     turtle.forward()
  50.     turtle.select(1)
  51.     turtle.placeDown()
  52.     for slot=2,16 do
  53.         turtle.select(slot)
  54.         turtle.dropDown(turtle.getItemCount(slot))
  55.     end
  56.     turtle.select(1)
  57. end
  58.  
  59. local function previousState()
  60.     turtle.turnLeft()
  61.     turtle.turnLeft()
  62.     turtle.forward()
  63.     turtle.down()
  64.     turtle.turnRight()
  65. end
  66.  
  67. local function checkSpace()
  68.     local space = 0
  69.     for slot=2,16 do
  70.         space = space + turtle.getItemSpace(slot)
  71.     end
  72.     if space == 0 then
  73.         depositChest()
  74.         previousState()
  75.         return false
  76.     else
  77.         return true
  78.     end
  79. end
  80.  
  81. -- Main Program for digging the tunnel
  82. write( "Tunnelling 4 block high" )
  83. for n=1,length do
  84.     write(".")
  85.     turtle.select(2)
  86.     turtle.placeDown()
  87.     if not checkSpace() then break end
  88.     tryDigUp()
  89.     turtle.turnLeft()
  90.     tryDig()
  91.     turtle.up()
  92.     tryDig()
  93.     tryDigUp()
  94.     turtle.up()
  95.     tryDig()
  96.     tryDigUp()
  97.     turtle.up()
  98.     tryDig()   
  99.     turtle.turnRight()
  100.     turtle.turnRight()
  101.     tryDig()
  102.     turtle.down()
  103.     tryDig()
  104.     turtle.down()
  105.     tryDig()
  106.     turtle.down()
  107.     tryDig()
  108.     turtle.turnLeft()
  109.    
  110.     if n<length then
  111.         tryDig()
  112.         if not turtle.forward() then
  113.             print( "Aborting Tunnel." )
  114.             break
  115.         end
  116.     else
  117.         print( "Tunnel complete." )
  118.     end
  119. end
RAW Paste Data Copied