Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Turtle AI with UI Dashboard
- -- Configuration
- local gpsFile = "/turtle_gps_data.json"
- -- Utility functions
- local function log(message)
- print("[Turtle AI] " .. 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("Turtle AI Dashboard")
- print("--------------------")
- print("Mode : " .. mode)
- print("Steps : " .. steps)
- print("Fuel : " .. fuel)
- if location then
- print(string.format("Location : (%.2f, %.2f, %.2f)", location.x, location.y, location.z))
- else
- print("Location : GPS unavailable")
- end
- print("--------------------")
- 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 < 100 then
- if turtle.refuel(1) then
- updateDashboard("Refueling", location, fuel, steps)
- else
- break
- end
- 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 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, stop")
- local command = read()
- if command == "mine" then
- navigateAndMine()
- elseif command == "explore" then
- exploreAndMap()
- elseif command == "stop" then
- break
- else
- print("Unknown command!")
- end
- end
- end
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement