Advertisement
Nihyo

Digmode test - Mining Turtle

May 19th, 2024 (edited)
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.63 KB | Gaming | 0 0
  1. -- Define starting position (replace with your coordinates)
  2. local startX, startY, startZ = 0, 0, 0
  3.  
  4. -- Ask for excavation distance
  5. local distance = redstone.getInput("Distance?")
  6.  
  7. -- Function to check if inventory is full
  8. function isInventoryFull()
  9.   for slot = 2, #turtle.getItemCount() do
  10.     if turtle.getItemCount(slot) == 0 then
  11.       return false
  12.     end
  13.   end
  14.   return true
  15. end
  16.  
  17. -- Function to deposit inventory (except coal)
  18. function depositInventory()
  19.   for slot = 2, #turtle.getItemCount() do
  20.     turtle.suckUp(slot)
  21.   end
  22. end
  23.  
  24. -- Function to refuel
  25. function refuelTurtle()
  26.   local chest = turtle.getFront()
  27.   if chest.getInventory() then
  28.     local coalSlot = chest.getItemSlot("coal")
  29.     if coalSlot then
  30.       turtle.suckUpFromSlot(chest, coalSlot)
  31.       turtle.selectSlot(1)
  32.       turtle.insertItem(1)
  33.     end
  34.   end
  35. end
  36.  
  37. -- Main program loop
  38. while distance > 0 do
  39.   -- Check fuel before digging
  40.   local fuelLevel = turtle.getItemCount(1)
  41.   if fuelLevel <= 0 then
  42.     refuelTurtle()
  43.     if turtle.getItemCount(1) == 0 then
  44.       break -- Pause program if no fuel available
  45.     end
  46.   end
  47.  
  48.   -- Dig and move forward
  49.   turtle.dig()
  50.   turtle.forward()
  51.   distance = distance - 1
  52.  
  53.   -- Check for completion or full inventory
  54.   if distance == 0 then
  55.     turtle.back() -- Return one block after completion
  56.   elseif isInventoryFull() then
  57.     turtle.goto(startX, startY, startZ) -- Return to starting point
  58.     depositInventory()
  59.   end
  60. end
  61.  
  62. -- Print message upon program completion/pause
  63. if distance == 0 then
  64.   print("Excavation complete!")
  65. else
  66.   print("Refueling required. Program paused.")
  67. end
  68.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement