Advertisement
ArsKvsh

[CC} tunnelfacing

Aug 4th, 2019
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.83 KB | None | 0 0
  1. if not turtle then
  2.     printError( "Requires a Turtle" )
  3.     return
  4. end
  5.  
  6. local tArgs = { ... }
  7. if #tArgs ~= 1 then
  8.     print( "Usage: tunface <length>" )
  9.     return
  10. end
  11.  
  12. -- Mine in a quarry pattern until we hit something we can't dig
  13. local length = tonumber( tArgs[1] )
  14.  
  15. if length < 1 then
  16.     print( "Facing length must be positive" )
  17.     return
  18. end
  19.  
  20. local depth = 0
  21. local item = 1
  22.  
  23. local function count()
  24.     length = length + 1
  25.     if math.fmod(length, 10) == 0 then
  26.         print( "Faced "..length.." section." )
  27.     end
  28. end
  29.  
  30. local function tryDig()
  31.     while turtle.detect() do
  32.         if turtle.dig() then
  33.             sleep(0.1)
  34.         else
  35.             return false
  36.         end
  37.     end
  38.     return true
  39. end
  40.  
  41. local function tryDigUp()
  42.     while turtle.detectUp() do
  43.         if turtle.digUp() then
  44.             sleep(0.1)
  45.         else
  46.             return false
  47.         end
  48.     end
  49.     return true
  50. end
  51.  
  52. local function tryDigDown()
  53.     while turtle.detectDown() do
  54.         if turtle.digDown() then
  55.             sleep(0.1)
  56.         else
  57.             return false
  58.         end
  59.     end
  60.     return true
  61. end
  62.  
  63. local function refuel()
  64.     local fuelLevel = turtle.getFuelLevel()
  65.     if fuelLevel == "unlimited" or fuelLevel > 0 then
  66.         return
  67.     end
  68.  
  69.     local function tryRefuel()
  70.         for n=1,16 do
  71.             if turtle.getItemCount(n) > 0 then
  72.                 turtle.select(n)
  73.                 if turtle.refuel(1) then
  74.                     turtle.select(1)
  75.                     return true
  76.                 end
  77.             end
  78.         end
  79.         turtle.select(1)
  80.         return false
  81.     end
  82.  
  83.     if not tryRefuel() then
  84.         print( "Add more fuel to continue." )
  85.         while not tryRefuel() do
  86.             os.pullEvent( "turtle_inventory" )
  87.         end
  88.         print( "Resuming Tunnel Facing." )
  89.     end
  90. end
  91.  
  92. local function charge()
  93.     local function tryCharge()
  94.         for n=1,16 do
  95.             if turtle.getItemCount(n) > 0 then
  96.                 if turtle.getItemDetail(n).name == "chisel:cobblestone2" then
  97.                     turtle.select(n)
  98.                     return true
  99.                 end
  100.             end
  101.         end
  102.         turtle.select(2)
  103.         return false
  104.     end
  105.     if not tryCharge() then
  106.         print( "Add more cobblestone to continue." )
  107.         while not tryCharge() do
  108.             os.pullEvent( "turtle_inventory" )
  109.         end
  110.         print( "Resuming Tunnel Facing." )
  111.     end
  112. end
  113.  
  114. local function chargeTorch()
  115.     local function tryChargeTorch()
  116.         for n=1,16 do
  117.             if turtle.getItemCount(n) > 0 then
  118.                 if turtle.getItemDetail(n).name == "tconstruct:stone_torch" then
  119.                     turtle.select(n)
  120.                     return true
  121.                 end
  122.             end
  123.         end
  124.         turtle.select(16)
  125.         return false
  126.     end
  127.     if not tryChargeTorch() then
  128.         print( "Add more torches to continue." )
  129.         while not tryChargeTorch() do
  130.             os.pullEvent( "turtle_inventory" )
  131.         end
  132.         print( "Resuming Tunnel Facing." )
  133.     end
  134. end
  135.  
  136. local function tryUp()
  137.     refuel()
  138.     while not turtle.up() do
  139.         if turtle.detectUp() then
  140.             if not tryDigUp() then
  141.                 return false
  142.             end
  143.         elseif turtle.attackUp() then
  144.         else
  145.             sleep( 0.1 )
  146.         end
  147.     end
  148.     return true
  149. end
  150.  
  151. local function tryBack()
  152.     refuel()
  153.     while not turtle.back() do
  154.             sleep( 0.1 )
  155.     end
  156.     return true
  157. end
  158.  
  159. local function tryDown()
  160.     refuel()
  161.     while not turtle.down() do
  162.         if turtle.detectDown() then
  163.             if not tryDigDown() then
  164.                 return false
  165.             end
  166.         elseif turtle.attackDown() then
  167.         else
  168.             sleep( 0.1 )
  169.         end
  170.     end
  171.     return true
  172. end
  173.  
  174. local function tryForward()
  175.     refuel()
  176.     while not turtle.forward() do
  177.         if turtle.detect() then
  178.             if not tryDig() then
  179.                 return false
  180.             end
  181.         elseif turtle.attack() then
  182.         else
  183.             sleep( 0.1 )
  184.         end
  185.     end
  186.     return true
  187. end
  188.  
  189. local function tryPlace()
  190.     refuel()
  191.     charge()
  192.     while not turtle.detect() do
  193.         if turtle.place() then
  194.             sleep(0.1)
  195.         else
  196.             return false
  197.         end
  198.     end
  199.     return true
  200. end
  201.  
  202. local function tryPlaceUp()
  203.     refuel()
  204.     charge()
  205.     while not turtle.detectUp() do
  206.         if turtle.placeUp() then
  207.             sleep(0.1)
  208.         else
  209.             return false
  210.         end
  211.     end
  212.     return true
  213. end
  214.  
  215. local function tryPlaceDown()
  216.     refuel()
  217.     charge()
  218.     while not turtle.detectDown() do
  219.         if turtle.placeDown() then
  220.             sleep(0.1)
  221.         else
  222.             return false
  223.         end
  224.     end
  225.     return true
  226. end
  227.  
  228. local function tryPlaceTorch()
  229.     refuel()
  230.     chargeTorch()
  231.     while not turtle.detectDown() do
  232.         if turtle.placeDown() then
  233.             sleep(0.1)
  234.         else
  235.             return false
  236.         end
  237.     end
  238.     return true
  239. end
  240.  
  241. print( "Facing the tunnel..." )
  242.  
  243. for n=1,length do
  244.     tryDig()
  245.     tryPlace()
  246.     tryUp()
  247.     tryDig()
  248.     tryPlace()
  249.     tryUp()
  250.     if math.fmod(n, 7) == 0 then
  251.         tryPlaceTorch()
  252.     end
  253.     tryDig()
  254.     tryPlace()
  255.  
  256.     turtle.turnRight()
  257.     turtle.turnRight()
  258.  
  259.     tryDigUp()
  260.     tryPlaceUp()
  261.     tryForward()
  262.     tryDigUp()
  263.     tryPlaceUp()
  264.     tryDigDown()
  265.     tryForward()
  266.     tryDigUp()
  267.     tryPlaceUp()
  268.  
  269.     tryDig()
  270.     tryPlace()
  271.     tryDown()
  272.     tryDig()
  273.     tryPlace()
  274.     tryDown()
  275.     tryDig()
  276.     tryPlace()
  277.  
  278.     turtle.turnRight()
  279.     turtle.turnRight()
  280.  
  281.     tryDigDown()
  282.     tryPlaceDown()
  283.     tryForward()
  284.     tryDigDown()
  285.     tryPlaceDown()
  286.     tryForward()
  287.     tryDigDown()
  288.     tryPlaceDown()
  289.  
  290.     turtle.turnRight()
  291.     tryForward()
  292.     turtle.turnLeft()
  293.  
  294.     if n == length then
  295.         print( "Tunnel facing complete." )
  296.     end
  297.  
  298. end
  299.  
  300. print( "Tunnel facing complete." )
  301. print( "Faced "..length.." segments total." )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement