Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Remote Turtle Script with Auto-Refuel and Logging
- -- Function to scan surrounding blocks
- local function scanSurroundings()
- local directions = {
- { action = "inspect", method = turtle.inspect, direction = "front" },
- { action = "inspectUp", method = turtle.inspectUp, direction = "up" },
- { action = "inspectDown", method = turtle.inspectDown, direction = "down" }
- }
- local scannedData = {}
- for _, dir in ipairs(directions) do
- local success, data = dir.method()
- if success then
- table.insert(scannedData, {
- direction = dir.direction,
- name = data.name,
- metadata = data.state or {}
- })
- else
- table.insert(scannedData, {
- direction = dir.direction,
- name = "air",
- metadata = {}
- })
- end
- end
- return scannedData
- end
- -- Function to refuel the turtle automatically
- local function autoRefuel()
- for slot = 1, 16 do
- local item = turtle.getItemDetail(slot)
- if item then
- turtle.select(slot)
- if turtle.refuel(0) then -- Check if the item is burnable
- print("Refueling with " .. item.name)
- turtle.refuel() -- Perform the refuel
- turtle.select(1) -- Reset to the first slot
- return true
- end
- end
- end
- turtle.select(1) -- Reset to the first slot if no fuel found
- return false
- end
- -- Function to log fuel and status to the turtle's console
- local function logStatus(action)
- term.clear()
- term.setCursorPos(1, 1)
- print("Turtle Status")
- print("-------------")
- local fuelLevel = turtle.getFuelLevel() or "N/A"
- local fuelLimit = turtle.getFuelLimit() or "N/A"
- print("Fuel Level: " .. fuelLevel)
- print("Fuel Limit: " .. fuelLimit)
- if fuelLevel ~= "N/A" and fuelLimit ~= "N/A" then
- local fuelPercent = math.floor((fuelLevel / fuelLimit) * 100)
- print("Fuel Percentage: " .. fuelPercent .. "%")
- -- Refuel if the fuel percentage is below 75%
- if fuelPercent < 75 then
- print("Fuel is below 75%, refueling...")
- turtle.refuel() -- Attempt to refuel
- fuelLevel = turtle.getFuelLevel() -- Get the updated fuel level
- fuelLimit = turtle.getFuelLimit() -- Get the updated fuel limit
- fuelPercent = math.floor((fuelLevel / fuelLimit) * 100)
- print("Fuel refueled. New Fuel Percentage: " .. fuelPercent .. "%")
- end
- else
- print("Fuel Percentage: N/A")
- end
- print("\nLast Action: " .. action)
- -- Call scanSurroundings and print the results
- local scanResults = scanSurroundings()
- print("\nBlocks Around Turtle:")
- for _, block in ipairs(scanResults) do
- print(block.direction:sub(1, 1):upper() .. block.direction:sub(2) .. ": " .. block.name)
- end
- end
- -- Function to handle incoming commands
- local function handleCommands()
- local modem = peripheral.find("modem")
- if not modem then
- error("No modem attached to the turtle!")
- end
- modem.open(23) -- Open the modem on channel 23
- logStatus("Turtle initialized")
- logStatus("Waiting for command...")
- while true do
- -- Listen for incoming commands
- local event, side, senderChannel, replyChannel, message = os.pullEvent("modem_message")
- local command = textutils.unserialize(message)
- if command.action == "scan" then
- -- Scan surrounding blocks and send results back
- local scanResults = scanSurroundings()
- modem.transmit(replyChannel, senderChannel, textutils.serialize({ success = true, blocks = scanResults }))
- logStatus("Scanned surroundings")
- elseif command.action == "move" then
- -- Dynamically call the appropriate turtle function
- local moveFunction = turtle[command.direction]
- if moveFunction and type(moveFunction) == "function" then
- local success = moveFunction() -- Call the function
- if success then
- logStatus("Moved " .. command.direction)
- end
- else
- logStatus("Invalid move direction: " .. tostring(command.direction))
- end
- modem.transmit(replyChannel, senderChannel, textutils.serialize({ success = success }))
- elseif command.action == "turn" then
- -- Turn the turtle based on the received direction
- if command.direction == "left" then
- turtle.turnLeft()
- logStatus("Turned left")
- elseif command.direction == "right" then
- turtle.turnRight()
- logStatus("Turned right")
- end
- modem.transmit(replyChannel, senderChannel, textutils.serialize({ success = true }))
- elseif command.action == "getFuel" then
- -- Send fuel info back to the computer
- local fuelLevel = turtle.getFuelLevel()
- local fuelLimit = turtle.getFuelLimit()
- modem.transmit(replyChannel, senderChannel, textutils.serialize({ fuel = fuelLevel, limit = fuelLimit }))
- logStatus("Sent fuel info")
- end
- end
- end
- -- Start listening for commands
- handleCommands()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement