Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- Doggy OS Turtle AI with Remote Control Feature (Turtle Side)
- -- Configuration
- local gpsFile = "/turtle_gps_data.json"
- local modemSide = "left" -- Change to the side your modem is on
- local gpsData = {}
- -- 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
- -- Initialize modem
- local function initModem()
- rednet.open(modemSide) -- Opens the modem for communication
- log("Modem initialized, waiting for remote control commands...")
- end
- -- Command handling
- local function handleCommand(command)
- if command == "mine" then
- navigateAndMine()
- elseif command == "explore" then
- exploreAndMap()
- elseif command == "stop" then
- log("Stopping Doggy OS Turtle. Returning to main menu.")
- return true -- Stop the turtle
- elseif command == "status" then
- local location = getGPSLocation()
- local fuel = turtle.getFuelLevel()
- -- Send back a status message
- rednet.send(senderID, string.format("Location: (%.2f, %.2f, %.2f) Fuel: %d", location.x, location.y, location.z, fuel))
- else
- log("Unknown command: " .. command)
- end
- return false
- end
- -- Main server loop
- local function main()
- initModem()
- while true do
- local senderID, message = rednet.receive() -- Receive command from the controller
- if message then
- log("Received command: " .. message)
- local stop = handleCommand(message)
- if stop then
- break -- Stop the turtle if the 'stop' command is received
- end
- end
- end
- end
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement