Advertisement
squidingtin

trurtle

Nov 25th, 2024 (edited)
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.62 KB | None | 0 0
  1. -- Remote Turtle Script with Auto-Refuel and Logging
  2.  
  3. -- Function to scan surrounding blocks
  4. local function scanSurroundings()
  5.     local directions = {
  6.         { action = "inspect", method = turtle.inspect, direction = "front" },
  7.         { action = "inspectUp", method = turtle.inspectUp, direction = "up" },
  8.         { action = "inspectDown", method = turtle.inspectDown, direction = "down" }
  9.     }
  10.  
  11.     local scannedData = {}
  12.  
  13.     for _, dir in ipairs(directions) do
  14.         local success, data = dir.method()
  15.         if success then
  16.             table.insert(scannedData, {
  17.                 direction = dir.direction,
  18.                 name = data.name,
  19.                 metadata = data.state or {}
  20.             })
  21.         else
  22.             table.insert(scannedData, {
  23.                 direction = dir.direction,
  24.                 name = "air",
  25.                 metadata = {}
  26.             })
  27.         end
  28.     end
  29.  
  30.     return scannedData
  31. end
  32.  
  33. -- Function to refuel the turtle automatically
  34. local function autoRefuel()
  35.     for slot = 1, 16 do
  36.         local item = turtle.getItemDetail(slot)
  37.         if item then
  38.             turtle.select(slot)
  39.             if turtle.refuel(0) then -- Check if the item is burnable
  40.                 print("Refueling with " .. item.name)
  41.                 turtle.refuel() -- Perform the refuel
  42.                 turtle.select(1) -- Reset to the first slot
  43.                 return true
  44.             end
  45.         end
  46.     end
  47.     turtle.select(1) -- Reset to the first slot if no fuel found
  48.     return false
  49. end
  50.  
  51. -- Function to log fuel and status to the turtle's console
  52. local function logStatus(action)
  53.     term.clear()
  54.     term.setCursorPos(1, 1)
  55.     print("Turtle Status")
  56.     print("-------------")
  57.     local fuelLevel = turtle.getFuelLevel() or "N/A"
  58.     local fuelLimit = turtle.getFuelLimit() or "N/A"
  59.     print("Fuel Level: " .. fuelLevel)
  60.     print("Fuel Limit: " .. fuelLimit)
  61.    
  62.     if fuelLevel ~= "N/A" and fuelLimit ~= "N/A" then
  63.         local fuelPercent = math.floor((fuelLevel / fuelLimit) * 100)
  64.         print("Fuel Percentage: " .. fuelPercent .. "%")
  65.        
  66.         -- Refuel if the fuel percentage is below 75%
  67.         if fuelPercent < 75 then
  68.             print("Fuel is below 75%, refueling...")
  69.             turtle.refuel()  -- Attempt to refuel
  70.             fuelLevel = turtle.getFuelLevel()  -- Get the updated fuel level
  71.             fuelLimit = turtle.getFuelLimit()  -- Get the updated fuel limit
  72.             fuelPercent = math.floor((fuelLevel / fuelLimit) * 100)
  73.             print("Fuel refueled. New Fuel Percentage: " .. fuelPercent .. "%")
  74.         end
  75.     else
  76.         print("Fuel Percentage: N/A")
  77.     end
  78.  
  79.     print("\nLast Action: " .. action)
  80.  
  81.     -- Call scanSurroundings and print the results
  82.     local scanResults = scanSurroundings()
  83.     print("\nBlocks Around Turtle:")
  84.     for _, block in ipairs(scanResults) do
  85.         print(block.direction:sub(1, 1):upper() .. block.direction:sub(2) .. ": " .. block.name)
  86.     end
  87. end
  88.  
  89.  
  90. -- Function to handle incoming commands
  91. local function handleCommands()
  92.     local modem = peripheral.find("modem")
  93.     if not modem then
  94.         error("No modem attached to the turtle!")
  95.     end
  96.  
  97.     modem.open(23)  -- Open the modem on channel 23
  98.     logStatus("Turtle initialized")
  99.         logStatus("Waiting for command...")
  100.  
  101.     while true do
  102.  
  103.         -- Listen for incoming commands
  104.         local event, side, senderChannel, replyChannel, message = os.pullEvent("modem_message")
  105.         local command = textutils.unserialize(message)
  106.  
  107.         if command.action == "scan" then
  108.             -- Scan surrounding blocks and send results back
  109.             local scanResults = scanSurroundings()
  110.             modem.transmit(replyChannel, senderChannel, textutils.serialize({ success = true, blocks = scanResults }))
  111.             logStatus("Scanned surroundings")
  112.         elseif command.action == "move" then
  113.             -- Dynamically call the appropriate turtle function
  114.             local moveFunction = turtle[command.direction]
  115.            
  116.             if moveFunction and type(moveFunction) == "function" then
  117.                 local success = moveFunction() -- Call the function
  118.                 if success then
  119.                     logStatus("Moved " .. command.direction)
  120.                 end
  121.             else
  122.                 logStatus("Invalid move direction: " .. tostring(command.direction))
  123.             end
  124.             modem.transmit(replyChannel, senderChannel, textutils.serialize({ success = success }))
  125.         elseif command.action == "turn" then
  126.             -- Turn the turtle based on the received direction
  127.             if command.direction == "left" then
  128.                 turtle.turnLeft()
  129.                 logStatus("Turned left")
  130.             elseif command.direction == "right" then
  131.                 turtle.turnRight()
  132.                 logStatus("Turned right")
  133.             end
  134.             modem.transmit(replyChannel, senderChannel, textutils.serialize({ success = true }))
  135.         elseif command.action == "getFuel" then
  136.             -- Send fuel info back to the computer
  137.             local fuelLevel = turtle.getFuelLevel()
  138.             local fuelLimit = turtle.getFuelLimit()
  139.             modem.transmit(replyChannel, senderChannel, textutils.serialize({ fuel = fuelLevel, limit = fuelLimit }))
  140.             logStatus("Sent fuel info")
  141.         end
  142.     end
  143. end
  144.  
  145. -- Start listening for commands
  146. handleCommands()
  147.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement