MatthiGamer

Mining Turtle Tunnel 3x3 with Torches

May 25th, 2021 (edited)
773
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.93 KB | None | 0 0
  1. -- 3 x 3 Tunnel with torches
  2. local tArgs = {...}
  3. print("Checking inventory...")
  4.  
  5. if (#tArgs ~= 1) then
  6.     print("Program call: <mprogramName> <tunnelLength>")
  7.     return
  8. end
  9.  
  10. local tunnelLength = tonumber(tArgs[1])
  11. local distanceTravelled = 0
  12. local torchSlot = 1
  13. local hasTorches = false;
  14.  
  15. -- define functions
  16. local function EmptyInventory()
  17.     print("Emptying inventory...")
  18.     for i = 1, 16, 1 do
  19.         turtle.select(i)
  20.         turtle.drop()
  21.     end
  22.     print("Fuel left: "..turtle.getFuelLevel())
  23. end
  24.  
  25. local function ReturnHome()
  26.     print("Returning...")
  27.     turtle.turnLeft()
  28.     turtle.turnLeft()
  29.     for i = 0, distanceTravelled, 1 do
  30.         turtle.forward()
  31.     end
  32.     EmptyInventory()
  33. end
  34.  
  35. local function CheckForFuel()
  36.     local foundFuel = false
  37.     for i = 1, 16, 1 do
  38.         turtle.select(i);
  39.         if (turtle.refuel(0)) then
  40.             print("Found Fuel.")
  41.             foundFuel = true
  42.             print("Trying to refuel...");
  43.             if (turtle.getFuelLevel() <= turtle.getFuelLimit()) then
  44.                 turtle.refuel()
  45.                 print("Refueled succesfully.")
  46.             else
  47.                 print("Refueling failed. Fuel already full.")
  48.             end
  49.         end
  50.     end
  51.     turtle.select(1)
  52.     if (foundFuel == false) then
  53.         print("No fuel found!")
  54.     end
  55.     return foundFuel
  56. end
  57.  
  58. local function CheckForTorches()
  59.     hasTorches = false;
  60.     for i = 1, 16, 1 do
  61.         turtle.select(i)
  62.         if (turtle.getItemDetail()) then
  63.             if (turtle.getItemDetail().name == "minecraft:torch") then
  64.                 print("Found torches.")
  65.                 torchSlot = i
  66.                 hasTorches = true
  67.             end
  68.         end
  69.     end
  70.     if (hasTorches == false) then
  71.         print("No torches found!.")
  72.     end
  73.     turtle.select(1)
  74.     return hasTorches
  75. end
  76.  
  77. local function CheckTorchSlot(index)
  78.     turtle.select(index)
  79.     if (turtle.getItemDetail()) then
  80.         if (turtle.getItemDetail().name == "minecraft:torch") then
  81.             return
  82.         else
  83.             print("torchSlot out of torches. Searching for torches...")
  84.             CheckForTorches()
  85.         end
  86.     else
  87.         print("torchSlot out of torches. Searching for torches...")
  88.         CheckForTorches()
  89.     end
  90. end
  91.  
  92. local function CheckDistance()
  93.     local isReturning = false
  94.     if (turtle.getFuelLevel() <= distanceTravelled + 5) then
  95.         print("Fuel is low.")
  96.         if (CheckForFuel() == false) then
  97.             isReturning = true
  98.             print("Returning...")
  99.             ReturnHome()
  100.         end
  101.     end
  102.     return isReturning
  103. end
  104.  
  105. local function ClearRow(setTorch)
  106.     turtle.turnLeft()
  107.     turtle.dig()
  108.     turtle.turnLeft()
  109.     turtle.turnLeft()
  110.     turtle.dig()
  111.     if (setTorch == true) then
  112.         turtle.select(torchSlot)
  113.         if (turtle.getItemDetail()) then
  114.             if (turtle.getItemDetail().name == "minecraft:torch") then
  115.                 turtle.place()
  116.             end
  117.         end
  118.         turtle.select(1)
  119.     end
  120.     turtle.turnLeft()
  121. end
  122.    
  123. local function ClearBlock()
  124.     ClearRow(false)
  125.     turtle.digUp()
  126.     turtle.up()
  127.     if distanceTravelled % 6 == 0 then
  128.         ClearRow(true)
  129.     else
  130.         ClearRow(false)
  131.     end
  132.     turtle.digUp()
  133.     turtle.up()
  134.     ClearRow(false)
  135. end
  136.  
  137. local function ResetPosition()
  138.     turtle.down()
  139.     turtle.down()
  140.     CheckDistance()
  141. end
  142.  
  143. local function GoBackABlock()
  144.     turtle.turnLeft()
  145.     turtle.turnLeft()
  146.     turtle.forward()
  147.     turtle.turnLeft()
  148.     turtle.turnLeft()
  149. end
  150.  
  151. -- tunnel
  152.  
  153. if(CheckForFuel() == false and turtle.getFuelLevel() < 5) then
  154.     print("Fuel is needed!")
  155.     return
  156. end
  157.  
  158. if (CheckForTorches() == false) then
  159.     print("Torches are needed!")
  160.     return
  161. end
  162.  
  163. for i = 1, tunnelLength, 1 do
  164.     ClearBlock()
  165.     ResetPosition()
  166.     turtle.dig()
  167.     turtle.forward()
  168.     distanceTravelled = distanceTravelled + 1
  169. end
  170. GoBackABlock()
  171. ReturnHome()
Add Comment
Please, Sign In to add comment