Advertisement
civilwargeeky

Claude's Torch Helper

Sep 15th, 2024 (edited)
286
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.28 KB | None | 0 0
  1. -- Configuration
  2. local TORCH_INTERVAL = 5  -- Place a torch every X blocks
  3.  
  4. -- Function to refuel the turtle
  5. local function refuel()
  6.     if turtle.getFuelLevel() < 500 then
  7.         print("Fuel level low. Please insert fuel items.")
  8.         while turtle.getFuelLevel() < 1000 do
  9.             print("Current fuel level: " .. turtle.getFuelLevel())
  10.             os.pullEvent("turtle_inventory")
  11.             for i = 1, 16 do
  12.                 turtle.select(i)
  13.                 if turtle.refuel(1) then
  14.                     print("Refueled. New fuel level: " .. turtle.getFuelLevel())
  15.                     break
  16.                 end
  17.             end
  18.         end
  19.     end
  20.     print("Fuel level sufficient. Continuing...")
  21. end
  22.  
  23. -- Function to check if an item is a torch
  24. local function isTorch(item)
  25.     return item and string.find(item.name:lower(), "torch")
  26. end
  27.  
  28. -- Function to check for torches and filler blocks
  29. local function checkInventory()
  30.     local hasTorches, hasFillerBlocks = false, false
  31.    
  32.     while not (hasTorches and hasFillerBlocks) do
  33.         print("Please insert torches in slot 1 and filler blocks (non-torch items) in slot 2.")
  34.         os.pullEvent("turtle_inventory")
  35.        
  36.         turtle.select(1)
  37.         local torchItem = turtle.getItemDetail()
  38.         if isTorch(torchItem) then
  39.             hasTorches = true
  40.         else
  41.             print("Slot 1 does not contain torches. Please add torches to slot 1.")
  42.         end
  43.        
  44.         turtle.select(2)
  45.         local fillerItem = turtle.getItemDetail()
  46.         if fillerItem and not isTorch(fillerItem) then
  47.             hasFillerBlocks = true
  48.         else
  49.             if isTorch(fillerItem) then
  50.                 print("Slot 2 contains torches. Please add non-torch items as filler blocks to slot 2.")
  51.             else
  52.                 print("Slot 2 is empty. Please add filler blocks to slot 2.")
  53.             end
  54.         end
  55.     end
  56.     print("Inventory ready. Starting torch placement...")
  57. end
  58.  
  59. -- Function to place a torch
  60. local function placeTorch()
  61.     -- Wait until there's no block in front
  62.     while turtle.detect() do
  63.         print("Block detected in front. Waiting 2 seconds...")
  64.         os.sleep(2)
  65.     end
  66.  
  67.     turtle.select(1)
  68.     if not turtle.placeUp() then
  69.         print("Failed to place torch. Attempting to place filler block...")
  70.         if turtle.up() then
  71.             turtle.turnRight()
  72.             turtle.select(2)
  73.             if turtle.place() then
  74.                 turtle.turnLeft()
  75.                 turtle.down()
  76.                 turtle.select(1)
  77.                 if not turtle.placeUp() then
  78.                     print("Failed to place torch even after placing filler block.")
  79.                     return false
  80.                 end
  81.             else
  82.                 print("Failed to place filler block. Returning to original position.")
  83.                 turtle.turnLeft()
  84.                 turtle.down()
  85.                 return false
  86.             end
  87.         else
  88.             print("Failed to move up to place filler block. Cannot place torch here.")
  89.             return false
  90.         end
  91.     end
  92.     return true
  93. end
  94.  
  95. -- Main function
  96. local function main()
  97.     refuel()
  98.     checkInventory()
  99.    
  100.     local blocksMoved = 0
  101.    
  102.     while true do
  103.         if not turtle.forward() then
  104.             print("Obstacle detected. Waiting 1 second...")
  105.             os.sleep(1)
  106.         else
  107.             blocksMoved = blocksMoved + 1
  108.            
  109.             if blocksMoved % TORCH_INTERVAL == 0 then
  110.                 if not placeTorch() then
  111.                     -- Check for more torches in inventory
  112.                     local foundTorches = false
  113.                     for i = 2, 16 do
  114.                         turtle.select(i)
  115.                         local item = turtle.getItemDetail()
  116.                         if isTorch(item) then
  117.                             turtle.transferTo(1)
  118.                             foundTorches = true
  119.                             break
  120.                         end
  121.                     end
  122.                    
  123.                     if not foundTorches then
  124.                         print("Out of torches. Terminating program.")
  125.                         return
  126.                     end
  127.                 end
  128.             end
  129.         end
  130.     end
  131. end
  132.  
  133. -- Run the program
  134. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement