Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Doggy OS Turtle AI with UI Dashboard and Refuel Option (No Auto-Refuel, Refuel at 0)
- -- Configuration
- local gpsFile = "/turtle_gps_data.json"
- -- Utility functions
- local function log(message)
- print("[Doggy OS Turtle] " .. message)
- end
- local function isBlocked()
- return turtle.detect()
- end
- local function tryMoveForward()
- if isBlocked() then
- if turtle.dig() then
- return turtle.forward()
- else
- return false
- end
- else
- return turtle.forward()
- end
- end
- local function tryTurn(direction)
- if direction == "left" then
- turtle.turnLeft()
- elseif direction == "right" then
- turtle.turnRight()
- end
- end
- local function saveGPSData(data)
- local file = fs.open(gpsFile, "w")
- file.write(textutils.serializeJSON(data))
- file.close()
- end
- local function loadGPSData()
- if fs.exists(gpsFile) then
- local file = fs.open(gpsFile, "r")
- local data = textutils.unserializeJSON(file.readAll())
- file.close()
- return data
- else
- return {}
- end
- end
- local function getGPSLocation()
- local x, y, z = gps.locate(5) -- 5-second timeout
- if x and y and z then
- return { x = x, y = y, z = z }
- else
- return nil
- end
- end
- local function updateDashboard(mode, location, fuel, steps)
- term.clear()
- term.setCursorPos(1, 1)
- print("Doggy OS Turtle Dashboard")
- -- Dynamic number of dashes based on screen width
- local screenWidth, _ = term.getSize()
- local dashLine = string.rep("-", screenWidth)
- print(dashLine)
- print("Mode : " .. mode)
- print("Steps : " .. steps)
- -- Fuel level indicator with blinking colors based on fuel
- if fuel == 0 then
- term.setTextColor(colors.red) -- Red for out of fuel
- print("Fuel : 0 (Out of Fuel)")
- elseif fuel < 20 then
- term.setTextColor(colors.red) -- Red for critical fuel level
- print("Fuel : " .. fuel .. " (Critical)")
- elseif fuel < 60 then
- term.setTextColor(colors.orange) -- Orange for low fuel level
- print("Fuel : " .. fuel .. " (Low)")
- else
- term.setTextColor(colors.green) -- Green for sufficient fuel
- print("Fuel : " .. fuel)
- end
- -- Reset to default text color
- term.setTextColor(colors.white)
- if location then
- print(string.format("Location : (%.2f, %.2f, %.2f)", location.x, location.y, location.z))
- else
- print("Location : GPS unavailable")
- end
- print(dashLine)
- end
- local function refuel()
- local fuel = turtle.getFuelLevel()
- if fuel == 0 then
- log("Fuel level is 0. Please refuel immediately!")
- -- Check for fuel in the Turtle's inventory
- for slot = 1, 16 do
- turtle.select(slot)
- if turtle.getItemCount() > 0 then
- local item = turtle.getItemDetail()
- if item.name == "minecraft:coal" or item.name == "minecraft:charcoal" then
- if turtle.refuel(1) then
- log("Refueled with " .. item.name)
- return true
- end
- end
- end
- end
- log("No fuel available in inventory. Stopping.")
- end
- return false
- end
- -- Core AI logic
- local function navigateAndMine()
- local steps = 0
- local gpsData = loadGPSData()
- while true do
- local location = getGPSLocation()
- local fuel = turtle.getFuelLevel()
- updateDashboard("Mining", location, fuel, steps)
- if fuel == 0 then
- log("Out of fuel! Stopping Doggy OS Turtle and returning to main menu.")
- break -- Stop movement and break out of the loop to return to main menu
- end
- if location then
- gpsData[tostring(os.clock())] = location
- saveGPSData(gpsData)
- end
- if tryMoveForward() then
- steps = steps + 1
- else
- tryTurn("right")
- end
- sleep(0.5)
- end
- end
- local function exploreAndMap()
- local gpsData = loadGPSData()
- local steps = 0
- while true do
- local location = getGPSLocation()
- local fuel = turtle.getFuelLevel()
- updateDashboard("Exploring", location, fuel, steps)
- if fuel == 0 then
- log("Out of fuel! Stopping Doggy OS Turtle and returning to main menu.")
- break -- Stop movement and break out of the loop to return to main menu
- end
- if location then
- gpsData[tostring(os.clock())] = location
- saveGPSData(gpsData)
- end
- if not tryMoveForward() then
- tryTurn("right")
- end
- steps = steps + 1
- sleep(0.5)
- end
- end
- -- Main program
- local function main()
- while true do
- updateDashboard("Idle", nil, turtle.getFuelLevel(), 0)
- print("Commands: mine, explore, refuel, stop")
- local command = read()
- if command == "mine" then
- navigateAndMine()
- elseif command == "explore" then
- exploreAndMap()
- elseif command == "refuel" then
- if refuel() then
- updateDashboard("Idle", getGPSLocation(), turtle.getFuelLevel(), 0)
- end
- elseif command == "stop" then
- log("Stopping Doggy OS Turtle. Returning to main menu.")
- break
- else
- print("Unknown command!")
- end
- end
- end
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement