Advertisement
arbarr3

Dig

Feb 4th, 2023 (edited)
1,219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.28 KB | None | 0 0
  1. local args = { ... }
  2.  
  3. numArgs = table.getn(args)
  4.  
  5. FUEL_INV_LOC = 1
  6. FUEL_REFUEL_AMMT = 1
  7. FUEL_LOWER_BOUND = 0
  8. TORCH_INV_LOC = 2
  9. TORCH_INTERVAL = 5
  10.  
  11. function fuel ()
  12.   if turtle.getFuelLevel() == FUEL_LOWER_BOUND then
  13.     turtle.select(FUEL_INV_LOC)
  14.     turtle.refuel(FUEL_REFUEL_AMMT)
  15.   end
  16. end
  17.  
  18. function plantTunnelTorch ()
  19.   turtle.select(TORCH_INV_LOC)
  20.   turtle.placeDown()
  21. end
  22.  
  23. function goodDig ()
  24.   while turtle.detect() do
  25.     fuel()
  26.     turtle.dig()
  27.   end
  28.   fuel()
  29. end
  30.  
  31. function goodDigUp ()
  32.   while turtle.detectUp() do
  33.     fuel()
  34.     turtle.digUp()
  35.     sleep(0.5)
  36.   end
  37.   fuel()
  38. end
  39.  
  40. function clearUpDown ()
  41.   goodDigUp()
  42.   turtle.digDown()
  43.   fuel()
  44. end
  45.  
  46. function tunnel (len, toTorch)
  47.   count = 0
  48.   torch = TORCH_INTERVAL
  49.   while count < len do
  50.     clearUpDown()
  51.    
  52.     if torch == TORCH_INTERVAL and toTorch == true then
  53.       plantTunnelTorch()
  54.       torch = 0
  55.     end
  56.     goodDig()
  57.     turtle.forward()
  58.     clearUpDown()
  59.     fuel()
  60.     count = count+1
  61.     torch = torch+1
  62.   end
  63. end
  64.  
  65. function leftOrRight (dire)
  66.   if dire == 'l' then
  67.     turtle.turnLeft()
  68.   elseif dire == 'r' then
  69.     turtle.turnRight()
  70.   else
  71.     term.write("Error, bad direction.")
  72.   end
  73. end
  74.  
  75. function swapDirection (dire)
  76.   if dire == 'l' then
  77.     return 'r'
  78.   elseif dire == 'r' then
  79.     return 'l'
  80.   end
  81. end
  82.  
  83. --================Main===============--
  84. digNum = tonumber(args[1])
  85.  
  86. if numArgs == 1.0 then
  87.   tunnel(digNum, true)
  88. elseif numArgs == 4.0 then
  89.   interval = tonumber(args[2])
  90.   direction = args[3]
  91.   if direction ~= 'r' and direction ~= 'l' then
  92.     error("Third argument must be 'r' or 'l'")
  93.   end
  94.   repeatNum = tonumber(args[4])
  95.  
  96.   if interval > 1 then
  97.     dropTorch = true
  98.   else
  99.     dropTorch = false
  100.   end
  101.  
  102.   while repeatNum > 0 do
  103.     tunnel(digNum, true)
  104.     repeatNum = repeatNum - 1
  105.     if repeatNum > 0 then
  106.       leftOrRight(direction)
  107.       tunnel(interval, true)
  108.       leftOrRight(direction)
  109.       tunnel(digNum, dropTorch)
  110.       repeatNum = repeatNum - 1
  111.       direction = swapDirection(direction)
  112.       leftOrRight(direction)
  113.       tunnel(interval, true)
  114.       leftOrRight(direction)
  115.       direction = swapDirection(direction)
  116.     end
  117.   end
  118.  
  119. else
  120.   error("Usage: dig [int:dist] [int:interval] opt[l | r] [int:times]")
  121. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement