Advertisement
squidingtin

trurtle bot test

Nov 25th, 2024 (edited)
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.02 KB | None | 0 0
  1. -- Helper function to get the fuel percentage
  2. local function getFuelPercentage()
  3.     local fuelLevel = turtle.getFuelLevel()
  4.     local fuelLimit = turtle.getFuelLimit()
  5.     if fuelLevel == "unlimited" or fuelLimit == "unlimited" then
  6.         return "Unlimited"
  7.     elseif fuelLimit > 0 then
  8.         return math.floor((fuelLevel / fuelLimit) * 100) .. "%"
  9.     else
  10.         return "N/A"
  11.     end
  12. end
  13.  
  14. -- Function to refuel the turtle
  15. local function autoRefuel()
  16.     for slot = 1, 16 do
  17.         local item = turtle.getItemDetail(slot)
  18.         if item then
  19.             -- Try to refuel with the item
  20.             turtle.select(slot)
  21.             if turtle.refuel(0) then -- Check if the item is burnable
  22.                 print("Refueling with " .. item.name)
  23.                 turtle.refuel() -- Perform the refuel
  24.                 break -- Stop after refueling
  25.             end
  26.         end
  27.     end
  28.     turtle.select(1) -- Reset to the first slot
  29. end
  30.  
  31. -- Helper function to inspect blocks
  32. local function inspectBlock(inspectFunc)
  33.     local success, data = inspectFunc()
  34.     if success then
  35.         return data.name or "Unknown Block"
  36.     else
  37.         return "Air/None"
  38.     end
  39. end
  40.  
  41. -- Main loop
  42. while true do
  43.     term.clear()
  44.     term.setCursorPos(1, 1)
  45.  
  46.     -- Print fuel info
  47.     print("Turtle Status Test")
  48.     print("------------------")
  49.     print("Fuel Level: " .. (turtle.getFuelLevel() or "N/A"))
  50.     print("Fuel Limit: " .. (turtle.getFuelLimit() or "N/A"))
  51.     print("Fuel Percentage: " .. getFuelPercentage())
  52.  
  53.     -- Refuel if needed
  54.     if turtle.getFuelLevel() < 100 then -- Refuel if fuel level is below 100
  55.         print("Fuel level low, attempting to refuel...")
  56.         autoRefuel()
  57.     end
  58.  
  59.     -- Inspect blocks around the turtle
  60.     print("\nBlocks Around Turtle:")
  61.     print("Front: " .. inspectBlock(turtle.inspect))
  62.     print("Above: " .. inspectBlock(turtle.inspectUp))
  63.     print("Below: " .. inspectBlock(turtle.inspectDown))
  64.  
  65.     -- Wait 5 seconds before refreshing
  66.     sleep(5)
  67. end
  68.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement