Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Doggy OS Turtle AI with Settings, GPS Display, Password Protection, and Update Script
- -- Configuration
- local gpsFile = "/turtle_gps_data.json"
- local settingsFile = "/turtle_settings.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
- -- Ensure that settings file exists or create with default values
- local function ensureSettingsExist()
- if not fs.exists(settingsFile) then
- -- Create the settings file with default values if it doesn't exist
- local defaultSettings = { password = nil, gpsEnabled = true } -- Default GPS enabled
- local file = fs.open(settingsFile, "w")
- file.write(textutils.serializeJSON(defaultSettings))
- file.close()
- end
- end
- local function loadSettings()
- ensureSettingsExist() -- Ensure settings file exists
- local file = fs.open(settingsFile, "r")
- local data = textutils.unserializeJSON(file.readAll())
- file.close()
- return data
- end
- local function saveSettings(settings)
- local file = fs.open(settingsFile, "w")
- file.write(textutils.serializeJSON(settings))
- file.close()
- end
- local function getGPSLocation()
- local gpsSettings = loadSettings()
- if gpsSettings.gpsEnabled then
- 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
- else
- return nil
- end
- end
- local function updateDashboard(mode, location, fuel, steps)
- os.pullEvent = os.pullEventRaw
- 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
- -- If GPS is disabled, print "GPS Disabled"
- if location == nil and not loadSettings().gpsEnabled then
- print("Location : GPS Disabled")
- else
- print("Location : GPS unavailable")
- end
- 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
- -- Settings Page
- local function settingsPage()
- local settings = loadSettings()
- local gpsData = loadGPSData()
- term.clear()
- term.setCursorPos(1, 1)
- print("Doggy OS Turtle - Settings")
- print("---------------------------")
- print("1. Set Password")
- print("2. Remove Password")
- print("3. Clear GPS Data")
- print("4. View GPS Data")
- print("5. Enable/Disable GPS")
- print("6. Run Update Script")
- print("7. Back to Main Menu")
- print("---------------------------")
- local option = read()
- if option == "1" then
- print("Enter new password:")
- local newPassword = read("*")
- settings.password = newPassword
- saveSettings(settings)
- log("Password set.")
- elseif option == "2" then
- settings.password = nil
- saveSettings(settings)
- log("Password removed.")
- elseif option == "3" then
- fs.delete(gpsFile)
- log("GPS data cleared.")
- elseif option == "4" then
- print("GPS Data:")
- if next(gpsData) == nil then
- print("No GPS data available.")
- else
- for timestamp, location in pairs(gpsData) do
- print(string.format("Time: %.2f | X: %.2f | Y: %.2f | Z: %.2f", tonumber(timestamp), location.x, location.y, location.z))
- end
- end
- print("Press Enter to go back...")
- read()
- elseif option == "5" then
- -- Toggle GPS
- settings.gpsEnabled = not settings.gpsEnabled
- if settings.gpsEnabled then
- log("GPS is now enabled.")
- else
- log("GPS is now disabled.")
- end
- saveSettings(settings)
- elseif option == "6" then
- -- Run the update script from Pastebin
- log("Running update script from Pastebin...")
- local pastebinURL = "https://pastebin.com/raw/FQZUNr1j"
- local response = http.get(pastebinURL)
- if response then
- local updateScript = response.readAll()
- loadstring(updateScript)()
- log("Update completed successfully.")
- else
- log("Failed to download update script.")
- end
- elseif option == "7" then
- return
- else
- log("Invalid option!")
- end
- settingsPage() -- Recursively call to allow more changes
- end
- -- Password Protection
- local function passwordProtection()
- local settings = loadSettings()
- if settings.password then
- os.pullEvent = os.pullEventRaw
- term.clear()
- term.setCursorPos(1, 1)
- print("Enter password to unlock:")
- local input = read("*")
- if input == settings.password then
- return true
- else
- log("Incorrect password!")
- sleep(1)
- os.reboot()
- return false
- end
- end
- return true
- end
- -- Main program
- local function main()
- while true do
- if not passwordProtection() then
- return
- end
- updateDashboard("Idle", nil, turtle.getFuelLevel(), 0)
- print("Commands: mine, explore, refuel, settings, stop")
- local command = read()
- if command == "mine" then
- navigateAndMine()
- elseif command == "explore" then
- exploreAndMap()
- elseif command == "refuel" then
- refuel()
- elseif command == "settings" then
- settingsPage()
- elseif command == "stop" then
- log("Stopping the Turtle.")
- os.shutdown()
- else
- log("Invalid command.")
- end
- end
- end
- -- Start the main program
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement