arbarr3

repeatDig

Nov 13th, 2014
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.82 KB | None | 0 0
  1. local args = { ... }
  2.  
  3. numArgs = table.getn(args)
  4.  
  5. TORCH_INTERVAL = 5
  6.  
  7. function fuel ()
  8.   if turtle.getFuelLevel() == 0 then
  9.     turtle.select(1)
  10.     turtle.refuel(1)
  11.   end
  12. end
  13.  
  14. function plantTunnelTorch ()
  15.   turtle.select(2)
  16.   turtle.placeDown()
  17. end
  18.  
  19. function goodDig ()
  20.   while turtle.detect() do
  21.     fuel()
  22.     turtle.dig()
  23.     sleep(0.5)
  24.   end
  25.   fuel()
  26. end
  27.  
  28. function goodDigUp ()
  29.   while turtle.detectUp() do
  30.     fuel()
  31.     turtle.digUp()
  32.     sleep(0.5)
  33.   end
  34.   fuel()
  35. end
  36.  
  37. function threeByThree ()
  38.   goodDigUp()
  39.   turtle.digDown()
  40.   fuel()
  41.   turtle.turnLeft()
  42.   goodDig()
  43.   turtle.forward()
  44.   goodDigUp()
  45.   turtle.digDown()
  46.   fuel()
  47.   turtle.back()
  48.   fuel()
  49.   turtle.turnRight()
  50.   fuel()
  51.   turtle.turnRight()
  52.   goodDig()
  53.   turtle.forward()
  54.   fuel()
  55.   goodDigUp()
  56.   turtle.digDown()
  57.   fuel()
  58.   turtle.back()
  59.   fuel()
  60.   turtle.turnLeft()
  61. end
  62.  
  63. function tunnel (len)
  64.   count = 0
  65.   torch = TORCH_INTERVAL
  66.   while count < len do
  67.     threeByThree()
  68.  
  69.     --[[Stairs
  70.     fuel()
  71.     turtle.up()
  72.     threeByThree()
  73.     turtle.up()
  74.     threeByThree()
  75.     turtle.down()
  76.     --EndStairs]]--
  77.    
  78.     if torch == TORCH_INTERVAL then
  79.       plantTunnelTorch()
  80.       torch = 0
  81.     end
  82.     goodDig()
  83.     turtle.forward()    
  84.     fuel()
  85.     count = count+1
  86.     torch = torch+1
  87.   end
  88. end
  89.  
  90. function leftOrRight (dire)
  91.   threeByThree()
  92.   goodDig()
  93.   turtle.forward()
  94.   threeByThree()
  95.   turtle.back()
  96.   fuel()
  97.   if dire == 'l' then
  98.     turtle.turnLeft()
  99.   elseif dire == 'r' then
  100.     turtle.turnRight()
  101.   else
  102.     term.write("Error, bad direction.")
  103.   end
  104. end
  105.  
  106. function swapDirection(dire)
  107.   if dire == 'l' then
  108.     return 'r'
  109.   elseif dire == 'r' then
  110.     return 'l'
  111.   else
  112.     term.write("Error, bad direction.")
  113.   end
  114. end
  115.  
  116. --================Main===============--
  117.  
  118. if numArgs == 1.0 then
  119.   digNum = tonumber(args[1])
  120.   tunnel(digNum)
  121. elseif numArgs == 2.0 or numArgs == 3.0 or numArgs == 4.0 then
  122.  
  123.   if numArgs > 2.0 then
  124.     intervalWidth = tonumber(args[3])
  125.   else
  126.     intervalWidth = TORCH_INTERVAL
  127.   end
  128.  
  129.   if numArgs == 4.0 then
  130.     cycle = tonumber(args[4])
  131.   else
  132.     cycle = 1
  133.   end
  134.  
  135.  
  136.   digNum = tonumber(args[1])
  137.   direction = args[2]
  138.   if direction ~= 'r' and direction ~= 'l' then
  139.     error("Second argument must be 'r' or 'l'")
  140.   end
  141.   while cycle > 0 do
  142.     tunnel(digNum)
  143.     leftOrRight(direction)
  144.     tunnel(intervalWidth)
  145.     leftOrRight(direction)
  146.     tunnel(digNum)
  147.     if cycle > 1 then
  148.       direction = swapDirection(direction)
  149.       leftOrRight(direction)
  150.       tunnel(intervalWidth)
  151.       leftOrRight(direction)
  152.       tunnel(digNum)
  153.       leftOrRight(direction)
  154.       tunnel(intervalWidth)
  155.       leftOrRight(direction)
  156.       tunnel(digNum)
  157.     end
  158.     cycle = cycle-1
  159.   end
  160. else
  161.   error("Usage: dig [int] opt[l | r]")
  162. end
Advertisement
Add Comment
Please, Sign In to add comment